From ca51e8788a58f2af3525b7214a675f2d0233e5da Mon Sep 17 00:00:00 2001 From: Adam Powell Date: Mon, 14 Feb 2011 19:54:29 -0800 Subject: [PATCH] Fix some bugs with MenuPopupHelper and ListPopupWindow Clean up handling of a few conditions in MenuPopupHelper that the monkeys manage to trigger around the use of ViewTreeObserver. (bug 3443819, bug 3312949) Fix a bug where a stale handler message could cause a ListPopupWindow to reopen itself after being dismissed. (bug 3453607) Change-Id: I488014767ccee785500862a2572beb35901d173b --- core/java/android/widget/ListPopupWindow.java | 1 + .../internal/view/menu/MenuPopupHelper.java | 22 ++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/core/java/android/widget/ListPopupWindow.java b/core/java/android/widget/ListPopupWindow.java index 8811492310958..560fc6801c125 100644 --- a/core/java/android/widget/ListPopupWindow.java +++ b/core/java/android/widget/ListPopupWindow.java @@ -604,6 +604,7 @@ public class ListPopupWindow { removePromptView(); mPopup.setContentView(null); mDropDownList = null; + mHandler.removeCallbacks(mResizePopupRunnable); } /** diff --git a/core/java/com/android/internal/view/menu/MenuPopupHelper.java b/core/java/com/android/internal/view/menu/MenuPopupHelper.java index 1f93eacf459be..6c9e7bb02dd91 100644 --- a/core/java/com/android/internal/view/menu/MenuPopupHelper.java +++ b/core/java/com/android/internal/view/menu/MenuPopupHelper.java @@ -101,8 +101,10 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On } if (anchor != null) { - mTreeObserver = anchor.getViewTreeObserver(); - mTreeObserver.addOnGlobalLayoutListener(this); + if (mTreeObserver == null) { + mTreeObserver = anchor.getViewTreeObserver(); + mTreeObserver.addOnGlobalLayoutListener(this); + } mPopup.setAnchorView(anchor); } else { return false; @@ -123,10 +125,10 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On public void onDismiss() { mPopup = null; - if (mTreeObserver != null) { - mTreeObserver.removeGlobalOnLayoutListener(MenuPopupHelper.this); - mTreeObserver = null; + if (mTreeObserver != null && mTreeObserver.isAlive()) { + mTreeObserver.removeGlobalOnLayoutListener(this); } + mTreeObserver = null; } public boolean isShowing() { @@ -134,6 +136,8 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On } public void onItemClick(AdapterView parent, View view, int position, long id) { + if (!isShowing()) return; + MenuItem item = null; if (mOverflowOnly) { item = mMenu.getOverflowItem(position); @@ -184,13 +188,15 @@ public class MenuPopupHelper implements AdapterView.OnItemClickListener, View.On @Override public void onGlobalLayout() { if (!isShowing()) { - mTreeObserver.removeGlobalOnLayoutListener(this); + if (mTreeObserver.isAlive()) { + mTreeObserver.removeGlobalOnLayoutListener(this); + } mTreeObserver = null; } else { final View anchor = mAnchorView != null ? mAnchorView.get() : null; - if (anchor != null && !anchor.isShown()) { + if (anchor == null || !anchor.isShown()) { dismiss(); - } else { + } else if (isShowing()) { // Recompute window size and position mPopup.show(); }