Bug 1220928 - Add footerbar button. r=sebastian
authorChenxia Liu <liuche@mozilla.com>
Thu, 25 Feb 2016 18:25:55 -0800
changeset 291322 fe241494b578e688550de433b8b6d838863af7f8
parent 291321 1139e8f7b278942800183d18d10ba17b937b6642
child 291323 60cd54c9bf895b1abccc89a3234a149f9277d314
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 footerbar button. r=sebastian MozReview-Commit-ID: 6R0RWsSljji
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
@@ -7,16 +7,17 @@ package org.mozilla.gecko.home;
 import android.support.v7.widget.RecyclerView;
 
 import android.content.Context;
 import android.database.Cursor;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import org.json.JSONArray;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.db.RemoteClient;
 import org.mozilla.gecko.db.RemoteTab;
 
 import java.util.Collections;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -55,16 +56,27 @@ public class CombinedHistoryAdapter exte
         notifyDataSetChanged();
     }
 
     public void setHistory(Cursor history) {
         historyCursor = history;
         notifyDataSetChanged();
     }
 
+    public JSONArray getCurrentChildTabs() {
+        if (clientChildren != null) {
+            final JSONArray urls = new JSONArray();
+            for (int i = 1; i < clientChildren.size(); i++) {
+                urls.put(clientChildren.get(i).url);
+            }
+            return urls;
+        }
+        return null;
+    }
+
     public void showChildView(int parentPosition) {
         if (clientChildren == null) {
             clientChildren = new ArrayList<>();
         }
         // Handle "back" view.
         clientChildren.add(null);
         clientChildren.addAll(remoteClients.get(transformPosition(ItemType.CLIENT, parentPosition)).tabs);
         inChildView = true;
--- a/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryPanel.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryPanel.java
@@ -1,60 +1,82 @@
 /* -*- 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 android.app.AlertDialog;
 import android.content.ContentResolver;
 import android.content.Context;
+import android.content.DialogInterface;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.support.v4.app.LoaderManager;
 import android.support.v4.content.Loader;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.Button;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.JSONArray;
+import org.mozilla.gecko.GeckoAppShell;
+import org.mozilla.gecko.GeckoEvent;
 import org.mozilla.gecko.GeckoProfile;
 import org.mozilla.gecko.R;
+import org.mozilla.gecko.Telemetry;
+import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.RemoteClient;
 
 import java.util.Collections;
 import java.util.List;
 
 public class CombinedHistoryPanel extends HomeFragment {
     private static final String LOGTAG = "GeckoCombinedHistoryPnl";
     private final int LOADER_ID_HISTORY = 0;
     private final int LOADER_ID_REMOTE = 1;
 
     private CombinedHistoryRecyclerView mRecyclerView;
     private CombinedHistoryAdapter mAdapter;
     private CursorLoaderCallbacks mCursorLoaderCallbacks;
 
-    // The button view for clearing browsing history.
-    private View mClearHistoryButton;
+    private OnPanelLevelChangeListener.PanelLevel mPanelLevel = OnPanelLevelChangeListener.PanelLevel.PARENT;
+    private Button mPanelFooterButton;
+
+    public interface OnPanelLevelChangeListener {
+        enum PanelLevel {
+        PARENT, CHILD
+        }
+
+        void onPanelLevelChange(PanelLevel level);
+    }
 
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         return inflater.inflate(R.layout.home_combined_history_panel, container, false);
     }
 
     @Override
     public void onViewCreated(View view, Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
 
         mRecyclerView = (CombinedHistoryRecyclerView) view.findViewById(R.id.combined_recycler_view);
         mAdapter = new CombinedHistoryAdapter(getContext());
         mRecyclerView.setAdapter(mAdapter);
         mRecyclerView.setOnHistoryClickedListener(mUrlOpenListener);
-        mClearHistoryButton = view.findViewById(R.id.clear_history_button);
-        // TODO: link up click handler for clear history button
+        mRecyclerView.setOnPanelLevelChangeListener(new OnLevelChangeListener());
+        mPanelFooterButton = (Button) view.findViewById(R.id.clear_history_button);
+        mPanelFooterButton.setOnClickListener(new OnFooterButtonClickListener());
+        mPanelFooterButton.setVisibility(View.VISIBLE);
+
+        // TODO: Check if empty state
         // TODO: Handle date headers.
     }
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
         mCursorLoaderCallbacks = new CursorLoaderCallbacks();
     }
@@ -134,9 +156,76 @@ public class CombinedHistoryPanel extend
 
         }
 
         public void onLoaderReset(Loader<Cursor> c) {
             mAdapter.setClients(Collections.<RemoteClient>emptyList());
             mAdapter.setHistory(null);
         }
     }
+
+    protected class OnLevelChangeListener implements OnPanelLevelChangeListener {
+        @Override
+        public void onPanelLevelChange(PanelLevel level) {
+            mPanelLevel = level;
+            switch (mPanelLevel) {
+                case PARENT:
+                    mPanelFooterButton.setText(R.string.home_clear_history_button);
+                    break;
+                case CHILD:
+                    mPanelFooterButton.setText(R.string.home_open_all);
+                    break;
+            }
+        }
+    }
+
+    private class OnFooterButtonClickListener implements View.OnClickListener {
+        @Override
+        public void onClick(View view) {
+            switch (mPanelLevel) {
+                case PARENT:
+                    final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
+                    dialogBuilder.setMessage(R.string.home_clear_history_confirm);
+                    dialogBuilder.setNegativeButton(R.string.button_cancel, new AlertDialog.OnClickListener() {
+                        @Override
+                        public void onClick(final DialogInterface dialog, final int which) {
+                            dialog.dismiss();
+                        }
+                    });
+
+                    dialogBuilder.setPositiveButton(R.string.button_ok, new AlertDialog.OnClickListener() {
+                        @Override
+                        public void onClick(final DialogInterface dialog, final int which) {
+                            dialog.dismiss();
+
+                            // Send message to Java to clear history.
+                            final JSONObject json = new JSONObject();
+                            try {
+                                json.put("history", true);
+                            } catch (JSONException e) {
+                                Log.e(LOGTAG, "JSON error", e);
+                            }
+
+                            GeckoAppShell.notifyObservers("Sanitize:ClearData", json.toString());;
+                            Telemetry.sendUIEvent(TelemetryContract.Event.SANITIZE, TelemetryContract.Method.BUTTON, "history");
+                        }
+                    });
+
+                    dialogBuilder.show();
+                    break;
+
+                case CHILD:
+                    final JSONArray tabUrls = ((CombinedHistoryAdapter) mRecyclerView.getAdapter()).getCurrentChildTabs();
+                    if (tabUrls != null) {
+                        final JSONObject message = new JSONObject();
+                        try {
+                            message.put("urls", tabUrls);
+                            message.put("shouldNotifyTabsOpenedToJava", false);
+                            GeckoAppShell.notifyObservers("Tabs:OpenMultiple", message.toString());;
+                        } catch (JSONException e) {
+                            Log.e(LOGTAG, "Error making JSON message to open tabs");
+                        }
+                    }
+                    break;
+            }
+        }
+    }
 }
--- a/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryRecyclerView.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryRecyclerView.java
@@ -5,26 +5,29 @@
 
 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.View;
+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;
 
 public class CombinedHistoryRecyclerView extends RecyclerView
         implements RecyclerViewClickSupport.OnItemClickListener, RecyclerViewClickSupport.OnItemLongClickListener {
 
     protected HomePager.OnUrlOpenListener mOnUrlOpenListener;
+    protected OnPanelLevelChangeListener mOnPanelLevelChangeListener;
 
     public CombinedHistoryRecyclerView(Context context) {
         super(context);
         init(context);
     }
 
     public CombinedHistoryRecyclerView(Context context, AttributeSet attributeSet) {
         super(context, attributeSet);
@@ -45,26 +48,32 @@ public class CombinedHistoryRecyclerView
             .setOnItemClickListener(this)
             .setOnItemLongClickListener(this);
     }
 
     public void setOnHistoryClickedListener(HomePager.OnUrlOpenListener listener) {
         this.mOnUrlOpenListener = listener;
     }
 
+    public void setOnPanelLevelChangeListener(OnPanelLevelChangeListener listener) {
+        this.mOnPanelLevelChangeListener = listener;
+    }
+
     @Override
     public void onItemClicked(RecyclerView recyclerView, int position, View v) {
         final int viewType = getAdapter().getItemViewType(position);
         final CombinedHistoryAdapter.ItemType itemType = CombinedHistoryAdapter.ItemType.viewTypeToItemType(viewType);
 
         switch(itemType) {
             case CLIENT:
+                mOnPanelLevelChangeListener.onPanelLevelChange(PanelLevel.CHILD);
                 ((CombinedHistoryAdapter) getAdapter()).showChildView(position);
                 break;
             case NAVIGATION_BACK:
+                mOnPanelLevelChangeListener.onPanelLevelChange(PanelLevel.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));