blob: 6ca29ce093fa8c0b8083bd70e95aee7d1d07240f [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
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.app.Activity;
The Android Open Source Project0c908882009-03-03 19:32:16 -080020import android.app.AlertDialog;
21import android.app.ProgressDialog;
22import android.app.SearchManager;
23import android.content.ActivityNotFoundException;
24import android.content.BroadcastReceiver;
25import android.content.ComponentName;
26import android.content.ContentResolver;
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -040027import android.content.ContentUris;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.content.ContentValues;
29import android.content.Context;
30import android.content.DialogInterface;
31import android.content.Intent;
32import android.content.IntentFilter;
33import android.content.ServiceConnection;
Grace Klobab4da0ad2009-05-14 14:45:40 -070034import android.content.pm.PackageInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.content.pm.PackageManager;
36import android.content.pm.ResolveInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.content.res.Configuration;
38import android.content.res.Resources;
39import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080040import android.graphics.Bitmap;
Andrei Popescu540035d2009-09-18 18:59:20 +010041import android.graphics.BitmapFactory;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042import android.graphics.Canvas;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import android.graphics.Picture;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040044import android.graphics.PixelFormat;
45import android.graphics.Rect;
The Android Open Source Project0c908882009-03-03 19:32:16 -080046import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047import android.net.ConnectivityManager;
Patrick Scotteb6ab2a2009-09-16 10:00:17 -040048import android.net.NetworkInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049import android.net.Uri;
50import android.net.WebAddress;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051import android.net.http.SslCertificate;
52import android.net.http.SslError;
53import android.os.AsyncTask;
54import android.os.Bundle;
55import android.os.Debug;
56import android.os.Environment;
57import android.os.Handler;
58import android.os.IBinder;
59import android.os.Message;
60import android.os.PowerManager;
61import android.os.Process;
62import android.os.RemoteException;
63import android.os.ServiceManager;
64import android.os.SystemClock;
The Android Open Source Project0c908882009-03-03 19:32:16 -080065import android.provider.Browser;
Cary Clark5e335a32009-09-22 14:53:11 -040066import android.provider.ContactsContract;
67import android.provider.ContactsContract.Intents.Insert;
The Android Open Source Project0c908882009-03-03 19:32:16 -080068import android.provider.Downloads;
69import android.provider.MediaStore;
The Android Open Source Project0c908882009-03-03 19:32:16 -080070import android.text.IClipboard;
71import android.text.TextUtils;
72import android.text.format.DateFormat;
Leon Scrogginsb94bf272009-09-25 15:22:08 -040073import android.util.AttributeSet;
The Android Open Source Project0c908882009-03-03 19:32:16 -080074import android.util.Log;
75import android.view.ContextMenu;
76import android.view.Gravity;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
80import android.view.MenuInflater;
81import android.view.MenuItem;
82import android.view.View;
83import android.view.ViewGroup;
84import android.view.Window;
85import android.view.WindowManager;
86import android.view.ContextMenu.ContextMenuInfo;
87import android.view.MenuItem.OnMenuItemClickListener;
The Android Open Source Project0c908882009-03-03 19:32:16 -080088import android.webkit.CookieManager;
89import android.webkit.CookieSyncManager;
90import android.webkit.DownloadListener;
91import android.webkit.HttpAuthHandler;
Grace Klobab4da0ad2009-05-14 14:45:40 -070092import android.webkit.PluginManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080093import android.webkit.SslErrorHandler;
94import android.webkit.URLUtil;
Leon Clarkecb6cc862009-09-29 18:35:13 +010095import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080096import android.webkit.WebChromeClient;
97import android.webkit.WebHistoryItem;
98import android.webkit.WebIconDatabase;
99import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800100import android.widget.EditText;
101import android.widget.FrameLayout;
102import android.widget.LinearLayout;
103import android.widget.TextView;
104import android.widget.Toast;
105
Dan Egnor5ee906c2009-11-18 12:11:49 -0800106import com.android.common.Patterns;
107
108import com.google.android.googleapps.IGoogleLoginService;
109import com.google.android.googlelogin.GoogleLoginServiceConstants;
110
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400111import java.io.ByteArrayOutputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800112import java.io.File;
Ben Murdoch4f75ba22009-10-27 11:48:28 +0000113import java.io.IOException;
114import java.io.InputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800115import java.net.MalformedURLException;
116import java.net.URI;
Dianne Hackborn99189432009-06-17 18:06:18 -0700117import java.net.URISyntaxException;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118import java.net.URL;
119import java.net.URLEncoder;
120import java.text.ParseException;
121import java.util.Date;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800122import java.util.HashMap;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123import java.util.regex.Matcher;
124import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800125
126public class BrowserActivity extends Activity
Grace Kloba5942df02009-09-18 11:48:29 -0700127 implements View.OnCreateContextMenuListener,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800128 DownloadListener {
129
Dave Bort31a6d1c2009-04-13 15:56:49 -0700130 /* Define some aliases to make these debugging flags easier to refer to.
131 * This file imports android.provider.Browser, so we can't just refer to "Browser.DEBUG".
132 */
133 private final static boolean DEBUG = com.android.browser.Browser.DEBUG;
134 private final static boolean LOGV_ENABLED = com.android.browser.Browser.LOGV_ENABLED;
135 private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
136
The Android Open Source Project0c908882009-03-03 19:32:16 -0800137 private IGoogleLoginService mGls = null;
138 private ServiceConnection mGlsConnection = null;
139
Satish Sampath565505b2009-05-29 15:37:27 +0100140 // These are single-character shortcuts for searching popular sources.
141 private static final int SHORTCUT_INVALID = 0;
142 private static final int SHORTCUT_GOOGLE_SEARCH = 1;
143 private static final int SHORTCUT_WIKIPEDIA_SEARCH = 2;
144 private static final int SHORTCUT_DICTIONARY_SEARCH = 3;
145 private static final int SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH = 4;
146
The Android Open Source Project0c908882009-03-03 19:32:16 -0800147 private void setupHomePage() {
148 final Runnable getAccount = new Runnable() {
149 public void run() {
150 // Lower priority
151 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
152 // get the default home page
153 String homepage = mSettings.getHomePage();
154
155 try {
156 if (mGls == null) return;
157
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700158 if (!homepage.startsWith("http://www.google.")) return;
159 if (homepage.indexOf('?') == -1) return;
160
The Android Open Source Project0c908882009-03-03 19:32:16 -0800161 String hostedUser = mGls.getAccount(GoogleLoginServiceConstants.PREFER_HOSTED);
162 String googleUser = mGls.getAccount(GoogleLoginServiceConstants.REQUIRE_GOOGLE);
163
164 // three cases:
165 //
166 // hostedUser == googleUser
167 // The device has only a google account
168 //
169 // hostedUser != googleUser
170 // The device has a hosted account and a google account
171 //
172 // hostedUser != null, googleUser == null
173 // The device has only a hosted account (so far)
174
175 // developers might have no accounts at all
176 if (hostedUser == null) return;
177
178 if (googleUser == null || !hostedUser.equals(googleUser)) {
179 String domain = hostedUser.substring(hostedUser.lastIndexOf('@')+1);
Grace Klobaf2c5c1b2009-05-26 10:48:31 -0700180 homepage = homepage.replace("?", "/a/" + domain + "?");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800181 }
182 } catch (RemoteException ignore) {
183 // Login service died; carry on
184 } catch (RuntimeException ignore) {
185 // Login service died; carry on
186 } finally {
187 finish(homepage);
188 }
189 }
190
191 private void finish(final String homepage) {
192 mHandler.post(new Runnable() {
193 public void run() {
194 mSettings.setHomePage(BrowserActivity.this, homepage);
195 resumeAfterCredentials();
196
197 // as this is running in a separate thread,
198 // BrowserActivity's onDestroy() may have been called,
199 // which also calls unbindService().
200 if (mGlsConnection != null) {
201 // we no longer need to keep GLS open
202 unbindService(mGlsConnection);
203 mGlsConnection = null;
204 }
205 } });
206 } };
207
208 final boolean[] done = { false };
209
210 // Open a connection to the Google Login Service. The first
211 // time the connection is established, set up the homepage depending on
212 // the account in a background thread.
213 mGlsConnection = new ServiceConnection() {
214 public void onServiceConnected(ComponentName className, IBinder service) {
215 mGls = IGoogleLoginService.Stub.asInterface(service);
216 if (done[0] == false) {
217 done[0] = true;
218 Thread account = new Thread(getAccount);
219 account.setName("GLSAccount");
220 account.start();
221 }
222 }
223 public void onServiceDisconnected(ComponentName className) {
224 mGls = null;
225 }
226 };
227
228 bindService(GoogleLoginServiceConstants.SERVICE_INTENT,
229 mGlsConnection, Context.BIND_AUTO_CREATE);
230 }
231
Cary Clarka9771242009-08-11 16:42:26 -0400232 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800233 @Override
234 public Void doInBackground(File... files) {
235 if (files != null) {
236 for (File f : files) {
Cary Clarkd6be1752009-08-12 12:56:42 -0400237 if (!f.delete()) {
238 Log.e(LOGTAG, f.getPath() + " was not deleted");
239 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800240 }
241 }
242 return null;
243 }
244 }
245
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400246 /**
247 * This layout holds everything you see below the status bar, including the
248 * error console, the custom view container, and the webviews.
249 */
250 private FrameLayout mBrowserFrameLayout;
Leon Scroggins81db3662009-06-04 17:45:11 -0400251
Grace Kloba22ac16e2009-10-07 18:00:23 -0700252 @Override
253 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700254 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800255 Log.v(LOGTAG, this + " onStart");
256 }
257 super.onCreate(icicle);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800258 // test the browser in OpenGL
259 // requestWindowFeature(Window.FEATURE_OPENGL);
260
261 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
262
263 mResolver = getContentResolver();
264
Grace Kloba0923d692009-09-23 21:37:25 -0700265 // If this was a web search request, pass it on to the default web
266 // search provider and finish this activity.
267 if (handleWebSearchIntent(getIntent())) {
268 finish();
269 return;
270 }
271
The Android Open Source Project0c908882009-03-03 19:32:16 -0800272 mSecLockIcon = Resources.getSystem().getDrawable(
273 android.R.drawable.ic_secure);
274 mMixLockIcon = Resources.getSystem().getDrawable(
275 android.R.drawable.ic_partial_secure);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800276
Leon Scroggins81db3662009-06-04 17:45:11 -0400277 FrameLayout frameLayout = (FrameLayout) getWindow().getDecorView()
278 .findViewById(com.android.internal.R.id.content);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400279 mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(this)
280 .inflate(R.layout.custom_screen, null);
281 mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(
282 R.id.main_content);
283 mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout
284 .findViewById(R.id.error_console);
285 mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
286 .findViewById(R.id.fullscreen_custom_content);
287 frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
Leon Scroggins68579392009-09-15 15:31:54 -0400288 mTitleBar = new TitleBar(this);
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400289 mFakeTitleBar = new TitleBar(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800290
291 // Create the tab control and our initial tab
292 mTabControl = new TabControl(this);
293
294 // Open the icon database and retain all the bookmark urls for favicons
295 retainIconsOnStartup();
296
297 // Keep a settings instance handy.
298 mSettings = BrowserSettings.getInstance();
299 mSettings.setTabControl(mTabControl);
300 mSettings.loadFromDb(this);
301
302 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
303 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
304
Grace Klobaa34f6862009-07-31 16:28:17 -0700305 /* enables registration for changes in network status from
306 http stack */
307 mNetworkStateChangedFilter = new IntentFilter();
308 mNetworkStateChangedFilter.addAction(
309 ConnectivityManager.CONNECTIVITY_ACTION);
310 mNetworkStateIntentReceiver = new BroadcastReceiver() {
311 @Override
312 public void onReceive(Context context, Intent intent) {
313 if (intent.getAction().equals(
314 ConnectivityManager.CONNECTIVITY_ACTION)) {
Robert Greenwalt7d899d62009-10-23 10:33:48 -0700315 boolean noConnectivity = intent.getBooleanExtra(
316 ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
317 onNetworkToggle(!noConnectivity);
Grace Klobaa34f6862009-07-31 16:28:17 -0700318 }
319 }
320 };
321
Grace Kloba615c6c92009-08-03 10:22:44 -0700322 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
323 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
324 filter.addDataScheme("package");
325 mPackageInstallationReceiver = new BroadcastReceiver() {
326 @Override
327 public void onReceive(Context context, Intent intent) {
328 final String action = intent.getAction();
329 final String packageName = intent.getData()
330 .getSchemeSpecificPart();
331 final boolean replacing = intent.getBooleanExtra(
332 Intent.EXTRA_REPLACING, false);
333 if (Intent.ACTION_PACKAGE_REMOVED.equals(action) && replacing) {
334 // if it is replacing, refreshPlugins() when adding
335 return;
336 }
337 PackageManager pm = BrowserActivity.this.getPackageManager();
338 PackageInfo pkgInfo = null;
339 try {
340 pkgInfo = pm.getPackageInfo(packageName,
341 PackageManager.GET_PERMISSIONS);
342 } catch (PackageManager.NameNotFoundException e) {
343 return;
344 }
345 if (pkgInfo != null) {
346 String permissions[] = pkgInfo.requestedPermissions;
347 if (permissions == null) {
348 return;
349 }
350 boolean permissionOk = false;
351 for (String permit : permissions) {
352 if (PluginManager.PLUGIN_PERMISSION.equals(permit)) {
353 permissionOk = true;
354 break;
355 }
356 }
357 if (permissionOk) {
358 PluginManager.getInstance(BrowserActivity.this)
359 .refreshPlugins(
360 Intent.ACTION_PACKAGE_ADDED
361 .equals(action));
362 }
363 }
364 }
365 };
366 registerReceiver(mPackageInstallationReceiver, filter);
367
The Android Open Source Project0c908882009-03-03 19:32:16 -0800368 if (!mTabControl.restoreState(icicle)) {
369 // clear up the thumbnail directory if we can't restore the state as
370 // none of the files in the directory are referenced any more.
371 new ClearThumbnails().execute(
372 mTabControl.getThumbnailDir().listFiles());
Grace Klobaaab3f092009-07-30 12:29:51 -0700373 // there is no quit on Android. But if we can't restore the state,
374 // we can treat it as a new Browser, remove the old session cookies.
375 CookieManager.getInstance().removeSessionCookie();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800376 final Intent intent = getIntent();
377 final Bundle extra = intent.getExtras();
378 // Create an initial tab.
379 // If the intent is ACTION_VIEW and data is not null, the Browser is
380 // invoked to view the content by another application. In this case,
381 // the tab will be close when exit.
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700382 UrlData urlData = getUrlDataFromIntent(intent);
383
Grace Kloba22ac16e2009-10-07 18:00:23 -0700384 final Tab t = mTabControl.createNewTab(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800385 Intent.ACTION_VIEW.equals(intent.getAction()) &&
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700386 intent.getData() != null,
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700387 intent.getStringExtra(Browser.EXTRA_APPLICATION_ID), urlData.mUrl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800388 mTabControl.setCurrentTab(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800389 attachTabToContentView(t);
390 WebView webView = t.getWebView();
391 if (extra != null) {
392 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
393 if (scale > 0 && scale <= 1000) {
394 webView.setInitialScale(scale);
395 }
396 }
397 // If we are not restoring from an icicle, then there is a high
398 // likely hood this is the first run. So, check to see if the
399 // homepage needs to be configured and copy any plugins from our
400 // asset directory to the data partition.
401 if ((extra == null || !extra.getBoolean("testing"))
402 && !mSettings.isLoginInitialized()) {
403 setupHomePage();
404 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800405
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700406 if (urlData.isEmpty()) {
Leon Scroggins30444232009-09-04 18:36:20 -0400407 if (mSettings.isLoginInitialized()) {
408 webView.loadUrl(mSettings.getHomePage());
409 } else {
410 waitForCredentials();
411 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800412 } else {
Grace Kloba81678d92009-06-30 07:09:56 -0700413 if (extra != null) {
414 urlData.setPostData(extra
415 .getByteArray(Browser.EXTRA_POST_DATA));
416 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700417 urlData.loadIn(webView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800418 }
419 } else {
420 // TabControl.restoreState() will create a new tab even if
Leon Scroggins1f005d32009-08-10 17:36:42 -0400421 // restoring the state fails.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800422 attachTabToContentView(mTabControl.getCurrentTab());
423 }
Grace Kloba615c6c92009-08-03 10:22:44 -0700424
Feng Qianb3c02da2009-06-29 15:58:08 -0700425 // Read JavaScript flags if it exists.
426 String jsFlags = mSettings.getJsFlags();
427 if (jsFlags.trim().length() != 0) {
428 mTabControl.getCurrentWebView().setJsFlags(jsFlags);
429 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800430 }
431
432 @Override
433 protected void onNewIntent(Intent intent) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700434 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800435 // When a tab is closed on exit, the current tab index is set to -1.
436 // Reset before proceed as Browser requires the current tab to be set.
437 if (current == null) {
438 // Try to reset the tab in case the index was incorrect.
439 current = mTabControl.getTab(0);
440 if (current == null) {
441 // No tabs at all so just ignore this intent.
442 return;
443 }
444 mTabControl.setCurrentTab(current);
445 attachTabToContentView(current);
446 resetTitleAndIcon(current.getWebView());
447 }
448 final String action = intent.getAction();
449 final int flags = intent.getFlags();
450 if (Intent.ACTION_MAIN.equals(action) ||
451 (flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
452 // just resume the browser
453 return;
454 }
455 if (Intent.ACTION_VIEW.equals(action)
456 || Intent.ACTION_SEARCH.equals(action)
457 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
458 || Intent.ACTION_WEB_SEARCH.equals(action)) {
Satish Sampath565505b2009-05-29 15:37:27 +0100459 // If this was a search request (e.g. search query directly typed into the address bar),
460 // pass it on to the default web search provider.
461 if (handleWebSearchIntent(intent)) {
462 return;
463 }
464
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700465 UrlData urlData = getUrlDataFromIntent(intent);
466 if (urlData.isEmpty()) {
467 urlData = new UrlData(mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800468 }
Grace Kloba81678d92009-06-30 07:09:56 -0700469 urlData.setPostData(intent
470 .getByteArrayExtra(Browser.EXTRA_POST_DATA));
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700471
Grace Klobacc634032009-07-28 15:58:19 -0700472 final String appId = intent
473 .getStringExtra(Browser.EXTRA_APPLICATION_ID);
474 if (Intent.ACTION_VIEW.equals(action)
475 && !getPackageName().equals(appId)
476 && (flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700477 Tab appTab = mTabControl.getTabFromId(appId);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700478 if (appTab != null) {
479 Log.i(LOGTAG, "Reusing tab for " + appId);
480 // Dismiss the subwindow if applicable.
481 dismissSubWindow(appTab);
482 // Since we might kill the WebView, remove it from the
483 // content view first.
484 removeTabFromContentView(appTab);
485 // Recreate the main WebView after destroying the old one.
486 // If the WebView has the same original url and is on that
487 // page, it can be reused.
488 boolean needsLoad =
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700489 mTabControl.recreateWebView(appTab, urlData.mUrl);
Ben Murdochbff2d602009-07-01 20:19:05 +0100490
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700491 if (current != appTab) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400492 switchToTab(mTabControl.getTabIndex(appTab));
493 if (needsLoad) {
494 urlData.loadIn(appTab.getWebView());
495 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700496 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400497 // If the tab was the current tab, we have to attach
498 // it to the view system again.
499 attachTabToContentView(appTab);
500 if (needsLoad) {
501 urlData.loadIn(appTab.getWebView());
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700502 }
503 }
504 return;
Patrick Scottcd115892009-07-16 09:42:58 -0400505 } else {
506 // No matching application tab, try to find a regular tab
507 // with a matching url.
508 appTab = mTabControl.findUnusedTabWithUrl(urlData.mUrl);
Leon Scroggins25515f82009-08-19 15:31:58 -0400509 if (appTab != null) {
510 if (current != appTab) {
511 switchToTab(mTabControl.getTabIndex(appTab));
512 }
513 // Otherwise, we are already viewing the correct tab.
Patrick Scottcd115892009-07-16 09:42:58 -0400514 } else {
515 // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
516 // will be opened in a new tab unless we have reached
517 // MAX_TABS. Then the url will be opened in the current
518 // tab. If a new tab is created, it will have "true" for
519 // exit on close.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400520 openTabAndShow(urlData, true, appId);
Patrick Scottcd115892009-07-16 09:42:58 -0400521 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700522 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800523 } else {
Grace Kloba638d3f42009-11-23 10:35:04 -0800524 if (!urlData.isEmpty()
525 && urlData.mUrl.startsWith("about:debug")) {
526 if ("about:debug.dom".equals(urlData.mUrl)) {
527 current.getWebView().dumpDomTree(false);
528 } else if ("about:debug.dom.file".equals(urlData.mUrl)) {
529 current.getWebView().dumpDomTree(true);
530 } else if ("about:debug.render".equals(urlData.mUrl)) {
531 current.getWebView().dumpRenderTree(false);
532 } else if ("about:debug.render.file".equals(urlData.mUrl)) {
533 current.getWebView().dumpRenderTree(true);
534 } else if ("about:debug.display".equals(urlData.mUrl)) {
535 current.getWebView().dumpDisplayTree();
536 } else {
537 mSettings.toggleDebugSettings();
538 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800539 return;
540 }
Leon Scroggins1f005d32009-08-10 17:36:42 -0400541 // Get rid of the subwindow if it exists
542 dismissSubWindow(current);
543 urlData.loadIn(current.getWebView());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800544 }
545 }
546 }
547
Satish Sampath565505b2009-05-29 15:37:27 +0100548 private int parseUrlShortcut(String url) {
549 if (url == null) return SHORTCUT_INVALID;
550
551 // FIXME: quick search, need to be customized by setting
552 if (url.length() > 2 && url.charAt(1) == ' ') {
553 switch (url.charAt(0)) {
554 case 'g': return SHORTCUT_GOOGLE_SEARCH;
555 case 'w': return SHORTCUT_WIKIPEDIA_SEARCH;
556 case 'd': return SHORTCUT_DICTIONARY_SEARCH;
557 case 'l': return SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH;
558 }
559 }
560 return SHORTCUT_INVALID;
561 }
562
563 /**
564 * Launches the default web search activity with the query parameters if the given intent's data
565 * are identified as plain search terms and not URLs/shortcuts.
566 * @return true if the intent was handled and web search activity was launched, false if not.
567 */
568 private boolean handleWebSearchIntent(Intent intent) {
569 if (intent == null) return false;
570
571 String url = null;
572 final String action = intent.getAction();
573 if (Intent.ACTION_VIEW.equals(action)) {
Grace Kloba1e705052009-09-29 13:13:36 -0700574 Uri data = intent.getData();
575 if (data != null) url = data.toString();
Satish Sampath565505b2009-05-29 15:37:27 +0100576 } else if (Intent.ACTION_SEARCH.equals(action)
577 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
578 || Intent.ACTION_WEB_SEARCH.equals(action)) {
579 url = intent.getStringExtra(SearchManager.QUERY);
580 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100581 return handleWebSearchRequest(url, intent.getBundleExtra(SearchManager.APP_DATA),
582 intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
Satish Sampath565505b2009-05-29 15:37:27 +0100583 }
584
585 /**
586 * Launches the default web search activity with the query parameters if the given url string
587 * was identified as plain search terms and not URL/shortcut.
588 * @return true if the request was handled and web search activity was launched, false if not.
589 */
Bjorn Bringert04851702009-09-22 10:36:01 +0100590 private boolean handleWebSearchRequest(String inUrl, Bundle appData, String extraData) {
Satish Sampath565505b2009-05-29 15:37:27 +0100591 if (inUrl == null) return false;
592
593 // In general, we shouldn't modify URL from Intent.
594 // But currently, we get the user-typed URL from search box as well.
595 String url = fixUrl(inUrl).trim();
596
597 // URLs and site specific search shortcuts are handled by the regular flow of control, so
598 // return early.
Dan Egnor5ee906c2009-11-18 12:11:49 -0800599 if (Patterns.WEB_URL.matcher(url).matches()
Satish Sampathbc5b9f32009-06-04 18:21:40 +0100600 || ACCEPTED_URI_SCHEMA.matcher(url).matches()
Satish Sampath565505b2009-05-29 15:37:27 +0100601 || parseUrlShortcut(url) != SHORTCUT_INVALID) {
602 return false;
603 }
604
605 Browser.updateVisitedHistory(mResolver, url, false);
606 Browser.addSearchUrl(mResolver, url);
607
608 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
609 intent.addCategory(Intent.CATEGORY_DEFAULT);
610 intent.putExtra(SearchManager.QUERY, url);
Satish Sampath15e9f2d2009-06-23 22:29:49 +0100611 if (appData != null) {
612 intent.putExtra(SearchManager.APP_DATA, appData);
613 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100614 if (extraData != null) {
615 intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
616 }
Grace Klobacc634032009-07-28 15:58:19 -0700617 intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName());
Satish Sampath565505b2009-05-29 15:37:27 +0100618 startActivity(intent);
619
620 return true;
621 }
622
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700623 private UrlData getUrlDataFromIntent(Intent intent) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800624 String url = null;
625 if (intent != null) {
626 final String action = intent.getAction();
627 if (Intent.ACTION_VIEW.equals(action)) {
628 url = smartUrlFilter(intent.getData());
629 if (url != null && url.startsWith("content:")) {
630 /* Append mimetype so webview knows how to display */
631 String mimeType = intent.resolveType(getContentResolver());
632 if (mimeType != null) {
633 url += "?" + mimeType;
634 }
635 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700636 if ("inline:".equals(url)) {
637 return new InlinedUrlData(
638 intent.getStringExtra(Browser.EXTRA_INLINE_CONTENT),
639 intent.getType(),
640 intent.getStringExtra(Browser.EXTRA_INLINE_ENCODING),
641 intent.getStringExtra(Browser.EXTRA_INLINE_FAILURL));
642 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800643 } else if (Intent.ACTION_SEARCH.equals(action)
644 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
645 || Intent.ACTION_WEB_SEARCH.equals(action)) {
646 url = intent.getStringExtra(SearchManager.QUERY);
647 if (url != null) {
648 mLastEnteredUrl = url;
649 // Don't add Urls, just search terms.
650 // Urls will get added when the page is loaded.
Dan Egnor5ee906c2009-11-18 12:11:49 -0800651 if (!Patterns.WEB_URL.matcher(url).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800652 Browser.updateVisitedHistory(mResolver, url, false);
653 }
654 // In general, we shouldn't modify URL from Intent.
655 // But currently, we get the user-typed URL from search box as well.
656 url = fixUrl(url);
657 url = smartUrlFilter(url);
658 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
659 if (url.contains(searchSource)) {
660 String source = null;
661 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
662 if (appData != null) {
663 source = appData.getString(SearchManager.SOURCE);
664 }
665 if (TextUtils.isEmpty(source)) {
666 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
667 }
668 url = url.replace(searchSource, "&source=android-"+source+"&");
669 }
670 }
671 }
672 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700673 return new UrlData(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674 }
675
676 /* package */ static String fixUrl(String inUrl) {
Cary Clark652ff872009-09-10 13:34:44 -0400677 // FIXME: Converting the url to lower case
678 // duplicates functionality in smartUrlFilter().
679 // However, changing all current callers of fixUrl to
680 // call smartUrlFilter in addition may have unwanted
681 // consequences, and is deferred for now.
682 int colon = inUrl.indexOf(':');
683 boolean allLower = true;
684 for (int index = 0; index < colon; index++) {
685 char ch = inUrl.charAt(index);
686 if (!Character.isLetter(ch)) {
687 break;
688 }
689 allLower &= Character.isLowerCase(ch);
690 if (index == colon - 1 && !allLower) {
691 inUrl = inUrl.substring(0, colon).toLowerCase()
692 + inUrl.substring(colon);
693 }
694 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800695 if (inUrl.startsWith("http://") || inUrl.startsWith("https://"))
696 return inUrl;
697 if (inUrl.startsWith("http:") ||
698 inUrl.startsWith("https:")) {
699 if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) {
700 inUrl = inUrl.replaceFirst("/", "//");
701 } else inUrl = inUrl.replaceFirst(":", "://");
702 }
703 return inUrl;
704 }
705
Grace Kloba22ac16e2009-10-07 18:00:23 -0700706 @Override
707 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800708 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700709 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800710 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
711 }
712
713 if (!mActivityInPause) {
714 Log.e(LOGTAG, "BrowserActivity is already resumed.");
715 return;
716 }
717
Mike Reed7bfa63b2009-05-28 11:08:32 -0400718 mTabControl.resumeCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800719 mActivityInPause = false;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400720 resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800721
722 if (mWakeLock.isHeld()) {
723 mHandler.removeMessages(RELEASE_WAKELOCK);
724 mWakeLock.release();
725 }
726
727 if (mCredsDlg != null) {
728 if (!mHandler.hasMessages(CANCEL_CREDS_REQUEST)) {
729 // In case credential request never comes back
730 mHandler.sendEmptyMessageDelayed(CANCEL_CREDS_REQUEST, 6000);
731 }
732 }
733
734 registerReceiver(mNetworkStateIntentReceiver,
735 mNetworkStateChangedFilter);
736 WebView.enablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800737 }
738
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400739 /**
740 * Since the actual title bar is embedded in the WebView, and removing it
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400741 * would change its appearance, use a different TitleBar to show overlayed
742 * at the top of the screen, when the menu is open or the page is loading.
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400743 */
744 private TitleBar mFakeTitleBar;
745
746 /**
Leon Scrogginsd8fd2fc2009-09-16 11:12:09 -0400747 * Holder for the fake title bar. It will have a foreground shadow, as well
748 * as a white background, so the fake title bar looks like the real one.
749 */
750 private ViewGroup mFakeTitleBarHolder;
751
752 /**
753 * Layout parameters for the fake title bar within mFakeTitleBarHolder
754 */
755 private FrameLayout.LayoutParams mFakeTitleBarParams
756 = new FrameLayout.LayoutParams(
Leon Scrogginsc01e4a82009-09-16 14:41:00 -0400757 ViewGroup.LayoutParams.FILL_PARENT,
Leon Scrogginsd8fd2fc2009-09-16 11:12:09 -0400758 ViewGroup.LayoutParams.WRAP_CONTENT);
759 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400760 * Keeps track of whether the options menu is open. This is important in
761 * determining whether to show or hide the title bar overlay.
762 */
763 private boolean mOptionsMenuOpen;
764
765 /**
766 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
767 * of whether the configuration has changed. The first onMenuOpened call
768 * after a configuration change is simply a reopening of the same menu
769 * (i.e. mIconView did not change).
770 */
771 private boolean mConfigChanged;
772
773 /**
774 * Whether or not the options menu is in its smaller, icon menu form. When
775 * true, we want the title bar overlay to be up. When false, we do not.
776 * Only meaningful if mOptionsMenuOpen is true.
777 */
778 private boolean mIconView;
779
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400780 @Override
781 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400782 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
783 if (mOptionsMenuOpen) {
784 if (mConfigChanged) {
785 // We do not need to make any changes to the state of the
786 // title bar, since the only thing that happened was a
787 // change in orientation
788 mConfigChanged = false;
789 } else {
790 if (mIconView) {
791 // Switching the menu to expanded view, so hide the
792 // title bar.
793 hideFakeTitleBar();
794 mIconView = false;
795 } else {
796 // Switching the menu back to icon view, so show the
797 // title bar once again.
798 showFakeTitleBar();
799 mIconView = true;
800 }
801 }
802 } else {
803 // The options menu is closed, so open it, and show the title
804 showFakeTitleBar();
805 mOptionsMenuOpen = true;
806 mConfigChanged = false;
807 mIconView = true;
808 }
809 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400810 return true;
811 }
812
Leon Scrogginsb94bf272009-09-25 15:22:08 -0400813 /**
814 * Special class used exclusively for the shadow drawn underneath the fake
815 * title bar. The shadow does not need to be drawn if the WebView
816 * underneath is scrolled to the top, because it will draw directly on top
817 * of the embedded shadow.
818 */
819 private static class Shadow extends View {
820 private WebView mWebView;
821
822 public Shadow(Context context, AttributeSet attrs) {
823 super(context, attrs);
824 }
825
826 public void setWebView(WebView view) {
827 mWebView = view;
828 }
829
830 @Override
831 public void draw(Canvas canvas) {
832 // In general onDraw is the method to override, but we care about
833 // whether or not the background gets drawn, which happens in draw()
834 if (mWebView == null || mWebView.getScrollY() > getHeight()) {
835 super.draw(canvas);
836 }
837 // Need to invalidate so that if the scroll position changes, we
838 // still draw as appropriate.
839 invalidate();
840 }
841 }
842
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400843 private void showFakeTitleBar() {
Patrick Scottf8de8ec2009-09-21 16:37:29 -0400844 final View decor = getWindow().peekDecorView();
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400845 if (mFakeTitleBar.getParent() == null && mActiveTabsPage == null
Patrick Scottf8de8ec2009-09-21 16:37:29 -0400846 && !mActivityInPause && decor != null
847 && decor.getWindowToken() != null) {
Cary Clarka0464552009-09-29 13:00:45 -0400848 Rect visRect = new Rect();
849 if (!mBrowserFrameLayout.getGlobalVisibleRect(visRect)) {
850 if (LOGD_ENABLED) {
851 Log.d(LOGTAG, "showFakeTitleBar visRect failed");
852 }
853 return;
854 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400855
856 WindowManager manager
857 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
858
859 // Add the title bar to the window manager so it can receive touches
860 // while the menu is up
861 WindowManager.LayoutParams params
862 = new WindowManager.LayoutParams(
863 ViewGroup.LayoutParams.FILL_PARENT,
864 ViewGroup.LayoutParams.WRAP_CONTENT,
865 WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL,
866 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
Leon Scroggins68549862009-09-21 16:02:01 -0400867 PixelFormat.TRANSLUCENT);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400868 params.gravity = Gravity.TOP;
Leon Scrogginsa27ff192009-09-14 12:58:04 -0400869 WebView mainView = mTabControl.getCurrentWebView();
Leon Scroggins68549862009-09-21 16:02:01 -0400870 boolean atTop = mainView != null && mainView.getScrollY() == 0;
Leon Scroggins83932c72009-09-30 11:55:54 -0400871 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400872 // XXX : Without providing an offset, the fake title bar will be
873 // placed underneath the status bar. Use the global visible rect
874 // of mBrowserFrameLayout to determine the bottom of the status bar
Cary Clarka0464552009-09-29 13:00:45 -0400875 params.y = visRect.top;
Leon Scroggins68549862009-09-21 16:02:01 -0400876 // Add a holder for the title bar. It also holds a shadow to show
877 // below the title bar.
Leon Scrogginsd8fd2fc2009-09-16 11:12:09 -0400878 if (mFakeTitleBarHolder == null) {
879 mFakeTitleBarHolder = (ViewGroup) LayoutInflater.from(this)
880 .inflate(R.layout.title_bar_bg, null);
881 }
Leon Scrogginsb94bf272009-09-25 15:22:08 -0400882 Shadow shadow = (Shadow) mFakeTitleBarHolder.findViewById(
883 R.id.shadow);
884 shadow.setWebView(mainView);
Leon Scroggins68549862009-09-21 16:02:01 -0400885 mFakeTitleBarHolder.addView(mFakeTitleBar, 0, mFakeTitleBarParams);
Leon Scrogginsd8fd2fc2009-09-16 11:12:09 -0400886 manager.addView(mFakeTitleBarHolder, params);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400887 }
888 }
889
890 @Override
891 public void onOptionsMenuClosed(Menu menu) {
892 mOptionsMenuOpen = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -0400893 if (!mInLoad) {
894 hideFakeTitleBar();
895 } else if (!mIconView) {
896 // The page is currently loading, and we are in expanded mode, so
897 // we were not showing the menu. Show it once again. It will be
898 // removed when the page finishes.
899 showFakeTitleBar();
900 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400901 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700902
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400903 private void hideFakeTitleBar() {
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400904 if (mFakeTitleBar.getParent() == null) return;
Leon Scroggins20329572009-09-23 17:42:41 -0400905 WindowManager.LayoutParams params = (WindowManager.LayoutParams)
906 mFakeTitleBarHolder.getLayoutParams();
907 WebView mainView = mTabControl.getCurrentWebView();
908 // Although we decided whether or not to animate based on the current
909 // scroll position, the scroll position may have changed since the
910 // fake title bar was displayed. Make sure it has the appropriate
911 // animation/lack thereof before removing.
912 params.windowAnimations = mainView != null && mainView.getScrollY() == 0
Leon Scroggins83932c72009-09-30 11:55:54 -0400913 ? 0 : R.style.TitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400914 WindowManager manager
915 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Leon Scroggins20329572009-09-23 17:42:41 -0400916 manager.updateViewLayout(mFakeTitleBarHolder, params);
Leon Scrogginsd8fd2fc2009-09-16 11:12:09 -0400917 mFakeTitleBarHolder.removeView(mFakeTitleBar);
918 manager.removeView(mFakeTitleBarHolder);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400919 }
920
The Android Open Source Project0c908882009-03-03 19:32:16 -0800921 /**
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400922 * Special method for the fake title bar to call when displaying its context
923 * menu, since it is in its own Window, and its parent does not show a
924 * context menu.
925 */
926 /* package */ void showTitleBarContextMenu() {
Cary Clark65f4a3c2009-09-28 17:05:06 -0400927 if (null == mTitleBar.getParent()) {
928 return;
929 }
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400930 openContextMenu(mTitleBar);
931 }
932
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400933 @Override
934 public void onContextMenuClosed(Menu menu) {
935 super.onContextMenuClosed(menu);
936 if (mInLoad) {
937 showFakeTitleBar();
938 }
939 }
940
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400941 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800942 * onSaveInstanceState(Bundle map)
943 * onSaveInstanceState is called right before onStop(). The map contains
944 * the saved state.
945 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700946 @Override
947 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700948 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800949 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
950 }
951 // the default implementation requires each view to have an id. As the
952 // browser handles the state itself and it doesn't use id for the views,
953 // don't call the default implementation. Otherwise it will trigger the
954 // warning like this, "couldn't save which view has focus because the
955 // focused view XXX has no id".
956
957 // Save all the tabs
958 mTabControl.saveState(outState);
959 }
960
Grace Kloba22ac16e2009-10-07 18:00:23 -0700961 @Override
962 protected void onPause() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800963 super.onPause();
964
965 if (mActivityInPause) {
966 Log.e(LOGTAG, "BrowserActivity is already paused.");
967 return;
968 }
969
Mike Reed7bfa63b2009-05-28 11:08:32 -0400970 mTabControl.pauseCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800971 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400972 if (mTabControl.getCurrentIndex() >= 0 && !pauseWebViewTimers()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800973 mWakeLock.acquire();
974 mHandler.sendMessageDelayed(mHandler
975 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
976 }
977
978 // Clear the credentials toast if it is up
979 if (mCredsDlg != null && mCredsDlg.isShowing()) {
980 mCredsDlg.dismiss();
981 }
982 mCredsDlg = null;
983
Leon Scrogginsa2ab6a72009-09-11 11:49:52 -0400984 // FIXME: This removes the active tabs page and resets the menu to
985 // MAIN_MENU. A better solution might be to do this work in onNewIntent
986 // but then we would need to save it in onSaveInstanceState and restore
987 // it in onCreate/onRestoreInstanceState
988 if (mActiveTabsPage != null) {
989 removeActiveTabPage(true);
990 }
991
The Android Open Source Project0c908882009-03-03 19:32:16 -0800992 cancelStopToast();
993
994 // unregister network state listener
995 unregisterReceiver(mNetworkStateIntentReceiver);
996 WebView.disablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800997 }
998
Grace Kloba22ac16e2009-10-07 18:00:23 -0700999 @Override
1000 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -07001001 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001002 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
1003 }
1004 super.onDestroy();
Grace Kloba0923d692009-09-23 21:37:25 -07001005
Leon Scroggins8d5fa432009-10-02 15:55:59 -04001006 if (mUploadMessage != null) {
1007 mUploadMessage.onReceiveValue(null);
1008 mUploadMessage = null;
1009 }
1010
Grace Kloba0923d692009-09-23 21:37:25 -07001011 if (mTabControl == null) return;
1012
Grace Kloba1fc98a32009-10-21 13:23:08 -07001013 // Remove the fake title bar if it is there
1014 hideFakeTitleBar();
1015
The Android Open Source Project0c908882009-03-03 19:32:16 -08001016 // Remove the current tab and sub window
Grace Kloba22ac16e2009-10-07 18:00:23 -07001017 Tab t = mTabControl.getCurrentTab();
Patrick Scottfb5e77f2009-04-08 19:17:37 -07001018 if (t != null) {
1019 dismissSubWindow(t);
1020 removeTabFromContentView(t);
1021 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001022 // Destroy all the tabs
1023 mTabControl.destroy();
1024 WebIconDatabase.getInstance().close();
1025 if (mGlsConnection != null) {
1026 unbindService(mGlsConnection);
1027 mGlsConnection = null;
1028 }
1029
Grace Klobab4da0ad2009-05-14 14:45:40 -07001030 unregisterReceiver(mPackageInstallationReceiver);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001031 }
1032
1033 @Override
1034 public void onConfigurationChanged(Configuration newConfig) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04001035 mConfigChanged = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001036 super.onConfigurationChanged(newConfig);
1037
1038 if (mPageInfoDialog != null) {
1039 mPageInfoDialog.dismiss();
1040 showPageInfo(
1041 mPageInfoView,
1042 mPageInfoFromShowSSLCertificateOnError.booleanValue());
1043 }
1044 if (mSSLCertificateDialog != null) {
1045 mSSLCertificateDialog.dismiss();
1046 showSSLCertificate(
1047 mSSLCertificateView);
1048 }
1049 if (mSSLCertificateOnErrorDialog != null) {
1050 mSSLCertificateOnErrorDialog.dismiss();
1051 showSSLCertificateOnError(
1052 mSSLCertificateOnErrorView,
1053 mSSLCertificateOnErrorHandler,
1054 mSSLCertificateOnErrorError);
1055 }
1056 if (mHttpAuthenticationDialog != null) {
1057 String title = ((TextView) mHttpAuthenticationDialog
1058 .findViewById(com.android.internal.R.id.alertTitle)).getText()
1059 .toString();
1060 String name = ((TextView) mHttpAuthenticationDialog
1061 .findViewById(R.id.username_edit)).getText().toString();
1062 String password = ((TextView) mHttpAuthenticationDialog
1063 .findViewById(R.id.password_edit)).getText().toString();
1064 int focusId = mHttpAuthenticationDialog.getCurrentFocus()
1065 .getId();
1066 mHttpAuthenticationDialog.dismiss();
1067 showHttpAuthentication(mHttpAuthHandler, null, null, title,
1068 name, password, focusId);
1069 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001070 }
1071
Grace Kloba22ac16e2009-10-07 18:00:23 -07001072 @Override
1073 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001074 super.onLowMemory();
1075 mTabControl.freeMemory();
1076 }
1077
Mike Reed7bfa63b2009-05-28 11:08:32 -04001078 private boolean resumeWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001079 Tab tab = mTabControl.getCurrentTab();
1080 boolean inLoad = tab.inLoad();
1081 if ((!mActivityInPause && !inLoad) || (mActivityInPause && inLoad)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001082 CookieSyncManager.getInstance().startSync();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001083 WebView w = tab.getWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001084 if (w != null) {
1085 w.resumeTimers();
1086 }
1087 return true;
1088 } else {
1089 return false;
1090 }
1091 }
1092
Mike Reed7bfa63b2009-05-28 11:08:32 -04001093 private boolean pauseWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001094 Tab tab = mTabControl.getCurrentTab();
1095 boolean inLoad = tab.inLoad();
1096 if (mActivityInPause && !inLoad) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001097 CookieSyncManager.getInstance().stopSync();
1098 WebView w = mTabControl.getCurrentWebView();
1099 if (w != null) {
1100 w.pauseTimers();
1101 }
1102 return true;
1103 } else {
1104 return false;
1105 }
1106 }
1107
Leon Scroggins1f005d32009-08-10 17:36:42 -04001108 // FIXME: Do we want to call this when loading google for the first time?
The Android Open Source Project0c908882009-03-03 19:32:16 -08001109 /*
1110 * This function is called when we are launching for the first time. We
1111 * are waiting for the login credentials before loading Google home
1112 * pages. This way the user will be logged in straight away.
1113 */
1114 private void waitForCredentials() {
1115 // Show a toast
1116 mCredsDlg = new ProgressDialog(this);
1117 mCredsDlg.setIndeterminate(true);
1118 mCredsDlg.setMessage(getText(R.string.retrieving_creds_dlg_msg));
1119 // If the user cancels the operation, then cancel the Google
1120 // Credentials request.
1121 mCredsDlg.setCancelMessage(mHandler.obtainMessage(CANCEL_CREDS_REQUEST));
1122 mCredsDlg.show();
1123
1124 // We set a timeout for the retrieval of credentials in onResume()
1125 // as that is when we have freed up some CPU time to get
1126 // the login credentials.
1127 }
1128
1129 /*
1130 * If we have received the credentials or we have timed out and we are
1131 * showing the credentials dialog, then it is time to move on.
1132 */
1133 private void resumeAfterCredentials() {
1134 if (mCredsDlg == null) {
1135 return;
1136 }
1137
1138 // Clear the toast
1139 if (mCredsDlg.isShowing()) {
1140 mCredsDlg.dismiss();
1141 }
1142 mCredsDlg = null;
1143
1144 // Clear any pending timeout
1145 mHandler.removeMessages(CANCEL_CREDS_REQUEST);
1146
1147 // Load the page
1148 WebView w = mTabControl.getCurrentWebView();
1149 if (w != null) {
1150 w.loadUrl(mSettings.getHomePage());
1151 }
1152
1153 // Update the settings, need to do this last as it can take a moment
1154 // to persist the settings. In the mean time we could be loading
1155 // content.
1156 mSettings.setLoginInitialized(this);
1157 }
1158
1159 // Open the icon database and retain all the icons for visited sites.
1160 private void retainIconsOnStartup() {
1161 final WebIconDatabase db = WebIconDatabase.getInstance();
1162 db.open(getDir("icons", 0).getPath());
1163 try {
1164 Cursor c = Browser.getAllBookmarks(mResolver);
1165 if (!c.moveToFirst()) {
1166 c.deactivate();
1167 return;
1168 }
1169 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
1170 do {
1171 String url = c.getString(urlIndex);
1172 db.retainIconForPageUrl(url);
1173 } while (c.moveToNext());
1174 c.deactivate();
1175 } catch (IllegalStateException e) {
1176 Log.e(LOGTAG, "retainIconsOnStartup", e);
1177 }
1178 }
1179
1180 // Helper method for getting the top window.
1181 WebView getTopWindow() {
1182 return mTabControl.getCurrentTopWebView();
1183 }
1184
Grace Kloba22ac16e2009-10-07 18:00:23 -07001185 TabControl getTabControl() {
1186 return mTabControl;
1187 }
1188
The Android Open Source Project0c908882009-03-03 19:32:16 -08001189 @Override
1190 public boolean onCreateOptionsMenu(Menu menu) {
1191 super.onCreateOptionsMenu(menu);
1192
1193 MenuInflater inflater = getMenuInflater();
1194 inflater.inflate(R.menu.browser, menu);
1195 mMenu = menu;
1196 updateInLoadMenuItems();
1197 return true;
1198 }
1199
1200 /**
1201 * As the menu can be open when loading state changes
1202 * we must manually update the state of the stop/reload menu
1203 * item
1204 */
1205 private void updateInLoadMenuItems() {
1206 if (mMenu == null) {
1207 return;
1208 }
1209 MenuItem src = mInLoad ?
1210 mMenu.findItem(R.id.stop_menu_id):
1211 mMenu.findItem(R.id.reload_menu_id);
1212 MenuItem dest = mMenu.findItem(R.id.stop_reload_menu_id);
1213 dest.setIcon(src.getIcon());
1214 dest.setTitle(src.getTitle());
1215 }
1216
1217 @Override
1218 public boolean onContextItemSelected(MenuItem item) {
1219 // chording is not an issue with context menus, but we use the same
1220 // options selector, so set mCanChord to true so we can access them.
1221 mCanChord = true;
1222 int id = item.getItemId();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001223 switch (id) {
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001224 // For the context menu from the title bar
1225 case R.id.title_bar_share_page_url:
1226 case R.id.title_bar_copy_page_url:
1227 WebView mainView = mTabControl.getCurrentWebView();
1228 if (null == mainView) {
1229 return false;
1230 }
1231 if (id == R.id.title_bar_share_page_url) {
1232 Browser.sendString(this, mainView.getUrl());
1233 } else {
1234 copy(mainView.getUrl());
1235 }
1236 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001237 // -- Browser context menu
1238 case R.id.open_context_menu_id:
1239 case R.id.open_newtab_context_menu_id:
1240 case R.id.bookmark_context_menu_id:
1241 case R.id.save_link_context_menu_id:
1242 case R.id.share_link_context_menu_id:
1243 case R.id.copy_link_context_menu_id:
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001244 final WebView webView = getTopWindow();
1245 if (null == webView) {
1246 return false;
1247 }
1248 final HashMap hrefMap = new HashMap();
1249 hrefMap.put("webview", webView);
1250 final Message msg = mHandler.obtainMessage(
1251 FOCUS_NODE_HREF, id, 0, hrefMap);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001252 webView.requestFocusNodeHref(msg);
1253 break;
1254
1255 default:
1256 // For other context menus
1257 return onOptionsItemSelected(item);
1258 }
1259 mCanChord = false;
1260 return true;
1261 }
1262
1263 private Bundle createGoogleSearchSourceBundle(String source) {
1264 Bundle bundle = new Bundle();
1265 bundle.putString(SearchManager.SOURCE, source);
1266 return bundle;
1267 }
1268
1269 /**
The Android Open Source Project4e5f5872009-03-09 11:52:14 -07001270 * Overriding this to insert a local information bundle
The Android Open Source Project0c908882009-03-03 19:32:16 -08001271 */
1272 @Override
1273 public boolean onSearchRequested() {
Leon Scroggins68579392009-09-15 15:31:54 -04001274 if (mOptionsMenuOpen) closeOptionsMenu();
Leon Scroggins5bbe9802009-07-31 13:10:55 -04001275 String url = (getTopWindow() == null) ? null : getTopWindow().getUrl();
Grace Kloba83f47342009-07-20 10:44:31 -07001276 startSearch(mSettings.getHomePage().equals(url) ? null : url, true,
The Android Open Source Project4e5f5872009-03-09 11:52:14 -07001277 createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_SEARCHKEY), false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001278 return true;
1279 }
1280
1281 @Override
1282 public void startSearch(String initialQuery, boolean selectInitialQuery,
1283 Bundle appSearchData, boolean globalSearch) {
1284 if (appSearchData == null) {
1285 appSearchData = createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_TYPE);
1286 }
1287 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1288 }
1289
Leon Scroggins1f005d32009-08-10 17:36:42 -04001290 /**
1291 * Switch tabs. Called by the TitleBarSet when sliding the title bar
1292 * results in changing tabs.
Leon Scroggins160a7e72009-08-14 18:28:01 -04001293 * @param index Index of the tab to change to, as defined by
1294 * mTabControl.getTabIndex(Tab t).
1295 * @return boolean True if we successfully switched to a different tab. If
1296 * the indexth tab is null, or if that tab is the same as
1297 * the current one, return false.
Leon Scroggins1f005d32009-08-10 17:36:42 -04001298 */
Leon Scroggins160a7e72009-08-14 18:28:01 -04001299 /* package */ boolean switchToTab(int index) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001300 Tab tab = mTabControl.getTab(index);
1301 Tab currentTab = mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001302 if (tab == null || tab == currentTab) {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001303 return false;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001304 }
1305 if (currentTab != null) {
1306 // currentTab may be null if it was just removed. In that case,
1307 // we do not need to remove it
1308 removeTabFromContentView(currentTab);
1309 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001310 mTabControl.setCurrentTab(tab);
1311 attachTabToContentView(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001312 resetTitleIconAndProgress();
1313 updateLockIconToLatest();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001314 return true;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001315 }
1316
Grace Kloba22ac16e2009-10-07 18:00:23 -07001317 /* package */ Tab openTabToHomePage() {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001318 return openTabAndShow(mSettings.getHomePage(), false, null);
1319 }
1320
Leon Scroggins1f005d32009-08-10 17:36:42 -04001321 /* package */ void closeCurrentWindow() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001322 final Tab current = mTabControl.getCurrentTab();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001323 if (mTabControl.getTabCount() == 1) {
Leon Scroggins30444232009-09-04 18:36:20 -04001324 // This is the last tab. Open a new one, with the home
1325 // page and close the current one.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001326 openTabToHomePage();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001327 closeTab(current);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001328 return;
1329 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001330 final Tab parent = current.getParentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001331 int indexToShow = -1;
1332 if (parent != null) {
1333 indexToShow = mTabControl.getTabIndex(parent);
1334 } else {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001335 final int currentIndex = mTabControl.getCurrentIndex();
1336 // Try to move to the tab to the right
1337 indexToShow = currentIndex + 1;
1338 if (indexToShow > mTabControl.getTabCount() - 1) {
1339 // Try to move to the tab to the left
1340 indexToShow = currentIndex - 1;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001341 }
1342 }
Leon Scroggins160a7e72009-08-14 18:28:01 -04001343 if (switchToTab(indexToShow)) {
1344 // Close window
1345 closeTab(current);
1346 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001347 }
1348
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001349 private ActiveTabsPage mActiveTabsPage;
1350
1351 /**
1352 * Remove the active tabs page.
1353 * @param needToAttach If true, the active tabs page did not attach a tab
1354 * to the content view, so we need to do that here.
1355 */
1356 /* package */ void removeActiveTabPage(boolean needToAttach) {
1357 mContentView.removeView(mActiveTabsPage);
1358 mActiveTabsPage = null;
1359 mMenuState = R.id.MAIN_MENU;
1360 if (needToAttach) {
1361 attachTabToContentView(mTabControl.getCurrentTab());
1362 }
1363 getTopWindow().requestFocus();
1364 }
1365
The Android Open Source Project0c908882009-03-03 19:32:16 -08001366 @Override
1367 public boolean onOptionsItemSelected(MenuItem item) {
1368 if (!mCanChord) {
1369 // The user has already fired a shortcut with this hold down of the
1370 // menu key.
1371 return false;
1372 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001373 if (null == getTopWindow()) {
Leon Scroggins0d7ae0e2009-06-05 11:04:45 -04001374 return false;
1375 }
Grace Kloba6ee9c492009-07-13 10:04:34 -07001376 if (mMenuIsDown) {
1377 // The shortcut action consumes the MENU. Even if it is still down,
1378 // it won't trigger the next shortcut action. In the case of the
1379 // shortcut action triggering a new activity, like Bookmarks, we
1380 // won't get onKeyUp for MENU. So it is important to reset it here.
1381 mMenuIsDown = false;
1382 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001383 switch (item.getItemId()) {
1384 // -- Main menu
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001385 case R.id.new_tab_menu_id:
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001386 openTabToHomePage();
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001387 break;
1388
Leon Scroggins64b80f32009-08-07 12:03:34 -04001389 case R.id.goto_menu_id:
Leon Scrogginsb3a5bed2009-09-28 11:21:56 -04001390 onSearchRequested();
1391 break;
1392
1393 case R.id.bookmarks_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001394 bookmarksOrHistoryPicker(false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001395 break;
1396
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001397 case R.id.active_tabs_menu_id:
1398 mActiveTabsPage = new ActiveTabsPage(this, mTabControl);
1399 removeTabFromContentView(mTabControl.getCurrentTab());
Leon Scroggins43de6162009-09-14 19:59:58 -04001400 hideFakeTitleBar();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001401 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
1402 mActiveTabsPage.requestFocus();
1403 mMenuState = EMPTY_MENU;
1404 break;
1405
Leon Scroggins1f005d32009-08-10 17:36:42 -04001406 case R.id.add_bookmark_menu_id:
1407 Intent i = new Intent(BrowserActivity.this,
1408 AddBookmarkPage.class);
1409 WebView w = getTopWindow();
1410 i.putExtra("url", w.getUrl());
1411 i.putExtra("title", w.getTitle());
Grace Kloba83cdb2c2009-09-16 00:48:57 -07001412 i.putExtra("touch_icon_url", w.getTouchIconUrl());
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01001413 i.putExtra("thumbnail", createScreenshot(w));
Leon Scroggins1f005d32009-08-10 17:36:42 -04001414 startActivity(i);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001415 break;
1416
1417 case R.id.stop_reload_menu_id:
1418 if (mInLoad) {
1419 stopLoading();
1420 } else {
1421 getTopWindow().reload();
1422 }
1423 break;
1424
1425 case R.id.back_menu_id:
1426 getTopWindow().goBack();
1427 break;
1428
1429 case R.id.forward_menu_id:
1430 getTopWindow().goForward();
1431 break;
1432
1433 case R.id.close_menu_id:
1434 // Close the subwindow if it exists.
1435 if (mTabControl.getCurrentSubWindow() != null) {
1436 dismissSubWindow(mTabControl.getCurrentTab());
1437 break;
1438 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001439 closeCurrentWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001440 break;
1441
1442 case R.id.homepage_menu_id:
Grace Kloba22ac16e2009-10-07 18:00:23 -07001443 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001444 if (current != null) {
1445 dismissSubWindow(current);
1446 current.getWebView().loadUrl(mSettings.getHomePage());
1447 }
1448 break;
1449
1450 case R.id.preferences_menu_id:
1451 Intent intent = new Intent(this,
1452 BrowserPreferencesPage.class);
1453 startActivityForResult(intent, PREFERENCES_PAGE);
1454 break;
1455
1456 case R.id.find_menu_id:
1457 if (null == mFindDialog) {
1458 mFindDialog = new FindDialog(this);
1459 }
1460 mFindDialog.setWebView(getTopWindow());
1461 mFindDialog.show();
1462 mMenuState = EMPTY_MENU;
1463 break;
1464
1465 case R.id.select_text_id:
1466 getTopWindow().emulateShiftHeld();
1467 break;
1468 case R.id.page_info_menu_id:
1469 showPageInfo(mTabControl.getCurrentTab(), false);
1470 break;
1471
1472 case R.id.classic_history_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001473 bookmarksOrHistoryPicker(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001474 break;
1475
1476 case R.id.share_page_menu_id:
Andrei Popescu10fdba82009-09-24 13:25:47 +01001477 Browser.sendString(this, getTopWindow().getUrl(),
1478 getText(R.string.choosertitle_sharevia).toString());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001479 break;
1480
1481 case R.id.dump_nav_menu_id:
1482 getTopWindow().debugDump();
1483 break;
1484
1485 case R.id.zoom_in_menu_id:
1486 getTopWindow().zoomIn();
1487 break;
1488
1489 case R.id.zoom_out_menu_id:
1490 getTopWindow().zoomOut();
1491 break;
1492
1493 case R.id.view_downloads_menu_id:
1494 viewDownloads(null);
1495 break;
1496
The Android Open Source Project0c908882009-03-03 19:32:16 -08001497 case R.id.window_one_menu_id:
1498 case R.id.window_two_menu_id:
1499 case R.id.window_three_menu_id:
1500 case R.id.window_four_menu_id:
1501 case R.id.window_five_menu_id:
1502 case R.id.window_six_menu_id:
1503 case R.id.window_seven_menu_id:
1504 case R.id.window_eight_menu_id:
1505 {
1506 int menuid = item.getItemId();
1507 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1508 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001509 Tab desiredTab = mTabControl.getTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001510 if (desiredTab != null &&
1511 desiredTab != mTabControl.getCurrentTab()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001512 switchToTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001513 }
1514 break;
1515 }
1516 }
1517 }
1518 break;
1519
1520 default:
1521 if (!super.onOptionsItemSelected(item)) {
1522 return false;
1523 }
1524 // Otherwise fall through.
1525 }
1526 mCanChord = false;
1527 return true;
1528 }
1529
1530 public void closeFind() {
1531 mMenuState = R.id.MAIN_MENU;
1532 }
1533
Grace Kloba22ac16e2009-10-07 18:00:23 -07001534 @Override
1535 public boolean onPrepareOptionsMenu(Menu menu) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001536 // This happens when the user begins to hold down the menu key, so
1537 // allow them to chord to get a shortcut.
1538 mCanChord = true;
1539 // Note: setVisible will decide whether an item is visible; while
1540 // setEnabled() will decide whether an item is enabled, which also means
1541 // whether the matching shortcut key will function.
1542 super.onPrepareOptionsMenu(menu);
1543 switch (mMenuState) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001544 case EMPTY_MENU:
1545 if (mCurrentMenuState != mMenuState) {
1546 menu.setGroupVisible(R.id.MAIN_MENU, false);
1547 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1548 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001549 }
1550 break;
1551 default:
1552 if (mCurrentMenuState != mMenuState) {
1553 menu.setGroupVisible(R.id.MAIN_MENU, true);
1554 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1555 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001556 }
1557 final WebView w = getTopWindow();
1558 boolean canGoBack = false;
1559 boolean canGoForward = false;
1560 boolean isHome = false;
1561 if (w != null) {
1562 canGoBack = w.canGoBack();
1563 canGoForward = w.canGoForward();
1564 isHome = mSettings.getHomePage().equals(w.getUrl());
1565 }
1566 final MenuItem back = menu.findItem(R.id.back_menu_id);
1567 back.setEnabled(canGoBack);
1568
1569 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1570 home.setEnabled(!isHome);
1571
1572 menu.findItem(R.id.forward_menu_id)
1573 .setEnabled(canGoForward);
1574
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001575 menu.findItem(R.id.new_tab_menu_id).setEnabled(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001576 mTabControl.canCreateNewTab());
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001577
The Android Open Source Project0c908882009-03-03 19:32:16 -08001578 // decide whether to show the share link option
1579 PackageManager pm = getPackageManager();
1580 Intent send = new Intent(Intent.ACTION_SEND);
1581 send.setType("text/plain");
1582 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1583 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1584
The Android Open Source Project0c908882009-03-03 19:32:16 -08001585 boolean isNavDump = mSettings.isNavDump();
1586 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1587 nav.setVisible(isNavDump);
1588 nav.setEnabled(isNavDump);
1589 break;
1590 }
1591 mCurrentMenuState = mMenuState;
1592 return true;
1593 }
1594
1595 @Override
1596 public void onCreateContextMenu(ContextMenu menu, View v,
1597 ContextMenuInfo menuInfo) {
1598 WebView webview = (WebView) v;
1599 WebView.HitTestResult result = webview.getHitTestResult();
1600 if (result == null) {
1601 return;
1602 }
1603
1604 int type = result.getType();
1605 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1606 Log.w(LOGTAG,
1607 "We should not show context menu when nothing is touched");
1608 return;
1609 }
1610 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1611 // let TextView handles context menu
1612 return;
1613 }
1614
1615 // Note, http://b/issue?id=1106666 is requesting that
1616 // an inflated menu can be used again. This is not available
1617 // yet, so inflate each time (yuk!)
1618 MenuInflater inflater = getMenuInflater();
1619 inflater.inflate(R.menu.browsercontext, menu);
1620
1621 // Show the correct menu group
1622 String extra = result.getExtra();
1623 menu.setGroupVisible(R.id.PHONE_MENU,
1624 type == WebView.HitTestResult.PHONE_TYPE);
1625 menu.setGroupVisible(R.id.EMAIL_MENU,
1626 type == WebView.HitTestResult.EMAIL_TYPE);
1627 menu.setGroupVisible(R.id.GEO_MENU,
1628 type == WebView.HitTestResult.GEO_TYPE);
1629 menu.setGroupVisible(R.id.IMAGE_MENU,
1630 type == WebView.HitTestResult.IMAGE_TYPE
1631 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1632 menu.setGroupVisible(R.id.ANCHOR_MENU,
1633 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1634 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1635
1636 // Setup custom handling depending on the type
1637 switch (type) {
1638 case WebView.HitTestResult.PHONE_TYPE:
1639 menu.setHeaderTitle(Uri.decode(extra));
1640 menu.findItem(R.id.dial_context_menu_id).setIntent(
1641 new Intent(Intent.ACTION_VIEW, Uri
1642 .parse(WebView.SCHEME_TEL + extra)));
1643 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1644 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
Cary Clark5e335a32009-09-22 14:53:11 -04001645 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001646 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1647 addIntent);
1648 menu.findItem(R.id.copy_phone_context_menu_id).setOnMenuItemClickListener(
1649 new Copy(extra));
1650 break;
1651
1652 case WebView.HitTestResult.EMAIL_TYPE:
1653 menu.setHeaderTitle(extra);
1654 menu.findItem(R.id.email_context_menu_id).setIntent(
1655 new Intent(Intent.ACTION_VIEW, Uri
1656 .parse(WebView.SCHEME_MAILTO + extra)));
1657 menu.findItem(R.id.copy_mail_context_menu_id).setOnMenuItemClickListener(
1658 new Copy(extra));
1659 break;
1660
1661 case WebView.HitTestResult.GEO_TYPE:
1662 menu.setHeaderTitle(extra);
1663 menu.findItem(R.id.map_context_menu_id).setIntent(
1664 new Intent(Intent.ACTION_VIEW, Uri
1665 .parse(WebView.SCHEME_GEO
1666 + URLEncoder.encode(extra))));
1667 menu.findItem(R.id.copy_geo_context_menu_id).setOnMenuItemClickListener(
1668 new Copy(extra));
1669 break;
1670
1671 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1672 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
1673 TextView titleView = (TextView) LayoutInflater.from(this)
1674 .inflate(android.R.layout.browser_link_context_header,
1675 null);
1676 titleView.setText(extra);
1677 menu.setHeaderView(titleView);
1678 // decide whether to show the open link in new tab option
1679 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001680 mTabControl.canCreateNewTab());
Ben Murdochde353622009-10-12 10:29:00 +01001681 menu.findItem(R.id.bookmark_context_menu_id).setVisible(
1682 Bookmarks.urlHasAcceptableScheme(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001683 PackageManager pm = getPackageManager();
1684 Intent send = new Intent(Intent.ACTION_SEND);
1685 send.setType("text/plain");
1686 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1687 menu.findItem(R.id.share_link_context_menu_id).setVisible(ri != null);
1688 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1689 break;
1690 }
1691 // otherwise fall through to handle image part
1692 case WebView.HitTestResult.IMAGE_TYPE:
1693 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1694 menu.setHeaderTitle(extra);
1695 }
1696 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1697 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1698 menu.findItem(R.id.download_context_menu_id).
1699 setOnMenuItemClickListener(new Download(extra));
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001700 menu.findItem(R.id.set_wallpaper_context_menu_id).
1701 setOnMenuItemClickListener(new SetAsWallpaper(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001702 break;
1703
1704 default:
1705 Log.w(LOGTAG, "We should not get here.");
1706 break;
1707 }
Leon Scrogginsb2b19f52009-10-09 16:10:00 -04001708 hideFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001709 }
1710
The Android Open Source Project0c908882009-03-03 19:32:16 -08001711 // Attach the given tab to the content view.
Grace Klobac928c302009-09-17 11:51:21 -07001712 // this should only be called for the current tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001713 private void attachTabToContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001714 // Attach the container that contains the main WebView and any other UI
1715 // associated with the tab.
Patrick Scottd0119532009-09-17 08:00:31 -04001716 t.attachTabToContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001717
1718 if (mShouldShowErrorConsole) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001719 ErrorConsoleView errorConsole = t.getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01001720 if (errorConsole.numberOfErrors() == 0) {
1721 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
1722 } else {
1723 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1724 }
1725
1726 mErrorConsoleContainer.addView(errorConsole,
1727 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
1728 ViewGroup.LayoutParams.WRAP_CONTENT));
1729 }
1730
Leon Scroggins39ab28e2009-09-02 21:20:30 -04001731 WebView view = t.getWebView();
Leon Scroggins55a5bc22009-09-04 17:00:08 -04001732 view.setEmbeddedTitleBar(mTitleBar);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001733 // Request focus on the top window.
1734 t.getTopWindow().requestFocus();
1735 }
1736
1737 // Attach a sub window to the main WebView of the given tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 void attachSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001739 t.attachSubWindow(mContentView);
1740 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001741 }
1742
1743 // Remove the given tab from the content view.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001744 private void removeTabFromContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001745 // Remove the container that contains the main WebView.
Patrick Scottd0119532009-09-17 08:00:31 -04001746 t.removeTabFromContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001747
Grace Kloba22ac16e2009-10-07 18:00:23 -07001748 ErrorConsoleView errorConsole = t.getErrorConsole(false);
1749 if (errorConsole != null) {
1750 mErrorConsoleContainer.removeView(errorConsole);
Ben Murdochbff2d602009-07-01 20:19:05 +01001751 }
1752
Leon Scroggins39ab28e2009-09-02 21:20:30 -04001753 WebView view = t.getWebView();
Leon Scrogginsbb85b902009-09-14 19:27:20 -04001754 if (view != null) {
1755 view.setEmbeddedTitleBar(null);
1756 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001757 }
1758
1759 // Remove the sub window if it exists. Also called by TabControl when the
1760 // user clicks the 'X' to dismiss a sub window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001761 /* package */ void dismissSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001762 t.removeSubWindow(mContentView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001763 // dismiss the subwindow. This will destroy the WebView.
1764 t.dismissSubWindow();
Patrick Scottd0119532009-09-17 08:00:31 -04001765 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001766 }
1767
Leon Scroggins1f005d32009-08-10 17:36:42 -04001768 // A wrapper function of {@link #openTabAndShow(UrlData, boolean, String)}
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07001769 // that accepts url as string.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001770 private Tab openTabAndShow(String url, boolean closeOnExit, String appId) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001771 return openTabAndShow(new UrlData(url), closeOnExit, appId);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001772 }
1773
1774 // This method does a ton of stuff. It will attempt to create a new tab
1775 // if we haven't reached MAX_TABS. Otherwise it uses the current tab. If
Leon Scroggins1f005d32009-08-10 17:36:42 -04001776 // url isn't null, it will load the given url.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001777 /* package */Tab openTabAndShow(UrlData urlData, boolean closeOnExit,
1778 String appId) {
1779 final Tab currentTab = mTabControl.getCurrentTab();
1780 if (mTabControl.canCreateNewTab()) {
1781 final Tab tab = mTabControl.createNewTab(closeOnExit, appId,
1782 urlData.mUrl);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001783 WebView webview = tab.getWebView();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001784 // If the last tab was removed from the active tabs page, currentTab
1785 // will be null.
1786 if (currentTab != null) {
1787 removeTabFromContentView(currentTab);
1788 }
Patrick Scott8bbd69f2009-08-14 13:35:53 -04001789 // We must set the new tab as the current tab to reflect the old
1790 // animation behavior.
1791 mTabControl.setCurrentTab(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001792 attachTabToContentView(tab);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001793 if (!urlData.isEmpty()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001794 urlData.loadIn(webview);
1795 }
1796 return tab;
1797 } else {
1798 // Get rid of the subwindow if it exists
1799 dismissSubWindow(currentTab);
1800 if (!urlData.isEmpty()) {
1801 // Load the given url.
1802 urlData.loadIn(currentTab.getWebView());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001803 }
1804 }
Grace Klobac9181842009-04-14 08:53:22 -07001805 return currentTab;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001806 }
1807
Grace Kloba22ac16e2009-10-07 18:00:23 -07001808 private Tab openTab(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001809 if (mSettings.openInBackground()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001810 Tab t = mTabControl.createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001811 if (t != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001812 WebView view = t.getWebView();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001813 view.loadUrl(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001814 }
Grace Klobac9181842009-04-14 08:53:22 -07001815 return t;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001816 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001817 return openTabAndShow(url, false, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001818 }
1819 }
1820
1821 private class Copy implements OnMenuItemClickListener {
1822 private CharSequence mText;
1823
1824 public boolean onMenuItemClick(MenuItem item) {
1825 copy(mText);
1826 return true;
1827 }
1828
1829 public Copy(CharSequence toCopy) {
1830 mText = toCopy;
1831 }
1832 }
1833
1834 private class Download implements OnMenuItemClickListener {
1835 private String mText;
1836
1837 public boolean onMenuItemClick(MenuItem item) {
1838 onDownloadStartNoStream(mText, null, null, null, -1);
1839 return true;
1840 }
1841
1842 public Download(String toDownload) {
1843 mText = toDownload;
1844 }
1845 }
1846
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001847 private class SetAsWallpaper extends Thread implements
1848 OnMenuItemClickListener, DialogInterface.OnCancelListener {
1849 private URL mUrl;
1850 private ProgressDialog mWallpaperProgress;
1851 private boolean mCanceled = false;
1852
1853 public SetAsWallpaper(String url) {
1854 try {
1855 mUrl = new URL(url);
1856 } catch (MalformedURLException e) {
1857 mUrl = null;
1858 }
1859 }
1860
1861 public void onCancel(DialogInterface dialog) {
1862 mCanceled = true;
1863 }
1864
1865 public boolean onMenuItemClick(MenuItem item) {
1866 if (mUrl != null) {
1867 // The user may have tried to set a image with a large file size as their
1868 // background so it may take a few moments to perform the operation. Display
1869 // a progress spinner while it is working.
1870 mWallpaperProgress = new ProgressDialog(BrowserActivity.this);
1871 mWallpaperProgress.setIndeterminate(true);
1872 mWallpaperProgress.setMessage(getText(R.string.progress_dialog_setting_wallpaper));
1873 mWallpaperProgress.setCancelable(true);
1874 mWallpaperProgress.setOnCancelListener(this);
1875 mWallpaperProgress.show();
1876 start();
1877 }
1878 return true;
1879 }
1880
1881 public void run() {
1882 Drawable oldWallpaper = BrowserActivity.this.getWallpaper();
1883 try {
1884 // TODO: This will cause the resource to be downloaded again, when we
1885 // should in most cases be able to grab it from the cache. To fix this
1886 // we should query WebCore to see if we can access a cached version and
1887 // instead open an input stream on that. This pattern could also be used
1888 // in the download manager where the same problem exists.
1889 InputStream inputstream = mUrl.openStream();
1890 if (inputstream != null) {
1891 setWallpaper(inputstream);
1892 }
1893 } catch (IOException e) {
1894 Log.e(LOGTAG, "Unable to set new wallpaper");
1895 // Act as though the user canceled the operation so we try to
1896 // restore the old wallpaper.
1897 mCanceled = true;
1898 }
1899
1900 if (mCanceled) {
1901 // Restore the old wallpaper if the user cancelled whilst we were setting
1902 // the new wallpaper.
1903 int width = oldWallpaper.getIntrinsicWidth();
1904 int height = oldWallpaper.getIntrinsicHeight();
1905 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1906 Canvas canvas = new Canvas(bm);
1907 oldWallpaper.setBounds(0, 0, width, height);
1908 oldWallpaper.draw(canvas);
1909 try {
1910 setWallpaper(bm);
1911 } catch (IOException e) {
1912 Log.e(LOGTAG, "Unable to restore old wallpaper.");
1913 }
1914 mCanceled = false;
1915 }
1916
1917 if (mWallpaperProgress.isShowing()) {
1918 mWallpaperProgress.dismiss();
1919 }
1920 }
1921 }
1922
The Android Open Source Project0c908882009-03-03 19:32:16 -08001923 private void copy(CharSequence text) {
1924 try {
1925 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
1926 if (clip != null) {
1927 clip.setClipboardText(text);
1928 }
1929 } catch (android.os.RemoteException e) {
1930 Log.e(LOGTAG, "Copy failed", e);
1931 }
1932 }
1933
1934 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001935 * Resets the browser title-view to whatever it must be
1936 * (for example, if we had a loading error)
1937 * When we have a new page, we call resetTitle, when we
1938 * have to reset the titlebar to whatever it used to be
1939 * (for example, if the user chose to stop loading), we
1940 * call resetTitleAndRevertLockIcon.
1941 */
1942 /* package */ void resetTitleAndRevertLockIcon() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001943 mTabControl.getCurrentTab().revertLockIcon();
1944 updateLockIconToLatest();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001945 resetTitleIconAndProgress();
1946 }
1947
1948 /**
1949 * Reset the title, favicon, and progress.
1950 */
1951 private void resetTitleIconAndProgress() {
1952 WebView current = mTabControl.getCurrentWebView();
1953 if (current == null) {
1954 return;
1955 }
1956 resetTitleAndIcon(current);
1957 int progress = current.getProgress();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001958 current.getWebChromeClient().onProgressChanged(current, progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001959 }
1960
1961 // Reset the title and the icon based on the given item.
1962 private void resetTitleAndIcon(WebView view) {
1963 WebHistoryItem item = view.copyBackForwardList().getCurrentItem();
1964 if (item != null) {
Leon Scroggins68579392009-09-15 15:31:54 -04001965 setUrlTitle(item.getUrl(), item.getTitle());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001966 setFavicon(item.getFavicon());
1967 } else {
Leon Scroggins68579392009-09-15 15:31:54 -04001968 setUrlTitle(null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001969 setFavicon(null);
1970 }
1971 }
1972
1973 /**
1974 * Sets a title composed of the URL and the title string.
1975 * @param url The URL of the site being loaded.
1976 * @param title The title of the site being loaded.
1977 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07001978 void setUrlTitle(String url, String title) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001979 mUrl = url;
1980 mTitle = title;
1981
Leon Scroggins68579392009-09-15 15:31:54 -04001982 mTitleBar.setTitleAndUrl(title, url);
Leon Scrogginsfe87bd32009-10-06 10:10:00 -04001983 mFakeTitleBar.setTitleAndUrl(title, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001984 }
1985
1986 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001987 * @param url The URL to build a title version of the URL from.
1988 * @return The title version of the URL or null if fails.
1989 * The title version of the URL can be either the URL hostname,
1990 * or the hostname with an "https://" prefix (for secure URLs),
1991 * or an empty string if, for example, the URL in question is a
1992 * file:// URL with no hostname.
1993 */
Leon Scroggins32e14a62009-06-11 10:26:34 -04001994 /* package */ static String buildTitleUrl(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001995 String titleUrl = null;
1996
1997 if (url != null) {
1998 try {
1999 // parse the url string
2000 URL urlObj = new URL(url);
2001 if (urlObj != null) {
2002 titleUrl = "";
2003
2004 String protocol = urlObj.getProtocol();
2005 String host = urlObj.getHost();
2006
2007 if (host != null && 0 < host.length()) {
2008 titleUrl = host;
2009 if (protocol != null) {
2010 // if a secure site, add an "https://" prefix!
2011 if (protocol.equalsIgnoreCase("https")) {
2012 titleUrl = protocol + "://" + host;
2013 }
2014 }
2015 }
2016 }
2017 } catch (MalformedURLException e) {}
2018 }
2019
2020 return titleUrl;
2021 }
2022
2023 // Set the favicon in the title bar.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002024 void setFavicon(Bitmap icon) {
Leon Scroggins68579392009-09-15 15:31:54 -04002025 mTitleBar.setFavicon(icon);
Leon Scrogginsfe87bd32009-10-06 10:10:00 -04002026 mFakeTitleBar.setFavicon(icon);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002027 }
2028
2029 /**
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002030 * Close the tab, remove its associated title bar, and adjust mTabControl's
2031 * current tab to a valid value.
Leon Scroggins1f005d32009-08-10 17:36:42 -04002032 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002033 /* package */ void closeTab(Tab t) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002034 int currentIndex = mTabControl.getCurrentIndex();
2035 int removeIndex = mTabControl.getTabIndex(t);
Leon Scroggins1f005d32009-08-10 17:36:42 -04002036 mTabControl.removeTab(t);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002037 if (currentIndex >= removeIndex && currentIndex != 0) {
2038 currentIndex--;
2039 }
2040 mTabControl.setCurrentTab(mTabControl.getTab(currentIndex));
Andrei Popescua5bf1de2009-09-23 16:39:23 +01002041 resetTitleIconAndProgress();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002042 }
2043
2044 private void goBackOnePageOrQuit() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002045 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002046 if (current == null) {
2047 /*
2048 * Instead of finishing the activity, simply push this to the back
2049 * of the stack and let ActivityManager to choose the foreground
2050 * activity. As BrowserActivity is singleTask, it will be always the
2051 * root of the task. So we can use either true or false for
2052 * moveTaskToBack().
2053 */
2054 moveTaskToBack(true);
Grace Kloba00d85e72009-09-23 18:50:05 -07002055 return;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002056 }
2057 WebView w = current.getWebView();
2058 if (w.canGoBack()) {
2059 w.goBack();
2060 } else {
2061 // Check to see if we are closing a window that was created by
2062 // another window. If so, we switch back to that window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002063 Tab parent = current.getParentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002064 if (parent != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04002065 switchToTab(mTabControl.getTabIndex(parent));
2066 // Now we close the other tab
2067 closeTab(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002068 } else {
2069 if (current.closeOnExit()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002070 // force the tab's inLoad() to be false as we are going to
2071 // either finish the activity or remove the tab. This will
2072 // ensure pauseWebViewTimers() taking action.
2073 mTabControl.getCurrentTab().clearInLoad();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002074 if (mTabControl.getTabCount() == 1) {
2075 finish();
2076 return;
2077 }
Mike Reed7bfa63b2009-05-28 11:08:32 -04002078 // call pauseWebViewTimers() now, we won't be able to call
2079 // it in onPause() as the WebView won't be valid.
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002080 // Temporarily change mActivityInPause to be true as
2081 // pauseWebViewTimers() will do nothing if mActivityInPause
2082 // is false.
Grace Kloba918e1d72009-08-13 14:55:06 -07002083 boolean savedState = mActivityInPause;
2084 if (savedState) {
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002085 Log.e(LOGTAG, "BrowserActivity is already paused "
2086 + "while handing goBackOnePageOrQuit.");
Grace Kloba918e1d72009-08-13 14:55:06 -07002087 }
2088 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -04002089 pauseWebViewTimers();
Grace Kloba918e1d72009-08-13 14:55:06 -07002090 mActivityInPause = savedState;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002091 removeTabFromContentView(current);
2092 mTabControl.removeTab(current);
2093 }
2094 /*
2095 * Instead of finishing the activity, simply push this to the back
2096 * of the stack and let ActivityManager to choose the foreground
2097 * activity. As BrowserActivity is singleTask, it will be always the
2098 * root of the task. So we can use either true or false for
2099 * moveTaskToBack().
2100 */
2101 moveTaskToBack(true);
2102 }
2103 }
2104 }
2105
Grace Kloba22ac16e2009-10-07 18:00:23 -07002106 boolean isMenuDown() {
2107 return mMenuIsDown;
2108 }
2109
Grace Kloba5942df02009-09-18 11:48:29 -07002110 @Override
2111 public boolean onKeyDown(int keyCode, KeyEvent event) {
2112 // The default key mode is DEFAULT_KEYS_SEARCH_LOCAL. As the MENU is
2113 // still down, we don't want to trigger the search. Pretend to consume
2114 // the key and do nothing.
2115 if (mMenuIsDown) return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002116
Grace Kloba5942df02009-09-18 11:48:29 -07002117 switch(keyCode) {
2118 case KeyEvent.KEYCODE_MENU:
2119 mMenuIsDown = true;
2120 break;
2121 case KeyEvent.KEYCODE_SPACE:
Grace Klobada0fe552009-09-22 18:17:24 -07002122 // WebView/WebTextView handle the keys in the KeyDown. As
2123 // the Activity's shortcut keys are only handled when WebView
2124 // doesn't, have to do it in onKeyDown instead of onKeyUp.
2125 if (event.isShiftPressed()) {
2126 getTopWindow().pageUp(false);
2127 } else {
2128 getTopWindow().pageDown(false);
2129 }
Grace Kloba5942df02009-09-18 11:48:29 -07002130 return true;
2131 case KeyEvent.KEYCODE_BACK:
2132 if (event.getRepeatCount() == 0) {
2133 event.startTracking();
2134 return true;
2135 } else if (mCustomView == null && mActiveTabsPage == null
2136 && event.isLongPress()) {
2137 bookmarksOrHistoryPicker(true);
2138 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002139 }
Grace Kloba5942df02009-09-18 11:48:29 -07002140 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002141 }
Grace Kloba5942df02009-09-18 11:48:29 -07002142 return super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002143 }
2144
Grace Kloba5942df02009-09-18 11:48:29 -07002145 @Override
2146 public boolean onKeyUp(int keyCode, KeyEvent event) {
2147 switch(keyCode) {
2148 case KeyEvent.KEYCODE_MENU:
2149 mMenuIsDown = false;
2150 break;
Grace Kloba5942df02009-09-18 11:48:29 -07002151 case KeyEvent.KEYCODE_BACK:
2152 if (event.isTracking() && !event.isCanceled()) {
2153 if (mCustomView != null) {
2154 // if a custom view is showing, hide it
Grace Kloba22ac16e2009-10-07 18:00:23 -07002155 mTabControl.getCurrentWebView().getWebChromeClient()
2156 .onHideCustomView();
Grace Kloba5942df02009-09-18 11:48:29 -07002157 } else if (mActiveTabsPage != null) {
2158 // if tab page is showing, hide it
2159 removeActiveTabPage(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002160 } else {
Grace Kloba5942df02009-09-18 11:48:29 -07002161 WebView subwindow = mTabControl.getCurrentSubWindow();
2162 if (subwindow != null) {
2163 if (subwindow.canGoBack()) {
2164 subwindow.goBack();
2165 } else {
2166 dismissSubWindow(mTabControl.getCurrentTab());
2167 }
2168 } else {
2169 goBackOnePageOrQuit();
2170 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002171 }
Grace Kloba5942df02009-09-18 11:48:29 -07002172 return true;
2173 }
2174 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002175 }
Grace Kloba5942df02009-09-18 11:48:29 -07002176 return super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002177 }
2178
Leon Scroggins68579392009-09-15 15:31:54 -04002179 /* package */ void stopLoading() {
Ben Murdochb7cc8b42009-09-28 10:59:09 +01002180 mDidStopLoad = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002181 resetTitleAndRevertLockIcon();
2182 WebView w = getTopWindow();
2183 w.stopLoading();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002184 // FIXME: before refactor, it is using mWebViewClient. So I keep the
2185 // same logic here. But for subwindow case, should we call into the main
2186 // WebView's onPageFinished as we never call its onPageStarted and if
2187 // the page finishes itself, we don't call onPageFinished.
2188 mTabControl.getCurrentWebView().getWebViewClient().onPageFinished(w,
2189 w.getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002190
2191 cancelStopToast();
2192 mStopToast = Toast
2193 .makeText(this, R.string.stopping, Toast.LENGTH_SHORT);
2194 mStopToast.show();
2195 }
2196
Grace Kloba22ac16e2009-10-07 18:00:23 -07002197 boolean didUserStopLoading() {
2198 return mDidStopLoad;
2199 }
2200
The Android Open Source Project0c908882009-03-03 19:32:16 -08002201 private void cancelStopToast() {
2202 if (mStopToast != null) {
2203 mStopToast.cancel();
2204 mStopToast = null;
2205 }
2206 }
2207
Grace Kloba22ac16e2009-10-07 18:00:23 -07002208 // called by a UI or non-UI thread to post the message
2209 public void postMessage(int what, int arg1, int arg2, Object obj,
2210 long delayMillis) {
2211 mHandler.sendMessageDelayed(mHandler.obtainMessage(what, arg1, arg2,
2212 obj), delayMillis);
2213 }
2214
2215 // called by a UI or non-UI thread to remove the message
2216 void removeMessages(int what, Object object) {
2217 mHandler.removeMessages(what, object);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002218 }
2219
2220 // public message ids
2221 public final static int LOAD_URL = 1001;
2222 public final static int STOP_LOAD = 1002;
2223
2224 // Message Ids
2225 private static final int FOCUS_NODE_HREF = 102;
2226 private static final int CANCEL_CREDS_REQUEST = 103;
Grace Kloba92c18a52009-07-31 23:48:32 -07002227 private static final int RELEASE_WAKELOCK = 107;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002228
Grace Kloba22ac16e2009-10-07 18:00:23 -07002229 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
Ben Murdoch2694e232009-09-29 09:41:11 +01002230
The Android Open Source Project0c908882009-03-03 19:32:16 -08002231 // Private handler for handling javascript and saving passwords
2232 private Handler mHandler = new Handler() {
2233
2234 public void handleMessage(Message msg) {
2235 switch (msg.what) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002236 case FOCUS_NODE_HREF:
Ben Murdoch2694e232009-09-29 09:41:11 +01002237 {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002238 String url = (String) msg.getData().get("url");
Ben Murdoch90d088c2009-11-17 18:14:04 +00002239 String title = (String) msg.getData().get("title");
The Android Open Source Project0c908882009-03-03 19:32:16 -08002240 if (url == null || url.length() == 0) {
2241 break;
2242 }
2243 HashMap focusNodeMap = (HashMap) msg.obj;
2244 WebView view = (WebView) focusNodeMap.get("webview");
2245 // Only apply the action if the top window did not change.
2246 if (getTopWindow() != view) {
2247 break;
2248 }
2249 switch (msg.arg1) {
2250 case R.id.open_context_menu_id:
2251 case R.id.view_image_context_menu_id:
2252 loadURL(getTopWindow(), url);
2253 break;
2254 case R.id.open_newtab_context_menu_id:
Grace Kloba22ac16e2009-10-07 18:00:23 -07002255 final Tab parent = mTabControl.getCurrentTab();
2256 final Tab newTab = openTab(url);
Grace Klobac9181842009-04-14 08:53:22 -07002257 if (newTab != parent) {
2258 parent.addChildTab(newTab);
2259 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002260 break;
2261 case R.id.bookmark_context_menu_id:
2262 Intent intent = new Intent(BrowserActivity.this,
2263 AddBookmarkPage.class);
2264 intent.putExtra("url", url);
Ben Murdoch90d088c2009-11-17 18:14:04 +00002265 intent.putExtra("title", title);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002266 startActivity(intent);
2267 break;
2268 case R.id.share_link_context_menu_id:
Andrei Popescu10fdba82009-09-24 13:25:47 +01002269 Browser.sendString(BrowserActivity.this, url,
2270 getText(R.string.choosertitle_sharevia).toString());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002271 break;
2272 case R.id.copy_link_context_menu_id:
2273 copy(url);
2274 break;
2275 case R.id.save_link_context_menu_id:
2276 case R.id.download_context_menu_id:
2277 onDownloadStartNoStream(url, null, null, null, -1);
2278 break;
2279 }
2280 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002281 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002282
2283 case LOAD_URL:
2284 loadURL(getTopWindow(), (String) msg.obj);
2285 break;
2286
2287 case STOP_LOAD:
2288 stopLoading();
2289 break;
2290
2291 case CANCEL_CREDS_REQUEST:
2292 resumeAfterCredentials();
2293 break;
2294
The Android Open Source Project0c908882009-03-03 19:32:16 -08002295 case RELEASE_WAKELOCK:
2296 if (mWakeLock.isHeld()) {
2297 mWakeLock.release();
Grace Kloba5d0e02e2009-10-05 15:15:36 -07002298 // if we reach here, Browser should be still in the
2299 // background loading after WAKELOCK_TIMEOUT (5-min).
2300 // To avoid burning the battery, stop loading.
2301 mTabControl.stopAllLoading();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002302 }
2303 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002304
2305 case UPDATE_BOOKMARK_THUMBNAIL:
2306 WebView view = (WebView) msg.obj;
2307 if (view != null) {
2308 updateScreenshot(view);
2309 }
2310 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002311 }
2312 }
2313 };
2314
Leon Scroggins89c6d362009-07-15 16:54:37 -04002315 private void updateScreenshot(WebView view) {
2316 // If this is a bookmarked site, add a screenshot to the database.
2317 // FIXME: When should we update? Every time?
2318 // FIXME: Would like to make sure there is actually something to
2319 // draw, but the API for that (WebViewCore.pictureReady()) is not
2320 // currently accessible here.
Ben Murdochaac7aa62009-09-17 16:57:40 +01002321
Patrick Scott3918d442009-08-04 13:22:29 -04002322 ContentResolver cr = getContentResolver();
2323 final Cursor c = BrowserBookmarksAdapter.queryBookmarksForUrl(
Ben Murdochaac7aa62009-09-17 16:57:40 +01002324 cr, view.getOriginalUrl(), view.getUrl(), true);
Patrick Scott3918d442009-08-04 13:22:29 -04002325 if (c != null) {
Leon Scroggins89c6d362009-07-15 16:54:37 -04002326 boolean succeed = c.moveToFirst();
2327 ContentValues values = null;
2328 while (succeed) {
2329 if (values == null) {
2330 final ByteArrayOutputStream os
2331 = new ByteArrayOutputStream();
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002332 Bitmap bm = createScreenshot(view);
Leon Scroggins45800572009-09-29 16:38:47 -04002333 if (bm == null) {
2334 c.close();
2335 return;
2336 }
Leon Scroggins89c6d362009-07-15 16:54:37 -04002337 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
2338 values = new ContentValues();
2339 values.put(Browser.BookmarkColumns.THUMBNAIL,
2340 os.toByteArray());
2341 }
2342 cr.update(ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
2343 c.getInt(0)), values, null, null);
2344 succeed = c.moveToNext();
2345 }
2346 c.close();
2347 }
2348 }
2349
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002350 /**
Leon Scrogginsf8551612009-09-24 16:06:02 -04002351 * Values for the size of the thumbnail created when taking a screenshot.
2352 * Lazily initialized. Instead of using these directly, use
2353 * getDesiredThumbnailWidth() or getDesiredThumbnailHeight().
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002354 */
Leon Scrogginsf8551612009-09-24 16:06:02 -04002355 private static int THUMBNAIL_WIDTH = 0;
2356 private static int THUMBNAIL_HEIGHT = 0;
2357
2358 /**
2359 * Return the desired width for thumbnail screenshots, which are stored in
2360 * the database, and used on the bookmarks screen.
2361 * @param context Context for finding out the density of the screen.
2362 * @return int desired width for thumbnail screenshot.
2363 */
2364 /* package */ static int getDesiredThumbnailWidth(Context context) {
2365 if (THUMBNAIL_WIDTH == 0) {
2366 float density = context.getResources().getDisplayMetrics().density;
2367 THUMBNAIL_WIDTH = (int) (90 * density);
2368 THUMBNAIL_HEIGHT = (int) (80 * density);
2369 }
2370 return THUMBNAIL_WIDTH;
2371 }
2372
2373 /**
2374 * Return the desired height for thumbnail screenshots, which are stored in
2375 * the database, and used on the bookmarks screen.
2376 * @param context Context for finding out the density of the screen.
2377 * @return int desired height for thumbnail screenshot.
2378 */
2379 /* package */ static int getDesiredThumbnailHeight(Context context) {
2380 // To ensure that they are both initialized.
2381 getDesiredThumbnailWidth(context);
2382 return THUMBNAIL_HEIGHT;
2383 }
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002384
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002385 private Bitmap createScreenshot(WebView view) {
2386 Picture thumbnail = view.capturePicture();
Leon Scroggins45800572009-09-29 16:38:47 -04002387 if (thumbnail == null) {
2388 return null;
2389 }
Leon Scrogginsf8551612009-09-24 16:06:02 -04002390 Bitmap bm = Bitmap.createBitmap(getDesiredThumbnailWidth(this),
2391 getDesiredThumbnailHeight(this), Bitmap.Config.ARGB_4444);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002392 Canvas canvas = new Canvas(bm);
2393 // May need to tweak these values to determine what is the
2394 // best scale factor
Ben Murdoch2694e232009-09-29 09:41:11 +01002395 int thumbnailWidth = thumbnail.getWidth();
Ben Murdochae59c3f2009-10-20 18:30:28 +01002396 int thumbnailHeight = thumbnail.getHeight();
2397 float scaleFactorX = 1.0f;
2398 float scaleFactorY = 1.0f;
Ben Murdoch2694e232009-09-29 09:41:11 +01002399 if (thumbnailWidth > 0) {
Ben Murdochae59c3f2009-10-20 18:30:28 +01002400 scaleFactorX = (float) getDesiredThumbnailWidth(this) /
Ben Murdoch2694e232009-09-29 09:41:11 +01002401 (float)thumbnailWidth;
Ben Murdochae59c3f2009-10-20 18:30:28 +01002402 } else {
2403 return null;
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002404 }
Ben Murdochae59c3f2009-10-20 18:30:28 +01002405
2406 if (view.getWidth() > view.getHeight() &&
2407 thumbnailHeight < view.getHeight() && thumbnailHeight > 0) {
2408 // If the device is in landscape and the page is shorter
2409 // than the height of the view, stretch the thumbnail to fill the
2410 // space.
2411 scaleFactorY = (float) getDesiredThumbnailHeight(this) /
2412 (float)thumbnailHeight;
2413 } else {
2414 // In the portrait case, this looks nice.
2415 scaleFactorY = scaleFactorX;
2416 }
2417
2418 canvas.scale(scaleFactorX, scaleFactorY);
2419
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002420 thumbnail.draw(canvas);
2421 return bm;
2422 }
2423
The Android Open Source Project0c908882009-03-03 19:32:16 -08002424 // -------------------------------------------------------------------------
Grace Kloba22ac16e2009-10-07 18:00:23 -07002425 // Helper function for WebViewClient.
The Android Open Source Project0c908882009-03-03 19:32:16 -08002426 //-------------------------------------------------------------------------
2427
2428 // Use in overrideUrlLoading
2429 /* package */ final static String SCHEME_WTAI = "wtai://wp/";
2430 /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
2431 /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;";
2432 /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;";
2433
Grace Kloba22ac16e2009-10-07 18:00:23 -07002434 void onPageStarted(WebView view, String url, Bitmap favicon) {
2435 // when BrowserActivity just starts, onPageStarted may be called before
2436 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
2437 // to start the timer. As we won't switch tabs while an activity is in
2438 // pause state, we can ensure calling resume and pause in pair.
2439 if (mActivityInPause) resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002440
Grace Kloba22ac16e2009-10-07 18:00:23 -07002441 resetLockIcon(url);
2442 setUrlTitle(url, null);
2443 setFavicon(favicon);
Leon Scroggins8cf8f682009-11-04 11:13:50 -08002444 // Keep this initial progress in sync with initialProgressValue (* 100)
2445 // in ProgressTracker.cpp
2446 // Show some progress so that the user knows the page is beginning to
2447 // load
2448 onProgressChanged(view, 10);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002449 mDidStopLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07002450 if (!mIsNetworkUp) createAndShowNetworkDialog();
Patrick Scott15525d42009-09-21 13:39:37 -04002451
Grace Kloba22ac16e2009-10-07 18:00:23 -07002452 if (mSettings.isTracing()) {
2453 String host;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002454 try {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002455 WebAddress uri = new WebAddress(url);
2456 host = uri.mHost;
2457 } catch (android.net.ParseException ex) {
2458 host = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -08002459 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002460 host = host.replace('.', '_');
2461 host += ".trace";
2462 mInTrace = true;
2463 Debug.startMethodTracing(host, 20 * 1024 * 1024);
2464 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002465
Grace Kloba22ac16e2009-10-07 18:00:23 -07002466 // Performance probe
2467 if (false) {
2468 mStart = SystemClock.uptimeMillis();
2469 mProcessStart = Process.getElapsedCpuTime();
2470 long[] sysCpu = new long[7];
2471 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2472 sysCpu, null)) {
2473 mUserStart = sysCpu[0] + sysCpu[1];
2474 mSystemStart = sysCpu[2];
2475 mIdleStart = sysCpu[3];
2476 mIrqStart = sysCpu[4] + sysCpu[5] + sysCpu[6];
2477 }
2478 mUiStart = SystemClock.currentThreadTimeMillis();
2479 }
2480 }
2481
2482 void onPageFinished(WebView view, String url) {
2483 // Reset the title and icon in case we stopped a provisional load.
2484 resetTitleAndIcon(view);
2485 // Update the lock icon image only once we are done loading
2486 updateLockIconToLatest();
2487 // pause the WebView timer and release the wake lock if it is finished
2488 // while BrowserActivity is in pause state.
2489 if (mActivityInPause && pauseWebViewTimers()) {
2490 if (mWakeLock.isHeld()) {
2491 mHandler.removeMessages(RELEASE_WAKELOCK);
2492 mWakeLock.release();
2493 }
2494 }
2495
2496 // Performance probe
2497 if (false) {
2498 long[] sysCpu = new long[7];
2499 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2500 sysCpu, null)) {
2501 String uiInfo = "UI thread used "
2502 + (SystemClock.currentThreadTimeMillis() - mUiStart)
2503 + " ms";
2504 if (LOGD_ENABLED) {
2505 Log.d(LOGTAG, uiInfo);
2506 }
2507 //The string that gets written to the log
2508 String performanceString = "It took total "
2509 + (SystemClock.uptimeMillis() - mStart)
2510 + " ms clock time to load the page."
2511 + "\nbrowser process used "
2512 + (Process.getElapsedCpuTime() - mProcessStart)
2513 + " ms, user processes used "
2514 + (sysCpu[0] + sysCpu[1] - mUserStart) * 10
2515 + " ms, kernel used "
2516 + (sysCpu[2] - mSystemStart) * 10
2517 + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10
2518 + " ms and irq took "
2519 + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart)
2520 * 10 + " ms, " + uiInfo;
2521 if (LOGD_ENABLED) {
2522 Log.d(LOGTAG, performanceString + "\nWebpage: " + url);
2523 }
2524 if (url != null) {
2525 // strip the url to maintain consistency
2526 String newUrl = new String(url);
2527 if (newUrl.startsWith("http://www.")) {
2528 newUrl = newUrl.substring(11);
2529 } else if (newUrl.startsWith("http://")) {
2530 newUrl = newUrl.substring(7);
2531 } else if (newUrl.startsWith("https://www.")) {
2532 newUrl = newUrl.substring(12);
2533 } else if (newUrl.startsWith("https://")) {
2534 newUrl = newUrl.substring(8);
2535 }
2536 if (LOGD_ENABLED) {
2537 Log.d(LOGTAG, newUrl + " loaded");
2538 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002539 }
2540 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002541 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002542
Grace Kloba22ac16e2009-10-07 18:00:23 -07002543 if (mInTrace) {
2544 mInTrace = false;
2545 Debug.stopMethodTracing();
2546 }
2547 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002548
Grace Kloba22ac16e2009-10-07 18:00:23 -07002549 boolean shouldOverrideUrlLoading(WebView view, String url) {
2550 if (url.startsWith(SCHEME_WTAI)) {
2551 // wtai://wp/mc;number
2552 // number=string(phone-number)
2553 if (url.startsWith(SCHEME_WTAI_MC)) {
2554 Intent intent = new Intent(Intent.ACTION_VIEW,
2555 Uri.parse(WebView.SCHEME_TEL +
2556 url.substring(SCHEME_WTAI_MC.length())));
2557 startActivity(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002558 return true;
2559 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002560 // wtai://wp/sd;dtmf
2561 // dtmf=string(dialstring)
2562 if (url.startsWith(SCHEME_WTAI_SD)) {
2563 // TODO: only send when there is active voice connection
2564 return false;
2565 }
2566 // wtai://wp/ap;number;name
2567 // number=string(phone-number)
2568 // name=string
2569 if (url.startsWith(SCHEME_WTAI_AP)) {
2570 // TODO
2571 return false;
2572 }
2573 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002574
Grace Kloba22ac16e2009-10-07 18:00:23 -07002575 // The "about:" schemes are internal to the browser; don't want these to
2576 // be dispatched to other apps.
2577 if (url.startsWith("about:")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002578 return false;
2579 }
2580
Grace Kloba22ac16e2009-10-07 18:00:23 -07002581 Intent intent;
2582 // perform generic parsing of the URI to turn it into an Intent.
2583 try {
2584 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
2585 } catch (URISyntaxException ex) {
2586 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
2587 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002588 }
2589
Grace Kloba22ac16e2009-10-07 18:00:23 -07002590 // check whether the intent can be resolved. If not, we will see
2591 // whether we can download it from the Market.
2592 if (getPackageManager().resolveActivity(intent, 0) == null) {
2593 String packagename = intent.getPackage();
2594 if (packagename != null) {
2595 intent = new Intent(Intent.ACTION_VIEW, Uri
2596 .parse("market://search?q=pname:" + packagename));
2597 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2598 startActivity(intent);
2599 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002600 } else {
2601 return false;
2602 }
2603 }
2604
Grace Kloba22ac16e2009-10-07 18:00:23 -07002605 // sanitize the Intent, ensuring web pages can not bypass browser
2606 // security (only access to BROWSABLE activities).
2607 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2608 intent.setComponent(null);
2609 try {
2610 if (startActivityIfNeeded(intent, -1)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002611 return true;
2612 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002613 } catch (ActivityNotFoundException ex) {
2614 // ignore the error. If no application can handle the URL,
2615 // eg about:blank, assume the browser can handle it.
2616 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002617
Grace Kloba22ac16e2009-10-07 18:00:23 -07002618 if (mMenuIsDown) {
2619 openTab(url);
2620 closeOptionsMenu();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002621 return true;
2622 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002623 return false;
2624 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002625
Grace Kloba22ac16e2009-10-07 18:00:23 -07002626 // -------------------------------------------------------------------------
2627 // Helper function for WebChromeClient
2628 // -------------------------------------------------------------------------
The Android Open Source Project0c908882009-03-03 19:32:16 -08002629
Grace Kloba22ac16e2009-10-07 18:00:23 -07002630 void onProgressChanged(WebView view, int newProgress) {
2631 mTitleBar.setProgress(newProgress);
2632 mFakeTitleBar.setProgress(newProgress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002633
Grace Kloba22ac16e2009-10-07 18:00:23 -07002634 if (newProgress == 100) {
2635 // onProgressChanged() may continue to be called after the main
2636 // frame has finished loading, as any remaining sub frames continue
2637 // to load. We'll only get called once though with newProgress as
2638 // 100 when everything is loaded. (onPageFinished is called once
2639 // when the main frame completes loading regardless of the state of
2640 // any sub frames so calls to onProgressChanges may continue after
2641 // onPageFinished has executed)
2642 if (mInLoad) {
2643 mInLoad = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -04002644 updateInLoadMenuItems();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002645 // If the options menu is open, leave the title bar
2646 if (!mOptionsMenuOpen || !mIconView) {
2647 hideFakeTitleBar();
The Android Open Source Projectcb9a0bb2009-03-11 12:11:58 -07002648 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002649 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002650 } else if (!mInLoad) {
2651 // onPageFinished may have already been called but a subframe is
2652 // still loading and updating the progress. Reset mInLoad and update
2653 // the menu items.
2654 mInLoad = true;
2655 updateInLoadMenuItems();
2656 if (!mOptionsMenuOpen || mIconView) {
2657 // This page has begun to load, so show the title bar
2658 showFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002659 }
2660 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002661 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002662
Grace Kloba22ac16e2009-10-07 18:00:23 -07002663 void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
2664 if (mCustomView != null)
2665 return;
2666
2667 // Add the custom view to its container.
2668 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
2669 mCustomView = view;
2670 mCustomViewCallback = callback;
2671 // Save the menu state and set it to empty while the custom
2672 // view is showing.
2673 mOldMenuState = mMenuState;
2674 mMenuState = EMPTY_MENU;
2675 // Hide the content view.
2676 mContentView.setVisibility(View.GONE);
2677 // Finally show the custom view container.
Andrei Popescu163ab742009-10-20 17:58:23 +01002678 setStatusBarVisibility(false);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002679 mCustomViewContainer.setVisibility(View.VISIBLE);
2680 mCustomViewContainer.bringToFront();
2681 }
2682
2683 void onHideCustomView() {
2684 if (mCustomView == null)
2685 return;
2686
2687 // Hide the custom view.
2688 mCustomView.setVisibility(View.GONE);
2689 // Remove the custom view from its container.
2690 mCustomViewContainer.removeView(mCustomView);
2691 mCustomView = null;
2692 // Reset the old menu state.
2693 mMenuState = mOldMenuState;
2694 mOldMenuState = EMPTY_MENU;
2695 mCustomViewContainer.setVisibility(View.GONE);
2696 mCustomViewCallback.onCustomViewHidden();
2697 // Show the content view.
Andrei Popescu163ab742009-10-20 17:58:23 +01002698 setStatusBarVisibility(true);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002699 mContentView.setVisibility(View.VISIBLE);
2700 }
2701
2702 Bitmap getDefaultVideoPoster() {
2703 if (mDefaultVideoPoster == null) {
2704 mDefaultVideoPoster = BitmapFactory.decodeResource(
2705 getResources(), R.drawable.default_video_poster);
Patrick Scott3918d442009-08-04 13:22:29 -04002706 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002707 return mDefaultVideoPoster;
2708 }
Patrick Scott3918d442009-08-04 13:22:29 -04002709
Grace Kloba22ac16e2009-10-07 18:00:23 -07002710 View getVideoLoadingProgressView() {
2711 if (mVideoProgressView == null) {
2712 LayoutInflater inflater = LayoutInflater.from(BrowserActivity.this);
2713 mVideoProgressView = inflater.inflate(
2714 R.layout.video_loading_progress, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002715 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002716 return mVideoProgressView;
2717 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002718
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002719 /*
2720 * The Object used to inform the WebView of the file to upload.
2721 */
2722 private ValueCallback<Uri> mUploadMessage;
2723
Grace Kloba22ac16e2009-10-07 18:00:23 -07002724 void openFileChooser(ValueCallback<Uri> uploadMsg) {
2725 if (mUploadMessage != null) return;
2726 mUploadMessage = uploadMsg;
2727 Intent i = new Intent(Intent.ACTION_GET_CONTENT);
2728 i.addCategory(Intent.CATEGORY_OPENABLE);
2729 i.setType("*/*");
2730 BrowserActivity.this.startActivityForResult(Intent.createChooser(i,
2731 getString(R.string.choose_upload)), FILE_SELECTED);
2732 }
2733
2734 // -------------------------------------------------------------------------
2735 // Implement functions for DownloadListener
2736 // -------------------------------------------------------------------------
2737
The Android Open Source Project0c908882009-03-03 19:32:16 -08002738 /**
2739 * Notify the host application a download should be done, or that
2740 * the data should be streamed if a streaming viewer is available.
2741 * @param url The full url to the content that should be downloaded
2742 * @param contentDisposition Content-disposition http header, if
2743 * present.
2744 * @param mimetype The mimetype of the content reported by the server
2745 * @param contentLength The file size reported by the server
2746 */
2747 public void onDownloadStart(String url, String userAgent,
2748 String contentDisposition, String mimetype, long contentLength) {
2749 // if we're dealing wih A/V content that's not explicitly marked
2750 // for download, check if it's streamable.
2751 if (contentDisposition == null
Patrick Scotte1fb9662009-08-31 14:31:52 -04002752 || !contentDisposition.regionMatches(
2753 true, 0, "attachment", 0, 10)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002754 // query the package manager to see if there's a registered handler
2755 // that matches.
2756 Intent intent = new Intent(Intent.ACTION_VIEW);
2757 intent.setDataAndType(Uri.parse(url), mimetype);
Patrick Scotte1fb9662009-08-31 14:31:52 -04002758 ResolveInfo info = getPackageManager().resolveActivity(intent,
2759 PackageManager.MATCH_DEFAULT_ONLY);
2760 if (info != null) {
2761 ComponentName myName = getComponentName();
2762 // If we resolved to ourselves, we don't want to attempt to
2763 // load the url only to try and download it again.
2764 if (!myName.getPackageName().equals(
2765 info.activityInfo.packageName)
2766 || !myName.getClassName().equals(
2767 info.activityInfo.name)) {
2768 // someone (other than us) knows how to handle this mime
2769 // type with this scheme, don't download.
2770 try {
2771 startActivity(intent);
2772 return;
2773 } catch (ActivityNotFoundException ex) {
2774 if (LOGD_ENABLED) {
2775 Log.d(LOGTAG, "activity not found for " + mimetype
2776 + " over " + Uri.parse(url).getScheme(),
2777 ex);
2778 }
2779 // Best behavior is to fall back to a download in this
2780 // case
The Android Open Source Project0c908882009-03-03 19:32:16 -08002781 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002782 }
2783 }
2784 }
2785 onDownloadStartNoStream(url, userAgent, contentDisposition, mimetype, contentLength);
2786 }
2787
2788 /**
2789 * Notify the host application a download should be done, even if there
2790 * is a streaming viewer available for thise type.
2791 * @param url The full url to the content that should be downloaded
2792 * @param contentDisposition Content-disposition http header, if
2793 * present.
2794 * @param mimetype The mimetype of the content reported by the server
2795 * @param contentLength The file size reported by the server
2796 */
2797 /*package */ void onDownloadStartNoStream(String url, String userAgent,
2798 String contentDisposition, String mimetype, long contentLength) {
2799
2800 String filename = URLUtil.guessFileName(url,
2801 contentDisposition, mimetype);
2802
2803 // Check to see if we have an SDCard
2804 String status = Environment.getExternalStorageState();
2805 if (!status.equals(Environment.MEDIA_MOUNTED)) {
2806 int title;
2807 String msg;
2808
2809 // Check to see if the SDCard is busy, same as the music app
2810 if (status.equals(Environment.MEDIA_SHARED)) {
2811 msg = getString(R.string.download_sdcard_busy_dlg_msg);
2812 title = R.string.download_sdcard_busy_dlg_title;
2813 } else {
2814 msg = getString(R.string.download_no_sdcard_dlg_msg, filename);
2815 title = R.string.download_no_sdcard_dlg_title;
2816 }
2817
2818 new AlertDialog.Builder(this)
2819 .setTitle(title)
2820 .setIcon(android.R.drawable.ic_dialog_alert)
2821 .setMessage(msg)
2822 .setPositiveButton(R.string.ok, null)
2823 .show();
2824 return;
2825 }
2826
2827 // java.net.URI is a lot stricter than KURL so we have to undo
2828 // KURL's percent-encoding and redo the encoding using java.net.URI.
2829 URI uri = null;
2830 try {
2831 // Undo the percent-encoding that KURL may have done.
2832 String newUrl = new String(URLUtil.decode(url.getBytes()));
2833 // Parse the url into pieces
2834 WebAddress w = new WebAddress(newUrl);
2835 String frag = null;
2836 String query = null;
2837 String path = w.mPath;
2838 // Break the path into path, query, and fragment
2839 if (path.length() > 0) {
2840 // Strip the fragment
2841 int idx = path.lastIndexOf('#');
2842 if (idx != -1) {
2843 frag = path.substring(idx + 1);
2844 path = path.substring(0, idx);
2845 }
2846 idx = path.lastIndexOf('?');
2847 if (idx != -1) {
2848 query = path.substring(idx + 1);
2849 path = path.substring(0, idx);
2850 }
2851 }
2852 uri = new URI(w.mScheme, w.mAuthInfo, w.mHost, w.mPort, path,
2853 query, frag);
2854 } catch (Exception e) {
2855 Log.e(LOGTAG, "Could not parse url for download: " + url, e);
2856 return;
2857 }
2858
2859 // XXX: Have to use the old url since the cookies were stored using the
2860 // old percent-encoded url.
2861 String cookies = CookieManager.getInstance().getCookie(url);
2862
2863 ContentValues values = new ContentValues();
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -07002864 values.put(Downloads.COLUMN_URI, uri.toString());
2865 values.put(Downloads.COLUMN_COOKIE_DATA, cookies);
2866 values.put(Downloads.COLUMN_USER_AGENT, userAgent);
2867 values.put(Downloads.COLUMN_NOTIFICATION_PACKAGE,
The Android Open Source Project0c908882009-03-03 19:32:16 -08002868 getPackageName());
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -07002869 values.put(Downloads.COLUMN_NOTIFICATION_CLASS,
The Android Open Source Project0c908882009-03-03 19:32:16 -08002870 BrowserDownloadPage.class.getCanonicalName());
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -07002871 values.put(Downloads.COLUMN_VISIBILITY, Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
2872 values.put(Downloads.COLUMN_MIME_TYPE, mimetype);
2873 values.put(Downloads.COLUMN_FILE_NAME_HINT, filename);
2874 values.put(Downloads.COLUMN_DESCRIPTION, uri.getHost());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002875 if (contentLength > 0) {
Jean-Baptiste Queru3dc09b22009-03-31 16:49:44 -07002876 values.put(Downloads.COLUMN_TOTAL_BYTES, contentLength);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002877 }
2878 if (mimetype == null) {
2879 // We must have long pressed on a link or image to download it. We
2880 // are not sure of the mimetype in this case, so do a head request
2881 new FetchUrlMimeType(this).execute(values);
2882 } else {
2883 final Uri contentUri =
2884 getContentResolver().insert(Downloads.CONTENT_URI, values);
2885 viewDownloads(contentUri);
2886 }
2887
2888 }
2889
Grace Kloba22ac16e2009-10-07 18:00:23 -07002890 // -------------------------------------------------------------------------
2891
The Android Open Source Project0c908882009-03-03 19:32:16 -08002892 /**
2893 * Resets the lock icon. This method is called when we start a new load and
2894 * know the url to be loaded.
2895 */
2896 private void resetLockIcon(String url) {
2897 // Save the lock-icon state (we revert to it if the load gets cancelled)
Grace Kloba22ac16e2009-10-07 18:00:23 -07002898 mTabControl.getCurrentTab().resetLockIcon(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002899 updateLockIconImage(LOCK_ICON_UNSECURE);
2900 }
2901
The Android Open Source Project0c908882009-03-03 19:32:16 -08002902 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04002903 * Update the lock icon to correspond to our latest state.
2904 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002905 private void updateLockIconToLatest() {
2906 updateLockIconImage(mTabControl.getCurrentTab().getLockIconType());
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04002907 }
2908
2909 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08002910 * Updates the lock-icon image in the title-bar.
2911 */
2912 private void updateLockIconImage(int lockIconType) {
2913 Drawable d = null;
2914 if (lockIconType == LOCK_ICON_SECURE) {
2915 d = mSecLockIcon;
2916 } else if (lockIconType == LOCK_ICON_MIXED) {
2917 d = mMixLockIcon;
2918 }
Leon Scroggins68579392009-09-15 15:31:54 -04002919 mTitleBar.setLock(d);
Leon Scrogginsfe87bd32009-10-06 10:10:00 -04002920 mFakeTitleBar.setLock(d);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002921 }
2922
2923 /**
2924 * Displays a page-info dialog.
2925 * @param tab The tab to show info about
2926 * @param fromShowSSLCertificateOnError The flag that indicates whether
2927 * this dialog was opened from the SSL-certificate-on-error dialog or
2928 * not. This is important, since we need to know whether to return to
2929 * the parent dialog or simply dismiss.
2930 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002931 private void showPageInfo(final Tab tab,
The Android Open Source Project0c908882009-03-03 19:32:16 -08002932 final boolean fromShowSSLCertificateOnError) {
2933 final LayoutInflater factory = LayoutInflater
2934 .from(this);
2935
2936 final View pageInfoView = factory.inflate(R.layout.page_info, null);
2937
2938 final WebView view = tab.getWebView();
2939
2940 String url = null;
2941 String title = null;
2942
2943 if (view == null) {
2944 url = tab.getUrl();
2945 title = tab.getTitle();
2946 } else if (view == mTabControl.getCurrentWebView()) {
2947 // Use the cached title and url if this is the current WebView
2948 url = mUrl;
2949 title = mTitle;
2950 } else {
2951 url = view.getUrl();
2952 title = view.getTitle();
2953 }
2954
2955 if (url == null) {
2956 url = "";
2957 }
2958 if (title == null) {
2959 title = "";
2960 }
2961
2962 ((TextView) pageInfoView.findViewById(R.id.address)).setText(url);
2963 ((TextView) pageInfoView.findViewById(R.id.title)).setText(title);
2964
2965 mPageInfoView = tab;
2966 mPageInfoFromShowSSLCertificateOnError = new Boolean(fromShowSSLCertificateOnError);
2967
2968 AlertDialog.Builder alertDialogBuilder =
2969 new AlertDialog.Builder(this)
2970 .setTitle(R.string.page_info).setIcon(android.R.drawable.ic_dialog_info)
2971 .setView(pageInfoView)
2972 .setPositiveButton(
2973 R.string.ok,
2974 new DialogInterface.OnClickListener() {
2975 public void onClick(DialogInterface dialog,
2976 int whichButton) {
2977 mPageInfoDialog = null;
2978 mPageInfoView = null;
2979 mPageInfoFromShowSSLCertificateOnError = null;
2980
2981 // if we came here from the SSL error dialog
2982 if (fromShowSSLCertificateOnError) {
2983 // go back to the SSL error dialog
2984 showSSLCertificateOnError(
2985 mSSLCertificateOnErrorView,
2986 mSSLCertificateOnErrorHandler,
2987 mSSLCertificateOnErrorError);
2988 }
2989 }
2990 })
2991 .setOnCancelListener(
2992 new DialogInterface.OnCancelListener() {
2993 public void onCancel(DialogInterface dialog) {
2994 mPageInfoDialog = null;
2995 mPageInfoView = null;
2996 mPageInfoFromShowSSLCertificateOnError = null;
2997
2998 // if we came here from the SSL error dialog
2999 if (fromShowSSLCertificateOnError) {
3000 // go back to the SSL error dialog
3001 showSSLCertificateOnError(
3002 mSSLCertificateOnErrorView,
3003 mSSLCertificateOnErrorHandler,
3004 mSSLCertificateOnErrorError);
3005 }
3006 }
3007 });
3008
3009 // if we have a main top-level page SSL certificate set or a certificate
3010 // error
3011 if (fromShowSSLCertificateOnError ||
3012 (view != null && view.getCertificate() != null)) {
3013 // add a 'View Certificate' button
3014 alertDialogBuilder.setNeutralButton(
3015 R.string.view_certificate,
3016 new DialogInterface.OnClickListener() {
3017 public void onClick(DialogInterface dialog,
3018 int whichButton) {
3019 mPageInfoDialog = null;
3020 mPageInfoView = null;
3021 mPageInfoFromShowSSLCertificateOnError = null;
3022
3023 // if we came here from the SSL error dialog
3024 if (fromShowSSLCertificateOnError) {
3025 // go back to the SSL error dialog
3026 showSSLCertificateOnError(
3027 mSSLCertificateOnErrorView,
3028 mSSLCertificateOnErrorHandler,
3029 mSSLCertificateOnErrorError);
3030 } else {
3031 // otherwise, display the top-most certificate from
3032 // the chain
3033 if (view.getCertificate() != null) {
3034 showSSLCertificate(tab);
3035 }
3036 }
3037 }
3038 });
3039 }
3040
3041 mPageInfoDialog = alertDialogBuilder.show();
3042 }
3043
3044 /**
3045 * Displays the main top-level page SSL certificate dialog
3046 * (accessible from the Page-Info dialog).
3047 * @param tab The tab to show certificate for.
3048 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003049 private void showSSLCertificate(final Tab tab) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003050 final View certificateView =
3051 inflateCertificateView(tab.getWebView().getCertificate());
3052 if (certificateView == null) {
3053 return;
3054 }
3055
3056 LayoutInflater factory = LayoutInflater.from(this);
3057
3058 final LinearLayout placeholder =
3059 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3060
3061 LinearLayout ll = (LinearLayout) factory.inflate(
3062 R.layout.ssl_success, placeholder);
3063 ((TextView)ll.findViewById(R.id.success))
3064 .setText(R.string.ssl_certificate_is_valid);
3065
3066 mSSLCertificateView = tab;
3067 mSSLCertificateDialog =
3068 new AlertDialog.Builder(this)
3069 .setTitle(R.string.ssl_certificate).setIcon(
3070 R.drawable.ic_dialog_browser_certificate_secure)
3071 .setView(certificateView)
3072 .setPositiveButton(R.string.ok,
3073 new DialogInterface.OnClickListener() {
3074 public void onClick(DialogInterface dialog,
3075 int whichButton) {
3076 mSSLCertificateDialog = null;
3077 mSSLCertificateView = null;
3078
3079 showPageInfo(tab, false);
3080 }
3081 })
3082 .setOnCancelListener(
3083 new DialogInterface.OnCancelListener() {
3084 public void onCancel(DialogInterface dialog) {
3085 mSSLCertificateDialog = null;
3086 mSSLCertificateView = null;
3087
3088 showPageInfo(tab, false);
3089 }
3090 })
3091 .show();
3092 }
3093
3094 /**
3095 * Displays the SSL error certificate dialog.
3096 * @param view The target web-view.
3097 * @param handler The SSL error handler responsible for cancelling the
3098 * connection that resulted in an SSL error or proceeding per user request.
3099 * @param error The SSL error object.
3100 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003101 void showSSLCertificateOnError(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003102 final WebView view, final SslErrorHandler handler, final SslError error) {
3103
3104 final View certificateView =
3105 inflateCertificateView(error.getCertificate());
3106 if (certificateView == null) {
3107 return;
3108 }
3109
3110 LayoutInflater factory = LayoutInflater.from(this);
3111
3112 final LinearLayout placeholder =
3113 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3114
3115 if (error.hasError(SslError.SSL_UNTRUSTED)) {
3116 LinearLayout ll = (LinearLayout)factory
3117 .inflate(R.layout.ssl_warning, placeholder);
3118 ((TextView)ll.findViewById(R.id.warning))
3119 .setText(R.string.ssl_untrusted);
3120 }
3121
3122 if (error.hasError(SslError.SSL_IDMISMATCH)) {
3123 LinearLayout ll = (LinearLayout)factory
3124 .inflate(R.layout.ssl_warning, placeholder);
3125 ((TextView)ll.findViewById(R.id.warning))
3126 .setText(R.string.ssl_mismatch);
3127 }
3128
3129 if (error.hasError(SslError.SSL_EXPIRED)) {
3130 LinearLayout ll = (LinearLayout)factory
3131 .inflate(R.layout.ssl_warning, placeholder);
3132 ((TextView)ll.findViewById(R.id.warning))
3133 .setText(R.string.ssl_expired);
3134 }
3135
3136 if (error.hasError(SslError.SSL_NOTYETVALID)) {
3137 LinearLayout ll = (LinearLayout)factory
3138 .inflate(R.layout.ssl_warning, placeholder);
3139 ((TextView)ll.findViewById(R.id.warning))
3140 .setText(R.string.ssl_not_yet_valid);
3141 }
3142
3143 mSSLCertificateOnErrorHandler = handler;
3144 mSSLCertificateOnErrorView = view;
3145 mSSLCertificateOnErrorError = error;
3146 mSSLCertificateOnErrorDialog =
3147 new AlertDialog.Builder(this)
3148 .setTitle(R.string.ssl_certificate).setIcon(
3149 R.drawable.ic_dialog_browser_certificate_partially_secure)
3150 .setView(certificateView)
3151 .setPositiveButton(R.string.ok,
3152 new DialogInterface.OnClickListener() {
3153 public void onClick(DialogInterface dialog,
3154 int whichButton) {
3155 mSSLCertificateOnErrorDialog = null;
3156 mSSLCertificateOnErrorView = null;
3157 mSSLCertificateOnErrorHandler = null;
3158 mSSLCertificateOnErrorError = null;
3159
Grace Kloba22ac16e2009-10-07 18:00:23 -07003160 view.getWebViewClient().onReceivedSslError(
3161 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003162 }
3163 })
3164 .setNeutralButton(R.string.page_info_view,
3165 new DialogInterface.OnClickListener() {
3166 public void onClick(DialogInterface dialog,
3167 int whichButton) {
3168 mSSLCertificateOnErrorDialog = null;
3169
3170 // do not clear the dialog state: we will
3171 // need to show the dialog again once the
3172 // user is done exploring the page-info details
3173
3174 showPageInfo(mTabControl.getTabFromView(view),
3175 true);
3176 }
3177 })
3178 .setOnCancelListener(
3179 new DialogInterface.OnCancelListener() {
3180 public void onCancel(DialogInterface dialog) {
3181 mSSLCertificateOnErrorDialog = null;
3182 mSSLCertificateOnErrorView = null;
3183 mSSLCertificateOnErrorHandler = null;
3184 mSSLCertificateOnErrorError = null;
3185
Grace Kloba22ac16e2009-10-07 18:00:23 -07003186 view.getWebViewClient().onReceivedSslError(
3187 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003188 }
3189 })
3190 .show();
3191 }
3192
3193 /**
3194 * Inflates the SSL certificate view (helper method).
3195 * @param certificate The SSL certificate.
3196 * @return The resultant certificate view with issued-to, issued-by,
3197 * issued-on, expires-on, and possibly other fields set.
3198 * If the input certificate is null, returns null.
3199 */
3200 private View inflateCertificateView(SslCertificate certificate) {
3201 if (certificate == null) {
3202 return null;
3203 }
3204
3205 LayoutInflater factory = LayoutInflater.from(this);
3206
3207 View certificateView = factory.inflate(
3208 R.layout.ssl_certificate, null);
3209
3210 // issued to:
3211 SslCertificate.DName issuedTo = certificate.getIssuedTo();
3212 if (issuedTo != null) {
3213 ((TextView) certificateView.findViewById(R.id.to_common))
3214 .setText(issuedTo.getCName());
3215 ((TextView) certificateView.findViewById(R.id.to_org))
3216 .setText(issuedTo.getOName());
3217 ((TextView) certificateView.findViewById(R.id.to_org_unit))
3218 .setText(issuedTo.getUName());
3219 }
3220
3221 // issued by:
3222 SslCertificate.DName issuedBy = certificate.getIssuedBy();
3223 if (issuedBy != null) {
3224 ((TextView) certificateView.findViewById(R.id.by_common))
3225 .setText(issuedBy.getCName());
3226 ((TextView) certificateView.findViewById(R.id.by_org))
3227 .setText(issuedBy.getOName());
3228 ((TextView) certificateView.findViewById(R.id.by_org_unit))
3229 .setText(issuedBy.getUName());
3230 }
3231
3232 // issued on:
3233 String issuedOn = reformatCertificateDate(
3234 certificate.getValidNotBefore());
3235 ((TextView) certificateView.findViewById(R.id.issued_on))
3236 .setText(issuedOn);
3237
3238 // expires on:
3239 String expiresOn = reformatCertificateDate(
3240 certificate.getValidNotAfter());
3241 ((TextView) certificateView.findViewById(R.id.expires_on))
3242 .setText(expiresOn);
3243
3244 return certificateView;
3245 }
3246
3247 /**
3248 * Re-formats the certificate date (Date.toString()) string to
3249 * a properly localized date string.
3250 * @return Properly localized version of the certificate date string and
3251 * the original certificate date string if fails to localize.
3252 * If the original string is null, returns an empty string "".
3253 */
3254 private String reformatCertificateDate(String certificateDate) {
3255 String reformattedDate = null;
3256
3257 if (certificateDate != null) {
3258 Date date = null;
3259 try {
3260 date = java.text.DateFormat.getInstance().parse(certificateDate);
3261 } catch (ParseException e) {
3262 date = null;
3263 }
3264
3265 if (date != null) {
3266 reformattedDate =
3267 DateFormat.getDateFormat(this).format(date);
3268 }
3269 }
3270
3271 return reformattedDate != null ? reformattedDate :
3272 (certificateDate != null ? certificateDate : "");
3273 }
3274
3275 /**
3276 * Displays an http-authentication dialog.
3277 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003278 void showHttpAuthentication(final HttpAuthHandler handler,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003279 final String host, final String realm, final String title,
3280 final String name, final String password, int focusId) {
3281 LayoutInflater factory = LayoutInflater.from(this);
3282 final View v = factory
3283 .inflate(R.layout.http_authentication, null);
3284 if (name != null) {
3285 ((EditText) v.findViewById(R.id.username_edit)).setText(name);
3286 }
3287 if (password != null) {
3288 ((EditText) v.findViewById(R.id.password_edit)).setText(password);
3289 }
3290
3291 String titleText = title;
3292 if (titleText == null) {
3293 titleText = getText(R.string.sign_in_to).toString().replace(
3294 "%s1", host).replace("%s2", realm);
3295 }
3296
3297 mHttpAuthHandler = handler;
3298 AlertDialog dialog = new AlertDialog.Builder(this)
3299 .setTitle(titleText)
3300 .setIcon(android.R.drawable.ic_dialog_alert)
3301 .setView(v)
3302 .setPositiveButton(R.string.action,
3303 new DialogInterface.OnClickListener() {
3304 public void onClick(DialogInterface dialog,
3305 int whichButton) {
3306 String nm = ((EditText) v
3307 .findViewById(R.id.username_edit))
3308 .getText().toString();
3309 String pw = ((EditText) v
3310 .findViewById(R.id.password_edit))
3311 .getText().toString();
3312 BrowserActivity.this.setHttpAuthUsernamePassword
3313 (host, realm, nm, pw);
3314 handler.proceed(nm, pw);
3315 mHttpAuthenticationDialog = null;
3316 mHttpAuthHandler = null;
3317 }})
3318 .setNegativeButton(R.string.cancel,
3319 new DialogInterface.OnClickListener() {
3320 public void onClick(DialogInterface dialog,
3321 int whichButton) {
3322 handler.cancel();
3323 BrowserActivity.this.resetTitleAndRevertLockIcon();
3324 mHttpAuthenticationDialog = null;
3325 mHttpAuthHandler = null;
3326 }})
3327 .setOnCancelListener(new DialogInterface.OnCancelListener() {
3328 public void onCancel(DialogInterface dialog) {
3329 handler.cancel();
3330 BrowserActivity.this.resetTitleAndRevertLockIcon();
3331 mHttpAuthenticationDialog = null;
3332 mHttpAuthHandler = null;
3333 }})
3334 .create();
3335 // Make the IME appear when the dialog is displayed if applicable.
3336 dialog.getWindow().setSoftInputMode(
3337 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
3338 dialog.show();
3339 if (focusId != 0) {
3340 dialog.findViewById(focusId).requestFocus();
3341 } else {
3342 v.findViewById(R.id.username_edit).requestFocus();
3343 }
3344 mHttpAuthenticationDialog = dialog;
3345 }
3346
3347 public int getProgress() {
3348 WebView w = mTabControl.getCurrentWebView();
3349 if (w != null) {
3350 return w.getProgress();
3351 } else {
3352 return 100;
3353 }
3354 }
3355
3356 /**
3357 * Set HTTP authentication password.
3358 *
3359 * @param host The host for the password
3360 * @param realm The realm for the password
3361 * @param username The username for the password. If it is null, it means
3362 * password can't be saved.
3363 * @param password The password
3364 */
3365 public void setHttpAuthUsernamePassword(String host, String realm,
3366 String username,
3367 String password) {
3368 WebView w = mTabControl.getCurrentWebView();
3369 if (w != null) {
3370 w.setHttpAuthUsernamePassword(host, realm, username, password);
3371 }
3372 }
3373
3374 /**
3375 * connectivity manager says net has come or gone... inform the user
3376 * @param up true if net has come up, false if net has gone down
3377 */
3378 public void onNetworkToggle(boolean up) {
3379 if (up == mIsNetworkUp) {
3380 return;
3381 } else if (up) {
3382 mIsNetworkUp = true;
3383 if (mAlertDialog != null) {
3384 mAlertDialog.cancel();
3385 mAlertDialog = null;
3386 }
3387 } else {
3388 mIsNetworkUp = false;
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003389 if (mInLoad) {
3390 createAndShowNetworkDialog();
3391 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003392 }
3393 WebView w = mTabControl.getCurrentWebView();
3394 if (w != null) {
3395 w.setNetworkAvailable(up);
3396 }
3397 }
3398
Grace Kloba22ac16e2009-10-07 18:00:23 -07003399 boolean isNetworkUp() {
3400 return mIsNetworkUp;
3401 }
3402
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003403 // This method shows the network dialog alerting the user that the net is
3404 // down. It will only show the dialog if mAlertDialog is null.
3405 private void createAndShowNetworkDialog() {
3406 if (mAlertDialog == null) {
3407 mAlertDialog = new AlertDialog.Builder(this)
3408 .setTitle(R.string.loadSuspendedTitle)
3409 .setMessage(R.string.loadSuspended)
3410 .setPositiveButton(R.string.ok, null)
3411 .show();
3412 }
3413 }
3414
The Android Open Source Project0c908882009-03-03 19:32:16 -08003415 @Override
3416 protected void onActivityResult(int requestCode, int resultCode,
3417 Intent intent) {
Grace Klobabb394f32009-11-19 10:26:37 -08003418 if (getTopWindow() == null) return;
3419
The Android Open Source Project0c908882009-03-03 19:32:16 -08003420 switch (requestCode) {
3421 case COMBO_PAGE:
3422 if (resultCode == RESULT_OK && intent != null) {
3423 String data = intent.getAction();
3424 Bundle extras = intent.getExtras();
3425 if (extras != null && extras.getBoolean("new_window", false)) {
Leon Scroggins25d35472009-09-15 11:37:27 -04003426 openTab(data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003427 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -07003428 final Tab currentTab =
The Android Open Source Project0c908882009-03-03 19:32:16 -08003429 mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04003430 dismissSubWindow(currentTab);
3431 if (data != null && data.length() != 0) {
3432 getTopWindow().loadUrl(data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003433 }
3434 }
3435 }
3436 break;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003437 // Choose a file from the file picker.
3438 case FILE_SELECTED:
3439 if (null == mUploadMessage) break;
3440 Uri result = intent == null || resultCode != RESULT_OK ? null
3441 : intent.getData();
3442 mUploadMessage.onReceiveValue(result);
3443 mUploadMessage = null;
3444 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003445 default:
3446 break;
3447 }
Leon Scroggins30444232009-09-04 18:36:20 -04003448 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003449 }
3450
3451 /*
3452 * This method is called as a result of the user selecting the options
3453 * menu to see the download window, or when a download changes state. It
3454 * shows the download window ontop of the current window.
3455 */
3456 /* package */ void viewDownloads(Uri downloadRecord) {
3457 Intent intent = new Intent(this,
3458 BrowserDownloadPage.class);
3459 intent.setData(downloadRecord);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003460 startActivityForResult(intent, BrowserActivity.DOWNLOAD_PAGE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003461
3462 }
3463
Leon Scroggins160a7e72009-08-14 18:28:01 -04003464 /**
3465 * Open the Go page.
3466 * @param startWithHistory If true, open starting on the history tab.
3467 * Otherwise, start with the bookmarks tab.
Leon Scroggins160a7e72009-08-14 18:28:01 -04003468 */
Leon Scroggins30444232009-09-04 18:36:20 -04003469 /* package */ void bookmarksOrHistoryPicker(boolean startWithHistory) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003470 WebView current = mTabControl.getCurrentWebView();
3471 if (current == null) {
3472 return;
3473 }
3474 Intent intent = new Intent(this,
3475 CombinedBookmarkHistoryActivity.class);
3476 String title = current.getTitle();
3477 String url = current.getUrl();
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003478 Bitmap thumbnail = createScreenshot(current);
3479
The Android Open Source Project0c908882009-03-03 19:32:16 -08003480 // Just in case the user opens bookmarks before a page finishes loading
3481 // so the current history item, and therefore the page, is null.
3482 if (null == url) {
3483 url = mLastEnteredUrl;
3484 // This can happen.
3485 if (null == url) {
3486 url = mSettings.getHomePage();
3487 }
3488 }
3489 // In case the web page has not yet received its associated title.
3490 if (title == null) {
3491 title = url;
3492 }
3493 intent.putExtra("title", title);
3494 intent.putExtra("url", url);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003495 intent.putExtra("thumbnail", thumbnail);
Leon Scroggins30444232009-09-04 18:36:20 -04003496 // Disable opening in a new window if we have maxed out the windows
Grace Kloba22ac16e2009-10-07 18:00:23 -07003497 intent.putExtra("disable_new_window", !mTabControl.canCreateNewTab());
Patrick Scott3918d442009-08-04 13:22:29 -04003498 intent.putExtra("touch_icon_url", current.getTouchIconUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003499 if (startWithHistory) {
3500 intent.putExtra(CombinedBookmarkHistoryActivity.STARTING_TAB,
3501 CombinedBookmarkHistoryActivity.HISTORY_TAB);
3502 }
3503 startActivityForResult(intent, COMBO_PAGE);
3504 }
3505
3506 // Called when loading from context menu or LOAD_URL message
3507 private void loadURL(WebView view, String url) {
3508 // In case the user enters nothing.
3509 if (url != null && url.length() != 0 && view != null) {
3510 url = smartUrlFilter(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003511 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003512 view.loadUrl(url);
3513 }
3514 }
3515 }
3516
The Android Open Source Project0c908882009-03-03 19:32:16 -08003517 private String smartUrlFilter(Uri inUri) {
3518 if (inUri != null) {
3519 return smartUrlFilter(inUri.toString());
3520 }
3521 return null;
3522 }
3523
Feng Qianb34f87a2009-03-24 21:27:26 -07003524 protected static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003525 "(?i)" + // switch on case insensitive matching
3526 "(" + // begin group for schema
3527 "(?:http|https|file):\\/\\/" +
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003528 "|(?:inline|data|about|content|javascript):" +
The Android Open Source Project0c908882009-03-03 19:32:16 -08003529 ")" +
3530 "(.*)" );
3531
3532 /**
3533 * Attempts to determine whether user input is a URL or search
3534 * terms. Anything with a space is passed to search.
3535 *
3536 * Converts to lowercase any mistakenly uppercased schema (i.e.,
3537 * "Http://" converts to "http://"
3538 *
3539 * @return Original or modified URL
3540 *
3541 */
3542 String smartUrlFilter(String url) {
3543
3544 String inUrl = url.trim();
3545 boolean hasSpace = inUrl.indexOf(' ') != -1;
3546
3547 Matcher matcher = ACCEPTED_URI_SCHEMA.matcher(inUrl);
3548 if (matcher.matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003549 // force scheme to lowercase
3550 String scheme = matcher.group(1);
3551 String lcScheme = scheme.toLowerCase();
3552 if (!lcScheme.equals(scheme)) {
Mitsuru Oshima123ecfb2009-05-18 19:11:14 -07003553 inUrl = lcScheme + matcher.group(2);
3554 }
3555 if (hasSpace) {
3556 inUrl = inUrl.replace(" ", "%20");
The Android Open Source Project0c908882009-03-03 19:32:16 -08003557 }
3558 return inUrl;
3559 }
3560 if (hasSpace) {
Satish Sampath565505b2009-05-29 15:37:27 +01003561 // FIXME: Is this the correct place to add to searches?
3562 // what if someone else calls this function?
3563 int shortcut = parseUrlShortcut(inUrl);
3564 if (shortcut != SHORTCUT_INVALID) {
3565 Browser.addSearchUrl(mResolver, inUrl);
3566 String query = inUrl.substring(2);
3567 switch (shortcut) {
3568 case SHORTCUT_GOOGLE_SEARCH:
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003569 return URLUtil.composeSearchUrl(query, QuickSearch_G, QUERY_PLACE_HOLDER);
Satish Sampath565505b2009-05-29 15:37:27 +01003570 case SHORTCUT_WIKIPEDIA_SEARCH:
3571 return URLUtil.composeSearchUrl(query, QuickSearch_W, QUERY_PLACE_HOLDER);
3572 case SHORTCUT_DICTIONARY_SEARCH:
3573 return URLUtil.composeSearchUrl(query, QuickSearch_D, QUERY_PLACE_HOLDER);
3574 case SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH:
The Android Open Source Project0c908882009-03-03 19:32:16 -08003575 // FIXME: we need location in this case
Satish Sampath565505b2009-05-29 15:37:27 +01003576 return URLUtil.composeSearchUrl(query, QuickSearch_L, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003577 }
3578 }
3579 } else {
Dan Egnor5ee906c2009-11-18 12:11:49 -08003580 if (Patterns.WEB_URL.matcher(inUrl).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003581 return URLUtil.guessUrl(inUrl);
3582 }
3583 }
3584
3585 Browser.addSearchUrl(mResolver, inUrl);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003586 return URLUtil.composeSearchUrl(inUrl, QuickSearch_G, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003587 }
3588
Ben Murdochbff2d602009-07-01 20:19:05 +01003589 /* package */ void setShouldShowErrorConsole(boolean flag) {
3590 if (flag == mShouldShowErrorConsole) {
3591 // Nothing to do.
3592 return;
3593 }
3594
3595 mShouldShowErrorConsole = flag;
3596
Grace Kloba22ac16e2009-10-07 18:00:23 -07003597 ErrorConsoleView errorConsole = mTabControl.getCurrentTab()
3598 .getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01003599
3600 if (flag) {
3601 // Setting the show state of the console will cause it's the layout to be inflated.
3602 if (errorConsole.numberOfErrors() > 0) {
3603 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
3604 } else {
3605 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
3606 }
3607
3608 // Now we can add it to the main view.
3609 mErrorConsoleContainer.addView(errorConsole,
3610 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
3611 ViewGroup.LayoutParams.WRAP_CONTENT));
3612 } else {
3613 mErrorConsoleContainer.removeView(errorConsole);
3614 }
3615
3616 }
3617
Grace Kloba22ac16e2009-10-07 18:00:23 -07003618 boolean shouldShowErrorConsole() {
3619 return mShouldShowErrorConsole;
3620 }
3621
Andrei Popescu163ab742009-10-20 17:58:23 +01003622 private void setStatusBarVisibility(boolean visible) {
3623 int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
3624 getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
3625 }
3626
Grace Klobaeb6eef42009-09-15 17:56:32 -07003627 final static int LOCK_ICON_UNSECURE = 0;
3628 final static int LOCK_ICON_SECURE = 1;
3629 final static int LOCK_ICON_MIXED = 2;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003630
The Android Open Source Project0c908882009-03-03 19:32:16 -08003631 private BrowserSettings mSettings;
3632 private TabControl mTabControl;
3633 private ContentResolver mResolver;
3634 private FrameLayout mContentView;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003635 private View mCustomView;
3636 private FrameLayout mCustomViewContainer;
Andrei Popescuc9b55562009-07-07 10:51:15 +01003637 private WebChromeClient.CustomViewCallback mCustomViewCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003638
3639 // FIXME, temp address onPrepareMenu performance problem. When we move everything out of
3640 // view, we should rewrite this.
3641 private int mCurrentMenuState = 0;
3642 private int mMenuState = R.id.MAIN_MENU;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003643 private int mOldMenuState = EMPTY_MENU;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003644 private static final int EMPTY_MENU = -1;
3645 private Menu mMenu;
3646
3647 private FindDialog mFindDialog;
3648 // Used to prevent chording to result in firing two shortcuts immediately
3649 // one after another. Fixes bug 1211714.
3650 boolean mCanChord;
3651
3652 private boolean mInLoad;
3653 private boolean mIsNetworkUp;
Ben Murdochb7cc8b42009-09-28 10:59:09 +01003654 private boolean mDidStopLoad;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003655
The Android Open Source Project0c908882009-03-03 19:32:16 -08003656 private boolean mActivityInPause = true;
3657
3658 private boolean mMenuIsDown;
3659
The Android Open Source Project0c908882009-03-03 19:32:16 -08003660 private static boolean mInTrace;
3661
3662 // Performance probe
3663 private static final int[] SYSTEM_CPU_FORMAT = new int[] {
3664 Process.PROC_SPACE_TERM | Process.PROC_COMBINE,
3665 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time
3666 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time
3667 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time
3668 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time
3669 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time
3670 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time
3671 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG // 7: softirq time
3672 };
3673
3674 private long mStart;
3675 private long mProcessStart;
3676 private long mUserStart;
3677 private long mSystemStart;
3678 private long mIdleStart;
3679 private long mIrqStart;
3680
3681 private long mUiStart;
3682
3683 private Drawable mMixLockIcon;
3684 private Drawable mSecLockIcon;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003685
3686 /* hold a ref so we can auto-cancel if necessary */
3687 private AlertDialog mAlertDialog;
3688
3689 // Wait for credentials before loading google.com
3690 private ProgressDialog mCredsDlg;
3691
3692 // The up-to-date URL and title (these can be different from those stored
3693 // in WebView, since it takes some time for the information in WebView to
3694 // get updated)
3695 private String mUrl;
3696 private String mTitle;
3697
3698 // As PageInfo has different style for landscape / portrait, we have
3699 // to re-open it when configuration changed
3700 private AlertDialog mPageInfoDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07003701 private Tab mPageInfoView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003702 // If the Page-Info dialog is launched from the SSL-certificate-on-error
3703 // dialog, we should not just dismiss it, but should get back to the
3704 // SSL-certificate-on-error dialog. This flag is used to store this state
3705 private Boolean mPageInfoFromShowSSLCertificateOnError;
3706
3707 // as SSLCertificateOnError has different style for landscape / portrait,
3708 // we have to re-open it when configuration changed
3709 private AlertDialog mSSLCertificateOnErrorDialog;
3710 private WebView mSSLCertificateOnErrorView;
3711 private SslErrorHandler mSSLCertificateOnErrorHandler;
3712 private SslError mSSLCertificateOnErrorError;
3713
3714 // as SSLCertificate has different style for landscape / portrait, we
3715 // have to re-open it when configuration changed
3716 private AlertDialog mSSLCertificateDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07003717 private Tab mSSLCertificateView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003718
3719 // as HttpAuthentication has different style for landscape / portrait, we
3720 // have to re-open it when configuration changed
3721 private AlertDialog mHttpAuthenticationDialog;
3722 private HttpAuthHandler mHttpAuthHandler;
3723
3724 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
3725 new FrameLayout.LayoutParams(
3726 ViewGroup.LayoutParams.FILL_PARENT,
3727 ViewGroup.LayoutParams.FILL_PARENT);
Andrei Popescuadc008d2009-06-26 14:11:30 +01003728 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
3729 new FrameLayout.LayoutParams(
3730 ViewGroup.LayoutParams.FILL_PARENT,
3731 ViewGroup.LayoutParams.FILL_PARENT,
3732 Gravity.CENTER);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003733 // Google search
3734 final static String QuickSearch_G = "http://www.google.com/m?q=%s";
The Android Open Source Project0c908882009-03-03 19:32:16 -08003735 // Wikipedia search
3736 final static String QuickSearch_W = "http://en.wikipedia.org/w/index.php?search=%s&go=Go";
3737 // Dictionary search
3738 final static String QuickSearch_D = "http://dictionary.reference.com/search?q=%s";
3739 // Google Mobile Local search
3740 final static String QuickSearch_L = "http://www.google.com/m/search?site=local&q=%s&near=mountain+view";
3741
3742 final static String QUERY_PLACE_HOLDER = "%s";
3743
3744 // "source" parameter for Google search through search key
3745 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
3746 // "source" parameter for Google search through goto menu
3747 final static String GOOGLE_SEARCH_SOURCE_GOTO = "browser-goto";
3748 // "source" parameter for Google search through simplily type
3749 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
3750 // "source" parameter for Google search suggested by the browser
3751 final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
3752 // "source" parameter for Google search from unknown source
3753 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown";
3754
3755 private final static String LOGTAG = "browser";
3756
The Android Open Source Project0c908882009-03-03 19:32:16 -08003757 private String mLastEnteredUrl;
3758
3759 private PowerManager.WakeLock mWakeLock;
3760 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
3761
3762 private Toast mStopToast;
3763
Leon Scroggins68579392009-09-15 15:31:54 -04003764 private TitleBar mTitleBar;
Leon Scroggins81db3662009-06-04 17:45:11 -04003765
Ben Murdochbff2d602009-07-01 20:19:05 +01003766 private LinearLayout mErrorConsoleContainer = null;
3767 private boolean mShouldShowErrorConsole = false;
3768
The Android Open Source Project0c908882009-03-03 19:32:16 -08003769 // As the ids are dynamically created, we can't guarantee that they will
3770 // be in sequence, so this static array maps ids to a window number.
3771 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
3772 { R.id.window_one_menu_id, R.id.window_two_menu_id, R.id.window_three_menu_id,
3773 R.id.window_four_menu_id, R.id.window_five_menu_id, R.id.window_six_menu_id,
3774 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
3775
3776 // monitor platform changes
3777 private IntentFilter mNetworkStateChangedFilter;
3778 private BroadcastReceiver mNetworkStateIntentReceiver;
3779
Grace Klobab4da0ad2009-05-14 14:45:40 -07003780 private BroadcastReceiver mPackageInstallationReceiver;
3781
The Android Open Source Project0c908882009-03-03 19:32:16 -08003782 // activity requestCode
Nicolas Roard78a98e42009-05-11 13:34:17 +01003783 final static int COMBO_PAGE = 1;
3784 final static int DOWNLOAD_PAGE = 2;
3785 final static int PREFERENCES_PAGE = 3;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003786 final static int FILE_SELECTED = 4;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003787
Andrei Popescu540035d2009-09-18 18:59:20 +01003788 // the default <video> poster
3789 private Bitmap mDefaultVideoPoster;
3790 // the video progress view
3791 private View mVideoProgressView;
3792
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003793 /**
3794 * A UrlData class to abstract how the content will be set to WebView.
3795 * This base class uses loadUrl to show the content.
3796 */
3797 private static class UrlData {
3798 String mUrl;
Grace Kloba60e095c2009-06-16 11:50:55 -07003799 byte[] mPostData;
3800
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003801 UrlData(String url) {
3802 this.mUrl = url;
3803 }
Grace Kloba60e095c2009-06-16 11:50:55 -07003804
3805 void setPostData(byte[] postData) {
3806 mPostData = postData;
3807 }
3808
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003809 boolean isEmpty() {
3810 return mUrl == null || mUrl.length() == 0;
3811 }
3812
Mitsuru Oshima7944b7d2009-06-16 16:34:51 -07003813 public void loadIn(WebView webView) {
Grace Kloba60e095c2009-06-16 11:50:55 -07003814 if (mPostData != null) {
3815 webView.postUrl(mUrl, mPostData);
3816 } else {
3817 webView.loadUrl(mUrl);
3818 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003819 }
3820 };
3821
3822 /**
3823 * A subclass of UrlData class that can display inlined content using
3824 * {@link WebView#loadDataWithBaseURL(String, String, String, String, String)}.
3825 */
3826 private static class InlinedUrlData extends UrlData {
3827 InlinedUrlData(String inlined, String mimeType, String encoding, String failUrl) {
3828 super(failUrl);
3829 mInlined = inlined;
3830 mMimeType = mimeType;
3831 mEncoding = encoding;
3832 }
3833 String mMimeType;
3834 String mInlined;
3835 String mEncoding;
Mitsuru Oshima7944b7d2009-06-16 16:34:51 -07003836 @Override
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003837 boolean isEmpty() {
Ben Murdochbff2d602009-07-01 20:19:05 +01003838 return mInlined == null || mInlined.length() == 0 || super.isEmpty();
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003839 }
3840
Mitsuru Oshima7944b7d2009-06-16 16:34:51 -07003841 @Override
3842 public void loadIn(WebView webView) {
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003843 webView.loadDataWithBaseURL(null, mInlined, mMimeType, mEncoding, mUrl);
3844 }
3845 }
3846
Leon Scroggins1f005d32009-08-10 17:36:42 -04003847 /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003848}