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