1 /* 2 * Copyright (C) 2020 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 static com.android.settingslib.core.lifecycle.HideNonSystemOverlayMixin.SECURE_OVERLAY_SETTINGS; 20 21 import android.content.Context; 22 import android.provider.Settings; 23 24 import androidx.annotation.VisibleForTesting; 25 import androidx.preference.Preference; 26 import androidx.preference.TwoStatePreference; 27 28 import com.android.settings.core.PreferenceControllerMixin; 29 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 30 31 /** 32 * A controller helps enable or disable a developer setting which allows non system overlays on 33 * Settings app. 34 */ 35 public class OverlaySettingsPreferenceController extends DeveloperOptionsPreferenceController 36 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { 37 38 private static final String KEY_OVERLAY_SETTINGS = "overlay_settings"; 39 OverlaySettingsPreferenceController(Context context)40 public OverlaySettingsPreferenceController(Context context) { 41 super(context); 42 } 43 44 @Override isAvailable()45 public boolean isAvailable() { 46 return true; 47 } 48 49 @Override getPreferenceKey()50 public String getPreferenceKey() { 51 return KEY_OVERLAY_SETTINGS; 52 } 53 54 @Override onPreferenceChange(Preference preference, Object newValue)55 public boolean onPreferenceChange(Preference preference, Object newValue) { 56 setOverlaySettingsEnabled(mContext, (Boolean) newValue); 57 return true; 58 } 59 60 @Override updateState(Preference preference)61 public void updateState(Preference preference) { 62 ((TwoStatePreference) preference).setChecked(isOverlaySettingsEnabled(mContext)); 63 } 64 65 /** 66 * Check if this setting is enabled or not. 67 */ 68 @VisibleForTesting isOverlaySettingsEnabled(Context context)69 static boolean isOverlaySettingsEnabled(Context context) { 70 return Settings.Secure.getInt(context.getContentResolver(), 71 SECURE_OVERLAY_SETTINGS, 0 /* defValue */) != 0; 72 } 73 74 /** 75 * Enable this setting. 76 */ 77 @VisibleForTesting setOverlaySettingsEnabled(Context context, boolean enabled)78 static void setOverlaySettingsEnabled(Context context, boolean enabled) { 79 Settings.Secure.putInt(context.getContentResolver(), 80 SECURE_OVERLAY_SETTINGS, enabled ? 1 : 0); 81 } 82 83 @Override onDeveloperOptionsSwitchDisabled()84 protected void onDeveloperOptionsSwitchDisabled() { 85 super.onDeveloperOptionsSwitchDisabled(); 86 setOverlaySettingsEnabled(mContext, false); 87 } 88 } 89