Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.home; |
michael@0 | 7 | |
michael@0 | 8 | import java.util.EnumSet; |
michael@0 | 9 | |
michael@0 | 10 | import org.mozilla.gecko.R; |
michael@0 | 11 | import org.mozilla.gecko.db.BrowserContract.HomeItems; |
michael@0 | 12 | import org.mozilla.gecko.home.HomeConfig.ItemHandler; |
michael@0 | 13 | import org.mozilla.gecko.home.HomeConfig.ViewConfig; |
michael@0 | 14 | import org.mozilla.gecko.home.HomePager.OnUrlOpenListener; |
michael@0 | 15 | import org.mozilla.gecko.home.PanelLayout.DatasetBacked; |
michael@0 | 16 | import org.mozilla.gecko.home.PanelLayout.FilterManager; |
michael@0 | 17 | import org.mozilla.gecko.home.PanelLayout.OnItemOpenListener; |
michael@0 | 18 | import org.mozilla.gecko.home.PanelLayout.PanelView; |
michael@0 | 19 | |
michael@0 | 20 | import android.content.Context; |
michael@0 | 21 | import android.database.Cursor; |
michael@0 | 22 | import android.util.Log; |
michael@0 | 23 | import android.view.View; |
michael@0 | 24 | import android.widget.AdapterView; |
michael@0 | 25 | import android.widget.GridView; |
michael@0 | 26 | |
michael@0 | 27 | public class PanelGridView extends GridView |
michael@0 | 28 | implements DatasetBacked, PanelView { |
michael@0 | 29 | private static final String LOGTAG = "GeckoPanelGridView"; |
michael@0 | 30 | |
michael@0 | 31 | private final ViewConfig viewConfig; |
michael@0 | 32 | private final PanelViewAdapter adapter; |
michael@0 | 33 | private PanelViewItemHandler itemHandler; |
michael@0 | 34 | private OnItemOpenListener itemOpenListener; |
michael@0 | 35 | private HomeContextMenuInfo mContextMenuInfo; |
michael@0 | 36 | private HomeContextMenuInfo.Factory mContextMenuInfoFactory; |
michael@0 | 37 | |
michael@0 | 38 | public PanelGridView(Context context, ViewConfig viewConfig) { |
michael@0 | 39 | super(context, null, R.attr.panelGridViewStyle); |
michael@0 | 40 | |
michael@0 | 41 | this.viewConfig = viewConfig; |
michael@0 | 42 | itemHandler = new PanelViewItemHandler(viewConfig); |
michael@0 | 43 | |
michael@0 | 44 | adapter = new PanelViewAdapter(context, viewConfig); |
michael@0 | 45 | setAdapter(adapter); |
michael@0 | 46 | |
michael@0 | 47 | setOnItemClickListener(new PanelGridItemClickListener()); |
michael@0 | 48 | setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { |
michael@0 | 49 | @Override |
michael@0 | 50 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
michael@0 | 51 | Cursor cursor = (Cursor) parent.getItemAtPosition(position); |
michael@0 | 52 | if (cursor == null || mContextMenuInfoFactory == null) { |
michael@0 | 53 | mContextMenuInfo = null; |
michael@0 | 54 | return false; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | mContextMenuInfo = mContextMenuInfoFactory.makeInfoForCursor(view, position, id, cursor); |
michael@0 | 58 | return showContextMenuForChild(PanelGridView.this); |
michael@0 | 59 | } |
michael@0 | 60 | }); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | @Override |
michael@0 | 64 | public void onAttachedToWindow() { |
michael@0 | 65 | super.onAttachedToWindow(); |
michael@0 | 66 | itemHandler.setOnItemOpenListener(itemOpenListener); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | @Override |
michael@0 | 70 | public void onDetachedFromWindow() { |
michael@0 | 71 | super.onDetachedFromWindow(); |
michael@0 | 72 | itemHandler.setOnItemOpenListener(null); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | @Override |
michael@0 | 76 | public void setDataset(Cursor cursor) { |
michael@0 | 77 | Log.d(LOGTAG, "Setting dataset: " + viewConfig.getDatasetId()); |
michael@0 | 78 | adapter.swapCursor(cursor); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | @Override |
michael@0 | 82 | public void setOnItemOpenListener(OnItemOpenListener listener) { |
michael@0 | 83 | itemHandler.setOnItemOpenListener(listener); |
michael@0 | 84 | itemOpenListener = listener; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | @Override |
michael@0 | 88 | public void setFilterManager(FilterManager filterManager) { |
michael@0 | 89 | adapter.setFilterManager(filterManager); |
michael@0 | 90 | itemHandler.setFilterManager(filterManager); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | private class PanelGridItemClickListener implements AdapterView.OnItemClickListener { |
michael@0 | 94 | @Override |
michael@0 | 95 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
michael@0 | 96 | itemHandler.openItemAtPosition(adapter.getCursor(), position); |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | @Override |
michael@0 | 101 | public HomeContextMenuInfo getContextMenuInfo() { |
michael@0 | 102 | return mContextMenuInfo; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | @Override |
michael@0 | 106 | public void setContextMenuInfoFactory(HomeContextMenuInfo.Factory factory) { |
michael@0 | 107 | mContextMenuInfoFactory = factory; |
michael@0 | 108 | } |
michael@0 | 109 | } |