1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/home/HomeListView.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,135 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.home; 1.10 + 1.11 +import org.mozilla.gecko.R; 1.12 +import org.mozilla.gecko.home.HomePager.OnUrlOpenListener; 1.13 + 1.14 +import android.content.Context; 1.15 +import android.content.res.TypedArray; 1.16 +import android.database.Cursor; 1.17 +import android.graphics.drawable.Drawable; 1.18 +import android.util.AttributeSet; 1.19 +import android.view.ContextMenu.ContextMenuInfo; 1.20 +import android.view.View; 1.21 +import android.widget.AdapterView; 1.22 +import android.widget.AdapterView.OnItemLongClickListener; 1.23 +import android.widget.ListView; 1.24 + 1.25 +/** 1.26 + * HomeListView is a custom extension of ListView, that packs a HomeContextMenuInfo 1.27 + * when any of its rows is long pressed. 1.28 + */ 1.29 +public class HomeListView extends ListView 1.30 + implements OnItemLongClickListener { 1.31 + 1.32 + // ContextMenuInfo associated with the currently long pressed list item. 1.33 + private HomeContextMenuInfo mContextMenuInfo; 1.34 + 1.35 + // On URL open listener 1.36 + protected OnUrlOpenListener mUrlOpenListener; 1.37 + 1.38 + // Top divider 1.39 + private boolean mShowTopDivider; 1.40 + 1.41 + // ContextMenuInfo maker 1.42 + private HomeContextMenuInfo.Factory mContextMenuInfoFactory; 1.43 + 1.44 + public HomeListView(Context context) { 1.45 + this(context, null); 1.46 + } 1.47 + 1.48 + public HomeListView(Context context, AttributeSet attrs) { 1.49 + this(context, attrs, R.attr.homeListViewStyle); 1.50 + } 1.51 + 1.52 + public HomeListView(Context context, AttributeSet attrs, int defStyle) { 1.53 + super(context, attrs, defStyle); 1.54 + 1.55 + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HomeListView, defStyle, 0); 1.56 + mShowTopDivider = a.getBoolean(R.styleable.HomeListView_topDivider, false); 1.57 + a.recycle(); 1.58 + 1.59 + setOnItemLongClickListener(this); 1.60 + } 1.61 + 1.62 + @Override 1.63 + public void onAttachedToWindow() { 1.64 + super.onAttachedToWindow(); 1.65 + 1.66 + final Drawable divider = getDivider(); 1.67 + if (mShowTopDivider && divider != null) { 1.68 + final int dividerHeight = getDividerHeight(); 1.69 + final View view = new View(getContext()); 1.70 + view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, dividerHeight)); 1.71 + addHeaderView(view); 1.72 + } 1.73 + } 1.74 + 1.75 + @Override 1.76 + public void onDetachedFromWindow() { 1.77 + super.onDetachedFromWindow(); 1.78 + 1.79 + mUrlOpenListener = null; 1.80 + } 1.81 + 1.82 + @Override 1.83 + public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 1.84 + Object item = parent.getItemAtPosition(position); 1.85 + 1.86 + // HomeListView could hold headers too. Add a context menu info only for its children. 1.87 + if (item instanceof Cursor) { 1.88 + Cursor cursor = (Cursor) item; 1.89 + if (cursor == null || mContextMenuInfoFactory == null) { 1.90 + mContextMenuInfo = null; 1.91 + return false; 1.92 + } 1.93 + 1.94 + mContextMenuInfo = mContextMenuInfoFactory.makeInfoForCursor(view, position, id, cursor); 1.95 + return showContextMenuForChild(HomeListView.this); 1.96 + 1.97 + } else { 1.98 + mContextMenuInfo = null; 1.99 + return false; 1.100 + } 1.101 + } 1.102 + 1.103 + @Override 1.104 + public ContextMenuInfo getContextMenuInfo() { 1.105 + return mContextMenuInfo; 1.106 + } 1.107 + 1.108 + @Override 1.109 + public void setOnItemClickListener(final AdapterView.OnItemClickListener listener) { 1.110 + if (listener == null) { 1.111 + super.setOnItemClickListener(null); 1.112 + return; 1.113 + } 1.114 + 1.115 + super.setOnItemClickListener(new AdapterView.OnItemClickListener() { 1.116 + @Override 1.117 + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 1.118 + if (mShowTopDivider) { 1.119 + position--; 1.120 + } 1.121 + 1.122 + listener.onItemClick(parent, view, position, id); 1.123 + } 1.124 + }); 1.125 + } 1.126 + 1.127 + public void setContextMenuInfoFactory(final HomeContextMenuInfo.Factory factory) { 1.128 + mContextMenuInfoFactory = factory; 1.129 + } 1.130 + 1.131 + public OnUrlOpenListener getOnUrlOpenListener() { 1.132 + return mUrlOpenListener; 1.133 + } 1.134 + 1.135 + public void setOnUrlOpenListener(OnUrlOpenListener listener) { 1.136 + mUrlOpenListener = listener; 1.137 + } 1.138 +}