Bug 1068425 - Move TopSitesGridView click listeners to TopSitesPanel. r=lucasr, a=lsblakk
authorBrian Nicholson <bnicholson@mozilla.com>
Wed, 05 Nov 2014 12:14:34 -0800
changeset 233856 18c86a0bf10542096eac435f7ddddf0293eb6d68
parent 233855 e1a303b5e43f5cf29750713604e46df5acd4ff74
child 233857 6cce1dc5f97e1e7295f3be0f2c1528e345787ee7
push id4187
push userbhearsum@mozilla.com
push dateFri, 28 Nov 2014 15:29:12 +0000
treeherdermozilla-beta@f23cc6a30c11 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerslucasr, lsblakk
bugs1068425
milestone35.0a2
Bug 1068425 - Move TopSitesGridView click listeners to TopSitesPanel. r=lucasr, a=lsblakk
mobile/android/base/home/TopSitesGridView.java
mobile/android/base/home/TopSitesPanel.java
--- a/mobile/android/base/home/TopSitesGridView.java
+++ b/mobile/android/base/home/TopSitesGridView.java
@@ -1,34 +1,24 @@
 /* -*- 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 java.util.EnumSet;
-
 import org.mozilla.gecko.R;
-import org.mozilla.gecko.Telemetry;
-import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.ThumbnailHelper;
-import org.mozilla.gecko.db.BrowserContract.TopSites;
-import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
-import org.mozilla.gecko.util.StringUtils;
-
 import android.content.Context;
 import android.content.res.TypedArray;
-import android.database.Cursor;
 import android.graphics.Rect;
 import android.util.AttributeSet;
 import android.view.ContextMenu.ContextMenuInfo;
 import android.view.View;
 import android.widget.AbsListView;
-import android.widget.AdapterView;
 import android.widget.GridView;
 
 /**
  * A grid view of top and pinned sites.
  * Each cell in the grid is a TopSitesGridItemView.
  */
 public class TopSitesGridView extends GridView {
     private static final String LOGTAG = "GeckoTopSitesGridView";
@@ -51,22 +41,16 @@ public class TopSitesGridView extends Gr
     private final int mVerticalSpacing;
 
     // Measured width of this view.
     private int mMeasuredWidth;
 
     // Measured height of this view.
     private int mMeasuredHeight;
 
-    // On URL open listener.
-    private OnUrlOpenListener mUrlOpenListener;
-
-    // Edit pinned site listener.
-    private OnEditPinnedSiteListener mEditPinnedSiteListener;
-
     // Context menu info.
     private TopSitesGridContextMenuInfo mContextMenuInfo;
 
     // Whether we're handling focus changes or not. This is used
     // to avoid infinite re-layouts when using this GridView as
     // a ListView header view (see bug 918044).
     private boolean mIsHandlingFocusChange;
 
@@ -85,108 +69,38 @@ public class TopSitesGridView extends Gr
         setNumColumns(mNumColumns);
 
         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TopSitesGridView, defStyle, 0);
         mHorizontalSpacing = a.getDimensionPixelOffset(R.styleable.TopSitesGridView_android_horizontalSpacing, 0x00);
         mVerticalSpacing = a.getDimensionPixelOffset(R.styleable.TopSitesGridView_android_verticalSpacing, 0x00);
         a.recycle();
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void onAttachedToWindow() {
-        super.onAttachedToWindow();
-
-        setOnItemClickListener(new AdapterView.OnItemClickListener() {
-            @Override
-            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-                TopSitesGridItemView item = (TopSitesGridItemView) view;
-
-                // Decode "user-entered" URLs before loading them.
-                String url = StringUtils.decodeUserEnteredUrl(item.getUrl());
-                int type = item.getType();
-
-                // If the url is empty, the user can pin a site.
-                // If not, navigate to the page given by the url.
-                if (type != TopSites.TYPE_BLANK) {
-                    if (mUrlOpenListener != null) {
-                        final TelemetryContract.Method method;
-                        if (type == TopSites.TYPE_SUGGESTED) {
-                            method = TelemetryContract.Method.SUGGESTION;
-                        } else {
-                            method = TelemetryContract.Method.GRID_ITEM;
-                        }
-                        Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL, method, Integer.toString(position));
-
-                        mUrlOpenListener.onUrlOpen(url, EnumSet.noneOf(OnUrlOpenListener.Flags.class));
-                    }
-                } else {
-                    if (mEditPinnedSiteListener != null) {
-                        mEditPinnedSiteListener.onEditPinnedSite(position, "");
-                    }
-                }
-            }
-        });
-
-        setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
-            @Override
-            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
-                Cursor cursor = (Cursor) parent.getItemAtPosition(position);
-
-                TopSitesGridItemView item = (TopSitesGridItemView) view;
-                if (cursor == null || item.getType() == TopSites.TYPE_BLANK) {
-                    mContextMenuInfo = null;
-                    return false;
-                }
-
-                mContextMenuInfo = new TopSitesGridContextMenuInfo(view, position, id);
-                updateContextMenuFromCursor(mContextMenuInfo, cursor);
-                return showContextMenuForChild(TopSitesGridView.this);
-            }
-        });
-    }
-
-    @Override
-    public void onDetachedFromWindow() {
-        super.onDetachedFromWindow();
-
-        mUrlOpenListener = null;
-        mEditPinnedSiteListener = null;
-    }
-
     @Override
     protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
         mIsHandlingFocusChange = true;
         super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
         mIsHandlingFocusChange = false;
     }
 
     @Override
     public void requestLayout() {
         if (!mIsHandlingFocusChange) {
             super.requestLayout();
         }
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public int getColumnWidth() {
         // This method will be called from onMeasure() too.
         // It's better to use getMeasuredWidth(), as it is safe in this case.
         final int totalHorizontalSpacing = mNumColumns > 0 ? (mNumColumns - 1) * mHorizontalSpacing : 0;
         return (getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - totalHorizontalSpacing) / mNumColumns;
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         // Sets the padding for this view.
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 
         final int measuredWidth = getMeasuredWidth();
         if (measuredWidth == mMeasuredWidth) {
             // Return the cached values as the width is the same.
@@ -231,45 +145,18 @@ public class TopSitesGridView extends Gr
         mMeasuredHeight = measuredHeight;
     }
 
     @Override
     public ContextMenuInfo getContextMenuInfo() {
         return mContextMenuInfo;
     }
 
-    /*
-     * Update the fields of a TopSitesGridContextMenuInfo object
-     * from a cursor.
-     *
-     * @param  info    context menu info object to be updated
-     * @param  cursor  used to update the context menu info object
-     */
-    private void updateContextMenuFromCursor(TopSitesGridContextMenuInfo info, Cursor cursor) {
-        info.url = cursor.getString(cursor.getColumnIndexOrThrow(TopSites.URL));
-        info.title = cursor.getString(cursor.getColumnIndexOrThrow(TopSites.TITLE));
-        info.type = cursor.getInt(cursor.getColumnIndexOrThrow(TopSites.TYPE));
-        info.historyId = cursor.getInt(cursor.getColumnIndexOrThrow(TopSites.HISTORY_ID));
-    }
-    /**
-     * Set an url open listener to be used by this view.
-     *
-     * @param listener An url open listener for this view.
-     */
-    public void setOnUrlOpenListener(OnUrlOpenListener listener) {
-        mUrlOpenListener = listener;
-    }
-
-    /**
-     * Set an edit pinned site listener to be used by this view.
-     *
-     * @param listener An edit pinned site listener for this view.
-     */
-    public void setOnEditPinnedSiteListener(final OnEditPinnedSiteListener listener) {
-        mEditPinnedSiteListener = listener;
+    public void setContextMenuInfo(TopSitesGridContextMenuInfo contextMenuInfo) {
+        mContextMenuInfo = contextMenuInfo;
     }
 
     /**
      * Stores information regarding the creation of the context menu for a GridView item.
      */
     public static class TopSitesGridContextMenuInfo extends HomeContextMenuInfo {
         public int type = -1;
 
--- a/mobile/android/base/home/TopSitesPanel.java
+++ b/mobile/android/base/home/TopSitesPanel.java
@@ -10,16 +10,17 @@ import static org.mozilla.gecko.db.URLMe
 
 import java.util.ArrayList;
 import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 import org.mozilla.gecko.GeckoProfile;
+import org.mozilla.gecko.GeckoSharedPrefs;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.db.BrowserContract.Thumbnails;
 import org.mozilla.gecko.db.BrowserContract.TopSites;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.URLMetadata;
 import org.mozilla.gecko.favicons.Favicons;
@@ -188,18 +189,79 @@ public class TopSitesPanel extends HomeF
                     info.bookmarkId =  -1;
                 } else {
                     info.bookmarkId = cursor.getInt(bookmarkIdCol);
                 }
                 return info;
             }
         });
 
-        mGrid.setOnUrlOpenListener(mUrlOpenListener);
-        mGrid.setOnEditPinnedSiteListener(mEditPinnedSiteListener);
+        mGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
+            @Override
+            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+                TopSitesGridItemView item = (TopSitesGridItemView) view;
+
+                // Decode "user-entered" URLs before loading them.
+                String url = StringUtils.decodeUserEnteredUrl(item.getUrl());
+                int type = item.getType();
+
+                // If the url is empty, the user can pin a site.
+                // If not, navigate to the page given by the url.
+                if (type != TopSites.TYPE_BLANK) {
+                    if (mUrlOpenListener != null) {
+                        final TelemetryContract.Method method;
+                        if (type == TopSites.TYPE_SUGGESTED) {
+                            method = TelemetryContract.Method.SUGGESTION;
+                        } else {
+                            method = TelemetryContract.Method.GRID_ITEM;
+                        }
+                        Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL, method, Integer.toString(position));
+
+                        mUrlOpenListener.onUrlOpen(url, EnumSet.noneOf(OnUrlOpenListener.Flags.class));
+                    }
+                } else {
+                    if (mEditPinnedSiteListener != null) {
+                        mEditPinnedSiteListener.onEditPinnedSite(position, "");
+                    }
+                }
+            }
+        });
+
+        mGrid.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
+            @Override
+            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
+
+                Cursor cursor = (Cursor) parent.getItemAtPosition(position);
+
+                TopSitesGridItemView item = (TopSitesGridItemView) view;
+                if (cursor == null || item.getType() == TopSites.TYPE_BLANK) {
+                    mGrid.setContextMenuInfo(null);
+                    return false;
+                }
+
+                TopSitesGridContextMenuInfo contextMenuInfo = new TopSitesGridContextMenuInfo(view, position, id);
+                updateContextMenuFromCursor(contextMenuInfo, cursor);
+                mGrid.setContextMenuInfo(contextMenuInfo);
+                return mGrid.showContextMenuForChild(mGrid);
+            }
+
+            /*
+             * Update the fields of a TopSitesGridContextMenuInfo object
+             * from a cursor.
+             *
+             * @param  info    context menu info object to be updated
+             * @param  cursor  used to update the context menu info object
+             */
+            private void updateContextMenuFromCursor(TopSitesGridContextMenuInfo info, Cursor cursor) {
+                info.url = cursor.getString(cursor.getColumnIndexOrThrow(TopSites.URL));
+                info.title = cursor.getString(cursor.getColumnIndexOrThrow(TopSites.TITLE));
+                info.type = cursor.getInt(cursor.getColumnIndexOrThrow(TopSites.TYPE));
+                info.historyId = cursor.getInt(cursor.getColumnIndexOrThrow(TopSites.HISTORY_ID));
+            }
+        });
 
         registerForContextMenu(mList);
         registerForContextMenu(mGrid);
     }
 
     @Override
     public void onDestroyView() {
         super.onDestroyView();