blob: 0a33f2bcfaef7672ad7c086a7934c03c3d7bc296 [file] [log] [blame]
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +02001/*
2 * Copyright (C) 2015 The CyanogenMod 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
micky38798506412023-10-23 16:33:47 +020017package omnirom.preference;
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +020018
19import android.content.Context;
Vachounet41e03b62019-09-11 10:40:12 +020020import androidx.preference.SwitchPreference;
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +020021import android.util.AttributeSet;
22
23import android.provider.Settings;
24
25public class SecureSettingSwitchPreference extends SwitchPreference {
26 public SecureSettingSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
27 super(context, attrs, defStyle);
28 }
29
30 public SecureSettingSwitchPreference(Context context, AttributeSet attrs) {
31 super(context, attrs);
32 }
33
34 public SecureSettingSwitchPreference(Context context) {
35 super(context, null);
36 }
37
38 @Override
39 protected boolean persistBoolean(boolean value) {
40 if (shouldPersist()) {
41 if (value == getPersistedBoolean(!value)) {
42 // It's already there, so the same as persisting
43 return true;
44 }
45 Settings.Secure.putInt(getContext().getContentResolver(), getKey(), value ? 1 : 0);
46 return true;
47 }
48 return false;
49 }
50
51 @Override
52 protected boolean getPersistedBoolean(boolean defaultReturnValue) {
53 if (!shouldPersist()) {
54 return defaultReturnValue;
55 }
56 return Settings.Secure.getInt(getContext().getContentResolver(),
57 getKey(), defaultReturnValue ? 1 : 0) != 0;
58 }
59
60 @Override
61 protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
62 setChecked(Settings.Secure.getString(getContext().getContentResolver(), getKey()) != null ? getPersistedBoolean(isChecked())
63 : (Boolean) defaultValue);
64 }
65}