Bug 729252 - Cache cursor data for AwesomeBar context menu. r=mfinkle

This commit is contained in:
Brian Nicholson 2012-03-05 13:31:55 -08:00
parent fef4714ccf
commit 84bac5f8c4

View File

@ -97,6 +97,7 @@ public class AwesomeBar extends Activity implements GeckoEventListener {
private AwesomeBarEditText mText; private AwesomeBarEditText mText;
private ImageButton mGoButton; private ImageButton mGoButton;
private ContentResolver mResolver; private ContentResolver mResolver;
private ContextMenuSubject mContextMenuSubject;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -410,14 +411,25 @@ public class AwesomeBar extends Activity implements GeckoEventListener {
GeckoAppShell.unregisterGeckoEventListener("SearchEngines:Data", this); GeckoAppShell.unregisterGeckoEventListener("SearchEngines:Data", this);
} }
private Object mContextMenuSubject = null; private class ContextMenuSubject {
public int id;
public String url;
public byte[] favicon;
public String title;
public ContextMenuSubject(int id, String url, byte[] favicon, String title) {
this.id = id;
this.url = url;
this.favicon = favicon;
this.title = title;
}
};
@Override @Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo); super.onCreateContextMenu(menu, view, menuInfo);
ListView list = (ListView) view; ListView list = (ListView) view;
Object selectedItem = null; mContextMenuSubject = null;
String title = "";
if (list == findViewById(R.id.history_list)) { if (list == findViewById(R.id.history_list)) {
if (!(menuInfo instanceof ExpandableListView.ExpandableListContextMenuInfo)) { if (!(menuInfo instanceof ExpandableListView.ExpandableListContextMenuInfo)) {
@ -434,12 +446,12 @@ public class AwesomeBar extends Activity implements GeckoEventListener {
return; return;
ExpandableListView exList = (ExpandableListView) list; ExpandableListView exList = (ExpandableListView) list;
selectedItem = exList.getExpandableListAdapter().getChild(groupPosition, childPosition);
// The history list is backed by a SimpleExpandableListAdapter // The history list is backed by a SimpleExpandableListAdapter
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
Map map = (Map) selectedItem; Map map = (Map) exList.getExpandableListAdapter().getChild(groupPosition, childPosition);
title = (String) map.get(URLColumns.TITLE); mContextMenuSubject = new ContextMenuSubject(-1, (String)map.get(URLColumns.URL),
(byte[]) map.get(URLColumns.FAVICON), (String)map.get(URLColumns.TITLE));
} else { } else {
if (!(menuInfo instanceof AdapterView.AdapterContextMenuInfo)) { if (!(menuInfo instanceof AdapterView.AdapterContextMenuInfo)) {
Log.e(LOGTAG, "menuInfo is not AdapterContextMenuInfo"); Log.e(LOGTAG, "menuInfo is not AdapterContextMenuInfo");
@ -447,7 +459,7 @@ public class AwesomeBar extends Activity implements GeckoEventListener {
} }
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
selectedItem = list.getItemAtPosition(info.position); Object selectedItem = list.getItemAtPosition(info.position);
if (!(selectedItem instanceof Cursor)) { if (!(selectedItem instanceof Cursor)) {
Log.e(LOGTAG, "item at " + info.position + " is not a Cursor"); Log.e(LOGTAG, "item at " + info.position + " is not a Cursor");
@ -455,21 +467,19 @@ public class AwesomeBar extends Activity implements GeckoEventListener {
} }
Cursor cursor = (Cursor) selectedItem; Cursor cursor = (Cursor) selectedItem;
title = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE));
// Don't show the context menu for folders // Don't show the context menu for folders
if (list == findViewById(R.id.bookmarks_list) && if (!(list == findViewById(R.id.bookmarks_list) && cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks.IS_FOLDER)) == 1)) {
cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks.IS_FOLDER)) == 1) { mContextMenuSubject = new ContextMenuSubject(cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks._ID)),
selectedItem = null; cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL)),
cursor.getBlob(cursor.getColumnIndexOrThrow(URLColumns.FAVICON)),
cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE))
);
} }
} }
if (selectedItem == null || !((selectedItem instanceof Cursor) || (selectedItem instanceof Map))) { if (mContextMenuSubject == null)
mContextMenuSubject = null;
return; return;
}
mContextMenuSubject = selectedItem;
MenuInflater inflater = getMenuInflater(); MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.awesomebar_contextmenu, menu); inflater.inflate(R.menu.awesomebar_contextmenu, menu);
@ -479,7 +489,7 @@ public class AwesomeBar extends Activity implements GeckoEventListener {
removeBookmarkItem.setVisible(false); removeBookmarkItem.setVisible(false);
} }
menu.setHeaderTitle(title); menu.setHeaderTitle(mContextMenuSubject.title);
} }
@Override @Override
@ -487,27 +497,10 @@ public class AwesomeBar extends Activity implements GeckoEventListener {
if (mContextMenuSubject == null) if (mContextMenuSubject == null)
return false; return false;
final int id; final int id = mContextMenuSubject.id;
final String url; final String url = mContextMenuSubject.url;
byte[] b = null; final byte[] b = mContextMenuSubject.favicon;
String title = ""; final String title = mContextMenuSubject.title;
if (mContextMenuSubject instanceof Cursor) {
Cursor cursor = (Cursor)mContextMenuSubject;
id = cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks._ID));
url = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL));
b = cursor.getBlob(cursor.getColumnIndexOrThrow(URLColumns.FAVICON));
title = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE));
} else if (mContextMenuSubject instanceof Map) {
@SuppressWarnings("rawtypes") Map map = (Map)mContextMenuSubject;
id = -1;
url = (String)map.get(URLColumns.URL);
b = (byte[]) map.get(URLColumns.FAVICON);
title = (String)map.get(URLColumns.TITLE);
} else {
return false;
}
mContextMenuSubject = null;
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.open_new_tab: { case R.id.open_new_tab: {