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 org.mozilla.gecko.R; michael@0: import org.mozilla.gecko.home.HomePager.OnUrlOpenListener; michael@0: michael@0: import android.content.Context; michael@0: import android.content.res.TypedArray; michael@0: import android.database.Cursor; michael@0: import android.graphics.drawable.Drawable; michael@0: import android.util.AttributeSet; michael@0: import android.view.ContextMenu.ContextMenuInfo; michael@0: import android.view.View; michael@0: import android.widget.AdapterView; michael@0: import android.widget.AdapterView.OnItemLongClickListener; michael@0: import android.widget.ListView; michael@0: michael@0: /** michael@0: * HomeListView is a custom extension of ListView, that packs a HomeContextMenuInfo michael@0: * when any of its rows is long pressed. michael@0: */ michael@0: public class HomeListView extends ListView michael@0: implements OnItemLongClickListener { michael@0: michael@0: // ContextMenuInfo associated with the currently long pressed list item. michael@0: private HomeContextMenuInfo mContextMenuInfo; michael@0: michael@0: // On URL open listener michael@0: protected OnUrlOpenListener mUrlOpenListener; michael@0: michael@0: // Top divider michael@0: private boolean mShowTopDivider; michael@0: michael@0: // ContextMenuInfo maker michael@0: private HomeContextMenuInfo.Factory mContextMenuInfoFactory; michael@0: michael@0: public HomeListView(Context context) { michael@0: this(context, null); michael@0: } michael@0: michael@0: public HomeListView(Context context, AttributeSet attrs) { michael@0: this(context, attrs, R.attr.homeListViewStyle); michael@0: } michael@0: michael@0: public HomeListView(Context context, AttributeSet attrs, int defStyle) { michael@0: super(context, attrs, defStyle); michael@0: michael@0: TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HomeListView, defStyle, 0); michael@0: mShowTopDivider = a.getBoolean(R.styleable.HomeListView_topDivider, false); michael@0: a.recycle(); michael@0: michael@0: setOnItemLongClickListener(this); michael@0: } michael@0: michael@0: @Override michael@0: public void onAttachedToWindow() { michael@0: super.onAttachedToWindow(); michael@0: michael@0: final Drawable divider = getDivider(); michael@0: if (mShowTopDivider && divider != null) { michael@0: final int dividerHeight = getDividerHeight(); michael@0: final View view = new View(getContext()); michael@0: view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, dividerHeight)); michael@0: addHeaderView(view); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public void onDetachedFromWindow() { michael@0: super.onDetachedFromWindow(); michael@0: michael@0: mUrlOpenListener = null; michael@0: } michael@0: michael@0: @Override michael@0: public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { michael@0: Object item = parent.getItemAtPosition(position); michael@0: michael@0: // HomeListView could hold headers too. Add a context menu info only for its children. michael@0: if (item instanceof Cursor) { michael@0: Cursor cursor = (Cursor) item; 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(HomeListView.this); michael@0: michael@0: } else { michael@0: mContextMenuInfo = null; michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public ContextMenuInfo getContextMenuInfo() { michael@0: return mContextMenuInfo; michael@0: } michael@0: michael@0: @Override michael@0: public void setOnItemClickListener(final AdapterView.OnItemClickListener listener) { michael@0: if (listener == null) { michael@0: super.setOnItemClickListener(null); michael@0: return; michael@0: } michael@0: michael@0: super.setOnItemClickListener(new AdapterView.OnItemClickListener() { michael@0: @Override michael@0: public void onItemClick(AdapterView parent, View view, int position, long id) { michael@0: if (mShowTopDivider) { michael@0: position--; michael@0: } michael@0: michael@0: listener.onItemClick(parent, view, position, id); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: public void setContextMenuInfoFactory(final HomeContextMenuInfo.Factory factory) { michael@0: mContextMenuInfoFactory = factory; michael@0: } michael@0: michael@0: public OnUrlOpenListener getOnUrlOpenListener() { michael@0: return mUrlOpenListener; michael@0: } michael@0: michael@0: public void setOnUrlOpenListener(OnUrlOpenListener listener) { michael@0: mUrlOpenListener = listener; michael@0: } michael@0: }