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.widget.ImageView;
31 import android.widget.ListAdapter;
32 import android.widget.TextView;
34 import java.util.ArrayList;
37 * Adapter used by ImageGrid.
39 public class ImageAdapter implements ListAdapter {
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;
49 ImageAdapter(Context context, ImageGrid grid, boolean live) {
52 mItems = new ArrayList<TabControl.Tab>();
54 mDataObservers = new ArrayList<DataSetObserver>();
57 void heightChanged(int newHeight) {
58 mTabHeight = newHeight;
62 * Whether the adapter is at its limit, determined by TabControl.MAX_TABS
64 * @return True if the number of Tabs represented in this Adapter is at its
67 public boolean maxedOut() {
72 * Clear the internal WebViews and remove their picture listeners.
80 * Add a new window web page to the grid
82 * @param t The tab to display
84 public void add(TabControl.Tab t) {
90 if (mItems.size() == TabControl.MAX_TABS) {
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
99 * @param index window to remove
101 public void remove(int index) {
102 if (index >= 0 && index < mItems.size()) {
109 * @see android.widget.ListAdapter#areAllItemsSelectable()
111 public boolean areAllItemsEnabled() {
116 * @see android.widget.ListAdapter#isSelectable(int)
118 public boolean isEnabled(int position) {
119 if (position >= 0 && position <= mItems.size()) {
126 * @see android.widget.Adapter#getCount()
128 public int getCount() {
129 // Include the New Window button if we have not reached the tab limit
131 return mItems.size()+1;
133 return mItems.size();
137 * @see android.widget.Adapter#getItem(int)
139 public Object getItem(int position) {
144 return mItems.get(position);
146 return mItems.get(position);
150 * @see android.widget.Adapter#getItemId(int)
152 public long getItemId(int position) {
157 * @see android.widget.Adapter#getView(int, android.view.View,
158 * android.view.ViewGroup)
160 public View getView(int position, View convertView, ViewGroup parent) {
162 if (convertView != null) {
165 LayoutInflater factory = LayoutInflater.from(mContext);
166 v = factory.inflate(R.layout.tabitem, null);
168 FakeWebView img = (FakeWebView) v.findViewById(R.id.icon);
169 ImageView close = (ImageView) v.findViewById(R.id.close);
170 TextView tv = (TextView) v.findViewById(R.id.label);
172 // position needs to be in the range of Tab indices.
177 // Create the View for actual tabs
178 if (position != ImageGrid.NEW_TAB) {
179 TabControl.Tab t = mItems.get(position);
181 tv.setText(t.getTitle());
182 // Do not put the 'X' if the tab picker isn't "live" (meaning the
183 // user cannot click on a tab)
185 close.setVisibility(View.GONE);
187 close.setVisibility(View.VISIBLE);
188 final int pos = position;
189 close.setOnClickListener(new View.OnClickListener() {
190 public void onClick(View v) {
191 ImageAdapter.this.confirmClose(pos);
196 img.setBackgroundColor(Color.BLACK);
197 img.setImageResource(R.drawable.ic_new_window);
198 img.setScaleType(ImageView.ScaleType.CENTER);
199 img.setPadding(0, 0, 0, 34);
200 tv.setText(R.string.new_window);
201 close.setVisibility(View.GONE);
203 ViewGroup.LayoutParams lp = img.getLayoutParams();
204 if (lp.height != mTabHeight) {
205 lp.height = mTabHeight;
212 * Pop a confirmation dialog to the user asking if they want to close this
215 private void confirmClose(final int position) {
216 final ImageGrid.Listener l = mImageGrid.getListener();
224 * @see android.widget.Adapter#registerDataSetObserver(android.database.DataSetObserver)
226 public void registerDataSetObserver(DataSetObserver observer) {
227 mDataObservers.add(observer);
231 * @see android.widget.Adapter#hasStableIds()
233 public boolean hasStableIds() {
238 * @see android.widget.Adapter#unregisterDataSetObserver(android.database.DataSetObserver)
240 public void unregisterDataSetObserver(DataSetObserver observer) {
241 mDataObservers.remove(observer);
245 * Notify all the observers that a change has happened.
247 void notifyObservers() {
248 for (DataSetObserver observer : mDataObservers) {
249 observer.onChanged();
253 public int getItemViewType(int position) {
257 public int getViewTypeCount() {
261 public boolean isEmpty() {
262 return getCount() == 0;