OSDN Git Service

Show the saved picture for freed tabs.
[android-x86/packages-apps-Browser.git] / src / com / android / browser / ImageAdapter.java
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
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() {
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      */
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()) {
103             notifyObservers();
104             mMaxedOut = false;
105         }
106     }
107
108     /* (non-Javadoc)
109      * @see android.widget.ListAdapter#areAllItemsSelectable()
110      */
111     public boolean areAllItemsEnabled() {
112         return true;
113     }
114
115     /* (non-Javadoc)
116      * @see android.widget.ListAdapter#isSelectable(int)
117      */
118     public boolean isEnabled(int position) {
119         if (position >= 0 && position <= mItems.size()) {
120             return true;
121         }
122         return false;
123     }
124
125     /* (non-Javadoc)
126      * @see android.widget.Adapter#getCount()
127      */
128     public int getCount() {
129         // Include the New Window button if we have not reached the tab limit
130         if (!mMaxedOut) {
131             return mItems.size()+1;
132         }
133         return mItems.size();
134     }
135
136     /* (non-Javadoc)
137      * @see android.widget.Adapter#getItem(int)
138      */
139     public Object getItem(int position) {
140         if (!mMaxedOut) {
141             if (0 == position) {
142                 return null;
143             }
144             return mItems.get(position);
145         }
146         return mItems.get(position);
147     }
148
149     /* (non-Javadoc)
150      * @see android.widget.Adapter#getItemId(int)
151      */
152     public long getItemId(int position) {
153         return position;
154     }
155
156     /* (non-Javadoc)
157      * @see android.widget.Adapter#getView(int, android.view.View, 
158      * android.view.ViewGroup)
159      */
160     public View getView(int position, View convertView, ViewGroup parent) {
161         View v = null;
162         if (convertView != null) {
163             v = convertView;
164         } else {
165             LayoutInflater factory = LayoutInflater.from(mContext);
166             v = factory.inflate(R.layout.tabitem, null);
167         }
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);
171
172         // position needs to be in the range of Tab indices.
173         if (!mMaxedOut) {
174             position--;
175         }
176
177         // Create the View for actual tabs
178         if (position != ImageGrid.NEW_TAB) {
179             TabControl.Tab t = mItems.get(position);
180             img.setTab(t);
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)
184             if (!mIsLive) {
185                 close.setVisibility(View.GONE);
186             } else {
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);
192                         }
193                     });
194             }
195         } else {
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);
202         }
203         ViewGroup.LayoutParams lp = img.getLayoutParams();
204         if (lp.height != mTabHeight) {
205             lp.height = mTabHeight;
206             img.requestLayout();
207         }
208         return v;
209     }
210
211     /*
212      * Pop a confirmation dialog to the user asking if they want to close this
213      * tab.
214      */
215     private void confirmClose(final int position) {
216         final ImageGrid.Listener l = mImageGrid.getListener();
217         if (l == null) {
218             return;
219         }
220         l.remove(position);
221     }
222
223     /* (non-Javadoc)
224      * @see android.widget.Adapter#registerDataSetObserver(android.database.DataSetObserver)
225      */
226     public void registerDataSetObserver(DataSetObserver observer) {
227         mDataObservers.add(observer);
228     }
229
230     /* (non-Javadoc)
231      * @see android.widget.Adapter#hasStableIds()
232      */
233     public boolean hasStableIds() {
234         return true;
235     }
236
237     /* (non-Javadoc)
238      * @see android.widget.Adapter#unregisterDataSetObserver(android.database.DataSetObserver)
239      */
240     public void unregisterDataSetObserver(DataSetObserver observer) {
241         mDataObservers.remove(observer);
242     }
243
244     /**
245      * Notify all the observers that a change has happened.
246      */
247     void notifyObservers() {
248         for (DataSetObserver observer : mDataObservers) {
249             observer.onChanged();
250         }
251     }
252
253     public int getItemViewType(int position) {
254         return 0;
255     }
256
257     public int getViewTypeCount() {
258         return 1;
259     }
260
261     public boolean isEmpty() {
262         return getCount() == 0;
263     }
264 }