Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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.content.Context; |
| 20 | import android.database.DataSetObserver; |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.util.AttributeSet; |
| 24 | import android.util.Log; |
| 25 | import android.view.LayoutInflater; |
| 26 | import android.view.MotionEvent; |
| 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
| 29 | import android.webkit.WebView; |
| 30 | import android.widget.AdapterView; |
| 31 | import android.widget.Gallery; |
| 32 | import android.widget.SpinnerAdapter; |
| 33 | |
| 34 | import java.util.Vector; |
| 35 | |
| 36 | /** |
| 37 | * The TitleBarSet holds a TitleBar for each open "tab" in the browser. |
| 38 | */ |
| 39 | public class TitleBarSet extends Gallery |
| 40 | implements AdapterView.OnItemSelectedListener { |
| 41 | private Vector<TitleBar> mTitleBars; |
| 42 | private BrowserActivity mBrowserActivity; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 43 | private int mCount; |
| 44 | private TitleAdapter mTitleAdapter; |
| 45 | private boolean mIgnoreSelectedListener; |
| 46 | private MotionEvent mLastTouchUp; |
| 47 | |
| 48 | public TitleBarSet(Context context) { |
| 49 | this(context, null); |
| 50 | } |
| 51 | |
| 52 | public TitleBarSet(Context context, AttributeSet attrs) { |
| 53 | super(context, attrs); |
| 54 | mTitleBars = new Vector<TitleBar>(TabControl.MAX_TABS); |
| 55 | mCount = 0; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 56 | mTitleAdapter = new TitleAdapter(); |
| 57 | setAdapter(mTitleAdapter); |
| 58 | setCallbackDuringFling(false); |
| 59 | setCallbackOnUnselectedItemClick(true); |
| 60 | setSpacing(0); |
| 61 | setOnItemSelectedListener(this); |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 62 | setBackgroundResource(R.drawable.tab_browser_unselected); |
| 63 | setPadding(0,0,0,0); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Add a tab/titlebar to our set. Called when BrowserActivity adds a new |
| 68 | * Tab to its TabControl. |
| 69 | * @param view WebView associated with this tab. Used to determine whether |
| 70 | * updates are going to the correct place. |
| 71 | * @param selected Whether to set the new tab to be selected. |
| 72 | */ |
| 73 | /* package */ void addTab(WebView view, boolean selected) { |
| 74 | if (TabControl.MAX_TABS == mCount) { |
| 75 | return; |
| 76 | } |
| 77 | int newSelection = mCount; |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 78 | TitleBar titleBar = new TitleBar(mBrowserActivity, view); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 79 | mTitleBars.add(titleBar); |
| 80 | mCount++; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 81 | // Need to refresh our list |
| 82 | setAdapter(mTitleAdapter); |
| 83 | mIgnoreSelectedListener = true; |
| 84 | // No need to call onItemSelected, since the Tab in BrowserActivity has |
| 85 | // already been changed. |
| 86 | if (selected) { |
| 87 | setSelection(newSelection); |
| 88 | } |
| 89 | mIgnoreSelectedListener = false; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Convenience method to get a particular title bar. |
| 94 | */ |
| 95 | private TitleBar getTitleBarAt(int position) { |
| 96 | if (position < 0 || position >= mCount) { |
| 97 | return null; |
| 98 | } |
| 99 | return (TitleBar) mTitleBars.elementAt(position); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Implementation for OnItemSelectedListener |
| 104 | */ |
| 105 | public void onItemSelected(AdapterView<?> parent, View view, int position, |
| 106 | long id) { |
| 107 | if (mIgnoreSelectedListener || !(view instanceof TitleBar)) { |
| 108 | return; |
| 109 | } |
| 110 | mBrowserActivity.switchToTab(position); |
| 111 | // In case the WebView finished loading while this TitleBar was out of |
| 112 | // focus, make sure all its data is up to date |
| 113 | TitleBar titleBar = getTitleBarAt(position); |
| 114 | WebView webview = titleBar.getWebView(); |
| 115 | if (webview == null) { |
| 116 | // FIXME: Possible that the tab needs to be restored. |
| 117 | return; |
| 118 | } |
| 119 | if (webview.getProgress() == 100) { |
| 120 | titleBar.setProgress(100); |
| 121 | titleBar.setTitleAndUrl(webview.getTitle(), webview.getUrl()); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 122 | // FIXME: BrowserActivity looks at the back forward list. Is this |
| 123 | // better? |
| 124 | titleBar.setFavicon(webview.getFavicon()); |
| 125 | mBrowserActivity.updateLockIconToLatest(); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Implementation for OnItemSelectedListener |
| 131 | */ |
| 132 | public void onNothingSelected(AdapterView<?> parent) { |
| 133 | // do nothing |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Override from GestureDetector.OnGestureListener. Store the MotionEvent |
| 138 | * so performItemClick can know how to handle the click. |
| 139 | */ |
| 140 | public boolean onSingleTapUp(MotionEvent e) { |
| 141 | mLastTouchUp = e; |
| 142 | // super.onSingleTapUp will call performItemClick |
| 143 | boolean result = super.onSingleTapUp(e); |
| 144 | mLastTouchUp = null; |
| 145 | return result; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Override from View to ensure that the TitleBars get resized to match |
| 150 | * the new screen width |
| 151 | */ |
| 152 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 153 | super.onSizeChanged(w, h, oldw, oldh); |
Leon Scroggins | 5a7e6c6 | 2009-08-14 16:32:43 -0400 | [diff] [blame] | 154 | int selection = getSelectedItemPosition(); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 155 | // Need to make sure getView gets called again |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 156 | setAdapter(mTitleAdapter); |
Leon Scroggins | 5a7e6c6 | 2009-08-14 16:32:43 -0400 | [diff] [blame] | 157 | // Stay on the same tab |
| 158 | setCurrentTab(selection); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Override from AdapterView. Using simple OnClickListeners overrides |
| 163 | * the GestureDetector.OnGestureListener, so we handle it here. |
| 164 | */ |
| 165 | public boolean performItemClick(View view, int position, long id) { |
| 166 | if (!(view instanceof TitleBar)) { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 167 | return super.performItemClick(view, position, id); |
| 168 | } |
| 169 | // If we have no mLastTouchUp, this was not called from onSingleTapUp, |
| 170 | // so ignore it. |
| 171 | if (null == mLastTouchUp) { |
| 172 | return false; |
| 173 | } |
| 174 | TitleBar titleBar = (TitleBar) view; |
| 175 | // If the user clicks on a view which is not selected, the Gallery will |
| 176 | // take care of making it selected. |
| 177 | if (titleBar != getTitleBarAt(position)) { |
| 178 | return false; |
| 179 | } |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame^] | 180 | View textfield = titleBar.findViewById(R.id.title); |
| 181 | // Interpret all touches past the right edge of the search field as |
| 182 | // a touch on the icon on the right. |
| 183 | if ((int) mLastTouchUp.getX() > textfield.getRight()) { |
| 184 | WebView webView = titleBar.getWebView(); |
| 185 | if (webView != null && webView.getProgress() < 100) { |
| 186 | webView.stopLoading(); |
| 187 | } else { |
| 188 | mBrowserActivity.bookmarksOrHistoryPicker(false); |
| 189 | } |
| 190 | return true; |
| 191 | } |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 192 | mBrowserActivity.onSearchRequested(); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 193 | return true; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Remove the tab at the given position. |
| 198 | */ |
| 199 | /* package */ void removeTab(int position) { |
Leon Scroggins | 53fe581 | 2009-09-01 12:33:07 -0400 | [diff] [blame] | 200 | int selection = getSelectedItemPosition(); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 201 | mTitleBars.remove(position); |
| 202 | mCount--; |
| 203 | // Need to refresh our list |
| 204 | setAdapter(mTitleAdapter); |
Leon Scroggins | 53fe581 | 2009-09-01 12:33:07 -0400 | [diff] [blame] | 205 | setCurrentTab(selection); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Convenience method to get the currently selected title bar. |
| 210 | */ |
| 211 | private TitleBar selectedTitleBar() { |
| 212 | return getTitleBarAt(getSelectedItemPosition()); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Set the owning BrowserActivity. Necessary so that we can call methods |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 217 | * on it. Only called once before adding any title bars. |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 218 | */ |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 219 | /* package */ void init(final BrowserActivity ba) { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 220 | mBrowserActivity = ba; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Change to the tab at the new position. |
| 225 | */ |
| 226 | /* package */ void setCurrentTab(int position) { |
Leon Scroggins | 53fe581 | 2009-09-01 12:33:07 -0400 | [diff] [blame] | 227 | if (position < 0 || position >= mCount) return; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 228 | mIgnoreSelectedListener = true; |
| 229 | setSelection(position); |
| 230 | mIgnoreSelectedListener = false; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Update the Favicon of the currently selected tab. |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 235 | * @param icon The new bitmap for the favicon |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 236 | * @param topWindow The WebView which posted the update. If it does not |
| 237 | * match the WebView of the currently selected tab, do |
| 238 | * nothing, since that tab is not being displayed. |
| 239 | */ |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 240 | /* package */ void setFavicon(Bitmap icon, WebView topWindow) { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 241 | TitleBar current = selectedTitleBar(); |
| 242 | if (current != null && current.getWebView() == topWindow) { |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 243 | current.setFavicon(icon); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Update the lock icon of the currently selected tab. |
| 249 | * @param d The new Drawable for the lock icon |
| 250 | * @param topWindow The WebView which posted the update. If it does not |
| 251 | * match the WebView of the currently selected tab, do |
| 252 | * nothing, since that tab is not being displayed. |
| 253 | */ |
| 254 | /* package */ void setLock(Drawable d, WebView topWindow) { |
| 255 | TitleBar current = selectedTitleBar(); |
| 256 | if (current != null && current.getWebView() == topWindow) { |
| 257 | current.setLock(d); |
| 258 | } |
| 259 | } |
| 260 | /** |
| 261 | * Update the progress of the currently selected tab. |
| 262 | * @param newProgress The progress, between 0 and 100, of the current tab. |
| 263 | * @param topWindow The WebView which posted the update. If it does not |
| 264 | * match the WebView of the currently selected tab, do |
| 265 | * nothing, since that tab is not being displayed. |
| 266 | */ |
| 267 | /* package */ void setProgress(int newProgress, WebView topWindow) { |
| 268 | TitleBar current = selectedTitleBar(); |
| 269 | if (current != null && current.getWebView() == topWindow) { |
| 270 | current.setProgress(newProgress); |
| 271 | } |
| 272 | } |
| 273 | /** |
| 274 | * Update the title and URL of the currently selected tab. |
| 275 | * @param title The title of the webpage |
| 276 | * @param url The URL of the webpage |
| 277 | * @param topWindow The WebView which posted the update. If it does not |
| 278 | * match the WebView of the currently selected tab, do |
| 279 | * nothing, since that tab is not being displayed. |
| 280 | */ |
| 281 | /* package */ void setTitleAndUrl(CharSequence title, CharSequence url, |
| 282 | WebView topWindow) { |
| 283 | TitleBar current = selectedTitleBar(); |
| 284 | if (current != null && current.getWebView() == topWindow) { |
| 285 | current.setTitleAndUrl(title, url); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // FIXME: Remove |
| 290 | /* package */ void setToTabPicker() { |
| 291 | TitleBar current = selectedTitleBar(); |
| 292 | if (current != null) { |
| 293 | current.setToTabPicker(); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Custom adapter which provides the TitleBars and the NewButton to the |
| 299 | * Gallery. |
| 300 | */ |
| 301 | private class TitleAdapter implements SpinnerAdapter { |
| 302 | public View getDropDownView(int position, View convertView, |
| 303 | ViewGroup parent) { |
| 304 | return null; |
| 305 | } |
| 306 | public void registerDataSetObserver(DataSetObserver observer) {} |
| 307 | public void unregisterDataSetObserver(DataSetObserver observer) {} |
| 308 | public int getCount() { |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 309 | return mCount; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 310 | } |
| 311 | public Object getItem(int position) { |
| 312 | return null; |
| 313 | } |
| 314 | public long getItemId(int position) { |
| 315 | return position; |
| 316 | } |
| 317 | public boolean hasStableIds() { |
| 318 | return true; |
| 319 | } |
| 320 | public View getView(int position, View convertView, ViewGroup parent) { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 321 | TitleBar titleBar = getTitleBarAt(position); |
| 322 | Gallery.LayoutParams lp; |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 323 | int desiredWidth = TitleBarSet.this.getWidth(); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 324 | ViewGroup.LayoutParams old = titleBar.getLayoutParams(); |
| 325 | if (old == null || !(old instanceof Gallery.LayoutParams)) { |
| 326 | lp = new Gallery.LayoutParams(desiredWidth, |
| 327 | ViewGroup.LayoutParams.WRAP_CONTENT); |
| 328 | titleBar.setLayoutParams(lp); |
| 329 | } else { |
| 330 | lp = (Gallery.LayoutParams) old; |
| 331 | if (lp.width != desiredWidth) { |
| 332 | lp.width = desiredWidth; |
| 333 | titleBar.setLayoutParams(lp); |
| 334 | requestLayout(); |
| 335 | } |
| 336 | } |
| 337 | return titleBar; |
| 338 | } |
| 339 | public int getItemViewType(int position) { |
| 340 | // We are managing our own views. |
| 341 | return AdapterView.ITEM_VIEW_TYPE_IGNORE; |
| 342 | } |
| 343 | public int getViewTypeCount() { |
| 344 | return 1; |
| 345 | } |
| 346 | public boolean isEmpty() { |
| 347 | // Will never be empty, because the NewButton is always there |
| 348 | // (though sometimes disabled). |
| 349 | return false; |
| 350 | } |
| 351 | } |
| 352 | } |