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