1 /*
2  * Copyright (C) 2021 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.settings.privacy;
18 
19 import static android.hardware.SensorPrivacyManager.Sources.SETTINGS;
20 
21 import android.content.Context;
22 
23 import androidx.preference.PreferenceScreen;
24 
25 import com.android.settings.R;
26 import com.android.settings.core.TogglePreferenceController;
27 import com.android.settings.utils.SensorPrivacyManagerHelper;
28 import com.android.settingslib.RestrictedLockUtilsInternal;
29 import com.android.settingslib.RestrictedSwitchPreference;
30 
31 import java.util.concurrent.Executor;
32 
33 /**
34  * Base class for sensor toggle controllers
35  */
36 public abstract class SensorToggleController extends TogglePreferenceController {
37 
38     protected final SensorPrivacyManagerHelper mSensorPrivacyManagerHelper;
39     private final Executor mCallbackExecutor;
40 
SensorToggleController(Context context, String preferenceKey)41     public SensorToggleController(Context context, String preferenceKey) {
42         super(context, preferenceKey);
43         mSensorPrivacyManagerHelper = SensorPrivacyManagerHelper.getInstance(context);
44         mCallbackExecutor = context.getMainExecutor();
45     }
46 
47     /**
48      * The sensor id, defined in SensorPrivacyManagerHelper, which an implementing class controls
49      */
getSensor()50     public abstract int getSensor();
51 
getRestriction()52     protected String getRestriction() {
53         return null;
54     }
55 
56     @Override
isChecked()57     public boolean isChecked() {
58         return !mSensorPrivacyManagerHelper.isSensorBlocked(getSensor());
59     }
60 
61     @Override
setChecked(boolean isChecked)62     public boolean setChecked(boolean isChecked) {
63         mSensorPrivacyManagerHelper.setSensorBlockedForProfileGroup(SETTINGS, getSensor(),
64                 !isChecked);
65         return true;
66     }
67 
68     @Override
displayPreference(PreferenceScreen screen)69     public void displayPreference(PreferenceScreen screen) {
70         super.displayPreference(screen);
71 
72         RestrictedSwitchPreference preference =
73                 (RestrictedSwitchPreference) screen.findPreference(getPreferenceKey());
74         if (preference != null) {
75             preference.setDisabledByAdmin(RestrictedLockUtilsInternal
76                     .checkIfRestrictionEnforced(mContext, getRestriction(), mContext.getUserId()));
77         }
78 
79         mSensorPrivacyManagerHelper.addSensorBlockedListener(
80                 getSensor(),
81                 (sensor, blocked) -> updateState(screen.findPreference(mPreferenceKey)),
82                 mCallbackExecutor);
83     }
84 
85     @Override
getSliceHighlightMenuRes()86     public int getSliceHighlightMenuRes() {
87         return R.string.menu_key_privacy;
88     }
89 }
90