The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame^] | 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.webkit.WebView; |
| 31 | import android.widget.ImageView; |
| 32 | import android.widget.ListAdapter; |
| 33 | import android.widget.TextView; |
| 34 | |
| 35 | import java.util.ArrayList; |
| 36 | |
| 37 | /** |
| 38 | * Adapter used by ImageGrid. |
| 39 | */ |
| 40 | public class ImageAdapter implements ListAdapter { |
| 41 | |
| 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; |
| 49 | |
| 50 | ImageAdapter(Context context, ImageGrid grid, boolean live) { |
| 51 | mContext = context; |
| 52 | mIsLive = live; |
| 53 | mItems = new ArrayList<TabControl.Tab>(); |
| 54 | mImageGrid = grid; |
| 55 | mDataObservers = new ArrayList<DataSetObserver>(); |
| 56 | } |
| 57 | |
| 58 | void heightChanged(int newHeight) { |
| 59 | mTabHeight = newHeight; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Whether the adapter is at its limit, determined by TabControl.MAX_TABS |
| 64 | * |
| 65 | * @return True if the number of Tabs represented in this Adapter is at its |
| 66 | * maximum. |
| 67 | */ |
| 68 | public boolean maxedOut() { |
| 69 | return mMaxedOut; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Clear the internal WebViews and remove their picture listeners. |
| 74 | */ |
| 75 | public void clear() { |
| 76 | for (TabControl.Tab t : mItems) { |
| 77 | clearPictureListeners(t); |
| 78 | } |
| 79 | mItems.clear(); |
| 80 | notifyObservers(); |
| 81 | } |
| 82 | |
| 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); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Add a new window web page to the grid |
| 94 | * |
| 95 | * @param t The tab to display |
| 96 | */ |
| 97 | public void add(TabControl.Tab t) { |
| 98 | if (mMaxedOut) { |
| 99 | return; |
| 100 | } |
| 101 | mItems.add(t); |
| 102 | notifyObservers(); |
| 103 | if (mItems.size() == TabControl.MAX_TABS) { |
| 104 | mMaxedOut = true; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 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 |
| 111 | * |
| 112 | * @param index window to remove |
| 113 | */ |
| 114 | public void remove(int index) { |
| 115 | if (index >= 0 && index < mItems.size()) { |
| 116 | clearPictureListeners(mItems.remove(index)); |
| 117 | notifyObservers(); |
| 118 | mMaxedOut = false; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /* (non-Javadoc) |
| 123 | * @see android.widget.ListAdapter#areAllItemsSelectable() |
| 124 | */ |
| 125 | public boolean areAllItemsEnabled() { |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | /* (non-Javadoc) |
| 130 | * @see android.widget.ListAdapter#isSelectable(int) |
| 131 | */ |
| 132 | public boolean isEnabled(int position) { |
| 133 | if (position >= 0 && position <= mItems.size()) { |
| 134 | return true; |
| 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | /* (non-Javadoc) |
| 140 | * @see android.widget.Adapter#getCount() |
| 141 | */ |
| 142 | public int getCount() { |
| 143 | // Include the New Window button if we have not reached the tab limit |
| 144 | if (!mMaxedOut) { |
| 145 | return mItems.size()+1; |
| 146 | } |
| 147 | return mItems.size(); |
| 148 | } |
| 149 | |
| 150 | /* (non-Javadoc) |
| 151 | * @see android.widget.Adapter#getItem(int) |
| 152 | */ |
| 153 | public Object getItem(int position) { |
| 154 | if (!mMaxedOut) { |
| 155 | if (0 == position) { |
| 156 | return null; |
| 157 | } |
| 158 | return mItems.get(position); |
| 159 | } |
| 160 | return mItems.get(position); |
| 161 | } |
| 162 | |
| 163 | /* (non-Javadoc) |
| 164 | * @see android.widget.Adapter#getItemId(int) |
| 165 | */ |
| 166 | public long getItemId(int position) { |
| 167 | return position; |
| 168 | } |
| 169 | |
| 170 | /* (non-Javadoc) |
| 171 | * @see android.widget.Adapter#getView(int, android.view.View, |
| 172 | * android.view.ViewGroup) |
| 173 | */ |
| 174 | public View getView(int position, View convertView, ViewGroup parent) { |
| 175 | View v = null; |
| 176 | if (convertView != null) { |
| 177 | v = convertView; |
| 178 | } else { |
| 179 | LayoutInflater factory = LayoutInflater.from(mContext); |
| 180 | v = factory.inflate(R.layout.tabitem, null); |
| 181 | } |
| 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); |
| 185 | |
| 186 | // position needs to be in the range of Tab indices. |
| 187 | if (!mMaxedOut) { |
| 188 | position--; |
| 189 | } |
| 190 | |
| 191 | // Create the View for actual tabs |
| 192 | if (position != ImageGrid.NEW_TAB) { |
| 193 | TabControl.Tab t = mItems.get(position); |
| 194 | img.setTab(t); |
| 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) |
| 198 | if (!mIsLive) { |
| 199 | close.setVisibility(View.GONE); |
| 200 | } else { |
| 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); |
| 206 | } |
| 207 | }); |
| 208 | } |
| 209 | } else { |
| 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); |
| 216 | } |
| 217 | ViewGroup.LayoutParams lp = img.getLayoutParams(); |
| 218 | if (lp.height != mTabHeight) { |
| 219 | lp.height = mTabHeight; |
| 220 | img.requestLayout(); |
| 221 | } |
| 222 | return v; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Pop a confirmation dialog to the user asking if they want to close this |
| 227 | * tab. |
| 228 | */ |
| 229 | private void confirmClose(final int position) { |
| 230 | final ImageGrid.Listener l = mImageGrid.getListener(); |
| 231 | if (l == null) { |
| 232 | return; |
| 233 | } |
| 234 | DialogInterface.OnClickListener confirm = |
| 235 | new DialogInterface.OnClickListener() { |
| 236 | public void onClick(DialogInterface dialog, |
| 237 | int whichButton) { |
| 238 | l.remove(position); |
| 239 | } |
| 240 | }; |
| 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) |
| 247 | .show(); |
| 248 | } |
| 249 | |
| 250 | /* (non-Javadoc) |
| 251 | * @see android.widget.Adapter#registerDataSetObserver(android.database.DataSetObserver) |
| 252 | */ |
| 253 | public void registerDataSetObserver(DataSetObserver observer) { |
| 254 | mDataObservers.add(observer); |
| 255 | } |
| 256 | |
| 257 | /* (non-Javadoc) |
| 258 | * @see android.widget.Adapter#hasStableIds() |
| 259 | */ |
| 260 | public boolean hasStableIds() { |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | /* (non-Javadoc) |
| 265 | * @see android.widget.Adapter#unregisterDataSetObserver(android.database.DataSetObserver) |
| 266 | */ |
| 267 | public void unregisterDataSetObserver(DataSetObserver observer) { |
| 268 | mDataObservers.remove(observer); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Notify all the observers that a change has happened. |
| 273 | */ |
| 274 | void notifyObservers() { |
| 275 | for (DataSetObserver observer : mDataObservers) { |
| 276 | observer.onChanged(); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | public int getItemViewType(int position) { |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | public int getViewTypeCount() { |
| 285 | return 1; |
| 286 | } |
| 287 | |
| 288 | public boolean isEmpty() { |
| 289 | return getCount() == 0; |
| 290 | } |
| 291 | } |