Bug 1116599 - Use a cache to create a GeckoMenu.findItem fast path r=rnewman

This commit is contained in:
Mark Finkle 2014-12-31 14:32:44 -05:00
Родитель 77b4193420
Коммит 2cfccff4b5
1 изменённых файлов: 19 добавлений и 1 удалений

Просмотреть файл

@ -15,6 +15,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
import android.util.SparseArray;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
@ -90,6 +91,9 @@ public class GeckoMenu extends ListView
// List of all menu items. // List of all menu items.
private final List<GeckoMenuItem> mItems; private final List<GeckoMenuItem> mItems;
// Quick lookup array used to make a fast path in findItem.
private final SparseArray<MenuItem> mItemsById;
// Map of "always" action-items in action-bar and their views. // Map of "always" action-items in action-bar and their views.
private final Map<GeckoMenuItem, View> mPrimaryActionItems; private final Map<GeckoMenuItem, View> mPrimaryActionItems;
@ -134,6 +138,7 @@ public class GeckoMenu extends ListView
setOnItemClickListener(this); setOnItemClickListener(this);
mItems = new ArrayList<GeckoMenuItem>(); mItems = new ArrayList<GeckoMenuItem>();
mItemsById = new SparseArray<MenuItem>();
mPrimaryActionItems = new HashMap<GeckoMenuItem, View>(); mPrimaryActionItems = new HashMap<GeckoMenuItem, View>();
mSecondaryActionItems = new HashMap<GeckoMenuItem, View>(); mSecondaryActionItems = new HashMap<GeckoMenuItem, View>();
@ -362,15 +367,24 @@ public class GeckoMenu extends ListView
@Override @Override
public MenuItem findItem(int id) { public MenuItem findItem(int id) {
assertOnUiThread();
MenuItem quickItem = mItemsById.get(id);
if (quickItem != null) {
return quickItem;
}
for (GeckoMenuItem menuItem : mItems) { for (GeckoMenuItem menuItem : mItems) {
if (menuItem.getItemId() == id) { if (menuItem.getItemId() == id) {
mItemsById.put(id, menuItem);
return menuItem; return menuItem;
} else if (menuItem.hasSubMenu()) { } else if (menuItem.hasSubMenu()) {
if (!menuItem.hasActionProvider()) { if (!menuItem.hasActionProvider()) {
SubMenu subMenu = menuItem.getSubMenu(); SubMenu subMenu = menuItem.getSubMenu();
MenuItem item = subMenu.findItem(id); MenuItem item = subMenu.findItem(id);
if (item != null) if (item != null) {
mItemsById.put(id, item);
return item; return item;
}
} }
} }
} }
@ -424,6 +438,9 @@ public class GeckoMenu extends ListView
if (item == null) if (item == null)
return; return;
// Remove it from the cache.
mItemsById.remove(id);
// Remove it from any sub-menu. // Remove it from any sub-menu.
for (GeckoMenuItem menuItem : mItems) { for (GeckoMenuItem menuItem : mItems) {
if (menuItem.hasSubMenu()) { if (menuItem.hasSubMenu()) {
@ -811,6 +828,7 @@ public class GeckoMenu extends ListView
} }
public void clear() { public void clear() {
mItemsById.clear();
mItems.clear(); mItems.clear();
notifyDataSetChanged(); notifyDataSetChanged();
} }