Bug 1220928 - Add context menu. r=sebastian
authorChenxia Liu <liuche@mozilla.com>
Wed, 23 Mar 2016 18:36:13 -0700
changeset 291327 d55a8f609a81a1c199232886b94a85419398d5c3
parent 291326 0242801375876b7f7a711546f543f361282c4ece
child 291328 72040ea10a6105007ce10cee52bb038d3e92a504
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1220928
milestone48.0a1
Bug 1220928 - Add context menu. r=sebastian MozReview-Commit-ID: CQ01edJs3vP
mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryAdapter.java
mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryPanel.java
mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryRecyclerView.java
--- a/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryAdapter.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryAdapter.java
@@ -106,16 +106,31 @@ public class CombinedHistoryAdapter exte
             return position - remoteClients.size();
         } else if (type == ItemType.HISTORY){
             return position - remoteClients.size() - getHeadersBefore(position);
         } else {
             return position;
         }
     }
 
+    public HomeContextMenuInfo makeContextMenuInfoFromPosition(View view, int position) {
+        final ItemType itemType = ItemType.viewTypeToItemType(getItemViewType(position));
+        HomeContextMenuInfo info;
+        switch (itemType) {
+            case CHILD:
+                info = new HomeContextMenuInfo(view, position, -1);
+                return CombinedHistoryPanel.populateChildInfoFromTab(info, clientChildren.get(position));
+            case HISTORY:
+                info = new HomeContextMenuInfo(view, position, -1);
+                historyCursor.moveToPosition(transformPosition(ItemType.HISTORY, position));
+                return CombinedHistoryPanel.populateHistoryInfoFromCursor(info, historyCursor);
+        }
+        return null;
+    }
+
     @Override
     public CombinedHistoryItem onCreateViewHolder(ViewGroup viewGroup, int viewType) {
         final LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
         final View view;
 
         final ItemType itemType = ItemType.viewTypeToItemType(viewType);
 
         switch (itemType) {
--- a/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryPanel.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryPanel.java
@@ -32,18 +32,20 @@ import org.json.JSONObject;
 import org.json.JSONArray;
 import org.mozilla.gecko.EventDispatcher;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoProfile;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Restrictions;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
+import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.RemoteClient;
+import org.mozilla.gecko.db.RemoteTab;
 import org.mozilla.gecko.home.HistorySectionsHelper.SectionDateRange;
 import org.mozilla.gecko.restrictions.Restrictable;
 import org.mozilla.gecko.widget.DividerItemDecoration;
 
 import java.util.Collections;
 import java.util.List;
 
 public class CombinedHistoryPanel extends HomeFragment {
@@ -100,16 +102,18 @@ public class CombinedHistoryPanel extend
 
         mRecyclerView = (CombinedHistoryRecyclerView) view.findViewById(R.id.combined_recycler_view);
         mAdapter = new CombinedHistoryAdapter(getContext());
         mRecyclerView.setAdapter(mAdapter);
         mRecyclerView.setItemAnimator(new DefaultItemAnimator());
         mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext()));
         mRecyclerView.setOnHistoryClickedListener(mUrlOpenListener);
         mRecyclerView.setOnPanelLevelChangeListener(new OnLevelChangeListener());
+        registerForContextMenu(mRecyclerView);
+
         mPanelFooterButton = (Button) view.findViewById(R.id.clear_history_button);
         mPanelFooterButton.setOnClickListener(new OnFooterButtonClickListener());
     }
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
         mCursorLoaderCallbacks = new CursorLoaderCallbacks();
@@ -376,9 +380,31 @@ public class CombinedHistoryPanel extend
         // Add underlining for "Private Browsing".
         ssb.setSpan(new UnderlineSpan(), underlineStart, underlineEnd, 0);
 
         ssb.delete(underlineEnd, underlineEnd + FORMAT_S2.length());
         ssb.delete(underlineStart, underlineStart + FORMAT_S1.length());
 
         return ssb;
     }
+
+    protected static HomeContextMenuInfo populateHistoryInfoFromCursor(HomeContextMenuInfo info, Cursor cursor) {
+        info.url = cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.Combined.URL));
+        info.title = cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.Combined.TITLE));
+        info.historyId = cursor.getInt(cursor.getColumnIndexOrThrow(BrowserContract.Combined.HISTORY_ID));
+        info.itemType = HomeContextMenuInfo.RemoveItemType.HISTORY;
+        final int bookmarkIdCol = cursor.getColumnIndexOrThrow(BrowserContract.Combined.BOOKMARK_ID);
+        if (cursor.isNull(bookmarkIdCol)) {
+            // If this is a combined cursor, we may get a history item without a
+            // bookmark, in which case the bookmarks ID column value will be null.
+            info.bookmarkId =  -1;
+        } else {
+            info.bookmarkId = cursor.getInt(bookmarkIdCol);
+        }
+        return info;
+    }
+
+    protected static HomeContextMenuInfo populateChildInfoFromTab(HomeContextMenuInfo info, RemoteTab tab) {
+        info.url = tab.url;
+        info.title = tab.title;
+        return info;
+    }
 }
--- a/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryRecyclerView.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryRecyclerView.java
@@ -18,16 +18,17 @@ import org.mozilla.gecko.widget.Recycler
 
 import java.util.EnumSet;
 
 public class CombinedHistoryRecyclerView extends RecyclerView
         implements RecyclerViewClickSupport.OnItemClickListener, RecyclerViewClickSupport.OnItemLongClickListener {
 
     protected HomePager.OnUrlOpenListener mOnUrlOpenListener;
     protected OnPanelLevelChangeListener mOnPanelLevelChangeListener;
+    protected HomeContextMenuInfo mContextMenuInfo;
 
     public CombinedHistoryRecyclerView(Context context) {
         super(context);
         init(context);
     }
 
     public CombinedHistoryRecyclerView(Context context, AttributeSet attributeSet) {
         super(context, attributeSet);
@@ -79,12 +80,18 @@ public class CombinedHistoryRecyclerView
                     mOnUrlOpenListener.onUrlOpen(historyItem.getUrl(), EnumSet.of(HomePager.OnUrlOpenListener.Flags.ALLOW_SWITCH_TO_TAB));
                 }
                 break;
         }
     }
 
     @Override
     public boolean onItemLongClicked(RecyclerView recyclerView, int position, View v) {
-        // TODO: open context menu if not a date title
+        mContextMenuInfo = ((CombinedHistoryAdapter) getAdapter()).makeContextMenuInfoFromPosition(v, position);
         return showContextMenuForChild(this);
     }
+
+    @Override
+    public HomeContextMenuInfo getContextMenuInfo() {
+        return mContextMenuInfo;
+    }
+
 }