blob: 0fbdd7ad21d8d0f61be9f429b65e8a4349743774 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2007 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
17package com.android.browser;
18
Mike Reede5073c22010-01-29 11:31:39 -050019import android.graphics.Bitmap;
20import android.graphics.BitmapFactory;
21import android.graphics.BitmapShader;
22import android.graphics.Paint;
23import android.graphics.Shader;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024import android.os.Bundle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.util.Log;
The Android Open Source Project0c908882009-03-03 19:32:16 -080026import android.view.View;
The Android Open Source Project0c908882009-03-03 19:32:16 -080027import android.webkit.WebBackForwardList;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029
30import java.io.File;
31import java.util.ArrayList;
32import java.util.Vector;
33
34class TabControl {
35 // Log Tag
36 private static final String LOGTAG = "TabControl";
37 // Maximum number of tabs.
Grace Kloba22ac16e2009-10-07 18:00:23 -070038 private static final int MAX_TABS = 8;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039 // Private array of WebViews that are used as tabs.
40 private ArrayList<Tab> mTabs = new ArrayList<Tab>(MAX_TABS);
41 // Queue of most recently viewed tabs.
42 private ArrayList<Tab> mTabQueue = new ArrayList<Tab>(MAX_TABS);
43 // Current position in mTabs.
44 private int mCurrentTab = -1;
45 // A private instance of BrowserActivity to interface with when adding and
46 // switching between tabs.
47 private final BrowserActivity mActivity;
The Android Open Source Project0c908882009-03-03 19:32:16 -080048 // Directory to store thumbnails for each WebView.
49 private final File mThumbnailDir;
50
51 /**
52 * Construct a new TabControl object that interfaces with the given
53 * BrowserActivity instance.
54 * @param activity A BrowserActivity instance that TabControl will interface
55 * with.
56 */
57 TabControl(BrowserActivity activity) {
58 mActivity = activity;
The Android Open Source Project0c908882009-03-03 19:32:16 -080059 mThumbnailDir = activity.getDir("thumbnails", 0);
60 }
61
62 File getThumbnailDir() {
63 return mThumbnailDir;
64 }
65
66 BrowserActivity getBrowserActivity() {
67 return mActivity;
68 }
69
70 /**
71 * Return the current tab's main WebView. This will always return the main
72 * WebView for a given tab and not a subwindow.
73 * @return The current tab's WebView.
74 */
75 WebView getCurrentWebView() {
76 Tab t = getTab(mCurrentTab);
77 if (t == null) {
78 return null;
79 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070080 return t.getWebView();
Ben Murdochbff2d602009-07-01 20:19:05 +010081 }
82
83 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080084 * Return the current tab's top-level WebView. This can return a subwindow
85 * if one exists.
86 * @return The top-level WebView of the current tab.
87 */
88 WebView getCurrentTopWebView() {
89 Tab t = getTab(mCurrentTab);
90 if (t == null) {
91 return null;
92 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070093 return t.getTopWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -080094 }
95
96 /**
97 * Return the current tab's subwindow if it exists.
98 * @return The subwindow of the current tab or null if it doesn't exist.
99 */
100 WebView getCurrentSubWindow() {
101 Tab t = getTab(mCurrentTab);
102 if (t == null) {
103 return null;
104 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700105 return t.getSubWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800106 }
107
108 /**
109 * Return the tab at the specified index.
110 * @return The Tab for the specified index or null if the tab does not
111 * exist.
112 */
113 Tab getTab(int index) {
114 if (index >= 0 && index < mTabs.size()) {
115 return mTabs.get(index);
116 }
117 return null;
118 }
119
120 /**
121 * Return the current tab.
122 * @return The current tab.
123 */
124 Tab getCurrentTab() {
125 return getTab(mCurrentTab);
126 }
127
128 /**
129 * Return the current tab index.
130 * @return The current tab index
131 */
132 int getCurrentIndex() {
133 return mCurrentTab;
134 }
Michael Kolbfe251992010-07-08 15:41:55 -0700135
The Android Open Source Project0c908882009-03-03 19:32:16 -0800136 /**
137 * Given a Tab, find it's index
138 * @param Tab to find
139 * @return index of Tab or -1 if not found
140 */
141 int getTabIndex(Tab tab) {
Patrick Scottae641ac2009-04-20 13:51:49 -0400142 if (tab == null) {
143 return -1;
144 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800145 return mTabs.indexOf(tab);
146 }
147
Grace Kloba22ac16e2009-10-07 18:00:23 -0700148 boolean canCreateNewTab() {
149 return MAX_TABS != mTabs.size();
150 }
151
The Android Open Source Project0c908882009-03-03 19:32:16 -0800152 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700153 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800154 * @return The newly createTab or null if we have reached the maximum
155 * number of open tabs.
156 */
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700157 Tab createNewTab(boolean closeOnExit, String appId, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800158 int size = mTabs.size();
159 // Return false if we have maxed out on tabs
160 if (MAX_TABS == size) {
161 return null;
162 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700163 final WebView w = createNewWebView();
Steve Block2bc69912009-07-30 14:45:13 +0100164
The Android Open Source Project0c908882009-03-03 19:32:16 -0800165 // Create a new tab and add it to the tab list
Grace Kloba22ac16e2009-10-07 18:00:23 -0700166 Tab t = new Tab(mActivity, w, closeOnExit, appId, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800167 mTabs.add(t);
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700168 // Initially put the tab in the background.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700169 t.putInBackground();
Michael Kolbfe251992010-07-08 15:41:55 -0700170 if (mTabChangeListener != null) {
171 mTabChangeListener.onNewTab(t);
172 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800173 return t;
174 }
175
176 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700177 * Create a new tab with default values for closeOnExit(false),
178 * appId(null), and url(null).
179 */
180 Tab createNewTab() {
181 return createNewTab(false, null, null);
182 }
183
184 /**
Leon Scrogginsfde97462010-01-11 13:06:21 -0500185 * Remove the parent child relationships from all tabs.
186 */
187 void removeParentChildRelationShips() {
188 for (Tab tab : mTabs) {
189 tab.removeFromTree();
190 }
191 }
192
193 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800194 * Remove the tab from the list. If the tab is the current tab shown, the
195 * last created tab will be shown.
196 * @param t The tab to be removed.
197 */
198 boolean removeTab(Tab t) {
199 if (t == null) {
200 return false;
201 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700202
Patrick Scottd944d4d2010-01-27 16:39:11 -0500203 // Grab the current tab before modifying the list.
204 Tab current = getCurrentTab();
205
206 // Remove t from our list of tabs.
207 mTabs.remove(t);
208
209 // Put the tab in the background only if it is the current one.
210 if (current == t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700211 t.putInBackground();
212 mCurrentTab = -1;
Patrick Scottd944d4d2010-01-27 16:39:11 -0500213 } else {
214 // If a tab that is earlier in the list gets removed, the current
215 // index no longer points to the correct tab.
216 mCurrentTab = getTabIndex(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800217 }
218
Grace Kloba22ac16e2009-10-07 18:00:23 -0700219 // destroy the tab
220 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800221 // clear it's references to parent and children
222 t.removeFromTree();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800223
224 // The tab indices have shifted, update all the saved state so we point
225 // to the correct index.
226 for (Tab tab : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700227 Vector<Tab> children = tab.getChildTabs();
228 if (children != null) {
229 for (Tab child : children) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230 child.setParentTab(tab);
231 }
232 }
233 }
234
The Android Open Source Project0c908882009-03-03 19:32:16 -0800235 // Remove it from the queue of viewed tabs.
236 mTabQueue.remove(t);
Michael Kolbfe251992010-07-08 15:41:55 -0700237 if (mTabChangeListener != null) {
238 mTabChangeListener.onRemoveTab(t);
239 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800240 return true;
241 }
242
243 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800244 * Destroy all the tabs and subwindows
245 */
246 void destroy() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800247 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700248 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800249 }
250 mTabs.clear();
251 mTabQueue.clear();
252 }
253
254 /**
255 * Returns the number of tabs created.
256 * @return The number of tabs created.
257 */
258 int getTabCount() {
259 return mTabs.size();
260 }
261
The Android Open Source Project0c908882009-03-03 19:32:16 -0800262
263 /**
264 * Save the state of all the Tabs.
265 * @param outState The Bundle to save the state to.
266 */
267 void saveState(Bundle outState) {
268 final int numTabs = getTabCount();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700269 outState.putInt(Tab.NUMTABS, numTabs);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800270 final int index = getCurrentIndex();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700271 outState.putInt(Tab.CURRTAB, (index >= 0 && index < numTabs) ? index : 0);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800272 for (int i = 0; i < numTabs; i++) {
273 final Tab t = getTab(i);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700274 if (t.saveState()) {
275 outState.putBundle(Tab.WEBVIEW + i, t.getSavedState());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800276 }
277 }
278 }
279
280 /**
281 * Restore the state of all the tabs.
282 * @param inState The saved state of all the tabs.
283 * @return True if there were previous tabs that were restored. False if
284 * there was no saved state or restoring the state failed.
285 */
286 boolean restoreState(Bundle inState) {
287 final int numTabs = (inState == null)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700288 ? -1 : inState.getInt(Tab.NUMTABS, -1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800289 if (numTabs == -1) {
290 return false;
291 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700292 final int currentTab = inState.getInt(Tab.CURRTAB, -1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800293 for (int i = 0; i < numTabs; i++) {
294 if (i == currentTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700295 Tab t = createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800296 // Me must set the current tab before restoring the state
297 // so that all the client classes are set.
298 setCurrentTab(t);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700299 if (!t.restoreState(inState.getBundle(Tab.WEBVIEW + i))) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800300 Log.w(LOGTAG, "Fail in restoreState, load home page.");
Grace Kloba22ac16e2009-10-07 18:00:23 -0700301 t.getWebView().loadUrl(BrowserSettings.getInstance()
The Android Open Source Project0c908882009-03-03 19:32:16 -0800302 .getHomePage());
303 }
304 } else {
305 // Create a new tab and don't restore the state yet, add it
306 // to the tab list
Grace Kloba22ac16e2009-10-07 18:00:23 -0700307 Tab t = new Tab(mActivity, null, false, null, null);
308 Bundle state = inState.getBundle(Tab.WEBVIEW + i);
309 if (state != null) {
310 t.setSavedState(state);
311 t.populatePickerDataFromSavedState();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700312 // Need to maintain the app id and original url so we
313 // can possibly reuse this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700314 t.setAppId(state.getString(Tab.APPID));
315 t.setOriginalUrl(state.getString(Tab.ORIGINALURL));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800316 }
317 mTabs.add(t);
Grace Klobaf56f68d2010-04-11 22:53:42 -0700318 // added the tab to the front as they are not current
319 mTabQueue.add(0, t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800320 }
321 }
322 // Rebuild the tree of tabs. Do this after all tabs have been
323 // created/restored so that the parent tab exists.
324 for (int i = 0; i < numTabs; i++) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700325 final Bundle b = inState.getBundle(Tab.WEBVIEW + i);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800326 final Tab t = getTab(i);
327 if (b != null && t != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700328 final int parentIndex = b.getInt(Tab.PARENTTAB, -1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800329 if (parentIndex != -1) {
330 final Tab parent = getTab(parentIndex);
331 if (parent != null) {
332 parent.addChildTab(t);
333 }
334 }
335 }
336 }
337 }
338 return true;
339 }
340
341 /**
Grace Klobaf56f68d2010-04-11 22:53:42 -0700342 * Free the memory in this order, 1) free the background tabs; 2) free the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800343 * WebView cache;
344 */
345 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700346 if (getTabCount() == 0) return;
347
Grace Klobaf56f68d2010-04-11 22:53:42 -0700348 // free the least frequently used background tabs
349 Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab());
350 if (tabs.size() > 0) {
351 Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser");
352 for (Tab t : tabs) {
353 // store the WebView's state.
354 t.saveState();
355 // destroy the tab
356 t.destroy();
357 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800358 return;
359 }
360
Derek Sollenberger4433d032009-06-10 15:37:21 -0400361 // free the WebView's unused memory (this includes the cache)
362 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800363 WebView view = getCurrentWebView();
364 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400365 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800366 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800367 }
368
Grace Klobaf56f68d2010-04-11 22:53:42 -0700369 private Vector<Tab> getHalfLeastUsedTabs(Tab current) {
370 Vector<Tab> tabsToGo = new Vector<Tab>();
371
Patrick Scott2a67de42009-08-31 09:48:37 -0400372 // Don't do anything if we only have 1 tab or if the current tab is
373 // null.
374 if (getTabCount() == 1 || current == null) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700375 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800376 }
377
Grace Klobaf56f68d2010-04-11 22:53:42 -0700378 if (mTabQueue.size() == 0) {
379 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800380 }
381
Grace Klobaf56f68d2010-04-11 22:53:42 -0700382 // Rip through the queue starting at the beginning and tear down half of
383 // available tabs which are not the current tab or the parent of the
384 // current tab.
385 int openTabCount = 0;
386 for (Tab t : mTabQueue) {
387 if (t != null && t.getWebView() != null) {
388 openTabCount++;
389 if (t != current && t != current.getParentTab()) {
390 tabsToGo.add(t);
391 }
392 }
393 }
394
395 openTabCount /= 2;
396 if (tabsToGo.size() > openTabCount) {
397 tabsToGo.setSize(openTabCount);
398 }
399
400 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800401 }
402
The Android Open Source Project0c908882009-03-03 19:32:16 -0800403 /**
404 * Show the tab that contains the given WebView.
405 * @param view The WebView used to find the tab.
406 */
407 Tab getTabFromView(WebView view) {
408 final int size = getTabCount();
409 for (int i = 0; i < size; i++) {
410 final Tab t = getTab(i);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700411 if (t.getSubWebView() == view || t.getWebView() == view) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800412 return t;
413 }
414 }
415 return null;
416 }
417
418 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700419 * Return the tab with the matching application id.
420 * @param id The application identifier.
421 */
422 Tab getTabFromId(String id) {
423 if (id == null) {
424 return null;
425 }
426 final int size = getTabCount();
427 for (int i = 0; i < size; i++) {
428 final Tab t = getTab(i);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700429 if (id.equals(t.getAppId())) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700430 return t;
431 }
432 }
433 return null;
434 }
435
Grace Kloba22ac16e2009-10-07 18:00:23 -0700436 /**
437 * Stop loading in all opened WebView including subWindows.
438 */
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700439 void stopAllLoading() {
440 final int size = getTabCount();
441 for (int i = 0; i < size; i++) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700442 final Tab t = getTab(i);
443 final WebView webview = t.getWebView();
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700444 if (webview != null) {
445 webview.stopLoading();
446 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700447 final WebView subview = t.getSubWebView();
448 if (subview != null) {
449 webview.stopLoading();
450 }
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700451 }
452 }
453
Patrick Scottcd115892009-07-16 09:42:58 -0400454 // This method checks if a non-app tab (one created within the browser)
455 // matches the given url.
456 private boolean tabMatchesUrl(Tab t, String url) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700457 if (t.getAppId() != null) {
Patrick Scottcd115892009-07-16 09:42:58 -0400458 return false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700459 }
460 WebView webview = t.getWebView();
461 if (webview == null) {
Patrick Scottcd115892009-07-16 09:42:58 -0400462 return false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700463 } else if (url.equals(webview.getUrl())
464 || url.equals(webview.getOriginalUrl())) {
Patrick Scottcd115892009-07-16 09:42:58 -0400465 return true;
466 }
467 return false;
468 }
469
470 /**
471 * Return the tab that has no app id associated with it and the url of the
472 * tab matches the given url.
473 * @param url The url to search for.
474 */
475 Tab findUnusedTabWithUrl(String url) {
476 if (url == null) {
477 return null;
478 }
479 // Check the current tab first.
480 Tab t = getCurrentTab();
481 if (t != null && tabMatchesUrl(t, url)) {
482 return t;
483 }
484 // Now check all the rest.
485 final int size = getTabCount();
486 for (int i = 0; i < size; i++) {
487 t = getTab(i);
488 if (tabMatchesUrl(t, url)) {
489 return t;
490 }
491 }
492 return null;
493 }
494
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700495 /**
496 * Recreate the main WebView of the given tab. Returns true if the WebView
Leon Scroggins6eac63e2010-03-15 18:19:14 -0400497 * requires a load, whether it was due to the fact that it was deleted, or
498 * it is because it was a voice search.
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700499 */
Leon Scroggins6eac63e2010-03-15 18:19:14 -0400500 boolean recreateWebView(Tab t, BrowserActivity.UrlData urlData) {
501 final String url = urlData.mUrl;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700502 final WebView w = t.getWebView();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700503 if (w != null) {
Leon Scroggins47208682010-04-07 17:59:48 -0400504 if (url != null && url.equals(t.getOriginalUrl())
505 // Treat a voice intent as though it is a different URL,
506 // since it most likely is.
507 && urlData.mVoiceIntent == null) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700508 // The original url matches the current url. Just go back to the
509 // first history item so we can load it faster than if we
510 // rebuilt the WebView.
511 final WebBackForwardList list = w.copyBackForwardList();
512 if (list != null) {
513 w.goBackOrForward(-list.getCurrentIndex());
514 w.clearHistory(); // maintains the current page.
515 return false;
516 }
517 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700518 t.destroy();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700519 }
520 // Create a new WebView. If this tab is the current tab, we need to put
521 // back all the clients so force it to be the current tab.
Steve Block2bc69912009-07-30 14:45:13 +0100522 t.setWebView(createNewWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700523 if (getCurrentTab() == t) {
524 setCurrentTab(t, true);
525 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700526 // Clear the saved state and picker data
527 t.setSavedState(null);
528 t.clearPickerData();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700529 // Save the new url in order to avoid deleting the WebView.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700530 t.setOriginalUrl(url);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700531 return true;
532 }
533
534 /**
535 * Creates a new WebView and registers it with the global settings.
536 */
537 private WebView createNewWebView() {
538 // Create a new WebView
539 WebView w = new WebView(mActivity);
Grace Kloba67f0a562009-09-28 21:24:51 -0700540 w.setScrollbarFadingEnabled(true);
541 w.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700542 w.setMapTrackballToArrowKeys(false); // use trackball directly
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700543 // Enable the built-in zoom
544 w.getSettings().setBuiltInZoomControls(true);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700545 // Add this WebView to the settings observer list and update the
546 // settings
547 final BrowserSettings s = BrowserSettings.getInstance();
548 s.addObserver(w.getSettings()).update(s, null);
Mike Reedd5a80a52009-11-12 12:49:34 -0500549
Mike Reeda947d2d2010-01-14 14:57:45 -0800550 // pick a default
Mike Reedd5c3e0f2010-02-24 11:12:54 -0500551 if (false) {
Mike Reede5073c22010-01-29 11:31:39 -0500552 MeshTracker mt = new MeshTracker(2);
553 Paint paint = new Paint();
554 Bitmap bm = BitmapFactory.decodeResource(mActivity.getResources(),
555 R.drawable.pattern_carbon_fiber_dark);
556 paint.setShader(new BitmapShader(bm, Shader.TileMode.REPEAT,
557 Shader.TileMode.REPEAT));
558 mt.setBGPaint(paint);
559 w.setDragTracker(mt);
560 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700561 return w;
562 }
563
564 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800565 * Put the current tab in the background and set newTab as the current tab.
566 * @param newTab The new tab. If newTab is null, the current tab is not
567 * set.
568 */
569 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700570 return setCurrentTab(newTab, false);
571 }
572
Grace Kloba22ac16e2009-10-07 18:00:23 -0700573 void pauseCurrentTab() {
Mike Reed7bfa63b2009-05-28 11:08:32 -0400574 Tab t = getCurrentTab();
575 if (t != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700576 t.pause();
Mike Reed7bfa63b2009-05-28 11:08:32 -0400577 }
578 }
579
Grace Kloba22ac16e2009-10-07 18:00:23 -0700580 void resumeCurrentTab() {
Mike Reed7bfa63b2009-05-28 11:08:32 -0400581 Tab t = getCurrentTab();
582 if (t != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700583 t.resume();
Mike Reed7bfa63b2009-05-28 11:08:32 -0400584 }
585 }
586
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700587 /**
588 * If force is true, this method skips the check for newTab == current.
589 */
590 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800591 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700592 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800593 return true;
594 }
595 if (current != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700596 current.putInBackground();
597 mCurrentTab = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800598 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800599 if (newTab == null) {
600 return false;
601 }
602
603 // Move the newTab to the end of the queue
604 int index = mTabQueue.indexOf(newTab);
605 if (index != -1) {
606 mTabQueue.remove(index);
607 }
608 mTabQueue.add(newTab);
609
The Android Open Source Project0c908882009-03-03 19:32:16 -0800610 // Display the new current tab
611 mCurrentTab = mTabs.indexOf(newTab);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700612 WebView mainView = newTab.getWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800613 boolean needRestore = (mainView == null);
614 if (needRestore) {
615 // Same work as in createNewTab() except don't do new Tab()
Steve Block2bc69912009-07-30 14:45:13 +0100616 mainView = createNewWebView();
617 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800618 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700619 newTab.putInForeground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800620 if (needRestore) {
621 // Have to finish setCurrentTab work before calling restoreState
Grace Kloba22ac16e2009-10-07 18:00:23 -0700622 if (!newTab.restoreState(newTab.getSavedState())) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800623 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
624 }
625 }
Michael Kolbfe251992010-07-08 15:41:55 -0700626 if (mTabChangeListener != null) {
627 mTabChangeListener.onCurrentTab(newTab);
628 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800629 return true;
630 }
Michael Kolbfe251992010-07-08 15:41:55 -0700631
632 interface TabChangeListener {
633
634 public void onNewTab(Tab tab);
635
636 public void onRemoveTab(Tab tab);
637
638 public void onCurrentTab(Tab tab);
639
640 public void onProgress(Tab tab, int progress);
641
642 public void onUrlAndTitle(Tab tab, String url, String title);
643
644 public void onFavicon(Tab tab, Bitmap favicon);
645
646 public void onPageStarted(Tab tab);
647
648 public void onPageFinished(Tab tab);
649
650 }
651
652 private TabChangeListener mTabChangeListener;
653
654 /**
655 * register the TabChangeListener with the tab control
656 * @param listener
657 */
658 void setOnTabChangeListener(TabChangeListener listener) {
659 mTabChangeListener = listener;
660 }
661
662 /**
663 * get the current TabChangeListener (used by the tabs)
664 */
665 TabChangeListener getTabChangeListener() {
666 return mTabChangeListener;
667 }
668
The Android Open Source Project0c908882009-03-03 19:32:16 -0800669}