Bug 881780 - Move ContextMenu creation handling to HomeFragment (r=sriram)
authorLucas Rocha <lucasr@mozilla.com>
Fri, 14 Jun 2013 17:35:11 +0100
changeset 143339 1298900196328e9b950e5d1ac1f102307d4dad23
parent 143338 62f98d2e95087e95f49a72c61dbf35218e9116b6
child 143340 621adb50d7eb3111415f7a9cc9473d53b5216314
push id25130
push userlrocha@mozilla.com
push dateWed, 21 Aug 2013 09:41:27 +0000
treeherdermozilla-central@b2486721572e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssriram
bugs881780
milestone24.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 881780 - Move ContextMenu creation handling to HomeFragment (r=sriram)
mobile/android/base/home/BookmarksPage.java
mobile/android/base/home/HomeFragment.java
--- a/mobile/android/base/home/BookmarksPage.java
+++ b/mobile/android/base/home/BookmarksPage.java
@@ -1,37 +1,31 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.home;
 
 import org.mozilla.gecko.R;
-import org.mozilla.gecko.Tab;
-import org.mozilla.gecko.Tabs;
 import org.mozilla.gecko.db.BrowserContract.Bookmarks;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.BrowserDB.URLColumns;
-import org.mozilla.gecko.home.HomeListView.HomeContextMenuInfo;
 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
 import org.mozilla.gecko.util.GamepadUtils;
 import org.mozilla.gecko.util.ThreadUtils;
 
 import android.app.Activity;
 import android.content.Context;
 import android.content.res.Configuration;
 import android.database.Cursor;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.util.Pair;
-import android.view.ContextMenu;
-import android.view.ContextMenu.ContextMenuInfo;
 import android.view.LayoutInflater;
-import android.view.MenuInflater;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
 import android.widget.ListView;
 import android.widget.SimpleCursorAdapter;
 
 import java.util.LinkedList;
@@ -116,19 +110,20 @@ public class BookmarksPage extends HomeF
         mFolderView = (BookmarkFolderView) LayoutInflater.from(getActivity()).inflate(R.layout.bookmark_folder_row, null);
         mFolderView.open();
 
         // We need to add the header before we set the adapter, hence make it null
         refreshListWithCursor(null);
 
         EventHandlers eventHandlers = new EventHandlers();
         mList.setOnItemClickListener(eventHandlers);
-        mList.setOnCreateContextMenuListener(eventHandlers);
         mList.setOnKeyListener(GamepadUtils.getListItemClickDispatcher());
 
+        registerForContextMenu(mList);
+
         mQueryTask = new BookmarksQueryTask();
         mQueryTask.execute();
     }
 
     @Override
     public void onDestroyView() {
         mList = null;
         mFolderView = null;
@@ -178,18 +173,17 @@ public class BookmarksPage extends HomeF
 
         // Reset the task.
         mQueryTask = null;
     }
 
     /**
      * Internal class to handle different event listeners on the ListView.
      */
-    private class EventHandlers implements AdapterView.OnItemClickListener,
-                                           View.OnCreateContextMenuListener {
+    private class EventHandlers implements AdapterView.OnItemClickListener {
         @Override
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             final ListView list = (ListView) parent;
             final int headerCount = list.getHeaderViewsCount();
 
             // If we tap on the header view, move back to parent folder.
             if (headerCount == 1 && position == 0) {
                 mCursorAdapter.moveToParentFolder();
@@ -217,52 +211,16 @@ public class BookmarksPage extends HomeF
              } else {
                 // Otherwise, just open the URL
                 String url = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL));
                 if (mUrlOpenListener != null) {
                     mUrlOpenListener.onUrlOpen(url);
                 }
              }
          }
-
-        @Override
-        public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
-            if (!(menuInfo instanceof HomeContextMenuInfo)) {
-                return;
-            }
-
-            HomeContextMenuInfo info = (HomeContextMenuInfo) menuInfo;
-
-            // Don't show the context menu for folders.
-            if (info.isFolder) {
-                return;
-            }
-
-            MenuInflater inflater = new MenuInflater(view.getContext());
-            inflater.inflate(R.menu.home_contextmenu, menu);
-
-            // Show Open Private Tab if we're in private mode, Open New Tab otherwise
-            boolean isPrivate = false;
-            Tab tab = Tabs.getInstance().getSelectedTab();
-            if (tab != null) {
-                isPrivate = tab.isPrivate();
-            }
-            menu.findItem(R.id.open_new_tab).setVisible(!isPrivate);
-            menu.findItem(R.id.open_private_tab).setVisible(isPrivate);
-
-            // Hide "Remove" item if there isn't a valid history ID
-            if (info.rowId < 0) {
-                menu.findItem(R.id.remove_history).setVisible(false);
-            }
-            menu.setHeaderTitle(info.title);
- 
-            menu.findItem(R.id.remove_history).setVisible(false);
-            menu.findItem(R.id.open_in_reader).setVisible(false);
-            return;
-        }
     }
 
     /**
      * Adapter to back the ListView with a list of bookmarks.
      */
     private class BookmarksListAdapter extends SimpleCursorAdapter {
         private static final int VIEW_TYPE_ITEM = 0;
         private static final int VIEW_TYPE_FOLDER = 1;
--- a/mobile/android/base/home/HomeFragment.java
+++ b/mobile/android/base/home/HomeFragment.java
@@ -2,33 +2,36 @@
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.home;
 
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.R;
+import org.mozilla.gecko.Tab;
 import org.mozilla.gecko.Tabs;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.gfx.BitmapUtils;
 import org.mozilla.gecko.home.HomeListView.HomeContextMenuInfo;
 import org.mozilla.gecko.util.ThreadUtils;
 import org.mozilla.gecko.util.UiAsyncTask;
 
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.support.v4.app.Fragment;
 import android.text.TextUtils;
 import android.util.Log;
+import android.view.ContextMenu;
 import android.view.ContextMenu.ContextMenuInfo;
 import android.view.LayoutInflater;
+import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.EditText;
 import android.widget.Toast;
 
 /**
  * HomeFragment is an empty fragment that can be added to the HomePager.
  * Subclasses can add their own views. 
@@ -39,16 +42,51 @@ public class HomeFragment extends Fragme
 
     // Share MIME type.
     private static final String SHARE_MIME_TYPE = "text/plain";
 
     // URL to Title replacement regex.
     private static final String REGEX_URL_TO_TITLE = "^([a-z]+://)?(www\\.)?";
 
     @Override
+    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
+        if (!(menuInfo instanceof HomeContextMenuInfo)) {
+            return;
+        }
+
+        HomeContextMenuInfo info = (HomeContextMenuInfo) menuInfo;
+
+        // Don't show the context menu for folders.
+        if (info.isFolder) {
+            return;
+        }
+
+        MenuInflater inflater = new MenuInflater(view.getContext());
+        inflater.inflate(R.menu.home_contextmenu, menu);
+
+        // Show Open Private Tab if we're in private mode, Open New Tab otherwise
+        boolean isPrivate = false;
+        Tab tab = Tabs.getInstance().getSelectedTab();
+        if (tab != null) {
+            isPrivate = tab.isPrivate();
+        }
+        menu.findItem(R.id.open_new_tab).setVisible(!isPrivate);
+        menu.findItem(R.id.open_private_tab).setVisible(isPrivate);
+
+        // Hide "Remove" item if there isn't a valid history ID
+        if (info.rowId < 0) {
+            menu.findItem(R.id.remove_history).setVisible(false);
+        }
+        menu.setHeaderTitle(info.title);
+
+        menu.findItem(R.id.remove_history).setVisible(false);
+        menu.findItem(R.id.open_in_reader).setVisible(false);
+    }
+
+    @Override
     public boolean onContextItemSelected(MenuItem item) {
         final Activity activity = getActivity();
         HomeContextMenuInfo info = null;
 
         try {
             ContextMenuInfo menuInfo = item.getMenuInfo();
             info = (HomeContextMenuInfo) menuInfo;
         } catch(ClassCastException e) {