1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.browser;
import android.content.Context;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
/**
* This class implements a Grid layout of Views for the Tab picker.
*/
class ImageGrid extends GridView implements OnItemClickListener,
OnCreateContextMenuListener {
private Listener mListener;
private ImageAdapter mAdapter;
private boolean mIsLive;
private static final int SPACING = 10;
public static final int CANCEL = -99;
public static final int NEW_TAB = -1;
/**
* Constructor
* @param context Context to use when inflating resources.
* @param live TRUE if the view can accept touch or click
* @param l Listener to respond to clicks etc.
*/
public ImageGrid(Context context, boolean live, Listener l) {
super(context);
mIsLive = live;
if (live) {
setFocusable(true);
setFocusableInTouchMode(true);
setOnItemClickListener(this);
setOnCreateContextMenuListener(this);
}
mListener = l;
mAdapter = new ImageAdapter(context, this, live);
setAdapter(mAdapter);
setBackgroundColor(0xFF000000);
setVerticalSpacing(SPACING);
setHorizontalSpacing(SPACING);
setNumColumns(2);
setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
setSelector(android.R.drawable.gallery_thumb);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
// We always consume the BACK key even if mListener is null or the
// ImageGrid is not "live." This prevents crashes during tab animations
// if the user presses BACK.
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(event.getKeyCode() == KeyEvent.KEYCODE_BACK)) {
if (mListener != null && mIsLive) {
mListener.onClick(CANCEL);
invalidate();
}
return true;
}
return super.dispatchKeyEvent(event);
}
/**
* Called by BrowserActivity to add a new window to the tab picker.
* This does not happen dynamically, this only happens during view
* setup.
*
* @param v Webview of the tab to add
* @param name Web page title
* @param url URL of the webpage
*/
public void add(TabControl.Tab t) {
mAdapter.add(t);
}
/**
* Called by BrowserActivity when a window has been removed from the
* tab list.
*
* @param index Window to remove, from 0 to MAX_TABS-1
*/
public void remove(int index) {
if (Browser.DEBUG && (index < 0 || index >= TabControl.MAX_TABS)) {
throw new AssertionError();
}
mAdapter.remove(index);
}
/**
* Request focus to initially set to a particular tab.
*
* @param startingIndex This is a Tab index from 0 - MAX_TABS-1 and does not
* include the "New Tab" cell.
*/
public void setCurrentIndex(int startingIndex) {
if (!mAdapter.maxedOut()) {
startingIndex++;
}
setSelection(startingIndex);
}
public Listener getListener() {
return mListener;
}
public void setListener(Listener l) {
mListener = l;
}
/**
* Return true if the ImageGrid is live. This means that tabs can be chosen
* and the menu can be invoked.
*/
public boolean isLive() {
return mIsLive;
}
/**
* Do some internal cleanup of the ImageGrid's adapter.
*/
public void clear() {
mAdapter.clear();
}
/* (non-Javadoc)
* @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
*/
public void onItemClick(AdapterView parent, View v, int position, long id) {
if (!mAdapter.maxedOut()) {
position--;
}
// Position will be -1 for the "New Tab" cell.
if (mListener != null) {
mListener.onClick(position);
}
}
/* (non-Javadoc)
* @see android.view.View.OnCreateContextMenuListener#onCreateContextMenu(android.view.ContextMenu, android.view.View, java.lang.Object)
*/
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// Do not create the context menu if there is no listener or the Tab
// overview is not "live."
if (mListener == null || !mIsLive) {
return;
}
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) menuInfo;
boolean maxed = mAdapter.maxedOut();
if (info.position > 0 || maxed) {
MenuInflater inflater = new MenuInflater(mContext);
inflater.inflate(R.menu.tabscontext, menu);
int position = info.position;
if (!maxed) {
position--;
}
menu.setHeaderTitle(mAdapter.mItems.get(position).getTitle());
}
}
// convert a context menu position to an actual tab position. Since context
// menus are not created for the "New Tab" cell, this will always return a
// valid tab position.
public int getContextMenuPosition(MenuItem menu) {
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) menu.getMenuInfo();
int pos = info.position;
if (!mAdapter.maxedOut()) {
pos--;
}
return pos;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// Called when our orientation changes. Tell the adapter about the new
// size. Compute the individual tab height by taking the grid height
// and subtracting the SPACING. Then subtract the list padding twice
// (once for each tab on screen) and divide the remaining height by 2.
int tabHeight = (h - SPACING
- 2 * (getListPaddingTop() + getListPaddingBottom())) / 2;
mAdapter.heightChanged(tabHeight);
super.onSizeChanged(w, h, oldw, oldh);
}
/**
* Listener to be notified by behavior of ImageGrid.
*/
public interface Listener {
/**
* Called when enter is pressed on the list.
* @param position The index of the selected image when
* enter is pressed.
*/
void onClick(int position);
/**
* Called when remove is called on the grid.
*/
void remove(int position);
}
}
|