blob: b4c1209db551062b297ea5530edce8f44350205a [file] [log] [blame]
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -07001/*
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;
The Android Open Source Projected217d92008-12-17 18:05:52 -080020import android.content.Context;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070021import android.content.DialogInterface;
The Android Open Source Projected217d92008-12-17 18:05:52 -080022import android.content.res.Configuration;
23import android.content.res.Resources;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070024import android.database.DataSetObserver;
25import android.graphics.Color;
26import android.view.KeyEvent;
27import android.view.View;
28import android.view.ViewGroup;
29import android.view.LayoutInflater;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070030import android.webkit.WebView;
The Android Open Source Projected217d92008-12-17 18:05:52 -080031import android.widget.ImageView;
32import android.widget.ListAdapter;
33import android.widget.TextView;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070034
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
The Android Open Source Projected217d92008-12-17 18:05:52 -080043 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;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070049
The Android Open Source Projected217d92008-12-17 18:05:52 -080050 ImageAdapter(Context context, ImageGrid grid, boolean live) {
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070051 mContext = context;
52 mIsLive = live;
The Android Open Source Projected217d92008-12-17 18:05:52 -080053 mItems = new ArrayList<TabControl.Tab>();
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070054 mImageGrid = grid;
55 mDataObservers = new ArrayList<DataSetObserver>();
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070056 }
The Android Open Source Projected217d92008-12-17 18:05:52 -080057
58 void heightChanged(int newHeight) {
59 mTabHeight = newHeight;
60 }
61
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070062 /**
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());
The Android Open Source Projected217d92008-12-17 18:05:52 -0800196 // Do not put the 'X' if the tab picker isn't "live" (meaning the
197 // user cannot click on a tab)
198 if (!mIsLive) {
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700199 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 }
The Android Open Source Projected217d92008-12-17 18:05:52 -0800217 ViewGroup.LayoutParams lp = img.getLayoutParams();
218 if (lp.height != mTabHeight) {
219 lp.height = mTabHeight;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700220 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 }
234 DialogInterface.OnClickListener confirm =
235 new DialogInterface.OnClickListener() {
236 public void onClick(DialogInterface dialog,
237 int whichButton) {
238 l.remove(position);
239 }
240 };
241 new AlertDialog.Builder(mContext)
242 .setTitle(R.string.close)
The Android Open Source Projected217d92008-12-17 18:05:52 -0800243 .setIcon(android.R.drawable.ic_dialog_alert)
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700244 .setMessage(R.string.close_window)
245 .setPositiveButton(R.string.ok, confirm)
246 .setNegativeButton(R.string.cancel, null)
247 .show();
248 }
249
250 /* (non-Javadoc)
251 * @see android.widget.Adapter#registerDataSetObserver(android.database.DataSetObserver)
252 */
253 public void registerDataSetObserver(DataSetObserver observer) {
254 mDataObservers.add(observer);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700255 }
256
257 /* (non-Javadoc)
258 * @see android.widget.Adapter#hasStableIds()
259 */
260 public boolean hasStableIds() {
261 return true;
262 }
263
264 /* (non-Javadoc)
265 * @see android.widget.Adapter#unregisterDataSetObserver(android.database.DataSetObserver)
266 */
267 public void unregisterDataSetObserver(DataSetObserver observer) {
268 mDataObservers.remove(observer);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700269 }
The Android Open Source Projected217d92008-12-17 18:05:52 -0800270
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700271 /**
272 * Notify all the observers that a change has happened.
273 */
274 void notifyObservers() {
275 for (DataSetObserver observer : mDataObservers) {
276 observer.onChanged();
277 }
278 }
279
280 public int getItemViewType(int position) {
281 return 0;
282 }
283
284 public int getViewTypeCount() {
285 return 1;
286 }
287
288 public boolean isEmpty() {
289 return getCount() == 0;
290 }
291}