diff --git a/api/current.txt b/api/current.txt index 072d4bbf2cf7a..e1b13ae39bcae 100644 --- a/api/current.txt +++ b/api/current.txt @@ -30768,6 +30768,7 @@ package android.preference { method public android.preference.Preference.OnPreferenceChangeListener getOnPreferenceChangeListener(); method public android.preference.Preference.OnPreferenceClickListener getOnPreferenceClickListener(); method public int getOrder(); + method public android.preference.PreferenceGroup getParent(); method protected boolean getPersistedBoolean(boolean); method protected float getPersistedFloat(float); method protected int getPersistedInt(int); diff --git a/api/system-current.txt b/api/system-current.txt index c5f38fb3b81bf..b448647eac09d 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -33601,6 +33601,7 @@ package android.preference { method public android.preference.Preference.OnPreferenceChangeListener getOnPreferenceChangeListener(); method public android.preference.Preference.OnPreferenceClickListener getOnPreferenceClickListener(); method public int getOrder(); + method public android.preference.PreferenceGroup getParent(); method protected boolean getPersistedBoolean(boolean); method protected float getPersistedFloat(float); method protected int getPersistedInt(int); diff --git a/api/test-current.txt b/api/test-current.txt index c7367291781de..7b1032261fe49 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -30881,6 +30881,7 @@ package android.preference { method public android.preference.Preference.OnPreferenceChangeListener getOnPreferenceChangeListener(); method public android.preference.Preference.OnPreferenceClickListener getOnPreferenceClickListener(); method public int getOrder(); + method public android.preference.PreferenceGroup getParent(); method protected boolean getPersistedBoolean(boolean); method protected float getPersistedFloat(float); method protected int getPersistedInt(int); diff --git a/core/java/android/preference/Preference.java b/core/java/android/preference/Preference.java index 8303bca34beee..089eaeceac551 100644 --- a/core/java/android/preference/Preference.java +++ b/core/java/android/preference/Preference.java @@ -145,6 +145,8 @@ public class Preference implements Comparable { private List mDependents; + private PreferenceGroup mParentGroup; + private boolean mBaseMethodCalled; /** @@ -1238,6 +1240,16 @@ public class Preference implements Comparable { registerDependency(); } + /** + * Assigns a {@link PreferenceGroup} as the parent of this Preference. Set null to remove + * the current parent. + * + * @param parentGroup Parent preference group of this Preference or null if none. + */ + void assignParent(@Nullable PreferenceGroup parentGroup) { + mParentGroup = parentGroup; + } + private void registerDependency() { if (TextUtils.isEmpty(mDependencyKey)) return; @@ -1400,6 +1412,17 @@ public class Preference implements Comparable { return mDependencyKey; } + /** + * Returns the {@link PreferenceGroup} which is this Preference assigned to or null if this + * preference is not assigned to any group or is a root Preference. + * + * @return The parent PreferenceGroup or null if not attached to any. + */ + @Nullable + public PreferenceGroup getParent() { + return mParentGroup; + } + /** * Called when this Preference is being removed from the hierarchy. You * should remove any references to this Preference that you know about. Make diff --git a/core/java/android/preference/PreferenceGroup.java b/core/java/android/preference/PreferenceGroup.java index f17506b98a2b1..f135b26fc5dfd 100644 --- a/core/java/android/preference/PreferenceGroup.java +++ b/core/java/android/preference/PreferenceGroup.java @@ -29,14 +29,14 @@ import android.util.AttributeSet; * A container for multiple * {@link Preference} objects. It is a base class for Preference objects that are * parents, such as {@link PreferenceCategory} and {@link PreferenceScreen}. - * + * *
*

Developer Guides

*

For information about building a settings UI with Preferences, * read the Settings * guide.

*
- * + * * @attr ref android.R.styleable#PreferenceGroup_orderingFromXml */ public abstract class PreferenceGroup extends Preference implements GenericInflater.Parent { @@ -80,7 +80,7 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla *

* If this is called after preferences are added, they will not be * re-ordered in the order they were added, hence call this method early on. - * + * * @param orderingAsAdded Whether to order according to the order added. * @see Preference#setOrder(int) */ @@ -90,7 +90,7 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla /** * Whether this group is ordering preferences in the order they are added. - * + * * @return Whether this group orders based on the order the children are added. * @see #setOrderingAsAdded(boolean) */ @@ -115,7 +115,7 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla /** * Returns the {@link Preference} at a particular index. - * + * * @param index The index of the {@link Preference} to retrieve. * @return The {@link Preference}. */ @@ -126,7 +126,7 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla /** * Adds a {@link Preference} at the correct position based on the * preference's order. - * + * * @param preference The preference to add. * @return Whether the preference is now in this group. */ @@ -135,7 +135,7 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla // Exists return true; } - + if (preference.getOrder() == Preference.DEFAULT_ORDER) { if (mOrderingAsAdded) { preference.setOrder(mCurrentPreferenceOrder++); @@ -161,11 +161,12 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla } preference.onAttachedToHierarchy(getPreferenceManager()); - + preference.assignParent(this); + if (mAttachedToActivity) { preference.onAttachedToActivity(); } - + notifyHierarchyChanged(); return true; @@ -173,7 +174,7 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla /** * Removes a {@link Preference} from this group. - * + * * @param preference The preference to remove. * @return Whether the preference was found and removed. */ @@ -186,10 +187,13 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla private boolean removePreferenceInt(Preference preference) { synchronized(this) { preference.onPrepareForRemoval(); + if (preference.getParent() == this) { + preference.assignParent(null); + } return mPreferenceList.remove(preference); } } - + /** * Removes all {@link Preference Preferences} from this group. */ @@ -202,10 +206,10 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla } notifyHierarchyChanged(); } - + /** * Prepares a {@link Preference} to be added to the group. - * + * * @param preference The preference to add. * @return Whether to allow adding the preference (true), or not (false). */ @@ -223,7 +227,7 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla *

* This will recursively search for the preference into children that are * also {@link PreferenceGroup PreferenceGroups}. - * + * * @param key The key of the preference to retrieve. * @return The {@link Preference} with the key, or null. */ @@ -239,7 +243,7 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla if (curKey != null && curKey.equals(key)) { return preference; } - + if (preference instanceof PreferenceGroup) { final Preference returnedPreference = ((PreferenceGroup)preference) .findPreference(key); @@ -255,14 +259,14 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla /** * Whether this preference group should be shown on the same screen as its * contained preferences. - * + * * @return True if the contained preferences should be shown on the same * screen as this preference. */ protected boolean isOnSameScreenAsChildren() { return true; } - + @Override protected void onAttachedToActivity() { super.onAttachedToActivity(); @@ -270,7 +274,7 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla // Mark as attached so if a preference is later added to this group, we // can tell it we are already attached mAttachedToActivity = true; - + // Dispatch to all contained preferences final int preferenceCount = getPreferenceCount(); for (int i = 0; i < preferenceCount; i++) { @@ -281,7 +285,7 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla @Override protected void onPrepareForRemoval() { super.onPrepareForRemoval(); - + // We won't be attached to the activity anymore mAttachedToActivity = false; } @@ -297,7 +301,7 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla getPreference(i).onParentChanged(this, disableDependents); } } - + void sortPreferences() { synchronized (this) { Collections.sort(mPreferenceList); @@ -314,7 +318,7 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla getPreference(i).dispatchSaveInstanceState(container); } } - + @Override protected void dispatchRestoreInstanceState(Bundle container) { super.dispatchRestoreInstanceState(container);