Merge "Adds data store to provide data abstracion layer for Preferences."
This commit is contained in:
@@ -30011,6 +30011,7 @@ package android.preference {
|
||||
method protected long getPersistedLong(long);
|
||||
method protected java.lang.String getPersistedString(java.lang.String);
|
||||
method public java.util.Set<java.lang.String> getPersistedStringSet(java.util.Set<java.lang.String>);
|
||||
method public android.preference.PreferenceDataStore getPreferenceDataStore();
|
||||
method public android.preference.PreferenceManager getPreferenceManager();
|
||||
method public android.content.SharedPreferences getSharedPreferences();
|
||||
method public boolean getShouldDisableView();
|
||||
@@ -30060,6 +30061,7 @@ package android.preference {
|
||||
method public void setOnPreferenceClickListener(android.preference.Preference.OnPreferenceClickListener);
|
||||
method public void setOrder(int);
|
||||
method public void setPersistent(boolean);
|
||||
method public void setPreferenceDataStore(android.preference.PreferenceDataStore);
|
||||
method public void setSelectable(boolean);
|
||||
method public void setShouldDisableView(boolean);
|
||||
method public void setSummary(java.lang.CharSequence);
|
||||
@@ -30160,6 +30162,21 @@ package android.preference {
|
||||
ctor public PreferenceCategory(android.content.Context);
|
||||
}
|
||||
|
||||
public abstract interface PreferenceDataStore {
|
||||
method public default boolean getBoolean(java.lang.String, boolean);
|
||||
method public default float getFloat(java.lang.String, float);
|
||||
method public default int getInt(java.lang.String, int);
|
||||
method public default long getLong(java.lang.String, long);
|
||||
method public default java.lang.String getString(java.lang.String, java.lang.String);
|
||||
method public default java.util.Set<java.lang.String> getStringSet(java.lang.String, java.util.Set<java.lang.String>);
|
||||
method public default void putBoolean(java.lang.String, boolean);
|
||||
method public default void putFloat(java.lang.String, float);
|
||||
method public default void putInt(java.lang.String, int);
|
||||
method public default void putLong(java.lang.String, long);
|
||||
method public default void putString(java.lang.String, java.lang.String);
|
||||
method public default void putStringSet(java.lang.String, java.util.Set<java.lang.String>);
|
||||
}
|
||||
|
||||
public abstract class PreferenceFragment extends android.app.Fragment {
|
||||
ctor public PreferenceFragment();
|
||||
method public void addPreferencesFromIntent(android.content.Intent);
|
||||
@@ -30199,6 +30216,7 @@ package android.preference {
|
||||
method public android.preference.Preference findPreference(java.lang.CharSequence);
|
||||
method public static android.content.SharedPreferences getDefaultSharedPreferences(android.content.Context);
|
||||
method public static java.lang.String getDefaultSharedPreferencesName(android.content.Context);
|
||||
method public android.preference.PreferenceDataStore getPreferenceDataStore();
|
||||
method public android.content.SharedPreferences getSharedPreferences();
|
||||
method public int getSharedPreferencesMode();
|
||||
method public java.lang.String getSharedPreferencesName();
|
||||
@@ -30206,6 +30224,7 @@ package android.preference {
|
||||
method public boolean isStorageDeviceProtected();
|
||||
method public static void setDefaultValues(android.content.Context, int, boolean);
|
||||
method public static void setDefaultValues(android.content.Context, java.lang.String, int, int, boolean);
|
||||
method public void setPreferenceDataStore(android.preference.PreferenceDataStore);
|
||||
method public void setSharedPreferencesMode(int);
|
||||
method public void setSharedPreferencesName(java.lang.String);
|
||||
method public void setStorageDefault();
|
||||
|
||||
@@ -32645,6 +32645,7 @@ package android.preference {
|
||||
method protected long getPersistedLong(long);
|
||||
method protected java.lang.String getPersistedString(java.lang.String);
|
||||
method public java.util.Set<java.lang.String> getPersistedStringSet(java.util.Set<java.lang.String>);
|
||||
method public android.preference.PreferenceDataStore getPreferenceDataStore();
|
||||
method public android.preference.PreferenceManager getPreferenceManager();
|
||||
method public android.content.SharedPreferences getSharedPreferences();
|
||||
method public boolean getShouldDisableView();
|
||||
@@ -32694,6 +32695,7 @@ package android.preference {
|
||||
method public void setOnPreferenceClickListener(android.preference.Preference.OnPreferenceClickListener);
|
||||
method public void setOrder(int);
|
||||
method public void setPersistent(boolean);
|
||||
method public void setPreferenceDataStore(android.preference.PreferenceDataStore);
|
||||
method public void setSelectable(boolean);
|
||||
method public void setShouldDisableView(boolean);
|
||||
method public void setSummary(java.lang.CharSequence);
|
||||
@@ -32794,6 +32796,21 @@ package android.preference {
|
||||
ctor public PreferenceCategory(android.content.Context);
|
||||
}
|
||||
|
||||
public abstract interface PreferenceDataStore {
|
||||
method public default boolean getBoolean(java.lang.String, boolean);
|
||||
method public default float getFloat(java.lang.String, float);
|
||||
method public default int getInt(java.lang.String, int);
|
||||
method public default long getLong(java.lang.String, long);
|
||||
method public default java.lang.String getString(java.lang.String, java.lang.String);
|
||||
method public default java.util.Set<java.lang.String> getStringSet(java.lang.String, java.util.Set<java.lang.String>);
|
||||
method public default void putBoolean(java.lang.String, boolean);
|
||||
method public default void putFloat(java.lang.String, float);
|
||||
method public default void putInt(java.lang.String, int);
|
||||
method public default void putLong(java.lang.String, long);
|
||||
method public default void putString(java.lang.String, java.lang.String);
|
||||
method public default void putStringSet(java.lang.String, java.util.Set<java.lang.String>);
|
||||
}
|
||||
|
||||
public abstract class PreferenceFragment extends android.app.Fragment {
|
||||
ctor public PreferenceFragment();
|
||||
method public void addPreferencesFromIntent(android.content.Intent);
|
||||
@@ -32833,6 +32850,7 @@ package android.preference {
|
||||
method public android.preference.Preference findPreference(java.lang.CharSequence);
|
||||
method public static android.content.SharedPreferences getDefaultSharedPreferences(android.content.Context);
|
||||
method public static java.lang.String getDefaultSharedPreferencesName(android.content.Context);
|
||||
method public android.preference.PreferenceDataStore getPreferenceDataStore();
|
||||
method public android.content.SharedPreferences getSharedPreferences();
|
||||
method public int getSharedPreferencesMode();
|
||||
method public java.lang.String getSharedPreferencesName();
|
||||
@@ -32841,6 +32859,7 @@ package android.preference {
|
||||
method public boolean isStorageDeviceProtected();
|
||||
method public static void setDefaultValues(android.content.Context, int, boolean);
|
||||
method public static void setDefaultValues(android.content.Context, java.lang.String, int, int, boolean);
|
||||
method public void setPreferenceDataStore(android.preference.PreferenceDataStore);
|
||||
method public void setSharedPreferencesMode(int);
|
||||
method public void setSharedPreferencesName(java.lang.String);
|
||||
method public void setStorageCredentialProtected();
|
||||
|
||||
@@ -30099,6 +30099,7 @@ package android.preference {
|
||||
method protected long getPersistedLong(long);
|
||||
method protected java.lang.String getPersistedString(java.lang.String);
|
||||
method public java.util.Set<java.lang.String> getPersistedStringSet(java.util.Set<java.lang.String>);
|
||||
method public android.preference.PreferenceDataStore getPreferenceDataStore();
|
||||
method public android.preference.PreferenceManager getPreferenceManager();
|
||||
method public android.content.SharedPreferences getSharedPreferences();
|
||||
method public boolean getShouldDisableView();
|
||||
@@ -30148,6 +30149,7 @@ package android.preference {
|
||||
method public void setOnPreferenceClickListener(android.preference.Preference.OnPreferenceClickListener);
|
||||
method public void setOrder(int);
|
||||
method public void setPersistent(boolean);
|
||||
method public void setPreferenceDataStore(android.preference.PreferenceDataStore);
|
||||
method public void setSelectable(boolean);
|
||||
method public void setShouldDisableView(boolean);
|
||||
method public void setSummary(java.lang.CharSequence);
|
||||
@@ -30248,6 +30250,21 @@ package android.preference {
|
||||
ctor public PreferenceCategory(android.content.Context);
|
||||
}
|
||||
|
||||
public abstract interface PreferenceDataStore {
|
||||
method public default boolean getBoolean(java.lang.String, boolean);
|
||||
method public default float getFloat(java.lang.String, float);
|
||||
method public default int getInt(java.lang.String, int);
|
||||
method public default long getLong(java.lang.String, long);
|
||||
method public default java.lang.String getString(java.lang.String, java.lang.String);
|
||||
method public default java.util.Set<java.lang.String> getStringSet(java.lang.String, java.util.Set<java.lang.String>);
|
||||
method public default void putBoolean(java.lang.String, boolean);
|
||||
method public default void putFloat(java.lang.String, float);
|
||||
method public default void putInt(java.lang.String, int);
|
||||
method public default void putLong(java.lang.String, long);
|
||||
method public default void putString(java.lang.String, java.lang.String);
|
||||
method public default void putStringSet(java.lang.String, java.util.Set<java.lang.String>);
|
||||
}
|
||||
|
||||
public abstract class PreferenceFragment extends android.app.Fragment {
|
||||
ctor public PreferenceFragment();
|
||||
method public void addPreferencesFromIntent(android.content.Intent);
|
||||
@@ -30287,6 +30304,7 @@ package android.preference {
|
||||
method public android.preference.Preference findPreference(java.lang.CharSequence);
|
||||
method public static android.content.SharedPreferences getDefaultSharedPreferences(android.content.Context);
|
||||
method public static java.lang.String getDefaultSharedPreferencesName(android.content.Context);
|
||||
method public android.preference.PreferenceDataStore getPreferenceDataStore();
|
||||
method public android.content.SharedPreferences getSharedPreferences();
|
||||
method public int getSharedPreferencesMode();
|
||||
method public java.lang.String getSharedPreferencesName();
|
||||
@@ -30294,6 +30312,7 @@ package android.preference {
|
||||
method public boolean isStorageDeviceProtected();
|
||||
method public static void setDefaultValues(android.content.Context, int, boolean);
|
||||
method public static void setDefaultValues(android.content.Context, java.lang.String, int, int, boolean);
|
||||
method public void setPreferenceDataStore(android.preference.PreferenceDataStore);
|
||||
method public void setSharedPreferencesMode(int);
|
||||
method public void setSharedPreferencesName(java.lang.String);
|
||||
method public void setStorageDefault();
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.android.internal.util.CharSequences;
|
||||
|
||||
import android.annotation.DrawableRes;
|
||||
import android.annotation.LayoutRes;
|
||||
import android.annotation.Nullable;
|
||||
import android.annotation.StringRes;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
@@ -88,8 +89,18 @@ public class Preference implements Comparable<Preference> {
|
||||
public static final int DEFAULT_ORDER = Integer.MAX_VALUE;
|
||||
|
||||
private Context mContext;
|
||||
|
||||
@Nullable
|
||||
private PreferenceManager mPreferenceManager;
|
||||
|
||||
/**
|
||||
* The data store that should be used by this Preference to store / retrieve data. If null then
|
||||
* {@link PreferenceManager#getPreferenceDataStore()} needs to be checked. If that one is null
|
||||
* too it means that we are using {@link android.content.SharedPreferences} to store the data.
|
||||
*/
|
||||
@Nullable
|
||||
private PreferenceDataStore mPreferenceDataStore;
|
||||
|
||||
/**
|
||||
* Set when added to hierarchy since we need a unique ID within that
|
||||
* hierarchy.
|
||||
@@ -394,6 +405,36 @@ public class Preference implements Comparable<Preference> {
|
||||
return mFragment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a {@link PreferenceDataStore} to be used by this Preference instead of using
|
||||
* {@link android.content.SharedPreferences}.
|
||||
* <p>
|
||||
* The data store will remain assigned even if the Preference is moved between multiple
|
||||
* instances of {@link PreferenceFragment}.
|
||||
*
|
||||
* @param dataStore The {@link PreferenceDataStore} to be used by this Preference.
|
||||
*/
|
||||
public void setPreferenceDataStore(PreferenceDataStore dataStore) {
|
||||
mPreferenceDataStore = dataStore;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@link PreferenceDataStore} used by this Preference. Returns {@code null} if
|
||||
* {@link android.content.SharedPreferences} is used instead.
|
||||
*
|
||||
* @return The {@link PreferenceDataStore} used by this Preference or {@code null} if none.
|
||||
*/
|
||||
@Nullable
|
||||
public PreferenceDataStore getPreferenceDataStore() {
|
||||
if (mPreferenceDataStore != null) {
|
||||
return mPreferenceDataStore;
|
||||
} else if (mPreferenceManager != null) {
|
||||
return mPreferenceManager.getPreferenceDataStore();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the extras Bundle object associated with this preference, creating
|
||||
* a new Bundle if there currently isn't one. You can use this to get and
|
||||
@@ -1426,44 +1467,42 @@ public class Preference implements Comparable<Preference> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to persist a String to the {@link android.content.SharedPreferences}.
|
||||
* <p>
|
||||
* This will check if this Preference is persistent, get an editor from
|
||||
* the {@link PreferenceManager}, put in the string, and check if we should commit (and
|
||||
* commit if so).
|
||||
* Attempts to persist a String if this Preference is persistent.
|
||||
*
|
||||
* @param value The value to persist.
|
||||
* @return True if the Preference is persistent. (This is not whether the
|
||||
* @return True if this Preference is persistent. (This is not whether the
|
||||
* value was persisted, since we may not necessarily commit if there
|
||||
* will be a batch commit later.)
|
||||
* @see #getPersistedString(String)
|
||||
*/
|
||||
protected boolean persistString(String value) {
|
||||
if (shouldPersist()) {
|
||||
// Shouldn't store null
|
||||
if (TextUtils.equals(value, getPersistedString(null))) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
if (!shouldPersist()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Shouldn't store null
|
||||
if (TextUtils.equals(value, getPersistedString(null))) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
|
||||
PreferenceDataStore dataStore = getPreferenceDataStore();
|
||||
if (dataStore != null) {
|
||||
dataStore.putString(mKey, value);
|
||||
} else {
|
||||
SharedPreferences.Editor editor = mPreferenceManager.getEditor();
|
||||
editor.putString(mKey, value);
|
||||
tryCommit(editor);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to get a persisted String from the {@link android.content.SharedPreferences}.
|
||||
* <p>
|
||||
* This will check if this Preference is persistent, get the SharedPreferences
|
||||
* from the {@link PreferenceManager}, and get the value.
|
||||
* Attempts to get a persisted String if this Preference is persistent.
|
||||
*
|
||||
* @param defaultReturnValue The default value to return if either the
|
||||
* Preference is not persistent or the Preference is not in the
|
||||
* shared preferences.
|
||||
* @return The value from the SharedPreferences or the default return
|
||||
* @param defaultReturnValue The default value to return if either this
|
||||
* Preference is not persistent or this Preference is not present.
|
||||
* @return The value from the data store or the default return
|
||||
* value.
|
||||
* @see #persistString(String)
|
||||
*/
|
||||
@@ -1472,49 +1511,51 @@ public class Preference implements Comparable<Preference> {
|
||||
return defaultReturnValue;
|
||||
}
|
||||
|
||||
PreferenceDataStore dataStore = getPreferenceDataStore();
|
||||
if (dataStore != null) {
|
||||
return dataStore.getString(mKey, defaultReturnValue);
|
||||
}
|
||||
|
||||
return mPreferenceManager.getSharedPreferences().getString(mKey, defaultReturnValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to persist a set of Strings to the {@link android.content.SharedPreferences}.
|
||||
* <p>
|
||||
* This will check if this Preference is persistent, get an editor from
|
||||
* the {@link PreferenceManager}, put in the strings, and check if we should commit (and
|
||||
* commit if so).
|
||||
* Attempts to persist a set of Strings if this Preference is persistent.
|
||||
*
|
||||
* @param values The values to persist.
|
||||
* @return True if the Preference is persistent. (This is not whether the
|
||||
* @return True if this Preference is persistent. (This is not whether the
|
||||
* value was persisted, since we may not necessarily commit if there
|
||||
* will be a batch commit later.)
|
||||
* @see #getPersistedStringSet(Set)
|
||||
*/
|
||||
public boolean persistStringSet(Set<String> values) {
|
||||
if (shouldPersist()) {
|
||||
// Shouldn't store null
|
||||
if (values.equals(getPersistedStringSet(null))) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
public boolean persistStringSet(Set<String> values) {
|
||||
if (!shouldPersist()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Shouldn't store null
|
||||
if (values.equals(getPersistedStringSet(null))) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
|
||||
PreferenceDataStore dataStore = getPreferenceDataStore();
|
||||
if (dataStore != null) {
|
||||
dataStore.putStringSet(mKey, values);
|
||||
} else {
|
||||
SharedPreferences.Editor editor = mPreferenceManager.getEditor();
|
||||
editor.putStringSet(mKey, values);
|
||||
tryCommit(editor);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to get a persisted set of Strings from the
|
||||
* {@link android.content.SharedPreferences}.
|
||||
* <p>
|
||||
* This will check if this Preference is persistent, get the SharedPreferences
|
||||
* from the {@link PreferenceManager}, and get the value.
|
||||
* Attempts to get a persisted set of Strings if this Preference is persistent.
|
||||
*
|
||||
* @param defaultReturnValue The default value to return if either the
|
||||
* Preference is not persistent or the Preference is not in the
|
||||
* shared preferences.
|
||||
* @return The value from the SharedPreferences or the default return
|
||||
* @param defaultReturnValue The default value to return if either this
|
||||
* Preference is not persistent or this Preference is not present.
|
||||
* @return The value from the data store or the default return
|
||||
* value.
|
||||
* @see #persistStringSet(Set)
|
||||
*/
|
||||
@@ -1523,41 +1564,51 @@ public class Preference implements Comparable<Preference> {
|
||||
return defaultReturnValue;
|
||||
}
|
||||
|
||||
PreferenceDataStore dataStore = getPreferenceDataStore();
|
||||
if (dataStore != null) {
|
||||
return dataStore.getStringSet(mKey, defaultReturnValue);
|
||||
}
|
||||
|
||||
return mPreferenceManager.getSharedPreferences().getStringSet(mKey, defaultReturnValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to persist an int to the {@link android.content.SharedPreferences}.
|
||||
* Attempts to persist an int if this Preference is persistent.
|
||||
*
|
||||
* @param value The value to persist.
|
||||
* @return True if the Preference is persistent. (This is not whether the
|
||||
* @return True if this Preference is persistent. (This is not whether the
|
||||
* value was persisted, since we may not necessarily commit if there
|
||||
* will be a batch commit later.)
|
||||
* @see #persistString(String)
|
||||
* @see #getPersistedInt(int)
|
||||
*/
|
||||
protected boolean persistInt(int value) {
|
||||
if (shouldPersist()) {
|
||||
if (value == getPersistedInt(~value)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
if (!shouldPersist()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (value == getPersistedInt(~value)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
|
||||
PreferenceDataStore dataStore = getPreferenceDataStore();
|
||||
if (dataStore != null) {
|
||||
dataStore.putInt(mKey, value);
|
||||
} else {
|
||||
SharedPreferences.Editor editor = mPreferenceManager.getEditor();
|
||||
editor.putInt(mKey, value);
|
||||
tryCommit(editor);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to get a persisted int from the {@link android.content.SharedPreferences}.
|
||||
* Attempts to get a persisted int if this Preference is persistent.
|
||||
*
|
||||
* @param defaultReturnValue The default value to return if either this
|
||||
* Preference is not persistent or this Preference is not in the
|
||||
* SharedPreferences.
|
||||
* @return The value from the SharedPreferences or the default return
|
||||
* Preference is not persistent or this Preference is not present.
|
||||
* @return The value from the data store or the default return
|
||||
* value.
|
||||
* @see #getPersistedString(String)
|
||||
* @see #persistInt(int)
|
||||
@@ -1567,11 +1618,16 @@ public class Preference implements Comparable<Preference> {
|
||||
return defaultReturnValue;
|
||||
}
|
||||
|
||||
PreferenceDataStore dataStore = getPreferenceDataStore();
|
||||
if (dataStore != null) {
|
||||
return dataStore.getInt(mKey, defaultReturnValue);
|
||||
}
|
||||
|
||||
return mPreferenceManager.getSharedPreferences().getInt(mKey, defaultReturnValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to persist a float to the {@link android.content.SharedPreferences}.
|
||||
* Attempts to persist a long if this Preference is persistent.
|
||||
*
|
||||
* @param value The value to persist.
|
||||
* @return True if this Preference is persistent. (This is not whether the
|
||||
@@ -1581,27 +1637,32 @@ public class Preference implements Comparable<Preference> {
|
||||
* @see #getPersistedFloat(float)
|
||||
*/
|
||||
protected boolean persistFloat(float value) {
|
||||
if (shouldPersist()) {
|
||||
if (value == getPersistedFloat(Float.NaN)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
if (!shouldPersist()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (value == getPersistedFloat(Float.NaN)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
|
||||
PreferenceDataStore dataStore = getPreferenceDataStore();
|
||||
if (dataStore != null) {
|
||||
dataStore.putFloat(mKey, value);
|
||||
} else {
|
||||
SharedPreferences.Editor editor = mPreferenceManager.getEditor();
|
||||
editor.putFloat(mKey, value);
|
||||
tryCommit(editor);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to get a persisted float from the {@link android.content.SharedPreferences}.
|
||||
* Attempts to get a persisted float if this Preference is persistent.
|
||||
*
|
||||
* @param defaultReturnValue The default value to return if either this
|
||||
* Preference is not persistent or this Preference is not in the
|
||||
* SharedPreferences.
|
||||
* @return The value from the SharedPreferences or the default return
|
||||
* Preference is not persistent or this Preference is not present.
|
||||
* @return The value from the data store or the default return
|
||||
* value.
|
||||
* @see #getPersistedString(String)
|
||||
* @see #persistFloat(float)
|
||||
@@ -1611,11 +1672,16 @@ public class Preference implements Comparable<Preference> {
|
||||
return defaultReturnValue;
|
||||
}
|
||||
|
||||
PreferenceDataStore dataStore = getPreferenceDataStore();
|
||||
if (dataStore != null) {
|
||||
return dataStore.getFloat(mKey, defaultReturnValue);
|
||||
}
|
||||
|
||||
return mPreferenceManager.getSharedPreferences().getFloat(mKey, defaultReturnValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to persist a long to the {@link android.content.SharedPreferences}.
|
||||
* Attempts to persist a long if this Preference is persistent.
|
||||
*
|
||||
* @param value The value to persist.
|
||||
* @return True if this Preference is persistent. (This is not whether the
|
||||
@@ -1625,27 +1691,32 @@ public class Preference implements Comparable<Preference> {
|
||||
* @see #getPersistedLong(long)
|
||||
*/
|
||||
protected boolean persistLong(long value) {
|
||||
if (shouldPersist()) {
|
||||
if (value == getPersistedLong(~value)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
if (!shouldPersist()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (value == getPersistedLong(~value)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
|
||||
PreferenceDataStore dataStore = getPreferenceDataStore();
|
||||
if (dataStore != null) {
|
||||
dataStore.putLong(mKey, value);
|
||||
} else {
|
||||
SharedPreferences.Editor editor = mPreferenceManager.getEditor();
|
||||
editor.putLong(mKey, value);
|
||||
tryCommit(editor);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to get a persisted long from the {@link android.content.SharedPreferences}.
|
||||
* Attempts to get a persisted long if this Preference is persistent.
|
||||
*
|
||||
* @param defaultReturnValue The default value to return if either this
|
||||
* Preference is not persistent or this Preference is not in the
|
||||
* SharedPreferences.
|
||||
* @return The value from the SharedPreferences or the default return
|
||||
* Preference is not persistent or this Preference is not present.
|
||||
* @return The value from the data store or the default return
|
||||
* value.
|
||||
* @see #getPersistedString(String)
|
||||
* @see #persistLong(long)
|
||||
@@ -1655,11 +1726,16 @@ public class Preference implements Comparable<Preference> {
|
||||
return defaultReturnValue;
|
||||
}
|
||||
|
||||
PreferenceDataStore dataStore = getPreferenceDataStore();
|
||||
if (dataStore != null) {
|
||||
return dataStore.getLong(mKey, defaultReturnValue);
|
||||
}
|
||||
|
||||
return mPreferenceManager.getSharedPreferences().getLong(mKey, defaultReturnValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to persist a boolean to the {@link android.content.SharedPreferences}.
|
||||
* Attempts to persist a boolean if this Preference is persistent.
|
||||
*
|
||||
* @param value The value to persist.
|
||||
* @return True if this Preference is persistent. (This is not whether the
|
||||
@@ -1669,27 +1745,32 @@ public class Preference implements Comparable<Preference> {
|
||||
* @see #getPersistedBoolean(boolean)
|
||||
*/
|
||||
protected boolean persistBoolean(boolean value) {
|
||||
if (shouldPersist()) {
|
||||
if (value == getPersistedBoolean(!value)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
if (!shouldPersist()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (value == getPersistedBoolean(!value)) {
|
||||
// It's already there, so the same as persisting
|
||||
return true;
|
||||
}
|
||||
|
||||
PreferenceDataStore dataStore = getPreferenceDataStore();
|
||||
if (dataStore != null) {
|
||||
dataStore.putBoolean(mKey, value);
|
||||
} else {
|
||||
SharedPreferences.Editor editor = mPreferenceManager.getEditor();
|
||||
editor.putBoolean(mKey, value);
|
||||
tryCommit(editor);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to get a persisted boolean from the {@link android.content.SharedPreferences}.
|
||||
* Attempts to get a persisted boolean if this Preference is persistent.
|
||||
*
|
||||
* @param defaultReturnValue The default value to return if either this
|
||||
* Preference is not persistent or this Preference is not in the
|
||||
* SharedPreferences.
|
||||
* @return The value from the SharedPreferences or the default return
|
||||
* Preference is not persistent or this Preference is not present.
|
||||
* @return The value from the data store or the default return
|
||||
* value.
|
||||
* @see #getPersistedString(String)
|
||||
* @see #persistBoolean(boolean)
|
||||
@@ -1699,6 +1780,11 @@ public class Preference implements Comparable<Preference> {
|
||||
return defaultReturnValue;
|
||||
}
|
||||
|
||||
PreferenceDataStore dataStore = getPreferenceDataStore();
|
||||
if (dataStore != null) {
|
||||
return dataStore.getBoolean(mKey, defaultReturnValue);
|
||||
}
|
||||
|
||||
return mPreferenceManager.getSharedPreferences().getBoolean(mKey, defaultReturnValue);
|
||||
}
|
||||
|
||||
|
||||
164
core/java/android/preference/PreferenceDataStore.java
Normal file
164
core/java/android/preference/PreferenceDataStore.java
Normal file
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
*
|
||||
* 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 android.preference;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A data store interface to be implemented and provided to the Preferences framework.
|
||||
*
|
||||
* Use this to replace the default {@link android.content.SharedPreferences}. By default, all "put"
|
||||
* methods throw {@link UnsupportedOperationException}.
|
||||
*/
|
||||
public interface PreferenceDataStore {
|
||||
|
||||
/**
|
||||
* Set a String value to the data store.
|
||||
*
|
||||
* @param key The name of the preference to modify.
|
||||
* @param value The new value for the preference.
|
||||
* @see #getString(String, String)
|
||||
*/
|
||||
default void putString(String key, @Nullable String value) {
|
||||
throw new UnsupportedOperationException("Not implemented on this data store");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a set of String value to the data store.
|
||||
*
|
||||
* @param key The name of the preference to modify.
|
||||
* @param values The set of new values for the preference.
|
||||
* @see #getStringSet(String, Set)
|
||||
*/
|
||||
default void putStringSet(String key, @Nullable Set<String> values) {
|
||||
throw new UnsupportedOperationException("Not implemented on this data store");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an int value to the data store.
|
||||
*
|
||||
* @param key The name of the preference to modify.
|
||||
* @param value The new value for the preference.
|
||||
* @see #getInt(String, int)
|
||||
*/
|
||||
default void putInt(String key, int value) {
|
||||
throw new UnsupportedOperationException("Not implemented on this data store");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a long value to the data store.
|
||||
*
|
||||
* @param key The name of the preference to modify.
|
||||
* @param value The new value for the preference.
|
||||
* @see #getLong(String, long)
|
||||
*/
|
||||
default void putLong(String key, long value) {
|
||||
throw new UnsupportedOperationException("Not implemented on this data store");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a float value to the data store.
|
||||
*
|
||||
* @param key The name of the preference to modify.
|
||||
* @param value The new value for the preference.
|
||||
* @see #getFloat(String, float)
|
||||
*/
|
||||
default void putFloat(String key, float value) {
|
||||
throw new UnsupportedOperationException("Not implemented on this data store");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a boolean value to the data store.
|
||||
*
|
||||
* @param key The name of the preference to modify.
|
||||
* @param value The new value for the preference.
|
||||
* @see #getBoolean(String, boolean)
|
||||
*/
|
||||
default void putBoolean(String key, boolean value) {
|
||||
throw new UnsupportedOperationException("Not implemented on this data store");
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a String value from the data store.
|
||||
*
|
||||
* @param key The name of the preference to retrieve.
|
||||
* @param defValue Value to return if this preference does not exist.
|
||||
* @see #putString(String, String)
|
||||
*/
|
||||
@Nullable
|
||||
default String getString(String key, @Nullable String defValue) {
|
||||
return defValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a set of String values from the data store.
|
||||
*
|
||||
* @param key The name of the preference to retrieve.
|
||||
* @param defValues Values to return if this preference does not exist.
|
||||
* @see #putStringSet(String, Set)
|
||||
*/
|
||||
@Nullable
|
||||
default Set<String> getStringSet(String key, @Nullable Set<String> defValues) {
|
||||
return defValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an int value from the data store.
|
||||
*
|
||||
* @param key The name of the preference to retrieve.
|
||||
* @param defValue Value to return if this preference does not exist.
|
||||
* @see #putInt(String, int)
|
||||
*/
|
||||
default int getInt(String key, int defValue) {
|
||||
return defValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a long value from the data store.
|
||||
*
|
||||
* @param key The name of the preference to retrieve.
|
||||
* @param defValue Value to return if this preference does not exist.
|
||||
* @see #putLong(String, long)
|
||||
*/
|
||||
default long getLong(String key, long defValue) {
|
||||
return defValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a float value from the data store.
|
||||
*
|
||||
* @param key The name of the preference to retrieve.
|
||||
* @param defValue Value to return if this preference does not exist.
|
||||
* @see #putFloat(String, float)
|
||||
*/
|
||||
default float getFloat(String key, float defValue) {
|
||||
return defValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a boolean value from the data store.
|
||||
*
|
||||
* @param key The name of the preference to retrieve.
|
||||
* @param defValue Value to return if this preference does not exist.
|
||||
* @see #getBoolean(String, boolean)
|
||||
*/
|
||||
default boolean getBoolean(String key, boolean defValue) {
|
||||
return defValue;
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package android.preference;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
import android.annotation.SystemApi;
|
||||
import android.annotation.XmlRes;
|
||||
import android.app.Activity;
|
||||
@@ -42,37 +43,39 @@ import java.util.List;
|
||||
* In most cases, clients should use
|
||||
* {@link PreferenceActivity#addPreferencesFromIntent} or
|
||||
* {@link PreferenceActivity#addPreferencesFromResource(int)}.
|
||||
*
|
||||
*
|
||||
* @see PreferenceActivity
|
||||
*/
|
||||
public class PreferenceManager {
|
||||
|
||||
|
||||
private static final String TAG = "PreferenceManager";
|
||||
|
||||
/**
|
||||
* The Activity meta-data key for its XML preference hierarchy.
|
||||
*/
|
||||
public static final String METADATA_KEY_PREFERENCES = "android.preference";
|
||||
|
||||
|
||||
public static final String KEY_HAS_SET_DEFAULT_VALUES = "_has_set_default_values";
|
||||
|
||||
|
||||
/**
|
||||
* @see #getActivity()
|
||||
*/
|
||||
@Nullable
|
||||
private Activity mActivity;
|
||||
|
||||
/**
|
||||
* Fragment that owns this instance.
|
||||
*/
|
||||
@Nullable
|
||||
private PreferenceFragment mFragment;
|
||||
|
||||
/**
|
||||
* The context to use. This should always be set.
|
||||
*
|
||||
*
|
||||
* @see #mActivity
|
||||
*/
|
||||
private Context mContext;
|
||||
|
||||
|
||||
/**
|
||||
* The counter for unique IDs.
|
||||
*/
|
||||
@@ -86,26 +89,35 @@ public class PreferenceManager {
|
||||
/**
|
||||
* Cached shared preferences.
|
||||
*/
|
||||
@Nullable
|
||||
private SharedPreferences mSharedPreferences;
|
||||
|
||||
|
||||
/**
|
||||
* Data store to be used by the Preferences or null if {@link android.content.SharedPreferences}
|
||||
* should be used.
|
||||
*/
|
||||
@Nullable
|
||||
private PreferenceDataStore mPreferenceDataStore;
|
||||
|
||||
/**
|
||||
* If in no-commit mode, the shared editor to give out (which will be
|
||||
* committed when exiting no-commit mode).
|
||||
*/
|
||||
@Nullable
|
||||
private SharedPreferences.Editor mEditor;
|
||||
|
||||
|
||||
/**
|
||||
* Blocks commits from happening on the shared editor. This is used when
|
||||
* inflating the hierarchy. Do not set this directly, use {@link #setNoCommit(boolean)}
|
||||
*/
|
||||
private boolean mNoCommit;
|
||||
|
||||
|
||||
/**
|
||||
* The SharedPreferences name that will be used for all {@link Preference}s
|
||||
* managed by this instance.
|
||||
*/
|
||||
private String mSharedPreferencesName;
|
||||
|
||||
|
||||
/**
|
||||
* The SharedPreferences mode that will be used for all {@link Preference}s
|
||||
* managed by this instance.
|
||||
@@ -121,38 +133,43 @@ public class PreferenceManager {
|
||||
/**
|
||||
* The {@link PreferenceScreen} at the root of the preference hierarchy.
|
||||
*/
|
||||
@Nullable
|
||||
private PreferenceScreen mPreferenceScreen;
|
||||
|
||||
/**
|
||||
* List of activity result listeners.
|
||||
*/
|
||||
@Nullable
|
||||
private List<OnActivityResultListener> mActivityResultListeners;
|
||||
|
||||
/**
|
||||
* List of activity stop listeners.
|
||||
*/
|
||||
@Nullable
|
||||
private List<OnActivityStopListener> mActivityStopListeners;
|
||||
|
||||
/**
|
||||
* List of activity destroy listeners.
|
||||
*/
|
||||
@Nullable
|
||||
private List<OnActivityDestroyListener> mActivityDestroyListeners;
|
||||
|
||||
/**
|
||||
* List of dialogs that should be dismissed when we receive onNewIntent in
|
||||
* our PreferenceActivity.
|
||||
*/
|
||||
@Nullable
|
||||
private List<DialogInterface> mPreferencesScreens;
|
||||
|
||||
|
||||
private OnPreferenceTreeClickListener mOnPreferenceTreeClickListener;
|
||||
|
||||
|
||||
/**
|
||||
* @hide
|
||||
*/
|
||||
public PreferenceManager(Activity activity, int firstRequestCode) {
|
||||
mActivity = activity;
|
||||
mNextRequestCode = firstRequestCode;
|
||||
|
||||
|
||||
init(activity);
|
||||
}
|
||||
|
||||
@@ -170,7 +187,7 @@ public class PreferenceManager {
|
||||
|
||||
private void init(Context context) {
|
||||
mContext = context;
|
||||
|
||||
|
||||
setSharedPreferencesName(getDefaultSharedPreferencesName(context));
|
||||
}
|
||||
|
||||
@@ -184,14 +201,37 @@ public class PreferenceManager {
|
||||
/**
|
||||
* Returns the owning preference fragment, if any.
|
||||
*/
|
||||
@Nullable
|
||||
PreferenceFragment getFragment() {
|
||||
return mFragment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a {@link PreferenceDataStore} to be used by all Preferences associated with this manager
|
||||
* that don't have a custom {@link PreferenceDataStore} assigned. Also if the data store is set,
|
||||
* the Preferences will no longer use {@link android.content.SharedPreferences}.
|
||||
*
|
||||
* @param dataStore The {@link PreferenceDataStore} to be used by this manager.
|
||||
*/
|
||||
public void setPreferenceDataStore(PreferenceDataStore dataStore) {
|
||||
mPreferenceDataStore = dataStore;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link PreferenceDataStore} associated with this manager or {@code null} if
|
||||
* {@link android.content.SharedPreferences} are used instead.
|
||||
*
|
||||
* @return The {@link PreferenceDataStore} associated with this manager or {@code null} if none.
|
||||
*/
|
||||
@Nullable
|
||||
public PreferenceDataStore getPreferenceDataStore() {
|
||||
return mPreferenceDataStore;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of {@link Activity} (indirectly) that match a given
|
||||
* {@link Intent}.
|
||||
*
|
||||
*
|
||||
* @param queryIntent The Intent to match.
|
||||
* @return The list of {@link ResolveInfo} that point to the matched
|
||||
* activities.
|
||||
@@ -200,7 +240,7 @@ public class PreferenceManager {
|
||||
return mContext.getPackageManager().queryIntentActivities(queryIntent,
|
||||
PackageManager.GET_META_DATA);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inflates a preference hierarchy from the preference hierarchies of
|
||||
* {@link Activity Activities} that match the given {@link Intent}. An
|
||||
@@ -209,7 +249,7 @@ public class PreferenceManager {
|
||||
* <p>
|
||||
* If a preference hierarchy is given, the new preference hierarchies will
|
||||
* be merged in.
|
||||
*
|
||||
*
|
||||
* @param queryIntent The intent to match activities.
|
||||
* @param rootPreferences Optional existing hierarchy to merge the new
|
||||
* hierarchies into.
|
||||
@@ -232,7 +272,7 @@ public class PreferenceManager {
|
||||
// can be re-used across contexts
|
||||
final String uniqueResId = activityInfo.packageName + ":"
|
||||
+ activityInfo.metaData.getInt(METADATA_KEY_PREFERENCES);
|
||||
|
||||
|
||||
if (!inflatedRes.contains(uniqueResId)) {
|
||||
inflatedRes.add(uniqueResId);
|
||||
|
||||
@@ -244,7 +284,7 @@ public class PreferenceManager {
|
||||
+ Log.getStackTraceString(e));
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
final PreferenceInflater inflater = new PreferenceInflater(context, this);
|
||||
final XmlResourceParser parser = activityInfo.loadXmlMetaData(context
|
||||
.getPackageManager(), METADATA_KEY_PREFERENCES);
|
||||
@@ -255,14 +295,14 @@ public class PreferenceManager {
|
||||
}
|
||||
|
||||
rootPreferences.onAttachedToHierarchy(this);
|
||||
|
||||
|
||||
return rootPreferences;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inflates a preference hierarchy from XML. If a preference hierarchy is
|
||||
* given, the new preference hierarchies will be merged in.
|
||||
*
|
||||
*
|
||||
* @param context The context of the resource.
|
||||
* @param resId The resource ID of the XML to inflate.
|
||||
* @param rootPreferences Optional existing hierarchy to merge the new
|
||||
@@ -285,16 +325,16 @@ public class PreferenceManager {
|
||||
|
||||
return rootPreferences;
|
||||
}
|
||||
|
||||
|
||||
public PreferenceScreen createPreferenceScreen(Context context) {
|
||||
final PreferenceScreen preferenceScreen = new PreferenceScreen(context, null);
|
||||
preferenceScreen.onAttachedToHierarchy(this);
|
||||
return preferenceScreen;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called by a preference to get a unique ID in its hierarchy.
|
||||
*
|
||||
*
|
||||
* @return A unique ID.
|
||||
*/
|
||||
long getNextId() {
|
||||
@@ -302,11 +342,11 @@ public class PreferenceManager {
|
||||
return mNextId++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the current name of the SharedPreferences file that preferences managed by
|
||||
* this will use.
|
||||
*
|
||||
*
|
||||
* @return The name that can be passed to {@link Context#getSharedPreferences(String, int)}.
|
||||
* @see Context#getSharedPreferences(String, int)
|
||||
*/
|
||||
@@ -317,7 +357,7 @@ public class PreferenceManager {
|
||||
/**
|
||||
* Sets the name of the SharedPreferences file that preferences managed by this
|
||||
* will use.
|
||||
*
|
||||
*
|
||||
* @param sharedPreferencesName The name of the SharedPreferences file.
|
||||
* @see Context#getSharedPreferences(String, int)
|
||||
*/
|
||||
@@ -329,7 +369,7 @@ public class PreferenceManager {
|
||||
/**
|
||||
* Returns the current mode of the SharedPreferences file that preferences managed by
|
||||
* this will use.
|
||||
*
|
||||
*
|
||||
* @return The mode that can be passed to {@link Context#getSharedPreferences(String, int)}.
|
||||
* @see Context#getSharedPreferences(String, int)
|
||||
*/
|
||||
@@ -340,7 +380,7 @@ public class PreferenceManager {
|
||||
/**
|
||||
* Sets the mode of the SharedPreferences file that preferences managed by this
|
||||
* will use.
|
||||
*
|
||||
*
|
||||
* @param sharedPreferencesMode The mode of the SharedPreferences file.
|
||||
* @see Context#getSharedPreferences(String, int)
|
||||
*/
|
||||
@@ -449,7 +489,7 @@ public class PreferenceManager {
|
||||
/**
|
||||
* Gets a SharedPreferences instance that preferences managed by this will
|
||||
* use.
|
||||
*
|
||||
*
|
||||
* @return A SharedPreferences instance pointing to the file that contains
|
||||
* the values of preferences that are managed by this.
|
||||
*/
|
||||
@@ -471,14 +511,14 @@ public class PreferenceManager {
|
||||
mSharedPreferences = storageContext.getSharedPreferences(mSharedPreferencesName,
|
||||
mSharedPreferencesMode);
|
||||
}
|
||||
|
||||
|
||||
return mSharedPreferences;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a SharedPreferences instance that points to the default file that is
|
||||
* used by the preference framework in the given context.
|
||||
*
|
||||
*
|
||||
* @param context The context of the preferences whose values are wanted.
|
||||
* @return A SharedPreferences instance that can be used to retrieve and
|
||||
* listen to values of the preferences.
|
||||
@@ -504,49 +544,51 @@ public class PreferenceManager {
|
||||
|
||||
/**
|
||||
* Returns the root of the preference hierarchy managed by this class.
|
||||
*
|
||||
*
|
||||
* @return The {@link PreferenceScreen} object that is at the root of the hierarchy.
|
||||
*/
|
||||
@Nullable
|
||||
PreferenceScreen getPreferenceScreen() {
|
||||
return mPreferenceScreen;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the root of the preference hierarchy.
|
||||
*
|
||||
*
|
||||
* @param preferenceScreen The root {@link PreferenceScreen} of the preference hierarchy.
|
||||
* @return Whether the {@link PreferenceScreen} given is different than the previous.
|
||||
* @return Whether the {@link PreferenceScreen} given is different than the previous.
|
||||
*/
|
||||
boolean setPreferences(PreferenceScreen preferenceScreen) {
|
||||
if (preferenceScreen != mPreferenceScreen) {
|
||||
mPreferenceScreen = preferenceScreen;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Finds a {@link Preference} based on its key.
|
||||
*
|
||||
*
|
||||
* @param key The key of the preference to retrieve.
|
||||
* @return The {@link Preference} with the key, or null.
|
||||
* @see PreferenceGroup#findPreference(CharSequence)
|
||||
*/
|
||||
@Nullable
|
||||
public Preference findPreference(CharSequence key) {
|
||||
if (mPreferenceScreen == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return mPreferenceScreen.findPreference(key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the default values from an XML preference file by reading the values defined
|
||||
* by each {@link Preference} item's {@code android:defaultValue} attribute. This should
|
||||
* be called by the application's main activity.
|
||||
* <p>
|
||||
*
|
||||
*
|
||||
* @param context The context of the shared preferences.
|
||||
* @param resId The resource ID of the preference XML file.
|
||||
* @param readAgain Whether to re-read the default values.
|
||||
@@ -563,12 +605,12 @@ public class PreferenceManager {
|
||||
* parameter set to true.
|
||||
*/
|
||||
public static void setDefaultValues(Context context, @XmlRes int resId, boolean readAgain) {
|
||||
|
||||
|
||||
// Use the default shared preferences name and mode
|
||||
setDefaultValues(context, getDefaultSharedPreferencesName(context),
|
||||
getDefaultSharedPreferencesMode(), resId, readAgain);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Similar to {@link #setDefaultValues(Context, int, boolean)} but allows
|
||||
* the client to provide the filename and mode of the shared preferences
|
||||
@@ -592,7 +634,7 @@ public class PreferenceManager {
|
||||
* {@link PreferenceManager#getDefaultSharedPreferences(Context)}
|
||||
* and clear it followed by a call to this method with this
|
||||
* parameter set to true.
|
||||
*
|
||||
*
|
||||
* @see #setDefaultValues(Context, int, boolean)
|
||||
* @see #setSharedPreferencesName(String)
|
||||
* @see #setSharedPreferencesMode(int)
|
||||
@@ -601,7 +643,7 @@ public class PreferenceManager {
|
||||
int sharedPreferencesMode, int resId, boolean readAgain) {
|
||||
final SharedPreferences defaultValueSp = context.getSharedPreferences(
|
||||
KEY_HAS_SET_DEFAULT_VALUES, Context.MODE_PRIVATE);
|
||||
|
||||
|
||||
if (readAgain || !defaultValueSp.getBoolean(KEY_HAS_SET_DEFAULT_VALUES, false)) {
|
||||
final PreferenceManager pm = new PreferenceManager(context);
|
||||
pm.setSharedPreferencesName(sharedPreferencesName);
|
||||
@@ -620,33 +662,33 @@ public class PreferenceManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns an editor to use when modifying the shared preferences.
|
||||
* <p>
|
||||
* Do NOT commit unless {@link #shouldCommit()} returns true.
|
||||
*
|
||||
*
|
||||
* @return An editor to use to write to shared preferences.
|
||||
* @see #shouldCommit()
|
||||
*/
|
||||
SharedPreferences.Editor getEditor() {
|
||||
|
||||
|
||||
if (mNoCommit) {
|
||||
if (mEditor == null) {
|
||||
mEditor = getSharedPreferences().edit();
|
||||
}
|
||||
|
||||
|
||||
return mEditor;
|
||||
} else {
|
||||
return getSharedPreferences().edit();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Whether it is the client's responsibility to commit on the
|
||||
* {@link #getEditor()}. This will return false in cases where the writes
|
||||
* should be batched, for example when inflating preferences from XML.
|
||||
*
|
||||
*
|
||||
* @return Whether the client should commit.
|
||||
*/
|
||||
boolean shouldCommit() {
|
||||
@@ -674,18 +716,19 @@ public class PreferenceManager {
|
||||
* <p>
|
||||
* This will return null if this class was instantiated with a Context
|
||||
* instead of Activity. For example, when setting the default values.
|
||||
*
|
||||
*
|
||||
* @return The activity that shows the preferences.
|
||||
* @see #mContext
|
||||
*/
|
||||
@Nullable
|
||||
Activity getActivity() {
|
||||
return mActivity;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the context. This is preferred over {@link #getActivity()} when
|
||||
* possible.
|
||||
*
|
||||
*
|
||||
* @return The context.
|
||||
*/
|
||||
Context getContext() {
|
||||
@@ -694,7 +737,7 @@ public class PreferenceManager {
|
||||
|
||||
/**
|
||||
* Registers a listener.
|
||||
*
|
||||
*
|
||||
* @see OnActivityResultListener
|
||||
*/
|
||||
void registerOnActivityResultListener(OnActivityResultListener listener) {
|
||||
@@ -702,7 +745,7 @@ public class PreferenceManager {
|
||||
if (mActivityResultListeners == null) {
|
||||
mActivityResultListeners = new ArrayList<OnActivityResultListener>();
|
||||
}
|
||||
|
||||
|
||||
if (!mActivityResultListeners.contains(listener)) {
|
||||
mActivityResultListeners.add(listener);
|
||||
}
|
||||
@@ -711,7 +754,7 @@ public class PreferenceManager {
|
||||
|
||||
/**
|
||||
* Unregisters a listener.
|
||||
*
|
||||
*
|
||||
* @see OnActivityResultListener
|
||||
*/
|
||||
void unregisterOnActivityResultListener(OnActivityResultListener listener) {
|
||||
@@ -727,7 +770,7 @@ public class PreferenceManager {
|
||||
*/
|
||||
void dispatchActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
List<OnActivityResultListener> list;
|
||||
|
||||
|
||||
synchronized (this) {
|
||||
if (mActivityResultListeners == null) return;
|
||||
list = new ArrayList<OnActivityResultListener>(mActivityResultListeners);
|
||||
@@ -743,7 +786,7 @@ public class PreferenceManager {
|
||||
|
||||
/**
|
||||
* Registers a listener.
|
||||
*
|
||||
*
|
||||
* @see OnActivityStopListener
|
||||
* @hide
|
||||
*/
|
||||
@@ -752,16 +795,16 @@ public class PreferenceManager {
|
||||
if (mActivityStopListeners == null) {
|
||||
mActivityStopListeners = new ArrayList<OnActivityStopListener>();
|
||||
}
|
||||
|
||||
|
||||
if (!mActivityStopListeners.contains(listener)) {
|
||||
mActivityStopListeners.add(listener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Unregisters a listener.
|
||||
*
|
||||
*
|
||||
* @see OnActivityStopListener
|
||||
* @hide
|
||||
*/
|
||||
@@ -772,14 +815,14 @@ public class PreferenceManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called by the {@link PreferenceManager} to dispatch the activity stop
|
||||
* event.
|
||||
*/
|
||||
void dispatchActivityStop() {
|
||||
List<OnActivityStopListener> list;
|
||||
|
||||
|
||||
synchronized (this) {
|
||||
if (mActivityStopListeners == null) return;
|
||||
list = new ArrayList<OnActivityStopListener>(mActivityStopListeners);
|
||||
@@ -793,7 +836,7 @@ public class PreferenceManager {
|
||||
|
||||
/**
|
||||
* Registers a listener.
|
||||
*
|
||||
*
|
||||
* @see OnActivityDestroyListener
|
||||
*/
|
||||
void registerOnActivityDestroyListener(OnActivityDestroyListener listener) {
|
||||
@@ -807,10 +850,10 @@ public class PreferenceManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Unregisters a listener.
|
||||
*
|
||||
*
|
||||
* @see OnActivityDestroyListener
|
||||
*/
|
||||
void unregisterOnActivityDestroyListener(OnActivityDestroyListener listener) {
|
||||
@@ -820,14 +863,14 @@ public class PreferenceManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called by the {@link PreferenceManager} to dispatch the activity destroy
|
||||
* event.
|
||||
*/
|
||||
void dispatchActivityDestroy() {
|
||||
List<OnActivityDestroyListener> list = null;
|
||||
|
||||
|
||||
synchronized (this) {
|
||||
if (mActivityDestroyListeners != null) {
|
||||
list = new ArrayList<OnActivityDestroyListener>(mActivityDestroyListeners);
|
||||
@@ -844,11 +887,11 @@ public class PreferenceManager {
|
||||
// Dismiss any PreferenceScreens still showing
|
||||
dismissAllScreens();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a request code that is unique for the activity. Each subsequent
|
||||
* call to this method should return another unique request code.
|
||||
*
|
||||
*
|
||||
* @return A unique request code that will never be used by anyone other
|
||||
* than the caller of this method.
|
||||
*/
|
||||
@@ -857,32 +900,32 @@ public class PreferenceManager {
|
||||
return mNextRequestCode++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void addPreferencesScreen(DialogInterface screen) {
|
||||
synchronized (this) {
|
||||
|
||||
|
||||
if (mPreferencesScreens == null) {
|
||||
mPreferencesScreens = new ArrayList<DialogInterface>();
|
||||
}
|
||||
|
||||
|
||||
mPreferencesScreens.add(screen);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void removePreferencesScreen(DialogInterface screen) {
|
||||
synchronized (this) {
|
||||
|
||||
|
||||
if (mPreferencesScreens == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
mPreferencesScreens.remove(screen);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called by {@link PreferenceActivity} to dispatch the new Intent event.
|
||||
*
|
||||
*
|
||||
* @param intent The new Intent.
|
||||
*/
|
||||
void dispatchNewIntent(Intent intent) {
|
||||
@@ -894,34 +937,35 @@ public class PreferenceManager {
|
||||
ArrayList<DialogInterface> screensToDismiss;
|
||||
|
||||
synchronized (this) {
|
||||
|
||||
|
||||
if (mPreferencesScreens == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
screensToDismiss = new ArrayList<DialogInterface>(mPreferencesScreens);
|
||||
mPreferencesScreens.clear();
|
||||
}
|
||||
|
||||
|
||||
for (int i = screensToDismiss.size() - 1; i >= 0; i--) {
|
||||
screensToDismiss.get(i).dismiss();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the callback to be invoked when a {@link Preference} in the
|
||||
* hierarchy rooted at this {@link PreferenceManager} is clicked.
|
||||
*
|
||||
*
|
||||
* @param listener The callback to be invoked.
|
||||
*/
|
||||
void setOnPreferenceTreeClickListener(OnPreferenceTreeClickListener listener) {
|
||||
mOnPreferenceTreeClickListener = listener;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
OnPreferenceTreeClickListener getOnPreferenceTreeClickListener() {
|
||||
return mOnPreferenceTreeClickListener;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Interface definition for a callback to be invoked when a
|
||||
* {@link Preference} in the hierarchy rooted at this {@link PreferenceScreen} is
|
||||
@@ -933,7 +977,7 @@ public class PreferenceManager {
|
||||
/**
|
||||
* Called when a preference in the tree rooted at this
|
||||
* {@link PreferenceScreen} has been clicked.
|
||||
*
|
||||
*
|
||||
* @param preferenceScreen The {@link PreferenceScreen} that the
|
||||
* preference is located in.
|
||||
* @param preference The preference that was clicked.
|
||||
@@ -947,22 +991,22 @@ public class PreferenceManager {
|
||||
* receives an activity result.
|
||||
*/
|
||||
public interface OnActivityResultListener {
|
||||
|
||||
|
||||
/**
|
||||
* See Activity's onActivityResult.
|
||||
*
|
||||
*
|
||||
* @return Whether the request code was handled (in which case
|
||||
* subsequent listeners will not be called.
|
||||
*/
|
||||
boolean onActivityResult(int requestCode, int resultCode, Intent data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Interface definition for a class that will be called when the container's activity
|
||||
* is stopped.
|
||||
*/
|
||||
public interface OnActivityStopListener {
|
||||
|
||||
|
||||
/**
|
||||
* See Activity's onStop.
|
||||
*/
|
||||
@@ -974,7 +1018,7 @@ public class PreferenceManager {
|
||||
* is destroyed.
|
||||
*/
|
||||
public interface OnActivityDestroyListener {
|
||||
|
||||
|
||||
/**
|
||||
* See Activity's onDestroy.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user