Bug 1312467 - Pass url-open listeners into TopSitesCard and handle clicks there r=sebastian
authorAndrzej Hunt <ahunt@mozilla.com>
Tue, 25 Oct 2016 11:29:23 -0700
changeset 346982 aa12d5ecd8b6cb4c7293302fb9c67bf273620aec
parent 346981 a207a3a364654ae584c59a4b37edff1d45d99cef
child 346983 9f65ac29fa2447c4a590dd025753ff133279b404
push id10298
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:33:03 +0000
treeherdermozilla-aurora@7e29173b1641 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1312467
milestone52.0a1
Bug 1312467 - Pass url-open listeners into TopSitesCard and handle clicks there r=sebastian This is in preparation for adding context-menu support to TopSitesCard, which requires the two url-open listeners. MozReview-Commit-ID: 7ubTCqCqcW9
mobile/android/base/java/org/mozilla/gecko/home/activitystream/StreamItem.java
mobile/android/base/java/org/mozilla/gecko/home/activitystream/StreamRecyclerAdapter.java
mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesCard.java
mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesPage.java
mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesPageAdapter.java
mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesPagerAdapter.java
--- a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/StreamItem.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/StreamItem.java
@@ -38,21 +38,21 @@ public abstract class StreamItem extends
         super(itemView);
     }
 
     public static class TopPanel extends StreamItem {
         public static final int LAYOUT_ID = R.layout.activity_stream_main_toppanel;
 
         private final ViewPager topSitesPager;
 
-        public TopPanel(View itemView, HomePager.OnUrlOpenListener onUrlOpenListener) {
+        public TopPanel(View itemView, HomePager.OnUrlOpenListener onUrlOpenListener, HomePager.OnUrlOpenInBackgroundListener onUrlOpenInBackgroundListener) {
             super(itemView);
 
             topSitesPager = (ViewPager) itemView.findViewById(R.id.topsites_pager);
-            topSitesPager.setAdapter(new TopSitesPagerAdapter(itemView.getContext(), onUrlOpenListener));
+            topSitesPager.setAdapter(new TopSitesPagerAdapter(itemView.getContext(), onUrlOpenListener, onUrlOpenInBackgroundListener));
 
             CirclePageIndicator indicator = (CirclePageIndicator) itemView.findViewById(R.id.topsites_indicator);
             indicator.setViewPager(topSitesPager);
         }
 
         public void bind(Cursor cursor, int tiles, int tilesWidth, int tilesHeight) {
             final TopSitesPagerAdapter adapter = (TopSitesPagerAdapter) topSitesPager.getAdapter();
             adapter.setTilesSize(tiles, tilesWidth, tilesHeight);
--- a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/StreamRecyclerAdapter.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/StreamRecyclerAdapter.java
@@ -52,17 +52,17 @@ public class StreamRecyclerAdapter exten
         }
     }
 
     @Override
     public StreamItem onCreateViewHolder(ViewGroup parent, final int type) {
         final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
 
         if (type == TopPanel.LAYOUT_ID) {
-            return new TopPanel(inflater.inflate(type, parent, false), onUrlOpenListener);
+            return new TopPanel(inflater.inflate(type, parent, false), onUrlOpenListener, onUrlOpenInBackgroundListener);
         } else if (type == HighlightItem.LAYOUT_ID) {
             return new HighlightItem(inflater.inflate(type, parent, false), onUrlOpenListener, onUrlOpenInBackgroundListener);
         } else {
             throw new IllegalStateException("Missing inflation for ViewType " + type);
         }
     }
 
     private int translatePositionToCursor(int position) {
--- a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesCard.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesCard.java
@@ -1,50 +1,67 @@
 /* -*- 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.activitystream.topsites;
 
 import android.graphics.Color;
 import android.support.v7.widget.RecyclerView;
+import android.view.View;
 import android.widget.FrameLayout;
 import android.widget.ImageView;
 import android.widget.TextView;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.activitystream.ActivityStream;
+import org.mozilla.gecko.home.HomePager;
+import org.mozilla.gecko.home.activitystream.menu.ActivityStreamContextMenu;
 import org.mozilla.gecko.icons.IconCallback;
 import org.mozilla.gecko.icons.IconResponse;
 import org.mozilla.gecko.icons.Icons;
 import org.mozilla.gecko.util.DrawableUtil;
 import org.mozilla.gecko.widget.FaviconView;
 
+import java.util.EnumSet;
 import java.util.concurrent.Future;
 
-class TopSitesCard extends RecyclerView.ViewHolder implements IconCallback {
+class TopSitesCard extends RecyclerView.ViewHolder
+        implements IconCallback, View.OnClickListener {
     private final FaviconView faviconView;
 
     private final TextView title;
     private final ImageView menuButton;
     private Future<IconResponse> ongoingIconLoad;
 
-    public TopSitesCard(FrameLayout card) {
+    private String url;
+
+    private final HomePager.OnUrlOpenListener onUrlOpenListener;
+    private final HomePager.OnUrlOpenInBackgroundListener onUrlOpenInBackgroundListener;
+
+    public TopSitesCard(FrameLayout card, final HomePager.OnUrlOpenListener onUrlOpenListener, final HomePager.OnUrlOpenInBackgroundListener onUrlOpenInBackgroundListener) {
         super(card);
 
         faviconView = (FaviconView) card.findViewById(R.id.favicon);
 
         title = (TextView) card.findViewById(R.id.title);
         menuButton = (ImageView) card.findViewById(R.id.menu);
+
+        this.onUrlOpenListener = onUrlOpenListener;
+        this.onUrlOpenInBackgroundListener = onUrlOpenInBackgroundListener;
+
+        card.setOnClickListener(this);
     }
 
-    void bind(TopSitesPageAdapter.TopSite topSite) {
+    void bind(final TopSitesPageAdapter.TopSite topSite) {
         final String label = ActivityStream.extractLabel(topSite.url, true);
         title.setText(label);
 
+        this.url = topSite.url;
+
         if (ongoingIconLoad != null) {
             ongoingIconLoad.cancel(true);
         }
 
         ongoingIconLoad = Icons.with(itemView.getContext())
                 .pageUrl(topSite.url)
                 .skipNetwork()
                 .build()
@@ -55,9 +72,16 @@ class TopSitesCard extends RecyclerView.
     public void onIconResponse(IconResponse response) {
         faviconView.updateImage(response);
 
         final int tintColor = !response.hasColor() || response.getColor() == Color.WHITE ? Color.LTGRAY : Color.WHITE;
 
         menuButton.setImageDrawable(
                 DrawableUtil.tintDrawable(menuButton.getContext(), R.drawable.menu, tintColor));
     }
+
+    @Override
+    public void onClick(View clickedView) {
+        if (clickedView == itemView) {
+            onUrlOpenListener.onUrlOpen(url, EnumSet.noneOf(HomePager.OnUrlOpenListener.Flags.class));
+        }
+    }
 }
--- a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesPage.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesPage.java
@@ -12,43 +12,27 @@ import android.util.AttributeSet;
 import android.view.View;
 
 import org.mozilla.gecko.home.HomePager;
 import org.mozilla.gecko.widget.RecyclerViewClickSupport;
 
 import java.util.EnumSet;
 
 public class TopSitesPage
-        extends RecyclerView
-        implements RecyclerViewClickSupport.OnItemClickListener {
+        extends RecyclerView {
     public TopSitesPage(Context context,
                         @Nullable AttributeSet attrs) {
         super(context, attrs);
 
         setLayoutManager(new GridLayoutManager(context, 1));
-
-        RecyclerViewClickSupport.addTo(this)
-                .setOnItemClickListener(this);
     }
 
     public void setTiles(int tiles) {
         setLayoutManager(new GridLayoutManager(getContext(), tiles));
     }
 
-    private HomePager.OnUrlOpenListener onUrlOpenListener = null;
+    private HomePager.OnUrlOpenListener onUrlOpenListener;
+    private HomePager.OnUrlOpenInBackgroundListener onUrlOpenInBackgroundListener;
 
     public TopSitesPageAdapter getAdapter() {
         return (TopSitesPageAdapter) super.getAdapter();
     }
-
-    public void setOnUrlOpenListener(HomePager.OnUrlOpenListener onUrlOpenListener) {
-        this.onUrlOpenListener = onUrlOpenListener;
-    }
-
-    @Override
-    public void onItemClicked(RecyclerView recyclerView, int position, View v) {
-        if (onUrlOpenListener != null) {
-            final String url = getAdapter().getURLForPosition(position);
-
-            onUrlOpenListener.onUrlOpen(url, EnumSet.noneOf(HomePager.OnUrlOpenListener.Flags.class));
-        }
-    }
 }
--- a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesPageAdapter.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesPageAdapter.java
@@ -10,16 +10,17 @@ import android.support.annotation.UiThre
 import android.support.v7.widget.RecyclerView;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.FrameLayout;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.db.BrowserContract;
+import org.mozilla.gecko.home.HomePager;
 
 import java.util.ArrayList;
 import java.util.List;
 
 public class TopSitesPageAdapter extends RecyclerView.Adapter<TopSitesCard> {
     static final class TopSite {
         public final long id;
         public final String url;
@@ -33,24 +34,31 @@ public class TopSitesPageAdapter extends
     }
 
     private List<TopSite> topSites;
     private int tiles;
     private int tilesWidth;
     private int tilesHeight;
     private int textHeight;
 
-    public TopSitesPageAdapter(Context context, int tiles, int tilesWidth, int tilesHeight) {
+    private final HomePager.OnUrlOpenListener onUrlOpenListener;
+    private final HomePager.OnUrlOpenInBackgroundListener onUrlOpenInBackgroundListener;
+
+    public TopSitesPageAdapter(Context context, int tiles, int tilesWidth, int tilesHeight,
+                               HomePager.OnUrlOpenListener onUrlOpenListener, HomePager.OnUrlOpenInBackgroundListener onUrlOpenInBackgroundListener) {
         setHasStableIds(true);
 
         this.topSites = new ArrayList<>();
         this.tiles = tiles;
         this.tilesWidth = tilesWidth;
         this.tilesHeight = tilesHeight;
         this.textHeight = context.getResources().getDimensionPixelSize(R.dimen.activity_stream_top_sites_text_height);
+
+        this.onUrlOpenListener = onUrlOpenListener;
+        this.onUrlOpenInBackgroundListener = onUrlOpenInBackgroundListener;
     }
 
     /**
      *
      * @param cursor
      * @param startIndex The first item that this topsites group should show. This item, and the following
      * 3 items will be displayed by this adapter.
      */
@@ -88,22 +96,17 @@ public class TopSitesPageAdapter extends
         final FrameLayout card = (FrameLayout) inflater.inflate(R.layout.activity_stream_topsites_card, parent, false);
         final View content = card.findViewById(R.id.content);
 
         ViewGroup.LayoutParams layoutParams = content.getLayoutParams();
         layoutParams.width = tilesWidth;
         layoutParams.height = tilesHeight + textHeight;
         content.setLayoutParams(layoutParams);
 
-        return new TopSitesCard(card);
-    }
-
-    @UiThread
-    public String getURLForPosition(int position) {
-        return topSites.get(position).url;
+        return new TopSitesCard(card, onUrlOpenListener, onUrlOpenInBackgroundListener);
     }
 
     @Override
     public int getItemCount() {
         return topSites.size();
     }
 
     @Override
--- a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesPagerAdapter.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesPagerAdapter.java
@@ -26,22 +26,26 @@ public class TopSitesPagerAdapter extend
     private int tiles;
     private int tilesWidth;
     private int tilesHeight;
 
     private LinkedList<TopSitesPage> pages = new LinkedList<>();
 
     private final Context context;
     private final HomePager.OnUrlOpenListener onUrlOpenListener;
+    private final HomePager.OnUrlOpenInBackgroundListener onUrlOpenInBackgroundListener;
 
     private int count = 0;
 
-    public TopSitesPagerAdapter(Context context, HomePager.OnUrlOpenListener onUrlOpenListener) {
+    public TopSitesPagerAdapter(Context context,
+                                HomePager.OnUrlOpenListener onUrlOpenListener,
+                                HomePager.OnUrlOpenInBackgroundListener onUrlOpenInBackgroundListener) {
         this.context = context;
         this.onUrlOpenListener = onUrlOpenListener;
+        this.onUrlOpenInBackgroundListener = onUrlOpenInBackgroundListener;
     }
 
     public void setTilesSize(int tiles, int tilesWidth, int tilesHeight) {
         this.tilesWidth = tilesWidth;
         this.tilesHeight = tilesHeight;
         this.tiles = tiles;
     }
 
@@ -86,18 +90,19 @@ public class TopSitesPagerAdapter extend
         final int pageDelta = count;
 
         if (pageDelta > 0) {
             final LayoutInflater inflater = LayoutInflater.from(context);
             for (int i = 0; i < pageDelta; i++) {
                 final TopSitesPage page = (TopSitesPage) inflater.inflate(R.layout.activity_stream_topsites_page, null, false);
 
                 page.setTiles(tiles);
-                page.setOnUrlOpenListener(onUrlOpenListener);
-                page.setAdapter(new TopSitesPageAdapter(context, tiles, tilesWidth, tilesHeight));
+                final TopSitesPageAdapter adapter = new TopSitesPageAdapter(context, tiles, tilesWidth, tilesHeight,
+                        onUrlOpenListener, onUrlOpenInBackgroundListener);
+                page.setAdapter(adapter);
                 pages.add(page);
             }
         } else if (pageDelta < 0) {
             for (int i = 0; i > pageDelta; i--) {
                 final TopSitesPage page = pages.getLast();
 
                 // Ensure the page doesn't use the old/invalid cursor anymore
                 page.getAdapter().swapCursor(null, 0);