blob: f95753a5095d1c21e9b465bbd8d674cd4dde9e01 [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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.widget.ImageView;
31import android.widget.ListAdapter;
32import android.widget.TextView;
33
34import java.util.ArrayList;
35
36/**
37 * Adapter used by ImageGrid.
38 */
39public class ImageAdapter implements ListAdapter {
40
41 ArrayList<TabControl.Tab> mItems; // Items shown in the grid
42 private ArrayList<DataSetObserver> mDataObservers; // Data change listeners
43 private Context mContext; // Context to use to inflate views
44 private boolean mMaxedOut;
45 private ImageGrid mImageGrid;
46 private boolean mIsLive;
47 private int mTabHeight;
48
49 ImageAdapter(Context context, ImageGrid grid, boolean live) {
50 mContext = context;
51 mIsLive = live;
52 mItems = new ArrayList<TabControl.Tab>();
53 mImageGrid = grid;
54 mDataObservers = new ArrayList<DataSetObserver>();
55 }
56
57 void heightChanged(int newHeight) {
58 mTabHeight = newHeight;
59 }
60
61 /**
62 * Whether the adapter is at its limit, determined by TabControl.MAX_TABS
63 *
64 * @return True if the number of Tabs represented in this Adapter is at its
65 * maximum.
66 */
67 public boolean maxedOut() {
68 return mMaxedOut;
69 }
70
71 /**
72 * Clear the internal WebViews and remove their picture listeners.
73 */
74 public void clear() {
The Android Open Source Project0c908882009-03-03 19:32:16 -080075 mItems.clear();
76 notifyObservers();
77 }
78
The Android Open Source Project0c908882009-03-03 19:32:16 -080079 /**
80 * Add a new window web page to the grid
81 *
82 * @param t The tab to display
83 */
84 public void add(TabControl.Tab t) {
85 if (mMaxedOut) {
86 return;
87 }
88 mItems.add(t);
89 notifyObservers();
90 if (mItems.size() == TabControl.MAX_TABS) {
91 mMaxedOut = true;
92 }
93 }
94
95 /**
96 * Remove a window from the list. At this point, the window
97 * has already gone. It just needs to be removed from the screen
98 *
99 * @param index window to remove
100 */
101 public void remove(int index) {
102 if (index >= 0 && index < mItems.size()) {
Patrick Scott8df19672009-04-28 08:57:56 -0400103 mItems.remove(index);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800104 notifyObservers();
105 mMaxedOut = false;
106 }
107 }
108
109 /* (non-Javadoc)
110 * @see android.widget.ListAdapter#areAllItemsSelectable()
111 */
112 public boolean areAllItemsEnabled() {
113 return true;
114 }
115
116 /* (non-Javadoc)
117 * @see android.widget.ListAdapter#isSelectable(int)
118 */
119 public boolean isEnabled(int position) {
120 if (position >= 0 && position <= mItems.size()) {
121 return true;
122 }
123 return false;
124 }
125
126 /* (non-Javadoc)
127 * @see android.widget.Adapter#getCount()
128 */
129 public int getCount() {
130 // Include the New Window button if we have not reached the tab limit
131 if (!mMaxedOut) {
132 return mItems.size()+1;
133 }
134 return mItems.size();
135 }
136
137 /* (non-Javadoc)
138 * @see android.widget.Adapter#getItem(int)
139 */
140 public Object getItem(int position) {
141 if (!mMaxedOut) {
142 if (0 == position) {
143 return null;
144 }
145 return mItems.get(position);
146 }
147 return mItems.get(position);
148 }
149
150 /* (non-Javadoc)
151 * @see android.widget.Adapter#getItemId(int)
152 */
153 public long getItemId(int position) {
154 return position;
155 }
156
157 /* (non-Javadoc)
158 * @see android.widget.Adapter#getView(int, android.view.View,
159 * android.view.ViewGroup)
160 */
161 public View getView(int position, View convertView, ViewGroup parent) {
162 View v = null;
163 if (convertView != null) {
164 v = convertView;
165 } else {
166 LayoutInflater factory = LayoutInflater.from(mContext);
167 v = factory.inflate(R.layout.tabitem, null);
168 }
169 FakeWebView img = (FakeWebView) v.findViewById(R.id.icon);
170 ImageView close = (ImageView) v.findViewById(R.id.close);
171 TextView tv = (TextView) v.findViewById(R.id.label);
172
173 // position needs to be in the range of Tab indices.
174 if (!mMaxedOut) {
175 position--;
176 }
177
178 // Create the View for actual tabs
179 if (position != ImageGrid.NEW_TAB) {
180 TabControl.Tab t = mItems.get(position);
181 img.setTab(t);
182 tv.setText(t.getTitle());
183 // Do not put the 'X' if the tab picker isn't "live" (meaning the
184 // user cannot click on a tab)
185 if (!mIsLive) {
186 close.setVisibility(View.GONE);
187 } else {
188 close.setVisibility(View.VISIBLE);
189 final int pos = position;
190 close.setOnClickListener(new View.OnClickListener() {
191 public void onClick(View v) {
192 ImageAdapter.this.confirmClose(pos);
193 }
194 });
195 }
196 } else {
197 img.setBackgroundColor(Color.BLACK);
198 img.setImageResource(R.drawable.ic_new_window);
199 img.setScaleType(ImageView.ScaleType.CENTER);
200 img.setPadding(0, 0, 0, 34);
201 tv.setText(R.string.new_window);
202 close.setVisibility(View.GONE);
203 }
204 ViewGroup.LayoutParams lp = img.getLayoutParams();
205 if (lp.height != mTabHeight) {
206 lp.height = mTabHeight;
207 img.requestLayout();
208 }
209 return v;
210 }
211
212 /*
213 * Pop a confirmation dialog to the user asking if they want to close this
214 * tab.
215 */
216 private void confirmClose(final int position) {
217 final ImageGrid.Listener l = mImageGrid.getListener();
218 if (l == null) {
219 return;
220 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700221 l.remove(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800222 }
223
224 /* (non-Javadoc)
225 * @see android.widget.Adapter#registerDataSetObserver(android.database.DataSetObserver)
226 */
227 public void registerDataSetObserver(DataSetObserver observer) {
228 mDataObservers.add(observer);
229 }
230
231 /* (non-Javadoc)
232 * @see android.widget.Adapter#hasStableIds()
233 */
234 public boolean hasStableIds() {
235 return true;
236 }
237
238 /* (non-Javadoc)
239 * @see android.widget.Adapter#unregisterDataSetObserver(android.database.DataSetObserver)
240 */
241 public void unregisterDataSetObserver(DataSetObserver observer) {
242 mDataObservers.remove(observer);
243 }
244
245 /**
246 * Notify all the observers that a change has happened.
247 */
248 void notifyObservers() {
249 for (DataSetObserver observer : mDataObservers) {
250 observer.onChanged();
251 }
252 }
253
254 public int getItemViewType(int position) {
255 return 0;
256 }
257
258 public int getViewTypeCount() {
259 return 1;
260 }
261
262 public boolean isEmpty() {
263 return getCount() == 0;
264 }
265}