mobile/android/base/home/HomeListView.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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 org.mozilla.gecko.R;
michael@0 9 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
michael@0 10
michael@0 11 import android.content.Context;
michael@0 12 import android.content.res.TypedArray;
michael@0 13 import android.database.Cursor;
michael@0 14 import android.graphics.drawable.Drawable;
michael@0 15 import android.util.AttributeSet;
michael@0 16 import android.view.ContextMenu.ContextMenuInfo;
michael@0 17 import android.view.View;
michael@0 18 import android.widget.AdapterView;
michael@0 19 import android.widget.AdapterView.OnItemLongClickListener;
michael@0 20 import android.widget.ListView;
michael@0 21
michael@0 22 /**
michael@0 23 * HomeListView is a custom extension of ListView, that packs a HomeContextMenuInfo
michael@0 24 * when any of its rows is long pressed.
michael@0 25 */
michael@0 26 public class HomeListView extends ListView
michael@0 27 implements OnItemLongClickListener {
michael@0 28
michael@0 29 // ContextMenuInfo associated with the currently long pressed list item.
michael@0 30 private HomeContextMenuInfo mContextMenuInfo;
michael@0 31
michael@0 32 // On URL open listener
michael@0 33 protected OnUrlOpenListener mUrlOpenListener;
michael@0 34
michael@0 35 // Top divider
michael@0 36 private boolean mShowTopDivider;
michael@0 37
michael@0 38 // ContextMenuInfo maker
michael@0 39 private HomeContextMenuInfo.Factory mContextMenuInfoFactory;
michael@0 40
michael@0 41 public HomeListView(Context context) {
michael@0 42 this(context, null);
michael@0 43 }
michael@0 44
michael@0 45 public HomeListView(Context context, AttributeSet attrs) {
michael@0 46 this(context, attrs, R.attr.homeListViewStyle);
michael@0 47 }
michael@0 48
michael@0 49 public HomeListView(Context context, AttributeSet attrs, int defStyle) {
michael@0 50 super(context, attrs, defStyle);
michael@0 51
michael@0 52 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HomeListView, defStyle, 0);
michael@0 53 mShowTopDivider = a.getBoolean(R.styleable.HomeListView_topDivider, false);
michael@0 54 a.recycle();
michael@0 55
michael@0 56 setOnItemLongClickListener(this);
michael@0 57 }
michael@0 58
michael@0 59 @Override
michael@0 60 public void onAttachedToWindow() {
michael@0 61 super.onAttachedToWindow();
michael@0 62
michael@0 63 final Drawable divider = getDivider();
michael@0 64 if (mShowTopDivider && divider != null) {
michael@0 65 final int dividerHeight = getDividerHeight();
michael@0 66 final View view = new View(getContext());
michael@0 67 view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, dividerHeight));
michael@0 68 addHeaderView(view);
michael@0 69 }
michael@0 70 }
michael@0 71
michael@0 72 @Override
michael@0 73 public void onDetachedFromWindow() {
michael@0 74 super.onDetachedFromWindow();
michael@0 75
michael@0 76 mUrlOpenListener = null;
michael@0 77 }
michael@0 78
michael@0 79 @Override
michael@0 80 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
michael@0 81 Object item = parent.getItemAtPosition(position);
michael@0 82
michael@0 83 // HomeListView could hold headers too. Add a context menu info only for its children.
michael@0 84 if (item instanceof Cursor) {
michael@0 85 Cursor cursor = (Cursor) item;
michael@0 86 if (cursor == null || mContextMenuInfoFactory == null) {
michael@0 87 mContextMenuInfo = null;
michael@0 88 return false;
michael@0 89 }
michael@0 90
michael@0 91 mContextMenuInfo = mContextMenuInfoFactory.makeInfoForCursor(view, position, id, cursor);
michael@0 92 return showContextMenuForChild(HomeListView.this);
michael@0 93
michael@0 94 } else {
michael@0 95 mContextMenuInfo = null;
michael@0 96 return false;
michael@0 97 }
michael@0 98 }
michael@0 99
michael@0 100 @Override
michael@0 101 public ContextMenuInfo getContextMenuInfo() {
michael@0 102 return mContextMenuInfo;
michael@0 103 }
michael@0 104
michael@0 105 @Override
michael@0 106 public void setOnItemClickListener(final AdapterView.OnItemClickListener listener) {
michael@0 107 if (listener == null) {
michael@0 108 super.setOnItemClickListener(null);
michael@0 109 return;
michael@0 110 }
michael@0 111
michael@0 112 super.setOnItemClickListener(new AdapterView.OnItemClickListener() {
michael@0 113 @Override
michael@0 114 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
michael@0 115 if (mShowTopDivider) {
michael@0 116 position--;
michael@0 117 }
michael@0 118
michael@0 119 listener.onItemClick(parent, view, position, id);
michael@0 120 }
michael@0 121 });
michael@0 122 }
michael@0 123
michael@0 124 public void setContextMenuInfoFactory(final HomeContextMenuInfo.Factory factory) {
michael@0 125 mContextMenuInfoFactory = factory;
michael@0 126 }
michael@0 127
michael@0 128 public OnUrlOpenListener getOnUrlOpenListener() {
michael@0 129 return mUrlOpenListener;
michael@0 130 }
michael@0 131
michael@0 132 public void setOnUrlOpenListener(OnUrlOpenListener listener) {
michael@0 133 mUrlOpenListener = listener;
michael@0 134 }
michael@0 135 }

mercurial