blob: a72c0e05e1a35345f9e205cb271b00b4e3101a98 [file] [log] [blame]
Doris Ling4fbf04c2018-03-01 10:33:14 -08001/*
2 * Copyright (C) 2018 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
17package com.android.settings.development;
18
19import android.content.Context;
20import android.provider.Settings;
Fan Zhangde117042018-09-04 13:52:15 -070021
Aurimas Liutikas03fcde32018-04-17 11:22:43 -070022import androidx.preference.Preference;
Fan Zhangde117042018-09-04 13:52:15 -070023import androidx.preference.SwitchPreference;
Doris Ling4fbf04c2018-03-01 10:33:14 -080024
25import com.android.settings.core.PreferenceControllerMixin;
26import com.android.settingslib.development.DeveloperOptionsPreferenceController;
27
28/**
29 * Base controller for Switch preference that maps to a specific value in Settings.Secure.
30 */
31public abstract class SecureSettingSwitchPreferenceController extends
32 DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener,
33 PreferenceControllerMixin {
34
35 private static final int SETTING_VALUE_OFF = 0;
36 private static final int SETTING_VALUE_ON = 1;
37
38 private final String mSettingsKey;
39
40 public SecureSettingSwitchPreferenceController(Context context, String secureSettingsKey) {
41 super(context);
42 mSettingsKey = secureSettingsKey;
43 }
44
45 @Override
46 public boolean onPreferenceChange(Preference preference, Object newValue) {
47 final boolean isEnabled = (Boolean) newValue;
48 Settings.Secure.putInt(mContext.getContentResolver(), mSettingsKey,
49 isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF);
50 return true;
51 }
52
53 @Override
54 public void updateState(Preference preference) {
55 final int mode = Settings.Secure.getInt(
56 mContext.getContentResolver(), mSettingsKey, SETTING_VALUE_OFF);
57 ((SwitchPreference) mPreference).setChecked(mode != SETTING_VALUE_OFF);
58 }
59
60 @Override
61 protected void onDeveloperOptionsSwitchDisabled() {
62 super.onDeveloperOptionsSwitchDisabled();
63 Settings.Secure.putInt(mContext.getContentResolver(), mSettingsKey, SETTING_VALUE_OFF);
64 ((SwitchPreference) mPreference).setChecked(false);
65 }
66}