Add switch preferences that hold system settings
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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.
|
||||
@@ -17,48 +17,35 @@
|
||||
package com.cherish.settings.preferences;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
import androidx.preference.SwitchPreference;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import androidx.preference.SwitchPreference;
|
||||
|
||||
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, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean persistBoolean(boolean value) {
|
||||
if (shouldPersist()) {
|
||||
if (value == getPersistedBoolean(!value)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
Settings.Global.putInt(getContext().getContentResolver(), getKey(), value ? 1 : 0);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean getPersistedBoolean(boolean defaultReturnValue) {
|
||||
if (!shouldPersist()) {
|
||||
return defaultReturnValue;
|
||||
}
|
||||
return Settings.Global.getInt(getContext().getContentResolver(),
|
||||
getKey(), defaultReturnValue ? 1 : 0) != 0;
|
||||
super(context);
|
||||
setPreferenceDataStore(new GlobalSettingsStore(context.getContentResolver()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
|
||||
setChecked(Settings.Global.getString(getContext().getContentResolver(), getKey()) != null ? getPersistedBoolean(isChecked())
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 com.cherish.settings.preferences;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
* 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
|
||||
* 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,
|
||||
@@ -17,48 +17,35 @@
|
||||
package com.cherish.settings.preferences;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
import androidx.preference.SwitchPreference;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import androidx.preference.SwitchPreference;
|
||||
|
||||
public class SecureSettingSwitchPreference extends SwitchPreference {
|
||||
|
||||
public SecureSettingSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
setPreferenceDataStore(new SecureSettingsStore(context.getContentResolver()));
|
||||
}
|
||||
|
||||
public SecureSettingSwitchPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
setPreferenceDataStore(new SecureSettingsStore(context.getContentResolver()));
|
||||
}
|
||||
|
||||
public SecureSettingSwitchPreference(Context context) {
|
||||
super(context, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean persistBoolean(boolean value) {
|
||||
if (shouldPersist()) {
|
||||
if (value == getPersistedBoolean(!value)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
Settings.Secure.putInt(getContext().getContentResolver(), getKey(), value ? 1 : 0);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean getPersistedBoolean(boolean defaultReturnValue) {
|
||||
if (!shouldPersist()) {
|
||||
return defaultReturnValue;
|
||||
}
|
||||
return Settings.Secure.getInt(getContext().getContentResolver(),
|
||||
getKey(), defaultReturnValue ? 1 : 0) != 0;
|
||||
super(context);
|
||||
setPreferenceDataStore(new SecureSettingsStore(context.getContentResolver()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
|
||||
setChecked(Settings.Secure.getString(getContext().getContentResolver(), getKey()) != null ? getPersistedBoolean(isChecked())
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 com.cherish.settings.preferences;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.preference.PreferenceDataStore;
|
||||
import android.provider.Settings;
|
||||
|
||||
public class SecureSettingsStore extends androidx.preference.PreferenceDataStore
|
||||
implements PreferenceDataStore {
|
||||
|
||||
private ContentResolver mContentResolver;
|
||||
|
||||
public SecureSettingsStore(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.Secure.getFloat(mContentResolver, key, defValue);
|
||||
}
|
||||
|
||||
public int getInt(String key, int defValue) {
|
||||
return Settings.Secure.getInt(mContentResolver, key, defValue);
|
||||
}
|
||||
|
||||
public long getLong(String key, long defValue) {
|
||||
return Settings.Secure.getLong(mContentResolver, key, defValue);
|
||||
}
|
||||
|
||||
public String getString(String key, String defValue) {
|
||||
String result = Settings.Secure.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.Secure.putFloat(mContentResolver, key, value);
|
||||
}
|
||||
|
||||
public void putInt(String key, int value) {
|
||||
Settings.Secure.putInt(mContentResolver, key, value);
|
||||
}
|
||||
|
||||
public void putLong(String key, long value) {
|
||||
Settings.Secure.putLong(mContentResolver, key, value);
|
||||
}
|
||||
|
||||
public void putString(String key, String value) {
|
||||
Settings.Secure.putString(mContentResolver, key, value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2013 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.
|
||||
@@ -17,48 +17,35 @@
|
||||
package com.cherish.settings.preferences;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
import androidx.preference.SwitchPreference;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import androidx.preference.SwitchPreference;
|
||||
|
||||
public class SystemSettingSwitchPreference extends SwitchPreference {
|
||||
|
||||
public SystemSettingSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
setPreferenceDataStore(new SystemSettingsStore(context.getContentResolver()));
|
||||
}
|
||||
|
||||
public SystemSettingSwitchPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
setPreferenceDataStore(new SystemSettingsStore(context.getContentResolver()));
|
||||
}
|
||||
|
||||
public SystemSettingSwitchPreference(Context context) {
|
||||
super(context, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean persistBoolean(boolean value) {
|
||||
if (shouldPersist()) {
|
||||
if (value == getPersistedBoolean(!value)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
Settings.System.putInt(getContext().getContentResolver(), getKey(), value ? 1 : 0);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean getPersistedBoolean(boolean defaultReturnValue) {
|
||||
if (!shouldPersist()) {
|
||||
return defaultReturnValue;
|
||||
}
|
||||
return Settings.System.getInt(getContext().getContentResolver(),
|
||||
getKey(), defaultReturnValue ? 1 : 0) != 0;
|
||||
super(context);
|
||||
setPreferenceDataStore(new SystemSettingsStore(context.getContentResolver()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
|
||||
setChecked(Settings.System.getString(getContext().getContentResolver(), getKey()) != null ? getPersistedBoolean(isChecked())
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 com.cherish.settings.preferences;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.preference.PreferenceDataStore;
|
||||
import android.provider.Settings;
|
||||
|
||||
public class SystemSettingsStore extends androidx.preference.PreferenceDataStore
|
||||
implements PreferenceDataStore {
|
||||
|
||||
private ContentResolver mContentResolver;
|
||||
|
||||
public SystemSettingsStore(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.System.getFloat(mContentResolver, key, defValue);
|
||||
}
|
||||
|
||||
public int getInt(String key, int defValue) {
|
||||
return Settings.System.getInt(mContentResolver, key, defValue);
|
||||
}
|
||||
|
||||
public long getLong(String key, long defValue) {
|
||||
return Settings.System.getLong(mContentResolver, key, defValue);
|
||||
}
|
||||
|
||||
public String getString(String key, String defValue) {
|
||||
String result = Settings.System.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.System.putFloat(mContentResolver, key, value);
|
||||
}
|
||||
|
||||
public void putInt(String key, int value) {
|
||||
Settings.System.putInt(mContentResolver, key, value);
|
||||
}
|
||||
|
||||
public void putLong(String key, long value) {
|
||||
Settings.System.putLong(mContentResolver, key, value);
|
||||
}
|
||||
|
||||
public void putString(String key, String value) {
|
||||
Settings.System.putString(mContentResolver, key, value);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user