blob: 1a6ee0da929d1e8b5ea121a0350c07c639881127 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 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
19import com.google.android.googleapps.IGoogleLoginService;
20import com.google.android.googlelogin.GoogleLoginServiceConstants;
21
22import android.app.Activity;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.app.AlertDialog;
24import android.app.ProgressDialog;
25import android.app.SearchManager;
26import android.content.ActivityNotFoundException;
27import android.content.BroadcastReceiver;
28import android.content.ComponentName;
29import android.content.ContentResolver;
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -040030import android.content.ContentUris;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.content.ContentValues;
32import android.content.Context;
33import android.content.DialogInterface;
34import android.content.Intent;
35import android.content.IntentFilter;
36import android.content.ServiceConnection;
Grace Klobab4da0ad2009-05-14 14:45:40 -070037import android.content.pm.PackageInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.content.pm.PackageManager;
39import android.content.pm.ResolveInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080040import android.content.res.Configuration;
41import android.content.res.Resources;
42import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import android.graphics.Bitmap;
Andrei Popescu540035d2009-09-18 18:59:20 +010044import android.graphics.BitmapFactory;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.graphics.Canvas;
The Android Open Source Project0c908882009-03-03 19:32:16 -080046import android.graphics.Picture;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040047import android.graphics.PixelFormat;
48import android.graphics.Rect;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050import android.net.ConnectivityManager;
Patrick Scotteb6ab2a2009-09-16 10:00:17 -040051import android.net.NetworkInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080052import android.net.Uri;
53import android.net.WebAddress;
The Android Open Source Project0c908882009-03-03 19:32:16 -080054import android.net.http.SslCertificate;
55import android.net.http.SslError;
56import android.os.AsyncTask;
57import android.os.Bundle;
58import android.os.Debug;
59import android.os.Environment;
60import android.os.Handler;
61import android.os.IBinder;
62import android.os.Message;
63import android.os.PowerManager;
64import android.os.Process;
65import android.os.RemoteException;
66import android.os.ServiceManager;
67import android.os.SystemClock;
The Android Open Source Project0c908882009-03-03 19:32:16 -080068import android.provider.Browser;
Cary Clark5e335a32009-09-22 14:53:11 -040069import android.provider.ContactsContract;
70import android.provider.ContactsContract.Intents.Insert;
The Android Open Source Project0c908882009-03-03 19:32:16 -080071import android.provider.Downloads;
72import android.provider.MediaStore;
The Android Open Source Project0c908882009-03-03 19:32:16 -080073import android.text.IClipboard;
74import android.text.TextUtils;
75import android.text.format.DateFormat;
76import android.text.util.Regex;
Leon Scrogginsb94bf272009-09-25 15:22:08 -040077import android.util.AttributeSet;
The Android Open Source Project0c908882009-03-03 19:32:16 -080078import android.util.Log;
79import android.view.ContextMenu;
80import android.view.Gravity;
81import android.view.KeyEvent;
82import android.view.LayoutInflater;
83import android.view.Menu;
84import android.view.MenuInflater;
85import android.view.MenuItem;
86import android.view.View;
87import android.view.ViewGroup;
88import android.view.Window;
89import android.view.WindowManager;
90import android.view.ContextMenu.ContextMenuInfo;
91import android.view.MenuItem.OnMenuItemClickListener;
The Android Open Source Project0c908882009-03-03 19:32:16 -080092import android.webkit.CookieManager;
93import android.webkit.CookieSyncManager;
94import android.webkit.DownloadListener;
95import android.webkit.HttpAuthHandler;
Grace Klobab4da0ad2009-05-14 14:45:40 -070096import android.webkit.PluginManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080097import android.webkit.SslErrorHandler;
98import android.webkit.URLUtil;
Leon Clarkecb6cc862009-09-29 18:35:13 +010099import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800100import android.webkit.WebChromeClient;
101import android.webkit.WebHistoryItem;
102import android.webkit.WebIconDatabase;
103import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800104import android.widget.EditText;
105import android.widget.FrameLayout;
106import android.widget.LinearLayout;
107import android.widget.TextView;
108import android.widget.Toast;
109
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400110import java.io.ByteArrayOutputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800111import java.io.File;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800112import java.net.MalformedURLException;
113import java.net.URI;
Dianne Hackborn99189432009-06-17 18:06:18 -0700114import java.net.URISyntaxException;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800115import java.net.URL;
116import java.net.URLEncoder;
117import java.text.ParseException;
118import java.util.Date;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119import java.util.HashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800120import java.util.regex.Matcher;
121import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800122
123public class BrowserActivity extends Activity
Grace Kloba5942df02009-09-18 11:48:29 -0700124 implements View.OnCreateContextMenuListener,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800125 DownloadListener {
126
Dave Bort31a6d1c2009-04-13 15:56:49 -0700127 /* Define some aliases to make these debugging flags easier to refer to.
128 * This file imports android.provider.Browser, so we can't just refer to "Browser.DEBUG".
129 */
130 private final static boolean DEBUG = com.android.browser.Browser.DEBUG;
131 private final static boolean LOGV_ENABLED = com.android.browser.Browser.LOGV_ENABLED;
132 private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
133
The Android Open Source Project0c908882009-03-03 19:32:16 -0800134 private IGoogleLoginService mGls = null;
135 private ServiceConnection mGlsConnection = null;
136
Satish Sampath565505b2009-05-29 15:37:27 +0100137 // These are single-character shortcuts for searching popular sources.
138 private static final int SHORTCUT_INVALID = 0;
139 private static final int SHORTCUT_GOOGLE_SEARCH = 1;
140 private static final int SHORTCUT_WIKIPEDIA_SEARCH = 2;
141 private static final int SHORTCUT_DICTIONARY_SEARCH = 3;
142 private static final int SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH = 4;
143
The Android Open Source Project0c908882009-03-03 19:32:16 -0800144 private void setupHomePage() {
145 final Runnable getAccount = new Runnable() {
146 public void run() {
147 // Lower priority
148 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
149 // get the default home page
150 String homepage = mSettings.getHomePage();
151
152 try {
153 if (mGls == null) return;
154
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700155 if (!homepage.startsWith("http://www.google.")) return;
156 if (homepage.indexOf('?') == -1) return;
157
The Android Open Source Project0c908882009-03-03 19:32:16 -0800158 String hostedUser = mGls.getAccount(GoogleLoginServiceConstants.PREFER_HOSTED);
159 String googleUser = mGls.getAccount(GoogleLoginServiceConstants.REQUIRE_GOOGLE);
160
161 // three cases:
162 //
163 // hostedUser == googleUser
164 // The device has only a google account
165 //
166 // hostedUser != googleUser
167 // The device has a hosted account and a google account
168 //
169 // hostedUser != null, googleUser == null
170 // The device has only a hosted account (so far)
171
172 // developers might have no accounts at all
173 if (hostedUser == null) return;
174
175 if (googleUser == null || !hostedUser.equals(googleUser)) {
176 String domain = hostedUser.substring(hostedUser.lastIndexOf('@')+1);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700177 homepage = homepage.replace("?", "/a/" + domain + "?");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178 }
179 } catch (RemoteException ignore) {
180 // Login service died; carry on
181 } catch (RuntimeException ignore) {
182 // Login service died; carry on
183 } finally {
184 finish(homepage);
185 }
186 }
187
188 private void finish(final String homepage) {
189 mHandler.post(new Runnable() {
190 public void run() {
191 mSettings.setHomePage(BrowserActivity.this, homepage);
192 resumeAfterCredentials();
193
194 // as this is running in a separate thread,
195 // BrowserActivity's onDestroy() may have been called,
196 // which also calls unbindService().
197 if (mGlsConnection != null) {
198 // we no longer need to keep GLS open
199 unbindService(mGlsConnection);
200 mGlsConnection = null;
201 }
202 } });
203 } };
204
205 final boolean[] done = { false };
206
207 // Open a connection to the Google Login Service. The first
208 // time the connection is established, set up the homepage depending on
209 // the account in a background thread.
210 mGlsConnection = new ServiceConnection() {
211 public void onServiceConnected(ComponentName className, IBinder service) {
212 mGls = IGoogleLoginService.Stub.asInterface(service);
213 if (done[0] == false) {
214 done[0] = true;
215 Thread account = new Thread(getAccount);
216 account.setName("GLSAccount");
217 account.start();
218 }
219 }
220 public void onServiceDisconnected(ComponentName className) {
221 mGls = null;
222 }
223 };
224
225 bindService(GoogleLoginServiceConstants.SERVICE_INTENT,
226 mGlsConnection, Context.BIND_AUTO_CREATE);
227 }
228
Cary Clarka9771242009-08-11 16:42:26 -0400229 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230 @Override
231 public Void doInBackground(File... files) {
232 if (files != null) {
233 for (File f : files) {
Cary Clarkd6be1752009-08-12 12:56:42 -0400234 if (!f.delete()) {
235 Log.e(LOGTAG, f.getPath() + " was not deleted");
236 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800237 }
238 }
239 return null;
240 }
241 }
242
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400243 /**
244 * This layout holds everything you see below the status bar, including the
245 * error console, the custom view container, and the webviews.
246 */
247 private FrameLayout mBrowserFrameLayout;
Leon Scroggins81db3662009-06-04 17:45:11 -0400248
Grace Kloba22ac16e2009-10-07 18:00:23 -0700249 @Override
250 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700251 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800252 Log.v(LOGTAG, this + " onStart");
253 }
254 super.onCreate(icicle);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800255 // test the browser in OpenGL
256 // requestWindowFeature(Window.FEATURE_OPENGL);
257
258 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
259
260 mResolver = getContentResolver();
261
Grace Kloba0923d692009-09-23 21:37:25 -0700262 // If this was a web search request, pass it on to the default web
263 // search provider and finish this activity.
264 if (handleWebSearchIntent(getIntent())) {
265 finish();
266 return;
267 }
268
The Android Open Source Project0c908882009-03-03 19:32:16 -0800269 mSecLockIcon = Resources.getSystem().getDrawable(
270 android.R.drawable.ic_secure);
271 mMixLockIcon = Resources.getSystem().getDrawable(
272 android.R.drawable.ic_partial_secure);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800273
Leon Scroggins81db3662009-06-04 17:45:11 -0400274 FrameLayout frameLayout = (FrameLayout) getWindow().getDecorView()
275 .findViewById(com.android.internal.R.id.content);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400276 mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(this)
277 .inflate(R.layout.custom_screen, null);
278 mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(
279 R.id.main_content);
280 mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout
281 .findViewById(R.id.error_console);
282 mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
283 .findViewById(R.id.fullscreen_custom_content);
284 frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
Leon Scroggins68579392009-09-15 15:31:54 -0400285 mTitleBar = new TitleBar(this);
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400286 mFakeTitleBar = new TitleBar(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800287
288 // Create the tab control and our initial tab
289 mTabControl = new TabControl(this);
290
291 // Open the icon database and retain all the bookmark urls for favicons
292 retainIconsOnStartup();
293
294 // Keep a settings instance handy.
295 mSettings = BrowserSettings.getInstance();
296 mSettings.setTabControl(mTabControl);
297 mSettings.loadFromDb(this);
298
299 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
300 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
301
Grace Klobaa34f6862009-07-31 16:28:17 -0700302 /* enables registration for changes in network status from
303 http stack */
304 mNetworkStateChangedFilter = new IntentFilter();
305 mNetworkStateChangedFilter.addAction(
306 ConnectivityManager.CONNECTIVITY_ACTION);
307 mNetworkStateIntentReceiver = new BroadcastReceiver() {
308 @Override
309 public void onReceive(Context context, Intent intent) {
310 if (intent.getAction().equals(
311 ConnectivityManager.CONNECTIVITY_ACTION)) {
Patrick Scotteb6ab2a2009-09-16 10:00:17 -0400312 NetworkInfo info =
313 (NetworkInfo) intent.getParcelableExtra(
314 ConnectivityManager.EXTRA_NETWORK_INFO);
315 onNetworkToggle(
316 (info != null) ? info.isConnected() : false);
Grace Klobaa34f6862009-07-31 16:28:17 -0700317 }
318 }
319 };
320
Grace Kloba615c6c92009-08-03 10:22:44 -0700321 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
322 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
323 filter.addDataScheme("package");
324 mPackageInstallationReceiver = new BroadcastReceiver() {
325 @Override
326 public void onReceive(Context context, Intent intent) {
327 final String action = intent.getAction();
328 final String packageName = intent.getData()
329 .getSchemeSpecificPart();
330 final boolean replacing = intent.getBooleanExtra(
331 Intent.EXTRA_REPLACING, false);
332 if (Intent.ACTION_PACKAGE_REMOVED.equals(action) && replacing) {
333 // if it is replacing, refreshPlugins() when adding
334 return;
335 }
336 PackageManager pm = BrowserActivity.this.getPackageManager();
337 PackageInfo pkgInfo = null;
338 try {
339 pkgInfo = pm.getPackageInfo(packageName,
340 PackageManager.GET_PERMISSIONS);
341 } catch (PackageManager.NameNotFoundException e) {
342 return;
343 }
344 if (pkgInfo != null) {
345 String permissions[] = pkgInfo.requestedPermissions;
346 if (permissions == null) {
347 return;
348 }
349 boolean permissionOk = false;
350 for (String permit : permissions) {
351 if (PluginManager.PLUGIN_PERMISSION.equals(permit)) {
352 permissionOk = true;
353 break;
354 }
355 }
356 if (permissionOk) {
357 PluginManager.getInstance(BrowserActivity.this)
358 .refreshPlugins(
359 Intent.ACTION_PACKAGE_ADDED
360 .equals(action));
361 }
362 }
363 }
364 };
365 registerReceiver(mPackageInstallationReceiver, filter);
366
The Android Open Source Project0c908882009-03-03 19:32:16 -0800367 if (!mTabControl.restoreState(icicle)) {
368 // clear up the thumbnail directory if we can't restore the state as
369 // none of the files in the directory are referenced any more.
370 new ClearThumbnails().execute(
371 mTabControl.getThumbnailDir().listFiles());
Grace Klobaaab3f092009-07-30 12:29:51 -0700372 // there is no quit on Android. But if we can't restore the state,
373 // we can treat it as a new Browser, remove the old session cookies.
374 CookieManager.getInstance().removeSessionCookie();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800375 final Intent intent = getIntent();
376 final Bundle extra = intent.getExtras();
377 // Create an initial tab.
378 // If the intent is ACTION_VIEW and data is not null, the Browser is
379 // invoked to view the content by another application. In this case,
380 // the tab will be close when exit.
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700381 UrlData urlData = getUrlDataFromIntent(intent);
382
Grace Kloba22ac16e2009-10-07 18:00:23 -0700383 final Tab t = mTabControl.createNewTab(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800384 Intent.ACTION_VIEW.equals(intent.getAction()) &&
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700385 intent.getData() != null,
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700386 intent.getStringExtra(Browser.EXTRA_APPLICATION_ID), urlData.mUrl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800387 mTabControl.setCurrentTab(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800388 attachTabToContentView(t);
389 WebView webView = t.getWebView();
390 if (extra != null) {
391 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
392 if (scale > 0 && scale <= 1000) {
393 webView.setInitialScale(scale);
394 }
395 }
396 // If we are not restoring from an icicle, then there is a high
397 // likely hood this is the first run. So, check to see if the
398 // homepage needs to be configured and copy any plugins from our
399 // asset directory to the data partition.
400 if ((extra == null || !extra.getBoolean("testing"))
401 && !mSettings.isLoginInitialized()) {
402 setupHomePage();
403 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800404
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700405 if (urlData.isEmpty()) {
Leon Scroggins30444232009-09-04 18:36:20 -0400406 if (mSettings.isLoginInitialized()) {
407 webView.loadUrl(mSettings.getHomePage());
408 } else {
409 waitForCredentials();
410 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800411 } else {
Grace Kloba81678d92009-06-30 07:09:56 -0700412 if (extra != null) {
413 urlData.setPostData(extra
414 .getByteArray(Browser.EXTRA_POST_DATA));
415 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700416 urlData.loadIn(webView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800417 }
418 } else {
419 // TabControl.restoreState() will create a new tab even if
Leon Scroggins1f005d32009-08-10 17:36:42 -0400420 // restoring the state fails.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800421 attachTabToContentView(mTabControl.getCurrentTab());
422 }
Grace Kloba615c6c92009-08-03 10:22:44 -0700423
Feng Qianb3c02da2009-06-29 15:58:08 -0700424 // Read JavaScript flags if it exists.
425 String jsFlags = mSettings.getJsFlags();
426 if (jsFlags.trim().length() != 0) {
427 mTabControl.getCurrentWebView().setJsFlags(jsFlags);
428 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800429 }
430
431 @Override
432 protected void onNewIntent(Intent intent) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700433 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800434 // When a tab is closed on exit, the current tab index is set to -1.
435 // Reset before proceed as Browser requires the current tab to be set.
436 if (current == null) {
437 // Try to reset the tab in case the index was incorrect.
438 current = mTabControl.getTab(0);
439 if (current == null) {
440 // No tabs at all so just ignore this intent.
441 return;
442 }
443 mTabControl.setCurrentTab(current);
444 attachTabToContentView(current);
445 resetTitleAndIcon(current.getWebView());
446 }
447 final String action = intent.getAction();
448 final int flags = intent.getFlags();
449 if (Intent.ACTION_MAIN.equals(action) ||
450 (flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
451 // just resume the browser
452 return;
453 }
454 if (Intent.ACTION_VIEW.equals(action)
455 || Intent.ACTION_SEARCH.equals(action)
456 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
457 || Intent.ACTION_WEB_SEARCH.equals(action)) {
Satish Sampath565505b2009-05-29 15:37:27 +0100458 // If this was a search request (e.g. search query directly typed into the address bar),
459 // pass it on to the default web search provider.
460 if (handleWebSearchIntent(intent)) {
461 return;
462 }
463
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700464 UrlData urlData = getUrlDataFromIntent(intent);
465 if (urlData.isEmpty()) {
466 urlData = new UrlData(mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800467 }
Grace Kloba81678d92009-06-30 07:09:56 -0700468 urlData.setPostData(intent
469 .getByteArrayExtra(Browser.EXTRA_POST_DATA));
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700470
Grace Klobacc634032009-07-28 15:58:19 -0700471 final String appId = intent
472 .getStringExtra(Browser.EXTRA_APPLICATION_ID);
473 if (Intent.ACTION_VIEW.equals(action)
474 && !getPackageName().equals(appId)
475 && (flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700476 Tab appTab = mTabControl.getTabFromId(appId);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700477 if (appTab != null) {
478 Log.i(LOGTAG, "Reusing tab for " + appId);
479 // Dismiss the subwindow if applicable.
480 dismissSubWindow(appTab);
481 // Since we might kill the WebView, remove it from the
482 // content view first.
483 removeTabFromContentView(appTab);
484 // Recreate the main WebView after destroying the old one.
485 // If the WebView has the same original url and is on that
486 // page, it can be reused.
487 boolean needsLoad =
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700488 mTabControl.recreateWebView(appTab, urlData.mUrl);
Ben Murdochbff2d602009-07-01 20:19:05 +0100489
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700490 if (current != appTab) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400491 switchToTab(mTabControl.getTabIndex(appTab));
492 if (needsLoad) {
493 urlData.loadIn(appTab.getWebView());
494 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700495 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400496 // If the tab was the current tab, we have to attach
497 // it to the view system again.
498 attachTabToContentView(appTab);
499 if (needsLoad) {
500 urlData.loadIn(appTab.getWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700501 }
502 }
503 return;
Patrick Scottcd115892009-07-16 09:42:58 -0400504 } else {
505 // No matching application tab, try to find a regular tab
506 // with a matching url.
507 appTab = mTabControl.findUnusedTabWithUrl(urlData.mUrl);
Leon Scroggins25515f82009-08-19 15:31:58 -0400508 if (appTab != null) {
509 if (current != appTab) {
510 switchToTab(mTabControl.getTabIndex(appTab));
511 }
512 // Otherwise, we are already viewing the correct tab.
Patrick Scottcd115892009-07-16 09:42:58 -0400513 } else {
514 // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
515 // will be opened in a new tab unless we have reached
516 // MAX_TABS. Then the url will be opened in the current
517 // tab. If a new tab is created, it will have "true" for
518 // exit on close.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400519 openTabAndShow(urlData, true, appId);
Patrick Scottcd115892009-07-16 09:42:58 -0400520 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700521 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800522 } else {
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700523 if ("about:debug".equals(urlData.mUrl)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800524 mSettings.toggleDebugSettings();
525 return;
526 }
Leon Scroggins1f005d32009-08-10 17:36:42 -0400527 // Get rid of the subwindow if it exists
528 dismissSubWindow(current);
529 urlData.loadIn(current.getWebView());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800530 }
531 }
532 }
533
Satish Sampath565505b2009-05-29 15:37:27 +0100534 private int parseUrlShortcut(String url) {
535 if (url == null) return SHORTCUT_INVALID;
536
537 // FIXME: quick search, need to be customized by setting
538 if (url.length() > 2 && url.charAt(1) == ' ') {
539 switch (url.charAt(0)) {
540 case 'g': return SHORTCUT_GOOGLE_SEARCH;
541 case 'w': return SHORTCUT_WIKIPEDIA_SEARCH;
542 case 'd': return SHORTCUT_DICTIONARY_SEARCH;
543 case 'l': return SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH;
544 }
545 }
546 return SHORTCUT_INVALID;
547 }
548
549 /**
550 * Launches the default web search activity with the query parameters if the given intent's data
551 * are identified as plain search terms and not URLs/shortcuts.
552 * @return true if the intent was handled and web search activity was launched, false if not.
553 */
554 private boolean handleWebSearchIntent(Intent intent) {
555 if (intent == null) return false;
556
557 String url = null;
558 final String action = intent.getAction();
559 if (Intent.ACTION_VIEW.equals(action)) {
Grace Kloba1e705052009-09-29 13:13:36 -0700560 Uri data = intent.getData();
561 if (data != null) url = data.toString();
Satish Sampath565505b2009-05-29 15:37:27 +0100562 } else if (Intent.ACTION_SEARCH.equals(action)
563 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
564 || Intent.ACTION_WEB_SEARCH.equals(action)) {
565 url = intent.getStringExtra(SearchManager.QUERY);
566 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100567 return handleWebSearchRequest(url, intent.getBundleExtra(SearchManager.APP_DATA),
568 intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
Satish Sampath565505b2009-05-29 15:37:27 +0100569 }
570
571 /**
572 * Launches the default web search activity with the query parameters if the given url string
573 * was identified as plain search terms and not URL/shortcut.
574 * @return true if the request was handled and web search activity was launched, false if not.
575 */
Bjorn Bringert04851702009-09-22 10:36:01 +0100576 private boolean handleWebSearchRequest(String inUrl, Bundle appData, String extraData) {
Satish Sampath565505b2009-05-29 15:37:27 +0100577 if (inUrl == null) return false;
578
579 // In general, we shouldn't modify URL from Intent.
580 // But currently, we get the user-typed URL from search box as well.
581 String url = fixUrl(inUrl).trim();
582
583 // URLs and site specific search shortcuts are handled by the regular flow of control, so
584 // return early.
585 if (Regex.WEB_URL_PATTERN.matcher(url).matches()
Satish Sampathbc5b9f32009-06-04 18:21:40 +0100586 || ACCEPTED_URI_SCHEMA.matcher(url).matches()
Satish Sampath565505b2009-05-29 15:37:27 +0100587 || parseUrlShortcut(url) != SHORTCUT_INVALID) {
588 return false;
589 }
590
591 Browser.updateVisitedHistory(mResolver, url, false);
592 Browser.addSearchUrl(mResolver, url);
593
594 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
595 intent.addCategory(Intent.CATEGORY_DEFAULT);
596 intent.putExtra(SearchManager.QUERY, url);
Satish Sampath15e9f2d2009-06-23 22:29:49 +0100597 if (appData != null) {
598 intent.putExtra(SearchManager.APP_DATA, appData);
599 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100600 if (extraData != null) {
601 intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
602 }
Grace Klobacc634032009-07-28 15:58:19 -0700603 intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName());
Satish Sampath565505b2009-05-29 15:37:27 +0100604 startActivity(intent);
605
606 return true;
607 }
608
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700609 private UrlData getUrlDataFromIntent(Intent intent) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800610 String url = null;
611 if (intent != null) {
612 final String action = intent.getAction();
613 if (Intent.ACTION_VIEW.equals(action)) {
614 url = smartUrlFilter(intent.getData());
615 if (url != null && url.startsWith("content:")) {
616 /* Append mimetype so webview knows how to display */
617 String mimeType = intent.resolveType(getContentResolver());
618 if (mimeType != null) {
619 url += "?" + mimeType;
620 }
621 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700622 if ("inline:".equals(url)) {
623 return new InlinedUrlData(
624 intent.getStringExtra(Browser.EXTRA_INLINE_CONTENT),
625 intent.getType(),
626 intent.getStringExtra(Browser.EXTRA_INLINE_ENCODING),
627 intent.getStringExtra(Browser.EXTRA_INLINE_FAILURL));
628 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800629 } else if (Intent.ACTION_SEARCH.equals(action)
630 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
631 || Intent.ACTION_WEB_SEARCH.equals(action)) {
632 url = intent.getStringExtra(SearchManager.QUERY);
633 if (url != null) {
634 mLastEnteredUrl = url;
635 // Don't add Urls, just search terms.
636 // Urls will get added when the page is loaded.
637 if (!Regex.WEB_URL_PATTERN.matcher(url).matches()) {
638 Browser.updateVisitedHistory(mResolver, url, false);
639 }
640 // In general, we shouldn't modify URL from Intent.
641 // But currently, we get the user-typed URL from search box as well.
642 url = fixUrl(url);
643 url = smartUrlFilter(url);
644 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
645 if (url.contains(searchSource)) {
646 String source = null;
647 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
648 if (appData != null) {
649 source = appData.getString(SearchManager.SOURCE);
650 }
651 if (TextUtils.isEmpty(source)) {
652 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
653 }
654 url = url.replace(searchSource, "&source=android-"+source+"&");
655 }
656 }
657 }
658 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700659 return new UrlData(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800660 }
661
662 /* package */ static String fixUrl(String inUrl) {
Cary Clark652ff872009-09-10 13:34:44 -0400663 // FIXME: Converting the url to lower case
664 // duplicates functionality in smartUrlFilter().
665 // However, changing all current callers of fixUrl to
666 // call smartUrlFilter in addition may have unwanted
667 // consequences, and is deferred for now.
668 int colon = inUrl.indexOf(':');
669 boolean allLower = true;
670 for (int index = 0; index < colon; index++) {
671 char ch = inUrl.charAt(index);
672 if (!Character.isLetter(ch)) {
673 break;
674 }
675 allLower &= Character.isLowerCase(ch);
676 if (index == colon - 1 && !allLower) {
677 inUrl = inUrl.substring(0, colon).toLowerCase()
678 + inUrl.substring(colon);
679 }
680 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800681 if (inUrl.startsWith("http://") || inUrl.startsWith("https://"))
682 return inUrl;
683 if (inUrl.startsWith("http:") ||
684 inUrl.startsWith("https:")) {
685 if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) {
686 inUrl = inUrl.replaceFirst("/", "//");
687 } else inUrl = inUrl.replaceFirst(":", "://");
688 }
689 return inUrl;
690 }
691
Grace Kloba22ac16e2009-10-07 18:00:23 -0700692 @Override
693 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800694 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700695 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800696 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
697 }
698
699 if (!mActivityInPause) {
700 Log.e(LOGTAG, "BrowserActivity is already resumed.");
701 return;
702 }
703
Mike Reed7bfa63b2009-05-28 11:08:32 -0400704 mTabControl.resumeCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800705 mActivityInPause = false;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400706 resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800707
708 if (mWakeLock.isHeld()) {
709 mHandler.removeMessages(RELEASE_WAKELOCK);
710 mWakeLock.release();
711 }
712
713 if (mCredsDlg != null) {
714 if (!mHandler.hasMessages(CANCEL_CREDS_REQUEST)) {
715 // In case credential request never comes back
716 mHandler.sendEmptyMessageDelayed(CANCEL_CREDS_REQUEST, 6000);
717 }
718 }
719
720 registerReceiver(mNetworkStateIntentReceiver,
721 mNetworkStateChangedFilter);
722 WebView.enablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800723 }
724
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400725 /**
726 * Since the actual title bar is embedded in the WebView, and removing it
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400727 * would change its appearance, use a different TitleBar to show overlayed
728 * at the top of the screen, when the menu is open or the page is loading.
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400729 */
730 private TitleBar mFakeTitleBar;
731
732 /**
Leon Scrogginsd8fd2fc2009-09-16 11:12:09 -0400733 * Holder for the fake title bar. It will have a foreground shadow, as well
734 * as a white background, so the fake title bar looks like the real one.
735 */
736 private ViewGroup mFakeTitleBarHolder;
737
738 /**
739 * Layout parameters for the fake title bar within mFakeTitleBarHolder
740 */
741 private FrameLayout.LayoutParams mFakeTitleBarParams
742 = new FrameLayout.LayoutParams(
Leon Scrogginsc01e4a82009-09-16 14:41:00 -0400743 ViewGroup.LayoutParams.FILL_PARENT,
Leon Scrogginsd8fd2fc2009-09-16 11:12:09 -0400744 ViewGroup.LayoutParams.WRAP_CONTENT);
745 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400746 * Keeps track of whether the options menu is open. This is important in
747 * determining whether to show or hide the title bar overlay.
748 */
749 private boolean mOptionsMenuOpen;
750
751 /**
752 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
753 * of whether the configuration has changed. The first onMenuOpened call
754 * after a configuration change is simply a reopening of the same menu
755 * (i.e. mIconView did not change).
756 */
757 private boolean mConfigChanged;
758
759 /**
760 * Whether or not the options menu is in its smaller, icon menu form. When
761 * true, we want the title bar overlay to be up. When false, we do not.
762 * Only meaningful if mOptionsMenuOpen is true.
763 */
764 private boolean mIconView;
765
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400766 @Override
767 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400768 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
769 if (mOptionsMenuOpen) {
770 if (mConfigChanged) {
771 // We do not need to make any changes to the state of the
772 // title bar, since the only thing that happened was a
773 // change in orientation
774 mConfigChanged = false;
775 } else {
776 if (mIconView) {
777 // Switching the menu to expanded view, so hide the
778 // title bar.
779 hideFakeTitleBar();
780 mIconView = false;
781 } else {
782 // Switching the menu back to icon view, so show the
783 // title bar once again.
784 showFakeTitleBar();
785 mIconView = true;
786 }
787 }
788 } else {
789 // The options menu is closed, so open it, and show the title
790 showFakeTitleBar();
791 mOptionsMenuOpen = true;
792 mConfigChanged = false;
793 mIconView = true;
794 }
795 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400796 return true;
797 }
798
Leon Scrogginsb94bf272009-09-25 15:22:08 -0400799 /**
800 * Special class used exclusively for the shadow drawn underneath the fake
801 * title bar. The shadow does not need to be drawn if the WebView
802 * underneath is scrolled to the top, because it will draw directly on top
803 * of the embedded shadow.
804 */
805 private static class Shadow extends View {
806 private WebView mWebView;
807
808 public Shadow(Context context, AttributeSet attrs) {
809 super(context, attrs);
810 }
811
812 public void setWebView(WebView view) {
813 mWebView = view;
814 }
815
816 @Override
817 public void draw(Canvas canvas) {
818 // In general onDraw is the method to override, but we care about
819 // whether or not the background gets drawn, which happens in draw()
820 if (mWebView == null || mWebView.getScrollY() > getHeight()) {
821 super.draw(canvas);
822 }
823 // Need to invalidate so that if the scroll position changes, we
824 // still draw as appropriate.
825 invalidate();
826 }
827 }
828
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400829 private void showFakeTitleBar() {
Patrick Scottf8de8ec2009-09-21 16:37:29 -0400830 final View decor = getWindow().peekDecorView();
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400831 if (mFakeTitleBar.getParent() == null && mActiveTabsPage == null
Patrick Scottf8de8ec2009-09-21 16:37:29 -0400832 && !mActivityInPause && decor != null
833 && decor.getWindowToken() != null) {
Cary Clarka0464552009-09-29 13:00:45 -0400834 Rect visRect = new Rect();
835 if (!mBrowserFrameLayout.getGlobalVisibleRect(visRect)) {
836 if (LOGD_ENABLED) {
837 Log.d(LOGTAG, "showFakeTitleBar visRect failed");
838 }
839 return;
840 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400841
842 WindowManager manager
843 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
844
845 // Add the title bar to the window manager so it can receive touches
846 // while the menu is up
847 WindowManager.LayoutParams params
848 = new WindowManager.LayoutParams(
849 ViewGroup.LayoutParams.FILL_PARENT,
850 ViewGroup.LayoutParams.WRAP_CONTENT,
851 WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL,
852 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
Leon Scroggins68549862009-09-21 16:02:01 -0400853 PixelFormat.TRANSLUCENT);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400854 params.gravity = Gravity.TOP;
Leon Scrogginsa27ff192009-09-14 12:58:04 -0400855 WebView mainView = mTabControl.getCurrentWebView();
Leon Scroggins68549862009-09-21 16:02:01 -0400856 boolean atTop = mainView != null && mainView.getScrollY() == 0;
Leon Scroggins83932c72009-09-30 11:55:54 -0400857 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400858 // XXX : Without providing an offset, the fake title bar will be
859 // placed underneath the status bar. Use the global visible rect
860 // of mBrowserFrameLayout to determine the bottom of the status bar
Cary Clarka0464552009-09-29 13:00:45 -0400861 params.y = visRect.top;
Leon Scroggins68549862009-09-21 16:02:01 -0400862 // Add a holder for the title bar. It also holds a shadow to show
863 // below the title bar.
Leon Scrogginsd8fd2fc2009-09-16 11:12:09 -0400864 if (mFakeTitleBarHolder == null) {
865 mFakeTitleBarHolder = (ViewGroup) LayoutInflater.from(this)
866 .inflate(R.layout.title_bar_bg, null);
867 }
Leon Scrogginsb94bf272009-09-25 15:22:08 -0400868 Shadow shadow = (Shadow) mFakeTitleBarHolder.findViewById(
869 R.id.shadow);
870 shadow.setWebView(mainView);
Leon Scroggins68549862009-09-21 16:02:01 -0400871 mFakeTitleBarHolder.addView(mFakeTitleBar, 0, mFakeTitleBarParams);
Leon Scrogginsd8fd2fc2009-09-16 11:12:09 -0400872 manager.addView(mFakeTitleBarHolder, params);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400873 }
874 }
875
876 @Override
877 public void onOptionsMenuClosed(Menu menu) {
878 mOptionsMenuOpen = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -0400879 if (!mInLoad) {
880 hideFakeTitleBar();
881 } else if (!mIconView) {
882 // The page is currently loading, and we are in expanded mode, so
883 // we were not showing the menu. Show it once again. It will be
884 // removed when the page finishes.
885 showFakeTitleBar();
886 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400887 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700888
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400889 private void hideFakeTitleBar() {
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400890 if (mFakeTitleBar.getParent() == null) return;
Leon Scroggins20329572009-09-23 17:42:41 -0400891 WindowManager.LayoutParams params = (WindowManager.LayoutParams)
892 mFakeTitleBarHolder.getLayoutParams();
893 WebView mainView = mTabControl.getCurrentWebView();
894 // Although we decided whether or not to animate based on the current
895 // scroll position, the scroll position may have changed since the
896 // fake title bar was displayed. Make sure it has the appropriate
897 // animation/lack thereof before removing.
898 params.windowAnimations = mainView != null && mainView.getScrollY() == 0
Leon Scroggins83932c72009-09-30 11:55:54 -0400899 ? 0 : R.style.TitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400900 WindowManager manager
901 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Leon Scroggins20329572009-09-23 17:42:41 -0400902 manager.updateViewLayout(mFakeTitleBarHolder, params);
Leon Scrogginsd8fd2fc2009-09-16 11:12:09 -0400903 mFakeTitleBarHolder.removeView(mFakeTitleBar);
904 manager.removeView(mFakeTitleBarHolder);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400905 }
906
The Android Open Source Project0c908882009-03-03 19:32:16 -0800907 /**
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400908 * Special method for the fake title bar to call when displaying its context
909 * menu, since it is in its own Window, and its parent does not show a
910 * context menu.
911 */
912 /* package */ void showTitleBarContextMenu() {
Cary Clark65f4a3c2009-09-28 17:05:06 -0400913 if (null == mTitleBar.getParent()) {
914 return;
915 }
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400916 openContextMenu(mTitleBar);
917 }
918
919 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800920 * onSaveInstanceState(Bundle map)
921 * onSaveInstanceState is called right before onStop(). The map contains
922 * the saved state.
923 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700924 @Override
925 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700926 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800927 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
928 }
929 // the default implementation requires each view to have an id. As the
930 // browser handles the state itself and it doesn't use id for the views,
931 // don't call the default implementation. Otherwise it will trigger the
932 // warning like this, "couldn't save which view has focus because the
933 // focused view XXX has no id".
934
935 // Save all the tabs
936 mTabControl.saveState(outState);
937 }
938
Grace Kloba22ac16e2009-10-07 18:00:23 -0700939 @Override
940 protected void onPause() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800941 super.onPause();
942
943 if (mActivityInPause) {
944 Log.e(LOGTAG, "BrowserActivity is already paused.");
945 return;
946 }
947
Mike Reed7bfa63b2009-05-28 11:08:32 -0400948 mTabControl.pauseCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800949 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400950 if (mTabControl.getCurrentIndex() >= 0 && !pauseWebViewTimers()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800951 mWakeLock.acquire();
952 mHandler.sendMessageDelayed(mHandler
953 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
954 }
955
956 // Clear the credentials toast if it is up
957 if (mCredsDlg != null && mCredsDlg.isShowing()) {
958 mCredsDlg.dismiss();
959 }
960 mCredsDlg = null;
961
Leon Scrogginsa2ab6a72009-09-11 11:49:52 -0400962 // FIXME: This removes the active tabs page and resets the menu to
963 // MAIN_MENU. A better solution might be to do this work in onNewIntent
964 // but then we would need to save it in onSaveInstanceState and restore
965 // it in onCreate/onRestoreInstanceState
966 if (mActiveTabsPage != null) {
967 removeActiveTabPage(true);
968 }
969
The Android Open Source Project0c908882009-03-03 19:32:16 -0800970 cancelStopToast();
971
972 // unregister network state listener
973 unregisterReceiver(mNetworkStateIntentReceiver);
974 WebView.disablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800975 }
976
Grace Kloba22ac16e2009-10-07 18:00:23 -0700977 @Override
978 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700979 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800980 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
981 }
982 super.onDestroy();
Grace Kloba0923d692009-09-23 21:37:25 -0700983
Leon Scroggins8d5fa432009-10-02 15:55:59 -0400984 if (mUploadMessage != null) {
985 mUploadMessage.onReceiveValue(null);
986 mUploadMessage = null;
987 }
988
Grace Kloba0923d692009-09-23 21:37:25 -0700989 if (mTabControl == null) return;
990
The Android Open Source Project0c908882009-03-03 19:32:16 -0800991 // Remove the current tab and sub window
Grace Kloba22ac16e2009-10-07 18:00:23 -0700992 Tab t = mTabControl.getCurrentTab();
Patrick Scottfb5e77f2009-04-08 19:17:37 -0700993 if (t != null) {
994 dismissSubWindow(t);
995 removeTabFromContentView(t);
996 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800997 // Destroy all the tabs
998 mTabControl.destroy();
999 WebIconDatabase.getInstance().close();
1000 if (mGlsConnection != null) {
1001 unbindService(mGlsConnection);
1002 mGlsConnection = null;
1003 }
1004
Grace Klobab4da0ad2009-05-14 14:45:40 -07001005 unregisterReceiver(mPackageInstallationReceiver);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001006 }
1007
1008 @Override
1009 public void onConfigurationChanged(Configuration newConfig) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04001010 mConfigChanged = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001011 super.onConfigurationChanged(newConfig);
1012
1013 if (mPageInfoDialog != null) {
1014 mPageInfoDialog.dismiss();
1015 showPageInfo(
1016 mPageInfoView,
1017 mPageInfoFromShowSSLCertificateOnError.booleanValue());
1018 }
1019 if (mSSLCertificateDialog != null) {
1020 mSSLCertificateDialog.dismiss();
1021 showSSLCertificate(
1022 mSSLCertificateView);
1023 }
1024 if (mSSLCertificateOnErrorDialog != null) {
1025 mSSLCertificateOnErrorDialog.dismiss();
1026 showSSLCertificateOnError(
1027 mSSLCertificateOnErrorView,
1028 mSSLCertificateOnErrorHandler,
1029 mSSLCertificateOnErrorError);
1030 }
1031 if (mHttpAuthenticationDialog != null) {
1032 String title = ((TextView) mHttpAuthenticationDialog
1033 .findViewById(com.android.internal.R.id.alertTitle)).getText()
1034 .toString();
1035 String name = ((TextView) mHttpAuthenticationDialog
1036 .findViewById(R.id.username_edit)).getText().toString();
1037 String password = ((TextView) mHttpAuthenticationDialog
1038 .findViewById(R.id.password_edit)).getText().toString();
1039 int focusId = mHttpAuthenticationDialog.getCurrentFocus()
1040 .getId();
1041 mHttpAuthenticationDialog.dismiss();
1042 showHttpAuthentication(mHttpAuthHandler, null, null, title,
1043 name, password, focusId);
1044 }
1045 if (mFindDialog != null && mFindDialog.isShowing()) {
1046 mFindDialog.onConfigurationChanged(newConfig);
1047 }
1048 }
1049
Grace Kloba22ac16e2009-10-07 18:00:23 -07001050 @Override
1051 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001052 super.onLowMemory();
1053 mTabControl.freeMemory();
1054 }
1055
Mike Reed7bfa63b2009-05-28 11:08:32 -04001056 private boolean resumeWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001057 Tab tab = mTabControl.getCurrentTab();
1058 boolean inLoad = tab.inLoad();
1059 if ((!mActivityInPause && !inLoad) || (mActivityInPause && inLoad)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001060 CookieSyncManager.getInstance().startSync();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001061 WebView w = tab.getWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001062 if (w != null) {
1063 w.resumeTimers();
1064 }
1065 return true;
1066 } else {
1067 return false;
1068 }
1069 }
1070
Mike Reed7bfa63b2009-05-28 11:08:32 -04001071 private boolean pauseWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001072 Tab tab = mTabControl.getCurrentTab();
1073 boolean inLoad = tab.inLoad();
1074 if (mActivityInPause && !inLoad) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001075 CookieSyncManager.getInstance().stopSync();
1076 WebView w = mTabControl.getCurrentWebView();
1077 if (w != null) {
1078 w.pauseTimers();
1079 }
1080 return true;
1081 } else {
1082 return false;
1083 }
1084 }
1085
Leon Scroggins1f005d32009-08-10 17:36:42 -04001086 // FIXME: Do we want to call this when loading google for the first time?
The Android Open Source Project0c908882009-03-03 19:32:16 -08001087 /*
1088 * This function is called when we are launching for the first time. We
1089 * are waiting for the login credentials before loading Google home
1090 * pages. This way the user will be logged in straight away.
1091 */
1092 private void waitForCredentials() {
1093 // Show a toast
1094 mCredsDlg = new ProgressDialog(this);
1095 mCredsDlg.setIndeterminate(true);
1096 mCredsDlg.setMessage(getText(R.string.retrieving_creds_dlg_msg));
1097 // If the user cancels the operation, then cancel the Google
1098 // Credentials request.
1099 mCredsDlg.setCancelMessage(mHandler.obtainMessage(CANCEL_CREDS_REQUEST));
1100 mCredsDlg.show();
1101
1102 // We set a timeout for the retrieval of credentials in onResume()
1103 // as that is when we have freed up some CPU time to get
1104 // the login credentials.
1105 }
1106
1107 /*
1108 * If we have received the credentials or we have timed out and we are
1109 * showing the credentials dialog, then it is time to move on.
1110 */
1111 private void resumeAfterCredentials() {
1112 if (mCredsDlg == null) {
1113 return;
1114 }
1115
1116 // Clear the toast
1117 if (mCredsDlg.isShowing()) {
1118 mCredsDlg.dismiss();
1119 }
1120 mCredsDlg = null;
1121
1122 // Clear any pending timeout
1123 mHandler.removeMessages(CANCEL_CREDS_REQUEST);
1124
1125 // Load the page
1126 WebView w = mTabControl.getCurrentWebView();
1127 if (w != null) {
1128 w.loadUrl(mSettings.getHomePage());
1129 }
1130
1131 // Update the settings, need to do this last as it can take a moment
1132 // to persist the settings. In the mean time we could be loading
1133 // content.
1134 mSettings.setLoginInitialized(this);
1135 }
1136
1137 // Open the icon database and retain all the icons for visited sites.
1138 private void retainIconsOnStartup() {
1139 final WebIconDatabase db = WebIconDatabase.getInstance();
1140 db.open(getDir("icons", 0).getPath());
1141 try {
1142 Cursor c = Browser.getAllBookmarks(mResolver);
1143 if (!c.moveToFirst()) {
1144 c.deactivate();
1145 return;
1146 }
1147 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
1148 do {
1149 String url = c.getString(urlIndex);
1150 db.retainIconForPageUrl(url);
1151 } while (c.moveToNext());
1152 c.deactivate();
1153 } catch (IllegalStateException e) {
1154 Log.e(LOGTAG, "retainIconsOnStartup", e);
1155 }
1156 }
1157
1158 // Helper method for getting the top window.
1159 WebView getTopWindow() {
1160 return mTabControl.getCurrentTopWebView();
1161 }
1162
Grace Kloba22ac16e2009-10-07 18:00:23 -07001163 TabControl getTabControl() {
1164 return mTabControl;
1165 }
1166
The Android Open Source Project0c908882009-03-03 19:32:16 -08001167 @Override
1168 public boolean onCreateOptionsMenu(Menu menu) {
1169 super.onCreateOptionsMenu(menu);
1170
1171 MenuInflater inflater = getMenuInflater();
1172 inflater.inflate(R.menu.browser, menu);
1173 mMenu = menu;
1174 updateInLoadMenuItems();
1175 return true;
1176 }
1177
1178 /**
1179 * As the menu can be open when loading state changes
1180 * we must manually update the state of the stop/reload menu
1181 * item
1182 */
1183 private void updateInLoadMenuItems() {
1184 if (mMenu == null) {
1185 return;
1186 }
1187 MenuItem src = mInLoad ?
1188 mMenu.findItem(R.id.stop_menu_id):
1189 mMenu.findItem(R.id.reload_menu_id);
1190 MenuItem dest = mMenu.findItem(R.id.stop_reload_menu_id);
1191 dest.setIcon(src.getIcon());
1192 dest.setTitle(src.getTitle());
1193 }
1194
1195 @Override
1196 public boolean onContextItemSelected(MenuItem item) {
1197 // chording is not an issue with context menus, but we use the same
1198 // options selector, so set mCanChord to true so we can access them.
1199 mCanChord = true;
1200 int id = item.getItemId();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001201 switch (id) {
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001202 // For the context menu from the title bar
1203 case R.id.title_bar_share_page_url:
1204 case R.id.title_bar_copy_page_url:
1205 WebView mainView = mTabControl.getCurrentWebView();
1206 if (null == mainView) {
1207 return false;
1208 }
1209 if (id == R.id.title_bar_share_page_url) {
1210 Browser.sendString(this, mainView.getUrl());
1211 } else {
1212 copy(mainView.getUrl());
1213 }
1214 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001215 // -- Browser context menu
1216 case R.id.open_context_menu_id:
1217 case R.id.open_newtab_context_menu_id:
1218 case R.id.bookmark_context_menu_id:
1219 case R.id.save_link_context_menu_id:
1220 case R.id.share_link_context_menu_id:
1221 case R.id.copy_link_context_menu_id:
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001222 final WebView webView = getTopWindow();
1223 if (null == webView) {
1224 return false;
1225 }
1226 final HashMap hrefMap = new HashMap();
1227 hrefMap.put("webview", webView);
1228 final Message msg = mHandler.obtainMessage(
1229 FOCUS_NODE_HREF, id, 0, hrefMap);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001230 webView.requestFocusNodeHref(msg);
1231 break;
1232
1233 default:
1234 // For other context menus
1235 return onOptionsItemSelected(item);
1236 }
1237 mCanChord = false;
1238 return true;
1239 }
1240
1241 private Bundle createGoogleSearchSourceBundle(String source) {
1242 Bundle bundle = new Bundle();
1243 bundle.putString(SearchManager.SOURCE, source);
1244 return bundle;
1245 }
1246
1247 /**
The Android Open Source Project4e5f5872009-03-09 11:52:14 -07001248 * Overriding this to insert a local information bundle
The Android Open Source Project0c908882009-03-03 19:32:16 -08001249 */
1250 @Override
1251 public boolean onSearchRequested() {
Leon Scroggins68579392009-09-15 15:31:54 -04001252 if (mOptionsMenuOpen) closeOptionsMenu();
Leon Scroggins5bbe9802009-07-31 13:10:55 -04001253 String url = (getTopWindow() == null) ? null : getTopWindow().getUrl();
Grace Kloba83f47342009-07-20 10:44:31 -07001254 startSearch(mSettings.getHomePage().equals(url) ? null : url, true,
The Android Open Source Project4e5f5872009-03-09 11:52:14 -07001255 createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_SEARCHKEY), false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001256 return true;
1257 }
1258
1259 @Override
1260 public void startSearch(String initialQuery, boolean selectInitialQuery,
1261 Bundle appSearchData, boolean globalSearch) {
1262 if (appSearchData == null) {
1263 appSearchData = createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_TYPE);
1264 }
1265 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1266 }
1267
Leon Scroggins1f005d32009-08-10 17:36:42 -04001268 /**
1269 * Switch tabs. Called by the TitleBarSet when sliding the title bar
1270 * results in changing tabs.
Leon Scroggins160a7e72009-08-14 18:28:01 -04001271 * @param index Index of the tab to change to, as defined by
1272 * mTabControl.getTabIndex(Tab t).
1273 * @return boolean True if we successfully switched to a different tab. If
1274 * the indexth tab is null, or if that tab is the same as
1275 * the current one, return false.
Leon Scroggins1f005d32009-08-10 17:36:42 -04001276 */
Leon Scroggins160a7e72009-08-14 18:28:01 -04001277 /* package */ boolean switchToTab(int index) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001278 Tab tab = mTabControl.getTab(index);
1279 Tab currentTab = mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001280 if (tab == null || tab == currentTab) {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001281 return false;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001282 }
1283 if (currentTab != null) {
1284 // currentTab may be null if it was just removed. In that case,
1285 // we do not need to remove it
1286 removeTabFromContentView(currentTab);
1287 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001288 mTabControl.setCurrentTab(tab);
1289 attachTabToContentView(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001290 resetTitleIconAndProgress();
1291 updateLockIconToLatest();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001292 return true;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001293 }
1294
Grace Kloba22ac16e2009-10-07 18:00:23 -07001295 /* package */ Tab openTabToHomePage() {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001296 return openTabAndShow(mSettings.getHomePage(), false, null);
1297 }
1298
Leon Scroggins1f005d32009-08-10 17:36:42 -04001299 /* package */ void closeCurrentWindow() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001300 final Tab current = mTabControl.getCurrentTab();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001301 if (mTabControl.getTabCount() == 1) {
Leon Scroggins30444232009-09-04 18:36:20 -04001302 // This is the last tab. Open a new one, with the home
1303 // page and close the current one.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001304 openTabToHomePage();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001305 closeTab(current);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001306 return;
1307 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001308 final Tab parent = current.getParentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001309 int indexToShow = -1;
1310 if (parent != null) {
1311 indexToShow = mTabControl.getTabIndex(parent);
1312 } else {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001313 final int currentIndex = mTabControl.getCurrentIndex();
1314 // Try to move to the tab to the right
1315 indexToShow = currentIndex + 1;
1316 if (indexToShow > mTabControl.getTabCount() - 1) {
1317 // Try to move to the tab to the left
1318 indexToShow = currentIndex - 1;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001319 }
1320 }
Leon Scroggins160a7e72009-08-14 18:28:01 -04001321 if (switchToTab(indexToShow)) {
1322 // Close window
1323 closeTab(current);
1324 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001325 }
1326
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001327 private ActiveTabsPage mActiveTabsPage;
1328
1329 /**
1330 * Remove the active tabs page.
1331 * @param needToAttach If true, the active tabs page did not attach a tab
1332 * to the content view, so we need to do that here.
1333 */
1334 /* package */ void removeActiveTabPage(boolean needToAttach) {
1335 mContentView.removeView(mActiveTabsPage);
1336 mActiveTabsPage = null;
1337 mMenuState = R.id.MAIN_MENU;
1338 if (needToAttach) {
1339 attachTabToContentView(mTabControl.getCurrentTab());
1340 }
1341 getTopWindow().requestFocus();
1342 }
1343
The Android Open Source Project0c908882009-03-03 19:32:16 -08001344 @Override
1345 public boolean onOptionsItemSelected(MenuItem item) {
1346 if (!mCanChord) {
1347 // The user has already fired a shortcut with this hold down of the
1348 // menu key.
1349 return false;
1350 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001351 if (null == getTopWindow()) {
Leon Scroggins0d7ae0e2009-06-05 11:04:45 -04001352 return false;
1353 }
Grace Kloba6ee9c492009-07-13 10:04:34 -07001354 if (mMenuIsDown) {
1355 // The shortcut action consumes the MENU. Even if it is still down,
1356 // it won't trigger the next shortcut action. In the case of the
1357 // shortcut action triggering a new activity, like Bookmarks, we
1358 // won't get onKeyUp for MENU. So it is important to reset it here.
1359 mMenuIsDown = false;
1360 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001361 switch (item.getItemId()) {
1362 // -- Main menu
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001363 case R.id.new_tab_menu_id:
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001364 openTabToHomePage();
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001365 break;
1366
Leon Scroggins64b80f32009-08-07 12:03:34 -04001367 case R.id.goto_menu_id:
Leon Scrogginsb3a5bed2009-09-28 11:21:56 -04001368 onSearchRequested();
1369 break;
1370
1371 case R.id.bookmarks_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001372 bookmarksOrHistoryPicker(false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001373 break;
1374
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001375 case R.id.active_tabs_menu_id:
1376 mActiveTabsPage = new ActiveTabsPage(this, mTabControl);
1377 removeTabFromContentView(mTabControl.getCurrentTab());
Leon Scroggins43de6162009-09-14 19:59:58 -04001378 hideFakeTitleBar();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001379 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
1380 mActiveTabsPage.requestFocus();
1381 mMenuState = EMPTY_MENU;
1382 break;
1383
Leon Scroggins1f005d32009-08-10 17:36:42 -04001384 case R.id.add_bookmark_menu_id:
1385 Intent i = new Intent(BrowserActivity.this,
1386 AddBookmarkPage.class);
1387 WebView w = getTopWindow();
1388 i.putExtra("url", w.getUrl());
1389 i.putExtra("title", w.getTitle());
Grace Kloba83cdb2c2009-09-16 00:48:57 -07001390 i.putExtra("touch_icon_url", w.getTouchIconUrl());
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01001391 i.putExtra("thumbnail", createScreenshot(w));
Leon Scroggins1f005d32009-08-10 17:36:42 -04001392 startActivity(i);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001393 break;
1394
1395 case R.id.stop_reload_menu_id:
1396 if (mInLoad) {
1397 stopLoading();
1398 } else {
1399 getTopWindow().reload();
1400 }
1401 break;
1402
1403 case R.id.back_menu_id:
1404 getTopWindow().goBack();
1405 break;
1406
1407 case R.id.forward_menu_id:
1408 getTopWindow().goForward();
1409 break;
1410
1411 case R.id.close_menu_id:
1412 // Close the subwindow if it exists.
1413 if (mTabControl.getCurrentSubWindow() != null) {
1414 dismissSubWindow(mTabControl.getCurrentTab());
1415 break;
1416 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001417 closeCurrentWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001418 break;
1419
1420 case R.id.homepage_menu_id:
Grace Kloba22ac16e2009-10-07 18:00:23 -07001421 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001422 if (current != null) {
1423 dismissSubWindow(current);
1424 current.getWebView().loadUrl(mSettings.getHomePage());
1425 }
1426 break;
1427
1428 case R.id.preferences_menu_id:
1429 Intent intent = new Intent(this,
1430 BrowserPreferencesPage.class);
1431 startActivityForResult(intent, PREFERENCES_PAGE);
1432 break;
1433
1434 case R.id.find_menu_id:
1435 if (null == mFindDialog) {
1436 mFindDialog = new FindDialog(this);
1437 }
1438 mFindDialog.setWebView(getTopWindow());
1439 mFindDialog.show();
1440 mMenuState = EMPTY_MENU;
1441 break;
1442
1443 case R.id.select_text_id:
1444 getTopWindow().emulateShiftHeld();
1445 break;
1446 case R.id.page_info_menu_id:
1447 showPageInfo(mTabControl.getCurrentTab(), false);
1448 break;
1449
1450 case R.id.classic_history_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001451 bookmarksOrHistoryPicker(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001452 break;
1453
1454 case R.id.share_page_menu_id:
Andrei Popescu10fdba82009-09-24 13:25:47 +01001455 Browser.sendString(this, getTopWindow().getUrl(),
1456 getText(R.string.choosertitle_sharevia).toString());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001457 break;
1458
1459 case R.id.dump_nav_menu_id:
1460 getTopWindow().debugDump();
1461 break;
1462
1463 case R.id.zoom_in_menu_id:
1464 getTopWindow().zoomIn();
1465 break;
1466
1467 case R.id.zoom_out_menu_id:
1468 getTopWindow().zoomOut();
1469 break;
1470
1471 case R.id.view_downloads_menu_id:
1472 viewDownloads(null);
1473 break;
1474
The Android Open Source Project0c908882009-03-03 19:32:16 -08001475 case R.id.window_one_menu_id:
1476 case R.id.window_two_menu_id:
1477 case R.id.window_three_menu_id:
1478 case R.id.window_four_menu_id:
1479 case R.id.window_five_menu_id:
1480 case R.id.window_six_menu_id:
1481 case R.id.window_seven_menu_id:
1482 case R.id.window_eight_menu_id:
1483 {
1484 int menuid = item.getItemId();
1485 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1486 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001487 Tab desiredTab = mTabControl.getTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001488 if (desiredTab != null &&
1489 desiredTab != mTabControl.getCurrentTab()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001490 switchToTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001491 }
1492 break;
1493 }
1494 }
1495 }
1496 break;
1497
1498 default:
1499 if (!super.onOptionsItemSelected(item)) {
1500 return false;
1501 }
1502 // Otherwise fall through.
1503 }
1504 mCanChord = false;
1505 return true;
1506 }
1507
1508 public void closeFind() {
1509 mMenuState = R.id.MAIN_MENU;
1510 }
1511
Grace Kloba22ac16e2009-10-07 18:00:23 -07001512 @Override
1513 public boolean onPrepareOptionsMenu(Menu menu) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001514 // This happens when the user begins to hold down the menu key, so
1515 // allow them to chord to get a shortcut.
1516 mCanChord = true;
1517 // Note: setVisible will decide whether an item is visible; while
1518 // setEnabled() will decide whether an item is enabled, which also means
1519 // whether the matching shortcut key will function.
1520 super.onPrepareOptionsMenu(menu);
1521 switch (mMenuState) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001522 case EMPTY_MENU:
1523 if (mCurrentMenuState != mMenuState) {
1524 menu.setGroupVisible(R.id.MAIN_MENU, false);
1525 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1526 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001527 }
1528 break;
1529 default:
1530 if (mCurrentMenuState != mMenuState) {
1531 menu.setGroupVisible(R.id.MAIN_MENU, true);
1532 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1533 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001534 }
1535 final WebView w = getTopWindow();
1536 boolean canGoBack = false;
1537 boolean canGoForward = false;
1538 boolean isHome = false;
1539 if (w != null) {
1540 canGoBack = w.canGoBack();
1541 canGoForward = w.canGoForward();
1542 isHome = mSettings.getHomePage().equals(w.getUrl());
1543 }
1544 final MenuItem back = menu.findItem(R.id.back_menu_id);
1545 back.setEnabled(canGoBack);
1546
1547 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1548 home.setEnabled(!isHome);
1549
1550 menu.findItem(R.id.forward_menu_id)
1551 .setEnabled(canGoForward);
1552
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001553 menu.findItem(R.id.new_tab_menu_id).setEnabled(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001554 mTabControl.canCreateNewTab());
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001555
The Android Open Source Project0c908882009-03-03 19:32:16 -08001556 // decide whether to show the share link option
1557 PackageManager pm = getPackageManager();
1558 Intent send = new Intent(Intent.ACTION_SEND);
1559 send.setType("text/plain");
1560 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1561 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1562
The Android Open Source Project0c908882009-03-03 19:32:16 -08001563 boolean isNavDump = mSettings.isNavDump();
1564 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1565 nav.setVisible(isNavDump);
1566 nav.setEnabled(isNavDump);
1567 break;
1568 }
1569 mCurrentMenuState = mMenuState;
1570 return true;
1571 }
1572
1573 @Override
1574 public void onCreateContextMenu(ContextMenu menu, View v,
1575 ContextMenuInfo menuInfo) {
1576 WebView webview = (WebView) v;
1577 WebView.HitTestResult result = webview.getHitTestResult();
1578 if (result == null) {
1579 return;
1580 }
1581
1582 int type = result.getType();
1583 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1584 Log.w(LOGTAG,
1585 "We should not show context menu when nothing is touched");
1586 return;
1587 }
1588 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1589 // let TextView handles context menu
1590 return;
1591 }
1592
1593 // Note, http://b/issue?id=1106666 is requesting that
1594 // an inflated menu can be used again. This is not available
1595 // yet, so inflate each time (yuk!)
1596 MenuInflater inflater = getMenuInflater();
1597 inflater.inflate(R.menu.browsercontext, menu);
1598
1599 // Show the correct menu group
1600 String extra = result.getExtra();
1601 menu.setGroupVisible(R.id.PHONE_MENU,
1602 type == WebView.HitTestResult.PHONE_TYPE);
1603 menu.setGroupVisible(R.id.EMAIL_MENU,
1604 type == WebView.HitTestResult.EMAIL_TYPE);
1605 menu.setGroupVisible(R.id.GEO_MENU,
1606 type == WebView.HitTestResult.GEO_TYPE);
1607 menu.setGroupVisible(R.id.IMAGE_MENU,
1608 type == WebView.HitTestResult.IMAGE_TYPE
1609 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1610 menu.setGroupVisible(R.id.ANCHOR_MENU,
1611 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1612 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1613
1614 // Setup custom handling depending on the type
1615 switch (type) {
1616 case WebView.HitTestResult.PHONE_TYPE:
1617 menu.setHeaderTitle(Uri.decode(extra));
1618 menu.findItem(R.id.dial_context_menu_id).setIntent(
1619 new Intent(Intent.ACTION_VIEW, Uri
1620 .parse(WebView.SCHEME_TEL + extra)));
1621 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1622 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
Cary Clark5e335a32009-09-22 14:53:11 -04001623 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001624 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1625 addIntent);
1626 menu.findItem(R.id.copy_phone_context_menu_id).setOnMenuItemClickListener(
1627 new Copy(extra));
1628 break;
1629
1630 case WebView.HitTestResult.EMAIL_TYPE:
1631 menu.setHeaderTitle(extra);
1632 menu.findItem(R.id.email_context_menu_id).setIntent(
1633 new Intent(Intent.ACTION_VIEW, Uri
1634 .parse(WebView.SCHEME_MAILTO + extra)));
1635 menu.findItem(R.id.copy_mail_context_menu_id).setOnMenuItemClickListener(
1636 new Copy(extra));
1637 break;
1638
1639 case WebView.HitTestResult.GEO_TYPE:
1640 menu.setHeaderTitle(extra);
1641 menu.findItem(R.id.map_context_menu_id).setIntent(
1642 new Intent(Intent.ACTION_VIEW, Uri
1643 .parse(WebView.SCHEME_GEO
1644 + URLEncoder.encode(extra))));
1645 menu.findItem(R.id.copy_geo_context_menu_id).setOnMenuItemClickListener(
1646 new Copy(extra));
1647 break;
1648
1649 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1650 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
1651 TextView titleView = (TextView) LayoutInflater.from(this)
1652 .inflate(android.R.layout.browser_link_context_header,
1653 null);
1654 titleView.setText(extra);
1655 menu.setHeaderView(titleView);
1656 // decide whether to show the open link in new tab option
1657 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001658 mTabControl.canCreateNewTab());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001659 PackageManager pm = getPackageManager();
1660 Intent send = new Intent(Intent.ACTION_SEND);
1661 send.setType("text/plain");
1662 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1663 menu.findItem(R.id.share_link_context_menu_id).setVisible(ri != null);
1664 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1665 break;
1666 }
1667 // otherwise fall through to handle image part
1668 case WebView.HitTestResult.IMAGE_TYPE:
1669 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1670 menu.setHeaderTitle(extra);
1671 }
1672 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1673 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1674 menu.findItem(R.id.download_context_menu_id).
1675 setOnMenuItemClickListener(new Download(extra));
1676 break;
1677
1678 default:
1679 Log.w(LOGTAG, "We should not get here.");
1680 break;
1681 }
1682 }
1683
The Android Open Source Project0c908882009-03-03 19:32:16 -08001684 // Attach the given tab to the content view.
Grace Klobac928c302009-09-17 11:51:21 -07001685 // this should only be called for the current tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001686 private void attachTabToContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001687 // Attach the container that contains the main WebView and any other UI
1688 // associated with the tab.
Patrick Scottd0119532009-09-17 08:00:31 -04001689 t.attachTabToContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001690
1691 if (mShouldShowErrorConsole) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 ErrorConsoleView errorConsole = t.getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01001693 if (errorConsole.numberOfErrors() == 0) {
1694 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
1695 } else {
1696 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1697 }
1698
1699 mErrorConsoleContainer.addView(errorConsole,
1700 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
1701 ViewGroup.LayoutParams.WRAP_CONTENT));
1702 }
1703
Leon Scroggins39ab28e2009-09-02 21:20:30 -04001704 WebView view = t.getWebView();
Leon Scroggins55a5bc22009-09-04 17:00:08 -04001705 view.setEmbeddedTitleBar(mTitleBar);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001706 // Request focus on the top window.
1707 t.getTopWindow().requestFocus();
1708 }
1709
1710 // Attach a sub window to the main WebView of the given tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001711 void attachSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001712 t.attachSubWindow(mContentView);
1713 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001714 }
1715
1716 // Remove the given tab from the content view.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001717 private void removeTabFromContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001718 // Remove the container that contains the main WebView.
Patrick Scottd0119532009-09-17 08:00:31 -04001719 t.removeTabFromContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001720
Grace Kloba22ac16e2009-10-07 18:00:23 -07001721 ErrorConsoleView errorConsole = t.getErrorConsole(false);
1722 if (errorConsole != null) {
1723 mErrorConsoleContainer.removeView(errorConsole);
Ben Murdochbff2d602009-07-01 20:19:05 +01001724 }
1725
Leon Scroggins39ab28e2009-09-02 21:20:30 -04001726 WebView view = t.getWebView();
Leon Scrogginsbb85b902009-09-14 19:27:20 -04001727 if (view != null) {
1728 view.setEmbeddedTitleBar(null);
1729 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001730 }
1731
1732 // Remove the sub window if it exists. Also called by TabControl when the
1733 // user clicks the 'X' to dismiss a sub window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001734 /* package */ void dismissSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001735 t.removeSubWindow(mContentView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001736 // dismiss the subwindow. This will destroy the WebView.
1737 t.dismissSubWindow();
Patrick Scottd0119532009-09-17 08:00:31 -04001738 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001739 }
1740
Leon Scroggins1f005d32009-08-10 17:36:42 -04001741 // A wrapper function of {@link #openTabAndShow(UrlData, boolean, String)}
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07001742 // that accepts url as string.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001743 private Tab openTabAndShow(String url, boolean closeOnExit, String appId) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001744 return openTabAndShow(new UrlData(url), closeOnExit, appId);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001745 }
1746
1747 // This method does a ton of stuff. It will attempt to create a new tab
1748 // if we haven't reached MAX_TABS. Otherwise it uses the current tab. If
Leon Scroggins1f005d32009-08-10 17:36:42 -04001749 // url isn't null, it will load the given url.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001750 /* package */Tab openTabAndShow(UrlData urlData, boolean closeOnExit,
1751 String appId) {
1752 final Tab currentTab = mTabControl.getCurrentTab();
1753 if (mTabControl.canCreateNewTab()) {
1754 final Tab tab = mTabControl.createNewTab(closeOnExit, appId,
1755 urlData.mUrl);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001756 WebView webview = tab.getWebView();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001757 // If the last tab was removed from the active tabs page, currentTab
1758 // will be null.
1759 if (currentTab != null) {
1760 removeTabFromContentView(currentTab);
1761 }
Patrick Scott8bbd69f2009-08-14 13:35:53 -04001762 // We must set the new tab as the current tab to reflect the old
1763 // animation behavior.
1764 mTabControl.setCurrentTab(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001765 attachTabToContentView(tab);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001766 if (!urlData.isEmpty()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001767 urlData.loadIn(webview);
1768 }
1769 return tab;
1770 } else {
1771 // Get rid of the subwindow if it exists
1772 dismissSubWindow(currentTab);
1773 if (!urlData.isEmpty()) {
1774 // Load the given url.
1775 urlData.loadIn(currentTab.getWebView());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001776 }
1777 }
Grace Klobac9181842009-04-14 08:53:22 -07001778 return currentTab;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001779 }
1780
Grace Kloba22ac16e2009-10-07 18:00:23 -07001781 private Tab openTab(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001782 if (mSettings.openInBackground()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001783 Tab t = mTabControl.createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001784 if (t != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001785 WebView view = t.getWebView();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001786 view.loadUrl(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001787 }
Grace Klobac9181842009-04-14 08:53:22 -07001788 return t;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001789 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001790 return openTabAndShow(url, false, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001791 }
1792 }
1793
1794 private class Copy implements OnMenuItemClickListener {
1795 private CharSequence mText;
1796
1797 public boolean onMenuItemClick(MenuItem item) {
1798 copy(mText);
1799 return true;
1800 }
1801
1802 public Copy(CharSequence toCopy) {
1803 mText = toCopy;
1804 }
1805 }
1806
1807 private class Download implements OnMenuItemClickListener {
1808 private String mText;
1809
1810 public boolean onMenuItemClick(MenuItem item) {
1811 onDownloadStartNoStream(mText, null, null, null, -1);
1812 return true;
1813 }
1814
1815 public Download(String toDownload) {
1816 mText = toDownload;
1817 }
1818 }
1819
1820 private void copy(CharSequence text) {
1821 try {
1822 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
1823 if (clip != null) {
1824 clip.setClipboardText(text);
1825 }
1826 } catch (android.os.RemoteException e) {
1827 Log.e(LOGTAG, "Copy failed", e);
1828 }
1829 }
1830
1831 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001832 * Resets the browser title-view to whatever it must be
1833 * (for example, if we had a loading error)
1834 * When we have a new page, we call resetTitle, when we
1835 * have to reset the titlebar to whatever it used to be
1836 * (for example, if the user chose to stop loading), we
1837 * call resetTitleAndRevertLockIcon.
1838 */
1839 /* package */ void resetTitleAndRevertLockIcon() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001840 mTabControl.getCurrentTab().revertLockIcon();
1841 updateLockIconToLatest();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001842 resetTitleIconAndProgress();
1843 }
1844
1845 /**
1846 * Reset the title, favicon, and progress.
1847 */
1848 private void resetTitleIconAndProgress() {
1849 WebView current = mTabControl.getCurrentWebView();
1850 if (current == null) {
1851 return;
1852 }
1853 resetTitleAndIcon(current);
1854 int progress = current.getProgress();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001855 current.getWebChromeClient().onProgressChanged(current, progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001856 }
1857
1858 // Reset the title and the icon based on the given item.
1859 private void resetTitleAndIcon(WebView view) {
1860 WebHistoryItem item = view.copyBackForwardList().getCurrentItem();
1861 if (item != null) {
Leon Scroggins68579392009-09-15 15:31:54 -04001862 setUrlTitle(item.getUrl(), item.getTitle());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001863 setFavicon(item.getFavicon());
1864 } else {
Leon Scroggins68579392009-09-15 15:31:54 -04001865 setUrlTitle(null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001866 setFavicon(null);
1867 }
1868 }
1869
1870 /**
1871 * Sets a title composed of the URL and the title string.
1872 * @param url The URL of the site being loaded.
1873 * @param title The title of the site being loaded.
1874 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07001875 void setUrlTitle(String url, String title) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001876 mUrl = url;
1877 mTitle = title;
1878
Leon Scroggins68579392009-09-15 15:31:54 -04001879 mTitleBar.setTitleAndUrl(title, url);
Leon Scrogginsfe87bd32009-10-06 10:10:00 -04001880 mFakeTitleBar.setTitleAndUrl(title, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001881 }
1882
1883 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001884 * @param url The URL to build a title version of the URL from.
1885 * @return The title version of the URL or null if fails.
1886 * The title version of the URL can be either the URL hostname,
1887 * or the hostname with an "https://" prefix (for secure URLs),
1888 * or an empty string if, for example, the URL in question is a
1889 * file:// URL with no hostname.
1890 */
Leon Scroggins32e14a62009-06-11 10:26:34 -04001891 /* package */ static String buildTitleUrl(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001892 String titleUrl = null;
1893
1894 if (url != null) {
1895 try {
1896 // parse the url string
1897 URL urlObj = new URL(url);
1898 if (urlObj != null) {
1899 titleUrl = "";
1900
1901 String protocol = urlObj.getProtocol();
1902 String host = urlObj.getHost();
1903
1904 if (host != null && 0 < host.length()) {
1905 titleUrl = host;
1906 if (protocol != null) {
1907 // if a secure site, add an "https://" prefix!
1908 if (protocol.equalsIgnoreCase("https")) {
1909 titleUrl = protocol + "://" + host;
1910 }
1911 }
1912 }
1913 }
1914 } catch (MalformedURLException e) {}
1915 }
1916
1917 return titleUrl;
1918 }
1919
1920 // Set the favicon in the title bar.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001921 void setFavicon(Bitmap icon) {
Leon Scroggins68579392009-09-15 15:31:54 -04001922 mTitleBar.setFavicon(icon);
Leon Scrogginsfe87bd32009-10-06 10:10:00 -04001923 mFakeTitleBar.setFavicon(icon);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001924 }
1925
1926 /**
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001927 * Close the tab, remove its associated title bar, and adjust mTabControl's
1928 * current tab to a valid value.
Leon Scroggins1f005d32009-08-10 17:36:42 -04001929 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07001930 /* package */ void closeTab(Tab t) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001931 int currentIndex = mTabControl.getCurrentIndex();
1932 int removeIndex = mTabControl.getTabIndex(t);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001933 mTabControl.removeTab(t);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001934 if (currentIndex >= removeIndex && currentIndex != 0) {
1935 currentIndex--;
1936 }
1937 mTabControl.setCurrentTab(mTabControl.getTab(currentIndex));
Andrei Popescua5bf1de2009-09-23 16:39:23 +01001938 resetTitleIconAndProgress();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001939 }
1940
1941 private void goBackOnePageOrQuit() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001942 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001943 if (current == null) {
1944 /*
1945 * Instead of finishing the activity, simply push this to the back
1946 * of the stack and let ActivityManager to choose the foreground
1947 * activity. As BrowserActivity is singleTask, it will be always the
1948 * root of the task. So we can use either true or false for
1949 * moveTaskToBack().
1950 */
1951 moveTaskToBack(true);
Grace Kloba00d85e72009-09-23 18:50:05 -07001952 return;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001953 }
1954 WebView w = current.getWebView();
1955 if (w.canGoBack()) {
1956 w.goBack();
1957 } else {
1958 // Check to see if we are closing a window that was created by
1959 // another window. If so, we switch back to that window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001960 Tab parent = current.getParentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001961 if (parent != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001962 switchToTab(mTabControl.getTabIndex(parent));
1963 // Now we close the other tab
1964 closeTab(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001965 } else {
1966 if (current.closeOnExit()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001967 // force the tab's inLoad() to be false as we are going to
1968 // either finish the activity or remove the tab. This will
1969 // ensure pauseWebViewTimers() taking action.
1970 mTabControl.getCurrentTab().clearInLoad();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001971 if (mTabControl.getTabCount() == 1) {
1972 finish();
1973 return;
1974 }
Mike Reed7bfa63b2009-05-28 11:08:32 -04001975 // call pauseWebViewTimers() now, we won't be able to call
1976 // it in onPause() as the WebView won't be valid.
Grace Klobaec1b5ad2009-08-18 08:42:32 -07001977 // Temporarily change mActivityInPause to be true as
1978 // pauseWebViewTimers() will do nothing if mActivityInPause
1979 // is false.
Grace Kloba918e1d72009-08-13 14:55:06 -07001980 boolean savedState = mActivityInPause;
1981 if (savedState) {
Grace Klobaec1b5ad2009-08-18 08:42:32 -07001982 Log.e(LOGTAG, "BrowserActivity is already paused "
1983 + "while handing goBackOnePageOrQuit.");
Grace Kloba918e1d72009-08-13 14:55:06 -07001984 }
1985 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -04001986 pauseWebViewTimers();
Grace Kloba918e1d72009-08-13 14:55:06 -07001987 mActivityInPause = savedState;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001988 removeTabFromContentView(current);
1989 mTabControl.removeTab(current);
1990 }
1991 /*
1992 * Instead of finishing the activity, simply push this to the back
1993 * of the stack and let ActivityManager to choose the foreground
1994 * activity. As BrowserActivity is singleTask, it will be always the
1995 * root of the task. So we can use either true or false for
1996 * moveTaskToBack().
1997 */
1998 moveTaskToBack(true);
1999 }
2000 }
2001 }
2002
Grace Kloba22ac16e2009-10-07 18:00:23 -07002003 boolean isMenuDown() {
2004 return mMenuIsDown;
2005 }
2006
Grace Kloba5942df02009-09-18 11:48:29 -07002007 @Override
2008 public boolean onKeyDown(int keyCode, KeyEvent event) {
2009 // The default key mode is DEFAULT_KEYS_SEARCH_LOCAL. As the MENU is
2010 // still down, we don't want to trigger the search. Pretend to consume
2011 // the key and do nothing.
2012 if (mMenuIsDown) return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002013
Grace Kloba5942df02009-09-18 11:48:29 -07002014 switch(keyCode) {
2015 case KeyEvent.KEYCODE_MENU:
2016 mMenuIsDown = true;
2017 break;
2018 case KeyEvent.KEYCODE_SPACE:
Grace Klobada0fe552009-09-22 18:17:24 -07002019 // WebView/WebTextView handle the keys in the KeyDown. As
2020 // the Activity's shortcut keys are only handled when WebView
2021 // doesn't, have to do it in onKeyDown instead of onKeyUp.
2022 if (event.isShiftPressed()) {
2023 getTopWindow().pageUp(false);
2024 } else {
2025 getTopWindow().pageDown(false);
2026 }
Grace Kloba5942df02009-09-18 11:48:29 -07002027 return true;
2028 case KeyEvent.KEYCODE_BACK:
2029 if (event.getRepeatCount() == 0) {
2030 event.startTracking();
2031 return true;
2032 } else if (mCustomView == null && mActiveTabsPage == null
2033 && event.isLongPress()) {
2034 bookmarksOrHistoryPicker(true);
2035 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002036 }
Grace Kloba5942df02009-09-18 11:48:29 -07002037 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002038 }
Grace Kloba5942df02009-09-18 11:48:29 -07002039 return super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002040 }
2041
Grace Kloba5942df02009-09-18 11:48:29 -07002042 @Override
2043 public boolean onKeyUp(int keyCode, KeyEvent event) {
2044 switch(keyCode) {
2045 case KeyEvent.KEYCODE_MENU:
2046 mMenuIsDown = false;
2047 break;
Grace Kloba5942df02009-09-18 11:48:29 -07002048 case KeyEvent.KEYCODE_BACK:
2049 if (event.isTracking() && !event.isCanceled()) {
2050 if (mCustomView != null) {
2051 // if a custom view is showing, hide it
Grace Kloba22ac16e2009-10-07 18:00:23 -07002052 mTabControl.getCurrentWebView().getWebChromeClient()
2053 .onHideCustomView();
Grace Kloba5942df02009-09-18 11:48:29 -07002054 } else if (mActiveTabsPage != null) {
2055 // if tab page is showing, hide it
2056 removeActiveTabPage(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002057 } else {
Grace Kloba5942df02009-09-18 11:48:29 -07002058 WebView subwindow = mTabControl.getCurrentSubWindow();
2059 if (subwindow != null) {
2060 if (subwindow.canGoBack()) {
2061 subwindow.goBack();
2062 } else {
2063 dismissSubWindow(mTabControl.getCurrentTab());
2064 }
2065 } else {
2066 goBackOnePageOrQuit();
2067 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002068 }
Grace Kloba5942df02009-09-18 11:48:29 -07002069 return true;
2070 }
2071 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002072 }
Grace Kloba5942df02009-09-18 11:48:29 -07002073 return super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002074 }
2075
Leon Scroggins68579392009-09-15 15:31:54 -04002076 /* package */ void stopLoading() {
Ben Murdochb7cc8b42009-09-28 10:59:09 +01002077 mDidStopLoad = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002078 resetTitleAndRevertLockIcon();
2079 WebView w = getTopWindow();
2080 w.stopLoading();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002081 // FIXME: before refactor, it is using mWebViewClient. So I keep the
2082 // same logic here. But for subwindow case, should we call into the main
2083 // WebView's onPageFinished as we never call its onPageStarted and if
2084 // the page finishes itself, we don't call onPageFinished.
2085 mTabControl.getCurrentWebView().getWebViewClient().onPageFinished(w,
2086 w.getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002087
2088 cancelStopToast();
2089 mStopToast = Toast
2090 .makeText(this, R.string.stopping, Toast.LENGTH_SHORT);
2091 mStopToast.show();
2092 }
2093
Grace Kloba22ac16e2009-10-07 18:00:23 -07002094 boolean didUserStopLoading() {
2095 return mDidStopLoad;
2096 }
2097
The Android Open Source Project0c908882009-03-03 19:32:16 -08002098 private void cancelStopToast() {
2099 if (mStopToast != null) {
2100 mStopToast.cancel();
2101 mStopToast = null;
2102 }
2103 }
2104
Grace Kloba22ac16e2009-10-07 18:00:23 -07002105 // called by a UI or non-UI thread to post the message
2106 public void postMessage(int what, int arg1, int arg2, Object obj,
2107 long delayMillis) {
2108 mHandler.sendMessageDelayed(mHandler.obtainMessage(what, arg1, arg2,
2109 obj), delayMillis);
2110 }
2111
2112 // called by a UI or non-UI thread to remove the message
2113 void removeMessages(int what, Object object) {
2114 mHandler.removeMessages(what, object);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002115 }
2116
2117 // public message ids
2118 public final static int LOAD_URL = 1001;
2119 public final static int STOP_LOAD = 1002;
2120
2121 // Message Ids
2122 private static final int FOCUS_NODE_HREF = 102;
2123 private static final int CANCEL_CREDS_REQUEST = 103;
Grace Kloba92c18a52009-07-31 23:48:32 -07002124 private static final int RELEASE_WAKELOCK = 107;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002125
Grace Kloba22ac16e2009-10-07 18:00:23 -07002126 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
Ben Murdoch2694e232009-09-29 09:41:11 +01002127
The Android Open Source Project0c908882009-03-03 19:32:16 -08002128 // Private handler for handling javascript and saving passwords
2129 private Handler mHandler = new Handler() {
2130
2131 public void handleMessage(Message msg) {
2132 switch (msg.what) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002133 case FOCUS_NODE_HREF:
Ben Murdoch2694e232009-09-29 09:41:11 +01002134 {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002135 String url = (String) msg.getData().get("url");
2136 if (url == null || url.length() == 0) {
2137 break;
2138 }
2139 HashMap focusNodeMap = (HashMap) msg.obj;
2140 WebView view = (WebView) focusNodeMap.get("webview");
2141 // Only apply the action if the top window did not change.
2142 if (getTopWindow() != view) {
2143 break;
2144 }
2145 switch (msg.arg1) {
2146 case R.id.open_context_menu_id:
2147 case R.id.view_image_context_menu_id:
2148 loadURL(getTopWindow(), url);
2149 break;
2150 case R.id.open_newtab_context_menu_id:
Grace Kloba22ac16e2009-10-07 18:00:23 -07002151 final Tab parent = mTabControl.getCurrentTab();
2152 final Tab newTab = openTab(url);
Grace Klobac9181842009-04-14 08:53:22 -07002153 if (newTab != parent) {
2154 parent.addChildTab(newTab);
2155 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002156 break;
2157 case R.id.bookmark_context_menu_id:
2158 Intent intent = new Intent(BrowserActivity.this,
2159 AddBookmarkPage.class);
2160 intent.putExtra("url", url);
2161 startActivity(intent);
2162 break;
2163 case R.id.share_link_context_menu_id:
Andrei Popescu10fdba82009-09-24 13:25:47 +01002164 Browser.sendString(BrowserActivity.this, url,
2165 getText(R.string.choosertitle_sharevia).toString());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002166 break;
2167 case R.id.copy_link_context_menu_id:
2168 copy(url);
2169 break;
2170 case R.id.save_link_context_menu_id:
2171 case R.id.download_context_menu_id:
2172 onDownloadStartNoStream(url, null, null, null, -1);
2173 break;
2174 }
2175 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002176 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002177
2178 case LOAD_URL:
2179 loadURL(getTopWindow(), (String) msg.obj);
2180 break;
2181
2182 case STOP_LOAD:
2183 stopLoading();
2184 break;
2185
2186 case CANCEL_CREDS_REQUEST:
2187 resumeAfterCredentials();
2188 break;
2189
The Android Open Source Project0c908882009-03-03 19:32:16 -08002190 case RELEASE_WAKELOCK:
2191 if (mWakeLock.isHeld()) {
2192 mWakeLock.release();
Grace Kloba5d0e02e2009-10-05 15:15:36 -07002193 // if we reach here, Browser should be still in the
2194 // background loading after WAKELOCK_TIMEOUT (5-min).
2195 // To avoid burning the battery, stop loading.
2196 mTabControl.stopAllLoading();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002197 }
2198 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002199
2200 case UPDATE_BOOKMARK_THUMBNAIL:
2201 WebView view = (WebView) msg.obj;
2202 if (view != null) {
2203 updateScreenshot(view);
2204 }
2205 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002206 }
2207 }
2208 };
2209
Leon Scroggins89c6d362009-07-15 16:54:37 -04002210 private void updateScreenshot(WebView view) {
2211 // If this is a bookmarked site, add a screenshot to the database.
2212 // FIXME: When should we update? Every time?
2213 // FIXME: Would like to make sure there is actually something to
2214 // draw, but the API for that (WebViewCore.pictureReady()) is not
2215 // currently accessible here.
Ben Murdochaac7aa62009-09-17 16:57:40 +01002216
Patrick Scott3918d442009-08-04 13:22:29 -04002217 ContentResolver cr = getContentResolver();
2218 final Cursor c = BrowserBookmarksAdapter.queryBookmarksForUrl(
Ben Murdochaac7aa62009-09-17 16:57:40 +01002219 cr, view.getOriginalUrl(), view.getUrl(), true);
Patrick Scott3918d442009-08-04 13:22:29 -04002220 if (c != null) {
Leon Scroggins89c6d362009-07-15 16:54:37 -04002221 boolean succeed = c.moveToFirst();
2222 ContentValues values = null;
2223 while (succeed) {
2224 if (values == null) {
2225 final ByteArrayOutputStream os
2226 = new ByteArrayOutputStream();
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002227 Bitmap bm = createScreenshot(view);
Leon Scroggins45800572009-09-29 16:38:47 -04002228 if (bm == null) {
2229 c.close();
2230 return;
2231 }
Leon Scroggins89c6d362009-07-15 16:54:37 -04002232 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
2233 values = new ContentValues();
2234 values.put(Browser.BookmarkColumns.THUMBNAIL,
2235 os.toByteArray());
2236 }
2237 cr.update(ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
2238 c.getInt(0)), values, null, null);
2239 succeed = c.moveToNext();
2240 }
2241 c.close();
2242 }
2243 }
2244
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002245 /**
Leon Scrogginsf8551612009-09-24 16:06:02 -04002246 * Values for the size of the thumbnail created when taking a screenshot.
2247 * Lazily initialized. Instead of using these directly, use
2248 * getDesiredThumbnailWidth() or getDesiredThumbnailHeight().
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002249 */
Leon Scrogginsf8551612009-09-24 16:06:02 -04002250 private static int THUMBNAIL_WIDTH = 0;
2251 private static int THUMBNAIL_HEIGHT = 0;
2252
2253 /**
2254 * Return the desired width for thumbnail screenshots, which are stored in
2255 * the database, and used on the bookmarks screen.
2256 * @param context Context for finding out the density of the screen.
2257 * @return int desired width for thumbnail screenshot.
2258 */
2259 /* package */ static int getDesiredThumbnailWidth(Context context) {
2260 if (THUMBNAIL_WIDTH == 0) {
2261 float density = context.getResources().getDisplayMetrics().density;
2262 THUMBNAIL_WIDTH = (int) (90 * density);
2263 THUMBNAIL_HEIGHT = (int) (80 * density);
2264 }
2265 return THUMBNAIL_WIDTH;
2266 }
2267
2268 /**
2269 * Return the desired height for thumbnail screenshots, which are stored in
2270 * the database, and used on the bookmarks screen.
2271 * @param context Context for finding out the density of the screen.
2272 * @return int desired height for thumbnail screenshot.
2273 */
2274 /* package */ static int getDesiredThumbnailHeight(Context context) {
2275 // To ensure that they are both initialized.
2276 getDesiredThumbnailWidth(context);
2277 return THUMBNAIL_HEIGHT;
2278 }
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002279
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002280 private Bitmap createScreenshot(WebView view) {
2281 Picture thumbnail = view.capturePicture();
Leon Scroggins45800572009-09-29 16:38:47 -04002282 if (thumbnail == null) {
2283 return null;
2284 }
Leon Scrogginsf8551612009-09-24 16:06:02 -04002285 Bitmap bm = Bitmap.createBitmap(getDesiredThumbnailWidth(this),
2286 getDesiredThumbnailHeight(this), Bitmap.Config.ARGB_4444);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002287 Canvas canvas = new Canvas(bm);
2288 // May need to tweak these values to determine what is the
2289 // best scale factor
Ben Murdoch2694e232009-09-29 09:41:11 +01002290 int thumbnailWidth = thumbnail.getWidth();
2291 if (thumbnailWidth > 0) {
2292 float scaleFactor = (float) getDesiredThumbnailWidth(this) /
2293 (float)thumbnailWidth;
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002294 canvas.scale(scaleFactor, scaleFactor);
2295 }
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002296 thumbnail.draw(canvas);
2297 return bm;
2298 }
2299
The Android Open Source Project0c908882009-03-03 19:32:16 -08002300 // -------------------------------------------------------------------------
Grace Kloba22ac16e2009-10-07 18:00:23 -07002301 // Helper function for WebViewClient.
The Android Open Source Project0c908882009-03-03 19:32:16 -08002302 //-------------------------------------------------------------------------
2303
2304 // Use in overrideUrlLoading
2305 /* package */ final static String SCHEME_WTAI = "wtai://wp/";
2306 /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
2307 /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;";
2308 /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;";
2309
Grace Kloba22ac16e2009-10-07 18:00:23 -07002310 void onPageStarted(WebView view, String url, Bitmap favicon) {
2311 // when BrowserActivity just starts, onPageStarted may be called before
2312 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
2313 // to start the timer. As we won't switch tabs while an activity is in
2314 // pause state, we can ensure calling resume and pause in pair.
2315 if (mActivityInPause) resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002316
Grace Kloba22ac16e2009-10-07 18:00:23 -07002317 resetLockIcon(url);
2318 setUrlTitle(url, null);
2319 setFavicon(favicon);
2320 mInLoad = true;
2321 mDidStopLoad = false;
2322 showFakeTitleBar();
2323 updateInLoadMenuItems();
2324 if (!mIsNetworkUp) createAndShowNetworkDialog();
Patrick Scott15525d42009-09-21 13:39:37 -04002325
Grace Kloba22ac16e2009-10-07 18:00:23 -07002326 if (mSettings.isTracing()) {
2327 String host;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002328 try {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002329 WebAddress uri = new WebAddress(url);
2330 host = uri.mHost;
2331 } catch (android.net.ParseException ex) {
2332 host = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -08002333 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002334 host = host.replace('.', '_');
2335 host += ".trace";
2336 mInTrace = true;
2337 Debug.startMethodTracing(host, 20 * 1024 * 1024);
2338 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002339
Grace Kloba22ac16e2009-10-07 18:00:23 -07002340 // Performance probe
2341 if (false) {
2342 mStart = SystemClock.uptimeMillis();
2343 mProcessStart = Process.getElapsedCpuTime();
2344 long[] sysCpu = new long[7];
2345 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2346 sysCpu, null)) {
2347 mUserStart = sysCpu[0] + sysCpu[1];
2348 mSystemStart = sysCpu[2];
2349 mIdleStart = sysCpu[3];
2350 mIrqStart = sysCpu[4] + sysCpu[5] + sysCpu[6];
2351 }
2352 mUiStart = SystemClock.currentThreadTimeMillis();
2353 }
2354 }
2355
2356 void onPageFinished(WebView view, String url) {
2357 // Reset the title and icon in case we stopped a provisional load.
2358 resetTitleAndIcon(view);
2359 // Update the lock icon image only once we are done loading
2360 updateLockIconToLatest();
2361 // pause the WebView timer and release the wake lock if it is finished
2362 // while BrowserActivity is in pause state.
2363 if (mActivityInPause && pauseWebViewTimers()) {
2364 if (mWakeLock.isHeld()) {
2365 mHandler.removeMessages(RELEASE_WAKELOCK);
2366 mWakeLock.release();
2367 }
2368 }
2369
2370 // Performance probe
2371 if (false) {
2372 long[] sysCpu = new long[7];
2373 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2374 sysCpu, null)) {
2375 String uiInfo = "UI thread used "
2376 + (SystemClock.currentThreadTimeMillis() - mUiStart)
2377 + " ms";
2378 if (LOGD_ENABLED) {
2379 Log.d(LOGTAG, uiInfo);
2380 }
2381 //The string that gets written to the log
2382 String performanceString = "It took total "
2383 + (SystemClock.uptimeMillis() - mStart)
2384 + " ms clock time to load the page."
2385 + "\nbrowser process used "
2386 + (Process.getElapsedCpuTime() - mProcessStart)
2387 + " ms, user processes used "
2388 + (sysCpu[0] + sysCpu[1] - mUserStart) * 10
2389 + " ms, kernel used "
2390 + (sysCpu[2] - mSystemStart) * 10
2391 + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10
2392 + " ms and irq took "
2393 + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart)
2394 * 10 + " ms, " + uiInfo;
2395 if (LOGD_ENABLED) {
2396 Log.d(LOGTAG, performanceString + "\nWebpage: " + url);
2397 }
2398 if (url != null) {
2399 // strip the url to maintain consistency
2400 String newUrl = new String(url);
2401 if (newUrl.startsWith("http://www.")) {
2402 newUrl = newUrl.substring(11);
2403 } else if (newUrl.startsWith("http://")) {
2404 newUrl = newUrl.substring(7);
2405 } else if (newUrl.startsWith("https://www.")) {
2406 newUrl = newUrl.substring(12);
2407 } else if (newUrl.startsWith("https://")) {
2408 newUrl = newUrl.substring(8);
2409 }
2410 if (LOGD_ENABLED) {
2411 Log.d(LOGTAG, newUrl + " loaded");
2412 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002413 }
2414 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002415 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002416
Grace Kloba22ac16e2009-10-07 18:00:23 -07002417 if (mInTrace) {
2418 mInTrace = false;
2419 Debug.stopMethodTracing();
2420 }
2421 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002422
Grace Kloba22ac16e2009-10-07 18:00:23 -07002423 boolean shouldOverrideUrlLoading(WebView view, String url) {
2424 if (url.startsWith(SCHEME_WTAI)) {
2425 // wtai://wp/mc;number
2426 // number=string(phone-number)
2427 if (url.startsWith(SCHEME_WTAI_MC)) {
2428 Intent intent = new Intent(Intent.ACTION_VIEW,
2429 Uri.parse(WebView.SCHEME_TEL +
2430 url.substring(SCHEME_WTAI_MC.length())));
2431 startActivity(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002432 return true;
2433 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002434 // wtai://wp/sd;dtmf
2435 // dtmf=string(dialstring)
2436 if (url.startsWith(SCHEME_WTAI_SD)) {
2437 // TODO: only send when there is active voice connection
2438 return false;
2439 }
2440 // wtai://wp/ap;number;name
2441 // number=string(phone-number)
2442 // name=string
2443 if (url.startsWith(SCHEME_WTAI_AP)) {
2444 // TODO
2445 return false;
2446 }
2447 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002448
Grace Kloba22ac16e2009-10-07 18:00:23 -07002449 // The "about:" schemes are internal to the browser; don't want these to
2450 // be dispatched to other apps.
2451 if (url.startsWith("about:")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002452 return false;
2453 }
2454
Grace Kloba22ac16e2009-10-07 18:00:23 -07002455 Intent intent;
2456 // perform generic parsing of the URI to turn it into an Intent.
2457 try {
2458 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
2459 } catch (URISyntaxException ex) {
2460 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
2461 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002462 }
2463
Grace Kloba22ac16e2009-10-07 18:00:23 -07002464 // check whether the intent can be resolved. If not, we will see
2465 // whether we can download it from the Market.
2466 if (getPackageManager().resolveActivity(intent, 0) == null) {
2467 String packagename = intent.getPackage();
2468 if (packagename != null) {
2469 intent = new Intent(Intent.ACTION_VIEW, Uri
2470 .parse("market://search?q=pname:" + packagename));
2471 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2472 startActivity(intent);
2473 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002474 } else {
2475 return false;
2476 }
2477 }
2478
Grace Kloba22ac16e2009-10-07 18:00:23 -07002479 // sanitize the Intent, ensuring web pages can not bypass browser
2480 // security (only access to BROWSABLE activities).
2481 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2482 intent.setComponent(null);
2483 try {
2484 if (startActivityIfNeeded(intent, -1)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002485 return true;
2486 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002487 } catch (ActivityNotFoundException ex) {
2488 // ignore the error. If no application can handle the URL,
2489 // eg about:blank, assume the browser can handle it.
2490 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002491
Grace Kloba22ac16e2009-10-07 18:00:23 -07002492 if (mMenuIsDown) {
2493 openTab(url);
2494 closeOptionsMenu();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002495 return true;
2496 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002497 return false;
2498 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002499
Grace Kloba22ac16e2009-10-07 18:00:23 -07002500 // -------------------------------------------------------------------------
2501 // Helper function for WebChromeClient
2502 // -------------------------------------------------------------------------
The Android Open Source Project0c908882009-03-03 19:32:16 -08002503
Grace Kloba22ac16e2009-10-07 18:00:23 -07002504 void onProgressChanged(WebView view, int newProgress) {
2505 mTitleBar.setProgress(newProgress);
2506 mFakeTitleBar.setProgress(newProgress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002507
Grace Kloba22ac16e2009-10-07 18:00:23 -07002508 if (newProgress == 100) {
2509 // onProgressChanged() may continue to be called after the main
2510 // frame has finished loading, as any remaining sub frames continue
2511 // to load. We'll only get called once though with newProgress as
2512 // 100 when everything is loaded. (onPageFinished is called once
2513 // when the main frame completes loading regardless of the state of
2514 // any sub frames so calls to onProgressChanges may continue after
2515 // onPageFinished has executed)
2516 if (mInLoad) {
2517 mInLoad = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -04002518 updateInLoadMenuItems();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002519 // If the options menu is open, leave the title bar
2520 if (!mOptionsMenuOpen || !mIconView) {
2521 hideFakeTitleBar();
The Android Open Source Projectcb9a0bb2009-03-11 12:11:58 -07002522 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002523 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002524 } else if (!mInLoad) {
2525 // onPageFinished may have already been called but a subframe is
2526 // still loading and updating the progress. Reset mInLoad and update
2527 // the menu items.
2528 mInLoad = true;
2529 updateInLoadMenuItems();
2530 if (!mOptionsMenuOpen || mIconView) {
2531 // This page has begun to load, so show the title bar
2532 showFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002533 }
2534 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002535 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002536
Grace Kloba22ac16e2009-10-07 18:00:23 -07002537 void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
2538 if (mCustomView != null)
2539 return;
2540
2541 // Add the custom view to its container.
2542 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
2543 mCustomView = view;
2544 mCustomViewCallback = callback;
2545 // Save the menu state and set it to empty while the custom
2546 // view is showing.
2547 mOldMenuState = mMenuState;
2548 mMenuState = EMPTY_MENU;
2549 // Hide the content view.
2550 mContentView.setVisibility(View.GONE);
2551 // Finally show the custom view container.
2552 mCustomViewContainer.setVisibility(View.VISIBLE);
2553 mCustomViewContainer.bringToFront();
2554 }
2555
2556 void onHideCustomView() {
2557 if (mCustomView == null)
2558 return;
2559
2560 // Hide the custom view.
2561 mCustomView.setVisibility(View.GONE);
2562 // Remove the custom view from its container.
2563 mCustomViewContainer.removeView(mCustomView);
2564 mCustomView = null;
2565 // Reset the old menu state.
2566 mMenuState = mOldMenuState;
2567 mOldMenuState = EMPTY_MENU;
2568 mCustomViewContainer.setVisibility(View.GONE);
2569 mCustomViewCallback.onCustomViewHidden();
2570 // Show the content view.
2571 mContentView.setVisibility(View.VISIBLE);
2572 }
2573
2574 Bitmap getDefaultVideoPoster() {
2575 if (mDefaultVideoPoster == null) {
2576 mDefaultVideoPoster = BitmapFactory.decodeResource(
2577 getResources(), R.drawable.default_video_poster);
Patrick Scott3918d442009-08-04 13:22:29 -04002578 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002579 return mDefaultVideoPoster;
2580 }
Patrick Scott3918d442009-08-04 13:22:29 -04002581
Grace Kloba22ac16e2009-10-07 18:00:23 -07002582 View getVideoLoadingProgressView() {
2583 if (mVideoProgressView == null) {
2584 LayoutInflater inflater = LayoutInflater.from(BrowserActivity.this);
2585 mVideoProgressView = inflater.inflate(
2586 R.layout.video_loading_progress, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002587 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002588 return mVideoProgressView;
2589 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002590
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002591 /*
2592 * The Object used to inform the WebView of the file to upload.
2593 */
2594 private ValueCallback<Uri> mUploadMessage;
2595
Grace Kloba22ac16e2009-10-07 18:00:23 -07002596 void openFileChooser(ValueCallback<Uri> uploadMsg) {
2597 if (mUploadMessage != null) return;
2598 mUploadMessage = uploadMsg;
2599 Intent i = new Intent(Intent.ACTION_GET_CONTENT);
2600 i.addCategory(Intent.CATEGORY_OPENABLE);
2601 i.setType("*/*");
2602 BrowserActivity.this.startActivityForResult(Intent.createChooser(i,
2603 getString(R.string.choose_upload)), FILE_SELECTED);
2604 }
2605
2606 // -------------------------------------------------------------------------
2607 // Implement functions for DownloadListener
2608 // -------------------------------------------------------------------------
2609
The Android Open Source Project0c908882009-03-03 19:32:16 -08002610 /**
2611 * Notify the host application a download should be done, or that
2612 * the data should be streamed if a streaming viewer is available.
2613 * @param url The full url to the content that should be downloaded
2614 * @param contentDisposition Content-disposition http header, if
2615 * present.
2616 * @param mimetype The mimetype of the content reported by the server
2617 * @param contentLength The file size reported by the server
2618 */
2619 public void onDownloadStart(String url, String userAgent,
2620 String contentDisposition, String mimetype, long contentLength) {
2621 // if we're dealing wih A/V content that's not explicitly marked
2622 // for download, check if it's streamable.
2623 if (contentDisposition == null
Patrick Scotte1fb9662009-08-31 14:31:52 -04002624 || !contentDisposition.regionMatches(
2625 true, 0, "attachment", 0, 10)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002626 // query the package manager to see if there's a registered handler
2627 // that matches.
2628 Intent intent = new Intent(Intent.ACTION_VIEW);
2629 intent.setDataAndType(Uri.parse(url), mimetype);
Patrick Scotte1fb9662009-08-31 14:31:52 -04002630 ResolveInfo info = getPackageManager().resolveActivity(intent,
2631 PackageManager.MATCH_DEFAULT_ONLY);
2632 if (info != null) {
2633 ComponentName myName = getComponentName();
2634 // If we resolved to ourselves, we don't want to attempt to
2635 // load the url only to try and download it again.
2636 if (!myName.getPackageName().equals(
2637 info.activityInfo.packageName)
2638 || !myName.getClassName().equals(
2639 info.activityInfo.name)) {
2640 // someone (other than us) knows how to handle this mime
2641 // type with this scheme, don't download.
2642 try {
2643 startActivity(intent);
2644 return;
2645 } catch (ActivityNotFoundException ex) {
2646 if (LOGD_ENABLED) {
2647 Log.d(LOGTAG, "activity not found for " + mimetype
2648 + " over " + Uri.parse(url).getScheme(),
2649 ex);
2650 }
2651 // Best behavior is to fall back to a download in this
2652 // case
The Android Open Source Project0c908882009-03-03 19:32:16 -08002653 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002654 }
2655 }
2656 }
2657 onDownloadStartNoStream(url, userAgent, contentDisposition, mimetype, contentLength);
2658 }
2659
2660 /**
2661 * Notify the host application a download should be done, even if there
2662 * is a streaming viewer available for thise type.
2663 * @param url The full url to the content that should be downloaded
2664 * @param contentDisposition Content-disposition http header, if
2665 * present.
2666 * @param mimetype The mimetype of the content reported by the server
2667 * @param contentLength The file size reported by the server
2668 */
2669 /*package */ void onDownloadStartNoStream(String url, String userAgent,
2670 String contentDisposition, String mimetype, long contentLength) {
2671
2672 String filename = URLUtil.guessFileName(url,
2673 contentDisposition, mimetype);
2674
2675 // Check to see if we have an SDCard
2676 String status = Environment.getExternalStorageState();
2677 if (!status.equals(Environment.MEDIA_MOUNTED)) {
2678 int title;
2679 String msg;
2680
2681 // Check to see if the SDCard is busy, same as the music app
2682 if (status.equals(Environment.MEDIA_SHARED)) {
2683 msg = getString(R.string.download_sdcard_busy_dlg_msg);
2684 title = R.string.download_sdcard_busy_dlg_title;
2685 } else {
2686 msg = getString(R.string.download_no_sdcard_dlg_msg, filename);
2687 title = R.string.download_no_sdcard_dlg_title;
2688 }
2689
2690 new AlertDialog.Builder(this)
2691 .setTitle(title)
2692 .setIcon(android.R.drawable.ic_dialog_alert)
2693 .setMessage(msg)
2694 .setPositiveButton(R.string.ok, null)
2695 .show();
2696 return;
2697 }
2698
2699 // java.net.URI is a lot stricter than KURL so we have to undo
2700 // KURL's percent-encoding and redo the encoding using java.net.URI.
2701 URI uri = null;
2702 try {
2703 // Undo the percent-encoding that KURL may have done.
2704 String newUrl = new String(URLUtil.decode(url.getBytes()));
2705 // Parse the url into pieces
2706 WebAddress w = new WebAddress(newUrl);
2707 String frag = null;
2708 String query = null;
2709 String path = w.mPath;
2710 // Break the path into path, query, and fragment
2711 if (path.length() > 0) {
2712 // Strip the fragment
2713 int idx = path.lastIndexOf('#');
2714 if (idx != -1) {
2715 frag = path.substring(idx + 1);
2716 path = path.substring(0, idx);
2717 }
2718 idx = path.lastIndexOf('?');
2719 if (idx != -1) {
2720 query = path.substring(idx + 1);
2721 path = path.substring(0, idx);
2722 }
2723 }
2724 uri = new URI(w.mScheme, w.mAuthInfo, w.mHost, w.mPort, path,
2725 query, frag);
2726 } catch (Exception e) {
2727 Log.e(LOGTAG, "Could not parse url for download: " + url, e);
2728 return;
2729 }
2730
2731 // XXX: Have to use the old url since the cookies were stored using the
2732 // old percent-encoded url.
2733 String cookies = CookieManager.getInstance().getCookie(url);
2734
2735 ContentValues values = new ContentValues();
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -07002736 values.put(Downloads.COLUMN_URI, uri.toString());
2737 values.put(Downloads.COLUMN_COOKIE_DATA, cookies);
2738 values.put(Downloads.COLUMN_USER_AGENT, userAgent);
2739 values.put(Downloads.COLUMN_NOTIFICATION_PACKAGE,
The Android Open Source Project0c908882009-03-03 19:32:16 -08002740 getPackageName());
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -07002741 values.put(Downloads.COLUMN_NOTIFICATION_CLASS,
The Android Open Source Project0c908882009-03-03 19:32:16 -08002742 BrowserDownloadPage.class.getCanonicalName());
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -07002743 values.put(Downloads.COLUMN_VISIBILITY, Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
2744 values.put(Downloads.COLUMN_MIME_TYPE, mimetype);
2745 values.put(Downloads.COLUMN_FILE_NAME_HINT, filename);
2746 values.put(Downloads.COLUMN_DESCRIPTION, uri.getHost());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002747 if (contentLength > 0) {
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -07002748 values.put(Downloads.COLUMN_TOTAL_BYTES, contentLength);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002749 }
2750 if (mimetype == null) {
2751 // We must have long pressed on a link or image to download it. We
2752 // are not sure of the mimetype in this case, so do a head request
2753 new FetchUrlMimeType(this).execute(values);
2754 } else {
2755 final Uri contentUri =
2756 getContentResolver().insert(Downloads.CONTENT_URI, values);
2757 viewDownloads(contentUri);
2758 }
2759
2760 }
2761
Grace Kloba22ac16e2009-10-07 18:00:23 -07002762 // -------------------------------------------------------------------------
2763
The Android Open Source Project0c908882009-03-03 19:32:16 -08002764 /**
2765 * Resets the lock icon. This method is called when we start a new load and
2766 * know the url to be loaded.
2767 */
2768 private void resetLockIcon(String url) {
2769 // Save the lock-icon state (we revert to it if the load gets cancelled)
Grace Kloba22ac16e2009-10-07 18:00:23 -07002770 mTabControl.getCurrentTab().resetLockIcon(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002771 updateLockIconImage(LOCK_ICON_UNSECURE);
2772 }
2773
The Android Open Source Project0c908882009-03-03 19:32:16 -08002774 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04002775 * Update the lock icon to correspond to our latest state.
2776 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002777 private void updateLockIconToLatest() {
2778 updateLockIconImage(mTabControl.getCurrentTab().getLockIconType());
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04002779 }
2780
2781 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08002782 * Updates the lock-icon image in the title-bar.
2783 */
2784 private void updateLockIconImage(int lockIconType) {
2785 Drawable d = null;
2786 if (lockIconType == LOCK_ICON_SECURE) {
2787 d = mSecLockIcon;
2788 } else if (lockIconType == LOCK_ICON_MIXED) {
2789 d = mMixLockIcon;
2790 }
Leon Scroggins68579392009-09-15 15:31:54 -04002791 mTitleBar.setLock(d);
Leon Scrogginsfe87bd32009-10-06 10:10:00 -04002792 mFakeTitleBar.setLock(d);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002793 }
2794
2795 /**
2796 * Displays a page-info dialog.
2797 * @param tab The tab to show info about
2798 * @param fromShowSSLCertificateOnError The flag that indicates whether
2799 * this dialog was opened from the SSL-certificate-on-error dialog or
2800 * not. This is important, since we need to know whether to return to
2801 * the parent dialog or simply dismiss.
2802 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002803 private void showPageInfo(final Tab tab,
The Android Open Source Project0c908882009-03-03 19:32:16 -08002804 final boolean fromShowSSLCertificateOnError) {
2805 final LayoutInflater factory = LayoutInflater
2806 .from(this);
2807
2808 final View pageInfoView = factory.inflate(R.layout.page_info, null);
2809
2810 final WebView view = tab.getWebView();
2811
2812 String url = null;
2813 String title = null;
2814
2815 if (view == null) {
2816 url = tab.getUrl();
2817 title = tab.getTitle();
2818 } else if (view == mTabControl.getCurrentWebView()) {
2819 // Use the cached title and url if this is the current WebView
2820 url = mUrl;
2821 title = mTitle;
2822 } else {
2823 url = view.getUrl();
2824 title = view.getTitle();
2825 }
2826
2827 if (url == null) {
2828 url = "";
2829 }
2830 if (title == null) {
2831 title = "";
2832 }
2833
2834 ((TextView) pageInfoView.findViewById(R.id.address)).setText(url);
2835 ((TextView) pageInfoView.findViewById(R.id.title)).setText(title);
2836
2837 mPageInfoView = tab;
2838 mPageInfoFromShowSSLCertificateOnError = new Boolean(fromShowSSLCertificateOnError);
2839
2840 AlertDialog.Builder alertDialogBuilder =
2841 new AlertDialog.Builder(this)
2842 .setTitle(R.string.page_info).setIcon(android.R.drawable.ic_dialog_info)
2843 .setView(pageInfoView)
2844 .setPositiveButton(
2845 R.string.ok,
2846 new DialogInterface.OnClickListener() {
2847 public void onClick(DialogInterface dialog,
2848 int whichButton) {
2849 mPageInfoDialog = null;
2850 mPageInfoView = null;
2851 mPageInfoFromShowSSLCertificateOnError = null;
2852
2853 // if we came here from the SSL error dialog
2854 if (fromShowSSLCertificateOnError) {
2855 // go back to the SSL error dialog
2856 showSSLCertificateOnError(
2857 mSSLCertificateOnErrorView,
2858 mSSLCertificateOnErrorHandler,
2859 mSSLCertificateOnErrorError);
2860 }
2861 }
2862 })
2863 .setOnCancelListener(
2864 new DialogInterface.OnCancelListener() {
2865 public void onCancel(DialogInterface dialog) {
2866 mPageInfoDialog = null;
2867 mPageInfoView = null;
2868 mPageInfoFromShowSSLCertificateOnError = null;
2869
2870 // if we came here from the SSL error dialog
2871 if (fromShowSSLCertificateOnError) {
2872 // go back to the SSL error dialog
2873 showSSLCertificateOnError(
2874 mSSLCertificateOnErrorView,
2875 mSSLCertificateOnErrorHandler,
2876 mSSLCertificateOnErrorError);
2877 }
2878 }
2879 });
2880
2881 // if we have a main top-level page SSL certificate set or a certificate
2882 // error
2883 if (fromShowSSLCertificateOnError ||
2884 (view != null && view.getCertificate() != null)) {
2885 // add a 'View Certificate' button
2886 alertDialogBuilder.setNeutralButton(
2887 R.string.view_certificate,
2888 new DialogInterface.OnClickListener() {
2889 public void onClick(DialogInterface dialog,
2890 int whichButton) {
2891 mPageInfoDialog = null;
2892 mPageInfoView = null;
2893 mPageInfoFromShowSSLCertificateOnError = null;
2894
2895 // if we came here from the SSL error dialog
2896 if (fromShowSSLCertificateOnError) {
2897 // go back to the SSL error dialog
2898 showSSLCertificateOnError(
2899 mSSLCertificateOnErrorView,
2900 mSSLCertificateOnErrorHandler,
2901 mSSLCertificateOnErrorError);
2902 } else {
2903 // otherwise, display the top-most certificate from
2904 // the chain
2905 if (view.getCertificate() != null) {
2906 showSSLCertificate(tab);
2907 }
2908 }
2909 }
2910 });
2911 }
2912
2913 mPageInfoDialog = alertDialogBuilder.show();
2914 }
2915
2916 /**
2917 * Displays the main top-level page SSL certificate dialog
2918 * (accessible from the Page-Info dialog).
2919 * @param tab The tab to show certificate for.
2920 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002921 private void showSSLCertificate(final Tab tab) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002922 final View certificateView =
2923 inflateCertificateView(tab.getWebView().getCertificate());
2924 if (certificateView == null) {
2925 return;
2926 }
2927
2928 LayoutInflater factory = LayoutInflater.from(this);
2929
2930 final LinearLayout placeholder =
2931 (LinearLayout)certificateView.findViewById(R.id.placeholder);
2932
2933 LinearLayout ll = (LinearLayout) factory.inflate(
2934 R.layout.ssl_success, placeholder);
2935 ((TextView)ll.findViewById(R.id.success))
2936 .setText(R.string.ssl_certificate_is_valid);
2937
2938 mSSLCertificateView = tab;
2939 mSSLCertificateDialog =
2940 new AlertDialog.Builder(this)
2941 .setTitle(R.string.ssl_certificate).setIcon(
2942 R.drawable.ic_dialog_browser_certificate_secure)
2943 .setView(certificateView)
2944 .setPositiveButton(R.string.ok,
2945 new DialogInterface.OnClickListener() {
2946 public void onClick(DialogInterface dialog,
2947 int whichButton) {
2948 mSSLCertificateDialog = null;
2949 mSSLCertificateView = null;
2950
2951 showPageInfo(tab, false);
2952 }
2953 })
2954 .setOnCancelListener(
2955 new DialogInterface.OnCancelListener() {
2956 public void onCancel(DialogInterface dialog) {
2957 mSSLCertificateDialog = null;
2958 mSSLCertificateView = null;
2959
2960 showPageInfo(tab, false);
2961 }
2962 })
2963 .show();
2964 }
2965
2966 /**
2967 * Displays the SSL error certificate dialog.
2968 * @param view The target web-view.
2969 * @param handler The SSL error handler responsible for cancelling the
2970 * connection that resulted in an SSL error or proceeding per user request.
2971 * @param error The SSL error object.
2972 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002973 void showSSLCertificateOnError(
The Android Open Source Project0c908882009-03-03 19:32:16 -08002974 final WebView view, final SslErrorHandler handler, final SslError error) {
2975
2976 final View certificateView =
2977 inflateCertificateView(error.getCertificate());
2978 if (certificateView == null) {
2979 return;
2980 }
2981
2982 LayoutInflater factory = LayoutInflater.from(this);
2983
2984 final LinearLayout placeholder =
2985 (LinearLayout)certificateView.findViewById(R.id.placeholder);
2986
2987 if (error.hasError(SslError.SSL_UNTRUSTED)) {
2988 LinearLayout ll = (LinearLayout)factory
2989 .inflate(R.layout.ssl_warning, placeholder);
2990 ((TextView)ll.findViewById(R.id.warning))
2991 .setText(R.string.ssl_untrusted);
2992 }
2993
2994 if (error.hasError(SslError.SSL_IDMISMATCH)) {
2995 LinearLayout ll = (LinearLayout)factory
2996 .inflate(R.layout.ssl_warning, placeholder);
2997 ((TextView)ll.findViewById(R.id.warning))
2998 .setText(R.string.ssl_mismatch);
2999 }
3000
3001 if (error.hasError(SslError.SSL_EXPIRED)) {
3002 LinearLayout ll = (LinearLayout)factory
3003 .inflate(R.layout.ssl_warning, placeholder);
3004 ((TextView)ll.findViewById(R.id.warning))
3005 .setText(R.string.ssl_expired);
3006 }
3007
3008 if (error.hasError(SslError.SSL_NOTYETVALID)) {
3009 LinearLayout ll = (LinearLayout)factory
3010 .inflate(R.layout.ssl_warning, placeholder);
3011 ((TextView)ll.findViewById(R.id.warning))
3012 .setText(R.string.ssl_not_yet_valid);
3013 }
3014
3015 mSSLCertificateOnErrorHandler = handler;
3016 mSSLCertificateOnErrorView = view;
3017 mSSLCertificateOnErrorError = error;
3018 mSSLCertificateOnErrorDialog =
3019 new AlertDialog.Builder(this)
3020 .setTitle(R.string.ssl_certificate).setIcon(
3021 R.drawable.ic_dialog_browser_certificate_partially_secure)
3022 .setView(certificateView)
3023 .setPositiveButton(R.string.ok,
3024 new DialogInterface.OnClickListener() {
3025 public void onClick(DialogInterface dialog,
3026 int whichButton) {
3027 mSSLCertificateOnErrorDialog = null;
3028 mSSLCertificateOnErrorView = null;
3029 mSSLCertificateOnErrorHandler = null;
3030 mSSLCertificateOnErrorError = null;
3031
Grace Kloba22ac16e2009-10-07 18:00:23 -07003032 view.getWebViewClient().onReceivedSslError(
3033 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003034 }
3035 })
3036 .setNeutralButton(R.string.page_info_view,
3037 new DialogInterface.OnClickListener() {
3038 public void onClick(DialogInterface dialog,
3039 int whichButton) {
3040 mSSLCertificateOnErrorDialog = null;
3041
3042 // do not clear the dialog state: we will
3043 // need to show the dialog again once the
3044 // user is done exploring the page-info details
3045
3046 showPageInfo(mTabControl.getTabFromView(view),
3047 true);
3048 }
3049 })
3050 .setOnCancelListener(
3051 new DialogInterface.OnCancelListener() {
3052 public void onCancel(DialogInterface dialog) {
3053 mSSLCertificateOnErrorDialog = null;
3054 mSSLCertificateOnErrorView = null;
3055 mSSLCertificateOnErrorHandler = null;
3056 mSSLCertificateOnErrorError = null;
3057
Grace Kloba22ac16e2009-10-07 18:00:23 -07003058 view.getWebViewClient().onReceivedSslError(
3059 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003060 }
3061 })
3062 .show();
3063 }
3064
3065 /**
3066 * Inflates the SSL certificate view (helper method).
3067 * @param certificate The SSL certificate.
3068 * @return The resultant certificate view with issued-to, issued-by,
3069 * issued-on, expires-on, and possibly other fields set.
3070 * If the input certificate is null, returns null.
3071 */
3072 private View inflateCertificateView(SslCertificate certificate) {
3073 if (certificate == null) {
3074 return null;
3075 }
3076
3077 LayoutInflater factory = LayoutInflater.from(this);
3078
3079 View certificateView = factory.inflate(
3080 R.layout.ssl_certificate, null);
3081
3082 // issued to:
3083 SslCertificate.DName issuedTo = certificate.getIssuedTo();
3084 if (issuedTo != null) {
3085 ((TextView) certificateView.findViewById(R.id.to_common))
3086 .setText(issuedTo.getCName());
3087 ((TextView) certificateView.findViewById(R.id.to_org))
3088 .setText(issuedTo.getOName());
3089 ((TextView) certificateView.findViewById(R.id.to_org_unit))
3090 .setText(issuedTo.getUName());
3091 }
3092
3093 // issued by:
3094 SslCertificate.DName issuedBy = certificate.getIssuedBy();
3095 if (issuedBy != null) {
3096 ((TextView) certificateView.findViewById(R.id.by_common))
3097 .setText(issuedBy.getCName());
3098 ((TextView) certificateView.findViewById(R.id.by_org))
3099 .setText(issuedBy.getOName());
3100 ((TextView) certificateView.findViewById(R.id.by_org_unit))
3101 .setText(issuedBy.getUName());
3102 }
3103
3104 // issued on:
3105 String issuedOn = reformatCertificateDate(
3106 certificate.getValidNotBefore());
3107 ((TextView) certificateView.findViewById(R.id.issued_on))
3108 .setText(issuedOn);
3109
3110 // expires on:
3111 String expiresOn = reformatCertificateDate(
3112 certificate.getValidNotAfter());
3113 ((TextView) certificateView.findViewById(R.id.expires_on))
3114 .setText(expiresOn);
3115
3116 return certificateView;
3117 }
3118
3119 /**
3120 * Re-formats the certificate date (Date.toString()) string to
3121 * a properly localized date string.
3122 * @return Properly localized version of the certificate date string and
3123 * the original certificate date string if fails to localize.
3124 * If the original string is null, returns an empty string "".
3125 */
3126 private String reformatCertificateDate(String certificateDate) {
3127 String reformattedDate = null;
3128
3129 if (certificateDate != null) {
3130 Date date = null;
3131 try {
3132 date = java.text.DateFormat.getInstance().parse(certificateDate);
3133 } catch (ParseException e) {
3134 date = null;
3135 }
3136
3137 if (date != null) {
3138 reformattedDate =
3139 DateFormat.getDateFormat(this).format(date);
3140 }
3141 }
3142
3143 return reformattedDate != null ? reformattedDate :
3144 (certificateDate != null ? certificateDate : "");
3145 }
3146
3147 /**
3148 * Displays an http-authentication dialog.
3149 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003150 void showHttpAuthentication(final HttpAuthHandler handler,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003151 final String host, final String realm, final String title,
3152 final String name, final String password, int focusId) {
3153 LayoutInflater factory = LayoutInflater.from(this);
3154 final View v = factory
3155 .inflate(R.layout.http_authentication, null);
3156 if (name != null) {
3157 ((EditText) v.findViewById(R.id.username_edit)).setText(name);
3158 }
3159 if (password != null) {
3160 ((EditText) v.findViewById(R.id.password_edit)).setText(password);
3161 }
3162
3163 String titleText = title;
3164 if (titleText == null) {
3165 titleText = getText(R.string.sign_in_to).toString().replace(
3166 "%s1", host).replace("%s2", realm);
3167 }
3168
3169 mHttpAuthHandler = handler;
3170 AlertDialog dialog = new AlertDialog.Builder(this)
3171 .setTitle(titleText)
3172 .setIcon(android.R.drawable.ic_dialog_alert)
3173 .setView(v)
3174 .setPositiveButton(R.string.action,
3175 new DialogInterface.OnClickListener() {
3176 public void onClick(DialogInterface dialog,
3177 int whichButton) {
3178 String nm = ((EditText) v
3179 .findViewById(R.id.username_edit))
3180 .getText().toString();
3181 String pw = ((EditText) v
3182 .findViewById(R.id.password_edit))
3183 .getText().toString();
3184 BrowserActivity.this.setHttpAuthUsernamePassword
3185 (host, realm, nm, pw);
3186 handler.proceed(nm, pw);
3187 mHttpAuthenticationDialog = null;
3188 mHttpAuthHandler = null;
3189 }})
3190 .setNegativeButton(R.string.cancel,
3191 new DialogInterface.OnClickListener() {
3192 public void onClick(DialogInterface dialog,
3193 int whichButton) {
3194 handler.cancel();
3195 BrowserActivity.this.resetTitleAndRevertLockIcon();
3196 mHttpAuthenticationDialog = null;
3197 mHttpAuthHandler = null;
3198 }})
3199 .setOnCancelListener(new DialogInterface.OnCancelListener() {
3200 public void onCancel(DialogInterface dialog) {
3201 handler.cancel();
3202 BrowserActivity.this.resetTitleAndRevertLockIcon();
3203 mHttpAuthenticationDialog = null;
3204 mHttpAuthHandler = null;
3205 }})
3206 .create();
3207 // Make the IME appear when the dialog is displayed if applicable.
3208 dialog.getWindow().setSoftInputMode(
3209 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
3210 dialog.show();
3211 if (focusId != 0) {
3212 dialog.findViewById(focusId).requestFocus();
3213 } else {
3214 v.findViewById(R.id.username_edit).requestFocus();
3215 }
3216 mHttpAuthenticationDialog = dialog;
3217 }
3218
3219 public int getProgress() {
3220 WebView w = mTabControl.getCurrentWebView();
3221 if (w != null) {
3222 return w.getProgress();
3223 } else {
3224 return 100;
3225 }
3226 }
3227
3228 /**
3229 * Set HTTP authentication password.
3230 *
3231 * @param host The host for the password
3232 * @param realm The realm for the password
3233 * @param username The username for the password. If it is null, it means
3234 * password can't be saved.
3235 * @param password The password
3236 */
3237 public void setHttpAuthUsernamePassword(String host, String realm,
3238 String username,
3239 String password) {
3240 WebView w = mTabControl.getCurrentWebView();
3241 if (w != null) {
3242 w.setHttpAuthUsernamePassword(host, realm, username, password);
3243 }
3244 }
3245
3246 /**
3247 * connectivity manager says net has come or gone... inform the user
3248 * @param up true if net has come up, false if net has gone down
3249 */
3250 public void onNetworkToggle(boolean up) {
3251 if (up == mIsNetworkUp) {
3252 return;
3253 } else if (up) {
3254 mIsNetworkUp = true;
3255 if (mAlertDialog != null) {
3256 mAlertDialog.cancel();
3257 mAlertDialog = null;
3258 }
3259 } else {
3260 mIsNetworkUp = false;
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003261 if (mInLoad) {
3262 createAndShowNetworkDialog();
3263 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003264 }
3265 WebView w = mTabControl.getCurrentWebView();
3266 if (w != null) {
3267 w.setNetworkAvailable(up);
3268 }
3269 }
3270
Grace Kloba22ac16e2009-10-07 18:00:23 -07003271 boolean isNetworkUp() {
3272 return mIsNetworkUp;
3273 }
3274
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003275 // This method shows the network dialog alerting the user that the net is
3276 // down. It will only show the dialog if mAlertDialog is null.
3277 private void createAndShowNetworkDialog() {
3278 if (mAlertDialog == null) {
3279 mAlertDialog = new AlertDialog.Builder(this)
3280 .setTitle(R.string.loadSuspendedTitle)
3281 .setMessage(R.string.loadSuspended)
3282 .setPositiveButton(R.string.ok, null)
3283 .show();
3284 }
3285 }
3286
The Android Open Source Project0c908882009-03-03 19:32:16 -08003287 @Override
3288 protected void onActivityResult(int requestCode, int resultCode,
3289 Intent intent) {
3290 switch (requestCode) {
3291 case COMBO_PAGE:
3292 if (resultCode == RESULT_OK && intent != null) {
3293 String data = intent.getAction();
3294 Bundle extras = intent.getExtras();
3295 if (extras != null && extras.getBoolean("new_window", false)) {
Leon Scroggins25d35472009-09-15 11:37:27 -04003296 openTab(data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003297 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -07003298 final Tab currentTab =
The Android Open Source Project0c908882009-03-03 19:32:16 -08003299 mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04003300 dismissSubWindow(currentTab);
3301 if (data != null && data.length() != 0) {
3302 getTopWindow().loadUrl(data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003303 }
3304 }
3305 }
3306 break;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003307 // Choose a file from the file picker.
3308 case FILE_SELECTED:
3309 if (null == mUploadMessage) break;
3310 Uri result = intent == null || resultCode != RESULT_OK ? null
3311 : intent.getData();
3312 mUploadMessage.onReceiveValue(result);
3313 mUploadMessage = null;
3314 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003315 default:
3316 break;
3317 }
Leon Scroggins30444232009-09-04 18:36:20 -04003318 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003319 }
3320
3321 /*
3322 * This method is called as a result of the user selecting the options
3323 * menu to see the download window, or when a download changes state. It
3324 * shows the download window ontop of the current window.
3325 */
3326 /* package */ void viewDownloads(Uri downloadRecord) {
3327 Intent intent = new Intent(this,
3328 BrowserDownloadPage.class);
3329 intent.setData(downloadRecord);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003330 startActivityForResult(intent, BrowserActivity.DOWNLOAD_PAGE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003331
3332 }
3333
Leon Scroggins160a7e72009-08-14 18:28:01 -04003334 /**
3335 * Open the Go page.
3336 * @param startWithHistory If true, open starting on the history tab.
3337 * Otherwise, start with the bookmarks tab.
Leon Scroggins160a7e72009-08-14 18:28:01 -04003338 */
Leon Scroggins30444232009-09-04 18:36:20 -04003339 /* package */ void bookmarksOrHistoryPicker(boolean startWithHistory) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003340 WebView current = mTabControl.getCurrentWebView();
3341 if (current == null) {
3342 return;
3343 }
3344 Intent intent = new Intent(this,
3345 CombinedBookmarkHistoryActivity.class);
3346 String title = current.getTitle();
3347 String url = current.getUrl();
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003348 Bitmap thumbnail = createScreenshot(current);
3349
The Android Open Source Project0c908882009-03-03 19:32:16 -08003350 // Just in case the user opens bookmarks before a page finishes loading
3351 // so the current history item, and therefore the page, is null.
3352 if (null == url) {
3353 url = mLastEnteredUrl;
3354 // This can happen.
3355 if (null == url) {
3356 url = mSettings.getHomePage();
3357 }
3358 }
3359 // In case the web page has not yet received its associated title.
3360 if (title == null) {
3361 title = url;
3362 }
3363 intent.putExtra("title", title);
3364 intent.putExtra("url", url);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003365 intent.putExtra("thumbnail", thumbnail);
Leon Scroggins30444232009-09-04 18:36:20 -04003366 // Disable opening in a new window if we have maxed out the windows
Grace Kloba22ac16e2009-10-07 18:00:23 -07003367 intent.putExtra("disable_new_window", !mTabControl.canCreateNewTab());
Patrick Scott3918d442009-08-04 13:22:29 -04003368 intent.putExtra("touch_icon_url", current.getTouchIconUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003369 if (startWithHistory) {
3370 intent.putExtra(CombinedBookmarkHistoryActivity.STARTING_TAB,
3371 CombinedBookmarkHistoryActivity.HISTORY_TAB);
3372 }
3373 startActivityForResult(intent, COMBO_PAGE);
3374 }
3375
3376 // Called when loading from context menu or LOAD_URL message
3377 private void loadURL(WebView view, String url) {
3378 // In case the user enters nothing.
3379 if (url != null && url.length() != 0 && view != null) {
3380 url = smartUrlFilter(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003381 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003382 view.loadUrl(url);
3383 }
3384 }
3385 }
3386
The Android Open Source Project0c908882009-03-03 19:32:16 -08003387 private String smartUrlFilter(Uri inUri) {
3388 if (inUri != null) {
3389 return smartUrlFilter(inUri.toString());
3390 }
3391 return null;
3392 }
3393
Feng Qianb34f87a2009-03-24 21:27:26 -07003394 protected static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003395 "(?i)" + // switch on case insensitive matching
3396 "(" + // begin group for schema
3397 "(?:http|https|file):\\/\\/" +
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003398 "|(?:inline|data|about|content|javascript):" +
The Android Open Source Project0c908882009-03-03 19:32:16 -08003399 ")" +
3400 "(.*)" );
3401
3402 /**
3403 * Attempts to determine whether user input is a URL or search
3404 * terms. Anything with a space is passed to search.
3405 *
3406 * Converts to lowercase any mistakenly uppercased schema (i.e.,
3407 * "Http://" converts to "http://"
3408 *
3409 * @return Original or modified URL
3410 *
3411 */
3412 String smartUrlFilter(String url) {
3413
3414 String inUrl = url.trim();
3415 boolean hasSpace = inUrl.indexOf(' ') != -1;
3416
3417 Matcher matcher = ACCEPTED_URI_SCHEMA.matcher(inUrl);
3418 if (matcher.matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003419 // force scheme to lowercase
3420 String scheme = matcher.group(1);
3421 String lcScheme = scheme.toLowerCase();
3422 if (!lcScheme.equals(scheme)) {
Mitsuru Oshima123ecfb2009-05-18 19:11:14 -07003423 inUrl = lcScheme + matcher.group(2);
3424 }
3425 if (hasSpace) {
3426 inUrl = inUrl.replace(" ", "%20");
The Android Open Source Project0c908882009-03-03 19:32:16 -08003427 }
3428 return inUrl;
3429 }
3430 if (hasSpace) {
Satish Sampath565505b2009-05-29 15:37:27 +01003431 // FIXME: Is this the correct place to add to searches?
3432 // what if someone else calls this function?
3433 int shortcut = parseUrlShortcut(inUrl);
3434 if (shortcut != SHORTCUT_INVALID) {
3435 Browser.addSearchUrl(mResolver, inUrl);
3436 String query = inUrl.substring(2);
3437 switch (shortcut) {
3438 case SHORTCUT_GOOGLE_SEARCH:
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003439 return URLUtil.composeSearchUrl(query, QuickSearch_G, QUERY_PLACE_HOLDER);
Satish Sampath565505b2009-05-29 15:37:27 +01003440 case SHORTCUT_WIKIPEDIA_SEARCH:
3441 return URLUtil.composeSearchUrl(query, QuickSearch_W, QUERY_PLACE_HOLDER);
3442 case SHORTCUT_DICTIONARY_SEARCH:
3443 return URLUtil.composeSearchUrl(query, QuickSearch_D, QUERY_PLACE_HOLDER);
3444 case SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH:
The Android Open Source Project0c908882009-03-03 19:32:16 -08003445 // FIXME: we need location in this case
Satish Sampath565505b2009-05-29 15:37:27 +01003446 return URLUtil.composeSearchUrl(query, QuickSearch_L, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003447 }
3448 }
3449 } else {
3450 if (Regex.WEB_URL_PATTERN.matcher(inUrl).matches()) {
3451 return URLUtil.guessUrl(inUrl);
3452 }
3453 }
3454
3455 Browser.addSearchUrl(mResolver, inUrl);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003456 return URLUtil.composeSearchUrl(inUrl, QuickSearch_G, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003457 }
3458
Ben Murdochbff2d602009-07-01 20:19:05 +01003459 /* package */ void setShouldShowErrorConsole(boolean flag) {
3460 if (flag == mShouldShowErrorConsole) {
3461 // Nothing to do.
3462 return;
3463 }
3464
3465 mShouldShowErrorConsole = flag;
3466
Grace Kloba22ac16e2009-10-07 18:00:23 -07003467 ErrorConsoleView errorConsole = mTabControl.getCurrentTab()
3468 .getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01003469
3470 if (flag) {
3471 // Setting the show state of the console will cause it's the layout to be inflated.
3472 if (errorConsole.numberOfErrors() > 0) {
3473 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
3474 } else {
3475 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
3476 }
3477
3478 // Now we can add it to the main view.
3479 mErrorConsoleContainer.addView(errorConsole,
3480 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
3481 ViewGroup.LayoutParams.WRAP_CONTENT));
3482 } else {
3483 mErrorConsoleContainer.removeView(errorConsole);
3484 }
3485
3486 }
3487
Grace Kloba22ac16e2009-10-07 18:00:23 -07003488 boolean shouldShowErrorConsole() {
3489 return mShouldShowErrorConsole;
3490 }
3491
Grace Klobaeb6eef42009-09-15 17:56:32 -07003492 final static int LOCK_ICON_UNSECURE = 0;
3493 final static int LOCK_ICON_SECURE = 1;
3494 final static int LOCK_ICON_MIXED = 2;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003495
The Android Open Source Project0c908882009-03-03 19:32:16 -08003496 private BrowserSettings mSettings;
3497 private TabControl mTabControl;
3498 private ContentResolver mResolver;
3499 private FrameLayout mContentView;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003500 private View mCustomView;
3501 private FrameLayout mCustomViewContainer;
Andrei Popescuc9b55562009-07-07 10:51:15 +01003502 private WebChromeClient.CustomViewCallback mCustomViewCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003503
3504 // FIXME, temp address onPrepareMenu performance problem. When we move everything out of
3505 // view, we should rewrite this.
3506 private int mCurrentMenuState = 0;
3507 private int mMenuState = R.id.MAIN_MENU;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003508 private int mOldMenuState = EMPTY_MENU;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003509 private static final int EMPTY_MENU = -1;
3510 private Menu mMenu;
3511
3512 private FindDialog mFindDialog;
3513 // Used to prevent chording to result in firing two shortcuts immediately
3514 // one after another. Fixes bug 1211714.
3515 boolean mCanChord;
3516
3517 private boolean mInLoad;
3518 private boolean mIsNetworkUp;
Ben Murdochb7cc8b42009-09-28 10:59:09 +01003519 private boolean mDidStopLoad;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003520
The Android Open Source Project0c908882009-03-03 19:32:16 -08003521 private boolean mActivityInPause = true;
3522
3523 private boolean mMenuIsDown;
3524
The Android Open Source Project0c908882009-03-03 19:32:16 -08003525 private static boolean mInTrace;
3526
3527 // Performance probe
3528 private static final int[] SYSTEM_CPU_FORMAT = new int[] {
3529 Process.PROC_SPACE_TERM | Process.PROC_COMBINE,
3530 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time
3531 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time
3532 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time
3533 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time
3534 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time
3535 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time
3536 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG // 7: softirq time
3537 };
3538
3539 private long mStart;
3540 private long mProcessStart;
3541 private long mUserStart;
3542 private long mSystemStart;
3543 private long mIdleStart;
3544 private long mIrqStart;
3545
3546 private long mUiStart;
3547
3548 private Drawable mMixLockIcon;
3549 private Drawable mSecLockIcon;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003550
3551 /* hold a ref so we can auto-cancel if necessary */
3552 private AlertDialog mAlertDialog;
3553
3554 // Wait for credentials before loading google.com
3555 private ProgressDialog mCredsDlg;
3556
3557 // The up-to-date URL and title (these can be different from those stored
3558 // in WebView, since it takes some time for the information in WebView to
3559 // get updated)
3560 private String mUrl;
3561 private String mTitle;
3562
3563 // As PageInfo has different style for landscape / portrait, we have
3564 // to re-open it when configuration changed
3565 private AlertDialog mPageInfoDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07003566 private Tab mPageInfoView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003567 // If the Page-Info dialog is launched from the SSL-certificate-on-error
3568 // dialog, we should not just dismiss it, but should get back to the
3569 // SSL-certificate-on-error dialog. This flag is used to store this state
3570 private Boolean mPageInfoFromShowSSLCertificateOnError;
3571
3572 // as SSLCertificateOnError has different style for landscape / portrait,
3573 // we have to re-open it when configuration changed
3574 private AlertDialog mSSLCertificateOnErrorDialog;
3575 private WebView mSSLCertificateOnErrorView;
3576 private SslErrorHandler mSSLCertificateOnErrorHandler;
3577 private SslError mSSLCertificateOnErrorError;
3578
3579 // as SSLCertificate has different style for landscape / portrait, we
3580 // have to re-open it when configuration changed
3581 private AlertDialog mSSLCertificateDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07003582 private Tab mSSLCertificateView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003583
3584 // as HttpAuthentication has different style for landscape / portrait, we
3585 // have to re-open it when configuration changed
3586 private AlertDialog mHttpAuthenticationDialog;
3587 private HttpAuthHandler mHttpAuthHandler;
3588
3589 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
3590 new FrameLayout.LayoutParams(
3591 ViewGroup.LayoutParams.FILL_PARENT,
3592 ViewGroup.LayoutParams.FILL_PARENT);
Andrei Popescuadc008d2009-06-26 14:11:30 +01003593 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
3594 new FrameLayout.LayoutParams(
3595 ViewGroup.LayoutParams.FILL_PARENT,
3596 ViewGroup.LayoutParams.FILL_PARENT,
3597 Gravity.CENTER);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003598 // Google search
3599 final static String QuickSearch_G = "http://www.google.com/m?q=%s";
The Android Open Source Project0c908882009-03-03 19:32:16 -08003600 // Wikipedia search
3601 final static String QuickSearch_W = "http://en.wikipedia.org/w/index.php?search=%s&go=Go";
3602 // Dictionary search
3603 final static String QuickSearch_D = "http://dictionary.reference.com/search?q=%s";
3604 // Google Mobile Local search
3605 final static String QuickSearch_L = "http://www.google.com/m/search?site=local&q=%s&near=mountain+view";
3606
3607 final static String QUERY_PLACE_HOLDER = "%s";
3608
3609 // "source" parameter for Google search through search key
3610 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
3611 // "source" parameter for Google search through goto menu
3612 final static String GOOGLE_SEARCH_SOURCE_GOTO = "browser-goto";
3613 // "source" parameter for Google search through simplily type
3614 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
3615 // "source" parameter for Google search suggested by the browser
3616 final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
3617 // "source" parameter for Google search from unknown source
3618 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown";
3619
3620 private final static String LOGTAG = "browser";
3621
The Android Open Source Project0c908882009-03-03 19:32:16 -08003622 private String mLastEnteredUrl;
3623
3624 private PowerManager.WakeLock mWakeLock;
3625 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
3626
3627 private Toast mStopToast;
3628
Leon Scroggins68579392009-09-15 15:31:54 -04003629 private TitleBar mTitleBar;
Leon Scroggins81db3662009-06-04 17:45:11 -04003630
Ben Murdochbff2d602009-07-01 20:19:05 +01003631 private LinearLayout mErrorConsoleContainer = null;
3632 private boolean mShouldShowErrorConsole = false;
3633
The Android Open Source Project0c908882009-03-03 19:32:16 -08003634 // As the ids are dynamically created, we can't guarantee that they will
3635 // be in sequence, so this static array maps ids to a window number.
3636 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
3637 { R.id.window_one_menu_id, R.id.window_two_menu_id, R.id.window_three_menu_id,
3638 R.id.window_four_menu_id, R.id.window_five_menu_id, R.id.window_six_menu_id,
3639 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
3640
3641 // monitor platform changes
3642 private IntentFilter mNetworkStateChangedFilter;
3643 private BroadcastReceiver mNetworkStateIntentReceiver;
3644
Grace Klobab4da0ad2009-05-14 14:45:40 -07003645 private BroadcastReceiver mPackageInstallationReceiver;
3646
The Android Open Source Project0c908882009-03-03 19:32:16 -08003647 // activity requestCode
Nicolas Roard78a98e42009-05-11 13:34:17 +01003648 final static int COMBO_PAGE = 1;
3649 final static int DOWNLOAD_PAGE = 2;
3650 final static int PREFERENCES_PAGE = 3;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003651 final static int FILE_SELECTED = 4;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003652
Andrei Popescu540035d2009-09-18 18:59:20 +01003653 // the default <video> poster
3654 private Bitmap mDefaultVideoPoster;
3655 // the video progress view
3656 private View mVideoProgressView;
3657
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003658 /**
3659 * A UrlData class to abstract how the content will be set to WebView.
3660 * This base class uses loadUrl to show the content.
3661 */
3662 private static class UrlData {
3663 String mUrl;
Grace Kloba60e095c2009-06-16 11:50:55 -07003664 byte[] mPostData;
3665
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003666 UrlData(String url) {
3667 this.mUrl = url;
3668 }
Grace Kloba60e095c2009-06-16 11:50:55 -07003669
3670 void setPostData(byte[] postData) {
3671 mPostData = postData;
3672 }
3673
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003674 boolean isEmpty() {
3675 return mUrl == null || mUrl.length() == 0;
3676 }
3677
Mitsuru Oshima7944b7d2009-06-16 16:34:51 -07003678 public void loadIn(WebView webView) {
Grace Kloba60e095c2009-06-16 11:50:55 -07003679 if (mPostData != null) {
3680 webView.postUrl(mUrl, mPostData);
3681 } else {
3682 webView.loadUrl(mUrl);
3683 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003684 }
3685 };
3686
3687 /**
3688 * A subclass of UrlData class that can display inlined content using
3689 * {@link WebView#loadDataWithBaseURL(String, String, String, String, String)}.
3690 */
3691 private static class InlinedUrlData extends UrlData {
3692 InlinedUrlData(String inlined, String mimeType, String encoding, String failUrl) {
3693 super(failUrl);
3694 mInlined = inlined;
3695 mMimeType = mimeType;
3696 mEncoding = encoding;
3697 }
3698 String mMimeType;
3699 String mInlined;
3700 String mEncoding;
Mitsuru Oshima7944b7d2009-06-16 16:34:51 -07003701 @Override
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003702 boolean isEmpty() {
Ben Murdochbff2d602009-07-01 20:19:05 +01003703 return mInlined == null || mInlined.length() == 0 || super.isEmpty();
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003704 }
3705
Mitsuru Oshima7944b7d2009-06-16 16:34:51 -07003706 @Override
3707 public void loadIn(WebView webView) {
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003708 webView.loadDataWithBaseURL(null, mInlined, mMimeType, mEncoding, mUrl);
3709 }
3710 }
3711
Leon Scroggins1f005d32009-08-10 17:36:42 -04003712 /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003713}