blob: 59378810e2fb74618958ea2d0c37ac3b741e1c7a [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;
37import android.content.DialogInterface.OnCancelListener;
Grace Klobab4da0ad2009-05-14 14:45:40 -070038import android.content.pm.PackageInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.content.pm.PackageManager;
40import android.content.pm.ResolveInfo;
41import android.content.res.AssetManager;
42import android.content.res.Configuration;
43import android.content.res.Resources;
44import android.database.Cursor;
45import android.database.sqlite.SQLiteDatabase;
46import android.database.sqlite.SQLiteException;
47import android.graphics.Bitmap;
Andrei Popescu540035d2009-09-18 18:59:20 +010048import android.graphics.BitmapFactory;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049import android.graphics.Canvas;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050import android.graphics.DrawFilter;
51import android.graphics.Paint;
52import android.graphics.PaintFlagsDrawFilter;
53import android.graphics.Picture;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040054import android.graphics.PixelFormat;
55import android.graphics.Rect;
The Android Open Source Project0c908882009-03-03 19:32:16 -080056import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080057import android.hardware.SensorListener;
58import android.hardware.SensorManager;
59import android.net.ConnectivityManager;
Patrick Scotteb6ab2a2009-09-16 10:00:17 -040060import android.net.NetworkInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080061import android.net.Uri;
62import android.net.WebAddress;
63import android.net.http.EventHandler;
64import android.net.http.SslCertificate;
65import android.net.http.SslError;
66import android.os.AsyncTask;
67import android.os.Bundle;
68import android.os.Debug;
69import android.os.Environment;
70import android.os.Handler;
71import android.os.IBinder;
72import android.os.Message;
73import android.os.PowerManager;
74import android.os.Process;
75import android.os.RemoteException;
76import android.os.ServiceManager;
77import android.os.SystemClock;
The Android Open Source Project0c908882009-03-03 19:32:16 -080078import android.provider.Browser;
Cary Clark5e335a32009-09-22 14:53:11 -040079import android.provider.ContactsContract;
80import android.provider.ContactsContract.Intents.Insert;
The Android Open Source Project0c908882009-03-03 19:32:16 -080081import android.provider.Downloads;
82import android.provider.MediaStore;
The Android Open Source Project0c908882009-03-03 19:32:16 -080083import android.text.IClipboard;
84import android.text.TextUtils;
85import android.text.format.DateFormat;
86import android.text.util.Regex;
Leon Scrogginsb94bf272009-09-25 15:22:08 -040087import android.util.AttributeSet;
The Android Open Source Project0c908882009-03-03 19:32:16 -080088import android.util.Log;
89import android.view.ContextMenu;
90import android.view.Gravity;
91import android.view.KeyEvent;
92import android.view.LayoutInflater;
93import android.view.Menu;
94import android.view.MenuInflater;
95import android.view.MenuItem;
96import android.view.View;
97import android.view.ViewGroup;
98import android.view.Window;
99import android.view.WindowManager;
100import android.view.ContextMenu.ContextMenuInfo;
101import android.view.MenuItem.OnMenuItemClickListener;
102import android.view.animation.AlphaAnimation;
103import android.view.animation.Animation;
104import android.view.animation.AnimationSet;
105import android.view.animation.DecelerateInterpolator;
106import android.view.animation.ScaleAnimation;
107import android.view.animation.TranslateAnimation;
108import android.webkit.CookieManager;
109import android.webkit.CookieSyncManager;
110import android.webkit.DownloadListener;
Steve Block2bc69912009-07-30 14:45:13 +0100111import android.webkit.GeolocationPermissions;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800112import android.webkit.HttpAuthHandler;
Grace Klobab4da0ad2009-05-14 14:45:40 -0700113import android.webkit.PluginManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114import android.webkit.SslErrorHandler;
115import android.webkit.URLUtil;
Leon Clarkecb6cc862009-09-29 18:35:13 +0100116import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800117import android.webkit.WebChromeClient;
Andrei Popescuc9b55562009-07-07 10:51:15 +0100118import android.webkit.WebChromeClient.CustomViewCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119import android.webkit.WebHistoryItem;
120import android.webkit.WebIconDatabase;
Ben Murdoch092dd5d2009-04-22 12:34:12 +0100121import android.webkit.WebStorage;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800122import android.webkit.WebView;
123import android.webkit.WebViewClient;
124import android.widget.EditText;
125import android.widget.FrameLayout;
126import android.widget.LinearLayout;
127import android.widget.TextView;
128import android.widget.Toast;
129
130import java.io.BufferedOutputStream;
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400131import java.io.ByteArrayOutputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132import java.io.File;
133import java.io.FileInputStream;
134import java.io.FileOutputStream;
135import java.io.IOException;
136import java.io.InputStream;
137import java.net.MalformedURLException;
138import java.net.URI;
Dianne Hackborn99189432009-06-17 18:06:18 -0700139import java.net.URISyntaxException;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800140import java.net.URL;
141import java.net.URLEncoder;
142import java.text.ParseException;
143import java.util.Date;
144import java.util.Enumeration;
145import java.util.HashMap;
Patrick Scott37911c72009-03-24 18:02:58 -0700146import java.util.LinkedList;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800147import java.util.Vector;
148import java.util.regex.Matcher;
149import java.util.regex.Pattern;
150import java.util.zip.ZipEntry;
151import java.util.zip.ZipFile;
152
153public class BrowserActivity extends Activity
Grace Kloba5942df02009-09-18 11:48:29 -0700154 implements View.OnCreateContextMenuListener,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800155 DownloadListener {
156
Dave Bort31a6d1c2009-04-13 15:56:49 -0700157 /* Define some aliases to make these debugging flags easier to refer to.
158 * This file imports android.provider.Browser, so we can't just refer to "Browser.DEBUG".
159 */
160 private final static boolean DEBUG = com.android.browser.Browser.DEBUG;
161 private final static boolean LOGV_ENABLED = com.android.browser.Browser.LOGV_ENABLED;
162 private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
163
The Android Open Source Project0c908882009-03-03 19:32:16 -0800164 private IGoogleLoginService mGls = null;
165 private ServiceConnection mGlsConnection = null;
166
167 private SensorManager mSensorManager = null;
168
Satish Sampath565505b2009-05-29 15:37:27 +0100169 // These are single-character shortcuts for searching popular sources.
170 private static final int SHORTCUT_INVALID = 0;
171 private static final int SHORTCUT_GOOGLE_SEARCH = 1;
172 private static final int SHORTCUT_WIKIPEDIA_SEARCH = 2;
173 private static final int SHORTCUT_DICTIONARY_SEARCH = 3;
174 private static final int SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH = 4;
175
The Android Open Source Project0c908882009-03-03 19:32:16 -0800176 /* Whitelisted webpages
177 private static HashSet<String> sWhiteList;
178
179 static {
180 sWhiteList = new HashSet<String>();
181 sWhiteList.add("cnn.com/");
182 sWhiteList.add("espn.go.com/");
183 sWhiteList.add("nytimes.com/");
184 sWhiteList.add("engadget.com/");
185 sWhiteList.add("yahoo.com/");
186 sWhiteList.add("msn.com/");
187 sWhiteList.add("amazon.com/");
188 sWhiteList.add("consumerist.com/");
189 sWhiteList.add("google.com/m/news");
190 }
191 */
192
193 private void setupHomePage() {
194 final Runnable getAccount = new Runnable() {
195 public void run() {
196 // Lower priority
197 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
198 // get the default home page
199 String homepage = mSettings.getHomePage();
200
201 try {
202 if (mGls == null) return;
203
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700204 if (!homepage.startsWith("http://www.google.")) return;
205 if (homepage.indexOf('?') == -1) return;
206
The Android Open Source Project0c908882009-03-03 19:32:16 -0800207 String hostedUser = mGls.getAccount(GoogleLoginServiceConstants.PREFER_HOSTED);
208 String googleUser = mGls.getAccount(GoogleLoginServiceConstants.REQUIRE_GOOGLE);
209
210 // three cases:
211 //
212 // hostedUser == googleUser
213 // The device has only a google account
214 //
215 // hostedUser != googleUser
216 // The device has a hosted account and a google account
217 //
218 // hostedUser != null, googleUser == null
219 // The device has only a hosted account (so far)
220
221 // developers might have no accounts at all
222 if (hostedUser == null) return;
223
224 if (googleUser == null || !hostedUser.equals(googleUser)) {
225 String domain = hostedUser.substring(hostedUser.lastIndexOf('@')+1);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700226 homepage = homepage.replace("?", "/a/" + domain + "?");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800227 }
228 } catch (RemoteException ignore) {
229 // Login service died; carry on
230 } catch (RuntimeException ignore) {
231 // Login service died; carry on
232 } finally {
233 finish(homepage);
234 }
235 }
236
237 private void finish(final String homepage) {
238 mHandler.post(new Runnable() {
239 public void run() {
240 mSettings.setHomePage(BrowserActivity.this, homepage);
241 resumeAfterCredentials();
242
243 // as this is running in a separate thread,
244 // BrowserActivity's onDestroy() may have been called,
245 // which also calls unbindService().
246 if (mGlsConnection != null) {
247 // we no longer need to keep GLS open
248 unbindService(mGlsConnection);
249 mGlsConnection = null;
250 }
251 } });
252 } };
253
254 final boolean[] done = { false };
255
256 // Open a connection to the Google Login Service. The first
257 // time the connection is established, set up the homepage depending on
258 // the account in a background thread.
259 mGlsConnection = new ServiceConnection() {
260 public void onServiceConnected(ComponentName className, IBinder service) {
261 mGls = IGoogleLoginService.Stub.asInterface(service);
262 if (done[0] == false) {
263 done[0] = true;
264 Thread account = new Thread(getAccount);
265 account.setName("GLSAccount");
266 account.start();
267 }
268 }
269 public void onServiceDisconnected(ComponentName className) {
270 mGls = null;
271 }
272 };
273
274 bindService(GoogleLoginServiceConstants.SERVICE_INTENT,
275 mGlsConnection, Context.BIND_AUTO_CREATE);
276 }
277
Cary Clarka9771242009-08-11 16:42:26 -0400278 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800279 @Override
280 public Void doInBackground(File... files) {
281 if (files != null) {
282 for (File f : files) {
Cary Clarkd6be1752009-08-12 12:56:42 -0400283 if (!f.delete()) {
284 Log.e(LOGTAG, f.getPath() + " was not deleted");
285 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800286 }
287 }
288 return null;
289 }
290 }
291
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400292 /**
293 * This layout holds everything you see below the status bar, including the
294 * error console, the custom view container, and the webviews.
295 */
296 private FrameLayout mBrowserFrameLayout;
Leon Scroggins81db3662009-06-04 17:45:11 -0400297
The Android Open Source Project0c908882009-03-03 19:32:16 -0800298 @Override public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700299 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800300 Log.v(LOGTAG, this + " onStart");
301 }
302 super.onCreate(icicle);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800303 // test the browser in OpenGL
304 // requestWindowFeature(Window.FEATURE_OPENGL);
305
306 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
307
308 mResolver = getContentResolver();
309
Grace Kloba0923d692009-09-23 21:37:25 -0700310 // If this was a web search request, pass it on to the default web
311 // search provider and finish this activity.
312 if (handleWebSearchIntent(getIntent())) {
313 finish();
314 return;
315 }
316
The Android Open Source Project0c908882009-03-03 19:32:16 -0800317 //
318 // start MASF proxy service
319 //
320 //Intent proxyServiceIntent = new Intent();
321 //proxyServiceIntent.setComponent
322 // (new ComponentName(
323 // "com.android.masfproxyservice",
324 // "com.android.masfproxyservice.MasfProxyService"));
325 //startService(proxyServiceIntent, null);
326
327 mSecLockIcon = Resources.getSystem().getDrawable(
328 android.R.drawable.ic_secure);
329 mMixLockIcon = Resources.getSystem().getDrawable(
330 android.R.drawable.ic_partial_secure);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800331
Leon Scroggins81db3662009-06-04 17:45:11 -0400332 FrameLayout frameLayout = (FrameLayout) getWindow().getDecorView()
333 .findViewById(com.android.internal.R.id.content);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400334 mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(this)
335 .inflate(R.layout.custom_screen, null);
336 mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(
337 R.id.main_content);
338 mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout
339 .findViewById(R.id.error_console);
340 mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
341 .findViewById(R.id.fullscreen_custom_content);
342 frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
Leon Scroggins68579392009-09-15 15:31:54 -0400343 mTitleBar = new TitleBar(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800344
345 // Create the tab control and our initial tab
346 mTabControl = new TabControl(this);
347
348 // Open the icon database and retain all the bookmark urls for favicons
349 retainIconsOnStartup();
350
351 // Keep a settings instance handy.
352 mSettings = BrowserSettings.getInstance();
353 mSettings.setTabControl(mTabControl);
354 mSettings.loadFromDb(this);
355
356 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
357 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
358
Grace Klobaa34f6862009-07-31 16:28:17 -0700359 /* enables registration for changes in network status from
360 http stack */
361 mNetworkStateChangedFilter = new IntentFilter();
362 mNetworkStateChangedFilter.addAction(
363 ConnectivityManager.CONNECTIVITY_ACTION);
364 mNetworkStateIntentReceiver = new BroadcastReceiver() {
365 @Override
366 public void onReceive(Context context, Intent intent) {
367 if (intent.getAction().equals(
368 ConnectivityManager.CONNECTIVITY_ACTION)) {
Robert Greenwalt7d899d62009-10-23 10:33:48 -0700369 boolean noConnectivity = intent.getBooleanExtra(
370 ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
371 onNetworkToggle(!noConnectivity);
Grace Klobaa34f6862009-07-31 16:28:17 -0700372 }
373 }
374 };
375
Grace Kloba615c6c92009-08-03 10:22:44 -0700376 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
377 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
378 filter.addDataScheme("package");
379 mPackageInstallationReceiver = new BroadcastReceiver() {
380 @Override
381 public void onReceive(Context context, Intent intent) {
382 final String action = intent.getAction();
383 final String packageName = intent.getData()
384 .getSchemeSpecificPart();
385 final boolean replacing = intent.getBooleanExtra(
386 Intent.EXTRA_REPLACING, false);
387 if (Intent.ACTION_PACKAGE_REMOVED.equals(action) && replacing) {
388 // if it is replacing, refreshPlugins() when adding
389 return;
390 }
391 PackageManager pm = BrowserActivity.this.getPackageManager();
392 PackageInfo pkgInfo = null;
393 try {
394 pkgInfo = pm.getPackageInfo(packageName,
395 PackageManager.GET_PERMISSIONS);
396 } catch (PackageManager.NameNotFoundException e) {
397 return;
398 }
399 if (pkgInfo != null) {
400 String permissions[] = pkgInfo.requestedPermissions;
401 if (permissions == null) {
402 return;
403 }
404 boolean permissionOk = false;
405 for (String permit : permissions) {
406 if (PluginManager.PLUGIN_PERMISSION.equals(permit)) {
407 permissionOk = true;
408 break;
409 }
410 }
411 if (permissionOk) {
412 PluginManager.getInstance(BrowserActivity.this)
413 .refreshPlugins(
414 Intent.ACTION_PACKAGE_ADDED
415 .equals(action));
416 }
417 }
418 }
419 };
420 registerReceiver(mPackageInstallationReceiver, filter);
421
The Android Open Source Project0c908882009-03-03 19:32:16 -0800422 if (!mTabControl.restoreState(icicle)) {
423 // clear up the thumbnail directory if we can't restore the state as
424 // none of the files in the directory are referenced any more.
425 new ClearThumbnails().execute(
426 mTabControl.getThumbnailDir().listFiles());
Grace Klobaaab3f092009-07-30 12:29:51 -0700427 // there is no quit on Android. But if we can't restore the state,
428 // we can treat it as a new Browser, remove the old session cookies.
429 CookieManager.getInstance().removeSessionCookie();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800430 final Intent intent = getIntent();
431 final Bundle extra = intent.getExtras();
432 // Create an initial tab.
433 // If the intent is ACTION_VIEW and data is not null, the Browser is
434 // invoked to view the content by another application. In this case,
435 // the tab will be close when exit.
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700436 UrlData urlData = getUrlDataFromIntent(intent);
437
The Android Open Source Project0c908882009-03-03 19:32:16 -0800438 final TabControl.Tab t = mTabControl.createNewTab(
439 Intent.ACTION_VIEW.equals(intent.getAction()) &&
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700440 intent.getData() != null,
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700441 intent.getStringExtra(Browser.EXTRA_APPLICATION_ID), urlData.mUrl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800442 mTabControl.setCurrentTab(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800443 attachTabToContentView(t);
444 WebView webView = t.getWebView();
445 if (extra != null) {
446 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
447 if (scale > 0 && scale <= 1000) {
448 webView.setInitialScale(scale);
449 }
450 }
451 // If we are not restoring from an icicle, then there is a high
452 // likely hood this is the first run. So, check to see if the
453 // homepage needs to be configured and copy any plugins from our
454 // asset directory to the data partition.
455 if ((extra == null || !extra.getBoolean("testing"))
456 && !mSettings.isLoginInitialized()) {
457 setupHomePage();
458 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800459
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700460 if (urlData.isEmpty()) {
Leon Scroggins30444232009-09-04 18:36:20 -0400461 if (mSettings.isLoginInitialized()) {
462 webView.loadUrl(mSettings.getHomePage());
463 } else {
464 waitForCredentials();
465 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800466 } else {
Grace Kloba81678d92009-06-30 07:09:56 -0700467 if (extra != null) {
468 urlData.setPostData(extra
469 .getByteArray(Browser.EXTRA_POST_DATA));
470 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700471 urlData.loadIn(webView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800472 }
473 } else {
474 // TabControl.restoreState() will create a new tab even if
Leon Scroggins1f005d32009-08-10 17:36:42 -0400475 // restoring the state fails.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800476 attachTabToContentView(mTabControl.getCurrentTab());
477 }
Grace Kloba615c6c92009-08-03 10:22:44 -0700478
Feng Qianb3c02da2009-06-29 15:58:08 -0700479 // Read JavaScript flags if it exists.
480 String jsFlags = mSettings.getJsFlags();
481 if (jsFlags.trim().length() != 0) {
482 mTabControl.getCurrentWebView().setJsFlags(jsFlags);
483 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800484 }
485
486 @Override
487 protected void onNewIntent(Intent intent) {
488 TabControl.Tab current = mTabControl.getCurrentTab();
489 // When a tab is closed on exit, the current tab index is set to -1.
490 // Reset before proceed as Browser requires the current tab to be set.
491 if (current == null) {
492 // Try to reset the tab in case the index was incorrect.
493 current = mTabControl.getTab(0);
494 if (current == null) {
495 // No tabs at all so just ignore this intent.
496 return;
497 }
498 mTabControl.setCurrentTab(current);
499 attachTabToContentView(current);
500 resetTitleAndIcon(current.getWebView());
501 }
502 final String action = intent.getAction();
503 final int flags = intent.getFlags();
504 if (Intent.ACTION_MAIN.equals(action) ||
505 (flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
506 // just resume the browser
507 return;
508 }
509 if (Intent.ACTION_VIEW.equals(action)
510 || Intent.ACTION_SEARCH.equals(action)
511 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
512 || Intent.ACTION_WEB_SEARCH.equals(action)) {
Satish Sampath565505b2009-05-29 15:37:27 +0100513 // If this was a search request (e.g. search query directly typed into the address bar),
514 // pass it on to the default web search provider.
515 if (handleWebSearchIntent(intent)) {
516 return;
517 }
518
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700519 UrlData urlData = getUrlDataFromIntent(intent);
520 if (urlData.isEmpty()) {
521 urlData = new UrlData(mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800522 }
Grace Kloba81678d92009-06-30 07:09:56 -0700523 urlData.setPostData(intent
524 .getByteArrayExtra(Browser.EXTRA_POST_DATA));
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700525
Grace Klobacc634032009-07-28 15:58:19 -0700526 final String appId = intent
527 .getStringExtra(Browser.EXTRA_APPLICATION_ID);
528 if (Intent.ACTION_VIEW.equals(action)
529 && !getPackageName().equals(appId)
530 && (flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
Patrick Scottcd115892009-07-16 09:42:58 -0400531 TabControl.Tab appTab = mTabControl.getTabFromId(appId);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700532 if (appTab != null) {
533 Log.i(LOGTAG, "Reusing tab for " + appId);
534 // Dismiss the subwindow if applicable.
535 dismissSubWindow(appTab);
536 // Since we might kill the WebView, remove it from the
537 // content view first.
538 removeTabFromContentView(appTab);
539 // Recreate the main WebView after destroying the old one.
540 // If the WebView has the same original url and is on that
541 // page, it can be reused.
542 boolean needsLoad =
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700543 mTabControl.recreateWebView(appTab, urlData.mUrl);
Ben Murdochbff2d602009-07-01 20:19:05 +0100544
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700545 if (current != appTab) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400546 switchToTab(mTabControl.getTabIndex(appTab));
547 if (needsLoad) {
548 urlData.loadIn(appTab.getWebView());
549 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700550 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400551 // If the tab was the current tab, we have to attach
552 // it to the view system again.
553 attachTabToContentView(appTab);
554 if (needsLoad) {
555 urlData.loadIn(appTab.getWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700556 }
557 }
558 return;
Patrick Scottcd115892009-07-16 09:42:58 -0400559 } else {
560 // No matching application tab, try to find a regular tab
561 // with a matching url.
562 appTab = mTabControl.findUnusedTabWithUrl(urlData.mUrl);
Leon Scroggins25515f82009-08-19 15:31:58 -0400563 if (appTab != null) {
564 if (current != appTab) {
565 switchToTab(mTabControl.getTabIndex(appTab));
566 }
567 // Otherwise, we are already viewing the correct tab.
Patrick Scottcd115892009-07-16 09:42:58 -0400568 } else {
569 // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
570 // will be opened in a new tab unless we have reached
571 // MAX_TABS. Then the url will be opened in the current
572 // tab. If a new tab is created, it will have "true" for
573 // exit on close.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400574 openTabAndShow(urlData, true, appId);
Patrick Scottcd115892009-07-16 09:42:58 -0400575 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700576 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800577 } else {
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700578 if ("about:debug".equals(urlData.mUrl)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800579 mSettings.toggleDebugSettings();
580 return;
581 }
Leon Scroggins1f005d32009-08-10 17:36:42 -0400582 // Get rid of the subwindow if it exists
583 dismissSubWindow(current);
584 urlData.loadIn(current.getWebView());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800585 }
586 }
587 }
588
Satish Sampath565505b2009-05-29 15:37:27 +0100589 private int parseUrlShortcut(String url) {
590 if (url == null) return SHORTCUT_INVALID;
591
592 // FIXME: quick search, need to be customized by setting
593 if (url.length() > 2 && url.charAt(1) == ' ') {
594 switch (url.charAt(0)) {
595 case 'g': return SHORTCUT_GOOGLE_SEARCH;
596 case 'w': return SHORTCUT_WIKIPEDIA_SEARCH;
597 case 'd': return SHORTCUT_DICTIONARY_SEARCH;
598 case 'l': return SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH;
599 }
600 }
601 return SHORTCUT_INVALID;
602 }
603
604 /**
605 * Launches the default web search activity with the query parameters if the given intent's data
606 * are identified as plain search terms and not URLs/shortcuts.
607 * @return true if the intent was handled and web search activity was launched, false if not.
608 */
609 private boolean handleWebSearchIntent(Intent intent) {
610 if (intent == null) return false;
611
612 String url = null;
613 final String action = intent.getAction();
614 if (Intent.ACTION_VIEW.equals(action)) {
Grace Kloba1e705052009-09-29 13:13:36 -0700615 Uri data = intent.getData();
616 if (data != null) url = data.toString();
Satish Sampath565505b2009-05-29 15:37:27 +0100617 } else if (Intent.ACTION_SEARCH.equals(action)
618 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
619 || Intent.ACTION_WEB_SEARCH.equals(action)) {
620 url = intent.getStringExtra(SearchManager.QUERY);
621 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100622 return handleWebSearchRequest(url, intent.getBundleExtra(SearchManager.APP_DATA),
623 intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
Satish Sampath565505b2009-05-29 15:37:27 +0100624 }
625
626 /**
627 * Launches the default web search activity with the query parameters if the given url string
628 * was identified as plain search terms and not URL/shortcut.
629 * @return true if the request was handled and web search activity was launched, false if not.
630 */
Bjorn Bringert04851702009-09-22 10:36:01 +0100631 private boolean handleWebSearchRequest(String inUrl, Bundle appData, String extraData) {
Satish Sampath565505b2009-05-29 15:37:27 +0100632 if (inUrl == null) return false;
633
634 // In general, we shouldn't modify URL from Intent.
635 // But currently, we get the user-typed URL from search box as well.
636 String url = fixUrl(inUrl).trim();
637
638 // URLs and site specific search shortcuts are handled by the regular flow of control, so
639 // return early.
640 if (Regex.WEB_URL_PATTERN.matcher(url).matches()
Satish Sampathbc5b9f32009-06-04 18:21:40 +0100641 || ACCEPTED_URI_SCHEMA.matcher(url).matches()
Satish Sampath565505b2009-05-29 15:37:27 +0100642 || parseUrlShortcut(url) != SHORTCUT_INVALID) {
643 return false;
644 }
645
646 Browser.updateVisitedHistory(mResolver, url, false);
647 Browser.addSearchUrl(mResolver, url);
648
649 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
650 intent.addCategory(Intent.CATEGORY_DEFAULT);
651 intent.putExtra(SearchManager.QUERY, url);
Satish Sampath15e9f2d2009-06-23 22:29:49 +0100652 if (appData != null) {
653 intent.putExtra(SearchManager.APP_DATA, appData);
654 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100655 if (extraData != null) {
656 intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
657 }
Grace Klobacc634032009-07-28 15:58:19 -0700658 intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName());
Satish Sampath565505b2009-05-29 15:37:27 +0100659 startActivity(intent);
660
661 return true;
662 }
663
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700664 private UrlData getUrlDataFromIntent(Intent intent) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800665 String url = null;
666 if (intent != null) {
667 final String action = intent.getAction();
668 if (Intent.ACTION_VIEW.equals(action)) {
669 url = smartUrlFilter(intent.getData());
670 if (url != null && url.startsWith("content:")) {
671 /* Append mimetype so webview knows how to display */
672 String mimeType = intent.resolveType(getContentResolver());
673 if (mimeType != null) {
674 url += "?" + mimeType;
675 }
676 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700677 if ("inline:".equals(url)) {
678 return new InlinedUrlData(
679 intent.getStringExtra(Browser.EXTRA_INLINE_CONTENT),
680 intent.getType(),
681 intent.getStringExtra(Browser.EXTRA_INLINE_ENCODING),
682 intent.getStringExtra(Browser.EXTRA_INLINE_FAILURL));
683 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800684 } else if (Intent.ACTION_SEARCH.equals(action)
685 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
686 || Intent.ACTION_WEB_SEARCH.equals(action)) {
687 url = intent.getStringExtra(SearchManager.QUERY);
688 if (url != null) {
689 mLastEnteredUrl = url;
690 // Don't add Urls, just search terms.
691 // Urls will get added when the page is loaded.
692 if (!Regex.WEB_URL_PATTERN.matcher(url).matches()) {
693 Browser.updateVisitedHistory(mResolver, url, false);
694 }
695 // In general, we shouldn't modify URL from Intent.
696 // But currently, we get the user-typed URL from search box as well.
697 url = fixUrl(url);
698 url = smartUrlFilter(url);
699 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
700 if (url.contains(searchSource)) {
701 String source = null;
702 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
703 if (appData != null) {
704 source = appData.getString(SearchManager.SOURCE);
705 }
706 if (TextUtils.isEmpty(source)) {
707 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
708 }
709 url = url.replace(searchSource, "&source=android-"+source+"&");
710 }
711 }
712 }
713 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700714 return new UrlData(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800715 }
716
717 /* package */ static String fixUrl(String inUrl) {
Cary Clark652ff872009-09-10 13:34:44 -0400718 // FIXME: Converting the url to lower case
719 // duplicates functionality in smartUrlFilter().
720 // However, changing all current callers of fixUrl to
721 // call smartUrlFilter in addition may have unwanted
722 // consequences, and is deferred for now.
723 int colon = inUrl.indexOf(':');
724 boolean allLower = true;
725 for (int index = 0; index < colon; index++) {
726 char ch = inUrl.charAt(index);
727 if (!Character.isLetter(ch)) {
728 break;
729 }
730 allLower &= Character.isLowerCase(ch);
731 if (index == colon - 1 && !allLower) {
732 inUrl = inUrl.substring(0, colon).toLowerCase()
733 + inUrl.substring(colon);
734 }
735 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800736 if (inUrl.startsWith("http://") || inUrl.startsWith("https://"))
737 return inUrl;
738 if (inUrl.startsWith("http:") ||
739 inUrl.startsWith("https:")) {
740 if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) {
741 inUrl = inUrl.replaceFirst("/", "//");
742 } else inUrl = inUrl.replaceFirst(":", "://");
743 }
744 return inUrl;
745 }
746
747 /**
748 * Looking for the pattern like this
749 *
750 * *
751 * * *
752 * *** * *******
753 * * *
754 * * *
755 * *
756 */
757 private final SensorListener mSensorListener = new SensorListener() {
758 private long mLastGestureTime;
759 private float[] mPrev = new float[3];
760 private float[] mPrevDiff = new float[3];
761 private float[] mDiff = new float[3];
762 private float[] mRevertDiff = new float[3];
763
764 public void onSensorChanged(int sensor, float[] values) {
765 boolean show = false;
766 float[] diff = new float[3];
767
768 for (int i = 0; i < 3; i++) {
769 diff[i] = values[i] - mPrev[i];
770 if (Math.abs(diff[i]) > 1) {
771 show = true;
772 }
773 if ((diff[i] > 1.0 && mDiff[i] < 0.2)
774 || (diff[i] < -1.0 && mDiff[i] > -0.2)) {
775 // start track when there is a big move, or revert
776 mRevertDiff[i] = mDiff[i];
777 mDiff[i] = 0;
778 } else if (diff[i] > -0.2 && diff[i] < 0.2) {
779 // reset when it is flat
780 mDiff[i] = mRevertDiff[i] = 0;
781 }
782 mDiff[i] += diff[i];
783 mPrevDiff[i] = diff[i];
784 mPrev[i] = values[i];
785 }
786
787 if (false) {
788 // only shows if we think the delta is big enough, in an attempt
789 // to detect "serious" moves left/right or up/down
790 Log.d("BrowserSensorHack", "sensorChanged " + sensor + " ("
791 + values[0] + ", " + values[1] + ", " + values[2] + ")"
792 + " diff(" + diff[0] + " " + diff[1] + " " + diff[2]
793 + ")");
794 Log.d("BrowserSensorHack", " mDiff(" + mDiff[0] + " "
795 + mDiff[1] + " " + mDiff[2] + ")" + " mRevertDiff("
796 + mRevertDiff[0] + " " + mRevertDiff[1] + " "
797 + mRevertDiff[2] + ")");
798 }
799
800 long now = android.os.SystemClock.uptimeMillis();
801 if (now - mLastGestureTime > 1000) {
802 mLastGestureTime = 0;
803
804 float y = mDiff[1];
805 float z = mDiff[2];
806 float ay = Math.abs(y);
807 float az = Math.abs(z);
808 float ry = mRevertDiff[1];
809 float rz = mRevertDiff[2];
810 float ary = Math.abs(ry);
811 float arz = Math.abs(rz);
812 boolean gestY = ay > 2.5f && ary > 1.0f && ay > ary;
813 boolean gestZ = az > 3.5f && arz > 1.0f && az > arz;
814
815 if ((gestY || gestZ) && !(gestY && gestZ)) {
816 WebView view = mTabControl.getCurrentWebView();
817
818 if (view != null) {
819 if (gestZ) {
820 if (z < 0) {
821 view.zoomOut();
822 } else {
823 view.zoomIn();
824 }
825 } else {
826 view.flingScroll(0, Math.round(y * 100));
827 }
828 }
829 mLastGestureTime = now;
830 }
831 }
832 }
833
834 public void onAccuracyChanged(int sensor, int accuracy) {
835 // TODO Auto-generated method stub
836
837 }
838 };
839
840 @Override protected void onResume() {
841 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700842 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800843 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
844 }
845
846 if (!mActivityInPause) {
847 Log.e(LOGTAG, "BrowserActivity is already resumed.");
848 return;
849 }
850
Mike Reed7bfa63b2009-05-28 11:08:32 -0400851 mTabControl.resumeCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800852 mActivityInPause = false;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400853 resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800854
855 if (mWakeLock.isHeld()) {
856 mHandler.removeMessages(RELEASE_WAKELOCK);
857 mWakeLock.release();
858 }
859
860 if (mCredsDlg != null) {
861 if (!mHandler.hasMessages(CANCEL_CREDS_REQUEST)) {
862 // In case credential request never comes back
863 mHandler.sendEmptyMessageDelayed(CANCEL_CREDS_REQUEST, 6000);
864 }
865 }
866
867 registerReceiver(mNetworkStateIntentReceiver,
868 mNetworkStateChangedFilter);
869 WebView.enablePlatformNotifications();
870
871 if (mSettings.doFlick()) {
872 if (mSensorManager == null) {
873 mSensorManager = (SensorManager) getSystemService(
874 Context.SENSOR_SERVICE);
875 }
876 mSensorManager.registerListener(mSensorListener,
877 SensorManager.SENSOR_ACCELEROMETER,
878 SensorManager.SENSOR_DELAY_FASTEST);
879 } else {
880 mSensorManager = null;
881 }
882 }
883
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400884 /**
885 * Since the actual title bar is embedded in the WebView, and removing it
886 * would change its appearance, create a temporary title bar to go at
887 * the top of the screen while the menu is open.
888 */
889 private TitleBar mFakeTitleBar;
890
891 /**
Leon Scrogginsd8fd2fc2009-09-16 11:12:09 -0400892 * Holder for the fake title bar. It will have a foreground shadow, as well
893 * as a white background, so the fake title bar looks like the real one.
894 */
895 private ViewGroup mFakeTitleBarHolder;
896
897 /**
898 * Layout parameters for the fake title bar within mFakeTitleBarHolder
899 */
900 private FrameLayout.LayoutParams mFakeTitleBarParams
901 = new FrameLayout.LayoutParams(
Leon Scrogginsc01e4a82009-09-16 14:41:00 -0400902 ViewGroup.LayoutParams.FILL_PARENT,
Leon Scrogginsd8fd2fc2009-09-16 11:12:09 -0400903 ViewGroup.LayoutParams.WRAP_CONTENT);
904 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400905 * Keeps track of whether the options menu is open. This is important in
906 * determining whether to show or hide the title bar overlay.
907 */
908 private boolean mOptionsMenuOpen;
909
910 /**
911 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
912 * of whether the configuration has changed. The first onMenuOpened call
913 * after a configuration change is simply a reopening of the same menu
914 * (i.e. mIconView did not change).
915 */
916 private boolean mConfigChanged;
917
918 /**
919 * Whether or not the options menu is in its smaller, icon menu form. When
920 * true, we want the title bar overlay to be up. When false, we do not.
921 * Only meaningful if mOptionsMenuOpen is true.
922 */
923 private boolean mIconView;
924
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400925 @Override
926 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400927 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
928 if (mOptionsMenuOpen) {
929 if (mConfigChanged) {
930 // We do not need to make any changes to the state of the
931 // title bar, since the only thing that happened was a
932 // change in orientation
933 mConfigChanged = false;
934 } else {
935 if (mIconView) {
936 // Switching the menu to expanded view, so hide the
937 // title bar.
938 hideFakeTitleBar();
939 mIconView = false;
940 } else {
941 // Switching the menu back to icon view, so show the
942 // title bar once again.
943 showFakeTitleBar();
944 mIconView = true;
945 }
946 }
947 } else {
948 // The options menu is closed, so open it, and show the title
949 showFakeTitleBar();
950 mOptionsMenuOpen = true;
951 mConfigChanged = false;
952 mIconView = true;
953 }
954 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400955 return true;
956 }
957
Leon Scrogginsb94bf272009-09-25 15:22:08 -0400958 /**
959 * Special class used exclusively for the shadow drawn underneath the fake
960 * title bar. The shadow does not need to be drawn if the WebView
961 * underneath is scrolled to the top, because it will draw directly on top
962 * of the embedded shadow.
963 */
964 private static class Shadow extends View {
965 private WebView mWebView;
966
967 public Shadow(Context context, AttributeSet attrs) {
968 super(context, attrs);
969 }
970
971 public void setWebView(WebView view) {
972 mWebView = view;
973 }
974
975 @Override
976 public void draw(Canvas canvas) {
977 // In general onDraw is the method to override, but we care about
978 // whether or not the background gets drawn, which happens in draw()
979 if (mWebView == null || mWebView.getScrollY() > getHeight()) {
980 super.draw(canvas);
981 }
982 // Need to invalidate so that if the scroll position changes, we
983 // still draw as appropriate.
984 invalidate();
985 }
986 }
987
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400988 private void showFakeTitleBar() {
Patrick Scottf8de8ec2009-09-21 16:37:29 -0400989 final View decor = getWindow().peekDecorView();
Leon Scroggins4d7e4062009-09-15 15:49:45 -0400990 if (mFakeTitleBar == null && mActiveTabsPage == null
Patrick Scottf8de8ec2009-09-21 16:37:29 -0400991 && !mActivityInPause && decor != null
992 && decor.getWindowToken() != null) {
Cary Clarka0464552009-09-29 13:00:45 -0400993 Rect visRect = new Rect();
994 if (!mBrowserFrameLayout.getGlobalVisibleRect(visRect)) {
995 if (LOGD_ENABLED) {
996 Log.d(LOGTAG, "showFakeTitleBar visRect failed");
997 }
998 return;
999 }
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -04001000 final WebView webView = getTopWindow();
Leon Scroggins68579392009-09-15 15:31:54 -04001001 mFakeTitleBar = new TitleBar(this);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04001002 mFakeTitleBar.setTitleAndUrl(null, webView.getUrl());
1003 mFakeTitleBar.setProgress(webView.getProgress());
1004 mFakeTitleBar.setFavicon(webView.getFavicon());
1005 updateLockIconToLatest();
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04001006
1007 WindowManager manager
1008 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
1009
1010 // Add the title bar to the window manager so it can receive touches
1011 // while the menu is up
1012 WindowManager.LayoutParams params
1013 = new WindowManager.LayoutParams(
1014 ViewGroup.LayoutParams.FILL_PARENT,
1015 ViewGroup.LayoutParams.WRAP_CONTENT,
1016 WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL,
1017 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
Leon Scroggins68549862009-09-21 16:02:01 -04001018 PixelFormat.TRANSLUCENT);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04001019 params.gravity = Gravity.TOP;
Leon Scrogginsa27ff192009-09-14 12:58:04 -04001020 WebView mainView = mTabControl.getCurrentWebView();
Leon Scroggins68549862009-09-21 16:02:01 -04001021 boolean atTop = mainView != null && mainView.getScrollY() == 0;
Leon Scroggins83932c72009-09-30 11:55:54 -04001022 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04001023 // XXX : Without providing an offset, the fake title bar will be
1024 // placed underneath the status bar. Use the global visible rect
1025 // of mBrowserFrameLayout to determine the bottom of the status bar
Cary Clarka0464552009-09-29 13:00:45 -04001026 params.y = visRect.top;
Leon Scroggins68549862009-09-21 16:02:01 -04001027 // Add a holder for the title bar. It also holds a shadow to show
1028 // below the title bar.
Leon Scrogginsd8fd2fc2009-09-16 11:12:09 -04001029 if (mFakeTitleBarHolder == null) {
1030 mFakeTitleBarHolder = (ViewGroup) LayoutInflater.from(this)
1031 .inflate(R.layout.title_bar_bg, null);
1032 }
Leon Scrogginsb94bf272009-09-25 15:22:08 -04001033 Shadow shadow = (Shadow) mFakeTitleBarHolder.findViewById(
1034 R.id.shadow);
1035 shadow.setWebView(mainView);
Leon Scroggins68549862009-09-21 16:02:01 -04001036 mFakeTitleBarHolder.addView(mFakeTitleBar, 0, mFakeTitleBarParams);
Leon Scrogginsd8fd2fc2009-09-16 11:12:09 -04001037 manager.addView(mFakeTitleBarHolder, params);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04001038 }
1039 }
1040
1041 @Override
1042 public void onOptionsMenuClosed(Menu menu) {
1043 mOptionsMenuOpen = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -04001044 if (!mInLoad) {
1045 hideFakeTitleBar();
1046 } else if (!mIconView) {
1047 // The page is currently loading, and we are in expanded mode, so
1048 // we were not showing the menu. Show it once again. It will be
1049 // removed when the page finishes.
1050 showFakeTitleBar();
1051 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04001052 }
1053 private void hideFakeTitleBar() {
1054 if (mFakeTitleBar == null) return;
Leon Scroggins20329572009-09-23 17:42:41 -04001055 WindowManager.LayoutParams params = (WindowManager.LayoutParams)
1056 mFakeTitleBarHolder.getLayoutParams();
1057 WebView mainView = mTabControl.getCurrentWebView();
1058 // Although we decided whether or not to animate based on the current
1059 // scroll position, the scroll position may have changed since the
1060 // fake title bar was displayed. Make sure it has the appropriate
1061 // animation/lack thereof before removing.
1062 params.windowAnimations = mainView != null && mainView.getScrollY() == 0
Leon Scroggins83932c72009-09-30 11:55:54 -04001063 ? 0 : R.style.TitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04001064 WindowManager manager
1065 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Leon Scroggins20329572009-09-23 17:42:41 -04001066 manager.updateViewLayout(mFakeTitleBarHolder, params);
Leon Scrogginsd8fd2fc2009-09-16 11:12:09 -04001067 mFakeTitleBarHolder.removeView(mFakeTitleBar);
1068 manager.removeView(mFakeTitleBarHolder);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04001069 mFakeTitleBar = null;
1070 }
1071
The Android Open Source Project0c908882009-03-03 19:32:16 -08001072 /**
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001073 * Special method for the fake title bar to call when displaying its context
1074 * menu, since it is in its own Window, and its parent does not show a
1075 * context menu.
1076 */
1077 /* package */ void showTitleBarContextMenu() {
Cary Clark65f4a3c2009-09-28 17:05:06 -04001078 if (null == mTitleBar.getParent()) {
1079 return;
1080 }
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001081 openContextMenu(mTitleBar);
1082 }
1083
1084 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001085 * onSaveInstanceState(Bundle map)
1086 * onSaveInstanceState is called right before onStop(). The map contains
1087 * the saved state.
1088 */
1089 @Override protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -07001090 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001091 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
1092 }
1093 // the default implementation requires each view to have an id. As the
1094 // browser handles the state itself and it doesn't use id for the views,
1095 // don't call the default implementation. Otherwise it will trigger the
1096 // warning like this, "couldn't save which view has focus because the
1097 // focused view XXX has no id".
1098
1099 // Save all the tabs
1100 mTabControl.saveState(outState);
1101 }
1102
1103 @Override protected void onPause() {
1104 super.onPause();
1105
1106 if (mActivityInPause) {
1107 Log.e(LOGTAG, "BrowserActivity is already paused.");
1108 return;
1109 }
1110
Mike Reed7bfa63b2009-05-28 11:08:32 -04001111 mTabControl.pauseCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001112 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -04001113 if (mTabControl.getCurrentIndex() >= 0 && !pauseWebViewTimers()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001114 mWakeLock.acquire();
1115 mHandler.sendMessageDelayed(mHandler
1116 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
1117 }
1118
1119 // Clear the credentials toast if it is up
1120 if (mCredsDlg != null && mCredsDlg.isShowing()) {
1121 mCredsDlg.dismiss();
1122 }
1123 mCredsDlg = null;
1124
Leon Scrogginsa2ab6a72009-09-11 11:49:52 -04001125 // FIXME: This removes the active tabs page and resets the menu to
1126 // MAIN_MENU. A better solution might be to do this work in onNewIntent
1127 // but then we would need to save it in onSaveInstanceState and restore
1128 // it in onCreate/onRestoreInstanceState
1129 if (mActiveTabsPage != null) {
1130 removeActiveTabPage(true);
1131 }
1132
The Android Open Source Project0c908882009-03-03 19:32:16 -08001133 cancelStopToast();
1134
1135 // unregister network state listener
1136 unregisterReceiver(mNetworkStateIntentReceiver);
1137 WebView.disablePlatformNotifications();
1138
1139 if (mSensorManager != null) {
1140 mSensorManager.unregisterListener(mSensorListener);
1141 }
1142 }
1143
1144 @Override protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -07001145 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001146 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
1147 }
1148 super.onDestroy();
Grace Kloba0923d692009-09-23 21:37:25 -07001149
1150 if (mTabControl == null) return;
1151
The Android Open Source Project0c908882009-03-03 19:32:16 -08001152 // Remove the current tab and sub window
1153 TabControl.Tab t = mTabControl.getCurrentTab();
Patrick Scottfb5e77f2009-04-08 19:17:37 -07001154 if (t != null) {
1155 dismissSubWindow(t);
1156 removeTabFromContentView(t);
1157 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001158 // Destroy all the tabs
1159 mTabControl.destroy();
1160 WebIconDatabase.getInstance().close();
1161 if (mGlsConnection != null) {
1162 unbindService(mGlsConnection);
1163 mGlsConnection = null;
1164 }
1165
1166 //
1167 // stop MASF proxy service
1168 //
1169 //Intent proxyServiceIntent = new Intent();
1170 //proxyServiceIntent.setComponent
1171 // (new ComponentName(
1172 // "com.android.masfproxyservice",
1173 // "com.android.masfproxyservice.MasfProxyService"));
1174 //stopService(proxyServiceIntent);
Grace Klobab4da0ad2009-05-14 14:45:40 -07001175
1176 unregisterReceiver(mPackageInstallationReceiver);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001177 }
1178
1179 @Override
1180 public void onConfigurationChanged(Configuration newConfig) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04001181 mConfigChanged = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001182 super.onConfigurationChanged(newConfig);
1183
1184 if (mPageInfoDialog != null) {
1185 mPageInfoDialog.dismiss();
1186 showPageInfo(
1187 mPageInfoView,
1188 mPageInfoFromShowSSLCertificateOnError.booleanValue());
1189 }
1190 if (mSSLCertificateDialog != null) {
1191 mSSLCertificateDialog.dismiss();
1192 showSSLCertificate(
1193 mSSLCertificateView);
1194 }
1195 if (mSSLCertificateOnErrorDialog != null) {
1196 mSSLCertificateOnErrorDialog.dismiss();
1197 showSSLCertificateOnError(
1198 mSSLCertificateOnErrorView,
1199 mSSLCertificateOnErrorHandler,
1200 mSSLCertificateOnErrorError);
1201 }
1202 if (mHttpAuthenticationDialog != null) {
1203 String title = ((TextView) mHttpAuthenticationDialog
1204 .findViewById(com.android.internal.R.id.alertTitle)).getText()
1205 .toString();
1206 String name = ((TextView) mHttpAuthenticationDialog
1207 .findViewById(R.id.username_edit)).getText().toString();
1208 String password = ((TextView) mHttpAuthenticationDialog
1209 .findViewById(R.id.password_edit)).getText().toString();
1210 int focusId = mHttpAuthenticationDialog.getCurrentFocus()
1211 .getId();
1212 mHttpAuthenticationDialog.dismiss();
1213 showHttpAuthentication(mHttpAuthHandler, null, null, title,
1214 name, password, focusId);
1215 }
1216 if (mFindDialog != null && mFindDialog.isShowing()) {
1217 mFindDialog.onConfigurationChanged(newConfig);
1218 }
1219 }
1220
1221 @Override public void onLowMemory() {
1222 super.onLowMemory();
1223 mTabControl.freeMemory();
1224 }
1225
Mike Reed7bfa63b2009-05-28 11:08:32 -04001226 private boolean resumeWebViewTimers() {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001227 if ((!mActivityInPause && !mPageStarted) ||
1228 (mActivityInPause && mPageStarted)) {
1229 CookieSyncManager.getInstance().startSync();
1230 WebView w = mTabControl.getCurrentWebView();
1231 if (w != null) {
1232 w.resumeTimers();
1233 }
1234 return true;
1235 } else {
1236 return false;
1237 }
1238 }
1239
Mike Reed7bfa63b2009-05-28 11:08:32 -04001240 private boolean pauseWebViewTimers() {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001241 if (mActivityInPause && !mPageStarted) {
1242 CookieSyncManager.getInstance().stopSync();
1243 WebView w = mTabControl.getCurrentWebView();
1244 if (w != null) {
1245 w.pauseTimers();
1246 }
1247 return true;
1248 } else {
1249 return false;
1250 }
1251 }
1252
Leon Scroggins1f005d32009-08-10 17:36:42 -04001253 // FIXME: Do we want to call this when loading google for the first time?
The Android Open Source Project0c908882009-03-03 19:32:16 -08001254 /*
1255 * This function is called when we are launching for the first time. We
1256 * are waiting for the login credentials before loading Google home
1257 * pages. This way the user will be logged in straight away.
1258 */
1259 private void waitForCredentials() {
1260 // Show a toast
1261 mCredsDlg = new ProgressDialog(this);
1262 mCredsDlg.setIndeterminate(true);
1263 mCredsDlg.setMessage(getText(R.string.retrieving_creds_dlg_msg));
1264 // If the user cancels the operation, then cancel the Google
1265 // Credentials request.
1266 mCredsDlg.setCancelMessage(mHandler.obtainMessage(CANCEL_CREDS_REQUEST));
1267 mCredsDlg.show();
1268
1269 // We set a timeout for the retrieval of credentials in onResume()
1270 // as that is when we have freed up some CPU time to get
1271 // the login credentials.
1272 }
1273
1274 /*
1275 * If we have received the credentials or we have timed out and we are
1276 * showing the credentials dialog, then it is time to move on.
1277 */
1278 private void resumeAfterCredentials() {
1279 if (mCredsDlg == null) {
1280 return;
1281 }
1282
1283 // Clear the toast
1284 if (mCredsDlg.isShowing()) {
1285 mCredsDlg.dismiss();
1286 }
1287 mCredsDlg = null;
1288
1289 // Clear any pending timeout
1290 mHandler.removeMessages(CANCEL_CREDS_REQUEST);
1291
1292 // Load the page
1293 WebView w = mTabControl.getCurrentWebView();
1294 if (w != null) {
1295 w.loadUrl(mSettings.getHomePage());
1296 }
1297
1298 // Update the settings, need to do this last as it can take a moment
1299 // to persist the settings. In the mean time we could be loading
1300 // content.
1301 mSettings.setLoginInitialized(this);
1302 }
1303
1304 // Open the icon database and retain all the icons for visited sites.
1305 private void retainIconsOnStartup() {
1306 final WebIconDatabase db = WebIconDatabase.getInstance();
1307 db.open(getDir("icons", 0).getPath());
1308 try {
1309 Cursor c = Browser.getAllBookmarks(mResolver);
1310 if (!c.moveToFirst()) {
1311 c.deactivate();
1312 return;
1313 }
1314 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
1315 do {
1316 String url = c.getString(urlIndex);
1317 db.retainIconForPageUrl(url);
1318 } while (c.moveToNext());
1319 c.deactivate();
1320 } catch (IllegalStateException e) {
1321 Log.e(LOGTAG, "retainIconsOnStartup", e);
1322 }
1323 }
1324
1325 // Helper method for getting the top window.
1326 WebView getTopWindow() {
1327 return mTabControl.getCurrentTopWebView();
1328 }
1329
1330 @Override
1331 public boolean onCreateOptionsMenu(Menu menu) {
1332 super.onCreateOptionsMenu(menu);
1333
1334 MenuInflater inflater = getMenuInflater();
1335 inflater.inflate(R.menu.browser, menu);
1336 mMenu = menu;
1337 updateInLoadMenuItems();
1338 return true;
1339 }
1340
1341 /**
1342 * As the menu can be open when loading state changes
1343 * we must manually update the state of the stop/reload menu
1344 * item
1345 */
1346 private void updateInLoadMenuItems() {
1347 if (mMenu == null) {
1348 return;
1349 }
1350 MenuItem src = mInLoad ?
1351 mMenu.findItem(R.id.stop_menu_id):
1352 mMenu.findItem(R.id.reload_menu_id);
1353 MenuItem dest = mMenu.findItem(R.id.stop_reload_menu_id);
1354 dest.setIcon(src.getIcon());
1355 dest.setTitle(src.getTitle());
1356 }
1357
1358 @Override
1359 public boolean onContextItemSelected(MenuItem item) {
1360 // chording is not an issue with context menus, but we use the same
1361 // options selector, so set mCanChord to true so we can access them.
1362 mCanChord = true;
1363 int id = item.getItemId();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001364 switch (id) {
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001365 // For the context menu from the title bar
1366 case R.id.title_bar_share_page_url:
1367 case R.id.title_bar_copy_page_url:
1368 WebView mainView = mTabControl.getCurrentWebView();
1369 if (null == mainView) {
1370 return false;
1371 }
1372 if (id == R.id.title_bar_share_page_url) {
1373 Browser.sendString(this, mainView.getUrl());
1374 } else {
1375 copy(mainView.getUrl());
1376 }
1377 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001378 // -- Browser context menu
1379 case R.id.open_context_menu_id:
1380 case R.id.open_newtab_context_menu_id:
1381 case R.id.bookmark_context_menu_id:
1382 case R.id.save_link_context_menu_id:
1383 case R.id.share_link_context_menu_id:
1384 case R.id.copy_link_context_menu_id:
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001385 final WebView webView = getTopWindow();
1386 if (null == webView) {
1387 return false;
1388 }
1389 final HashMap hrefMap = new HashMap();
1390 hrefMap.put("webview", webView);
1391 final Message msg = mHandler.obtainMessage(
1392 FOCUS_NODE_HREF, id, 0, hrefMap);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001393 webView.requestFocusNodeHref(msg);
1394 break;
1395
1396 default:
1397 // For other context menus
1398 return onOptionsItemSelected(item);
1399 }
1400 mCanChord = false;
1401 return true;
1402 }
1403
1404 private Bundle createGoogleSearchSourceBundle(String source) {
1405 Bundle bundle = new Bundle();
1406 bundle.putString(SearchManager.SOURCE, source);
1407 return bundle;
1408 }
1409
1410 /**
The Android Open Source Project4e5f5872009-03-09 11:52:14 -07001411 * Overriding this to insert a local information bundle
The Android Open Source Project0c908882009-03-03 19:32:16 -08001412 */
1413 @Override
1414 public boolean onSearchRequested() {
Leon Scroggins68579392009-09-15 15:31:54 -04001415 if (mOptionsMenuOpen) closeOptionsMenu();
Leon Scroggins5bbe9802009-07-31 13:10:55 -04001416 String url = (getTopWindow() == null) ? null : getTopWindow().getUrl();
Grace Kloba83f47342009-07-20 10:44:31 -07001417 startSearch(mSettings.getHomePage().equals(url) ? null : url, true,
The Android Open Source Project4e5f5872009-03-09 11:52:14 -07001418 createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_SEARCHKEY), false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001419 return true;
1420 }
1421
1422 @Override
1423 public void startSearch(String initialQuery, boolean selectInitialQuery,
1424 Bundle appSearchData, boolean globalSearch) {
1425 if (appSearchData == null) {
1426 appSearchData = createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_TYPE);
1427 }
1428 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1429 }
1430
Leon Scroggins1f005d32009-08-10 17:36:42 -04001431 /**
1432 * Switch tabs. Called by the TitleBarSet when sliding the title bar
1433 * results in changing tabs.
Leon Scroggins160a7e72009-08-14 18:28:01 -04001434 * @param index Index of the tab to change to, as defined by
1435 * mTabControl.getTabIndex(Tab t).
1436 * @return boolean True if we successfully switched to a different tab. If
1437 * the indexth tab is null, or if that tab is the same as
1438 * the current one, return false.
Leon Scroggins1f005d32009-08-10 17:36:42 -04001439 */
Leon Scroggins160a7e72009-08-14 18:28:01 -04001440 /* package */ boolean switchToTab(int index) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001441 TabControl.Tab tab = mTabControl.getTab(index);
1442 TabControl.Tab currentTab = mTabControl.getCurrentTab();
1443 if (tab == null || tab == currentTab) {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001444 return false;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001445 }
1446 if (currentTab != null) {
1447 // currentTab may be null if it was just removed. In that case,
1448 // we do not need to remove it
1449 removeTabFromContentView(currentTab);
1450 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001451 mTabControl.setCurrentTab(tab);
1452 attachTabToContentView(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001453 resetTitleIconAndProgress();
1454 updateLockIconToLatest();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001455 return true;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001456 }
1457
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001458 /* package */ TabControl.Tab openTabToHomePage() {
1459 return openTabAndShow(mSettings.getHomePage(), false, null);
1460 }
1461
Leon Scroggins1f005d32009-08-10 17:36:42 -04001462 /* package */ void closeCurrentWindow() {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001463 final TabControl.Tab current = mTabControl.getCurrentTab();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001464 if (mTabControl.getTabCount() == 1) {
Leon Scroggins30444232009-09-04 18:36:20 -04001465 // This is the last tab. Open a new one, with the home
1466 // page and close the current one.
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001467 TabControl.Tab newTab = openTabToHomePage();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001468 closeTab(current);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001469 return;
1470 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001471 final TabControl.Tab parent = current.getParentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001472 int indexToShow = -1;
1473 if (parent != null) {
1474 indexToShow = mTabControl.getTabIndex(parent);
1475 } else {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001476 final int currentIndex = mTabControl.getCurrentIndex();
1477 // Try to move to the tab to the right
1478 indexToShow = currentIndex + 1;
1479 if (indexToShow > mTabControl.getTabCount() - 1) {
1480 // Try to move to the tab to the left
1481 indexToShow = currentIndex - 1;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001482 }
1483 }
Leon Scroggins160a7e72009-08-14 18:28:01 -04001484 if (switchToTab(indexToShow)) {
1485 // Close window
1486 closeTab(current);
1487 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001488 }
1489
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001490 private ActiveTabsPage mActiveTabsPage;
1491
1492 /**
1493 * Remove the active tabs page.
1494 * @param needToAttach If true, the active tabs page did not attach a tab
1495 * to the content view, so we need to do that here.
1496 */
1497 /* package */ void removeActiveTabPage(boolean needToAttach) {
1498 mContentView.removeView(mActiveTabsPage);
1499 mActiveTabsPage = null;
1500 mMenuState = R.id.MAIN_MENU;
1501 if (needToAttach) {
1502 attachTabToContentView(mTabControl.getCurrentTab());
1503 }
1504 getTopWindow().requestFocus();
1505 }
1506
The Android Open Source Project0c908882009-03-03 19:32:16 -08001507 @Override
1508 public boolean onOptionsItemSelected(MenuItem item) {
1509 if (!mCanChord) {
1510 // The user has already fired a shortcut with this hold down of the
1511 // menu key.
1512 return false;
1513 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001514 if (null == getTopWindow()) {
Leon Scroggins0d7ae0e2009-06-05 11:04:45 -04001515 return false;
1516 }
Grace Kloba6ee9c492009-07-13 10:04:34 -07001517 if (mMenuIsDown) {
1518 // The shortcut action consumes the MENU. Even if it is still down,
1519 // it won't trigger the next shortcut action. In the case of the
1520 // shortcut action triggering a new activity, like Bookmarks, we
1521 // won't get onKeyUp for MENU. So it is important to reset it here.
1522 mMenuIsDown = false;
1523 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001524 switch (item.getItemId()) {
1525 // -- Main menu
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001526 case R.id.new_tab_menu_id:
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001527 openTabToHomePage();
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001528 break;
1529
Leon Scroggins64b80f32009-08-07 12:03:34 -04001530 case R.id.goto_menu_id:
Leon Scrogginsb3a5bed2009-09-28 11:21:56 -04001531 onSearchRequested();
1532 break;
1533
1534 case R.id.bookmarks_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001535 bookmarksOrHistoryPicker(false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001536 break;
1537
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001538 case R.id.active_tabs_menu_id:
1539 mActiveTabsPage = new ActiveTabsPage(this, mTabControl);
1540 removeTabFromContentView(mTabControl.getCurrentTab());
Leon Scroggins43de6162009-09-14 19:59:58 -04001541 hideFakeTitleBar();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001542 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
1543 mActiveTabsPage.requestFocus();
1544 mMenuState = EMPTY_MENU;
1545 break;
1546
Leon Scroggins1f005d32009-08-10 17:36:42 -04001547 case R.id.add_bookmark_menu_id:
1548 Intent i = new Intent(BrowserActivity.this,
1549 AddBookmarkPage.class);
1550 WebView w = getTopWindow();
1551 i.putExtra("url", w.getUrl());
1552 i.putExtra("title", w.getTitle());
Grace Kloba83cdb2c2009-09-16 00:48:57 -07001553 i.putExtra("touch_icon_url", w.getTouchIconUrl());
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01001554 i.putExtra("thumbnail", createScreenshot(w));
Leon Scroggins1f005d32009-08-10 17:36:42 -04001555 startActivity(i);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001556 break;
1557
1558 case R.id.stop_reload_menu_id:
1559 if (mInLoad) {
1560 stopLoading();
1561 } else {
1562 getTopWindow().reload();
1563 }
1564 break;
1565
1566 case R.id.back_menu_id:
1567 getTopWindow().goBack();
1568 break;
1569
1570 case R.id.forward_menu_id:
1571 getTopWindow().goForward();
1572 break;
1573
1574 case R.id.close_menu_id:
1575 // Close the subwindow if it exists.
1576 if (mTabControl.getCurrentSubWindow() != null) {
1577 dismissSubWindow(mTabControl.getCurrentTab());
1578 break;
1579 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001580 closeCurrentWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001581 break;
1582
1583 case R.id.homepage_menu_id:
1584 TabControl.Tab current = mTabControl.getCurrentTab();
1585 if (current != null) {
1586 dismissSubWindow(current);
1587 current.getWebView().loadUrl(mSettings.getHomePage());
1588 }
1589 break;
1590
1591 case R.id.preferences_menu_id:
1592 Intent intent = new Intent(this,
1593 BrowserPreferencesPage.class);
1594 startActivityForResult(intent, PREFERENCES_PAGE);
1595 break;
1596
1597 case R.id.find_menu_id:
1598 if (null == mFindDialog) {
1599 mFindDialog = new FindDialog(this);
1600 }
1601 mFindDialog.setWebView(getTopWindow());
1602 mFindDialog.show();
1603 mMenuState = EMPTY_MENU;
1604 break;
1605
1606 case R.id.select_text_id:
1607 getTopWindow().emulateShiftHeld();
1608 break;
1609 case R.id.page_info_menu_id:
1610 showPageInfo(mTabControl.getCurrentTab(), false);
1611 break;
1612
1613 case R.id.classic_history_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001614 bookmarksOrHistoryPicker(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001615 break;
1616
1617 case R.id.share_page_menu_id:
Andrei Popescu10fdba82009-09-24 13:25:47 +01001618 Browser.sendString(this, getTopWindow().getUrl(),
1619 getText(R.string.choosertitle_sharevia).toString());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001620 break;
1621
1622 case R.id.dump_nav_menu_id:
1623 getTopWindow().debugDump();
1624 break;
1625
1626 case R.id.zoom_in_menu_id:
1627 getTopWindow().zoomIn();
1628 break;
1629
1630 case R.id.zoom_out_menu_id:
1631 getTopWindow().zoomOut();
1632 break;
1633
1634 case R.id.view_downloads_menu_id:
1635 viewDownloads(null);
1636 break;
1637
The Android Open Source Project0c908882009-03-03 19:32:16 -08001638 case R.id.window_one_menu_id:
1639 case R.id.window_two_menu_id:
1640 case R.id.window_three_menu_id:
1641 case R.id.window_four_menu_id:
1642 case R.id.window_five_menu_id:
1643 case R.id.window_six_menu_id:
1644 case R.id.window_seven_menu_id:
1645 case R.id.window_eight_menu_id:
1646 {
1647 int menuid = item.getItemId();
1648 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1649 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1650 TabControl.Tab desiredTab = mTabControl.getTab(id);
1651 if (desiredTab != null &&
1652 desiredTab != mTabControl.getCurrentTab()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001653 switchToTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001654 }
1655 break;
1656 }
1657 }
1658 }
1659 break;
1660
1661 default:
1662 if (!super.onOptionsItemSelected(item)) {
1663 return false;
1664 }
1665 // Otherwise fall through.
1666 }
1667 mCanChord = false;
1668 return true;
1669 }
1670
1671 public void closeFind() {
1672 mMenuState = R.id.MAIN_MENU;
1673 }
1674
1675 @Override public boolean onPrepareOptionsMenu(Menu menu)
1676 {
1677 // This happens when the user begins to hold down the menu key, so
1678 // allow them to chord to get a shortcut.
1679 mCanChord = true;
1680 // Note: setVisible will decide whether an item is visible; while
1681 // setEnabled() will decide whether an item is enabled, which also means
1682 // whether the matching shortcut key will function.
1683 super.onPrepareOptionsMenu(menu);
1684 switch (mMenuState) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001685 case EMPTY_MENU:
1686 if (mCurrentMenuState != mMenuState) {
1687 menu.setGroupVisible(R.id.MAIN_MENU, false);
1688 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1689 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001690 }
1691 break;
1692 default:
1693 if (mCurrentMenuState != mMenuState) {
1694 menu.setGroupVisible(R.id.MAIN_MENU, true);
1695 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1696 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001697 }
1698 final WebView w = getTopWindow();
1699 boolean canGoBack = false;
1700 boolean canGoForward = false;
1701 boolean isHome = false;
1702 if (w != null) {
1703 canGoBack = w.canGoBack();
1704 canGoForward = w.canGoForward();
1705 isHome = mSettings.getHomePage().equals(w.getUrl());
1706 }
1707 final MenuItem back = menu.findItem(R.id.back_menu_id);
1708 back.setEnabled(canGoBack);
1709
1710 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1711 home.setEnabled(!isHome);
1712
1713 menu.findItem(R.id.forward_menu_id)
1714 .setEnabled(canGoForward);
1715
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001716 menu.findItem(R.id.new_tab_menu_id).setEnabled(
1717 mTabControl.getTabCount() < TabControl.MAX_TABS);
1718
The Android Open Source Project0c908882009-03-03 19:32:16 -08001719 // decide whether to show the share link option
1720 PackageManager pm = getPackageManager();
1721 Intent send = new Intent(Intent.ACTION_SEND);
1722 send.setType("text/plain");
1723 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1724 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1725
The Android Open Source Project0c908882009-03-03 19:32:16 -08001726 boolean isNavDump = mSettings.isNavDump();
1727 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1728 nav.setVisible(isNavDump);
1729 nav.setEnabled(isNavDump);
1730 break;
1731 }
1732 mCurrentMenuState = mMenuState;
1733 return true;
1734 }
1735
1736 @Override
1737 public void onCreateContextMenu(ContextMenu menu, View v,
1738 ContextMenuInfo menuInfo) {
1739 WebView webview = (WebView) v;
1740 WebView.HitTestResult result = webview.getHitTestResult();
1741 if (result == null) {
1742 return;
1743 }
1744
1745 int type = result.getType();
1746 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1747 Log.w(LOGTAG,
1748 "We should not show context menu when nothing is touched");
1749 return;
1750 }
1751 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1752 // let TextView handles context menu
1753 return;
1754 }
1755
1756 // Note, http://b/issue?id=1106666 is requesting that
1757 // an inflated menu can be used again. This is not available
1758 // yet, so inflate each time (yuk!)
1759 MenuInflater inflater = getMenuInflater();
1760 inflater.inflate(R.menu.browsercontext, menu);
1761
1762 // Show the correct menu group
1763 String extra = result.getExtra();
1764 menu.setGroupVisible(R.id.PHONE_MENU,
1765 type == WebView.HitTestResult.PHONE_TYPE);
1766 menu.setGroupVisible(R.id.EMAIL_MENU,
1767 type == WebView.HitTestResult.EMAIL_TYPE);
1768 menu.setGroupVisible(R.id.GEO_MENU,
1769 type == WebView.HitTestResult.GEO_TYPE);
1770 menu.setGroupVisible(R.id.IMAGE_MENU,
1771 type == WebView.HitTestResult.IMAGE_TYPE
1772 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1773 menu.setGroupVisible(R.id.ANCHOR_MENU,
1774 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1775 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1776
1777 // Setup custom handling depending on the type
1778 switch (type) {
1779 case WebView.HitTestResult.PHONE_TYPE:
1780 menu.setHeaderTitle(Uri.decode(extra));
1781 menu.findItem(R.id.dial_context_menu_id).setIntent(
1782 new Intent(Intent.ACTION_VIEW, Uri
1783 .parse(WebView.SCHEME_TEL + extra)));
1784 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1785 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
Cary Clark5e335a32009-09-22 14:53:11 -04001786 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001787 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1788 addIntent);
1789 menu.findItem(R.id.copy_phone_context_menu_id).setOnMenuItemClickListener(
1790 new Copy(extra));
1791 break;
1792
1793 case WebView.HitTestResult.EMAIL_TYPE:
1794 menu.setHeaderTitle(extra);
1795 menu.findItem(R.id.email_context_menu_id).setIntent(
1796 new Intent(Intent.ACTION_VIEW, Uri
1797 .parse(WebView.SCHEME_MAILTO + extra)));
1798 menu.findItem(R.id.copy_mail_context_menu_id).setOnMenuItemClickListener(
1799 new Copy(extra));
1800 break;
1801
1802 case WebView.HitTestResult.GEO_TYPE:
1803 menu.setHeaderTitle(extra);
1804 menu.findItem(R.id.map_context_menu_id).setIntent(
1805 new Intent(Intent.ACTION_VIEW, Uri
1806 .parse(WebView.SCHEME_GEO
1807 + URLEncoder.encode(extra))));
1808 menu.findItem(R.id.copy_geo_context_menu_id).setOnMenuItemClickListener(
1809 new Copy(extra));
1810 break;
1811
1812 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1813 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
1814 TextView titleView = (TextView) LayoutInflater.from(this)
1815 .inflate(android.R.layout.browser_link_context_header,
1816 null);
1817 titleView.setText(extra);
1818 menu.setHeaderView(titleView);
1819 // decide whether to show the open link in new tab option
1820 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(
1821 mTabControl.getTabCount() < TabControl.MAX_TABS);
1822 PackageManager pm = getPackageManager();
1823 Intent send = new Intent(Intent.ACTION_SEND);
1824 send.setType("text/plain");
1825 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1826 menu.findItem(R.id.share_link_context_menu_id).setVisible(ri != null);
1827 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1828 break;
1829 }
1830 // otherwise fall through to handle image part
1831 case WebView.HitTestResult.IMAGE_TYPE:
1832 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1833 menu.setHeaderTitle(extra);
1834 }
1835 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1836 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1837 menu.findItem(R.id.download_context_menu_id).
1838 setOnMenuItemClickListener(new Download(extra));
1839 break;
1840
1841 default:
1842 Log.w(LOGTAG, "We should not get here.");
1843 break;
1844 }
1845 }
1846
The Android Open Source Project0c908882009-03-03 19:32:16 -08001847 // Attach the given tab to the content view.
Grace Klobac928c302009-09-17 11:51:21 -07001848 // this should only be called for the current tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -08001849 private void attachTabToContentView(TabControl.Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001850 // Attach the container that contains the main WebView and any other UI
1851 // associated with the tab.
Patrick Scottd0119532009-09-17 08:00:31 -04001852 t.attachTabToContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001853
1854 if (mShouldShowErrorConsole) {
1855 ErrorConsoleView errorConsole = mTabControl.getCurrentErrorConsole(true);
1856 if (errorConsole.numberOfErrors() == 0) {
1857 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
1858 } else {
1859 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1860 }
1861
1862 mErrorConsoleContainer.addView(errorConsole,
1863 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
1864 ViewGroup.LayoutParams.WRAP_CONTENT));
1865 }
1866
Grace Klobac928c302009-09-17 11:51:21 -07001867 setLockIconType(t.getLockIconType());
1868 setPrevLockType(t.getPrevLockIconType());
1869
1870 // this is to match the code in removeTabFromContentView()
1871 if (!mPageStarted && t.getTopWindow().getProgress() < 100) {
1872 mPageStarted = true;
Grace Klobaeb6eef42009-09-15 17:56:32 -07001873 }
1874
Leon Scroggins39ab28e2009-09-02 21:20:30 -04001875 WebView view = t.getWebView();
Leon Scroggins55a5bc22009-09-04 17:00:08 -04001876 view.setEmbeddedTitleBar(mTitleBar);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001877 // Request focus on the top window.
1878 t.getTopWindow().requestFocus();
1879 }
1880
1881 // Attach a sub window to the main WebView of the given tab.
1882 private void attachSubWindow(TabControl.Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001883 t.attachSubWindow(mContentView);
1884 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001885 }
1886
1887 // Remove the given tab from the content view.
1888 private void removeTabFromContentView(TabControl.Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001889 // Remove the container that contains the main WebView.
Patrick Scottd0119532009-09-17 08:00:31 -04001890 t.removeTabFromContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001891
1892 if (mTabControl.getCurrentErrorConsole(false) != null) {
1893 mErrorConsoleContainer.removeView(mTabControl.getCurrentErrorConsole(false));
1894 }
1895
Leon Scroggins39ab28e2009-09-02 21:20:30 -04001896 WebView view = t.getWebView();
Leon Scrogginsbb85b902009-09-14 19:27:20 -04001897 if (view != null) {
1898 view.setEmbeddedTitleBar(null);
1899 }
Leon Scroggins39ab28e2009-09-02 21:20:30 -04001900
Grace Klobac928c302009-09-17 11:51:21 -07001901 // unlike attachTabToContentView(), removeTabFromContentView() can be
1902 // called for the non-current tab. Need to add the check.
Grace Klobaeb6eef42009-09-15 17:56:32 -07001903 if (t == mTabControl.getCurrentTab()) {
1904 t.setLockIconType(getLockIconType());
1905 t.setPrevLockIconType(getPrevLockType());
Grace Klobac928c302009-09-17 11:51:21 -07001906
1907 // this is not a perfect solution. But currently there is one
1908 // WebViewClient for all the WebView. if user switches from an
1909 // in-load window to an already loaded window, mPageStarted will not
1910 // be set to false. If user leaves the Browser, pauseWebViewTimers()
1911 // won't do anything and leaves the timer running even Browser is in
1912 // the background.
1913 if (mPageStarted) {
1914 mPageStarted = false;
1915 }
Grace Klobaeb6eef42009-09-15 17:56:32 -07001916 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001917 }
1918
1919 // Remove the sub window if it exists. Also called by TabControl when the
1920 // user clicks the 'X' to dismiss a sub window.
1921 /* package */ void dismissSubWindow(TabControl.Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001922 t.removeSubWindow(mContentView);
1923 // Tell the TabControl to dismiss the subwindow. This will destroy
1924 // the WebView.
1925 mTabControl.dismissSubWindow(t);
1926 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001927 }
1928
Leon Scroggins1f005d32009-08-10 17:36:42 -04001929 // A wrapper function of {@link #openTabAndShow(UrlData, boolean, String)}
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07001930 // that accepts url as string.
Leon Scroggins1f005d32009-08-10 17:36:42 -04001931 private TabControl.Tab openTabAndShow(String url, boolean closeOnExit,
1932 String appId) {
1933 return openTabAndShow(new UrlData(url), closeOnExit, appId);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001934 }
1935
1936 // This method does a ton of stuff. It will attempt to create a new tab
1937 // if we haven't reached MAX_TABS. Otherwise it uses the current tab. If
Leon Scroggins1f005d32009-08-10 17:36:42 -04001938 // url isn't null, it will load the given url.
1939 /* package */ TabControl.Tab openTabAndShow(UrlData urlData,
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001940 boolean closeOnExit, String appId) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001941 final boolean newTab = mTabControl.getTabCount() != TabControl.MAX_TABS;
1942 final TabControl.Tab currentTab = mTabControl.getCurrentTab();
1943 if (newTab) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001944 final TabControl.Tab tab = mTabControl.createNewTab(
1945 closeOnExit, appId, urlData.mUrl);
1946 WebView webview = tab.getWebView();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001947 // If the last tab was removed from the active tabs page, currentTab
1948 // will be null.
1949 if (currentTab != null) {
1950 removeTabFromContentView(currentTab);
1951 }
Patrick Scott8bbd69f2009-08-14 13:35:53 -04001952 // We must set the new tab as the current tab to reflect the old
1953 // animation behavior.
1954 mTabControl.setCurrentTab(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001955 attachTabToContentView(tab);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001956 if (!urlData.isEmpty()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001957 urlData.loadIn(webview);
1958 }
1959 return tab;
1960 } else {
1961 // Get rid of the subwindow if it exists
1962 dismissSubWindow(currentTab);
1963 if (!urlData.isEmpty()) {
1964 // Load the given url.
1965 urlData.loadIn(currentTab.getWebView());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001966 }
1967 }
Grace Klobac9181842009-04-14 08:53:22 -07001968 return currentTab;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001969 }
1970
Grace Klobac9181842009-04-14 08:53:22 -07001971 private TabControl.Tab openTab(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001972 if (mSettings.openInBackground()) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -07001973 TabControl.Tab t = mTabControl.createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001974 if (t != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001975 WebView view = t.getWebView();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001976 view.loadUrl(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001977 }
Grace Klobac9181842009-04-14 08:53:22 -07001978 return t;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001979 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001980 return openTabAndShow(url, false, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001981 }
1982 }
1983
1984 private class Copy implements OnMenuItemClickListener {
1985 private CharSequence mText;
1986
1987 public boolean onMenuItemClick(MenuItem item) {
1988 copy(mText);
1989 return true;
1990 }
1991
1992 public Copy(CharSequence toCopy) {
1993 mText = toCopy;
1994 }
1995 }
1996
1997 private class Download implements OnMenuItemClickListener {
1998 private String mText;
1999
2000 public boolean onMenuItemClick(MenuItem item) {
2001 onDownloadStartNoStream(mText, null, null, null, -1);
2002 return true;
2003 }
2004
2005 public Download(String toDownload) {
2006 mText = toDownload;
2007 }
2008 }
2009
2010 private void copy(CharSequence text) {
2011 try {
2012 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
2013 if (clip != null) {
2014 clip.setClipboardText(text);
2015 }
2016 } catch (android.os.RemoteException e) {
2017 Log.e(LOGTAG, "Copy failed", e);
2018 }
2019 }
2020
2021 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08002022 * Resets the browser title-view to whatever it must be
2023 * (for example, if we had a loading error)
2024 * When we have a new page, we call resetTitle, when we
2025 * have to reset the titlebar to whatever it used to be
2026 * (for example, if the user chose to stop loading), we
2027 * call resetTitleAndRevertLockIcon.
2028 */
2029 /* package */ void resetTitleAndRevertLockIcon() {
2030 revertLockIcon();
2031 resetTitleIconAndProgress();
2032 }
2033
2034 /**
2035 * Reset the title, favicon, and progress.
2036 */
2037 private void resetTitleIconAndProgress() {
2038 WebView current = mTabControl.getCurrentWebView();
2039 if (current == null) {
2040 return;
2041 }
2042 resetTitleAndIcon(current);
2043 int progress = current.getProgress();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002044 mWebChromeClient.onProgressChanged(current, progress);
2045 }
2046
2047 // Reset the title and the icon based on the given item.
2048 private void resetTitleAndIcon(WebView view) {
2049 WebHistoryItem item = view.copyBackForwardList().getCurrentItem();
2050 if (item != null) {
Leon Scroggins68579392009-09-15 15:31:54 -04002051 setUrlTitle(item.getUrl(), item.getTitle());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002052 setFavicon(item.getFavicon());
2053 } else {
Leon Scroggins68579392009-09-15 15:31:54 -04002054 setUrlTitle(null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002055 setFavicon(null);
2056 }
2057 }
2058
2059 /**
2060 * Sets a title composed of the URL and the title string.
2061 * @param url The URL of the site being loaded.
2062 * @param title The title of the site being loaded.
2063 */
Leon Scroggins68579392009-09-15 15:31:54 -04002064 private void setUrlTitle(String url, String title) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002065 mUrl = url;
2066 mTitle = title;
2067
Leon Scroggins68579392009-09-15 15:31:54 -04002068 mTitleBar.setTitleAndUrl(title, url);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04002069 if (mFakeTitleBar != null) {
2070 mFakeTitleBar.setTitleAndUrl(title, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002071 }
2072 }
2073
2074 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08002075 * @param url The URL to build a title version of the URL from.
2076 * @return The title version of the URL or null if fails.
2077 * The title version of the URL can be either the URL hostname,
2078 * or the hostname with an "https://" prefix (for secure URLs),
2079 * or an empty string if, for example, the URL in question is a
2080 * file:// URL with no hostname.
2081 */
Leon Scroggins32e14a62009-06-11 10:26:34 -04002082 /* package */ static String buildTitleUrl(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002083 String titleUrl = null;
2084
2085 if (url != null) {
2086 try {
2087 // parse the url string
2088 URL urlObj = new URL(url);
2089 if (urlObj != null) {
2090 titleUrl = "";
2091
2092 String protocol = urlObj.getProtocol();
2093 String host = urlObj.getHost();
2094
2095 if (host != null && 0 < host.length()) {
2096 titleUrl = host;
2097 if (protocol != null) {
2098 // if a secure site, add an "https://" prefix!
2099 if (protocol.equalsIgnoreCase("https")) {
2100 titleUrl = protocol + "://" + host;
2101 }
2102 }
2103 }
2104 }
2105 } catch (MalformedURLException e) {}
2106 }
2107
2108 return titleUrl;
2109 }
2110
2111 // Set the favicon in the title bar.
2112 private void setFavicon(Bitmap icon) {
Leon Scroggins68579392009-09-15 15:31:54 -04002113 mTitleBar.setFavicon(icon);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04002114 if (mFakeTitleBar != null) {
2115 mFakeTitleBar.setFavicon(icon);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002116 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002117 }
2118
2119 /**
2120 * Saves the current lock-icon state before resetting
2121 * the lock icon. If we have an error, we may need to
2122 * roll back to the previous state.
2123 */
2124 private void saveLockIcon() {
2125 mPrevLockType = mLockIconType;
2126 }
2127
2128 /**
2129 * Reverts the lock-icon state to the last saved state,
2130 * for example, if we had an error, and need to cancel
2131 * the load.
2132 */
2133 private void revertLockIcon() {
2134 mLockIconType = mPrevLockType;
2135
Dave Bort31a6d1c2009-04-13 15:56:49 -07002136 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002137 Log.v(LOGTAG, "BrowserActivity.revertLockIcon:" +
2138 " revert lock icon to " + mLockIconType);
2139 }
2140
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04002141 updateLockIconToLatest();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002142 }
2143
Leon Scroggins1f005d32009-08-10 17:36:42 -04002144 /**
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002145 * Close the tab, remove its associated title bar, and adjust mTabControl's
2146 * current tab to a valid value.
Leon Scroggins1f005d32009-08-10 17:36:42 -04002147 */
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002148 /* package */ void closeTab(TabControl.Tab t) {
2149 int currentIndex = mTabControl.getCurrentIndex();
2150 int removeIndex = mTabControl.getTabIndex(t);
Leon Scroggins1f005d32009-08-10 17:36:42 -04002151 mTabControl.removeTab(t);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002152 if (currentIndex >= removeIndex && currentIndex != 0) {
2153 currentIndex--;
2154 }
2155 mTabControl.setCurrentTab(mTabControl.getTab(currentIndex));
Andrei Popescua5bf1de2009-09-23 16:39:23 +01002156 resetTitleIconAndProgress();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002157 }
2158
2159 private void goBackOnePageOrQuit() {
2160 TabControl.Tab current = mTabControl.getCurrentTab();
2161 if (current == null) {
2162 /*
2163 * Instead of finishing the activity, simply push this to the back
2164 * of the stack and let ActivityManager to choose the foreground
2165 * activity. As BrowserActivity is singleTask, it will be always the
2166 * root of the task. So we can use either true or false for
2167 * moveTaskToBack().
2168 */
2169 moveTaskToBack(true);
Grace Kloba00d85e72009-09-23 18:50:05 -07002170 return;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002171 }
2172 WebView w = current.getWebView();
2173 if (w.canGoBack()) {
2174 w.goBack();
2175 } else {
2176 // Check to see if we are closing a window that was created by
2177 // another window. If so, we switch back to that window.
2178 TabControl.Tab parent = current.getParentTab();
2179 if (parent != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04002180 switchToTab(mTabControl.getTabIndex(parent));
2181 // Now we close the other tab
2182 closeTab(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002183 } else {
2184 if (current.closeOnExit()) {
Grace Klobabb0af5c2009-09-01 00:56:09 -07002185 // force mPageStarted to be false as we are going to either
2186 // finish the activity or remove the tab. This will ensure
2187 // pauseWebView() taking action.
2188 mPageStarted = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002189 if (mTabControl.getTabCount() == 1) {
2190 finish();
2191 return;
2192 }
Mike Reed7bfa63b2009-05-28 11:08:32 -04002193 // call pauseWebViewTimers() now, we won't be able to call
2194 // it in onPause() as the WebView won't be valid.
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002195 // Temporarily change mActivityInPause to be true as
2196 // pauseWebViewTimers() will do nothing if mActivityInPause
2197 // is false.
Grace Kloba918e1d72009-08-13 14:55:06 -07002198 boolean savedState = mActivityInPause;
2199 if (savedState) {
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002200 Log.e(LOGTAG, "BrowserActivity is already paused "
2201 + "while handing goBackOnePageOrQuit.");
Grace Kloba918e1d72009-08-13 14:55:06 -07002202 }
2203 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -04002204 pauseWebViewTimers();
Grace Kloba918e1d72009-08-13 14:55:06 -07002205 mActivityInPause = savedState;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002206 removeTabFromContentView(current);
2207 mTabControl.removeTab(current);
2208 }
2209 /*
2210 * Instead of finishing the activity, simply push this to the back
2211 * of the stack and let ActivityManager to choose the foreground
2212 * activity. As BrowserActivity is singleTask, it will be always the
2213 * root of the task. So we can use either true or false for
2214 * moveTaskToBack().
2215 */
2216 moveTaskToBack(true);
2217 }
2218 }
2219 }
2220
Grace Kloba5942df02009-09-18 11:48:29 -07002221 @Override
2222 public boolean onKeyDown(int keyCode, KeyEvent event) {
2223 // The default key mode is DEFAULT_KEYS_SEARCH_LOCAL. As the MENU is
2224 // still down, we don't want to trigger the search. Pretend to consume
2225 // the key and do nothing.
2226 if (mMenuIsDown) return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002227
Grace Kloba5942df02009-09-18 11:48:29 -07002228 switch(keyCode) {
2229 case KeyEvent.KEYCODE_MENU:
2230 mMenuIsDown = true;
2231 break;
2232 case KeyEvent.KEYCODE_SPACE:
Grace Klobada0fe552009-09-22 18:17:24 -07002233 // WebView/WebTextView handle the keys in the KeyDown. As
2234 // the Activity's shortcut keys are only handled when WebView
2235 // doesn't, have to do it in onKeyDown instead of onKeyUp.
2236 if (event.isShiftPressed()) {
2237 getTopWindow().pageUp(false);
2238 } else {
2239 getTopWindow().pageDown(false);
2240 }
Grace Kloba5942df02009-09-18 11:48:29 -07002241 return true;
2242 case KeyEvent.KEYCODE_BACK:
2243 if (event.getRepeatCount() == 0) {
2244 event.startTracking();
2245 return true;
2246 } else if (mCustomView == null && mActiveTabsPage == null
2247 && event.isLongPress()) {
2248 bookmarksOrHistoryPicker(true);
2249 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002250 }
Grace Kloba5942df02009-09-18 11:48:29 -07002251 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002252 }
Grace Kloba5942df02009-09-18 11:48:29 -07002253 return super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002254 }
2255
Grace Kloba5942df02009-09-18 11:48:29 -07002256 @Override
2257 public boolean onKeyUp(int keyCode, KeyEvent event) {
2258 switch(keyCode) {
2259 case KeyEvent.KEYCODE_MENU:
2260 mMenuIsDown = false;
2261 break;
Grace Kloba5942df02009-09-18 11:48:29 -07002262 case KeyEvent.KEYCODE_BACK:
2263 if (event.isTracking() && !event.isCanceled()) {
2264 if (mCustomView != null) {
2265 // if a custom view is showing, hide it
2266 mWebChromeClient.onHideCustomView();
2267 } else if (mActiveTabsPage != null) {
2268 // if tab page is showing, hide it
2269 removeActiveTabPage(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002270 } else {
Grace Kloba5942df02009-09-18 11:48:29 -07002271 WebView subwindow = mTabControl.getCurrentSubWindow();
2272 if (subwindow != null) {
2273 if (subwindow.canGoBack()) {
2274 subwindow.goBack();
2275 } else {
2276 dismissSubWindow(mTabControl.getCurrentTab());
2277 }
2278 } else {
2279 goBackOnePageOrQuit();
2280 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002281 }
Grace Kloba5942df02009-09-18 11:48:29 -07002282 return true;
2283 }
2284 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002285 }
Grace Kloba5942df02009-09-18 11:48:29 -07002286 return super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002287 }
2288
Leon Scroggins68579392009-09-15 15:31:54 -04002289 /* package */ void stopLoading() {
Ben Murdochb7cc8b42009-09-28 10:59:09 +01002290 mDidStopLoad = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002291 resetTitleAndRevertLockIcon();
2292 WebView w = getTopWindow();
2293 w.stopLoading();
2294 mWebViewClient.onPageFinished(w, w.getUrl());
2295
2296 cancelStopToast();
2297 mStopToast = Toast
2298 .makeText(this, R.string.stopping, Toast.LENGTH_SHORT);
2299 mStopToast.show();
2300 }
2301
2302 private void cancelStopToast() {
2303 if (mStopToast != null) {
2304 mStopToast.cancel();
2305 mStopToast = null;
2306 }
2307 }
2308
2309 // called by a non-UI thread to post the message
2310 public void postMessage(int what, int arg1, int arg2, Object obj) {
2311 mHandler.sendMessage(mHandler.obtainMessage(what, arg1, arg2, obj));
2312 }
2313
2314 // public message ids
2315 public final static int LOAD_URL = 1001;
2316 public final static int STOP_LOAD = 1002;
2317
2318 // Message Ids
2319 private static final int FOCUS_NODE_HREF = 102;
2320 private static final int CANCEL_CREDS_REQUEST = 103;
Grace Kloba92c18a52009-07-31 23:48:32 -07002321 private static final int RELEASE_WAKELOCK = 107;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002322
Ben Murdoch2694e232009-09-29 09:41:11 +01002323 private static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
2324
The Android Open Source Project0c908882009-03-03 19:32:16 -08002325 // Private handler for handling javascript and saving passwords
2326 private Handler mHandler = new Handler() {
2327
2328 public void handleMessage(Message msg) {
2329 switch (msg.what) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002330 case FOCUS_NODE_HREF:
Ben Murdoch2694e232009-09-29 09:41:11 +01002331 {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002332 String url = (String) msg.getData().get("url");
2333 if (url == null || url.length() == 0) {
2334 break;
2335 }
2336 HashMap focusNodeMap = (HashMap) msg.obj;
2337 WebView view = (WebView) focusNodeMap.get("webview");
2338 // Only apply the action if the top window did not change.
2339 if (getTopWindow() != view) {
2340 break;
2341 }
2342 switch (msg.arg1) {
2343 case R.id.open_context_menu_id:
2344 case R.id.view_image_context_menu_id:
2345 loadURL(getTopWindow(), url);
2346 break;
2347 case R.id.open_newtab_context_menu_id:
Grace Klobac9181842009-04-14 08:53:22 -07002348 final TabControl.Tab parent = mTabControl
2349 .getCurrentTab();
2350 final TabControl.Tab newTab = openTab(url);
2351 if (newTab != parent) {
2352 parent.addChildTab(newTab);
2353 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002354 break;
2355 case R.id.bookmark_context_menu_id:
2356 Intent intent = new Intent(BrowserActivity.this,
2357 AddBookmarkPage.class);
2358 intent.putExtra("url", url);
2359 startActivity(intent);
2360 break;
2361 case R.id.share_link_context_menu_id:
Andrei Popescu10fdba82009-09-24 13:25:47 +01002362 Browser.sendString(BrowserActivity.this, url,
2363 getText(R.string.choosertitle_sharevia).toString());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002364 break;
2365 case R.id.copy_link_context_menu_id:
2366 copy(url);
2367 break;
2368 case R.id.save_link_context_menu_id:
2369 case R.id.download_context_menu_id:
2370 onDownloadStartNoStream(url, null, null, null, -1);
2371 break;
2372 }
2373 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002374 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002375
2376 case LOAD_URL:
2377 loadURL(getTopWindow(), (String) msg.obj);
2378 break;
2379
2380 case STOP_LOAD:
2381 stopLoading();
2382 break;
2383
2384 case CANCEL_CREDS_REQUEST:
2385 resumeAfterCredentials();
2386 break;
2387
The Android Open Source Project0c908882009-03-03 19:32:16 -08002388 case RELEASE_WAKELOCK:
2389 if (mWakeLock.isHeld()) {
2390 mWakeLock.release();
2391 }
2392 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002393
2394 case UPDATE_BOOKMARK_THUMBNAIL:
2395 WebView view = (WebView) msg.obj;
2396 if (view != null) {
2397 updateScreenshot(view);
2398 }
2399 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002400 }
2401 }
2402 };
2403
Leon Scroggins89c6d362009-07-15 16:54:37 -04002404 private void updateScreenshot(WebView view) {
2405 // If this is a bookmarked site, add a screenshot to the database.
2406 // FIXME: When should we update? Every time?
2407 // FIXME: Would like to make sure there is actually something to
2408 // draw, but the API for that (WebViewCore.pictureReady()) is not
2409 // currently accessible here.
Ben Murdochaac7aa62009-09-17 16:57:40 +01002410
Patrick Scott3918d442009-08-04 13:22:29 -04002411 ContentResolver cr = getContentResolver();
2412 final Cursor c = BrowserBookmarksAdapter.queryBookmarksForUrl(
Ben Murdochaac7aa62009-09-17 16:57:40 +01002413 cr, view.getOriginalUrl(), view.getUrl(), true);
Patrick Scott3918d442009-08-04 13:22:29 -04002414 if (c != null) {
Leon Scroggins89c6d362009-07-15 16:54:37 -04002415 boolean succeed = c.moveToFirst();
2416 ContentValues values = null;
2417 while (succeed) {
2418 if (values == null) {
2419 final ByteArrayOutputStream os
2420 = new ByteArrayOutputStream();
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002421 Bitmap bm = createScreenshot(view);
Leon Scroggins45800572009-09-29 16:38:47 -04002422 if (bm == null) {
2423 c.close();
2424 return;
2425 }
Leon Scroggins89c6d362009-07-15 16:54:37 -04002426 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
2427 values = new ContentValues();
2428 values.put(Browser.BookmarkColumns.THUMBNAIL,
2429 os.toByteArray());
2430 }
2431 cr.update(ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
2432 c.getInt(0)), values, null, null);
2433 succeed = c.moveToNext();
2434 }
2435 c.close();
2436 }
2437 }
2438
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002439 /**
Leon Scrogginsf8551612009-09-24 16:06:02 -04002440 * Values for the size of the thumbnail created when taking a screenshot.
2441 * Lazily initialized. Instead of using these directly, use
2442 * getDesiredThumbnailWidth() or getDesiredThumbnailHeight().
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002443 */
Leon Scrogginsf8551612009-09-24 16:06:02 -04002444 private static int THUMBNAIL_WIDTH = 0;
2445 private static int THUMBNAIL_HEIGHT = 0;
2446
2447 /**
2448 * Return the desired width for thumbnail screenshots, which are stored in
2449 * the database, and used on the bookmarks screen.
2450 * @param context Context for finding out the density of the screen.
2451 * @return int desired width for thumbnail screenshot.
2452 */
2453 /* package */ static int getDesiredThumbnailWidth(Context context) {
2454 if (THUMBNAIL_WIDTH == 0) {
2455 float density = context.getResources().getDisplayMetrics().density;
2456 THUMBNAIL_WIDTH = (int) (90 * density);
2457 THUMBNAIL_HEIGHT = (int) (80 * density);
2458 }
2459 return THUMBNAIL_WIDTH;
2460 }
2461
2462 /**
2463 * Return the desired height for thumbnail screenshots, which are stored in
2464 * the database, and used on the bookmarks screen.
2465 * @param context Context for finding out the density of the screen.
2466 * @return int desired height for thumbnail screenshot.
2467 */
2468 /* package */ static int getDesiredThumbnailHeight(Context context) {
2469 // To ensure that they are both initialized.
2470 getDesiredThumbnailWidth(context);
2471 return THUMBNAIL_HEIGHT;
2472 }
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002473
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002474 private Bitmap createScreenshot(WebView view) {
2475 Picture thumbnail = view.capturePicture();
Leon Scroggins45800572009-09-29 16:38:47 -04002476 if (thumbnail == null) {
2477 return null;
2478 }
Leon Scrogginsf8551612009-09-24 16:06:02 -04002479 Bitmap bm = Bitmap.createBitmap(getDesiredThumbnailWidth(this),
2480 getDesiredThumbnailHeight(this), Bitmap.Config.ARGB_4444);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002481 Canvas canvas = new Canvas(bm);
2482 // May need to tweak these values to determine what is the
2483 // best scale factor
Ben Murdoch2694e232009-09-29 09:41:11 +01002484 int thumbnailWidth = thumbnail.getWidth();
2485 if (thumbnailWidth > 0) {
2486 float scaleFactor = (float) getDesiredThumbnailWidth(this) /
2487 (float)thumbnailWidth;
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002488 canvas.scale(scaleFactor, scaleFactor);
2489 }
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002490 thumbnail.draw(canvas);
2491 return bm;
2492 }
2493
The Android Open Source Project0c908882009-03-03 19:32:16 -08002494 // -------------------------------------------------------------------------
2495 // WebViewClient implementation.
2496 //-------------------------------------------------------------------------
2497
2498 // Use in overrideUrlLoading
2499 /* package */ final static String SCHEME_WTAI = "wtai://wp/";
2500 /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
2501 /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;";
2502 /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;";
2503
2504 /* package */ WebViewClient getWebViewClient() {
2505 return mWebViewClient;
2506 }
2507
Patrick Scott3918d442009-08-04 13:22:29 -04002508 private void updateIcon(WebView view, Bitmap icon) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002509 if (icon != null) {
2510 BrowserBookmarksAdapter.updateBookmarkFavicon(mResolver,
Patrick Scott15525d42009-09-21 13:39:37 -04002511 view.getOriginalUrl(), view.getUrl(), icon);
2512 }
2513 setFavicon(icon);
2514 }
2515
2516 private void updateIcon(String url, Bitmap icon) {
2517 if (icon != null) {
2518 BrowserBookmarksAdapter.updateBookmarkFavicon(mResolver,
2519 null, url, icon);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002520 }
2521 setFavicon(icon);
2522 }
2523
2524 private final WebViewClient mWebViewClient = new WebViewClient() {
2525 @Override
2526 public void onPageStarted(WebView view, String url, Bitmap favicon) {
2527 resetLockIcon(url);
Leon Scroggins68579392009-09-15 15:31:54 -04002528 setUrlTitle(url, null);
Ben Murdochbff2d602009-07-01 20:19:05 +01002529
Ben Murdoch2694e232009-09-29 09:41:11 +01002530 // We've started to load a new page. If there was a pending message
2531 // to save a screenshot then we will now take the new page and
2532 // save an incorrect screenshot. Therefore, remove any pending
2533 // thumbnail messages from the queue.
2534 mHandler.removeMessages(UPDATE_BOOKMARK_THUMBNAIL);
2535
Patrick Scott59ce8302009-09-18 16:29:38 -04002536 // If we start a touch icon load and then load a new page, we don't
2537 // want to cancel the current touch icon loader. But, we do want to
2538 // create a new one when the touch icon url is known.
2539 if (mTouchIconLoader != null) {
2540 mTouchIconLoader.mActivity = null;
2541 mTouchIconLoader = null;
2542 }
2543
Ben Murdochbff2d602009-07-01 20:19:05 +01002544 ErrorConsoleView errorConsole = mTabControl.getCurrentErrorConsole(false);
2545 if (errorConsole != null) {
2546 errorConsole.clearErrorMessages();
2547 if (mShouldShowErrorConsole) {
2548 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
2549 }
2550 }
2551
The Android Open Source Project0c908882009-03-03 19:32:16 -08002552 // Call updateIcon instead of setFavicon so the bookmark
2553 // database can be updated.
Patrick Scott15525d42009-09-21 13:39:37 -04002554 updateIcon(url, favicon);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002555
Grace Kloba4d7880f2009-08-12 09:35:42 -07002556 if (mSettings.isTracing()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002557 String host;
2558 try {
2559 WebAddress uri = new WebAddress(url);
2560 host = uri.mHost;
2561 } catch (android.net.ParseException ex) {
Grace Kloba4d7880f2009-08-12 09:35:42 -07002562 host = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -08002563 }
2564 host = host.replace('.', '_');
Grace Kloba4d7880f2009-08-12 09:35:42 -07002565 host += ".trace";
The Android Open Source Project0c908882009-03-03 19:32:16 -08002566 mInTrace = true;
Grace Kloba4d7880f2009-08-12 09:35:42 -07002567 Debug.startMethodTracing(host, 20 * 1024 * 1024);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002568 }
2569
2570 // Performance probe
2571 if (false) {
2572 mStart = SystemClock.uptimeMillis();
2573 mProcessStart = Process.getElapsedCpuTime();
2574 long[] sysCpu = new long[7];
2575 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2576 sysCpu, null)) {
2577 mUserStart = sysCpu[0] + sysCpu[1];
2578 mSystemStart = sysCpu[2];
2579 mIdleStart = sysCpu[3];
2580 mIrqStart = sysCpu[4] + sysCpu[5] + sysCpu[6];
2581 }
2582 mUiStart = SystemClock.currentThreadTimeMillis();
2583 }
2584
2585 if (!mPageStarted) {
2586 mPageStarted = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -04002587 // if onResume() has been called, resumeWebViewTimers() does
2588 // nothing.
2589 resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002590 }
2591
2592 // reset sync timer to avoid sync starts during loading a page
2593 CookieSyncManager.getInstance().resetSync();
2594
2595 mInLoad = true;
Ben Murdochb7cc8b42009-09-28 10:59:09 +01002596 mDidStopLoad = false;
Leon Scroggins184f5e32009-09-21 10:38:24 -04002597 showFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002598 updateInLoadMenuItems();
2599 if (!mIsNetworkUp) {
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04002600 createAndShowNetworkDialog();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002601 if (view != null) {
2602 view.setNetworkAvailable(false);
2603 }
2604 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002605 }
2606
2607 @Override
2608 public void onPageFinished(WebView view, String url) {
2609 // Reset the title and icon in case we stopped a provisional
2610 // load.
2611 resetTitleAndIcon(view);
Ben Murdochb7cc8b42009-09-28 10:59:09 +01002612
2613 if (!mDidStopLoad) {
2614 // Only update the bookmark screenshot if the user did not
2615 // cancel the load early.
Ben Murdoch2694e232009-09-29 09:41:11 +01002616 Message updateScreenshot = Message.obtain(mHandler, UPDATE_BOOKMARK_THUMBNAIL, view);
2617 mHandler.sendMessageDelayed(updateScreenshot, 500);
Ben Murdochb7cc8b42009-09-28 10:59:09 +01002618 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002619
2620 // Update the lock icon image only once we are done loading
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04002621 updateLockIconToLatest();
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -04002622
The Android Open Source Project0c908882009-03-03 19:32:16 -08002623 // Performance probe
The Android Open Source Projectcb9a0bb2009-03-11 12:11:58 -07002624 if (false) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002625 long[] sysCpu = new long[7];
2626 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2627 sysCpu, null)) {
2628 String uiInfo = "UI thread used "
2629 + (SystemClock.currentThreadTimeMillis() - mUiStart)
2630 + " ms";
Dave Bort31a6d1c2009-04-13 15:56:49 -07002631 if (LOGD_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002632 Log.d(LOGTAG, uiInfo);
2633 }
2634 //The string that gets written to the log
2635 String performanceString = "It took total "
2636 + (SystemClock.uptimeMillis() - mStart)
2637 + " ms clock time to load the page."
2638 + "\nbrowser process used "
2639 + (Process.getElapsedCpuTime() - mProcessStart)
2640 + " ms, user processes used "
2641 + (sysCpu[0] + sysCpu[1] - mUserStart) * 10
2642 + " ms, kernel used "
2643 + (sysCpu[2] - mSystemStart) * 10
2644 + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10
2645 + " ms and irq took "
2646 + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart)
2647 * 10 + " ms, " + uiInfo;
Dave Bort31a6d1c2009-04-13 15:56:49 -07002648 if (LOGD_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002649 Log.d(LOGTAG, performanceString + "\nWebpage: " + url);
2650 }
2651 if (url != null) {
2652 // strip the url to maintain consistency
2653 String newUrl = new String(url);
2654 if (newUrl.startsWith("http://www.")) {
2655 newUrl = newUrl.substring(11);
2656 } else if (newUrl.startsWith("http://")) {
2657 newUrl = newUrl.substring(7);
2658 } else if (newUrl.startsWith("https://www.")) {
2659 newUrl = newUrl.substring(12);
2660 } else if (newUrl.startsWith("https://")) {
2661 newUrl = newUrl.substring(8);
2662 }
Dave Bort31a6d1c2009-04-13 15:56:49 -07002663 if (LOGD_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002664 Log.d(LOGTAG, newUrl + " loaded");
2665 }
2666 /*
2667 if (sWhiteList.contains(newUrl)) {
2668 // The string that gets pushed to the statistcs
2669 // service
2670 performanceString = performanceString
2671 + "\nWebpage: "
2672 + newUrl
2673 + "\nCarrier: "
2674 + android.os.SystemProperties
2675 .get("gsm.sim.operator.alpha");
2676 if (mWebView != null
2677 && mWebView.getContext() != null
2678 && mWebView.getContext().getSystemService(
2679 Context.CONNECTIVITY_SERVICE) != null) {
2680 ConnectivityManager cManager =
2681 (ConnectivityManager) mWebView
2682 .getContext().getSystemService(
2683 Context.CONNECTIVITY_SERVICE);
2684 NetworkInfo nInfo = cManager
2685 .getActiveNetworkInfo();
2686 if (nInfo != null) {
2687 performanceString = performanceString
2688 + "\nNetwork Type: "
2689 + nInfo.getType().toString();
2690 }
2691 }
2692 Checkin.logEvent(mResolver,
2693 Checkin.Events.Tag.WEBPAGE_LOAD,
2694 performanceString);
2695 Log.w(LOGTAG, "pushed to the statistics service");
2696 }
2697 */
2698 }
2699 }
2700 }
2701
2702 if (mInTrace) {
2703 mInTrace = false;
2704 Debug.stopMethodTracing();
2705 }
2706
2707 if (mPageStarted) {
2708 mPageStarted = false;
Mike Reed7bfa63b2009-05-28 11:08:32 -04002709 // pauseWebViewTimers() will do nothing and return false if
2710 // onPause() is not called yet.
2711 if (pauseWebViewTimers()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002712 if (mWakeLock.isHeld()) {
2713 mHandler.removeMessages(RELEASE_WAKELOCK);
2714 mWakeLock.release();
2715 }
2716 }
2717 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002718 }
2719
2720 // return true if want to hijack the url to let another app to handle it
2721 @Override
2722 public boolean shouldOverrideUrlLoading(WebView view, String url) {
2723 if (url.startsWith(SCHEME_WTAI)) {
2724 // wtai://wp/mc;number
2725 // number=string(phone-number)
2726 if (url.startsWith(SCHEME_WTAI_MC)) {
2727 Intent intent = new Intent(Intent.ACTION_VIEW,
2728 Uri.parse(WebView.SCHEME_TEL +
2729 url.substring(SCHEME_WTAI_MC.length())));
2730 startActivity(intent);
2731 return true;
2732 }
2733 // wtai://wp/sd;dtmf
2734 // dtmf=string(dialstring)
2735 if (url.startsWith(SCHEME_WTAI_SD)) {
2736 // TODO
2737 // only send when there is active voice connection
2738 return false;
2739 }
2740 // wtai://wp/ap;number;name
2741 // number=string(phone-number)
2742 // name=string
2743 if (url.startsWith(SCHEME_WTAI_AP)) {
2744 // TODO
2745 return false;
2746 }
2747 }
2748
Dianne Hackborn99189432009-06-17 18:06:18 -07002749 // The "about:" schemes are internal to the browser; don't
2750 // want these to be dispatched to other apps.
2751 if (url.startsWith("about:")) {
2752 return false;
2753 }
Ben Murdochbff2d602009-07-01 20:19:05 +01002754
Dianne Hackborn99189432009-06-17 18:06:18 -07002755 Intent intent;
Ben Murdochbff2d602009-07-01 20:19:05 +01002756
Dianne Hackborn99189432009-06-17 18:06:18 -07002757 // perform generic parsing of the URI to turn it into an Intent.
The Android Open Source Project0c908882009-03-03 19:32:16 -08002758 try {
Dianne Hackborn99189432009-06-17 18:06:18 -07002759 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
2760 } catch (URISyntaxException ex) {
2761 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002762 return false;
2763 }
2764
Grace Kloba5b078b52009-06-24 20:23:41 -07002765 // check whether the intent can be resolved. If not, we will see
2766 // whether we can download it from the Market.
2767 if (getPackageManager().resolveActivity(intent, 0) == null) {
2768 String packagename = intent.getPackage();
2769 if (packagename != null) {
2770 intent = new Intent(Intent.ACTION_VIEW, Uri
2771 .parse("market://search?q=pname:" + packagename));
2772 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2773 startActivity(intent);
2774 return true;
2775 } else {
2776 return false;
2777 }
2778 }
2779
Dianne Hackborn99189432009-06-17 18:06:18 -07002780 // sanitize the Intent, ensuring web pages can not bypass browser
2781 // security (only access to BROWSABLE activities).
The Android Open Source Project0c908882009-03-03 19:32:16 -08002782 intent.addCategory(Intent.CATEGORY_BROWSABLE);
Dianne Hackborn99189432009-06-17 18:06:18 -07002783 intent.setComponent(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002784 try {
2785 if (startActivityIfNeeded(intent, -1)) {
2786 return true;
2787 }
2788 } catch (ActivityNotFoundException ex) {
2789 // ignore the error. If no application can handle the URL,
2790 // eg about:blank, assume the browser can handle it.
2791 }
2792
2793 if (mMenuIsDown) {
2794 openTab(url);
2795 closeOptionsMenu();
2796 return true;
2797 }
2798
2799 return false;
2800 }
2801
2802 /**
2803 * Updates the lock icon. This method is called when we discover another
2804 * resource to be loaded for this page (for example, javascript). While
2805 * we update the icon type, we do not update the lock icon itself until
2806 * we are done loading, it is slightly more secure this way.
2807 */
2808 @Override
2809 public void onLoadResource(WebView view, String url) {
2810 if (url != null && url.length() > 0) {
2811 // It is only if the page claims to be secure
2812 // that we may have to update the lock:
2813 if (mLockIconType == LOCK_ICON_SECURE) {
2814 // If NOT a 'safe' url, change the lock to mixed content!
2815 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url) || URLUtil.isAboutUrl(url))) {
2816 mLockIconType = LOCK_ICON_MIXED;
Dave Bort31a6d1c2009-04-13 15:56:49 -07002817 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002818 Log.v(LOGTAG, "BrowserActivity.updateLockIcon:" +
2819 " updated lock icon to " + mLockIconType + " due to " + url);
2820 }
2821 }
2822 }
2823 }
2824 }
2825
2826 /**
2827 * Show the dialog, asking the user if they would like to continue after
2828 * an excessive number of HTTP redirects.
2829 */
2830 @Override
2831 public void onTooManyRedirects(WebView view, final Message cancelMsg,
2832 final Message continueMsg) {
2833 new AlertDialog.Builder(BrowserActivity.this)
2834 .setTitle(R.string.browserFrameRedirect)
2835 .setMessage(R.string.browserFrame307Post)
2836 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
2837 public void onClick(DialogInterface dialog, int which) {
2838 continueMsg.sendToTarget();
2839 }})
2840 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
2841 public void onClick(DialogInterface dialog, int which) {
2842 cancelMsg.sendToTarget();
2843 }})
2844 .setOnCancelListener(new OnCancelListener() {
2845 public void onCancel(DialogInterface dialog) {
2846 cancelMsg.sendToTarget();
2847 }})
2848 .show();
2849 }
2850
Patrick Scott37911c72009-03-24 18:02:58 -07002851 // Container class for the next error dialog that needs to be
2852 // displayed.
2853 class ErrorDialog {
2854 public final int mTitle;
2855 public final String mDescription;
2856 public final int mError;
2857 ErrorDialog(int title, String desc, int error) {
2858 mTitle = title;
2859 mDescription = desc;
2860 mError = error;
2861 }
2862 };
2863
2864 private void processNextError() {
2865 if (mQueuedErrors == null) {
2866 return;
2867 }
2868 // The first one is currently displayed so just remove it.
2869 mQueuedErrors.removeFirst();
2870 if (mQueuedErrors.size() == 0) {
2871 mQueuedErrors = null;
2872 return;
2873 }
2874 showError(mQueuedErrors.getFirst());
2875 }
2876
2877 private DialogInterface.OnDismissListener mDialogListener =
2878 new DialogInterface.OnDismissListener() {
2879 public void onDismiss(DialogInterface d) {
2880 processNextError();
2881 }
2882 };
2883 private LinkedList<ErrorDialog> mQueuedErrors;
2884
2885 private void queueError(int err, String desc) {
2886 if (mQueuedErrors == null) {
2887 mQueuedErrors = new LinkedList<ErrorDialog>();
2888 }
2889 for (ErrorDialog d : mQueuedErrors) {
2890 if (d.mError == err) {
2891 // Already saw a similar error, ignore the new one.
2892 return;
2893 }
2894 }
2895 ErrorDialog errDialog = new ErrorDialog(
Patrick Scott5d61a6c2009-08-25 13:52:46 -04002896 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
Patrick Scott37911c72009-03-24 18:02:58 -07002897 R.string.browserFrameFileErrorLabel :
2898 R.string.browserFrameNetworkErrorLabel,
2899 desc, err);
2900 mQueuedErrors.addLast(errDialog);
2901
2902 // Show the dialog now if the queue was empty.
2903 if (mQueuedErrors.size() == 1) {
2904 showError(errDialog);
2905 }
2906 }
2907
2908 private void showError(ErrorDialog errDialog) {
2909 AlertDialog d = new AlertDialog.Builder(BrowserActivity.this)
2910 .setTitle(errDialog.mTitle)
2911 .setMessage(errDialog.mDescription)
2912 .setPositiveButton(R.string.ok, null)
2913 .create();
2914 d.setOnDismissListener(mDialogListener);
2915 d.show();
2916 }
2917
The Android Open Source Project0c908882009-03-03 19:32:16 -08002918 /**
2919 * Show a dialog informing the user of the network error reported by
2920 * WebCore.
2921 */
2922 @Override
2923 public void onReceivedError(WebView view, int errorCode,
2924 String description, String failingUrl) {
Patrick Scott5d61a6c2009-08-25 13:52:46 -04002925 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
2926 errorCode != WebViewClient.ERROR_CONNECT &&
2927 errorCode != WebViewClient.ERROR_BAD_URL &&
2928 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
2929 errorCode != WebViewClient.ERROR_FILE) {
Patrick Scott37911c72009-03-24 18:02:58 -07002930 queueError(errorCode, description);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002931 }
Patrick Scott37911c72009-03-24 18:02:58 -07002932 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
2933 + " " + description);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002934
2935 // We need to reset the title after an error.
2936 resetTitleAndRevertLockIcon();
2937 }
2938
2939 /**
2940 * Check with the user if it is ok to resend POST data as the page they
2941 * are trying to navigate to is the result of a POST.
2942 */
2943 @Override
2944 public void onFormResubmission(WebView view, final Message dontResend,
2945 final Message resend) {
2946 new AlertDialog.Builder(BrowserActivity.this)
2947 .setTitle(R.string.browserFrameFormResubmitLabel)
2948 .setMessage(R.string.browserFrameFormResubmitMessage)
2949 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
2950 public void onClick(DialogInterface dialog, int which) {
2951 resend.sendToTarget();
2952 }})
2953 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
2954 public void onClick(DialogInterface dialog, int which) {
2955 dontResend.sendToTarget();
2956 }})
2957 .setOnCancelListener(new OnCancelListener() {
2958 public void onCancel(DialogInterface dialog) {
2959 dontResend.sendToTarget();
2960 }})
2961 .show();
2962 }
2963
2964 /**
2965 * Insert the url into the visited history database.
2966 * @param url The url to be inserted.
2967 * @param isReload True if this url is being reloaded.
2968 * FIXME: Not sure what to do when reloading the page.
2969 */
2970 @Override
2971 public void doUpdateVisitedHistory(WebView view, String url,
2972 boolean isReload) {
2973 if (url.regionMatches(true, 0, "about:", 0, 6)) {
2974 return;
2975 }
Grace Kloba6b52a552009-09-03 16:29:56 -07002976 // remove "client" before updating it to the history so that it wont
2977 // show up in the auto-complete list.
2978 int index = url.indexOf("client=ms-");
2979 if (index > 0 && url.contains(".google.")) {
2980 int end = url.indexOf('&', index);
2981 if (end > 0) {
Grace Klobaef292152009-10-07 19:15:59 -07002982 url = url.substring(0, index)
2983 .concat(url.substring(end + 1));
Grace Kloba6b52a552009-09-03 16:29:56 -07002984 } else {
Grace Klobaef292152009-10-07 19:15:59 -07002985 // the url.charAt(index-1) should be either '?' or '&'
Grace Kloba6b52a552009-09-03 16:29:56 -07002986 url = url.substring(0, index-1);
2987 }
2988 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002989 Browser.updateVisitedHistory(mResolver, url, true);
2990 WebIconDatabase.getInstance().retainIconForPageUrl(url);
2991 }
2992
2993 /**
2994 * Displays SSL error(s) dialog to the user.
2995 */
2996 @Override
2997 public void onReceivedSslError(
2998 final WebView view, final SslErrorHandler handler, final SslError error) {
2999
3000 if (mSettings.showSecurityWarnings()) {
3001 final LayoutInflater factory =
3002 LayoutInflater.from(BrowserActivity.this);
3003 final View warningsView =
3004 factory.inflate(R.layout.ssl_warnings, null);
3005 final LinearLayout placeholder =
3006 (LinearLayout)warningsView.findViewById(R.id.placeholder);
3007
3008 if (error.hasError(SslError.SSL_UNTRUSTED)) {
3009 LinearLayout ll = (LinearLayout)factory
3010 .inflate(R.layout.ssl_warning, null);
3011 ((TextView)ll.findViewById(R.id.warning))
3012 .setText(R.string.ssl_untrusted);
3013 placeholder.addView(ll);
3014 }
3015
3016 if (error.hasError(SslError.SSL_IDMISMATCH)) {
3017 LinearLayout ll = (LinearLayout)factory
3018 .inflate(R.layout.ssl_warning, null);
3019 ((TextView)ll.findViewById(R.id.warning))
3020 .setText(R.string.ssl_mismatch);
3021 placeholder.addView(ll);
3022 }
3023
3024 if (error.hasError(SslError.SSL_EXPIRED)) {
3025 LinearLayout ll = (LinearLayout)factory
3026 .inflate(R.layout.ssl_warning, null);
3027 ((TextView)ll.findViewById(R.id.warning))
3028 .setText(R.string.ssl_expired);
3029 placeholder.addView(ll);
3030 }
3031
3032 if (error.hasError(SslError.SSL_NOTYETVALID)) {
3033 LinearLayout ll = (LinearLayout)factory
3034 .inflate(R.layout.ssl_warning, null);
3035 ((TextView)ll.findViewById(R.id.warning))
3036 .setText(R.string.ssl_not_yet_valid);
3037 placeholder.addView(ll);
3038 }
3039
3040 new AlertDialog.Builder(BrowserActivity.this)
3041 .setTitle(R.string.security_warning)
3042 .setIcon(android.R.drawable.ic_dialog_alert)
3043 .setView(warningsView)
3044 .setPositiveButton(R.string.ssl_continue,
3045 new DialogInterface.OnClickListener() {
3046 public void onClick(DialogInterface dialog, int whichButton) {
3047 handler.proceed();
3048 }
3049 })
3050 .setNeutralButton(R.string.view_certificate,
3051 new DialogInterface.OnClickListener() {
3052 public void onClick(DialogInterface dialog, int whichButton) {
3053 showSSLCertificateOnError(view, handler, error);
3054 }
3055 })
3056 .setNegativeButton(R.string.cancel,
3057 new DialogInterface.OnClickListener() {
3058 public void onClick(DialogInterface dialog, int whichButton) {
3059 handler.cancel();
3060 BrowserActivity.this.resetTitleAndRevertLockIcon();
3061 }
3062 })
3063 .setOnCancelListener(
3064 new DialogInterface.OnCancelListener() {
3065 public void onCancel(DialogInterface dialog) {
3066 handler.cancel();
3067 BrowserActivity.this.resetTitleAndRevertLockIcon();
3068 }
3069 })
3070 .show();
3071 } else {
3072 handler.proceed();
3073 }
3074 }
3075
3076 /**
3077 * Handles an HTTP authentication request.
3078 *
3079 * @param handler The authentication handler
3080 * @param host The host
3081 * @param realm The realm
3082 */
3083 @Override
3084 public void onReceivedHttpAuthRequest(WebView view,
3085 final HttpAuthHandler handler, final String host, final String realm) {
3086 String username = null;
3087 String password = null;
3088
3089 boolean reuseHttpAuthUsernamePassword =
3090 handler.useHttpAuthUsernamePassword();
3091
3092 if (reuseHttpAuthUsernamePassword &&
3093 (mTabControl.getCurrentWebView() != null)) {
3094 String[] credentials =
3095 mTabControl.getCurrentWebView()
3096 .getHttpAuthUsernamePassword(host, realm);
3097 if (credentials != null && credentials.length == 2) {
3098 username = credentials[0];
3099 password = credentials[1];
3100 }
3101 }
3102
3103 if (username != null && password != null) {
3104 handler.proceed(username, password);
3105 } else {
3106 showHttpAuthentication(handler, host, realm, null, null, null, 0);
3107 }
3108 }
3109
3110 @Override
3111 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
3112 if (mMenuIsDown) {
3113 // only check shortcut key when MENU is held
3114 return getWindow().isShortcutKey(event.getKeyCode(), event);
3115 } else {
3116 return false;
3117 }
3118 }
3119
3120 @Override
3121 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
3122 if (view != mTabControl.getCurrentTopWebView()) {
3123 return;
3124 }
3125 if (event.isDown()) {
3126 BrowserActivity.this.onKeyDown(event.getKeyCode(), event);
3127 } else {
3128 BrowserActivity.this.onKeyUp(event.getKeyCode(), event);
3129 }
3130 }
3131 };
3132
3133 //--------------------------------------------------------------------------
3134 // WebChromeClient implementation
3135 //--------------------------------------------------------------------------
3136
3137 /* package */ WebChromeClient getWebChromeClient() {
3138 return mWebChromeClient;
3139 }
3140
3141 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
3142 // Helper method to create a new tab or sub window.
3143 private void createWindow(final boolean dialog, final Message msg) {
3144 if (dialog) {
3145 mTabControl.createSubWindow();
3146 final TabControl.Tab t = mTabControl.getCurrentTab();
3147 attachSubWindow(t);
3148 WebView.WebViewTransport transport =
3149 (WebView.WebViewTransport) msg.obj;
3150 transport.setWebView(t.getSubWebView());
3151 msg.sendToTarget();
3152 } else {
3153 final TabControl.Tab parent = mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04003154 final TabControl.Tab newTab
3155 = openTabAndShow(EMPTY_URL_DATA, false, null);
Grace Klobac9181842009-04-14 08:53:22 -07003156 if (newTab != parent) {
3157 parent.addChildTab(newTab);
3158 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003159 WebView.WebViewTransport transport =
3160 (WebView.WebViewTransport) msg.obj;
3161 transport.setWebView(mTabControl.getCurrentWebView());
Leon Scroggins1f005d32009-08-10 17:36:42 -04003162 msg.sendToTarget();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003163 }
3164 }
3165
3166 @Override
3167 public boolean onCreateWindow(WebView view, final boolean dialog,
3168 final boolean userGesture, final Message resultMsg) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003169 // Short-circuit if we can't create any more tabs or sub windows.
3170 if (dialog && mTabControl.getCurrentSubWindow() != null) {
3171 new AlertDialog.Builder(BrowserActivity.this)
3172 .setTitle(R.string.too_many_subwindows_dialog_title)
3173 .setIcon(android.R.drawable.ic_dialog_alert)
3174 .setMessage(R.string.too_many_subwindows_dialog_message)
3175 .setPositiveButton(R.string.ok, null)
3176 .show();
3177 return false;
3178 } else if (mTabControl.getTabCount() >= TabControl.MAX_TABS) {
3179 new AlertDialog.Builder(BrowserActivity.this)
3180 .setTitle(R.string.too_many_windows_dialog_title)
3181 .setIcon(android.R.drawable.ic_dialog_alert)
3182 .setMessage(R.string.too_many_windows_dialog_message)
3183 .setPositiveButton(R.string.ok, null)
3184 .show();
3185 return false;
3186 }
3187
3188 // Short-circuit if this was a user gesture.
3189 if (userGesture) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003190 createWindow(dialog, resultMsg);
3191 return true;
3192 }
3193
3194 // Allow the popup and create the appropriate window.
3195 final AlertDialog.OnClickListener allowListener =
3196 new AlertDialog.OnClickListener() {
3197 public void onClick(DialogInterface d,
3198 int which) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003199 createWindow(dialog, resultMsg);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003200 }
3201 };
3202
3203 // Block the popup by returning a null WebView.
3204 final AlertDialog.OnClickListener blockListener =
3205 new AlertDialog.OnClickListener() {
3206 public void onClick(DialogInterface d, int which) {
3207 resultMsg.sendToTarget();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003208 }
3209 };
3210
3211 // Build a confirmation dialog to display to the user.
3212 final AlertDialog d =
3213 new AlertDialog.Builder(BrowserActivity.this)
3214 .setTitle(R.string.attention)
3215 .setIcon(android.R.drawable.ic_dialog_alert)
3216 .setMessage(R.string.popup_window_attempt)
3217 .setPositiveButton(R.string.allow, allowListener)
3218 .setNegativeButton(R.string.block, blockListener)
3219 .setCancelable(false)
3220 .create();
3221
3222 // Show the confirmation dialog.
3223 d.show();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003224 return true;
3225 }
3226
3227 @Override
3228 public void onCloseWindow(WebView window) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04003229 final TabControl.Tab current = mTabControl.getCurrentTab();
3230 final TabControl.Tab parent = current.getParentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003231 if (parent != null) {
3232 // JavaScript can only close popup window.
Leon Scroggins1f005d32009-08-10 17:36:42 -04003233 switchToTab(mTabControl.getTabIndex(parent));
3234 // Now we need to close the window
3235 closeTab(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003236 }
3237 }
3238
3239 @Override
3240 public void onProgressChanged(WebView view, int newProgress) {
Leon Scroggins68579392009-09-15 15:31:54 -04003241 mTitleBar.setProgress(newProgress);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003242 if (mFakeTitleBar != null) {
3243 mFakeTitleBar.setProgress(newProgress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003244 }
3245
3246 if (newProgress == 100) {
Ben Murdochaac7aa62009-09-17 16:57:40 +01003247 // onProgressChanged() may continue to be called after the main
3248 // frame has finished loading, as any remaining sub frames
3249 // continue to load. We'll only get called once though with
3250 // newProgress as 100 when everything is loaded.
3251 // (onPageFinished is called once when the main frame completes
3252 // loading regardless of the state of any sub frames so calls
3253 // to onProgressChanges may continue after onPageFinished has
3254 // executed)
3255
3256 // sync cookies and cache promptly here.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003257 CookieSyncManager.getInstance().sync();
The Android Open Source Projectcb9a0bb2009-03-11 12:11:58 -07003258 if (mInLoad) {
3259 mInLoad = false;
3260 updateInLoadMenuItems();
Leon Scrogginsa27ff192009-09-14 12:58:04 -04003261 // If the options menu is open, leave the title bar
3262 if (!mOptionsMenuOpen || !mIconView) {
3263 hideFakeTitleBar();
3264 }
The Android Open Source Projectcb9a0bb2009-03-11 12:11:58 -07003265 }
Leon Scrogginsa27ff192009-09-14 12:58:04 -04003266 } else if (!mInLoad) {
The Android Open Source Projectcb9a0bb2009-03-11 12:11:58 -07003267 // onPageFinished may have already been called but a subframe
3268 // is still loading and updating the progress. Reset mInLoad
3269 // and update the menu items.
Leon Scrogginsa27ff192009-09-14 12:58:04 -04003270 mInLoad = true;
3271 updateInLoadMenuItems();
Leon Scroggins184f5e32009-09-21 10:38:24 -04003272 if (!mOptionsMenuOpen || mIconView) {
Leon Scrogginsa27ff192009-09-14 12:58:04 -04003273 // This page has begun to load, so show the title bar
3274 showFakeTitleBar();
The Android Open Source Projectcb9a0bb2009-03-11 12:11:58 -07003275 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003276 }
3277 }
3278
3279 @Override
3280 public void onReceivedTitle(WebView view, String title) {
Patrick Scott598c9cc2009-06-04 11:10:38 -04003281 String url = view.getUrl();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003282
3283 // here, if url is null, we want to reset the title
Leon Scroggins68579392009-09-15 15:31:54 -04003284 setUrlTitle(url, title);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003285
3286 if (url == null ||
3287 url.length() >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
3288 return;
3289 }
Leon Scrogginsfce182b2009-05-08 13:54:52 -04003290 // See if we can find the current url in our history database and
3291 // add the new title to it.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003292 if (url.startsWith("http://www.")) {
3293 url = url.substring(11);
3294 } else if (url.startsWith("http://")) {
3295 url = url.substring(4);
3296 }
3297 try {
3298 url = "%" + url;
3299 String [] selArgs = new String[] { url };
3300
3301 String where = Browser.BookmarkColumns.URL + " LIKE ? AND "
3302 + Browser.BookmarkColumns.BOOKMARK + " = 0";
3303 Cursor c = mResolver.query(Browser.BOOKMARKS_URI,
3304 Browser.HISTORY_PROJECTION, where, selArgs, null);
3305 if (c.moveToFirst()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003306 // Current implementation of database only has one entry per
3307 // url.
Leon Scrogginsfce182b2009-05-08 13:54:52 -04003308 ContentValues map = new ContentValues();
3309 map.put(Browser.BookmarkColumns.TITLE, title);
3310 mResolver.update(Browser.BOOKMARKS_URI, map,
3311 "_id = " + c.getInt(0), null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003312 }
3313 c.close();
3314 } catch (IllegalStateException e) {
3315 Log.e(LOGTAG, "BrowserActivity onReceived title", e);
3316 } catch (SQLiteException ex) {
3317 Log.e(LOGTAG, "onReceivedTitle() caught SQLiteException: ", ex);
3318 }
3319 }
3320
3321 @Override
3322 public void onReceivedIcon(WebView view, Bitmap icon) {
Patrick Scott3918d442009-08-04 13:22:29 -04003323 updateIcon(view, icon);
3324 }
3325
3326 @Override
Patrick Scott59ce8302009-09-18 16:29:38 -04003327 public void onReceivedTouchIconUrl(WebView view, String url,
3328 boolean precomposed) {
Patrick Scott3918d442009-08-04 13:22:29 -04003329 final ContentResolver cr = getContentResolver();
3330 final Cursor c =
3331 BrowserBookmarksAdapter.queryBookmarksForUrl(cr,
Leon Scrogginsa5d669e2009-08-05 14:07:58 -04003332 view.getOriginalUrl(), view.getUrl(), true);
Patrick Scott3918d442009-08-04 13:22:29 -04003333 if (c != null) {
3334 if (c.getCount() > 0) {
Patrick Scott59ce8302009-09-18 16:29:38 -04003335 // Let precomposed icons take precedence over non-composed
3336 // icons.
3337 if (precomposed && mTouchIconLoader != null) {
3338 mTouchIconLoader.cancel(false);
3339 mTouchIconLoader = null;
3340 }
3341 // Have only one async task at a time.
3342 if (mTouchIconLoader == null) {
3343 mTouchIconLoader = new DownloadTouchIcon(
3344 BrowserActivity.this, cr, c, view);
3345 mTouchIconLoader.execute(url);
3346 }
Patrick Scott3918d442009-08-04 13:22:29 -04003347 } else {
3348 c.close();
3349 }
3350 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003351 }
Ben Murdoch092dd5d2009-04-22 12:34:12 +01003352
Andrei Popescuadc008d2009-06-26 14:11:30 +01003353 @Override
Andrei Popescuc9b55562009-07-07 10:51:15 +01003354 public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
Andrei Popescuadc008d2009-06-26 14:11:30 +01003355 if (mCustomView != null)
3356 return;
3357
3358 // Add the custom view to its container.
3359 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
3360 mCustomView = view;
Andrei Popescuc9b55562009-07-07 10:51:15 +01003361 mCustomViewCallback = callback;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003362 // Save the menu state and set it to empty while the custom
3363 // view is showing.
3364 mOldMenuState = mMenuState;
3365 mMenuState = EMPTY_MENU;
Andrei Popescuc9b55562009-07-07 10:51:15 +01003366 // Hide the content view.
3367 mContentView.setVisibility(View.GONE);
Andrei Popescuadc008d2009-06-26 14:11:30 +01003368 // Finally show the custom view container.
Andrei Popescuc9b55562009-07-07 10:51:15 +01003369 mCustomViewContainer.setVisibility(View.VISIBLE);
3370 mCustomViewContainer.bringToFront();
Andrei Popescuadc008d2009-06-26 14:11:30 +01003371 }
3372
3373 @Override
3374 public void onHideCustomView() {
3375 if (mCustomView == null)
3376 return;
3377
Andrei Popescuc9b55562009-07-07 10:51:15 +01003378 // Hide the custom view.
3379 mCustomView.setVisibility(View.GONE);
Andrei Popescuadc008d2009-06-26 14:11:30 +01003380 // Remove the custom view from its container.
3381 mCustomViewContainer.removeView(mCustomView);
3382 mCustomView = null;
3383 // Reset the old menu state.
3384 mMenuState = mOldMenuState;
3385 mOldMenuState = EMPTY_MENU;
3386 mCustomViewContainer.setVisibility(View.GONE);
Andrei Popescuc9b55562009-07-07 10:51:15 +01003387 mCustomViewCallback.onCustomViewHidden();
3388 // Show the content view.
3389 mContentView.setVisibility(View.VISIBLE);
Andrei Popescuadc008d2009-06-26 14:11:30 +01003390 }
3391
Ben Murdoch092dd5d2009-04-22 12:34:12 +01003392 /**
Andrei Popescu79e82b72009-07-27 12:01:59 +01003393 * The origin has exceeded its database quota.
Ben Murdoch092dd5d2009-04-22 12:34:12 +01003394 * @param url the URL that exceeded the quota
3395 * @param databaseIdentifier the identifier of the database on
3396 * which the transaction that caused the quota overflow was run
3397 * @param currentQuota the current quota for the origin.
Ben Murdoch25a15232009-08-25 19:38:07 +01003398 * @param estimatedSize the estimated size of the database.
Andrei Popescu79e82b72009-07-27 12:01:59 +01003399 * @param totalUsedQuota is the sum of all origins' quota.
Ben Murdoch092dd5d2009-04-22 12:34:12 +01003400 * @param quotaUpdater The callback to run when a decision to allow or
3401 * deny quota has been made. Don't forget to call this!
3402 */
3403 @Override
3404 public void onExceededDatabaseQuota(String url,
Ben Murdoch25a15232009-08-25 19:38:07 +01003405 String databaseIdentifier, long currentQuota, long estimatedSize,
3406 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
Andrei Popescu79e82b72009-07-27 12:01:59 +01003407 mSettings.getWebStorageSizeManager().onExceededDatabaseQuota(
Ben Murdoch25a15232009-08-25 19:38:07 +01003408 url, databaseIdentifier, currentQuota, estimatedSize,
3409 totalUsedQuota, quotaUpdater);
Andrei Popescu79e82b72009-07-27 12:01:59 +01003410 }
3411
3412 /**
3413 * The Application Cache has exceeded its max size.
3414 * @param spaceNeeded is the amount of disk space that would be needed
3415 * in order for the last appcache operation to succeed.
3416 * @param totalUsedQuota is the sum of all origins' quota.
3417 * @param quotaUpdater A callback to inform the WebCore thread that a new
3418 * app cache size is available. This callback must always be executed at
3419 * some point to ensure that the sleeping WebCore thread is woken up.
3420 */
3421 @Override
3422 public void onReachedMaxAppCacheSize(long spaceNeeded,
3423 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
3424 mSettings.getWebStorageSizeManager().onReachedMaxAppCacheSize(
3425 spaceNeeded, totalUsedQuota, quotaUpdater);
Ben Murdoch092dd5d2009-04-22 12:34:12 +01003426 }
Ben Murdoch7db26342009-06-03 18:21:19 +01003427
Steve Block2bc69912009-07-30 14:45:13 +01003428 /**
3429 * Instructs the browser to show a prompt to ask the user to set the
3430 * Geolocation permission state for the specified origin.
3431 * @param origin The origin for which Geolocation permissions are
3432 * requested.
3433 * @param callback The callback to call once the user has set the
3434 * Geolocation permission state.
3435 */
3436 @Override
3437 public void onGeolocationPermissionsShowPrompt(String origin,
3438 GeolocationPermissions.Callback callback) {
3439 mTabControl.getCurrentTab().getGeolocationPermissionsPrompt().show(
3440 origin, callback);
3441 }
3442
3443 /**
3444 * Instructs the browser to hide the Geolocation permissions prompt.
3445 */
3446 @Override
3447 public void onGeolocationPermissionsHidePrompt() {
3448 mTabControl.getCurrentTab().getGeolocationPermissionsPrompt().hide();
3449 }
3450
Ben Murdoch7db26342009-06-03 18:21:19 +01003451 /* Adds a JavaScript error message to the system log.
3452 * @param message The error message to report.
3453 * @param lineNumber The line number of the error.
3454 * @param sourceID The name of the source file that caused the error.
3455 */
3456 @Override
3457 public void addMessageToConsole(String message, int lineNumber, String sourceID) {
Ben Murdochbff2d602009-07-01 20:19:05 +01003458 ErrorConsoleView errorConsole = mTabControl.getCurrentErrorConsole(true);
3459 errorConsole.addErrorMessage(message, sourceID, lineNumber);
3460 if (mShouldShowErrorConsole &&
3461 errorConsole.getShowState() != ErrorConsoleView.SHOW_MAXIMIZED) {
3462 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
3463 }
3464 Log.w(LOGTAG, "Console: " + message + " " + sourceID + ":" + lineNumber);
Ben Murdoch7db26342009-06-03 18:21:19 +01003465 }
Andrei Popescu540035d2009-09-18 18:59:20 +01003466
3467 /**
3468 * Ask the browser for an icon to represent a <video> element.
3469 * This icon will be used if the Web page did not specify a poster attribute.
3470 *
3471 * @return Bitmap The icon or null if no such icon is available.
3472 * @hide pending API Council approval
3473 */
3474 @Override
3475 public Bitmap getDefaultVideoPoster() {
3476 if (mDefaultVideoPoster == null) {
3477 mDefaultVideoPoster = BitmapFactory.decodeResource(
3478 getResources(), R.drawable.default_video_poster);
3479 }
3480 return mDefaultVideoPoster;
3481 }
3482
3483 /**
3484 * Ask the host application for a custom progress view to show while
3485 * a <video> is loading.
3486 *
3487 * @return View The progress view.
3488 * @hide pending API Council approval
3489 */
3490 @Override
3491 public View getVideoLoadingProgressView() {
3492 if (mVideoProgressView == null) {
3493 LayoutInflater inflater = LayoutInflater.from(BrowserActivity.this);
3494 mVideoProgressView = inflater.inflate(R.layout.video_loading_progress, null);
3495 }
3496 return mVideoProgressView;
3497 }
Leon Clarkecb6cc862009-09-29 18:35:13 +01003498
3499 /**
3500 * Deliver a list of already-visited URLs
3501 * @hide pending API Council approval
3502 */
3503 @Override
3504 public void getVisitedHistory(final ValueCallback<String[]> callback) {
3505 AsyncTask<Void, Void, String[]> task = new AsyncTask<Void, Void, String[]>() {
3506 public String[] doInBackground(Void... unused) {
3507 return Browser.getVisitedHistory(getContentResolver());
3508 }
3509
3510 public void onPostExecute(String[] result) {
3511 callback.onReceiveValue(result);
3512
3513 };
3514 };
3515 task.execute();
3516 };
The Android Open Source Project0c908882009-03-03 19:32:16 -08003517 };
3518
3519 /**
3520 * Notify the host application a download should be done, or that
3521 * the data should be streamed if a streaming viewer is available.
3522 * @param url The full url to the content that should be downloaded
3523 * @param contentDisposition Content-disposition http header, if
3524 * present.
3525 * @param mimetype The mimetype of the content reported by the server
3526 * @param contentLength The file size reported by the server
3527 */
3528 public void onDownloadStart(String url, String userAgent,
3529 String contentDisposition, String mimetype, long contentLength) {
3530 // if we're dealing wih A/V content that's not explicitly marked
3531 // for download, check if it's streamable.
3532 if (contentDisposition == null
Patrick Scotte1fb9662009-08-31 14:31:52 -04003533 || !contentDisposition.regionMatches(
3534 true, 0, "attachment", 0, 10)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003535 // query the package manager to see if there's a registered handler
3536 // that matches.
3537 Intent intent = new Intent(Intent.ACTION_VIEW);
3538 intent.setDataAndType(Uri.parse(url), mimetype);
Patrick Scotte1fb9662009-08-31 14:31:52 -04003539 ResolveInfo info = getPackageManager().resolveActivity(intent,
3540 PackageManager.MATCH_DEFAULT_ONLY);
3541 if (info != null) {
3542 ComponentName myName = getComponentName();
3543 // If we resolved to ourselves, we don't want to attempt to
3544 // load the url only to try and download it again.
3545 if (!myName.getPackageName().equals(
3546 info.activityInfo.packageName)
3547 || !myName.getClassName().equals(
3548 info.activityInfo.name)) {
3549 // someone (other than us) knows how to handle this mime
3550 // type with this scheme, don't download.
3551 try {
3552 startActivity(intent);
3553 return;
3554 } catch (ActivityNotFoundException ex) {
3555 if (LOGD_ENABLED) {
3556 Log.d(LOGTAG, "activity not found for " + mimetype
3557 + " over " + Uri.parse(url).getScheme(),
3558 ex);
3559 }
3560 // Best behavior is to fall back to a download in this
3561 // case
The Android Open Source Project0c908882009-03-03 19:32:16 -08003562 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003563 }
3564 }
3565 }
3566 onDownloadStartNoStream(url, userAgent, contentDisposition, mimetype, contentLength);
3567 }
3568
3569 /**
3570 * Notify the host application a download should be done, even if there
3571 * is a streaming viewer available for thise type.
3572 * @param url The full url to the content that should be downloaded
3573 * @param contentDisposition Content-disposition http header, if
3574 * present.
3575 * @param mimetype The mimetype of the content reported by the server
3576 * @param contentLength The file size reported by the server
3577 */
3578 /*package */ void onDownloadStartNoStream(String url, String userAgent,
3579 String contentDisposition, String mimetype, long contentLength) {
3580
3581 String filename = URLUtil.guessFileName(url,
3582 contentDisposition, mimetype);
3583
3584 // Check to see if we have an SDCard
3585 String status = Environment.getExternalStorageState();
3586 if (!status.equals(Environment.MEDIA_MOUNTED)) {
3587 int title;
3588 String msg;
3589
3590 // Check to see if the SDCard is busy, same as the music app
3591 if (status.equals(Environment.MEDIA_SHARED)) {
3592 msg = getString(R.string.download_sdcard_busy_dlg_msg);
3593 title = R.string.download_sdcard_busy_dlg_title;
3594 } else {
3595 msg = getString(R.string.download_no_sdcard_dlg_msg, filename);
3596 title = R.string.download_no_sdcard_dlg_title;
3597 }
3598
3599 new AlertDialog.Builder(this)
3600 .setTitle(title)
3601 .setIcon(android.R.drawable.ic_dialog_alert)
3602 .setMessage(msg)
3603 .setPositiveButton(R.string.ok, null)
3604 .show();
3605 return;
3606 }
3607
3608 // java.net.URI is a lot stricter than KURL so we have to undo
3609 // KURL's percent-encoding and redo the encoding using java.net.URI.
3610 URI uri = null;
3611 try {
3612 // Undo the percent-encoding that KURL may have done.
3613 String newUrl = new String(URLUtil.decode(url.getBytes()));
3614 // Parse the url into pieces
3615 WebAddress w = new WebAddress(newUrl);
3616 String frag = null;
3617 String query = null;
3618 String path = w.mPath;
3619 // Break the path into path, query, and fragment
3620 if (path.length() > 0) {
3621 // Strip the fragment
3622 int idx = path.lastIndexOf('#');
3623 if (idx != -1) {
3624 frag = path.substring(idx + 1);
3625 path = path.substring(0, idx);
3626 }
3627 idx = path.lastIndexOf('?');
3628 if (idx != -1) {
3629 query = path.substring(idx + 1);
3630 path = path.substring(0, idx);
3631 }
3632 }
3633 uri = new URI(w.mScheme, w.mAuthInfo, w.mHost, w.mPort, path,
3634 query, frag);
3635 } catch (Exception e) {
3636 Log.e(LOGTAG, "Could not parse url for download: " + url, e);
3637 return;
3638 }
3639
3640 // XXX: Have to use the old url since the cookies were stored using the
3641 // old percent-encoded url.
3642 String cookies = CookieManager.getInstance().getCookie(url);
3643
3644 ContentValues values = new ContentValues();
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -07003645 values.put(Downloads.COLUMN_URI, uri.toString());
3646 values.put(Downloads.COLUMN_COOKIE_DATA, cookies);
3647 values.put(Downloads.COLUMN_USER_AGENT, userAgent);
3648 values.put(Downloads.COLUMN_NOTIFICATION_PACKAGE,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003649 getPackageName());
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -07003650 values.put(Downloads.COLUMN_NOTIFICATION_CLASS,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003651 BrowserDownloadPage.class.getCanonicalName());
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -07003652 values.put(Downloads.COLUMN_VISIBILITY, Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
3653 values.put(Downloads.COLUMN_MIME_TYPE, mimetype);
3654 values.put(Downloads.COLUMN_FILE_NAME_HINT, filename);
3655 values.put(Downloads.COLUMN_DESCRIPTION, uri.getHost());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003656 if (contentLength > 0) {
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -07003657 values.put(Downloads.COLUMN_TOTAL_BYTES, contentLength);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003658 }
3659 if (mimetype == null) {
3660 // We must have long pressed on a link or image to download it. We
3661 // are not sure of the mimetype in this case, so do a head request
3662 new FetchUrlMimeType(this).execute(values);
3663 } else {
3664 final Uri contentUri =
3665 getContentResolver().insert(Downloads.CONTENT_URI, values);
3666 viewDownloads(contentUri);
3667 }
3668
3669 }
3670
3671 /**
3672 * Resets the lock icon. This method is called when we start a new load and
3673 * know the url to be loaded.
3674 */
3675 private void resetLockIcon(String url) {
3676 // Save the lock-icon state (we revert to it if the load gets cancelled)
3677 saveLockIcon();
3678
3679 mLockIconType = LOCK_ICON_UNSECURE;
3680 if (URLUtil.isHttpsUrl(url)) {
3681 mLockIconType = LOCK_ICON_SECURE;
Dave Bort31a6d1c2009-04-13 15:56:49 -07003682 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003683 Log.v(LOGTAG, "BrowserActivity.resetLockIcon:" +
3684 " reset lock icon to " + mLockIconType);
3685 }
3686 }
3687
3688 updateLockIconImage(LOCK_ICON_UNSECURE);
3689 }
3690
Grace Klobaeb6eef42009-09-15 17:56:32 -07003691 /* package */ void setLockIconType(int type) {
3692 mLockIconType = type;
3693 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003694
Grace Klobaeb6eef42009-09-15 17:56:32 -07003695 /* package */ int getLockIconType() {
3696 return mLockIconType;
3697 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003698
Grace Klobaeb6eef42009-09-15 17:56:32 -07003699 /* package */ void setPrevLockType(int type) {
3700 mPrevLockType = type;
3701 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003702
Grace Klobaeb6eef42009-09-15 17:56:32 -07003703 /* package */ int getPrevLockType() {
3704 return mPrevLockType;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003705 }
3706
3707 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003708 * Update the lock icon to correspond to our latest state.
3709 */
3710 /* package */ void updateLockIconToLatest() {
3711 updateLockIconImage(mLockIconType);
3712 }
3713
3714 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08003715 * Updates the lock-icon image in the title-bar.
3716 */
3717 private void updateLockIconImage(int lockIconType) {
3718 Drawable d = null;
3719 if (lockIconType == LOCK_ICON_SECURE) {
3720 d = mSecLockIcon;
3721 } else if (lockIconType == LOCK_ICON_MIXED) {
3722 d = mMixLockIcon;
3723 }
Leon Scroggins68579392009-09-15 15:31:54 -04003724 mTitleBar.setLock(d);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003725 if (mFakeTitleBar != null) {
3726 mFakeTitleBar.setLock(d);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003727 }
3728 }
3729
3730 /**
3731 * Displays a page-info dialog.
3732 * @param tab The tab to show info about
3733 * @param fromShowSSLCertificateOnError The flag that indicates whether
3734 * this dialog was opened from the SSL-certificate-on-error dialog or
3735 * not. This is important, since we need to know whether to return to
3736 * the parent dialog or simply dismiss.
3737 */
3738 private void showPageInfo(final TabControl.Tab tab,
3739 final boolean fromShowSSLCertificateOnError) {
3740 final LayoutInflater factory = LayoutInflater
3741 .from(this);
3742
3743 final View pageInfoView = factory.inflate(R.layout.page_info, null);
3744
3745 final WebView view = tab.getWebView();
3746
3747 String url = null;
3748 String title = null;
3749
3750 if (view == null) {
3751 url = tab.getUrl();
3752 title = tab.getTitle();
3753 } else if (view == mTabControl.getCurrentWebView()) {
3754 // Use the cached title and url if this is the current WebView
3755 url = mUrl;
3756 title = mTitle;
3757 } else {
3758 url = view.getUrl();
3759 title = view.getTitle();
3760 }
3761
3762 if (url == null) {
3763 url = "";
3764 }
3765 if (title == null) {
3766 title = "";
3767 }
3768
3769 ((TextView) pageInfoView.findViewById(R.id.address)).setText(url);
3770 ((TextView) pageInfoView.findViewById(R.id.title)).setText(title);
3771
3772 mPageInfoView = tab;
3773 mPageInfoFromShowSSLCertificateOnError = new Boolean(fromShowSSLCertificateOnError);
3774
3775 AlertDialog.Builder alertDialogBuilder =
3776 new AlertDialog.Builder(this)
3777 .setTitle(R.string.page_info).setIcon(android.R.drawable.ic_dialog_info)
3778 .setView(pageInfoView)
3779 .setPositiveButton(
3780 R.string.ok,
3781 new DialogInterface.OnClickListener() {
3782 public void onClick(DialogInterface dialog,
3783 int whichButton) {
3784 mPageInfoDialog = null;
3785 mPageInfoView = null;
3786 mPageInfoFromShowSSLCertificateOnError = null;
3787
3788 // if we came here from the SSL error dialog
3789 if (fromShowSSLCertificateOnError) {
3790 // go back to the SSL error dialog
3791 showSSLCertificateOnError(
3792 mSSLCertificateOnErrorView,
3793 mSSLCertificateOnErrorHandler,
3794 mSSLCertificateOnErrorError);
3795 }
3796 }
3797 })
3798 .setOnCancelListener(
3799 new DialogInterface.OnCancelListener() {
3800 public void onCancel(DialogInterface dialog) {
3801 mPageInfoDialog = null;
3802 mPageInfoView = null;
3803 mPageInfoFromShowSSLCertificateOnError = null;
3804
3805 // if we came here from the SSL error dialog
3806 if (fromShowSSLCertificateOnError) {
3807 // go back to the SSL error dialog
3808 showSSLCertificateOnError(
3809 mSSLCertificateOnErrorView,
3810 mSSLCertificateOnErrorHandler,
3811 mSSLCertificateOnErrorError);
3812 }
3813 }
3814 });
3815
3816 // if we have a main top-level page SSL certificate set or a certificate
3817 // error
3818 if (fromShowSSLCertificateOnError ||
3819 (view != null && view.getCertificate() != null)) {
3820 // add a 'View Certificate' button
3821 alertDialogBuilder.setNeutralButton(
3822 R.string.view_certificate,
3823 new DialogInterface.OnClickListener() {
3824 public void onClick(DialogInterface dialog,
3825 int whichButton) {
3826 mPageInfoDialog = null;
3827 mPageInfoView = null;
3828 mPageInfoFromShowSSLCertificateOnError = null;
3829
3830 // if we came here from the SSL error dialog
3831 if (fromShowSSLCertificateOnError) {
3832 // go back to the SSL error dialog
3833 showSSLCertificateOnError(
3834 mSSLCertificateOnErrorView,
3835 mSSLCertificateOnErrorHandler,
3836 mSSLCertificateOnErrorError);
3837 } else {
3838 // otherwise, display the top-most certificate from
3839 // the chain
3840 if (view.getCertificate() != null) {
3841 showSSLCertificate(tab);
3842 }
3843 }
3844 }
3845 });
3846 }
3847
3848 mPageInfoDialog = alertDialogBuilder.show();
3849 }
3850
3851 /**
3852 * Displays the main top-level page SSL certificate dialog
3853 * (accessible from the Page-Info dialog).
3854 * @param tab The tab to show certificate for.
3855 */
3856 private void showSSLCertificate(final TabControl.Tab tab) {
3857 final View certificateView =
3858 inflateCertificateView(tab.getWebView().getCertificate());
3859 if (certificateView == null) {
3860 return;
3861 }
3862
3863 LayoutInflater factory = LayoutInflater.from(this);
3864
3865 final LinearLayout placeholder =
3866 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3867
3868 LinearLayout ll = (LinearLayout) factory.inflate(
3869 R.layout.ssl_success, placeholder);
3870 ((TextView)ll.findViewById(R.id.success))
3871 .setText(R.string.ssl_certificate_is_valid);
3872
3873 mSSLCertificateView = tab;
3874 mSSLCertificateDialog =
3875 new AlertDialog.Builder(this)
3876 .setTitle(R.string.ssl_certificate).setIcon(
3877 R.drawable.ic_dialog_browser_certificate_secure)
3878 .setView(certificateView)
3879 .setPositiveButton(R.string.ok,
3880 new DialogInterface.OnClickListener() {
3881 public void onClick(DialogInterface dialog,
3882 int whichButton) {
3883 mSSLCertificateDialog = null;
3884 mSSLCertificateView = null;
3885
3886 showPageInfo(tab, false);
3887 }
3888 })
3889 .setOnCancelListener(
3890 new DialogInterface.OnCancelListener() {
3891 public void onCancel(DialogInterface dialog) {
3892 mSSLCertificateDialog = null;
3893 mSSLCertificateView = null;
3894
3895 showPageInfo(tab, false);
3896 }
3897 })
3898 .show();
3899 }
3900
3901 /**
3902 * Displays the SSL error certificate dialog.
3903 * @param view The target web-view.
3904 * @param handler The SSL error handler responsible for cancelling the
3905 * connection that resulted in an SSL error or proceeding per user request.
3906 * @param error The SSL error object.
3907 */
3908 private void showSSLCertificateOnError(
3909 final WebView view, final SslErrorHandler handler, final SslError error) {
3910
3911 final View certificateView =
3912 inflateCertificateView(error.getCertificate());
3913 if (certificateView == null) {
3914 return;
3915 }
3916
3917 LayoutInflater factory = LayoutInflater.from(this);
3918
3919 final LinearLayout placeholder =
3920 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3921
3922 if (error.hasError(SslError.SSL_UNTRUSTED)) {
3923 LinearLayout ll = (LinearLayout)factory
3924 .inflate(R.layout.ssl_warning, placeholder);
3925 ((TextView)ll.findViewById(R.id.warning))
3926 .setText(R.string.ssl_untrusted);
3927 }
3928
3929 if (error.hasError(SslError.SSL_IDMISMATCH)) {
3930 LinearLayout ll = (LinearLayout)factory
3931 .inflate(R.layout.ssl_warning, placeholder);
3932 ((TextView)ll.findViewById(R.id.warning))
3933 .setText(R.string.ssl_mismatch);
3934 }
3935
3936 if (error.hasError(SslError.SSL_EXPIRED)) {
3937 LinearLayout ll = (LinearLayout)factory
3938 .inflate(R.layout.ssl_warning, placeholder);
3939 ((TextView)ll.findViewById(R.id.warning))
3940 .setText(R.string.ssl_expired);
3941 }
3942
3943 if (error.hasError(SslError.SSL_NOTYETVALID)) {
3944 LinearLayout ll = (LinearLayout)factory
3945 .inflate(R.layout.ssl_warning, placeholder);
3946 ((TextView)ll.findViewById(R.id.warning))
3947 .setText(R.string.ssl_not_yet_valid);
3948 }
3949
3950 mSSLCertificateOnErrorHandler = handler;
3951 mSSLCertificateOnErrorView = view;
3952 mSSLCertificateOnErrorError = error;
3953 mSSLCertificateOnErrorDialog =
3954 new AlertDialog.Builder(this)
3955 .setTitle(R.string.ssl_certificate).setIcon(
3956 R.drawable.ic_dialog_browser_certificate_partially_secure)
3957 .setView(certificateView)
3958 .setPositiveButton(R.string.ok,
3959 new DialogInterface.OnClickListener() {
3960 public void onClick(DialogInterface dialog,
3961 int whichButton) {
3962 mSSLCertificateOnErrorDialog = null;
3963 mSSLCertificateOnErrorView = null;
3964 mSSLCertificateOnErrorHandler = null;
3965 mSSLCertificateOnErrorError = null;
3966
3967 mWebViewClient.onReceivedSslError(
3968 view, handler, error);
3969 }
3970 })
3971 .setNeutralButton(R.string.page_info_view,
3972 new DialogInterface.OnClickListener() {
3973 public void onClick(DialogInterface dialog,
3974 int whichButton) {
3975 mSSLCertificateOnErrorDialog = null;
3976
3977 // do not clear the dialog state: we will
3978 // need to show the dialog again once the
3979 // user is done exploring the page-info details
3980
3981 showPageInfo(mTabControl.getTabFromView(view),
3982 true);
3983 }
3984 })
3985 .setOnCancelListener(
3986 new DialogInterface.OnCancelListener() {
3987 public void onCancel(DialogInterface dialog) {
3988 mSSLCertificateOnErrorDialog = null;
3989 mSSLCertificateOnErrorView = null;
3990 mSSLCertificateOnErrorHandler = null;
3991 mSSLCertificateOnErrorError = null;
3992
3993 mWebViewClient.onReceivedSslError(
3994 view, handler, error);
3995 }
3996 })
3997 .show();
3998 }
3999
4000 /**
4001 * Inflates the SSL certificate view (helper method).
4002 * @param certificate The SSL certificate.
4003 * @return The resultant certificate view with issued-to, issued-by,
4004 * issued-on, expires-on, and possibly other fields set.
4005 * If the input certificate is null, returns null.
4006 */
4007 private View inflateCertificateView(SslCertificate certificate) {
4008 if (certificate == null) {
4009 return null;
4010 }
4011
4012 LayoutInflater factory = LayoutInflater.from(this);
4013
4014 View certificateView = factory.inflate(
4015 R.layout.ssl_certificate, null);
4016
4017 // issued to:
4018 SslCertificate.DName issuedTo = certificate.getIssuedTo();
4019 if (issuedTo != null) {
4020 ((TextView) certificateView.findViewById(R.id.to_common))
4021 .setText(issuedTo.getCName());
4022 ((TextView) certificateView.findViewById(R.id.to_org))
4023 .setText(issuedTo.getOName());
4024 ((TextView) certificateView.findViewById(R.id.to_org_unit))
4025 .setText(issuedTo.getUName());
4026 }
4027
4028 // issued by:
4029 SslCertificate.DName issuedBy = certificate.getIssuedBy();
4030 if (issuedBy != null) {
4031 ((TextView) certificateView.findViewById(R.id.by_common))
4032 .setText(issuedBy.getCName());
4033 ((TextView) certificateView.findViewById(R.id.by_org))
4034 .setText(issuedBy.getOName());
4035 ((TextView) certificateView.findViewById(R.id.by_org_unit))
4036 .setText(issuedBy.getUName());
4037 }
4038
4039 // issued on:
4040 String issuedOn = reformatCertificateDate(
4041 certificate.getValidNotBefore());
4042 ((TextView) certificateView.findViewById(R.id.issued_on))
4043 .setText(issuedOn);
4044
4045 // expires on:
4046 String expiresOn = reformatCertificateDate(
4047 certificate.getValidNotAfter());
4048 ((TextView) certificateView.findViewById(R.id.expires_on))
4049 .setText(expiresOn);
4050
4051 return certificateView;
4052 }
4053
4054 /**
4055 * Re-formats the certificate date (Date.toString()) string to
4056 * a properly localized date string.
4057 * @return Properly localized version of the certificate date string and
4058 * the original certificate date string if fails to localize.
4059 * If the original string is null, returns an empty string "".
4060 */
4061 private String reformatCertificateDate(String certificateDate) {
4062 String reformattedDate = null;
4063
4064 if (certificateDate != null) {
4065 Date date = null;
4066 try {
4067 date = java.text.DateFormat.getInstance().parse(certificateDate);
4068 } catch (ParseException e) {
4069 date = null;
4070 }
4071
4072 if (date != null) {
4073 reformattedDate =
4074 DateFormat.getDateFormat(this).format(date);
4075 }
4076 }
4077
4078 return reformattedDate != null ? reformattedDate :
4079 (certificateDate != null ? certificateDate : "");
4080 }
4081
4082 /**
4083 * Displays an http-authentication dialog.
4084 */
4085 private void showHttpAuthentication(final HttpAuthHandler handler,
4086 final String host, final String realm, final String title,
4087 final String name, final String password, int focusId) {
4088 LayoutInflater factory = LayoutInflater.from(this);
4089 final View v = factory
4090 .inflate(R.layout.http_authentication, null);
4091 if (name != null) {
4092 ((EditText) v.findViewById(R.id.username_edit)).setText(name);
4093 }
4094 if (password != null) {
4095 ((EditText) v.findViewById(R.id.password_edit)).setText(password);
4096 }
4097
4098 String titleText = title;
4099 if (titleText == null) {
4100 titleText = getText(R.string.sign_in_to).toString().replace(
4101 "%s1", host).replace("%s2", realm);
4102 }
4103
4104 mHttpAuthHandler = handler;
4105 AlertDialog dialog = new AlertDialog.Builder(this)
4106 .setTitle(titleText)
4107 .setIcon(android.R.drawable.ic_dialog_alert)
4108 .setView(v)
4109 .setPositiveButton(R.string.action,
4110 new DialogInterface.OnClickListener() {
4111 public void onClick(DialogInterface dialog,
4112 int whichButton) {
4113 String nm = ((EditText) v
4114 .findViewById(R.id.username_edit))
4115 .getText().toString();
4116 String pw = ((EditText) v
4117 .findViewById(R.id.password_edit))
4118 .getText().toString();
4119 BrowserActivity.this.setHttpAuthUsernamePassword
4120 (host, realm, nm, pw);
4121 handler.proceed(nm, pw);
4122 mHttpAuthenticationDialog = null;
4123 mHttpAuthHandler = null;
4124 }})
4125 .setNegativeButton(R.string.cancel,
4126 new DialogInterface.OnClickListener() {
4127 public void onClick(DialogInterface dialog,
4128 int whichButton) {
4129 handler.cancel();
4130 BrowserActivity.this.resetTitleAndRevertLockIcon();
4131 mHttpAuthenticationDialog = null;
4132 mHttpAuthHandler = null;
4133 }})
4134 .setOnCancelListener(new DialogInterface.OnCancelListener() {
4135 public void onCancel(DialogInterface dialog) {
4136 handler.cancel();
4137 BrowserActivity.this.resetTitleAndRevertLockIcon();
4138 mHttpAuthenticationDialog = null;
4139 mHttpAuthHandler = null;
4140 }})
4141 .create();
4142 // Make the IME appear when the dialog is displayed if applicable.
4143 dialog.getWindow().setSoftInputMode(
4144 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
4145 dialog.show();
4146 if (focusId != 0) {
4147 dialog.findViewById(focusId).requestFocus();
4148 } else {
4149 v.findViewById(R.id.username_edit).requestFocus();
4150 }
4151 mHttpAuthenticationDialog = dialog;
4152 }
4153
4154 public int getProgress() {
4155 WebView w = mTabControl.getCurrentWebView();
4156 if (w != null) {
4157 return w.getProgress();
4158 } else {
4159 return 100;
4160 }
4161 }
4162
4163 /**
4164 * Set HTTP authentication password.
4165 *
4166 * @param host The host for the password
4167 * @param realm The realm for the password
4168 * @param username The username for the password. If it is null, it means
4169 * password can't be saved.
4170 * @param password The password
4171 */
4172 public void setHttpAuthUsernamePassword(String host, String realm,
4173 String username,
4174 String password) {
4175 WebView w = mTabControl.getCurrentWebView();
4176 if (w != null) {
4177 w.setHttpAuthUsernamePassword(host, realm, username, password);
4178 }
4179 }
4180
4181 /**
4182 * connectivity manager says net has come or gone... inform the user
4183 * @param up true if net has come up, false if net has gone down
4184 */
4185 public void onNetworkToggle(boolean up) {
4186 if (up == mIsNetworkUp) {
4187 return;
4188 } else if (up) {
4189 mIsNetworkUp = true;
4190 if (mAlertDialog != null) {
4191 mAlertDialog.cancel();
4192 mAlertDialog = null;
4193 }
4194 } else {
4195 mIsNetworkUp = false;
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04004196 if (mInLoad) {
4197 createAndShowNetworkDialog();
4198 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08004199 }
4200 WebView w = mTabControl.getCurrentWebView();
4201 if (w != null) {
4202 w.setNetworkAvailable(up);
4203 }
4204 }
4205
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04004206 // This method shows the network dialog alerting the user that the net is
4207 // down. It will only show the dialog if mAlertDialog is null.
4208 private void createAndShowNetworkDialog() {
4209 if (mAlertDialog == null) {
4210 mAlertDialog = new AlertDialog.Builder(this)
4211 .setTitle(R.string.loadSuspendedTitle)
4212 .setMessage(R.string.loadSuspended)
4213 .setPositiveButton(R.string.ok, null)
4214 .show();
4215 }
4216 }
4217
The Android Open Source Project0c908882009-03-03 19:32:16 -08004218 @Override
4219 protected void onActivityResult(int requestCode, int resultCode,
4220 Intent intent) {
4221 switch (requestCode) {
4222 case COMBO_PAGE:
4223 if (resultCode == RESULT_OK && intent != null) {
4224 String data = intent.getAction();
4225 Bundle extras = intent.getExtras();
4226 if (extras != null && extras.getBoolean("new_window", false)) {
Leon Scroggins25d35472009-09-15 11:37:27 -04004227 openTab(data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08004228 } else {
4229 final TabControl.Tab currentTab =
4230 mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04004231 dismissSubWindow(currentTab);
4232 if (data != null && data.length() != 0) {
4233 getTopWindow().loadUrl(data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08004234 }
4235 }
4236 }
4237 break;
4238 default:
4239 break;
4240 }
Leon Scroggins30444232009-09-04 18:36:20 -04004241 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08004242 }
4243
4244 /*
4245 * This method is called as a result of the user selecting the options
4246 * menu to see the download window, or when a download changes state. It
4247 * shows the download window ontop of the current window.
4248 */
4249 /* package */ void viewDownloads(Uri downloadRecord) {
4250 Intent intent = new Intent(this,
4251 BrowserDownloadPage.class);
4252 intent.setData(downloadRecord);
4253 startActivityForResult(intent, this.DOWNLOAD_PAGE);
4254
4255 }
4256
Leon Scroggins160a7e72009-08-14 18:28:01 -04004257 /**
4258 * Open the Go page.
4259 * @param startWithHistory If true, open starting on the history tab.
4260 * Otherwise, start with the bookmarks tab.
Leon Scroggins160a7e72009-08-14 18:28:01 -04004261 */
Leon Scroggins30444232009-09-04 18:36:20 -04004262 /* package */ void bookmarksOrHistoryPicker(boolean startWithHistory) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08004263 WebView current = mTabControl.getCurrentWebView();
4264 if (current == null) {
4265 return;
4266 }
4267 Intent intent = new Intent(this,
4268 CombinedBookmarkHistoryActivity.class);
4269 String title = current.getTitle();
4270 String url = current.getUrl();
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01004271 Bitmap thumbnail = createScreenshot(current);
4272
The Android Open Source Project0c908882009-03-03 19:32:16 -08004273 // Just in case the user opens bookmarks before a page finishes loading
4274 // so the current history item, and therefore the page, is null.
4275 if (null == url) {
4276 url = mLastEnteredUrl;
4277 // This can happen.
4278 if (null == url) {
4279 url = mSettings.getHomePage();
4280 }
4281 }
4282 // In case the web page has not yet received its associated title.
4283 if (title == null) {
4284 title = url;
4285 }
4286 intent.putExtra("title", title);
4287 intent.putExtra("url", url);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01004288 intent.putExtra("thumbnail", thumbnail);
Leon Scroggins30444232009-09-04 18:36:20 -04004289 // Disable opening in a new window if we have maxed out the windows
4290 intent.putExtra("disable_new_window", mTabControl.getTabCount()
4291 >= TabControl.MAX_TABS);
Patrick Scott3918d442009-08-04 13:22:29 -04004292 intent.putExtra("touch_icon_url", current.getTouchIconUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08004293 if (startWithHistory) {
4294 intent.putExtra(CombinedBookmarkHistoryActivity.STARTING_TAB,
4295 CombinedBookmarkHistoryActivity.HISTORY_TAB);
4296 }
4297 startActivityForResult(intent, COMBO_PAGE);
4298 }
4299
4300 // Called when loading from context menu or LOAD_URL message
4301 private void loadURL(WebView view, String url) {
4302 // In case the user enters nothing.
4303 if (url != null && url.length() != 0 && view != null) {
4304 url = smartUrlFilter(url);
4305 if (!mWebViewClient.shouldOverrideUrlLoading(view, url)) {
4306 view.loadUrl(url);
4307 }
4308 }
4309 }
4310
The Android Open Source Project0c908882009-03-03 19:32:16 -08004311 private String smartUrlFilter(Uri inUri) {
4312 if (inUri != null) {
4313 return smartUrlFilter(inUri.toString());
4314 }
4315 return null;
4316 }
4317
4318
4319 // get window count
4320
4321 int getWindowCount(){
4322 if(mTabControl != null){
4323 return mTabControl.getTabCount();
4324 }
4325 return 0;
4326 }
4327
Feng Qianb34f87a2009-03-24 21:27:26 -07004328 protected static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile(
The Android Open Source Project0c908882009-03-03 19:32:16 -08004329 "(?i)" + // switch on case insensitive matching
4330 "(" + // begin group for schema
4331 "(?:http|https|file):\\/\\/" +
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004332 "|(?:inline|data|about|content|javascript):" +
The Android Open Source Project0c908882009-03-03 19:32:16 -08004333 ")" +
4334 "(.*)" );
4335
4336 /**
4337 * Attempts to determine whether user input is a URL or search
4338 * terms. Anything with a space is passed to search.
4339 *
4340 * Converts to lowercase any mistakenly uppercased schema (i.e.,
4341 * "Http://" converts to "http://"
4342 *
4343 * @return Original or modified URL
4344 *
4345 */
4346 String smartUrlFilter(String url) {
4347
4348 String inUrl = url.trim();
4349 boolean hasSpace = inUrl.indexOf(' ') != -1;
4350
4351 Matcher matcher = ACCEPTED_URI_SCHEMA.matcher(inUrl);
4352 if (matcher.matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08004353 // force scheme to lowercase
4354 String scheme = matcher.group(1);
4355 String lcScheme = scheme.toLowerCase();
4356 if (!lcScheme.equals(scheme)) {
Mitsuru Oshima123ecfb2009-05-18 19:11:14 -07004357 inUrl = lcScheme + matcher.group(2);
4358 }
4359 if (hasSpace) {
4360 inUrl = inUrl.replace(" ", "%20");
The Android Open Source Project0c908882009-03-03 19:32:16 -08004361 }
4362 return inUrl;
4363 }
4364 if (hasSpace) {
Satish Sampath565505b2009-05-29 15:37:27 +01004365 // FIXME: Is this the correct place to add to searches?
4366 // what if someone else calls this function?
4367 int shortcut = parseUrlShortcut(inUrl);
4368 if (shortcut != SHORTCUT_INVALID) {
4369 Browser.addSearchUrl(mResolver, inUrl);
4370 String query = inUrl.substring(2);
4371 switch (shortcut) {
4372 case SHORTCUT_GOOGLE_SEARCH:
Grace Kloba47fdfdb2009-06-30 11:15:34 -07004373 return URLUtil.composeSearchUrl(query, QuickSearch_G, QUERY_PLACE_HOLDER);
Satish Sampath565505b2009-05-29 15:37:27 +01004374 case SHORTCUT_WIKIPEDIA_SEARCH:
4375 return URLUtil.composeSearchUrl(query, QuickSearch_W, QUERY_PLACE_HOLDER);
4376 case SHORTCUT_DICTIONARY_SEARCH:
4377 return URLUtil.composeSearchUrl(query, QuickSearch_D, QUERY_PLACE_HOLDER);
4378 case SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH:
The Android Open Source Project0c908882009-03-03 19:32:16 -08004379 // FIXME: we need location in this case
Satish Sampath565505b2009-05-29 15:37:27 +01004380 return URLUtil.composeSearchUrl(query, QuickSearch_L, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08004381 }
4382 }
4383 } else {
4384 if (Regex.WEB_URL_PATTERN.matcher(inUrl).matches()) {
4385 return URLUtil.guessUrl(inUrl);
4386 }
4387 }
4388
4389 Browser.addSearchUrl(mResolver, inUrl);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07004390 return URLUtil.composeSearchUrl(inUrl, QuickSearch_G, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08004391 }
4392
Ben Murdochbff2d602009-07-01 20:19:05 +01004393 /* package */ void setShouldShowErrorConsole(boolean flag) {
4394 if (flag == mShouldShowErrorConsole) {
4395 // Nothing to do.
4396 return;
4397 }
4398
4399 mShouldShowErrorConsole = flag;
4400
4401 ErrorConsoleView errorConsole = mTabControl.getCurrentErrorConsole(true);
4402
4403 if (flag) {
4404 // Setting the show state of the console will cause it's the layout to be inflated.
4405 if (errorConsole.numberOfErrors() > 0) {
4406 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
4407 } else {
4408 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
4409 }
4410
4411 // Now we can add it to the main view.
4412 mErrorConsoleContainer.addView(errorConsole,
4413 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
4414 ViewGroup.LayoutParams.WRAP_CONTENT));
4415 } else {
4416 mErrorConsoleContainer.removeView(errorConsole);
4417 }
4418
4419 }
4420
Grace Klobaeb6eef42009-09-15 17:56:32 -07004421 final static int LOCK_ICON_UNSECURE = 0;
4422 final static int LOCK_ICON_SECURE = 1;
4423 final static int LOCK_ICON_MIXED = 2;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004424
4425 private int mLockIconType = LOCK_ICON_UNSECURE;
4426 private int mPrevLockType = LOCK_ICON_UNSECURE;
4427
4428 private BrowserSettings mSettings;
4429 private TabControl mTabControl;
4430 private ContentResolver mResolver;
4431 private FrameLayout mContentView;
Andrei Popescuadc008d2009-06-26 14:11:30 +01004432 private View mCustomView;
4433 private FrameLayout mCustomViewContainer;
Andrei Popescuc9b55562009-07-07 10:51:15 +01004434 private WebChromeClient.CustomViewCallback mCustomViewCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004435
4436 // FIXME, temp address onPrepareMenu performance problem. When we move everything out of
4437 // view, we should rewrite this.
4438 private int mCurrentMenuState = 0;
4439 private int mMenuState = R.id.MAIN_MENU;
Andrei Popescuadc008d2009-06-26 14:11:30 +01004440 private int mOldMenuState = EMPTY_MENU;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004441 private static final int EMPTY_MENU = -1;
4442 private Menu mMenu;
4443
4444 private FindDialog mFindDialog;
4445 // Used to prevent chording to result in firing two shortcuts immediately
4446 // one after another. Fixes bug 1211714.
4447 boolean mCanChord;
4448
4449 private boolean mInLoad;
4450 private boolean mIsNetworkUp;
Ben Murdochb7cc8b42009-09-28 10:59:09 +01004451 private boolean mDidStopLoad;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004452
4453 private boolean mPageStarted;
4454 private boolean mActivityInPause = true;
4455
4456 private boolean mMenuIsDown;
4457
The Android Open Source Project0c908882009-03-03 19:32:16 -08004458 private static boolean mInTrace;
4459
4460 // Performance probe
4461 private static final int[] SYSTEM_CPU_FORMAT = new int[] {
4462 Process.PROC_SPACE_TERM | Process.PROC_COMBINE,
4463 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time
4464 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time
4465 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time
4466 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time
4467 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time
4468 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time
4469 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG // 7: softirq time
4470 };
4471
4472 private long mStart;
4473 private long mProcessStart;
4474 private long mUserStart;
4475 private long mSystemStart;
4476 private long mIdleStart;
4477 private long mIrqStart;
4478
4479 private long mUiStart;
4480
4481 private Drawable mMixLockIcon;
4482 private Drawable mSecLockIcon;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004483
4484 /* hold a ref so we can auto-cancel if necessary */
4485 private AlertDialog mAlertDialog;
4486
4487 // Wait for credentials before loading google.com
4488 private ProgressDialog mCredsDlg;
4489
4490 // The up-to-date URL and title (these can be different from those stored
4491 // in WebView, since it takes some time for the information in WebView to
4492 // get updated)
4493 private String mUrl;
4494 private String mTitle;
4495
4496 // As PageInfo has different style for landscape / portrait, we have
4497 // to re-open it when configuration changed
4498 private AlertDialog mPageInfoDialog;
4499 private TabControl.Tab mPageInfoView;
4500 // If the Page-Info dialog is launched from the SSL-certificate-on-error
4501 // dialog, we should not just dismiss it, but should get back to the
4502 // SSL-certificate-on-error dialog. This flag is used to store this state
4503 private Boolean mPageInfoFromShowSSLCertificateOnError;
4504
4505 // as SSLCertificateOnError has different style for landscape / portrait,
4506 // we have to re-open it when configuration changed
4507 private AlertDialog mSSLCertificateOnErrorDialog;
4508 private WebView mSSLCertificateOnErrorView;
4509 private SslErrorHandler mSSLCertificateOnErrorHandler;
4510 private SslError mSSLCertificateOnErrorError;
4511
4512 // as SSLCertificate has different style for landscape / portrait, we
4513 // have to re-open it when configuration changed
4514 private AlertDialog mSSLCertificateDialog;
4515 private TabControl.Tab mSSLCertificateView;
4516
4517 // as HttpAuthentication has different style for landscape / portrait, we
4518 // have to re-open it when configuration changed
4519 private AlertDialog mHttpAuthenticationDialog;
4520 private HttpAuthHandler mHttpAuthHandler;
4521
4522 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
4523 new FrameLayout.LayoutParams(
4524 ViewGroup.LayoutParams.FILL_PARENT,
4525 ViewGroup.LayoutParams.FILL_PARENT);
Andrei Popescuadc008d2009-06-26 14:11:30 +01004526 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
4527 new FrameLayout.LayoutParams(
4528 ViewGroup.LayoutParams.FILL_PARENT,
4529 ViewGroup.LayoutParams.FILL_PARENT,
4530 Gravity.CENTER);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07004531 // Google search
4532 final static String QuickSearch_G = "http://www.google.com/m?q=%s";
The Android Open Source Project0c908882009-03-03 19:32:16 -08004533 // Wikipedia search
4534 final static String QuickSearch_W = "http://en.wikipedia.org/w/index.php?search=%s&go=Go";
4535 // Dictionary search
4536 final static String QuickSearch_D = "http://dictionary.reference.com/search?q=%s";
4537 // Google Mobile Local search
4538 final static String QuickSearch_L = "http://www.google.com/m/search?site=local&q=%s&near=mountain+view";
4539
4540 final static String QUERY_PLACE_HOLDER = "%s";
4541
4542 // "source" parameter for Google search through search key
4543 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
4544 // "source" parameter for Google search through goto menu
4545 final static String GOOGLE_SEARCH_SOURCE_GOTO = "browser-goto";
4546 // "source" parameter for Google search through simplily type
4547 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
4548 // "source" parameter for Google search suggested by the browser
4549 final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
4550 // "source" parameter for Google search from unknown source
4551 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown";
4552
4553 private final static String LOGTAG = "browser";
4554
The Android Open Source Project0c908882009-03-03 19:32:16 -08004555 private String mLastEnteredUrl;
4556
4557 private PowerManager.WakeLock mWakeLock;
4558 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
4559
4560 private Toast mStopToast;
4561
Leon Scroggins68579392009-09-15 15:31:54 -04004562 private TitleBar mTitleBar;
Leon Scroggins81db3662009-06-04 17:45:11 -04004563
Ben Murdochbff2d602009-07-01 20:19:05 +01004564 private LinearLayout mErrorConsoleContainer = null;
4565 private boolean mShouldShowErrorConsole = false;
4566
The Android Open Source Project0c908882009-03-03 19:32:16 -08004567 // As the ids are dynamically created, we can't guarantee that they will
4568 // be in sequence, so this static array maps ids to a window number.
4569 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
4570 { R.id.window_one_menu_id, R.id.window_two_menu_id, R.id.window_three_menu_id,
4571 R.id.window_four_menu_id, R.id.window_five_menu_id, R.id.window_six_menu_id,
4572 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
4573
4574 // monitor platform changes
4575 private IntentFilter mNetworkStateChangedFilter;
4576 private BroadcastReceiver mNetworkStateIntentReceiver;
4577
Grace Klobab4da0ad2009-05-14 14:45:40 -07004578 private BroadcastReceiver mPackageInstallationReceiver;
4579
Patrick Scott59ce8302009-09-18 16:29:38 -04004580 // AsyncTask for downloading touch icons
4581 /* package */ DownloadTouchIcon mTouchIconLoader;
4582
The Android Open Source Project0c908882009-03-03 19:32:16 -08004583 // activity requestCode
Nicolas Roard78a98e42009-05-11 13:34:17 +01004584 final static int COMBO_PAGE = 1;
4585 final static int DOWNLOAD_PAGE = 2;
4586 final static int PREFERENCES_PAGE = 3;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004587
Andrei Popescu540035d2009-09-18 18:59:20 +01004588 // the default <video> poster
4589 private Bitmap mDefaultVideoPoster;
4590 // the video progress view
4591 private View mVideoProgressView;
4592
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004593 /**
4594 * A UrlData class to abstract how the content will be set to WebView.
4595 * This base class uses loadUrl to show the content.
4596 */
4597 private static class UrlData {
4598 String mUrl;
Grace Kloba60e095c2009-06-16 11:50:55 -07004599 byte[] mPostData;
4600
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004601 UrlData(String url) {
4602 this.mUrl = url;
4603 }
Grace Kloba60e095c2009-06-16 11:50:55 -07004604
4605 void setPostData(byte[] postData) {
4606 mPostData = postData;
4607 }
4608
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004609 boolean isEmpty() {
4610 return mUrl == null || mUrl.length() == 0;
4611 }
4612
Mitsuru Oshima7944b7d2009-06-16 16:34:51 -07004613 public void loadIn(WebView webView) {
Grace Kloba60e095c2009-06-16 11:50:55 -07004614 if (mPostData != null) {
4615 webView.postUrl(mUrl, mPostData);
4616 } else {
4617 webView.loadUrl(mUrl);
4618 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004619 }
4620 };
4621
4622 /**
4623 * A subclass of UrlData class that can display inlined content using
4624 * {@link WebView#loadDataWithBaseURL(String, String, String, String, String)}.
4625 */
4626 private static class InlinedUrlData extends UrlData {
4627 InlinedUrlData(String inlined, String mimeType, String encoding, String failUrl) {
4628 super(failUrl);
4629 mInlined = inlined;
4630 mMimeType = mimeType;
4631 mEncoding = encoding;
4632 }
4633 String mMimeType;
4634 String mInlined;
4635 String mEncoding;
Mitsuru Oshima7944b7d2009-06-16 16:34:51 -07004636 @Override
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004637 boolean isEmpty() {
Ben Murdochbff2d602009-07-01 20:19:05 +01004638 return mInlined == null || mInlined.length() == 0 || super.isEmpty();
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004639 }
4640
Mitsuru Oshima7944b7d2009-06-16 16:34:51 -07004641 @Override
4642 public void loadIn(WebView webView) {
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004643 webView.loadDataWithBaseURL(null, mInlined, mMimeType, mEncoding, mUrl);
4644 }
4645 }
4646
Leon Scroggins1f005d32009-08-10 17:36:42 -04004647 /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08004648}