blob: 01a9e69a825bc5bca368f07414fcd9049ab8fffa [file] [log] [blame]
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +02001/*
2 * Copyright (C) 2013 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.CheckBoxPreference;
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +020021import android.provider.Settings;
22import android.util.AttributeSet;
23
24public class GlobalCheckBoxPreference extends CheckBoxPreference {
25 public GlobalCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
26 super(context, attrs, defStyle);
27 }
28
29 public GlobalCheckBoxPreference(Context context, AttributeSet attrs) {
30 super(context, attrs);
31 }
32
33 public GlobalCheckBoxPreference(Context context) {
34 super(context, null);
35 }
36
37 @Override
38 protected boolean persistBoolean(boolean value) {
39 if (shouldPersist()) {
40 if (value == getPersistedBoolean(!value)) {
41 // It's already there, so the same as persisting
42 return true;
43 }
44
45 Settings.Global.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
57 return Settings.Global.getInt(getContext().getContentResolver(),
58 getKey(), defaultReturnValue ? 1 : 0) != 0;
59 }
60
61 @Override
62 protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
63 setChecked(Settings.Global.getString(getContext().getContentResolver(), getKey()) != null ? getPersistedBoolean(isChecked())
64 : (Boolean) defaultValue);
65 }
66}