michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.home; michael@0: michael@0: import java.util.EnumSet; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: import org.mozilla.gecko.db.BrowserContract.HomeItems; michael@0: import org.mozilla.gecko.home.HomeConfig.ItemHandler; michael@0: import org.mozilla.gecko.home.HomeConfig.ViewConfig; michael@0: import org.mozilla.gecko.home.HomePager.OnUrlOpenListener; michael@0: import org.mozilla.gecko.home.PanelLayout.DatasetBacked; michael@0: import org.mozilla.gecko.home.PanelLayout.FilterManager; michael@0: import org.mozilla.gecko.home.PanelLayout.OnItemOpenListener; michael@0: import org.mozilla.gecko.home.PanelLayout.PanelView; michael@0: michael@0: import android.content.Context; michael@0: import android.database.Cursor; michael@0: import android.util.Log; michael@0: import android.view.View; michael@0: import android.widget.AdapterView; michael@0: import android.widget.GridView; michael@0: michael@0: public class PanelGridView extends GridView michael@0: implements DatasetBacked, PanelView { michael@0: private static final String LOGTAG = "GeckoPanelGridView"; michael@0: michael@0: private final ViewConfig viewConfig; michael@0: private final PanelViewAdapter adapter; michael@0: private PanelViewItemHandler itemHandler; michael@0: private OnItemOpenListener itemOpenListener; michael@0: private HomeContextMenuInfo mContextMenuInfo; michael@0: private HomeContextMenuInfo.Factory mContextMenuInfoFactory; michael@0: michael@0: public PanelGridView(Context context, ViewConfig viewConfig) { michael@0: super(context, null, R.attr.panelGridViewStyle); michael@0: michael@0: this.viewConfig = viewConfig; michael@0: itemHandler = new PanelViewItemHandler(viewConfig); michael@0: michael@0: adapter = new PanelViewAdapter(context, viewConfig); michael@0: setAdapter(adapter); michael@0: michael@0: setOnItemClickListener(new PanelGridItemClickListener()); michael@0: setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { michael@0: @Override michael@0: public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { michael@0: Cursor cursor = (Cursor) parent.getItemAtPosition(position); michael@0: if (cursor == null || mContextMenuInfoFactory == null) { michael@0: mContextMenuInfo = null; michael@0: return false; michael@0: } michael@0: michael@0: mContextMenuInfo = mContextMenuInfoFactory.makeInfoForCursor(view, position, id, cursor); michael@0: return showContextMenuForChild(PanelGridView.this); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: @Override michael@0: public void onAttachedToWindow() { michael@0: super.onAttachedToWindow(); michael@0: itemHandler.setOnItemOpenListener(itemOpenListener); michael@0: } michael@0: michael@0: @Override michael@0: public void onDetachedFromWindow() { michael@0: super.onDetachedFromWindow(); michael@0: itemHandler.setOnItemOpenListener(null); michael@0: } michael@0: michael@0: @Override michael@0: public void setDataset(Cursor cursor) { michael@0: Log.d(LOGTAG, "Setting dataset: " + viewConfig.getDatasetId()); michael@0: adapter.swapCursor(cursor); michael@0: } michael@0: michael@0: @Override michael@0: public void setOnItemOpenListener(OnItemOpenListener listener) { michael@0: itemHandler.setOnItemOpenListener(listener); michael@0: itemOpenListener = listener; michael@0: } michael@0: michael@0: @Override michael@0: public void setFilterManager(FilterManager filterManager) { michael@0: adapter.setFilterManager(filterManager); michael@0: itemHandler.setFilterManager(filterManager); michael@0: } michael@0: michael@0: private class PanelGridItemClickListener implements AdapterView.OnItemClickListener { michael@0: @Override michael@0: public void onItemClick(AdapterView parent, View view, int position, long id) { michael@0: itemHandler.openItemAtPosition(adapter.getCursor(), position); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public HomeContextMenuInfo getContextMenuInfo() { michael@0: return mContextMenuInfo; michael@0: } michael@0: michael@0: @Override michael@0: public void setContextMenuInfoFactory(HomeContextMenuInfo.Factory factory) { michael@0: mContextMenuInfoFactory = factory; michael@0: } michael@0: }