1 /*
2  * Copyright (C) 2017 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.development;
18 
19 import android.content.Context;
20 import android.os.RemoteException;
21 import android.os.ServiceManager;
22 import android.os.StrictMode;
23 import android.os.SystemProperties;
24 import android.view.IWindowManager;
25 
26 import androidx.annotation.VisibleForTesting;
27 import androidx.preference.Preference;
28 import androidx.preference.SwitchPreference;
29 
30 import com.android.settings.core.PreferenceControllerMixin;
31 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
32 
33 public class StrictModePreferenceController extends DeveloperOptionsPreferenceController implements
34         Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
35 
36     private static final String STRICT_MODE_KEY = "strict_mode";
37     private static final String WINDOW_MANAGER_KEY = "window";
38 
39     @VisibleForTesting
40     static final String STRICT_MODE_ENABLED = "1";
41     @VisibleForTesting
42     static final String STRICT_MODE_DISABLED = "";
43 
44     private final IWindowManager mWindowManager;
45 
StrictModePreferenceController(Context context)46     public StrictModePreferenceController(Context context) {
47         super(context);
48 
49         mWindowManager = IWindowManager.Stub.asInterface(
50                 ServiceManager.getService(WINDOW_MANAGER_KEY));
51     }
52 
53     @Override
getPreferenceKey()54     public String getPreferenceKey() {
55         return STRICT_MODE_KEY;
56     }
57 
58     @Override
onPreferenceChange(Preference preference, Object newValue)59     public boolean onPreferenceChange(Preference preference, Object newValue) {
60         final boolean isEnabled = (Boolean) newValue;
61         writeStrictModeVisualOptions(isEnabled);
62         return true;
63     }
64 
65     @Override
updateState(Preference preference)66     public void updateState(Preference preference) {
67         ((SwitchPreference) mPreference).setChecked(isStrictModeEnabled());
68     }
69 
70     @Override
onDeveloperOptionsSwitchDisabled()71     protected void onDeveloperOptionsSwitchDisabled() {
72         super.onDeveloperOptionsSwitchDisabled();
73         writeStrictModeVisualOptions(false);
74         ((SwitchPreference) mPreference).setChecked(false);
75     }
76 
isStrictModeEnabled()77     private boolean isStrictModeEnabled() {
78         return SystemProperties.getBoolean(StrictMode.VISUAL_PROPERTY, false /* default */);
79     }
80 
writeStrictModeVisualOptions(boolean isEnabled)81     private void writeStrictModeVisualOptions(boolean isEnabled) {
82         try {
83             mWindowManager.setStrictModeVisualIndicatorPreference(
84                     isEnabled ? STRICT_MODE_ENABLED : STRICT_MODE_DISABLED);
85         } catch (RemoteException e) {
86             // intentional no-op
87         }
88     }
89 }
90