Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
micky387 | 9850641 | 2023-10-23 16:33:47 +0200 | [diff] [blame^] | 17 | package omnirom.preference; |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.provider.Settings; |
Vachounet | 41e03b6 | 2019-09-11 10:40:12 +0200 | [diff] [blame] | 21 | import androidx.preference.SwitchPreference; |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 22 | import android.util.AttributeSet; |
| 23 | |
| 24 | public class SystemSettingSwitchPreference extends SwitchPreference { |
| 25 | public SystemSettingSwitchPreference(Context context, AttributeSet attrs, int defStyle) { |
| 26 | super(context, attrs, defStyle); |
| 27 | } |
| 28 | |
| 29 | public SystemSettingSwitchPreference(Context context, AttributeSet attrs) { |
| 30 | super(context, attrs); |
| 31 | } |
| 32 | |
| 33 | public SystemSettingSwitchPreference(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.System.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.System.getInt(getContext().getContentResolver(), |
| 56 | getKey(), defaultReturnValue ? 1 : 0) != 0; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { |
| 61 | setChecked(Settings.System.getString(getContext().getContentResolver(), getKey()) != null ? getPersistedBoolean(isChecked()) |
| 62 | : (Boolean) defaultValue); |
| 63 | } |
| 64 | } |