Bug 1220928 - Handle back button when in client's child view. r=sebastian
authorChenxia Liu <liuche@mozilla.com>
Mon, 28 Mar 2016 12:04:03 -0700
changeset 291329 c5cdf619cd2e8f1e6b5891c443f5d9f0ef38466d
parent 291328 72040ea10a6105007ce10cee52bb038d3e92a504
child 291330 c63615d8c252e55111fbb35daec7f0f35a53b73d
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 - Handle back button when in client's child view. r=sebastian MozReview-Commit-ID: BDiyR5JCHQD
mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryAdapter.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
@@ -192,20 +192,24 @@ public class CombinedHistoryAdapter exte
         }
         // Handle "back" view.
         clientChildren.add(null);
         clientChildren.addAll(remoteClients.get(transformAdapterPositionForDataStructure(ItemType.CLIENT, parentPosition)).tabs);
         inChildView = true;
         notifyDataSetChanged();
     }
 
-    public void exitChildView() {
+    public boolean exitChildView() {
+        if (!inChildView) {
+            return false;
+        }
         inChildView = false;
         clientChildren.clear();
         notifyDataSetChanged();
+        return true;
     }
 
     private ItemType getItemTypeForPosition(int position) {
         return ItemType.viewTypeToItemType(getItemViewType(position));
     }
 
     /**
      * Transform an adapter position to the position for the data structure backing the item type.
--- a/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryRecyclerView.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryRecyclerView.java
@@ -4,26 +4,29 @@
  * 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.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.util.AttributeSet;
+import android.view.KeyEvent;
 import android.view.View;
 import org.mozilla.gecko.db.RemoteClient;
 import org.mozilla.gecko.home.CombinedHistoryPanel.OnPanelLevelChangeListener;
 import org.mozilla.gecko.home.CombinedHistoryPanel.OnPanelLevelChangeListener.PanelLevel;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.widget.RecyclerViewClickSupport;
 
 import java.util.EnumSet;
 
+import static org.mozilla.gecko.home.CombinedHistoryPanel.OnPanelLevelChangeListener.PanelLevel.PARENT;
+
 public class CombinedHistoryRecyclerView extends RecyclerView
         implements RecyclerViewClickSupport.OnItemClickListener, RecyclerViewClickSupport.OnItemLongClickListener {
 
     protected HomePager.OnUrlOpenListener mOnUrlOpenListener;
     protected OnPanelLevelChangeListener mOnPanelLevelChangeListener;
     protected CombinedHistoryPanel.DialogBuilder<RemoteClient> mDialogBuilder;
     protected HomeContextMenuInfo mContextMenuInfo;
 
@@ -45,16 +48,30 @@ public class CombinedHistoryRecyclerView
     private void init(Context context) {
         LinearLayoutManager layoutManager = new LinearLayoutManager(context);
         layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
         setLayoutManager(layoutManager);
 
         RecyclerViewClickSupport.addTo(this)
             .setOnItemClickListener(this)
             .setOnItemLongClickListener(this);
+
+        setOnKeyListener(new View.OnKeyListener() {
+            @Override
+            public boolean onKey(View v, int keyCode, KeyEvent event) {
+                final int action = event.getAction();
+
+                // If the user hit the BACK key, try to move to the parent folder.
+                if (action == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
+                    mOnPanelLevelChangeListener.onPanelLevelChange(PARENT);
+                    return ((CombinedHistoryAdapter) getAdapter()).exitChildView();
+                }
+                return false;
+            }
+        });
     }
 
     public void setOnHistoryClickedListener(HomePager.OnUrlOpenListener listener) {
         this.mOnUrlOpenListener = listener;
     }
 
     public void setOnPanelLevelChangeListener(OnPanelLevelChangeListener listener) {
         this.mOnPanelLevelChangeListener = listener;
@@ -76,17 +93,17 @@ public class CombinedHistoryRecyclerView
                 break;
             case HIDDEN_DEVICES:
                 if (mDialogBuilder != null) {
                     mDialogBuilder.createAndShowDialog(((CombinedHistoryAdapter) getAdapter()).getHiddenClients());
                 }
                 break;
 
             case NAVIGATION_BACK:
-                mOnPanelLevelChangeListener.onPanelLevelChange(PanelLevel.PARENT);
+                mOnPanelLevelChangeListener.onPanelLevelChange(PARENT);
                 ((CombinedHistoryAdapter) getAdapter()).exitChildView();
                 break;
             case CHILD:
             case HISTORY:
                 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));