blob: e9571433b9db0628fcb11b7e109907b64d3fba34 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
19import android.app.AlertDialog;
20import android.content.Context;
21import android.content.DialogInterface;
22import android.content.res.Configuration;
23import android.content.res.Resources;
24import android.database.DataSetObserver;
25import android.graphics.Color;
26import android.view.KeyEvent;
27import android.view.View;
28import android.view.ViewGroup;
29import android.view.LayoutInflater;
30import android.webkit.WebView;
31import android.widget.ImageView;
32import android.widget.ListAdapter;
33import android.widget.TextView;
34
35import java.util.ArrayList;
36
37/**
38 * Adapter used by ImageGrid.
39 */
40public class ImageAdapter implements ListAdapter {
41
42 ArrayList<TabControl.Tab> mItems; // Items shown in the grid
43 private ArrayList<DataSetObserver> mDataObservers; // Data change listeners
44 private Context mContext; // Context to use to inflate views
45 private boolean mMaxedOut;
46 private ImageGrid mImageGrid;
47 private boolean mIsLive;
48 private int mTabHeight;
49
50 ImageAdapter(Context context, ImageGrid grid, boolean live) {
51 mContext = context;
52 mIsLive = live;
53 mItems = new ArrayList<TabControl.Tab>();
54 mImageGrid = grid;
55 mDataObservers = new ArrayList<DataSetObserver>();
56 }
57
58 void heightChanged(int newHeight) {
59 mTabHeight = newHeight;
60 }
61
62 /**
63 * Whether the adapter is at its limit, determined by TabControl.MAX_TABS
64 *
65 * @return True if the number of Tabs represented in this Adapter is at its
66 * maximum.
67 */
68 public boolean maxedOut() {
69 return mMaxedOut;
70 }
71
72 /**
73 * Clear the internal WebViews and remove their picture listeners.
74 */
75 public void clear() {
76 for (TabControl.Tab t : mItems) {
77 clearPictureListeners(t);
78 }
79 mItems.clear();
80 notifyObservers();
81 }
82
83 private void clearPictureListeners(TabControl.Tab t) {
84 if (t.getWebView() != null) {
85 t.getWebView().setPictureListener(null);
86 if (t.getSubWebView() != null) {
87 t.getSubWebView().setPictureListener(null);
88 }
89 }
90 }
91
92 /**
93 * Add a new window web page to the grid
94 *
95 * @param t The tab to display
96 */
97 public void add(TabControl.Tab t) {
98 if (mMaxedOut) {
99 return;
100 }
101 mItems.add(t);
102 notifyObservers();
103 if (mItems.size() == TabControl.MAX_TABS) {
104 mMaxedOut = true;
105 }
106 }
107
108 /**
109 * Remove a window from the list. At this point, the window
110 * has already gone. It just needs to be removed from the screen
111 *
112 * @param index window to remove
113 */
114 public void remove(int index) {
115 if (index >= 0 && index < mItems.size()) {
116 clearPictureListeners(mItems.remove(index));
117 notifyObservers();
118 mMaxedOut = false;
119 }
120 }
121
122 /* (non-Javadoc)
123 * @see android.widget.ListAdapter#areAllItemsSelectable()
124 */
125 public boolean areAllItemsEnabled() {
126 return true;
127 }
128
129 /* (non-Javadoc)
130 * @see android.widget.ListAdapter#isSelectable(int)
131 */
132 public boolean isEnabled(int position) {
133 if (position >= 0 && position <= mItems.size()) {
134 return true;
135 }
136 return false;
137 }
138
139 /* (non-Javadoc)
140 * @see android.widget.Adapter#getCount()
141 */
142 public int getCount() {
143 // Include the New Window button if we have not reached the tab limit
144 if (!mMaxedOut) {
145 return mItems.size()+1;
146 }
147 return mItems.size();
148 }
149
150 /* (non-Javadoc)
151 * @see android.widget.Adapter#getItem(int)
152 */
153 public Object getItem(int position) {
154 if (!mMaxedOut) {
155 if (0 == position) {
156 return null;
157 }
158 return mItems.get(position);
159 }
160 return mItems.get(position);
161 }
162
163 /* (non-Javadoc)
164 * @see android.widget.Adapter#getItemId(int)
165 */
166 public long getItemId(int position) {
167 return position;
168 }
169
170 /* (non-Javadoc)
171 * @see android.widget.Adapter#getView(int, android.view.View,
172 * android.view.ViewGroup)
173 */
174 public View getView(int position, View convertView, ViewGroup parent) {
175 View v = null;
176 if (convertView != null) {
177 v = convertView;
178 } else {
179 LayoutInflater factory = LayoutInflater.from(mContext);
180 v = factory.inflate(R.layout.tabitem, null);
181 }
182 FakeWebView img = (FakeWebView) v.findViewById(R.id.icon);
183 ImageView close = (ImageView) v.findViewById(R.id.close);
184 TextView tv = (TextView) v.findViewById(R.id.label);
185
186 // position needs to be in the range of Tab indices.
187 if (!mMaxedOut) {
188 position--;
189 }
190
191 // Create the View for actual tabs
192 if (position != ImageGrid.NEW_TAB) {
193 TabControl.Tab t = mItems.get(position);
194 img.setTab(t);
195 tv.setText(t.getTitle());
196 // Do not put the 'X' if the tab picker isn't "live" (meaning the
197 // user cannot click on a tab)
198 if (!mIsLive) {
199 close.setVisibility(View.GONE);
200 } else {
201 close.setVisibility(View.VISIBLE);
202 final int pos = position;
203 close.setOnClickListener(new View.OnClickListener() {
204 public void onClick(View v) {
205 ImageAdapter.this.confirmClose(pos);
206 }
207 });
208 }
209 } else {
210 img.setBackgroundColor(Color.BLACK);
211 img.setImageResource(R.drawable.ic_new_window);
212 img.setScaleType(ImageView.ScaleType.CENTER);
213 img.setPadding(0, 0, 0, 34);
214 tv.setText(R.string.new_window);
215 close.setVisibility(View.GONE);
216 }
217 ViewGroup.LayoutParams lp = img.getLayoutParams();
218 if (lp.height != mTabHeight) {
219 lp.height = mTabHeight;
220 img.requestLayout();
221 }
222 return v;
223 }
224
225 /*
226 * Pop a confirmation dialog to the user asking if they want to close this
227 * tab.
228 */
229 private void confirmClose(final int position) {
230 final ImageGrid.Listener l = mImageGrid.getListener();
231 if (l == null) {
232 return;
233 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700234 l.remove(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800235 }
236
237 /* (non-Javadoc)
238 * @see android.widget.Adapter#registerDataSetObserver(android.database.DataSetObserver)
239 */
240 public void registerDataSetObserver(DataSetObserver observer) {
241 mDataObservers.add(observer);
242 }
243
244 /* (non-Javadoc)
245 * @see android.widget.Adapter#hasStableIds()
246 */
247 public boolean hasStableIds() {
248 return true;
249 }
250
251 /* (non-Javadoc)
252 * @see android.widget.Adapter#unregisterDataSetObserver(android.database.DataSetObserver)
253 */
254 public void unregisterDataSetObserver(DataSetObserver observer) {
255 mDataObservers.remove(observer);
256 }
257
258 /**
259 * Notify all the observers that a change has happened.
260 */
261 void notifyObservers() {
262 for (DataSetObserver observer : mDataObservers) {
263 observer.onChanged();
264 }
265 }
266
267 public int getItemViewType(int position) {
268 return 0;
269 }
270
271 public int getViewTypeCount() {
272 return 1;
273 }
274
275 public boolean isEmpty() {
276 return getCount() == 0;
277 }
278}