blob: 1996a4b98ce86d5d1ad59aaec7d84a87e5bfb204 [file] [log] [blame]
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +02001/*
2 * Copyright (C) 2014 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.provider.Settings;
22import android.util.AttributeSet;
23
24public class GlobalSettingSwitchPreference extends SwitchPreference {
25 public GlobalSettingSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
26 super(context, attrs, defStyle);
27 }
28
29 public GlobalSettingSwitchPreference(Context context, AttributeSet attrs) {
30 super(context, attrs);
31 }
32
33 public GlobalSettingSwitchPreference(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 Settings.Global.putInt(getContext().getContentResolver(), getKey(), value ? 1 : 0);
45 return true;
46 }
47 return false;
48 }
49
50 @Override
51 protected boolean getPersistedBoolean(boolean defaultReturnValue) {
52 if (!shouldPersist()) {
53 return defaultReturnValue;
54 }
55 return Settings.Global.getInt(getContext().getContentResolver(),
56 getKey(), defaultReturnValue ? 1 : 0) != 0;
57 }
58
59 @Override
60 protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
61 setChecked(Settings.Global.getString(getContext().getContentResolver(), getKey()) != null ? getPersistedBoolean(isChecked())
62 : (Boolean) defaultValue);
63 }
64}