Bug 1116693 - Support stopping/starting dispatch of onItemChanged updates r=rnewman
authorMark Finkle <mfinkle@mozilla.com>
Wed, 31 Dec 2014 14:43:59 -0500
changeset 247599 e517f97929b49683fcc3804a9b9820597f33a075
parent 247598 0d284cdc38f6a7031d811a12638f6d8f619e730a
child 247600 f0568ac27834c6246d035fdfdf015224f5079b02
push id4489
push userraliiev@mozilla.com
push dateMon, 23 Feb 2015 15:17:55 +0000
treeherdermozilla-beta@fd7c3dc24146 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs1116693
milestone37.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1116693 - Support stopping/starting dispatch of onItemChanged updates r=rnewman
mobile/android/base/menu/GeckoMenuItem.java
--- a/mobile/android/base/menu/GeckoMenuItem.java
+++ b/mobile/android/base/menu/GeckoMenuItem.java
@@ -47,30 +47,64 @@ public class GeckoMenuItem implements Me
     private Drawable mIcon;
     private int mIconRes;
     private GeckoActionProvider mActionProvider;
     private GeckoSubMenu mSubMenu;
     private MenuItem.OnMenuItemClickListener mMenuItemClickListener;
     final GeckoMenu mMenu;
     OnShowAsActionChangedListener mShowAsActionChangedListener;
 
+    private volatile boolean mShouldDispatchChanges = true;
+    private volatile boolean mDidChange;
+
     public GeckoMenuItem(GeckoMenu menu, int id, int order, int titleRes) {
         mMenu = menu;
         mId = id;
         mOrder = order;
         setTitle(titleRes);
     }
 
     public GeckoMenuItem(GeckoMenu menu, int id, int order, CharSequence title) {
         mMenu = menu;
         mId = id;
         mOrder = order;
         setTitle(title);
     }
 
+    /**
+     * Stop dispatching item changed events to presenters until
+     * [start|resume]DispatchingItemsChanged() is called. Useful when
+     * many menu operations are going to be performed as a batch.
+     */
+    public void stopDispatchingChanges() {
+        mDidChange = false;
+        mShouldDispatchChanges = false;
+    }
+
+    /**
+     * Resume dispatching item changed events to presenters. This method
+     * will NOT call onItemChanged if any menu operations were queued.
+     * Only future menu operations will call onItemChanged. Useful for
+     * sequelching presenter updates.
+     */
+    public void resumeDispatchingChanges() {
+        mShouldDispatchChanges = true;
+    }
+
+    /**
+     * Start dispatching item changed events to presenters. This method
+     * will call onItemChanged if any menu operations were queued.
+     */
+    public void startDispatchingChanges() {
+        if (mDidChange) {
+            mMenu.onItemChanged(this);
+        }
+        mShouldDispatchChanges = true;
+    }
+
     @Override
     public boolean collapseActionView() {
         return false;
     }
 
     @Override
     public boolean expandActionView() {
         return false;
@@ -242,53 +276,73 @@ public class GeckoMenuItem implements Me
     public MenuItem setAlphabeticShortcut(char alphaChar) {
         return this;
     }
 
     @Override
     public MenuItem setCheckable(boolean checkable) {
         if (mCheckable != checkable) {
             mCheckable = checkable;
-            mMenu.onItemChanged(this);
+            if (mShouldDispatchChanges) {
+                mMenu.onItemChanged(this);
+            } else {
+                mDidChange = true;
+            }
         }
         return this;
     }
 
     @Override
     public MenuItem setChecked(boolean checked) {
         if (mChecked != checked) {
             mChecked = checked;
-            mMenu.onItemChanged(this);
+            if (mShouldDispatchChanges) {
+                mMenu.onItemChanged(this);
+            } else {
+                mDidChange = true;
+            }
         }
         return this;
     }
 
     @Override
     public MenuItem setEnabled(boolean enabled) {
         if (mEnabled != enabled) {
             mEnabled = enabled;
-            mMenu.onItemChanged(this);
+            if (mShouldDispatchChanges) {
+                mMenu.onItemChanged(this);
+            } else {
+                mDidChange = true;
+            }
         }
         return this;
     }
 
     @Override
     public MenuItem setIcon(Drawable icon) {
         if (mIcon != icon) {
             mIcon = icon;
-            mMenu.onItemChanged(this);
+            if (mShouldDispatchChanges) {
+                mMenu.onItemChanged(this);
+            } else {
+                mDidChange = true;
+            }
         }
         return this;
     }
 
     @Override
     public MenuItem setIcon(int iconRes) {
         if (mIconRes != iconRes) {
             mIconRes = iconRes;
-            mMenu.onItemChanged(this);
+            if (mShouldDispatchChanges) {
+                mMenu.onItemChanged(this);
+            } else {
+                mDidChange = true;
+            }
         }
         return this;
     }
 
     @Override
     public MenuItem setIntent(Intent intent) {
         return this;
     }
@@ -362,17 +416,21 @@ public class GeckoMenuItem implements Me
         mSubMenu = subMenu;
         return this;
     }
 
     @Override
     public MenuItem setTitle(CharSequence title) {
         if (!TextUtils.equals(mTitle, title)) {
             mTitle = title;
-            mMenu.onItemChanged(this);
+            if (mShouldDispatchChanges) {
+                mMenu.onItemChanged(this);
+            } else {
+                mDidChange = true;
+            }
         }
         return this;
     }
 
     @Override
     public MenuItem setTitle(int title) {
         CharSequence newTitle = mMenu.getResources().getString(title);
         return setTitle(newTitle);
@@ -383,17 +441,21 @@ public class GeckoMenuItem implements Me
         mTitleCondensed = title;
         return this;
     }
 
     @Override
     public MenuItem setVisible(boolean visible) {
         if (mVisible != visible) {
             mVisible = visible;
-            mMenu.onItemChanged(this);
+            if (mShouldDispatchChanges) {
+                mMenu.onItemChanged(this);
+            } else {
+                mDidChange = true;
+            }
         }
         return this;
     }
 
     public boolean invoke() {
         if (mMenuItemClickListener != null)
             return mMenuItemClickListener.onMenuItemClick(this);
         else