1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.permissioncontroller.safetycenter.ui; 18 19 import android.os.Build; 20 import android.os.Bundle; 21 22 import androidx.annotation.RequiresApi; 23 import androidx.lifecycle.ViewModelProvider; 24 import androidx.preference.PreferenceFragmentCompat; 25 26 import com.android.permissioncontroller.R; 27 import com.android.permissioncontroller.safetycenter.ui.model.PrivacyControlsViewModel; 28 import com.android.permissioncontroller.safetycenter.ui.model.PrivacyControlsViewModel.Pref; 29 import com.android.permissioncontroller.safetycenter.ui.model.PrivacyControlsViewModel.PrefState; 30 import com.android.permissioncontroller.safetycenter.ui.model.PrivacyControlsViewModelFactory; 31 32 import java.util.Map; 33 34 /** Fragment that shows several privacy toggle controls, alongside a link to location settings */ 35 @RequiresApi(Build.VERSION_CODES.TIRAMISU) 36 public final class PrivacyControlsFragment extends PreferenceFragmentCompat { 37 38 /** Create a new instance of this fragment */ newInstance()39 public static PrivacyControlsFragment newInstance() { 40 return new PrivacyControlsFragment(); 41 } 42 43 private PrivacyControlsViewModel mViewModel; 44 private String mRootKey; 45 private boolean mPrefsSet; 46 47 @Override onCreatePreferences(Bundle bundle, String rootKey)48 public void onCreatePreferences(Bundle bundle, String rootKey) { 49 mRootKey = rootKey; 50 mPrefsSet = false; 51 52 PrivacyControlsViewModelFactory factory = 53 new PrivacyControlsViewModelFactory(getActivity().getApplication()); 54 mViewModel = new ViewModelProvider(this, factory).get(PrivacyControlsViewModel.class); 55 mViewModel.getControlStateLiveData().observe(this, this::setPreferences); 56 } 57 setPreferences(Map<Pref, PrefState> prefStates)58 private void setPreferences(Map<Pref, PrefState> prefStates) { 59 // Delaying setting of preferences, in order to avoid disabled prefs being briefly visible 60 if (!mPrefsSet) { 61 setPreferencesFromResource(R.xml.privacy_controls, mRootKey); 62 mPrefsSet = true; 63 } 64 65 setSwitchPreference(prefStates, Pref.MIC); 66 setSwitchPreference(prefStates, Pref.CAMERA); 67 setSwitchPreference(prefStates, Pref.CLIPBOARD); 68 setSwitchPreference(prefStates, Pref.SHOW_PASSWORD); 69 70 findPreference(Pref.LOCATION.getKey()) 71 .setOnPreferenceClickListener( 72 (v) -> { 73 mViewModel.handlePrefClick(this, Pref.LOCATION, null); 74 return true; 75 }); 76 } 77 setSwitchPreference(Map<Pref, PrefState> prefStates, Pref prefType)78 private void setSwitchPreference(Map<Pref, PrefState> prefStates, Pref prefType) { 79 ClickableDisabledSwitchPreference preference = findPreference(prefType.getKey()); 80 preference.setupState(prefStates.get(prefType), prefType, mViewModel, this); 81 } 82 } 83