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) 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user