Bug 1251362 - Part 6 - Hook up tab restoring code on click draft
authorJan Henning <jh+bugzilla@buttercookie.de>
Wed, 18 May 2016 18:55:32 +0200
changeset 369300 a4e7e03d4d5d176ac3c9802a89bebd6ab562123c
parent 369299 88414e47c29ca5e30ae5a526be6ddf1da6972845
child 369301 50f88f712e303059b8229613e2d06401cc8067ff
push id18831
push usermozilla@buttercookie.de
push dateSat, 21 May 2016 18:52:21 +0000
bugs1251362
milestone49.0a1
Bug 1251362 - Part 6 - Hook up tab restoring code on click 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,19 +119,21 @@ 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;
 
             case OPEN_ALL_TABS:
+                ((RecentTabsAdapter) getAdapter()).restoreAllTabsFromPosition(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
@@ -147,16 +147,49 @@ public class RecentTabsAdapter extends R
                         panelStateUpdateHandler.onPanelStateUpdated();
                         notifyDataSetChanged();
                     }
                 });
             }
         });
     }
 
+    public void restoreTabFromPosition(int position) {
+        final List<String> dataList = new ArrayList<>(1);
+        dataList.add(getClosedTabForPosition(position).data);
+        restoreSessionWithHistory(dataList);
+    }
+
+    public void restoreAllTabsFromPosition(int position) {
+        final ClosedTab[] allClosedTabs;
+        if (isOpenAllLastSessionTabsButtonVisible() && position == getOpenAllLastSessionTabsButtonIndex()) {
+            allClosedTabs = mLastSessionTabs;
+        } else {
+            allClosedTabs = mRecentlyClosedTabs;
+        }
+
+        final List<String> dataList = new ArrayList<>(allClosedTabs.length);
+        for (ClosedTab closedTab : allClosedTabs) {
+            dataList.add(closedTab.data);
+        }
+
+        restoreSessionWithHistory(dataList);
+    }
+
+    private static void restoreSessionWithHistory(List<String> dataList) {
+        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) {
@@ -184,22 +217,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_last_tabs_title));
                 break;
 
             case CLOSED_TAB:
-                ClosedTab closedTab;
-                if (position <= getLastRecentTabIndex()) {
-                    closedTab = mRecentlyClosedTabs[position - getFirstRecentTabIndex()];
-                } else {
-                    closedTab = mLastSessionTabs[position - getFirstLastSessionTabIndex()];
-                }
+                ClosedTab closedTab = getClosedTabForPosition(position);
                 ((CombinedHistoryItem.HistoryItem) holder).bind(closedTab);
                 break;
         }
     }
 
     @Override
     public int getItemCount() {
         int itemCount = 1; // NAVIGATION_BACK button is always visible.
@@ -280,16 +308,27 @@ public class RecentTabsAdapter extends R
     }
 
     private int getOpenAllLastSessionTabsButtonIndex() {
         return isOpenAllLastSessionTabsButtonVisible() ?
                 getLastSessionTabsSectionHeaderIndex() + mLastSessionTabs.length + BUTTON_LENGTH :
                 getLastSessionTabsSectionHeaderIndex() + mLastSessionTabs.length;
     }
 
+    private ClosedTab getClosedTabForPosition(int position) {
+        ClosedTab closedTab;
+        if (position <= getLastRecentTabIndex()) {
+            closedTab = mRecentlyClosedTabs[position - getFirstRecentTabIndex()];
+        } else {
+            closedTab = mLastSessionTabs[position - getFirstLastSessionTabIndex()];
+        }
+
+        return closedTab;
+    }
+
     @Override
     public HomeContextMenuInfo makeContextMenuInfoFromPosition(View view, int position) {
         final CombinedHistoryItem.ItemType itemType = getItemTypeForPosition(position);
         HomeContextMenuInfo info;
 
         return null;
     }
 }