Bug 712627 - Fennec crashes if a long tap is performed in AwesomeBar/History [r=mfinkle]
authorWesley Johnston <wjohnston@mozilla.com>
Tue, 27 Dec 2011 01:07:46 -0500
changeset 84603 e63189dcf24ca0200396648d40caa09f645bd7d1
parent 84602 04815244051b3a7c079583e29c3446fb3aadbb8f
child 84604 02f09fcbf282b023b27183132353dfd8028a2ebf
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs712627
milestone12.0a1
Bug 712627 - Fennec crashes if a long tap is performed in AwesomeBar/History [r=mfinkle]
mobile/android/base/AwesomeBar.java
--- a/mobile/android/base/AwesomeBar.java
+++ b/mobile/android/base/AwesomeBar.java
@@ -63,20 +63,23 @@ import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.inputmethod.InputMethodManager;
 import android.view.inputmethod.EditorInfo;
 import android.widget.AdapterView;
 import android.widget.AdapterView.AdapterContextMenuInfo;
 import android.widget.Button;
 import android.widget.EditText;
+import android.widget.ExpandableListView;
 import android.widget.ImageButton;
 import android.widget.RelativeLayout;
 import android.widget.ListView;
 
+import java.util.Map;
+
 import org.mozilla.gecko.db.BrowserDB.URLColumns;
 import org.mozilla.gecko.db.BrowserDB;
 
 import org.json.JSONArray;
 import org.json.JSONObject;
 
 public class AwesomeBar extends Activity implements GeckoEventListener {
     private static final String LOGTAG = "GeckoAwesomeBar";
@@ -381,78 +384,102 @@ public class AwesomeBar extends Activity
 
     @Override
     public void onDestroy() {
         super.onDestroy();
         mAwesomeTabs.destroy();
         GeckoAppShell.unregisterGeckoEventListener("SearchEngines:Data", this);
     }
 
-    private Cursor mContextMenuCursor = null;
+    private Object mContextMenuSubject = null;
 
     @Override
     public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
         super.onCreateContextMenu(menu, view, menuInfo);
+        ListView list = (ListView) view;
+        Object selectedItem = null;
+        String title = "";
 
-        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
-        ListView list = (ListView) view;
-        Object selecteditem = list.getItemAtPosition(info.position);
+        if (view == (ListView)findViewById(R.id.history_list)) {
+            ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
+            ExpandableListView exList = (ExpandableListView)list;
+            int childPosition = exList.getPackedPositionChild(info.packedPosition);
+            int groupPosition = exList.getPackedPositionGroup(info.packedPosition);
+            Log.i(LOGTAG, "wesj - Got position " + groupPosition + " - " + childPosition);
+            selectedItem = exList.getExpandableListAdapter().getChild(groupPosition, childPosition);
 
-        if (!(selecteditem instanceof Cursor)) {
-            mContextMenuCursor = null;
+            Map<String, Object> map = (Map<String, Object>)selectedItem;
+            title = (String)map.get(URLColumns.TITLE);
+        } else {
+            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
+            selectedItem = list.getItemAtPosition(info.position);
+
+            Cursor cursor = (Cursor)selectedItem;
+            title = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE));
+        }
+
+        if (selectedItem == null || !((selectedItem instanceof Cursor) || (selectedItem instanceof Map))) {
+            mContextMenuSubject = null;
             return;
         }
 
-        mContextMenuCursor = (Cursor) selecteditem;
+        mContextMenuSubject = selectedItem;
 
         MenuInflater inflater = getMenuInflater();
         inflater.inflate(R.menu.awesomebar_contextmenu, menu);
 
-        String title = mContextMenuCursor.getString(mContextMenuCursor.getColumnIndexOrThrow(URLColumns.TITLE));
         menu.setHeaderTitle(title);
     }
 
     @Override
     public boolean onContextItemSelected(MenuItem item) {
-        if (mContextMenuCursor == null)
+        if (mContextMenuSubject == null)
             return false;
 
-        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
+        String url = "";
+        byte[] b = null;
+        String title = "";
+        if (mContextMenuSubject instanceof Cursor) {
+            Cursor cursor = (Cursor)mContextMenuSubject;
+            url = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL));
+            b = (byte[]) cursor.getBlob(cursor.getColumnIndexOrThrow(URLColumns.FAVICON));
+            title = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE));
+        } else if (mContextMenuSubject instanceof Map) {
+            Map<String, Object> map = (Map<String, Object>)mContextMenuSubject;
+            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()) {
             case R.id.open_new_tab: {
-                String url = mContextMenuCursor.getString(mContextMenuCursor.getColumnIndexOrThrow(URLColumns.URL));
                 GeckoApp.mAppContext.loadUrl(url, AwesomeBar.Type.ADD);
                 break;
             }
             case R.id.add_to_launcher: {
-                String url = mContextMenuCursor.getString(mContextMenuCursor.getColumnIndexOrThrow(URLColumns.URL));
-                byte[] b = (byte[]) mContextMenuCursor.getBlob(mContextMenuCursor.getColumnIndexOrThrow(URLColumns.FAVICON));
-                String title = mContextMenuCursor.getString(mContextMenuCursor.getColumnIndexOrThrow(URLColumns.TITLE));
-    
                 Bitmap bitmap = null;
                 if (b != null)
                     bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
     
                 GeckoAppShell.createShortcut(title, url, bitmap, "");
                 break;
             }
             case R.id.share: {
-                String url = mContextMenuCursor.getString(mContextMenuCursor.getColumnIndexOrThrow(URLColumns.URL));
-                String title = mContextMenuCursor.getString(mContextMenuCursor.getColumnIndexOrThrow(URLColumns.TITLE));
                 GeckoAppShell.openUriExternal(url, "text/plain", "", "",
                                               Intent.ACTION_SEND, title);
                 break;
             }
             default: {
-                mContextMenuCursor = null;
                 return super.onContextItemSelected(item);
             }
         }
-        mContextMenuCursor = null;
         return true;
     }
 
     public static class AwesomeBarEditText extends EditText {
         OnKeyPreImeListener mOnKeyPreImeListener;
 
         public interface OnKeyPreImeListener {
             public boolean onKeyPreIme(View v, int keyCode, KeyEvent event);