From f78d68857222f9b08dc84417ef175f7c4b080d21 Mon Sep 17 00:00:00 2001 From: Lucas Rocha Date: Tue, 25 Feb 2014 10:41:57 +0000 Subject: [PATCH] Bug 972351 - Factor URL handling into PanelViewUrlHandler (r=margaret) --- mobile/android/base/home/PanelGridView.java | 25 ++++------ mobile/android/base/home/PanelListView.java | 26 +++++------ .../base/home/PanelViewUrlHandler.java | 46 +++++++++++++++++++ mobile/android/base/moz.build | 1 + 4 files changed, 67 insertions(+), 31 deletions(-) create mode 100644 mobile/android/base/home/PanelViewUrlHandler.java diff --git a/mobile/android/base/home/PanelGridView.java b/mobile/android/base/home/PanelGridView.java index f1c198909f48..7b1f89f353d2 100644 --- a/mobile/android/base/home/PanelGridView.java +++ b/mobile/android/base/home/PanelGridView.java @@ -27,20 +27,24 @@ public class PanelGridView extends GridView 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 @@ -50,26 +54,13 @@ public class PanelGridView extends GridView @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 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); } } } diff --git a/mobile/android/base/home/PanelListView.java b/mobile/android/base/home/PanelListView.java index 69e7324b4529..50c32c98d5bf 100644 --- a/mobile/android/base/home/PanelListView.java +++ b/mobile/android/base/home/PanelListView.java @@ -27,12 +27,17 @@ public class PanelListView extends HomeListView 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()); } @@ -42,23 +47,16 @@ public class PanelListView extends HomeListView 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 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); } } } diff --git a/mobile/android/base/home/PanelViewUrlHandler.java b/mobile/android/base/home/PanelViewUrlHandler.java new file mode 100644 index 000000000000..a2669de51e30 --- /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 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); + } + } +} diff --git a/mobile/android/base/moz.build b/mobile/android/base/moz.build index 05bcee108c48..c887c52990f4 100644 --- a/mobile/android/base/moz.build +++ b/mobile/android/base/moz.build @@ -239,6 +239,7 @@ gbjar.sources += [ 'home/PanelListView.java', 'home/PanelManager.java', 'home/PanelViewAdapter.java', + 'home/PanelViewUrlHandler.java', 'home/PinSiteDialog.java', 'home/ReadingListPanel.java', 'home/SearchEngine.java',