blob: afd4ea82799c32a68ae23dad2b690ebe1b3c6b1f [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 }
135
136 /**
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();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800170 return t;
171 }
172
173 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700174 * Create a new tab with default values for closeOnExit(false),
175 * appId(null), and url(null).
176 */
177 Tab createNewTab() {
178 return createNewTab(false, null, null);
179 }
180
181 /**
Leon Scrogginsfde97462010-01-11 13:06:21 -0500182 * Remove the parent child relationships from all tabs.
183 */
184 void removeParentChildRelationShips() {
185 for (Tab tab : mTabs) {
186 tab.removeFromTree();
187 }
188 }
189
190 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800191 * Remove the tab from the list. If the tab is the current tab shown, the
192 * last created tab will be shown.
193 * @param t The tab to be removed.
194 */
195 boolean removeTab(Tab t) {
196 if (t == null) {
197 return false;
198 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700199
Patrick Scottd944d4d2010-01-27 16:39:11 -0500200 // Grab the current tab before modifying the list.
201 Tab current = getCurrentTab();
202
203 // Remove t from our list of tabs.
204 mTabs.remove(t);
205
206 // Put the tab in the background only if it is the current one.
207 if (current == t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700208 t.putInBackground();
209 mCurrentTab = -1;
Patrick Scottd944d4d2010-01-27 16:39:11 -0500210 } else {
211 // If a tab that is earlier in the list gets removed, the current
212 // index no longer points to the correct tab.
213 mCurrentTab = getTabIndex(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800214 }
215
Grace Kloba22ac16e2009-10-07 18:00:23 -0700216 // destroy the tab
217 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800218 // clear it's references to parent and children
219 t.removeFromTree();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800220
221 // The tab indices have shifted, update all the saved state so we point
222 // to the correct index.
223 for (Tab tab : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700224 Vector<Tab> children = tab.getChildTabs();
225 if (children != null) {
226 for (Tab child : children) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800227 child.setParentTab(tab);
228 }
229 }
230 }
231
The Android Open Source Project0c908882009-03-03 19:32:16 -0800232 // Remove it from the queue of viewed tabs.
233 mTabQueue.remove(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800234 return true;
235 }
236
237 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800238 * Destroy all the tabs and subwindows
239 */
240 void destroy() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800241 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700242 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800243 }
244 mTabs.clear();
245 mTabQueue.clear();
246 }
247
248 /**
249 * Returns the number of tabs created.
250 * @return The number of tabs created.
251 */
252 int getTabCount() {
253 return mTabs.size();
254 }
255
The Android Open Source Project0c908882009-03-03 19:32:16 -0800256
257 /**
258 * Save the state of all the Tabs.
259 * @param outState The Bundle to save the state to.
260 */
261 void saveState(Bundle outState) {
262 final int numTabs = getTabCount();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700263 outState.putInt(Tab.NUMTABS, numTabs);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800264 final int index = getCurrentIndex();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700265 outState.putInt(Tab.CURRTAB, (index >= 0 && index < numTabs) ? index : 0);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800266 for (int i = 0; i < numTabs; i++) {
267 final Tab t = getTab(i);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700268 if (t.saveState()) {
269 outState.putBundle(Tab.WEBVIEW + i, t.getSavedState());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800270 }
271 }
272 }
273
274 /**
275 * Restore the state of all the tabs.
276 * @param inState The saved state of all the tabs.
277 * @return True if there were previous tabs that were restored. False if
278 * there was no saved state or restoring the state failed.
279 */
280 boolean restoreState(Bundle inState) {
281 final int numTabs = (inState == null)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700282 ? -1 : inState.getInt(Tab.NUMTABS, -1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800283 if (numTabs == -1) {
284 return false;
285 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700286 final int currentTab = inState.getInt(Tab.CURRTAB, -1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800287 for (int i = 0; i < numTabs; i++) {
288 if (i == currentTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700289 Tab t = createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800290 // Me must set the current tab before restoring the state
291 // so that all the client classes are set.
292 setCurrentTab(t);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700293 if (!t.restoreState(inState.getBundle(Tab.WEBVIEW + i))) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800294 Log.w(LOGTAG, "Fail in restoreState, load home page.");
Grace Kloba22ac16e2009-10-07 18:00:23 -0700295 t.getWebView().loadUrl(BrowserSettings.getInstance()
The Android Open Source Project0c908882009-03-03 19:32:16 -0800296 .getHomePage());
297 }
298 } else {
299 // Create a new tab and don't restore the state yet, add it
300 // to the tab list
Grace Kloba22ac16e2009-10-07 18:00:23 -0700301 Tab t = new Tab(mActivity, null, false, null, null);
302 Bundle state = inState.getBundle(Tab.WEBVIEW + i);
303 if (state != null) {
304 t.setSavedState(state);
305 t.populatePickerDataFromSavedState();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700306 // Need to maintain the app id and original url so we
307 // can possibly reuse this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700308 t.setAppId(state.getString(Tab.APPID));
309 t.setOriginalUrl(state.getString(Tab.ORIGINALURL));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800310 }
311 mTabs.add(t);
Grace Klobaf56f68d2010-04-11 22:53:42 -0700312 // added the tab to the front as they are not current
313 mTabQueue.add(0, t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800314 }
315 }
316 // Rebuild the tree of tabs. Do this after all tabs have been
317 // created/restored so that the parent tab exists.
318 for (int i = 0; i < numTabs; i++) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700319 final Bundle b = inState.getBundle(Tab.WEBVIEW + i);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800320 final Tab t = getTab(i);
321 if (b != null && t != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700322 final int parentIndex = b.getInt(Tab.PARENTTAB, -1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800323 if (parentIndex != -1) {
324 final Tab parent = getTab(parentIndex);
325 if (parent != null) {
326 parent.addChildTab(t);
327 }
328 }
329 }
330 }
331 }
332 return true;
333 }
334
335 /**
Grace Klobaf56f68d2010-04-11 22:53:42 -0700336 * Free the memory in this order, 1) free the background tabs; 2) free the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800337 * WebView cache;
338 */
339 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700340 if (getTabCount() == 0) return;
341
Grace Klobaf56f68d2010-04-11 22:53:42 -0700342 // free the least frequently used background tabs
343 Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab());
344 if (tabs.size() > 0) {
345 Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser");
346 for (Tab t : tabs) {
347 // store the WebView's state.
348 t.saveState();
349 // destroy the tab
350 t.destroy();
351 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800352 return;
353 }
354
Derek Sollenberger4433d032009-06-10 15:37:21 -0400355 // free the WebView's unused memory (this includes the cache)
356 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800357 WebView view = getCurrentWebView();
358 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400359 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800360 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800361 }
362
Grace Klobaf56f68d2010-04-11 22:53:42 -0700363 private Vector<Tab> getHalfLeastUsedTabs(Tab current) {
364 Vector<Tab> tabsToGo = new Vector<Tab>();
365
Patrick Scott2a67de42009-08-31 09:48:37 -0400366 // Don't do anything if we only have 1 tab or if the current tab is
367 // null.
368 if (getTabCount() == 1 || current == null) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700369 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800370 }
371
Grace Klobaf56f68d2010-04-11 22:53:42 -0700372 if (mTabQueue.size() == 0) {
373 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800374 }
375
Grace Klobaf56f68d2010-04-11 22:53:42 -0700376 // Rip through the queue starting at the beginning and tear down half of
377 // available tabs which are not the current tab or the parent of the
378 // current tab.
379 int openTabCount = 0;
380 for (Tab t : mTabQueue) {
381 if (t != null && t.getWebView() != null) {
382 openTabCount++;
383 if (t != current && t != current.getParentTab()) {
384 tabsToGo.add(t);
385 }
386 }
387 }
388
389 openTabCount /= 2;
390 if (tabsToGo.size() > openTabCount) {
391 tabsToGo.setSize(openTabCount);
392 }
393
394 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800395 }
396
The Android Open Source Project0c908882009-03-03 19:32:16 -0800397 /**
398 * Show the tab that contains the given WebView.
399 * @param view The WebView used to find the tab.
400 */
401 Tab getTabFromView(WebView view) {
402 final int size = getTabCount();
403 for (int i = 0; i < size; i++) {
404 final Tab t = getTab(i);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700405 if (t.getSubWebView() == view || t.getWebView() == view) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800406 return t;
407 }
408 }
409 return null;
410 }
411
412 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700413 * Return the tab with the matching application id.
414 * @param id The application identifier.
415 */
416 Tab getTabFromId(String id) {
417 if (id == null) {
418 return null;
419 }
420 final int size = getTabCount();
421 for (int i = 0; i < size; i++) {
422 final Tab t = getTab(i);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700423 if (id.equals(t.getAppId())) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700424 return t;
425 }
426 }
427 return null;
428 }
429
Grace Kloba22ac16e2009-10-07 18:00:23 -0700430 /**
431 * Stop loading in all opened WebView including subWindows.
432 */
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700433 void stopAllLoading() {
434 final int size = getTabCount();
435 for (int i = 0; i < size; i++) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700436 final Tab t = getTab(i);
437 final WebView webview = t.getWebView();
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700438 if (webview != null) {
439 webview.stopLoading();
440 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700441 final WebView subview = t.getSubWebView();
442 if (subview != null) {
443 webview.stopLoading();
444 }
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700445 }
446 }
447
Patrick Scottcd115892009-07-16 09:42:58 -0400448 // This method checks if a non-app tab (one created within the browser)
449 // matches the given url.
450 private boolean tabMatchesUrl(Tab t, String url) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700451 if (t.getAppId() != null) {
Patrick Scottcd115892009-07-16 09:42:58 -0400452 return false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700453 }
454 WebView webview = t.getWebView();
455 if (webview == null) {
Patrick Scottcd115892009-07-16 09:42:58 -0400456 return false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700457 } else if (url.equals(webview.getUrl())
458 || url.equals(webview.getOriginalUrl())) {
Patrick Scottcd115892009-07-16 09:42:58 -0400459 return true;
460 }
461 return false;
462 }
463
464 /**
465 * Return the tab that has no app id associated with it and the url of the
466 * tab matches the given url.
467 * @param url The url to search for.
468 */
469 Tab findUnusedTabWithUrl(String url) {
470 if (url == null) {
471 return null;
472 }
473 // Check the current tab first.
474 Tab t = getCurrentTab();
475 if (t != null && tabMatchesUrl(t, url)) {
476 return t;
477 }
478 // Now check all the rest.
479 final int size = getTabCount();
480 for (int i = 0; i < size; i++) {
481 t = getTab(i);
482 if (tabMatchesUrl(t, url)) {
483 return t;
484 }
485 }
486 return null;
487 }
488
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700489 /**
490 * Recreate the main WebView of the given tab. Returns true if the WebView
Leon Scroggins6eac63e2010-03-15 18:19:14 -0400491 * requires a load, whether it was due to the fact that it was deleted, or
492 * it is because it was a voice search.
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700493 */
Leon Scroggins6eac63e2010-03-15 18:19:14 -0400494 boolean recreateWebView(Tab t, BrowserActivity.UrlData urlData) {
495 final String url = urlData.mUrl;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700496 final WebView w = t.getWebView();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700497 if (w != null) {
Leon Scroggins47208682010-04-07 17:59:48 -0400498 if (url != null && url.equals(t.getOriginalUrl())
499 // Treat a voice intent as though it is a different URL,
500 // since it most likely is.
501 && urlData.mVoiceIntent == null) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700502 // The original url matches the current url. Just go back to the
503 // first history item so we can load it faster than if we
504 // rebuilt the WebView.
505 final WebBackForwardList list = w.copyBackForwardList();
506 if (list != null) {
507 w.goBackOrForward(-list.getCurrentIndex());
508 w.clearHistory(); // maintains the current page.
509 return false;
510 }
511 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700512 t.destroy();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700513 }
514 // Create a new WebView. If this tab is the current tab, we need to put
515 // back all the clients so force it to be the current tab.
Steve Block2bc69912009-07-30 14:45:13 +0100516 t.setWebView(createNewWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700517 if (getCurrentTab() == t) {
518 setCurrentTab(t, true);
519 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700520 // Clear the saved state and picker data
521 t.setSavedState(null);
522 t.clearPickerData();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700523 // Save the new url in order to avoid deleting the WebView.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700524 t.setOriginalUrl(url);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700525 return true;
526 }
527
528 /**
529 * Creates a new WebView and registers it with the global settings.
530 */
531 private WebView createNewWebView() {
532 // Create a new WebView
533 WebView w = new WebView(mActivity);
Grace Kloba67f0a562009-09-28 21:24:51 -0700534 w.setScrollbarFadingEnabled(true);
535 w.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700536 w.setMapTrackballToArrowKeys(false); // use trackball directly
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700537 // Enable the built-in zoom
538 w.getSettings().setBuiltInZoomControls(true);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700539 // Add this WebView to the settings observer list and update the
540 // settings
541 final BrowserSettings s = BrowserSettings.getInstance();
542 s.addObserver(w.getSettings()).update(s, null);
Mike Reedd5a80a52009-11-12 12:49:34 -0500543
Mike Reeda947d2d2010-01-14 14:57:45 -0800544 // pick a default
Mike Reedd5c3e0f2010-02-24 11:12:54 -0500545 if (false) {
Mike Reede5073c22010-01-29 11:31:39 -0500546 MeshTracker mt = new MeshTracker(2);
547 Paint paint = new Paint();
548 Bitmap bm = BitmapFactory.decodeResource(mActivity.getResources(),
549 R.drawable.pattern_carbon_fiber_dark);
550 paint.setShader(new BitmapShader(bm, Shader.TileMode.REPEAT,
551 Shader.TileMode.REPEAT));
552 mt.setBGPaint(paint);
553 w.setDragTracker(mt);
554 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700555 return w;
556 }
557
558 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800559 * Put the current tab in the background and set newTab as the current tab.
560 * @param newTab The new tab. If newTab is null, the current tab is not
561 * set.
562 */
563 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700564 return setCurrentTab(newTab, false);
565 }
566
Grace Kloba22ac16e2009-10-07 18:00:23 -0700567 void pauseCurrentTab() {
Mike Reed7bfa63b2009-05-28 11:08:32 -0400568 Tab t = getCurrentTab();
569 if (t != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700570 t.pause();
Mike Reed7bfa63b2009-05-28 11:08:32 -0400571 }
572 }
573
Grace Kloba22ac16e2009-10-07 18:00:23 -0700574 void resumeCurrentTab() {
Mike Reed7bfa63b2009-05-28 11:08:32 -0400575 Tab t = getCurrentTab();
576 if (t != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700577 t.resume();
Mike Reed7bfa63b2009-05-28 11:08:32 -0400578 }
579 }
580
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700581 /**
582 * If force is true, this method skips the check for newTab == current.
583 */
584 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800585 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700586 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800587 return true;
588 }
589 if (current != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700590 current.putInBackground();
591 mCurrentTab = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800592 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800593 if (newTab == null) {
594 return false;
595 }
596
597 // Move the newTab to the end of the queue
598 int index = mTabQueue.indexOf(newTab);
599 if (index != -1) {
600 mTabQueue.remove(index);
601 }
602 mTabQueue.add(newTab);
603
The Android Open Source Project0c908882009-03-03 19:32:16 -0800604 // Display the new current tab
605 mCurrentTab = mTabs.indexOf(newTab);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700606 WebView mainView = newTab.getWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800607 boolean needRestore = (mainView == null);
608 if (needRestore) {
609 // Same work as in createNewTab() except don't do new Tab()
Steve Block2bc69912009-07-30 14:45:13 +0100610 mainView = createNewWebView();
611 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800612 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700613 newTab.putInForeground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800614 if (needRestore) {
615 // Have to finish setCurrentTab work before calling restoreState
Grace Kloba22ac16e2009-10-07 18:00:23 -0700616 if (!newTab.restoreState(newTab.getSavedState())) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800617 mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
618 }
619 }
620 return true;
621 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800622}