2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.browser;
19 import android.app.AlertDialog;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.content.res.Configuration;
23 import android.content.res.Resources;
24 import android.database.DataSetObserver;
25 import android.graphics.Color;
26 import android.view.KeyEvent;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.view.LayoutInflater;
30 import android.webkit.WebView;
31 import android.widget.ImageView;
32 import android.widget.ListAdapter;
33 import android.widget.TextView;
35 import java.util.ArrayList;
38 * Adapter used by ImageGrid.
40 public class ImageAdapter implements ListAdapter {
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;
50 ImageAdapter(Context context, ImageGrid grid, boolean live) {
53 mItems = new ArrayList<TabControl.Tab>();
55 mDataObservers = new ArrayList<DataSetObserver>();
58 void heightChanged(int newHeight) {
59 mTabHeight = newHeight;
63 * Whether the adapter is at its limit, determined by TabControl.MAX_TABS
65 * @return True if the number of Tabs represented in this Adapter is at its
68 public boolean maxedOut() {
73 * Clear the internal WebViews and remove their picture listeners.
76 for (TabControl.Tab t : mItems) {
77 clearPictureListeners(t);
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);
93 * Add a new window web page to the grid
95 * @param t The tab to display
97 public void add(TabControl.Tab t) {
103 if (mItems.size() == TabControl.MAX_TABS) {
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
112 * @param index window to remove
114 public void remove(int index) {
115 if (index >= 0 && index < mItems.size()) {
116 clearPictureListeners(mItems.remove(index));
123 * @see android.widget.ListAdapter#areAllItemsSelectable()
125 public boolean areAllItemsEnabled() {
130 * @see android.widget.ListAdapter#isSelectable(int)
132 public boolean isEnabled(int position) {
133 if (position >= 0 && position <= mItems.size()) {
140 * @see android.widget.Adapter#getCount()
142 public int getCount() {
143 // Include the New Window button if we have not reached the tab limit
145 return mItems.size()+1;
147 return mItems.size();
151 * @see android.widget.Adapter#getItem(int)
153 public Object getItem(int position) {
158 return mItems.get(position);
160 return mItems.get(position);
164 * @see android.widget.Adapter#getItemId(int)
166 public long getItemId(int position) {
171 * @see android.widget.Adapter#getView(int, android.view.View,
172 * android.view.ViewGroup)
174 public View getView(int position, View convertView, ViewGroup parent) {
176 if (convertView != null) {
179 LayoutInflater factory = LayoutInflater.from(mContext);
180 v = factory.inflate(R.layout.tabitem, null);
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);
186 // position needs to be in the range of Tab indices.
191 // Create the View for actual tabs
192 if (position != ImageGrid.NEW_TAB) {
193 TabControl.Tab t = mItems.get(position);
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)
199 close.setVisibility(View.GONE);
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);
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);
217 ViewGroup.LayoutParams lp = img.getLayoutParams();
218 if (lp.height != mTabHeight) {
219 lp.height = mTabHeight;
226 * Pop a confirmation dialog to the user asking if they want to close this
229 private void confirmClose(final int position) {
230 final ImageGrid.Listener l = mImageGrid.getListener();
234 DialogInterface.OnClickListener confirm =
235 new DialogInterface.OnClickListener() {
236 public void onClick(DialogInterface dialog,
241 new AlertDialog.Builder(mContext)
242 .setTitle(R.string.close)
243 .setIcon(android.R.drawable.ic_dialog_alert)
244 .setMessage(R.string.close_window)
245 .setPositiveButton(R.string.ok, confirm)
246 .setNegativeButton(R.string.cancel, null)
251 * @see android.widget.Adapter#registerDataSetObserver(android.database.DataSetObserver)
253 public void registerDataSetObserver(DataSetObserver observer) {
254 mDataObservers.add(observer);
258 * @see android.widget.Adapter#hasStableIds()
260 public boolean hasStableIds() {
265 * @see android.widget.Adapter#unregisterDataSetObserver(android.database.DataSetObserver)
267 public void unregisterDataSetObserver(DataSetObserver observer) {
268 mDataObservers.remove(observer);
272 * Notify all the observers that a change has happened.
274 void notifyObservers() {
275 for (DataSetObserver observer : mDataObservers) {
276 observer.onChanged();
280 public int getItemViewType(int position) {
284 public int getViewTypeCount() {
288 public boolean isEmpty() {
289 return getCount() == 0;