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