lineage-sdk: Use PreferenceDataStore for lineage-sdk preferences
Replaces the need for:
32e67e6020
*) frameworks/support are now built with gradle and are included as prebuilts.
*) API level 26 (8.0) introduced PreferenceDataStorage which can be used
to implement what we need without touching the support library.
Change-Id: Ide3417019ccf8bde5dca3b2aec3d2075e7c8b597
This commit is contained in:
committed by
Bruno Martins
parent
dee3258356
commit
a713e602ef
@@ -1,5 +1,6 @@
|
||||
/**
|
||||
* Copyright (C) 2014-2016 The CyanogenMod project
|
||||
* Copyright (C) 2014-2016 The CyanogenMod Project
|
||||
* Copyright (C) 2018 The LineageOS Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -34,30 +35,19 @@ public class GlobalSettingSwitchPreference extends SelfRemovingSwitchPreference
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isPersisted() {
|
||||
return Settings.Global.getString(getContext().getContentResolver(), getKey()) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void putBoolean(String key, boolean value) {
|
||||
Settings.Global.putInt(getContext().getContentResolver(), key, value ? 1 : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean getBoolean(String key, boolean defaultValue) {
|
||||
return Settings.Global.getInt(getContext().getContentResolver(),
|
||||
key, defaultValue ? 1 : 0) != 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The CyanogenMod project
|
||||
* Copyright (C) 2016 The CyanogenMod Project
|
||||
* Copyright (C) 2018 The LineageOS Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -35,30 +36,19 @@ public class LineageGlobalSettingSwitchPreference extends SelfRemovingSwitchPref
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean persistBoolean(boolean value) {
|
||||
if (shouldPersist()) {
|
||||
if (value == getPersistedBoolean(!value)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
LineageSettings.Global.putInt(getContext().getContentResolver(), getKey(), value ? 1 : 0);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean getPersistedBoolean(boolean defaultReturnValue) {
|
||||
if (!shouldPersist()) {
|
||||
return defaultReturnValue;
|
||||
}
|
||||
return LineageSettings.Global.getInt(getContext().getContentResolver(),
|
||||
getKey(), defaultReturnValue ? 1 : 0) != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isPersisted() {
|
||||
return LineageSettings.Global.getString(getContext().getContentResolver(), getKey()) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void putBoolean(String key, boolean value) {
|
||||
LineageSettings.Global.putInt(getContext().getContentResolver(), key, value ? 1 : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean getBoolean(String key, boolean defaultValue) {
|
||||
return LineageSettings.Global.getInt(getContext().getContentResolver(),
|
||||
key, defaultValue ? 1 : 0) != 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The CyanogenMod project
|
||||
* Copyright (C) 2016 The CyanogenMod Project
|
||||
* Copyright (C) 2018 The LineageOS Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -31,26 +32,8 @@ public class LineageSecureSettingListPreference extends SelfRemovingListPreferen
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean persistString(String value) {
|
||||
if (shouldPersist()) {
|
||||
if (value == getPersistedString(null)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
LineageSettings.Secure.putString(getContext().getContentResolver(), getKey(), value);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getPersistedString(String defaultReturnValue) {
|
||||
if (!shouldPersist()) {
|
||||
return defaultReturnValue;
|
||||
}
|
||||
String value = LineageSettings.Secure.getString(getContext().getContentResolver(), getKey());
|
||||
return value == null ? defaultReturnValue : value;
|
||||
public int getIntValue(int defValue) {
|
||||
return getValue() == null ? defValue : Integer.valueOf(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -58,7 +41,14 @@ public class LineageSecureSettingListPreference extends SelfRemovingListPreferen
|
||||
return LineageSettings.Secure.getString(getContext().getContentResolver(), getKey()) != null;
|
||||
}
|
||||
|
||||
public int getIntValue(int defValue) {
|
||||
return getValue() == null ? defValue : Integer.valueOf(getValue());
|
||||
@Override
|
||||
protected void putString(String key, String value) {
|
||||
LineageSettings.Secure.putString(getContext().getContentResolver(), key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getString(String key, String defaultValue) {
|
||||
return LineageSettings.Secure.getString(getContext().getContentResolver(),
|
||||
key, defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (C) 2015 The CyanogenMod project
|
||||
* Copyright (C) 2015 The CyanogenMod Project
|
||||
* Copyright (C) 2018 The LineageOS Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -35,30 +36,19 @@ public class LineageSecureSettingSwitchPreference extends SelfRemovingSwitchPref
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean persistBoolean(boolean value) {
|
||||
if (shouldPersist()) {
|
||||
if (value == getPersistedBoolean(!value)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
LineageSettings.Secure.putInt(getContext().getContentResolver(), getKey(), value ? 1 : 0);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean getPersistedBoolean(boolean defaultReturnValue) {
|
||||
if (!shouldPersist()) {
|
||||
return defaultReturnValue;
|
||||
}
|
||||
return LineageSettings.Secure.getInt(getContext().getContentResolver(),
|
||||
getKey(), defaultReturnValue ? 1 : 0) != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isPersisted() {
|
||||
return LineageSettings.Secure.getString(getContext().getContentResolver(), getKey()) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void putBoolean(String key, boolean value) {
|
||||
LineageSettings.Secure.putInt(getContext().getContentResolver(), key, value ? 1 : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean getBoolean(String key, boolean defaultValue) {
|
||||
return LineageSettings.Secure.getInt(getContext().getContentResolver(),
|
||||
key, defaultValue ? 1 : 0) != 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The CyanogenMod project
|
||||
* Copyright (C) 2016 The CyanogenMod Project
|
||||
* Copyright (C) 2018 The LineageOS Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -30,26 +31,8 @@ public class LineageSystemSettingDropDownPreference extends SelfRemovingDropDown
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean persistString(String value) {
|
||||
if (shouldPersist()) {
|
||||
if (value == getPersistedString(null)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
LineageSettings.System.putString(getContext().getContentResolver(), getKey(), value);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getPersistedString(String defaultReturnValue) {
|
||||
if (!shouldPersist()) {
|
||||
return defaultReturnValue;
|
||||
}
|
||||
String value = LineageSettings.System.getString(getContext().getContentResolver(), getKey());
|
||||
return value == null ? defaultReturnValue : value;
|
||||
public int getIntValue(int defValue) {
|
||||
return getValue() == null ? defValue : Integer.valueOf(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -57,7 +40,14 @@ public class LineageSystemSettingDropDownPreference extends SelfRemovingDropDown
|
||||
return LineageSettings.System.getString(getContext().getContentResolver(), getKey()) != null;
|
||||
}
|
||||
|
||||
public int getIntValue(int defValue) {
|
||||
return getValue() == null ? defValue : Integer.valueOf(getValue());
|
||||
@Override
|
||||
protected void putString(String key, String value) {
|
||||
LineageSettings.System.putString(getContext().getContentResolver(), key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getString(String key, String defaultValue) {
|
||||
return LineageSettings.System.getString(getContext().getContentResolver(),
|
||||
key, defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The CyanogenMod project
|
||||
* Copyright (C) 2016 The CyanogenMod Project
|
||||
* Copyright (C) 2018 The LineageOS Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -30,26 +31,8 @@ public class LineageSystemSettingListPreference extends SelfRemovingListPreferen
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean persistString(String value) {
|
||||
if (shouldPersist()) {
|
||||
if (value == getPersistedString(null)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
LineageSettings.System.putString(getContext().getContentResolver(), getKey(), value);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getPersistedString(String defaultReturnValue) {
|
||||
if (!shouldPersist()) {
|
||||
return defaultReturnValue;
|
||||
}
|
||||
String value = LineageSettings.System.getString(getContext().getContentResolver(), getKey());
|
||||
return value == null ? defaultReturnValue : value;
|
||||
public int getIntValue(int defValue) {
|
||||
return getValue() == null ? defValue : Integer.valueOf(getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -57,7 +40,14 @@ public class LineageSystemSettingListPreference extends SelfRemovingListPreferen
|
||||
return LineageSettings.System.getString(getContext().getContentResolver(), getKey()) != null;
|
||||
}
|
||||
|
||||
public int getIntValue(int defValue) {
|
||||
return getValue() == null ? defValue : Integer.valueOf(getValue());
|
||||
@Override
|
||||
protected void putString(String key, String value) {
|
||||
LineageSettings.System.putString(getContext().getContentResolver(), key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getString(String key, String defaultValue) {
|
||||
return LineageSettings.System.getString(getContext().getContentResolver(),
|
||||
key, defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (C) 2015 The CyanogenMod project
|
||||
* Copyright (C) 2015 The CyanogenMod Project
|
||||
* Copyright (C) 2018 The LineageOS Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -35,30 +36,19 @@ public class LineageSystemSettingSwitchPreference extends SelfRemovingSwitchPref
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean persistBoolean(boolean value) {
|
||||
if (shouldPersist()) {
|
||||
if (value == getPersistedBoolean(!value)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
LineageSettings.System.putInt(getContext().getContentResolver(), getKey(), value ? 1 : 0);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean getPersistedBoolean(boolean defaultReturnValue) {
|
||||
if (!shouldPersist()) {
|
||||
return defaultReturnValue;
|
||||
}
|
||||
return LineageSettings.System.getInt(getContext().getContentResolver(),
|
||||
getKey(), defaultReturnValue ? 1 : 0) != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isPersisted() {
|
||||
return LineageSettings.System.getString(getContext().getContentResolver(), getKey()) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void putBoolean(String key, boolean value) {
|
||||
LineageSettings.System.putInt(getContext().getContentResolver(), key, value ? 1 : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean getBoolean(String key, boolean defaultValue) {
|
||||
return LineageSettings.System.getInt(getContext().getContentResolver(),
|
||||
key, defaultValue ? 1 : 0) != 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/**
|
||||
* Copyright (C) 2014-2016 The CyanogenMod project
|
||||
* Copyright (C) 2014-2016 The CyanogenMod Project
|
||||
* Copyright (C) 2018 The LineageOS Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -34,30 +35,19 @@ public class SecureSettingSwitchPreference extends SelfRemovingSwitchPreference
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isPersisted() {
|
||||
return Settings.Secure.getString(getContext().getContentResolver(), getKey()) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void putBoolean(String key, boolean value) {
|
||||
Settings.Secure.putInt(getContext().getContentResolver(), key, value ? 1 : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean getBoolean(String key, boolean defaultValue) {
|
||||
return Settings.Secure.getInt(getContext().getContentResolver(),
|
||||
key, defaultValue ? 1 : 0) != 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The CyanogenMod Project
|
||||
* Copyright (C) 2018 The LineageOS Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,6 +18,7 @@ package lineageos.preference;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v7.preference.DropDownPreference;
|
||||
import android.support.v7.preference.PreferenceDataStore;
|
||||
import android.support.v7.preference.PreferenceViewHolder;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
@@ -24,23 +26,26 @@ import android.util.AttributeSet;
|
||||
* A Preference which can automatically remove itself from the hierarchy
|
||||
* based on constraints set in XML.
|
||||
*/
|
||||
public class SelfRemovingDropDownPreference extends DropDownPreference {
|
||||
public abstract class SelfRemovingDropDownPreference extends DropDownPreference {
|
||||
|
||||
private final ConstraintsHelper mConstraints;
|
||||
|
||||
public SelfRemovingDropDownPreference(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
mConstraints = new ConstraintsHelper(context, attrs, this);
|
||||
setPreferenceDataStore(new DataStore());
|
||||
}
|
||||
|
||||
public SelfRemovingDropDownPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
mConstraints = new ConstraintsHelper(context, attrs, this);
|
||||
setPreferenceDataStore(new DataStore());
|
||||
}
|
||||
|
||||
public SelfRemovingDropDownPreference(Context context) {
|
||||
super(context);
|
||||
mConstraints = new ConstraintsHelper(context, null, this);
|
||||
setPreferenceDataStore(new DataStore());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -62,4 +67,39 @@ public class SelfRemovingDropDownPreference extends DropDownPreference {
|
||||
public boolean isAvailable() {
|
||||
return mConstraints.isAvailable();
|
||||
}
|
||||
|
||||
protected abstract boolean isPersisted();
|
||||
protected abstract void putString(String key, String value);
|
||||
protected abstract String getString(String key, String defaultValue);
|
||||
|
||||
@Override
|
||||
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
|
||||
final String value;
|
||||
if (!restorePersistedValue || !isPersisted()) {
|
||||
if (defaultValue == null) {
|
||||
return;
|
||||
}
|
||||
value = (String) defaultValue;
|
||||
if (shouldPersist()) {
|
||||
persistString(value);
|
||||
}
|
||||
} else {
|
||||
// Note: the default is not used because to have got here
|
||||
// isPersisted() must be true.
|
||||
value = getString(getKey(), null /* not used */);
|
||||
}
|
||||
setValue(value);
|
||||
}
|
||||
|
||||
private class DataStore extends PreferenceDataStore {
|
||||
@Override
|
||||
public void putString(String key, String value) {
|
||||
SelfRemovingDropDownPreference.this.putString(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String key, String defaultValue) {
|
||||
return SelfRemovingDropDownPreference.this.getString(key, defaultValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The CyanogenMod Project
|
||||
* Copyright (C) 2018 The LineageOS Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,6 +18,7 @@ package lineageos.preference;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v7.preference.ListPreference;
|
||||
import android.support.v7.preference.PreferenceDataStore;
|
||||
import android.support.v7.preference.PreferenceViewHolder;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
@@ -24,23 +26,26 @@ import android.util.AttributeSet;
|
||||
* A Preference which can automatically remove itself from the hierarchy
|
||||
* based on constraints set in XML.
|
||||
*/
|
||||
public class SelfRemovingListPreference extends ListPreference {
|
||||
public abstract class SelfRemovingListPreference extends ListPreference {
|
||||
|
||||
private final ConstraintsHelper mConstraints;
|
||||
|
||||
public SelfRemovingListPreference(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
mConstraints = new ConstraintsHelper(context, attrs, this);
|
||||
setPreferenceDataStore(new DataStore());
|
||||
}
|
||||
|
||||
public SelfRemovingListPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
mConstraints = new ConstraintsHelper(context, attrs, this);
|
||||
setPreferenceDataStore(new DataStore());
|
||||
}
|
||||
|
||||
public SelfRemovingListPreference(Context context) {
|
||||
super(context);
|
||||
mConstraints = new ConstraintsHelper(context, null, this);
|
||||
setPreferenceDataStore(new DataStore());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -63,4 +68,38 @@ public class SelfRemovingListPreference extends ListPreference {
|
||||
return mConstraints.isAvailable();
|
||||
}
|
||||
|
||||
protected abstract boolean isPersisted();
|
||||
protected abstract void putString(String key, String value);
|
||||
protected abstract String getString(String key, String defaultValue);
|
||||
|
||||
@Override
|
||||
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
|
||||
final String value;
|
||||
if (!restorePersistedValue || !isPersisted()) {
|
||||
if (defaultValue == null) {
|
||||
return;
|
||||
}
|
||||
value = (String) defaultValue;
|
||||
if (shouldPersist()) {
|
||||
persistString(value);
|
||||
}
|
||||
} else {
|
||||
// Note: the default is not used because to have got here
|
||||
// isPersisted() must be true.
|
||||
value = getString(getKey(), null /* not used */);
|
||||
}
|
||||
setValue(value);
|
||||
}
|
||||
|
||||
private class DataStore extends PreferenceDataStore {
|
||||
@Override
|
||||
public void putString(String key, String value) {
|
||||
SelfRemovingListPreference.this.putString(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String key, String defaultValue) {
|
||||
return SelfRemovingListPreference.this.getString(key, defaultValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The CyanogenMod project
|
||||
* Copyright (C) 2016 The CyanogenMod Project
|
||||
* Copyright (C) 2018 The LineageOS Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,6 +18,7 @@ package lineageos.preference;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v14.preference.SwitchPreference;
|
||||
import android.support.v7.preference.PreferenceDataStore;
|
||||
import android.support.v7.preference.PreferenceViewHolder;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
@@ -24,23 +26,26 @@ import android.util.AttributeSet;
|
||||
* A SwitchPreference which can automatically remove itself from the hierarchy
|
||||
* based on constraints set in XML.
|
||||
*/
|
||||
public class SelfRemovingSwitchPreference extends SwitchPreference {
|
||||
public abstract class SelfRemovingSwitchPreference extends SwitchPreference {
|
||||
|
||||
private final ConstraintsHelper mConstraints;
|
||||
|
||||
public SelfRemovingSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
mConstraints = new ConstraintsHelper(context, attrs, this);
|
||||
setPreferenceDataStore(new DataStore());
|
||||
}
|
||||
|
||||
public SelfRemovingSwitchPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
mConstraints = new ConstraintsHelper(context, attrs, this);
|
||||
setPreferenceDataStore(new DataStore());
|
||||
}
|
||||
|
||||
public SelfRemovingSwitchPreference(Context context) {
|
||||
super(context);
|
||||
mConstraints = new ConstraintsHelper(context, null, this);
|
||||
setPreferenceDataStore(new DataStore());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -62,4 +67,39 @@ public class SelfRemovingSwitchPreference extends SwitchPreference {
|
||||
public boolean isAvailable() {
|
||||
return mConstraints.isAvailable();
|
||||
}
|
||||
|
||||
protected abstract boolean isPersisted();
|
||||
protected abstract void putBoolean(String key, boolean value);
|
||||
protected abstract boolean getBoolean(String key, boolean defaultValue);
|
||||
|
||||
@Override
|
||||
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
|
||||
final boolean checked;
|
||||
if (!restorePersistedValue || !isPersisted()) {
|
||||
if (defaultValue == null) {
|
||||
return;
|
||||
}
|
||||
checked = (boolean) defaultValue;
|
||||
if (shouldPersist()) {
|
||||
persistBoolean(checked);
|
||||
}
|
||||
} else {
|
||||
// Note: the default is not used because to have got here
|
||||
// isPersisted() must be true.
|
||||
checked = getBoolean(getKey(), false /* not used */);
|
||||
}
|
||||
setChecked(checked);
|
||||
}
|
||||
|
||||
private class DataStore extends PreferenceDataStore {
|
||||
@Override
|
||||
public void putBoolean(String key, boolean value) {
|
||||
SelfRemovingSwitchPreference.this.putBoolean(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String key, boolean defaultValue) {
|
||||
return SelfRemovingSwitchPreference.this.getBoolean(key, defaultValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (C) 2013 The CyanogenMod project
|
||||
* Copyright (C) 2013 The CyanogenMod Project
|
||||
* Copyright (C) 2018 The LineageOS Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -34,30 +35,19 @@ public class SystemSettingSwitchPreference extends SelfRemovingSwitchPreference
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isPersisted() {
|
||||
return Settings.System.getString(getContext().getContentResolver(), getKey()) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void putBoolean(String key, boolean value) {
|
||||
Settings.System.putInt(getContext().getContentResolver(), key, value ? 1 : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean getBoolean(String key, boolean defaultValue) {
|
||||
return Settings.System.getInt(getContext().getContentResolver(),
|
||||
key, defaultValue ? 1 : 0) != 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user