From 9103a52d003d25232510e3d1aa51e9bec0c0bea0 Mon Sep 17 00:00:00 2001 From: Adithya R Date: Mon, 23 Oct 2023 17:26:46 +0530 Subject: [PATCH] aospa: extensions: Add GlobalSettingSwitchPreference Just renamed from SystemSetting* Change-Id: I4fd7e5960f2b68185c7e38b30c2421d5a4495bdf --- .../GlobalSettingSwitchPreference.java | 51 +++++++++++++ .../preference/GlobalSettingsStore.java | 73 +++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 extensions/framework/src/co/aospa/framework/preference/GlobalSettingSwitchPreference.java create mode 100644 extensions/framework/src/co/aospa/framework/preference/GlobalSettingsStore.java diff --git a/extensions/framework/src/co/aospa/framework/preference/GlobalSettingSwitchPreference.java b/extensions/framework/src/co/aospa/framework/preference/GlobalSettingSwitchPreference.java new file mode 100644 index 00000000..268918c6 --- /dev/null +++ b/extensions/framework/src/co/aospa/framework/preference/GlobalSettingSwitchPreference.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2014 The CyanogenMod Project + * Copyright (C) 2017 AICP + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package co.aospa.framework.preference; + +import android.content.Context; +import androidx.preference.SwitchPreference; +import android.util.AttributeSet; + +public class GlobalSettingSwitchPreference extends SwitchPreference { + + public GlobalSettingSwitchPreference(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + setPreferenceDataStore(new GlobalSettingsStore(context.getContentResolver())); + } + + public GlobalSettingSwitchPreference(Context context, AttributeSet attrs) { + super(context, attrs); + setPreferenceDataStore(new GlobalSettingsStore(context.getContentResolver())); + } + + public GlobalSettingSwitchPreference(Context context) { + super(context); + setPreferenceDataStore(new GlobalSettingsStore(context.getContentResolver())); + } + + @Override + protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { + // This is what default TwoStatePreference implementation is doing without respecting + // real default value: + //setChecked(restoreValue ? getPersistedBoolean(mChecked) + // : (Boolean) defaultValue); + // Instead, we better do + setChecked(restoreValue ? getPersistedBoolean((Boolean) defaultValue) + : (Boolean) defaultValue); + } +} diff --git a/extensions/framework/src/co/aospa/framework/preference/GlobalSettingsStore.java b/extensions/framework/src/co/aospa/framework/preference/GlobalSettingsStore.java new file mode 100644 index 00000000..5f4befee --- /dev/null +++ b/extensions/framework/src/co/aospa/framework/preference/GlobalSettingsStore.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2017 AICP + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package co.aospa.framework.preference; + +import android.content.ContentResolver; +import android.preference.PreferenceDataStore; +import android.provider.Settings; + +public class GlobalSettingsStore extends androidx.preference.PreferenceDataStore + implements PreferenceDataStore { + + private ContentResolver mContentResolver; + + public GlobalSettingsStore(ContentResolver contentResolver) { + mContentResolver = contentResolver; + } + + public boolean getBoolean(String key, boolean defValue) { + return getInt(key, defValue ? 1 : 0) != 0; + } + + public float getFloat(String key, float defValue) { + return Settings.Global.getFloat(mContentResolver, key, defValue); + } + + public int getInt(String key, int defValue) { + return Settings.Global.getInt(mContentResolver, key, defValue); + } + + public long getLong(String key, long defValue) { + return Settings.Global.getLong(mContentResolver, key, defValue); + } + + public String getString(String key, String defValue) { + String result = Settings.Global.getString(mContentResolver, key); + return result == null ? defValue : result; + } + + public void putBoolean(String key, boolean value) { + putInt(key, value ? 1 : 0); + } + + public void putFloat(String key, float value) { + Settings.Global.putFloat(mContentResolver, key, value); + } + + public void putInt(String key, int value) { + Settings.Global.putInt(mContentResolver, key, value); + } + + public void putLong(String key, long value) { + Settings.Global.putLong(mContentResolver, key, value); + } + + public void putString(String key, String value) { + Settings.Global.putString(mContentResolver, key, value); + } + +}