From 6690d018b9c08757d20182170948a3cba961e84c Mon Sep 17 00:00:00 2001 From: Adam Powell Date: Wed, 17 Jun 2015 16:41:56 -0700 Subject: [PATCH] Un-hide ViewGroup#onViewAdded/onViewRemoved These methods are generally useful for writing custom views, and by exposing them we make it easier for custom view authors to still allow app developers to use an OnHierarchyChangedListener since it will not be occupied by a custom view's implementation. Also move the actual dispatch to package-scoped dispatch methods so that a developer forgetting to call super won't stop a listener from functioning. Bug 21866523 Change-Id: Ie2bb5e241d7c5a02a5033f33ecdaeb40aceb20b5 --- api/current.txt | 2 ++ api/system-current.txt | 2 ++ core/java/android/view/ViewGroup.java | 36 +++++++++++++------ core/java/android/widget/GridLayout.java | 12 ++----- .../stack/NotificationStackScrollLayout.java | 4 +-- 5 files changed, 33 insertions(+), 23 deletions(-) diff --git a/api/current.txt b/api/current.txt index b72730a8cb90a..27abd247400f5 100644 --- a/api/current.txt +++ b/api/current.txt @@ -36817,6 +36817,8 @@ package android.view { method public boolean onRequestSendAccessibilityEvent(android.view.View, android.view.accessibility.AccessibilityEvent); method public boolean onStartNestedScroll(android.view.View, android.view.View, int); method public void onStopNestedScroll(android.view.View); + method public void onViewAdded(android.view.View); + method public void onViewRemoved(android.view.View); method public void recomputeViewAttributes(android.view.View); method public void removeAllViews(); method public void removeAllViewsInLayout(); diff --git a/api/system-current.txt b/api/system-current.txt index e328920959915..d0795c0b40d89 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -39105,6 +39105,8 @@ package android.view { method public boolean onRequestSendAccessibilityEvent(android.view.View, android.view.accessibility.AccessibilityEvent); method public boolean onStartNestedScroll(android.view.View, android.view.View, int); method public void onStopNestedScroll(android.view.View); + method public void onViewAdded(android.view.View); + method public void onViewRemoved(android.view.View); method public void recomputeViewAttributes(android.view.View); method public void removeAllViews(); method public void removeAllViewsInLayout(); diff --git a/core/java/android/view/ViewGroup.java b/core/java/android/view/ViewGroup.java index 73cfd8c381314..2e2ba881425a0 100644 --- a/core/java/android/view/ViewGroup.java +++ b/core/java/android/view/ViewGroup.java @@ -4148,24 +4148,38 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager mOnHierarchyChangeListener = listener; } - /** - * @hide - */ - protected void onViewAdded(View child) { + void dispatchViewAdded(View child) { + onViewAdded(child); if (mOnHierarchyChangeListener != null) { mOnHierarchyChangeListener.onChildViewAdded(this, child); } } /** - * @hide + * Called when a new child is added to this ViewGroup. Overrides should always + * call super.onViewAdded. + * + * @param child the added child view */ - protected void onViewRemoved(View child) { + public void onViewAdded(View child) { + } + + void dispatchViewRemoved(View child) { + onViewRemoved(child); if (mOnHierarchyChangeListener != null) { mOnHierarchyChangeListener.onChildViewRemoved(this, child); } } + /** + * Called when a child view is removed from this ViewGroup. Overrides should always + * call super.onViewRemoved. + * + * @param child the removed child view + */ + public void onViewRemoved(View child) { + } + private void clearCachedLayoutMode() { if (!hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) { mLayoutMode = LAYOUT_MODE_UNDEFINED; @@ -4292,7 +4306,7 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager child.resetRtlProperties(); } - onViewAdded(child); + dispatchViewAdded(child); if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) { mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE; @@ -4554,7 +4568,7 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager } } - onViewRemoved(view); + dispatchViewRemoved(view); if (view.getVisibility() != View.GONE) { notifySubtreeAccessibilityStateChangedIfNeeded(); @@ -4646,7 +4660,7 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager needGlobalAttributesUpdate(false); - onViewRemoved(view); + dispatchViewRemoved(view); } removeFromArray(start, count); @@ -4729,7 +4743,7 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager childHasTransientStateChanged(view, false); } - onViewRemoved(view); + dispatchViewRemoved(view); view.mParent = null; children[i] = null; @@ -4788,7 +4802,7 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager childHasTransientStateChanged(child, false); } - onViewRemoved(child); + dispatchViewRemoved(child); } /** diff --git a/core/java/android/widget/GridLayout.java b/core/java/android/widget/GridLayout.java index 6cc4bda435100..258424a2e574d 100644 --- a/core/java/android/widget/GridLayout.java +++ b/core/java/android/widget/GridLayout.java @@ -935,22 +935,14 @@ public class GridLayout extends ViewGroup { super.onDebugDraw(canvas); } - // Add/remove - - /** - * @hide - */ @Override - protected void onViewAdded(View child) { + public void onViewAdded(View child) { super.onViewAdded(child); invalidateStructure(); } - /** - * @hide - */ @Override - protected void onViewRemoved(View child) { + public void onViewRemoved(View child) { super.onViewRemoved(child); invalidateStructure(); } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationStackScrollLayout.java b/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationStackScrollLayout.java index 1bf4547c61db2..1bb07a89dcdf3 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationStackScrollLayout.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationStackScrollLayout.java @@ -1608,7 +1608,7 @@ public class NotificationStackScrollLayout extends ViewGroup } @Override - protected void onViewRemoved(View child) { + public void onViewRemoved(View child) { super.onViewRemoved(child); // we only call our internal methods if this is actually a removal and not just a // notification which becomes a child notification @@ -1745,7 +1745,7 @@ public class NotificationStackScrollLayout extends ViewGroup } @Override - protected void onViewAdded(View child) { + public void onViewAdded(View child) { super.onViewAdded(child); onViewAddedInternal(child); }