Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ee3bc34c authored by Filip Pavlis's avatar Filip Pavlis
Browse files

Small cleanup or DataStore API javadoc.

There is also change in PreferenceManager.getSharedPreferences. The
original was not always returning null in case mPreferenceDataStore is
not null. That was contradictory with the java doc and conufing during
the runtime. Added tests for it.

Test: Separate CTS (2) being submitted along this one, see the topic.
Bug: b/34322039
Change-Id: I78a6758a674c3c8139bbe66ec17f2e29ca847828
parent 77aac13d
Loading
Loading
Loading
Loading
+24 −24
Original line number Original line Diff line number Diff line
@@ -458,8 +458,8 @@ public class Preference implements Comparable<Preference> {
    }
    }


    /**
    /**
     * Return the extras Bundle object associated with this preference,
     * Return the extras Bundle object associated with this preference, returning {@code null} if
     * returning null if there is not currently one.
     * there is not currently one.
     */
     */
    public Bundle peekExtras() {
    public Bundle peekExtras() {
        return mExtras;
        return mExtras;
@@ -974,7 +974,7 @@ public class Preference implements Comparable<Preference> {
     * the persistent {@link SharedPreferences} storage by default or into
     * the persistent {@link SharedPreferences} storage by default or into
     * {@link PreferenceDataStore} if assigned.
     * {@link PreferenceDataStore} if assigned.
     *
     *
     * @param persistent Set true if it should store its value(s) into the {@link SharedPreferences}.
     * @param persistent set {@code true} if it should store its value(s) into the storage.
     */
     */
    public void setPersistent(boolean persistent) {
    public void setPersistent(boolean persistent) {
        mPersistent = persistent;
        mPersistent = persistent;
@@ -1035,7 +1035,7 @@ public class Preference implements Comparable<Preference> {
     *
     *
     * @param preferenceScreen A {@link PreferenceScreen} whose hierarchy click
     * @param preferenceScreen A {@link PreferenceScreen} whose hierarchy click
     *            listener should be called in the proper order (between other
     *            listener should be called in the proper order (between other
     *            processing). May be null.
     *            processing). May be {@code null}.
     * @hide
     * @hide
     */
     */
    public void performClick(PreferenceScreen preferenceScreen) {
    public void performClick(PreferenceScreen preferenceScreen) {
@@ -1102,9 +1102,9 @@ public class Preference implements Comparable<Preference> {
     * {@link SharedPreferences}, this is intended behavior to improve
     * {@link SharedPreferences}, this is intended behavior to improve
     * performance.
     * performance.
     *
     *
     * @return The {@link SharedPreferences} where this Preference reads its value(s), or null if it
     * @return the {@link SharedPreferences} where this Preference reads its value(s). If
     *         isn't attached to a Preference hierarchy or if {@link PreferenceDataStore} is used
     *         this preference isn't attached to a Preference hierarchy or if
     *         instead.
     *         a {@link PreferenceDataStore} has been set, this method returns {@code null}.
     * @see #getEditor()
     * @see #getEditor()
     * @see #setPreferenceDataStore(PreferenceDataStore)
     * @see #setPreferenceDataStore(PreferenceDataStore)
     */
     */
@@ -1129,9 +1129,9 @@ public class Preference implements Comparable<Preference> {
     * not show up in the SharedPreferences, this is intended behavior to
     * not show up in the SharedPreferences, this is intended behavior to
     * improve performance.
     * improve performance.
     *
     *
     * @return A {@link SharedPreferences.Editor} where this preference saves its value(s), or null
     * @return a {@link SharedPreferences.Editor} where this preference saves its value(s). If
     *         if it isn't attached to a Preference hierarchy or if {@link PreferenceDataStore} is
     *         this preference isn't attached to a Preference hierarchy or if
     *         used instead.
     *         a {@link PreferenceDataStore} has been set, this method returns {@code null}.
     * @see #shouldCommit()
     * @see #shouldCommit()
     * @see #getSharedPreferences()
     * @see #getSharedPreferences()
     * @see #setPreferenceDataStore(PreferenceDataStore)
     * @see #setPreferenceDataStore(PreferenceDataStore)
@@ -1149,7 +1149,7 @@ public class Preference implements Comparable<Preference> {
     * {@link #getEditor()}. This may return false in situations where batch
     * {@link #getEditor()}. This may return false in situations where batch
     * committing is being done (by the manager) to improve performance.
     * committing is being done (by the manager) to improve performance.
     *
     *
     * <p>If this preference is using {@link PreferenceDataStore} this value should be irrelevant.
     * <p>If this preference is using {@link PreferenceDataStore} this value is irrelevant.
     *
     *
     * @return Whether the Preference should commit its saved value(s).
     * @return Whether the Preference should commit its saved value(s).
     * @see #getEditor()
     * @see #getEditor()
@@ -1253,10 +1253,10 @@ public class Preference implements Comparable<Preference> {
    }
    }


    /**
    /**
     * Assigns a {@link PreferenceGroup} as the parent of this Preference. Set null to remove
     * Assigns a {@link PreferenceGroup} as the parent of this Preference. Set {@code null} to
     * the current parent.
     * remove the current parent.
     *
     *
     * @param parentGroup Parent preference group of this Preference or null if none.
     * @param parentGroup Parent preference group of this Preference or {@code null} if none.
     */
     */
    void assignParent(@Nullable PreferenceGroup parentGroup) {
    void assignParent(@Nullable PreferenceGroup parentGroup) {
        mParentGroup = parentGroup;
        mParentGroup = parentGroup;
@@ -1425,10 +1425,10 @@ public class Preference implements Comparable<Preference> {
    }
    }


    /**
    /**
     * Returns the {@link PreferenceGroup} which is this Preference assigned to or null if this
     * Returns the {@link PreferenceGroup} which is this Preference assigned to or {@code null} if
     * preference is not assigned to any group or is a root Preference.
     * this preference is not assigned to any group or is a root Preference.
     *
     *
     * @return The parent PreferenceGroup or null if not attached to any.
     * @return the parent PreferenceGroup or {@code null} if not attached to any
     */
     */
    @Nullable
    @Nullable
    public PreferenceGroup getParent() {
    public PreferenceGroup getParent() {
@@ -1483,7 +1483,7 @@ public class Preference implements Comparable<Preference> {
     * if {@link #shouldPersist()} is true).
     * if {@link #shouldPersist()} is true).
     *
     *
     * <p>In case of using {@link PreferenceDataStore}, the <var>restorePersistedValue</var> is
     * <p>In case of using {@link PreferenceDataStore}, the <var>restorePersistedValue</var> is
     * always false. But the default value (if provided) is set.
     * always {@code true}. But the default value (if provided) is set.
     *
     *
     * <p>This may not always be called. One example is if it should not persist
     * <p>This may not always be called. One example is if it should not persist
     * but there is no default value given.
     * but there is no default value given.
@@ -1910,9 +1910,9 @@ public class Preference implements Comparable<Preference> {
     * state. This state should only contain information that is not persistent
     * state. This state should only contain information that is not persistent
     * or can be reconstructed later.
     * or can be reconstructed later.
     *
     *
     * @return A Parcelable object containing the current dynamic state of
     * @return A Parcelable object containing the current dynamic state of this Preference, or
     *         this Preference, or null if there is nothing interesting to save.
     *         {@code null} if there is nothing interesting to save. The default implementation
     *         The default implementation returns null.
     *         returns {@code null}.
     * @see #onRestoreInstanceState
     * @see #onRestoreInstanceState
     * @see #saveHierarchyState
     * @see #saveHierarchyState
     */
     */
@@ -1958,9 +1958,9 @@ public class Preference implements Comparable<Preference> {
    }
    }


    /**
    /**
     * Hook allowing a Preference to re-apply a representation of its internal
     * Hook allowing a Preference to re-apply a representation of its internal state that had
     * state that had previously been generated by {@link #onSaveInstanceState}.
     * previously been generated by {@link #onSaveInstanceState}. This function will never be called
     * This function will never be called with a null state.
     * with a {@code null} state.
     *
     *
     * @param state The saved state that had previously been returned by
     * @param state The saved state that had previously been returned by
     *            {@link #onSaveInstanceState}.
     *            {@link #onSaveInstanceState}.
+20 −13
Original line number Original line Diff line number Diff line
@@ -93,8 +93,8 @@ public class PreferenceManager {
    private SharedPreferences mSharedPreferences;
    private SharedPreferences mSharedPreferences;


    /**
    /**
     * Data store to be used by the Preferences or null if {@link android.content.SharedPreferences}
     * Data store to be used by the Preferences or {@code null} if
     * should be used.
     * {@link android.content.SharedPreferences} should be used.
     */
     */
    @Nullable
    @Nullable
    private PreferenceDataStore mPreferenceDataStore;
    private PreferenceDataStore mPreferenceDataStore;
@@ -484,11 +484,16 @@ public class PreferenceManager {
    /**
    /**
     * Gets a {@link SharedPreferences} instance that preferences managed by this will use.
     * Gets a {@link SharedPreferences} instance that preferences managed by this will use.
     *
     *
     * @return A {@link SharedPreferences} instance pointing to the file that contains the values of
     * @return a {@link SharedPreferences} instance pointing to the file that contains the values of
     * preferences that are managed by this or null if {@link PreferenceDataStore} is used instead.
     *         preferences that are managed by this PreferenceManager. If a
     *         {@link PreferenceDataStore} has been set, this method returns {@code null}.
     */
     */
    public SharedPreferences getSharedPreferences() {
    public SharedPreferences getSharedPreferences() {
        if (mSharedPreferences == null && getPreferenceDataStore() == null) {
        if (mPreferenceDataStore != null) {
            return null;
        }

        if (mSharedPreferences == null) {
            final Context storageContext;
            final Context storageContext;
            switch (mStorage) {
            switch (mStorage) {
                case STORAGE_DEVICE_PROTECTED:
                case STORAGE_DEVICE_PROTECTED:
@@ -564,8 +569,8 @@ public class PreferenceManager {
    /**
    /**
     * Finds a {@link Preference} based on its key.
     * Finds a {@link Preference} based on its key.
     *
     *
     * @param key The key of the preference to retrieve.
     * @param key the key of the preference to retrieve
     * @return The {@link Preference} with the key, or null.
     * @return the {@link Preference} with the key, or {@code null}
     * @see PreferenceGroup#findPreference(CharSequence)
     * @see PreferenceGroup#findPreference(CharSequence)
     */
     */
    @Nullable
    @Nullable
@@ -659,11 +664,11 @@ public class PreferenceManager {


    /**
    /**
     * Returns an editor to use when modifying the shared preferences.
     * 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 or null if
     * <p>Do NOT commit unless {@link #shouldCommit()} returns true.
     * {@link PreferenceDataStore} is used instead.
     *
     * @return an editor to use to write to shared preferences. If a {@link PreferenceDataStore}
     *         has been set, this method returns {@code null}.
     * @see #shouldCommit()
     * @see #shouldCommit()
     */
     */
    SharedPreferences.Editor getEditor() {
    SharedPreferences.Editor getEditor() {
@@ -687,6 +692,8 @@ public class PreferenceManager {
     * {@link #getEditor()}. This will return false in cases where the writes
     * {@link #getEditor()}. This will return false in cases where the writes
     * should be batched, for example when inflating preferences from XML.
     * should be batched, for example when inflating preferences from XML.
     *
     *
     * <p>If preferences are using {@link PreferenceDataStore} this value is irrelevant.
     *
     * @return Whether the client should commit.
     * @return Whether the client should commit.
     */
     */
    boolean shouldCommit() {
    boolean shouldCommit() {
@@ -711,8 +718,8 @@ public class PreferenceManager {
     * Returns the activity that shows the preferences. This is useful for doing
     * Returns the activity that shows the preferences. This is useful for doing
     * managed queries, but in most cases the use of {@link #getContext()} is
     * managed queries, but in most cases the use of {@link #getContext()} is
     * preferred.
     * preferred.
     * <p>
     *
     * This will return null if this class was instantiated with a Context
     * <p>This will return {@code null} if this class was instantiated with a Context
     * instead of Activity. For example, when setting the default values.
     * instead of Activity. For example, when setting the default values.
     *
     *
     * @return The activity that shows the preferences.
     * @return The activity that shows the preferences.