Bug 972351 - Factor URL handling into PanelViewUrlHandler (r=margaret)
authorLucas Rocha <lucasr@mozilla.com>
Tue, 25 Feb 2014 10:41:57 +0000
changeset 170866 6c895af7ccf169ab9bc3cea07618a59b9ee4c1cb
parent 170865 053ed9efd81bea92f4f529378a86b0b7462c687e
child 170867 989f56e2fca980b0518bf136a0a07ed63ab1ad02
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersmargaret
bugs972351
milestone30.0a1
Bug 972351 - Factor URL handling into PanelViewUrlHandler (r=margaret)
mobile/android/base/home/PanelGridView.java
mobile/android/base/home/PanelListView.java
mobile/android/base/home/PanelViewUrlHandler.java
mobile/android/base/moz.build
--- a/mobile/android/base/home/PanelGridView.java
+++ b/mobile/android/base/home/PanelGridView.java
@@ -22,54 +22,45 @@ import android.widget.AdapterView;
 import android.widget.GridView;
 
 public class PanelGridView extends GridView
                            implements DatasetBacked, PanelView {
     private static final String LOGTAG = "GeckoPanelGridView";
 
     private final ViewConfig mViewConfig;
     private final PanelViewAdapter mAdapter;
-    protected OnUrlOpenListener mUrlOpenListener;
+    private PanelViewUrlHandler mUrlHandler;
 
     public PanelGridView(Context context, ViewConfig viewConfig) {
         super(context, null, R.attr.panelGridViewStyle);
+
         mViewConfig = viewConfig;
+        mUrlHandler = new PanelViewUrlHandler(viewConfig);
+
         mAdapter = new PanelViewAdapter(context, viewConfig.getItemType());
         setAdapter(mAdapter);
+
         setOnItemClickListener(new PanelGridItemClickListener());
     }
 
     @Override
     public void onDetachedFromWindow() {
         super.onDetachedFromWindow();
-        mUrlOpenListener = null;
+        mUrlHandler.setOnUrlOpenListener(null);
     }
 
     @Override
     public void setDataset(Cursor cursor) {
         mAdapter.swapCursor(cursor);
     }
 
     @Override
     public void setOnUrlOpenListener(OnUrlOpenListener listener) {
-        mUrlOpenListener = listener;
+        mUrlHandler.setOnUrlOpenListener(listener);
     }
 
     private class PanelGridItemClickListener implements AdapterView.OnItemClickListener {
         @Override
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-            Cursor cursor = mAdapter.getCursor();
-            if (cursor == null || !cursor.moveToPosition(position)) {
-                throw new IllegalStateException("Couldn't move cursor to position " + position);
-            }
-
-            int urlIndex = cursor.getColumnIndexOrThrow(HomeItems.URL);
-            final String url = cursor.getString(urlIndex);
-
-            EnumSet<OnUrlOpenListener.Flags> flags = EnumSet.noneOf(OnUrlOpenListener.Flags.class);
-            if (mViewConfig.getItemHandler() == ItemHandler.INTENT) {
-                flags.add(OnUrlOpenListener.Flags.OPEN_WITH_INTENT);
-            }
-
-            mUrlOpenListener.onUrlOpen(url, flags);
+            mUrlHandler.openUrlAtPosition(mAdapter.getCursor(), position);
         }
     }
 }
--- a/mobile/android/base/home/PanelListView.java
+++ b/mobile/android/base/home/PanelListView.java
@@ -22,43 +22,41 @@ import android.widget.AdapterView;
 
 public class PanelListView extends HomeListView
                            implements DatasetBacked, PanelView {
 
     private static final String LOGTAG = "GeckoPanelListView";
 
     private final PanelViewAdapter mAdapter;
     private final ViewConfig mViewConfig;
+    private final PanelViewUrlHandler mUrlHandler;
 
     public PanelListView(Context context, ViewConfig viewConfig) {
         super(context);
+
         mViewConfig = viewConfig;
+        mUrlHandler = new PanelViewUrlHandler(viewConfig);
+
         mAdapter = new PanelViewAdapter(context, viewConfig.getItemType());
         setAdapter(mAdapter);
+
         setOnItemClickListener(new PanelListItemClickListener());
     }
 
     @Override
     public void setDataset(Cursor cursor) {
         Log.d(LOGTAG, "Setting dataset: " + mViewConfig.getDatasetId());
         mAdapter.swapCursor(cursor);
     }
 
+    @Override
+    public void setOnUrlOpenListener(OnUrlOpenListener listener) {
+        super.setOnUrlOpenListener(listener);
+        mUrlHandler.setOnUrlOpenListener(listener);
+    }
+
     private class PanelListItemClickListener implements AdapterView.OnItemClickListener {
         @Override
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-            Cursor cursor = mAdapter.getCursor();
-            if (cursor == null || !cursor.moveToPosition(position)) {
-                throw new IllegalStateException("Couldn't move cursor to position " + position);
-            }
-
-            int urlIndex = cursor.getColumnIndexOrThrow(HomeItems.URL);
-            final String url = cursor.getString(urlIndex);
-
-            EnumSet<OnUrlOpenListener.Flags> flags = EnumSet.noneOf(OnUrlOpenListener.Flags.class);
-            if (mViewConfig.getItemHandler() == ItemHandler.INTENT) {
-                flags.add(OnUrlOpenListener.Flags.OPEN_WITH_INTENT);
-            }
-
-            mUrlOpenListener.onUrlOpen(url, flags);
+            mUrlHandler.openUrlAtPosition(mAdapter.getCursor(), position);
         }
     }
 }
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/home/PanelViewUrlHandler.java
@@ -0,0 +1,46 @@
+/* -*- 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 org.mozilla.gecko.db.BrowserContract.HomeItems;
+import org.mozilla.gecko.home.HomeConfig.ItemHandler;
+import org.mozilla.gecko.home.HomeConfig.ViewConfig;
+import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
+
+import android.database.Cursor;
+
+import java.util.EnumSet;
+
+class PanelViewUrlHandler {
+    private final ViewConfig mViewConfig;
+    private OnUrlOpenListener mUrlOpenListener;
+
+    public PanelViewUrlHandler(ViewConfig viewConfig) {
+        mViewConfig = viewConfig;
+    }
+
+    public void setOnUrlOpenListener(OnUrlOpenListener listener) {
+        mUrlOpenListener = listener;
+    }
+
+    public void openUrlAtPosition(Cursor cursor, int position) {
+        if (cursor == null || !cursor.moveToPosition(position)) {
+            throw new IllegalStateException("Couldn't move cursor to position " + position);
+        }
+
+        int urlIndex = cursor.getColumnIndexOrThrow(HomeItems.URL);
+        final String url = cursor.getString(urlIndex);
+
+        EnumSet<OnUrlOpenListener.Flags> flags = EnumSet.noneOf(OnUrlOpenListener.Flags.class);
+        if (mViewConfig.getItemHandler() == ItemHandler.INTENT) {
+            flags.add(OnUrlOpenListener.Flags.OPEN_WITH_INTENT);
+        }
+
+        if (mUrlOpenListener != null) {
+            mUrlOpenListener.onUrlOpen(url, flags);
+        }
+    }
+}
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -234,16 +234,17 @@ gbjar.sources += [
     'home/MostRecentPanel.java',
     'home/MultiTypeCursorAdapter.java',
     'home/PanelGridView.java',
     'home/PanelItemView.java',
     'home/PanelLayout.java',
     'home/PanelListView.java',
     'home/PanelManager.java',
     'home/PanelViewAdapter.java',
+    'home/PanelViewUrlHandler.java',
     'home/PinSiteDialog.java',
     'home/ReadingListPanel.java',
     'home/SearchEngine.java',
     'home/SearchEngineRow.java',
     'home/SearchLoader.java',
     'home/SimpleCursorLoader.java',
     'home/SuggestClient.java',
     'home/TabMenuStrip.java',