• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.browser;
18 
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;
33 
34 import java.util.ArrayList;
35 
36 /**
37  * Adapter used by ImageGrid.
38  */
39 public 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 
ImageAdapter(Context context, ImageGrid grid, boolean live)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 
heightChanged(int newHeight)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      */
maxedOut()67     public boolean maxedOut() {
68         return mMaxedOut;
69     }
70 
71     /**
72      * Clear the internal WebViews and remove their picture listeners.
73      */
clear()74     public void clear() {
75         mItems.clear();
76         notifyObservers();
77     }
78 
79     /**
80      * Add a new window web page to the grid
81      *
82      * @param t The tab to display
83      */
add(TabControl.Tab t)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      */
remove(int index)101     public void remove(int index) {
102         if (index >= 0 && index < mItems.size()) {
103             mItems.remove(index);
104             notifyObservers();
105             mMaxedOut = false;
106         }
107     }
108 
109     /* (non-Javadoc)
110      * @see android.widget.ListAdapter#areAllItemsSelectable()
111      */
areAllItemsEnabled()112     public boolean areAllItemsEnabled() {
113         return true;
114     }
115 
116     /* (non-Javadoc)
117      * @see android.widget.ListAdapter#isSelectable(int)
118      */
isEnabled(int position)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      */
getCount()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      */
getItem(int position)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      */
getItemId(int position)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      */
getView(int position, View convertView, ViewGroup parent)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      */
confirmClose(final int position)216     private void confirmClose(final int position) {
217         final ImageGrid.Listener l = mImageGrid.getListener();
218         if (l == null) {
219             return;
220         }
221         l.remove(position);
222     }
223 
224     /* (non-Javadoc)
225      * @see android.widget.Adapter#registerDataSetObserver(android.database.DataSetObserver)
226      */
registerDataSetObserver(DataSetObserver observer)227     public void registerDataSetObserver(DataSetObserver observer) {
228         mDataObservers.add(observer);
229     }
230 
231     /* (non-Javadoc)
232      * @see android.widget.Adapter#hasStableIds()
233      */
hasStableIds()234     public boolean hasStableIds() {
235         return true;
236     }
237 
238     /* (non-Javadoc)
239      * @see android.widget.Adapter#unregisterDataSetObserver(android.database.DataSetObserver)
240      */
unregisterDataSetObserver(DataSetObserver observer)241     public void unregisterDataSetObserver(DataSetObserver observer) {
242         mDataObservers.remove(observer);
243     }
244 
245     /**
246      * Notify all the observers that a change has happened.
247      */
notifyObservers()248     void notifyObservers() {
249         for (DataSetObserver observer : mDataObservers) {
250             observer.onChanged();
251         }
252     }
253 
getItemViewType(int position)254     public int getItemViewType(int position) {
255         return 0;
256     }
257 
getViewTypeCount()258     public int getViewTypeCount() {
259         return 1;
260     }
261 
isEmpty()262     public boolean isEmpty() {
263         return getCount() == 0;
264     }
265 }
266