Bug 1251362 - Part 8 - Handle Recent Tabs in onItemClicked(). r=liuche
authorJan Henning <jh+bugzilla@buttercookie.de>
Wed, 18 May 2016 18:55:32 +0200
changeset 301161 1e50a28316e7321897b9c4545049eba887192592
parent 301160 26b34894a22e724ba180143f41cfed5c825ead78
child 301162 8c7176aa08d7e4baa4f7848a2e0731d893f8d66c
push id78241
push userkwierso@gmail.com
push dateThu, 09 Jun 2016 00:09:10 +0000
treeherdermozilla-inbound@2c66b75bbb7f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersliuche
bugs1251362
milestone50.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 1251362 - Part 8 - Handle Recent Tabs in onItemClicked(). r=liuche Clicking a tab in the list of recently closed tabs now sends the appropriate message back to the session store. MozReview-Commit-ID: KF3UJjq5zQK
mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryRecyclerView.java
mobile/android/base/java/org/mozilla/gecko/home/RecentTabsAdapter.java
--- a/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryRecyclerView.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryRecyclerView.java
@@ -119,16 +119,17 @@ public class CombinedHistoryRecyclerView
                 if (mOnUrlOpenListener != null) {
                     final TwoLinePageRow historyItem = (TwoLinePageRow) v;
                     Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL, TelemetryContract.Method.LIST_ITEM, "history");
                     mOnUrlOpenListener.onUrlOpen(historyItem.getUrl(), EnumSet.of(HomePager.OnUrlOpenListener.Flags.ALLOW_SWITCH_TO_TAB));
                 }
                 break;
 
             case CLOSED_TAB:
+                ((RecentTabsAdapter) getAdapter()).restoreTabFromPosition(position);
                 break;
         }
     }
 
     @Override
     public boolean onItemLongClicked(RecyclerView recyclerView, int position, View v) {
         mContextMenuInfo = ((AdapterContextMenuBuilder) getAdapter()).makeContextMenuInfoFromPosition(v, position);
         return showContextMenuForChild(this);
--- a/mobile/android/base/java/org/mozilla/gecko/home/RecentTabsAdapter.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/RecentTabsAdapter.java
@@ -2,21 +2,25 @@
  * 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 android.content.Context;
 import android.support.v7.widget.RecyclerView;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
 
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
 import org.mozilla.gecko.AboutPages;
 import org.mozilla.gecko.EventDispatcher;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoProfile;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.SessionParser;
 import org.mozilla.gecko.home.CombinedHistoryAdapter.RecentTabsUpdateHandler;
 import org.mozilla.gecko.home.CombinedHistoryPanel.PanelStateUpdateHandler;
@@ -196,16 +200,33 @@ public class RecentTabsAdapter extends R
             notifyItemRangeRemoved(lastTabListIndex + 1, -closedTabsCountChange); // Remove tabs from the bottom of the list.
             notifyItemRangeChanged(firstTabListIndex, closedTabsCount); // Update the contents of the remaining items.
         } else { // closedTabsCountChange > 0
             notifyItemRangeInserted(firstTabListIndex, closedTabsCountChange); // Add additional tabs at the top of the list.
             notifyItemRangeChanged(firstTabListIndex + closedTabsCountChange, prevClosedTabsCount); // Update any previous list items.
         }
     }
 
+    public void restoreTabFromPosition(int position) {
+        final List<String> dataList = new ArrayList<>(1);
+        dataList.add(getClosedTabForPosition(position).data);
+        restoreSessionWithHistory(dataList);
+    }
+
+    private static void restoreSessionWithHistory(List<String> dataList) {
+        final JSONObject json = new JSONObject();
+        try {
+            json.put("tabs", new JSONArray(dataList));
+        } catch (JSONException e) {
+            Log.e(LOGTAG, "JSON error", e);
+        }
+
+        GeckoAppShell.notifyObservers("Session:RestoreRecentTabs", json.toString());
+    }
+
     @Override
     public CombinedHistoryItem onCreateViewHolder(ViewGroup parent, int viewType) {
         final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
         final View view;
 
         final CombinedHistoryItem.ItemType itemType = CombinedHistoryItem.ItemType.viewTypeToItemType(viewType);
 
         switch (itemType) {
@@ -229,22 +250,17 @@ public class RecentTabsAdapter extends R
         final CombinedHistoryItem.ItemType itemType = getItemTypeForPosition(position);
 
         switch (itemType) {
             case SECTION_HEADER:
                 ((TextView) holder.itemView).setText(context.getString(R.string.home_closed_tabs_title2));
                 break;
 
             case CLOSED_TAB:
-                final ClosedTab closedTab;
-                if (position <= getLastRecentTabIndex()) {
-                    closedTab = recentlyClosedTabs[position - getFirstRecentTabIndex()];
-                } else {
-                    closedTab = lastSessionTabs[position - getFirstLastSessionTabIndex()];
-                }
+                final ClosedTab closedTab = getClosedTabForPosition(position);
                 ((CombinedHistoryItem.HistoryItem) holder).bind(closedTab);
                 break;
         }
     }
 
     @Override
     public int getItemCount() {
         int itemCount = 1; // NAVIGATION_BACK button is always visible.
@@ -300,16 +316,34 @@ public class RecentTabsAdapter extends R
     private int getFirstLastSessionTabIndex() {
         return getLastRecentTabIndex() + 1;
     }
 
     private int getLastLastSessionTabIndex() {
         return getLastRecentTabIndex() + lastSessionTabs.length;
     }
 
+    /**
+     * Get the closed tab corresponding to a RecyclerView list item.
+     *
+     * The Recent Tab folder combines two data sources, so if we want to get the ClosedTab object
+     * behind a certain list item, we need to route this request to the corresponding data source
+     * and also transform the global list position into a local array index.
+     */
+    private ClosedTab getClosedTabForPosition(int position) {
+        final ClosedTab closedTab;
+        if (position <= getLastRecentTabIndex()) { // Upper part of the list is "Recently closed tabs".
+            closedTab = recentlyClosedTabs[position - getFirstRecentTabIndex()];
+        } else { // Lower part is "Tabs from last time".
+            closedTab = lastSessionTabs[position - getFirstLastSessionTabIndex()];
+        }
+
+        return closedTab;
+    }
+
     @Override
     public HomeContextMenuInfo makeContextMenuInfoFromPosition(View view, int position) {
         final CombinedHistoryItem.ItemType itemType = getItemTypeForPosition(position);
         final HomeContextMenuInfo info;
 
         return null;
     }
 }