blob: 098e3b2f460ee41ee09d13325c507ce1f05ec123 [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;
Leon Scroggins58d56c62010-01-28 15:12:40 -050026import android.content.ContentProvider;
27import android.content.ContentProviderClient;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.content.ContentResolver;
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -040029import android.content.ContentUris;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.content.ContentValues;
31import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
34import android.content.IntentFilter;
Grace Klobab4da0ad2009-05-14 14:45:40 -070035import android.content.pm.PackageInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.content.pm.PackageManager;
37import android.content.pm.ResolveInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.content.res.Configuration;
39import android.content.res.Resources;
40import android.database.Cursor;
Leon Scroggins96afcb12009-12-10 12:35:56 -050041import android.database.DatabaseUtils;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042import android.graphics.Bitmap;
Andrei Popescu540035d2009-09-18 18:59:20 +010043import android.graphics.BitmapFactory;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import android.graphics.Canvas;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.graphics.Picture;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040046import android.graphics.PixelFormat;
47import android.graphics.Rect;
The Android Open Source Project0c908882009-03-03 19:32:16 -080048import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049import android.net.ConnectivityManager;
Andrei Popescu56199cc2010-01-12 22:39:16 +000050import android.net.NetworkInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051import android.net.Uri;
52import android.net.WebAddress;
The Android Open Source Project0c908882009-03-03 19:32:16 -080053import android.net.http.SslCertificate;
54import android.net.http.SslError;
55import android.os.AsyncTask;
56import android.os.Bundle;
57import android.os.Debug;
58import android.os.Environment;
59import android.os.Handler;
The Android Open Source Project0c908882009-03-03 19:32:16 -080060import android.os.Message;
61import android.os.PowerManager;
62import android.os.Process;
The Android Open Source Project0c908882009-03-03 19:32:16 -080063import 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;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050070import android.speech.RecognizerResultsIntent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080071import android.text.IClipboard;
72import android.text.TextUtils;
73import android.text.format.DateFormat;
Leon Scrogginsb94bf272009-09-25 15:22:08 -040074import android.util.AttributeSet;
The Android Open Source Project0c908882009-03-03 19:32:16 -080075import android.util.Log;
Dianne Hackborn385effd2010-02-24 20:03:04 -080076import android.util.Patterns;
The Android Open Source Project0c908882009-03-03 19:32:16 -080077import android.view.ContextMenu;
78import android.view.Gravity;
79import android.view.KeyEvent;
80import android.view.LayoutInflater;
81import android.view.Menu;
82import android.view.MenuInflater;
83import android.view.MenuItem;
84import android.view.View;
85import android.view.ViewGroup;
86import android.view.Window;
87import android.view.WindowManager;
88import android.view.ContextMenu.ContextMenuInfo;
89import android.view.MenuItem.OnMenuItemClickListener;
The Android Open Source Project0c908882009-03-03 19:32:16 -080090import android.webkit.CookieManager;
91import android.webkit.CookieSyncManager;
92import android.webkit.DownloadListener;
93import android.webkit.HttpAuthHandler;
Grace Klobab4da0ad2009-05-14 14:45:40 -070094import android.webkit.PluginManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080095import android.webkit.SslErrorHandler;
96import android.webkit.URLUtil;
Leon Clarkecb6cc862009-09-29 18:35:13 +010097import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080098import android.webkit.WebChromeClient;
99import android.webkit.WebHistoryItem;
100import android.webkit.WebIconDatabase;
101import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800102import android.widget.EditText;
103import android.widget.FrameLayout;
104import android.widget.LinearLayout;
105import android.widget.TextView;
106import android.widget.Toast;
Fred Quintana752b6562009-12-18 10:18:26 -0800107import android.accounts.Account;
108import android.accounts.AccountManager;
109import android.accounts.AccountManagerFuture;
110import android.accounts.AuthenticatorException;
111import android.accounts.OperationCanceledException;
112import android.accounts.AccountManagerCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800113
Bjorn Bringertd2670652010-09-13 14:06:41 +0100114import com.android.browser.search.SearchEngine;
Bjorn Bringert10d1cca2010-02-10 14:22:12 +0000115import com.android.common.Search;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500116import com.android.common.speech.LoggingEvents;
Dan Egnor5ee906c2009-11-18 12:11:49 -0800117
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400118import java.io.ByteArrayOutputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119import java.io.File;
Ben Murdoch4f75ba22009-10-27 11:48:28 +0000120import java.io.IOException;
121import java.io.InputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800122import java.net.MalformedURLException;
123import java.net.URI;
Dianne Hackborn99189432009-06-17 18:06:18 -0700124import java.net.URISyntaxException;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800125import java.net.URL;
126import java.net.URLEncoder;
127import java.text.ParseException;
128import java.util.Date;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800129import java.util.HashMap;
Andrei Popescu30995e72010-02-09 16:59:58 +0000130import java.util.HashSet;
Grace Kloba00f54c52010-01-27 14:53:51 -0800131import java.util.Iterator;
Andrei Popescu30995e72010-02-09 16:59:58 +0000132import java.util.List;
Grace Kloba068e48b2010-01-26 18:11:27 -0800133import java.util.Map;
Andrei Popescu30995e72010-02-09 16:59:58 +0000134import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800135import java.util.regex.Matcher;
136import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800137
138public class BrowserActivity extends Activity
Shimeng (Simon) Wang98d5fce2010-03-16 13:23:39 -0700139 implements View.OnCreateContextMenuListener, DownloadListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800140
Dave Bort31a6d1c2009-04-13 15:56:49 -0700141 /* Define some aliases to make these debugging flags easier to refer to.
142 * This file imports android.provider.Browser, so we can't just refer to "Browser.DEBUG".
143 */
144 private final static boolean DEBUG = com.android.browser.Browser.DEBUG;
145 private final static boolean LOGV_ENABLED = com.android.browser.Browser.LOGV_ENABLED;
146 private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
147
Satish Sampath565505b2009-05-29 15:37:27 +0100148 // These are single-character shortcuts for searching popular sources.
149 private static final int SHORTCUT_INVALID = 0;
150 private static final int SHORTCUT_GOOGLE_SEARCH = 1;
151 private static final int SHORTCUT_WIKIPEDIA_SEARCH = 2;
152 private static final int SHORTCUT_DICTIONARY_SEARCH = 3;
153 private static final int SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH = 4;
154
Cary Clarka9771242009-08-11 16:42:26 -0400155 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800156 @Override
157 public Void doInBackground(File... files) {
158 if (files != null) {
159 for (File f : files) {
Cary Clarkd6be1752009-08-12 12:56:42 -0400160 if (!f.delete()) {
161 Log.e(LOGTAG, f.getPath() + " was not deleted");
162 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800163 }
164 }
165 return null;
166 }
167 }
168
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400169 /**
170 * This layout holds everything you see below the status bar, including the
171 * error console, the custom view container, and the webviews.
172 */
173 private FrameLayout mBrowserFrameLayout;
Leon Scroggins81db3662009-06-04 17:45:11 -0400174
Grace Kloba22ac16e2009-10-07 18:00:23 -0700175 @Override
176 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700177 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178 Log.v(LOGTAG, this + " onStart");
179 }
180 super.onCreate(icicle);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800181 // test the browser in OpenGL
182 // requestWindowFeature(Window.FEATURE_OPENGL);
183
Mike Reedd334bf52010-01-26 15:21:44 -0500184 // enable this to test the browser in 32bit
185 if (false) {
186 getWindow().setFormat(PixelFormat.RGBX_8888);
187 BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
188 }
189
The Android Open Source Project0c908882009-03-03 19:32:16 -0800190 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
191
192 mResolver = getContentResolver();
193
Grace Kloba0923d692009-09-23 21:37:25 -0700194 // If this was a web search request, pass it on to the default web
195 // search provider and finish this activity.
196 if (handleWebSearchIntent(getIntent())) {
197 finish();
198 return;
199 }
200
The Android Open Source Project0c908882009-03-03 19:32:16 -0800201 mSecLockIcon = Resources.getSystem().getDrawable(
202 android.R.drawable.ic_secure);
203 mMixLockIcon = Resources.getSystem().getDrawable(
204 android.R.drawable.ic_partial_secure);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800205
Leon Scroggins81db3662009-06-04 17:45:11 -0400206 FrameLayout frameLayout = (FrameLayout) getWindow().getDecorView()
207 .findViewById(com.android.internal.R.id.content);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400208 mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(this)
209 .inflate(R.layout.custom_screen, null);
210 mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(
211 R.id.main_content);
212 mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout
213 .findViewById(R.id.error_console);
214 mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
215 .findViewById(R.id.fullscreen_custom_content);
216 frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
Leon Scroggins68579392009-09-15 15:31:54 -0400217 mTitleBar = new TitleBar(this);
Grace Kloba1542f742010-03-17 21:11:15 -0700218 // mTitleBar will be always shown in the fully loaded mode
219 mTitleBar.setProgress(100);
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400220 mFakeTitleBar = new TitleBar(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800221
222 // Create the tab control and our initial tab
223 mTabControl = new TabControl(this);
224
225 // Open the icon database and retain all the bookmark urls for favicons
226 retainIconsOnStartup();
227
228 // Keep a settings instance handy.
229 mSettings = BrowserSettings.getInstance();
230 mSettings.setTabControl(mTabControl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800231
232 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
233 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
234
Patrick Scott6adacc92010-03-05 08:24:51 -0500235 // Find out if the network is currently up.
236 ConnectivityManager cm = (ConnectivityManager) getSystemService(
237 Context.CONNECTIVITY_SERVICE);
238 NetworkInfo info = cm.getActiveNetworkInfo();
239 if (info != null) {
240 mIsNetworkUp = info.isAvailable();
241 }
242
Grace Klobaa34f6862009-07-31 16:28:17 -0700243 /* enables registration for changes in network status from
244 http stack */
245 mNetworkStateChangedFilter = new IntentFilter();
246 mNetworkStateChangedFilter.addAction(
247 ConnectivityManager.CONNECTIVITY_ACTION);
248 mNetworkStateIntentReceiver = new BroadcastReceiver() {
249 @Override
250 public void onReceive(Context context, Intent intent) {
251 if (intent.getAction().equals(
252 ConnectivityManager.CONNECTIVITY_ACTION)) {
Andrei Popescue4c98462010-02-19 15:44:13 +0000253
254 NetworkInfo info = intent.getParcelableExtra(
255 ConnectivityManager.EXTRA_NETWORK_INFO);
256 String typeName = info.getTypeName();
257 String subtypeName = info.getSubtypeName();
258 sendNetworkType(typeName.toLowerCase(),
259 (subtypeName != null ? subtypeName.toLowerCase() : ""));
260
261 onNetworkToggle(info.isAvailable());
Grace Klobaa34f6862009-07-31 16:28:17 -0700262 }
263 }
264 };
265
Grace Kloba615c6c92009-08-03 10:22:44 -0700266 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
267 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
268 filter.addDataScheme("package");
269 mPackageInstallationReceiver = new BroadcastReceiver() {
270 @Override
271 public void onReceive(Context context, Intent intent) {
272 final String action = intent.getAction();
273 final String packageName = intent.getData()
274 .getSchemeSpecificPart();
275 final boolean replacing = intent.getBooleanExtra(
276 Intent.EXTRA_REPLACING, false);
277 if (Intent.ACTION_PACKAGE_REMOVED.equals(action) && replacing) {
278 // if it is replacing, refreshPlugins() when adding
279 return;
280 }
Andrei Popescu30995e72010-02-09 16:59:58 +0000281
282 if (sGoogleApps.contains(packageName)) {
283 BrowserActivity.this.packageChanged(packageName,
284 Intent.ACTION_PACKAGE_ADDED.equals(action));
285 }
286
Grace Kloba615c6c92009-08-03 10:22:44 -0700287 PackageManager pm = BrowserActivity.this.getPackageManager();
288 PackageInfo pkgInfo = null;
289 try {
290 pkgInfo = pm.getPackageInfo(packageName,
291 PackageManager.GET_PERMISSIONS);
292 } catch (PackageManager.NameNotFoundException e) {
293 return;
294 }
295 if (pkgInfo != null) {
296 String permissions[] = pkgInfo.requestedPermissions;
297 if (permissions == null) {
298 return;
299 }
300 boolean permissionOk = false;
301 for (String permit : permissions) {
302 if (PluginManager.PLUGIN_PERMISSION.equals(permit)) {
303 permissionOk = true;
304 break;
305 }
306 }
307 if (permissionOk) {
308 PluginManager.getInstance(BrowserActivity.this)
309 .refreshPlugins(
310 Intent.ACTION_PACKAGE_ADDED
311 .equals(action));
312 }
313 }
314 }
315 };
316 registerReceiver(mPackageInstallationReceiver, filter);
317
The Android Open Source Project0c908882009-03-03 19:32:16 -0800318 if (!mTabControl.restoreState(icicle)) {
319 // clear up the thumbnail directory if we can't restore the state as
320 // none of the files in the directory are referenced any more.
321 new ClearThumbnails().execute(
322 mTabControl.getThumbnailDir().listFiles());
Grace Klobaaab3f092009-07-30 12:29:51 -0700323 // there is no quit on Android. But if we can't restore the state,
324 // we can treat it as a new Browser, remove the old session cookies.
325 CookieManager.getInstance().removeSessionCookie();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800326 final Intent intent = getIntent();
327 final Bundle extra = intent.getExtras();
328 // Create an initial tab.
329 // If the intent is ACTION_VIEW and data is not null, the Browser is
330 // invoked to view the content by another application. In this case,
331 // the tab will be close when exit.
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700332 UrlData urlData = getUrlDataFromIntent(intent);
333
Leon Scroggins58d56c62010-01-28 15:12:40 -0500334 String action = intent.getAction();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700335 final Tab t = mTabControl.createNewTab(
Leon Scroggins58d56c62010-01-28 15:12:40 -0500336 (Intent.ACTION_VIEW.equals(action) &&
337 intent.getData() != null)
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500338 || RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
339 .equals(action),
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700340 intent.getStringExtra(Browser.EXTRA_APPLICATION_ID), urlData.mUrl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800341 mTabControl.setCurrentTab(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800342 attachTabToContentView(t);
343 WebView webView = t.getWebView();
344 if (extra != null) {
345 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
346 if (scale > 0 && scale <= 1000) {
347 webView.setInitialScale(scale);
348 }
349 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800350
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700351 if (urlData.isEmpty()) {
Shimeng (Simon) Wang98d5fce2010-03-16 13:23:39 -0700352 loadUrl(webView, mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800353 } else {
Patrick Scott9d53da02010-02-19 10:19:01 -0500354 loadUrlDataIn(t, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800355 }
356 } else {
357 // TabControl.restoreState() will create a new tab even if
Leon Scroggins1f005d32009-08-10 17:36:42 -0400358 // restoring the state fails.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800359 attachTabToContentView(mTabControl.getCurrentTab());
360 }
Grace Kloba615c6c92009-08-03 10:22:44 -0700361
Cary Clark23cbeb72010-07-01 12:36:56 -0400362 // Delete old thumbnails to save space
363 File dir = mTabControl.getThumbnailDir();
364 if (dir.exists()) {
365 for (String child : dir.list()) {
366 File f = new File(dir, child);
367 f.delete();
368 }
369 }
370
Feng Qianb3c02da2009-06-29 15:58:08 -0700371 // Read JavaScript flags if it exists.
372 String jsFlags = mSettings.getJsFlags();
373 if (jsFlags.trim().length() != 0) {
374 mTabControl.getCurrentWebView().setJsFlags(jsFlags);
375 }
Andrei Popescu30995e72010-02-09 16:59:58 +0000376 // Work out which packages are installed on the system.
377 getInstalledPackages();
Bjorn Bringerta7611812010-03-24 11:12:02 +0000378
379 // Start watching the default geolocation permissions
380 mSystemAllowGeolocationOrigins
381 = new SystemAllowGeolocationOrigins(getApplicationContext());
382 mSystemAllowGeolocationOrigins.start();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800383 }
384
Leon Scroggins58d56c62010-01-28 15:12:40 -0500385 /**
386 * Feed the previously stored results strings to the BrowserProvider so that
387 * the SearchDialog will show them instead of the standard searches.
388 * @param result String to show on the editable line of the SearchDialog.
389 */
390 /* package */ void showVoiceSearchResults(String result) {
391 ContentProviderClient client = mResolver.acquireContentProviderClient(
392 Browser.BOOKMARKS_URI);
393 ContentProvider prov = client.getLocalContentProvider();
394 BrowserProvider bp = (BrowserProvider) prov;
395 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
396 client.release();
397
Leon Scrogginsfbb3f152010-03-09 17:26:56 -0500398 Bundle bundle = createGoogleSearchSourceBundle(
399 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
400 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
401 startSearch(result, false, bundle, false);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500402 }
403
The Android Open Source Project0c908882009-03-03 19:32:16 -0800404 @Override
405 protected void onNewIntent(Intent intent) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700406 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800407 // When a tab is closed on exit, the current tab index is set to -1.
408 // Reset before proceed as Browser requires the current tab to be set.
409 if (current == null) {
410 // Try to reset the tab in case the index was incorrect.
411 current = mTabControl.getTab(0);
412 if (current == null) {
413 // No tabs at all so just ignore this intent.
414 return;
415 }
416 mTabControl.setCurrentTab(current);
417 attachTabToContentView(current);
418 resetTitleAndIcon(current.getWebView());
419 }
420 final String action = intent.getAction();
421 final int flags = intent.getFlags();
422 if (Intent.ACTION_MAIN.equals(action) ||
423 (flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
424 // just resume the browser
425 return;
426 }
Leon Scrogginsb8a844d2010-03-18 15:06:15 -0400427 // In case the SearchDialog is open.
428 ((SearchManager) getSystemService(Context.SEARCH_SERVICE))
429 .stopSearch();
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500430 boolean activateVoiceSearch = RecognizerResultsIntent
431 .ACTION_VOICE_SEARCH_RESULTS.equals(action);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800432 if (Intent.ACTION_VIEW.equals(action)
433 || Intent.ACTION_SEARCH.equals(action)
434 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
Leon Scroggins58d56c62010-01-28 15:12:40 -0500435 || Intent.ACTION_WEB_SEARCH.equals(action)
436 || activateVoiceSearch) {
Leon Scroggins3b18ce32010-02-08 17:35:59 -0500437 if (current.isInVoiceSearchMode()) {
438 String title = current.getVoiceDisplayTitle();
439 if (title != null && title.equals(intent.getStringExtra(
440 SearchManager.QUERY))) {
441 // The user submitted the same search as the last voice
442 // search, so do nothing.
443 return;
444 }
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500445 if (Intent.ACTION_SEARCH.equals(action)
446 && current.voiceSearchSourceIsGoogle()) {
447 Intent logIntent = new Intent(
448 LoggingEvents.ACTION_LOG_EVENT);
449 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
450 LoggingEvents.VoiceSearch.QUERY_UPDATED);
451 logIntent.putExtra(
452 LoggingEvents.VoiceSearch.EXTRA_QUERY_UPDATED_VALUE,
453 intent.getDataString());
454 sendBroadcast(logIntent);
455 // Note, onPageStarted will revert the voice title bar
456 // When http://b/issue?id=2379215 is fixed, we should update
457 // the title bar here.
458 }
Leon Scroggins3b18ce32010-02-08 17:35:59 -0500459 }
Satish Sampath565505b2009-05-29 15:37:27 +0100460 // If this was a search request (e.g. search query directly typed into the address bar),
461 // pass it on to the default web search provider.
462 if (handleWebSearchIntent(intent)) {
463 return;
464 }
465
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700466 UrlData urlData = getUrlDataFromIntent(intent);
467 if (urlData.isEmpty()) {
468 urlData = new UrlData(mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800469 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700470
Grace Klobacc634032009-07-28 15:58:19 -0700471 final String appId = intent
472 .getStringExtra(Browser.EXTRA_APPLICATION_ID);
Leon Scroggins47208682010-04-07 17:59:48 -0400473 if ((Intent.ACTION_VIEW.equals(action)
474 // If a voice search has no appId, it means that it came
475 // from the browser. In that case, reuse the current tab.
476 || (activateVoiceSearch && appId != null))
Grace Klobacc634032009-07-28 15:58:19 -0700477 && !getPackageName().equals(appId)
478 && (flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700479 Tab appTab = mTabControl.getTabFromId(appId);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700480 if (appTab != null) {
481 Log.i(LOGTAG, "Reusing tab for " + appId);
482 // Dismiss the subwindow if applicable.
483 dismissSubWindow(appTab);
484 // Since we might kill the WebView, remove it from the
485 // content view first.
486 removeTabFromContentView(appTab);
487 // Recreate the main WebView after destroying the old one.
488 // If the WebView has the same original url and is on that
489 // page, it can be reused.
490 boolean needsLoad =
Leon Scroggins6eac63e2010-03-15 18:19:14 -0400491 mTabControl.recreateWebView(appTab, urlData);
Ben Murdochbff2d602009-07-01 20:19:05 +0100492
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700493 if (current != appTab) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400494 switchToTab(mTabControl.getTabIndex(appTab));
495 if (needsLoad) {
Patrick Scott9d53da02010-02-19 10:19:01 -0500496 loadUrlDataIn(appTab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400497 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700498 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400499 // If the tab was the current tab, we have to attach
500 // it to the view system again.
501 attachTabToContentView(appTab);
502 if (needsLoad) {
Patrick Scott9d53da02010-02-19 10:19:01 -0500503 loadUrlDataIn(appTab, urlData);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700504 }
505 }
506 return;
Patrick Scottcd115892009-07-16 09:42:58 -0400507 } else {
508 // No matching application tab, try to find a regular tab
509 // with a matching url.
510 appTab = mTabControl.findUnusedTabWithUrl(urlData.mUrl);
Leon Scroggins25515f82009-08-19 15:31:58 -0400511 if (appTab != null) {
512 if (current != appTab) {
513 switchToTab(mTabControl.getTabIndex(appTab));
514 }
515 // Otherwise, we are already viewing the correct tab.
Patrick Scottcd115892009-07-16 09:42:58 -0400516 } else {
517 // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
518 // will be opened in a new tab unless we have reached
519 // MAX_TABS. Then the url will be opened in the current
520 // tab. If a new tab is created, it will have "true" for
521 // exit on close.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400522 openTabAndShow(urlData, true, appId);
Patrick Scottcd115892009-07-16 09:42:58 -0400523 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700524 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800525 } else {
Grace Kloba638d3f42009-11-23 10:35:04 -0800526 if (!urlData.isEmpty()
527 && urlData.mUrl.startsWith("about:debug")) {
528 if ("about:debug.dom".equals(urlData.mUrl)) {
529 current.getWebView().dumpDomTree(false);
530 } else if ("about:debug.dom.file".equals(urlData.mUrl)) {
531 current.getWebView().dumpDomTree(true);
532 } else if ("about:debug.render".equals(urlData.mUrl)) {
533 current.getWebView().dumpRenderTree(false);
534 } else if ("about:debug.render.file".equals(urlData.mUrl)) {
535 current.getWebView().dumpRenderTree(true);
536 } else if ("about:debug.display".equals(urlData.mUrl)) {
537 current.getWebView().dumpDisplayTree();
Mike Reed9b78e1d2010-01-13 14:40:52 -0800538 } else if (urlData.mUrl.startsWith("about:debug.drag")) {
539 int index = urlData.mUrl.codePointAt(16) - '0';
540 if (index <= 0 || index > 9) {
541 current.getWebView().setDragTracker(null);
542 } else {
543 current.getWebView().setDragTracker(new MeshTracker(index));
544 }
Grace Kloba638d3f42009-11-23 10:35:04 -0800545 } else {
546 mSettings.toggleDebugSettings();
547 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800548 return;
549 }
Leon Scroggins1f005d32009-08-10 17:36:42 -0400550 // Get rid of the subwindow if it exists
551 dismissSubWindow(current);
Leon Scroggins8588d152010-04-15 11:01:53 -0400552 // If the current Tab is being used as an application tab,
553 // remove the association, since the new Intent means that it is
554 // no longer associated with that application.
555 current.setAppId(null);
Patrick Scott9d53da02010-02-19 10:19:01 -0500556 loadUrlDataIn(current, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800557 }
558 }
559 }
560
Satish Sampath565505b2009-05-29 15:37:27 +0100561 private int parseUrlShortcut(String url) {
562 if (url == null) return SHORTCUT_INVALID;
563
564 // FIXME: quick search, need to be customized by setting
565 if (url.length() > 2 && url.charAt(1) == ' ') {
566 switch (url.charAt(0)) {
567 case 'g': return SHORTCUT_GOOGLE_SEARCH;
568 case 'w': return SHORTCUT_WIKIPEDIA_SEARCH;
569 case 'd': return SHORTCUT_DICTIONARY_SEARCH;
570 case 'l': return SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH;
571 }
572 }
573 return SHORTCUT_INVALID;
574 }
575
576 /**
577 * Launches the default web search activity with the query parameters if the given intent's data
578 * are identified as plain search terms and not URLs/shortcuts.
579 * @return true if the intent was handled and web search activity was launched, false if not.
580 */
581 private boolean handleWebSearchIntent(Intent intent) {
582 if (intent == null) return false;
583
584 String url = null;
585 final String action = intent.getAction();
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500586 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS.equals(
587 action)) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500588 return false;
589 }
Satish Sampath565505b2009-05-29 15:37:27 +0100590 if (Intent.ACTION_VIEW.equals(action)) {
Grace Kloba1e705052009-09-29 13:13:36 -0700591 Uri data = intent.getData();
592 if (data != null) url = data.toString();
Satish Sampath565505b2009-05-29 15:37:27 +0100593 } else if (Intent.ACTION_SEARCH.equals(action)
594 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
595 || Intent.ACTION_WEB_SEARCH.equals(action)) {
596 url = intent.getStringExtra(SearchManager.QUERY);
597 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100598 return handleWebSearchRequest(url, intent.getBundleExtra(SearchManager.APP_DATA),
599 intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
Satish Sampath565505b2009-05-29 15:37:27 +0100600 }
601
602 /**
603 * Launches the default web search activity with the query parameters if the given url string
604 * was identified as plain search terms and not URL/shortcut.
605 * @return true if the request was handled and web search activity was launched, false if not.
606 */
Bjorn Bringert04851702009-09-22 10:36:01 +0100607 private boolean handleWebSearchRequest(String inUrl, Bundle appData, String extraData) {
Satish Sampath565505b2009-05-29 15:37:27 +0100608 if (inUrl == null) return false;
609
610 // In general, we shouldn't modify URL from Intent.
611 // But currently, we get the user-typed URL from search box as well.
612 String url = fixUrl(inUrl).trim();
613
614 // URLs and site specific search shortcuts are handled by the regular flow of control, so
615 // return early.
Dan Egnor5ee906c2009-11-18 12:11:49 -0800616 if (Patterns.WEB_URL.matcher(url).matches()
Satish Sampathbc5b9f32009-06-04 18:21:40 +0100617 || ACCEPTED_URI_SCHEMA.matcher(url).matches()
Satish Sampath565505b2009-05-29 15:37:27 +0100618 || parseUrlShortcut(url) != SHORTCUT_INVALID) {
619 return false;
620 }
621
Leon Scroggins8d06e362010-03-24 14:45:57 -0400622 final ContentResolver cr = mResolver;
623 final String newUrl = url;
624 new AsyncTask<Void, Void, Void>() {
625 protected Void doInBackground(Void... unused) {
626 Browser.updateVisitedHistory(cr, newUrl, false);
627 Browser.addSearchUrl(cr, newUrl);
628 return null;
629 }
630 }.execute();
Satish Sampath565505b2009-05-29 15:37:27 +0100631
Bjorn Bringertd2670652010-09-13 14:06:41 +0100632 SearchEngine searchEngine = mSettings.getSearchEngine();
633 if (searchEngine == null) return false;
634 searchEngine.startSearch(this, url, appData, extraData);
Satish Sampath565505b2009-05-29 15:37:27 +0100635
636 return true;
637 }
638
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700639 private UrlData getUrlDataFromIntent(Intent intent) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500640 String url = "";
Grace Kloba068e48b2010-01-26 18:11:27 -0800641 Map<String, String> headers = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800642 if (intent != null) {
643 final String action = intent.getAction();
644 if (Intent.ACTION_VIEW.equals(action)) {
645 url = smartUrlFilter(intent.getData());
646 if (url != null && url.startsWith("content:")) {
647 /* Append mimetype so webview knows how to display */
648 String mimeType = intent.resolveType(getContentResolver());
649 if (mimeType != null) {
650 url += "?" + mimeType;
651 }
652 }
Grace Kloba068e48b2010-01-26 18:11:27 -0800653 if (url != null && url.startsWith("http")) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800654 final Bundle pairs = intent
655 .getBundleExtra(Browser.EXTRA_HEADERS);
Grace Kloba2d508ed2010-01-28 11:39:15 -0800656 if (pairs != null && !pairs.isEmpty()) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800657 Iterator<String> iter = pairs.keySet().iterator();
Grace Kloba068e48b2010-01-26 18:11:27 -0800658 headers = new HashMap<String, String>();
Grace Kloba00f54c52010-01-27 14:53:51 -0800659 while (iter.hasNext()) {
660 String key = iter.next();
661 headers.put(key, pairs.getString(key));
Grace Kloba068e48b2010-01-26 18:11:27 -0800662 }
663 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700664 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800665 } else if (Intent.ACTION_SEARCH.equals(action)
666 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
667 || Intent.ACTION_WEB_SEARCH.equals(action)) {
668 url = intent.getStringExtra(SearchManager.QUERY);
669 if (url != null) {
670 mLastEnteredUrl = url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800671 // In general, we shouldn't modify URL from Intent.
672 // But currently, we get the user-typed URL from search box as well.
673 url = fixUrl(url);
674 url = smartUrlFilter(url);
Leon Scroggins8d06e362010-03-24 14:45:57 -0400675 final ContentResolver cr = mResolver;
676 final String newUrl = url;
677 new AsyncTask<Void, Void, Void>() {
678 protected Void doInBackground(Void... unused) {
679 Browser.updateVisitedHistory(cr, newUrl, false);
680 return null;
681 }
682 }.execute();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800683 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
684 if (url.contains(searchSource)) {
685 String source = null;
686 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
687 if (appData != null) {
Bjorn Bringert10d1cca2010-02-10 14:22:12 +0000688 source = appData.getString(Search.SOURCE);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800689 }
690 if (TextUtils.isEmpty(source)) {
691 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
692 }
693 url = url.replace(searchSource, "&source=android-"+source+"&");
694 }
695 }
696 }
697 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500698 return new UrlData(url, headers, intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800699 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500700 /* package */ void showVoiceTitleBar(String title) {
701 mTitleBar.setInVoiceMode(true);
702 mFakeTitleBar.setInVoiceMode(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800703
Leon Scroggins58d56c62010-01-28 15:12:40 -0500704 mTitleBar.setDisplayTitle(title);
705 mFakeTitleBar.setDisplayTitle(title);
706 }
707 /* package */ void revertVoiceTitleBar() {
708 mTitleBar.setInVoiceMode(false);
709 mFakeTitleBar.setInVoiceMode(false);
710
Leon Scroggins003a5dd2010-03-10 12:13:14 -0500711 mTitleBar.setDisplayTitle(mUrl);
712 mFakeTitleBar.setDisplayTitle(mUrl);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500713 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800714 /* package */ static String fixUrl(String inUrl) {
Cary Clark652ff872009-09-10 13:34:44 -0400715 // FIXME: Converting the url to lower case
716 // duplicates functionality in smartUrlFilter().
717 // However, changing all current callers of fixUrl to
718 // call smartUrlFilter in addition may have unwanted
719 // consequences, and is deferred for now.
720 int colon = inUrl.indexOf(':');
721 boolean allLower = true;
722 for (int index = 0; index < colon; index++) {
723 char ch = inUrl.charAt(index);
724 if (!Character.isLetter(ch)) {
725 break;
726 }
727 allLower &= Character.isLowerCase(ch);
728 if (index == colon - 1 && !allLower) {
729 inUrl = inUrl.substring(0, colon).toLowerCase()
730 + inUrl.substring(colon);
731 }
732 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800733 if (inUrl.startsWith("http://") || inUrl.startsWith("https://"))
734 return inUrl;
735 if (inUrl.startsWith("http:") ||
736 inUrl.startsWith("https:")) {
737 if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) {
738 inUrl = inUrl.replaceFirst("/", "//");
739 } else inUrl = inUrl.replaceFirst(":", "://");
740 }
741 return inUrl;
742 }
743
Grace Kloba22ac16e2009-10-07 18:00:23 -0700744 @Override
745 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800746 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700747 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800748 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
749 }
750
751 if (!mActivityInPause) {
752 Log.e(LOGTAG, "BrowserActivity is already resumed.");
753 return;
754 }
755
Mike Reed7bfa63b2009-05-28 11:08:32 -0400756 mTabControl.resumeCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800757 mActivityInPause = false;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400758 resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800759
760 if (mWakeLock.isHeld()) {
761 mHandler.removeMessages(RELEASE_WAKELOCK);
762 mWakeLock.release();
763 }
764
The Android Open Source Project0c908882009-03-03 19:32:16 -0800765 registerReceiver(mNetworkStateIntentReceiver,
766 mNetworkStateChangedFilter);
767 WebView.enablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800768 }
769
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400770 /**
771 * Since the actual title bar is embedded in the WebView, and removing it
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400772 * would change its appearance, use a different TitleBar to show overlayed
773 * at the top of the screen, when the menu is open or the page is loading.
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400774 */
775 private TitleBar mFakeTitleBar;
776
777 /**
778 * Keeps track of whether the options menu is open. This is important in
779 * determining whether to show or hide the title bar overlay.
780 */
781 private boolean mOptionsMenuOpen;
782
783 /**
784 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
785 * of whether the configuration has changed. The first onMenuOpened call
786 * after a configuration change is simply a reopening of the same menu
787 * (i.e. mIconView did not change).
788 */
789 private boolean mConfigChanged;
790
791 /**
792 * Whether or not the options menu is in its smaller, icon menu form. When
793 * true, we want the title bar overlay to be up. When false, we do not.
794 * Only meaningful if mOptionsMenuOpen is true.
795 */
796 private boolean mIconView;
797
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400798 @Override
799 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400800 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
801 if (mOptionsMenuOpen) {
802 if (mConfigChanged) {
803 // We do not need to make any changes to the state of the
804 // title bar, since the only thing that happened was a
805 // change in orientation
806 mConfigChanged = false;
807 } else {
808 if (mIconView) {
809 // Switching the menu to expanded view, so hide the
810 // title bar.
811 hideFakeTitleBar();
812 mIconView = false;
813 } else {
814 // Switching the menu back to icon view, so show the
815 // title bar once again.
816 showFakeTitleBar();
817 mIconView = true;
818 }
819 }
820 } else {
821 // The options menu is closed, so open it, and show the title
822 showFakeTitleBar();
823 mOptionsMenuOpen = true;
824 mConfigChanged = false;
825 mIconView = true;
826 }
827 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400828 return true;
829 }
830
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400831 private void showFakeTitleBar() {
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400832 if (mFakeTitleBar.getParent() == null && mActiveTabsPage == null
Grace Kloba847c25b2010-03-30 16:00:26 -0700833 && !mActivityInPause) {
834 WebView mainView = mTabControl.getCurrentWebView();
835 // if there is no current WebView, don't show the faked title bar;
Grace Kloba65190702010-04-02 23:37:26 -0700836 if (mainView == null) {
Cary Clarka0464552009-09-29 13:00:45 -0400837 return;
838 }
Cary Clark2c326e62010-08-19 18:40:57 -0400839 // Do not need to check for null, since the current tab will have
840 // at least a main WebView, or we would have returned above.
841 if (dialogIsUp()) {
842 // Do not show the fake title bar, which would cover up the
843 // find or select dialog.
844 return;
845 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400846
847 WindowManager manager
848 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
849
850 // Add the title bar to the window manager so it can receive touches
851 // while the menu is up
852 WindowManager.LayoutParams params
853 = new WindowManager.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -0800854 ViewGroup.LayoutParams.MATCH_PARENT,
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400855 ViewGroup.LayoutParams.WRAP_CONTENT,
Grace Kloba847c25b2010-03-30 16:00:26 -0700856 WindowManager.LayoutParams.TYPE_APPLICATION,
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400857 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
Leon Scroggins68549862009-09-21 16:02:01 -0400858 PixelFormat.TRANSLUCENT);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400859 params.gravity = Gravity.TOP;
Grace Kloba847c25b2010-03-30 16:00:26 -0700860 boolean atTop = mainView.getScrollY() == 0;
Leon Scroggins83932c72009-09-30 11:55:54 -0400861 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
Grace Kloba13f9ace2010-03-18 21:44:14 -0700862 manager.addView(mFakeTitleBar, params);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400863 }
864 }
865
866 @Override
867 public void onOptionsMenuClosed(Menu menu) {
868 mOptionsMenuOpen = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -0400869 if (!mInLoad) {
870 hideFakeTitleBar();
871 } else if (!mIconView) {
872 // The page is currently loading, and we are in expanded mode, so
873 // we were not showing the menu. Show it once again. It will be
874 // removed when the page finishes.
875 showFakeTitleBar();
876 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400877 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700878
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400879 private void hideFakeTitleBar() {
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400880 if (mFakeTitleBar.getParent() == null) return;
Leon Scroggins20329572009-09-23 17:42:41 -0400881 WindowManager.LayoutParams params = (WindowManager.LayoutParams)
Grace Kloba13f9ace2010-03-18 21:44:14 -0700882 mFakeTitleBar.getLayoutParams();
Leon Scroggins20329572009-09-23 17:42:41 -0400883 WebView mainView = mTabControl.getCurrentWebView();
884 // Although we decided whether or not to animate based on the current
885 // scroll position, the scroll position may have changed since the
886 // fake title bar was displayed. Make sure it has the appropriate
887 // animation/lack thereof before removing.
888 params.windowAnimations = mainView != null && mainView.getScrollY() == 0
Leon Scroggins83932c72009-09-30 11:55:54 -0400889 ? 0 : R.style.TitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400890 WindowManager manager
891 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Grace Kloba13f9ace2010-03-18 21:44:14 -0700892 manager.updateViewLayout(mFakeTitleBar, params);
893 manager.removeView(mFakeTitleBar);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400894 }
895
The Android Open Source Project0c908882009-03-03 19:32:16 -0800896 /**
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400897 * Special method for the fake title bar to call when displaying its context
898 * menu, since it is in its own Window, and its parent does not show a
899 * context menu.
900 */
901 /* package */ void showTitleBarContextMenu() {
Cary Clark65f4a3c2009-09-28 17:05:06 -0400902 if (null == mTitleBar.getParent()) {
903 return;
904 }
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400905 openContextMenu(mTitleBar);
906 }
907
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400908 @Override
909 public void onContextMenuClosed(Menu menu) {
910 super.onContextMenuClosed(menu);
911 if (mInLoad) {
912 showFakeTitleBar();
913 }
914 }
915
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400916 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800917 * onSaveInstanceState(Bundle map)
918 * onSaveInstanceState is called right before onStop(). The map contains
919 * the saved state.
920 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700921 @Override
922 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700923 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800924 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
925 }
926 // the default implementation requires each view to have an id. As the
927 // browser handles the state itself and it doesn't use id for the views,
928 // don't call the default implementation. Otherwise it will trigger the
929 // warning like this, "couldn't save which view has focus because the
930 // focused view XXX has no id".
931
932 // Save all the tabs
933 mTabControl.saveState(outState);
934 }
935
Grace Kloba22ac16e2009-10-07 18:00:23 -0700936 @Override
937 protected void onPause() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800938 super.onPause();
939
940 if (mActivityInPause) {
941 Log.e(LOGTAG, "BrowserActivity is already paused.");
942 return;
943 }
944
Mike Reed7bfa63b2009-05-28 11:08:32 -0400945 mTabControl.pauseCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800946 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400947 if (mTabControl.getCurrentIndex() >= 0 && !pauseWebViewTimers()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800948 mWakeLock.acquire();
949 mHandler.sendMessageDelayed(mHandler
950 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
951 }
952
Leon Scrogginsa2ab6a72009-09-11 11:49:52 -0400953 // FIXME: This removes the active tabs page and resets the menu to
954 // MAIN_MENU. A better solution might be to do this work in onNewIntent
955 // but then we would need to save it in onSaveInstanceState and restore
956 // it in onCreate/onRestoreInstanceState
957 if (mActiveTabsPage != null) {
958 removeActiveTabPage(true);
959 }
960
The Android Open Source Project0c908882009-03-03 19:32:16 -0800961 cancelStopToast();
962
963 // unregister network state listener
964 unregisterReceiver(mNetworkStateIntentReceiver);
965 WebView.disablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800966 }
967
Grace Kloba22ac16e2009-10-07 18:00:23 -0700968 @Override
969 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700970 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800971 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
972 }
973 super.onDestroy();
Grace Kloba0923d692009-09-23 21:37:25 -0700974
Leon Scroggins8d5fa432009-10-02 15:55:59 -0400975 if (mUploadMessage != null) {
976 mUploadMessage.onReceiveValue(null);
977 mUploadMessage = null;
978 }
979
Grace Kloba0923d692009-09-23 21:37:25 -0700980 if (mTabControl == null) return;
981
Grace Kloba1fc98a32009-10-21 13:23:08 -0700982 // Remove the fake title bar if it is there
983 hideFakeTitleBar();
984
The Android Open Source Project0c908882009-03-03 19:32:16 -0800985 // Remove the current tab and sub window
Grace Kloba22ac16e2009-10-07 18:00:23 -0700986 Tab t = mTabControl.getCurrentTab();
Patrick Scottfb5e77f2009-04-08 19:17:37 -0700987 if (t != null) {
988 dismissSubWindow(t);
989 removeTabFromContentView(t);
990 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800991 // Destroy all the tabs
992 mTabControl.destroy();
993 WebIconDatabase.getInstance().close();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800994
Grace Klobab4da0ad2009-05-14 14:45:40 -0700995 unregisterReceiver(mPackageInstallationReceiver);
Bjorn Bringerta7611812010-03-24 11:12:02 +0000996
997 // Stop watching the default geolocation permissions
998 mSystemAllowGeolocationOrigins.stop();
999 mSystemAllowGeolocationOrigins = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001000 }
1001
1002 @Override
1003 public void onConfigurationChanged(Configuration newConfig) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04001004 mConfigChanged = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001005 super.onConfigurationChanged(newConfig);
1006
1007 if (mPageInfoDialog != null) {
1008 mPageInfoDialog.dismiss();
1009 showPageInfo(
1010 mPageInfoView,
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05001011 mPageInfoFromShowSSLCertificateOnError);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001012 }
1013 if (mSSLCertificateDialog != null) {
1014 mSSLCertificateDialog.dismiss();
1015 showSSLCertificate(
1016 mSSLCertificateView);
1017 }
1018 if (mSSLCertificateOnErrorDialog != null) {
1019 mSSLCertificateOnErrorDialog.dismiss();
1020 showSSLCertificateOnError(
1021 mSSLCertificateOnErrorView,
1022 mSSLCertificateOnErrorHandler,
1023 mSSLCertificateOnErrorError);
1024 }
1025 if (mHttpAuthenticationDialog != null) {
1026 String title = ((TextView) mHttpAuthenticationDialog
1027 .findViewById(com.android.internal.R.id.alertTitle)).getText()
1028 .toString();
1029 String name = ((TextView) mHttpAuthenticationDialog
1030 .findViewById(R.id.username_edit)).getText().toString();
1031 String password = ((TextView) mHttpAuthenticationDialog
1032 .findViewById(R.id.password_edit)).getText().toString();
1033 int focusId = mHttpAuthenticationDialog.getCurrentFocus()
1034 .getId();
1035 mHttpAuthenticationDialog.dismiss();
1036 showHttpAuthentication(mHttpAuthHandler, null, null, title,
1037 name, password, focusId);
1038 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001039 }
1040
Grace Kloba22ac16e2009-10-07 18:00:23 -07001041 @Override
1042 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001043 super.onLowMemory();
1044 mTabControl.freeMemory();
1045 }
1046
Cary Clarkff4d92c2010-03-25 11:17:03 -04001047 private void resumeWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001048 Tab tab = mTabControl.getCurrentTab();
Cary Clarkff4d92c2010-03-25 11:17:03 -04001049 if (tab == null) return; // monkey can trigger this
Grace Kloba22ac16e2009-10-07 18:00:23 -07001050 boolean inLoad = tab.inLoad();
1051 if ((!mActivityInPause && !inLoad) || (mActivityInPause && inLoad)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001052 CookieSyncManager.getInstance().startSync();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001053 WebView w = tab.getWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001054 if (w != null) {
1055 w.resumeTimers();
1056 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001057 }
1058 }
1059
Mike Reed7bfa63b2009-05-28 11:08:32 -04001060 private boolean pauseWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001061 Tab tab = mTabControl.getCurrentTab();
1062 boolean inLoad = tab.inLoad();
1063 if (mActivityInPause && !inLoad) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001064 CookieSyncManager.getInstance().stopSync();
1065 WebView w = mTabControl.getCurrentWebView();
1066 if (w != null) {
1067 w.pauseTimers();
1068 }
1069 return true;
1070 } else {
1071 return false;
1072 }
1073 }
1074
The Android Open Source Project0c908882009-03-03 19:32:16 -08001075 // Open the icon database and retain all the icons for visited sites.
1076 private void retainIconsOnStartup() {
1077 final WebIconDatabase db = WebIconDatabase.getInstance();
1078 db.open(getDir("icons", 0).getPath());
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001079 Cursor c = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001080 try {
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001081 c = Browser.getAllBookmarks(mResolver);
1082 if (c.moveToFirst()) {
1083 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
1084 do {
1085 String url = c.getString(urlIndex);
1086 db.retainIconForPageUrl(url);
1087 } while (c.moveToNext());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001088 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001089 } catch (IllegalStateException e) {
1090 Log.e(LOGTAG, "retainIconsOnStartup", e);
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001091 } finally {
1092 if (c!= null) c.close();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001093 }
1094 }
1095
1096 // Helper method for getting the top window.
1097 WebView getTopWindow() {
1098 return mTabControl.getCurrentTopWebView();
1099 }
1100
Grace Kloba22ac16e2009-10-07 18:00:23 -07001101 TabControl getTabControl() {
1102 return mTabControl;
1103 }
1104
The Android Open Source Project0c908882009-03-03 19:32:16 -08001105 @Override
1106 public boolean onCreateOptionsMenu(Menu menu) {
1107 super.onCreateOptionsMenu(menu);
1108
1109 MenuInflater inflater = getMenuInflater();
1110 inflater.inflate(R.menu.browser, menu);
1111 mMenu = menu;
1112 updateInLoadMenuItems();
1113 return true;
1114 }
1115
1116 /**
1117 * As the menu can be open when loading state changes
1118 * we must manually update the state of the stop/reload menu
1119 * item
1120 */
1121 private void updateInLoadMenuItems() {
1122 if (mMenu == null) {
1123 return;
1124 }
1125 MenuItem src = mInLoad ?
1126 mMenu.findItem(R.id.stop_menu_id):
1127 mMenu.findItem(R.id.reload_menu_id);
1128 MenuItem dest = mMenu.findItem(R.id.stop_reload_menu_id);
1129 dest.setIcon(src.getIcon());
1130 dest.setTitle(src.getTitle());
1131 }
1132
1133 @Override
1134 public boolean onContextItemSelected(MenuItem item) {
1135 // chording is not an issue with context menus, but we use the same
1136 // options selector, so set mCanChord to true so we can access them.
1137 mCanChord = true;
1138 int id = item.getItemId();
Leon Scroggins96afcb12009-12-10 12:35:56 -05001139 boolean result = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001140 switch (id) {
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001141 // For the context menu from the title bar
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001142 case R.id.title_bar_copy_page_url:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001143 Tab currentTab = mTabControl.getCurrentTab();
1144 if (null == currentTab) {
1145 result = false;
1146 break;
1147 }
1148 WebView mainView = currentTab.getWebView();
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001149 if (null == mainView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001150 result = false;
1151 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001152 }
Leon Scroggins96afcb12009-12-10 12:35:56 -05001153 copy(mainView.getUrl());
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001154 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001155 // -- Browser context menu
1156 case R.id.open_context_menu_id:
1157 case R.id.open_newtab_context_menu_id:
1158 case R.id.bookmark_context_menu_id:
1159 case R.id.save_link_context_menu_id:
1160 case R.id.share_link_context_menu_id:
1161 case R.id.copy_link_context_menu_id:
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001162 final WebView webView = getTopWindow();
1163 if (null == webView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001164 result = false;
1165 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001166 }
1167 final HashMap hrefMap = new HashMap();
1168 hrefMap.put("webview", webView);
1169 final Message msg = mHandler.obtainMessage(
1170 FOCUS_NODE_HREF, id, 0, hrefMap);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001171 webView.requestFocusNodeHref(msg);
1172 break;
1173
1174 default:
1175 // For other context menus
Leon Scroggins96afcb12009-12-10 12:35:56 -05001176 result = onOptionsItemSelected(item);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001177 }
1178 mCanChord = false;
Leon Scroggins96afcb12009-12-10 12:35:56 -05001179 return result;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001180 }
1181
1182 private Bundle createGoogleSearchSourceBundle(String source) {
1183 Bundle bundle = new Bundle();
Bjorn Bringert10d1cca2010-02-10 14:22:12 +00001184 bundle.putString(Search.SOURCE, source);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001185 return bundle;
1186 }
1187
Leon Scroggins8ad29922010-02-16 12:33:55 -05001188 /* package */ void editUrl() {
Leon Scroggins68579392009-09-15 15:31:54 -04001189 if (mOptionsMenuOpen) closeOptionsMenu();
Leon Scroggins5bbe9802009-07-31 13:10:55 -04001190 String url = (getTopWindow() == null) ? null : getTopWindow().getUrl();
Grace Kloba83f47342009-07-20 10:44:31 -07001191 startSearch(mSettings.getHomePage().equals(url) ? null : url, true,
Leon Scroggins8ad29922010-02-16 12:33:55 -05001192 null, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001193 }
1194
Leon Scroggins8ad29922010-02-16 12:33:55 -05001195 /**
1196 * Overriding this to insert a local information bundle
1197 */
The Android Open Source Project0c908882009-03-03 19:32:16 -08001198 @Override
1199 public void startSearch(String initialQuery, boolean selectInitialQuery,
1200 Bundle appSearchData, boolean globalSearch) {
1201 if (appSearchData == null) {
1202 appSearchData = createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_TYPE);
1203 }
Leon Scroggins III430057d2010-09-14 10:57:37 -04001204
1205 SearchEngine searchEngine = mSettings.getSearchEngine();
1206 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
1207 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
1208 }
1209
The Android Open Source Project0c908882009-03-03 19:32:16 -08001210 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1211 }
1212
Leon Scroggins1f005d32009-08-10 17:36:42 -04001213 /**
1214 * Switch tabs. Called by the TitleBarSet when sliding the title bar
1215 * results in changing tabs.
Leon Scroggins160a7e72009-08-14 18:28:01 -04001216 * @param index Index of the tab to change to, as defined by
1217 * mTabControl.getTabIndex(Tab t).
1218 * @return boolean True if we successfully switched to a different tab. If
1219 * the indexth tab is null, or if that tab is the same as
1220 * the current one, return false.
Leon Scroggins1f005d32009-08-10 17:36:42 -04001221 */
Leon Scroggins160a7e72009-08-14 18:28:01 -04001222 /* package */ boolean switchToTab(int index) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001223 Tab tab = mTabControl.getTab(index);
1224 Tab currentTab = mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001225 if (tab == null || tab == currentTab) {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001226 return false;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001227 }
1228 if (currentTab != null) {
1229 // currentTab may be null if it was just removed. In that case,
1230 // we do not need to remove it
1231 removeTabFromContentView(currentTab);
1232 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001233 mTabControl.setCurrentTab(tab);
1234 attachTabToContentView(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001235 resetTitleIconAndProgress();
1236 updateLockIconToLatest();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001237 return true;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001238 }
1239
Grace Kloba22ac16e2009-10-07 18:00:23 -07001240 /* package */ Tab openTabToHomePage() {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001241 return openTabAndShow(mSettings.getHomePage(), false, null);
1242 }
1243
Leon Scroggins1f005d32009-08-10 17:36:42 -04001244 /* package */ void closeCurrentWindow() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001245 final Tab current = mTabControl.getCurrentTab();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001246 if (mTabControl.getTabCount() == 1) {
Leon Scroggins30444232009-09-04 18:36:20 -04001247 // This is the last tab. Open a new one, with the home
1248 // page and close the current one.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001249 openTabToHomePage();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001250 closeTab(current);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001251 return;
1252 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001253 final Tab parent = current.getParentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001254 int indexToShow = -1;
1255 if (parent != null) {
1256 indexToShow = mTabControl.getTabIndex(parent);
1257 } else {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001258 final int currentIndex = mTabControl.getCurrentIndex();
1259 // Try to move to the tab to the right
1260 indexToShow = currentIndex + 1;
1261 if (indexToShow > mTabControl.getTabCount() - 1) {
1262 // Try to move to the tab to the left
1263 indexToShow = currentIndex - 1;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001264 }
1265 }
Leon Scroggins160a7e72009-08-14 18:28:01 -04001266 if (switchToTab(indexToShow)) {
1267 // Close window
1268 closeTab(current);
1269 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001270 }
1271
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001272 private ActiveTabsPage mActiveTabsPage;
1273
1274 /**
1275 * Remove the active tabs page.
1276 * @param needToAttach If true, the active tabs page did not attach a tab
1277 * to the content view, so we need to do that here.
1278 */
1279 /* package */ void removeActiveTabPage(boolean needToAttach) {
1280 mContentView.removeView(mActiveTabsPage);
1281 mActiveTabsPage = null;
1282 mMenuState = R.id.MAIN_MENU;
1283 if (needToAttach) {
1284 attachTabToContentView(mTabControl.getCurrentTab());
1285 }
1286 getTopWindow().requestFocus();
1287 }
1288
Cary Clark2c326e62010-08-19 18:40:57 -04001289 private WebView showDialog(WebDialog dialog) {
Cary Clark2c326e62010-08-19 18:40:57 -04001290 Tab tab = mTabControl.getCurrentTab();
1291 if (tab.getSubWebView() == null) {
1292 // If the find or select is being performed on the main webview,
1293 // remove the embedded title bar.
1294 WebView mainView = tab.getWebView();
1295 if (mainView != null) {
1296 mainView.setEmbeddedTitleBar(null);
1297 }
1298 }
1299 hideFakeTitleBar();
1300 mMenuState = EMPTY_MENU;
1301 return tab.showDialog(dialog);
1302 }
1303
The Android Open Source Project0c908882009-03-03 19:32:16 -08001304 @Override
1305 public boolean onOptionsItemSelected(MenuItem item) {
1306 if (!mCanChord) {
1307 // The user has already fired a shortcut with this hold down of the
1308 // menu key.
1309 return false;
1310 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001311 if (null == getTopWindow()) {
Leon Scroggins0d7ae0e2009-06-05 11:04:45 -04001312 return false;
1313 }
Grace Kloba6ee9c492009-07-13 10:04:34 -07001314 if (mMenuIsDown) {
1315 // The shortcut action consumes the MENU. Even if it is still down,
1316 // it won't trigger the next shortcut action. In the case of the
1317 // shortcut action triggering a new activity, like Bookmarks, we
1318 // won't get onKeyUp for MENU. So it is important to reset it here.
1319 mMenuIsDown = false;
1320 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001321 switch (item.getItemId()) {
1322 // -- Main menu
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001323 case R.id.new_tab_menu_id:
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001324 openTabToHomePage();
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001325 break;
1326
Leon Scroggins64b80f32009-08-07 12:03:34 -04001327 case R.id.goto_menu_id:
Leon Scroggins8ad29922010-02-16 12:33:55 -05001328 editUrl();
Leon Scrogginsb3a5bed2009-09-28 11:21:56 -04001329 break;
1330
1331 case R.id.bookmarks_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001332 bookmarksOrHistoryPicker(false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001333 break;
1334
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001335 case R.id.active_tabs_menu_id:
1336 mActiveTabsPage = new ActiveTabsPage(this, mTabControl);
1337 removeTabFromContentView(mTabControl.getCurrentTab());
Leon Scroggins43de6162009-09-14 19:59:58 -04001338 hideFakeTitleBar();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001339 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
1340 mActiveTabsPage.requestFocus();
1341 mMenuState = EMPTY_MENU;
1342 break;
1343
Leon Scroggins1f005d32009-08-10 17:36:42 -04001344 case R.id.add_bookmark_menu_id:
1345 Intent i = new Intent(BrowserActivity.this,
1346 AddBookmarkPage.class);
1347 WebView w = getTopWindow();
1348 i.putExtra("url", w.getUrl());
1349 i.putExtra("title", w.getTitle());
Grace Kloba83cdb2c2009-09-16 00:48:57 -07001350 i.putExtra("touch_icon_url", w.getTouchIconUrl());
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01001351 i.putExtra("thumbnail", createScreenshot(w));
Leon Scroggins1f005d32009-08-10 17:36:42 -04001352 startActivity(i);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001353 break;
1354
1355 case R.id.stop_reload_menu_id:
1356 if (mInLoad) {
1357 stopLoading();
1358 } else {
1359 getTopWindow().reload();
1360 }
1361 break;
1362
1363 case R.id.back_menu_id:
1364 getTopWindow().goBack();
1365 break;
1366
1367 case R.id.forward_menu_id:
1368 getTopWindow().goForward();
1369 break;
1370
1371 case R.id.close_menu_id:
1372 // Close the subwindow if it exists.
1373 if (mTabControl.getCurrentSubWindow() != null) {
1374 dismissSubWindow(mTabControl.getCurrentTab());
1375 break;
1376 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001377 closeCurrentWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001378 break;
1379
1380 case R.id.homepage_menu_id:
Grace Kloba22ac16e2009-10-07 18:00:23 -07001381 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001382 if (current != null) {
1383 dismissSubWindow(current);
Leon Scroggins92472e82010-02-17 16:32:28 -05001384 loadUrl(current.getWebView(), mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001385 }
1386 break;
1387
1388 case R.id.preferences_menu_id:
1389 Intent intent = new Intent(this,
1390 BrowserPreferencesPage.class);
Leon Scrogginsd5304942009-12-10 16:11:39 -05001391 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1392 getTopWindow().getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001393 startActivityForResult(intent, PREFERENCES_PAGE);
1394 break;
1395
1396 case R.id.find_menu_id:
Cary Clark2c326e62010-08-19 18:40:57 -04001397 showFindDialog();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001398 break;
1399
1400 case R.id.select_text_id:
Cary Clark5feb8ad2010-09-21 14:28:49 -04001401 if (true) {
1402 Tab currentTab = mTabControl.getCurrentTab();
1403 if (currentTab != null) {
1404 currentTab.getWebView().setUpSelect();
1405 }
1406 } else {
1407 showSelectDialog();
1408 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001409 break;
Cary Clark2c326e62010-08-19 18:40:57 -04001410
The Android Open Source Project0c908882009-03-03 19:32:16 -08001411 case R.id.page_info_menu_id:
1412 showPageInfo(mTabControl.getCurrentTab(), false);
1413 break;
1414
1415 case R.id.classic_history_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001416 bookmarksOrHistoryPicker(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001417 break;
1418
Leon Scroggins96afcb12009-12-10 12:35:56 -05001419 case R.id.title_bar_share_page_url:
The Android Open Source Project0c908882009-03-03 19:32:16 -08001420 case R.id.share_page_menu_id:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001421 Tab currentTab = mTabControl.getCurrentTab();
1422 if (null == currentTab) {
1423 mCanChord = false;
1424 return false;
1425 }
1426 currentTab.populatePickerData();
1427 sharePage(this, currentTab.getTitle(),
1428 currentTab.getUrl(), currentTab.getFavicon(),
1429 createScreenshot(currentTab.getWebView()));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001430 break;
1431
1432 case R.id.dump_nav_menu_id:
1433 getTopWindow().debugDump();
1434 break;
1435
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001436 case R.id.dump_counters_menu_id:
1437 getTopWindow().dumpV8Counters();
1438 break;
1439
The Android Open Source Project0c908882009-03-03 19:32:16 -08001440 case R.id.zoom_in_menu_id:
1441 getTopWindow().zoomIn();
1442 break;
1443
1444 case R.id.zoom_out_menu_id:
1445 getTopWindow().zoomOut();
1446 break;
1447
1448 case R.id.view_downloads_menu_id:
1449 viewDownloads(null);
1450 break;
1451
The Android Open Source Project0c908882009-03-03 19:32:16 -08001452 case R.id.window_one_menu_id:
1453 case R.id.window_two_menu_id:
1454 case R.id.window_three_menu_id:
1455 case R.id.window_four_menu_id:
1456 case R.id.window_five_menu_id:
1457 case R.id.window_six_menu_id:
1458 case R.id.window_seven_menu_id:
1459 case R.id.window_eight_menu_id:
1460 {
1461 int menuid = item.getItemId();
1462 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1463 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001464 Tab desiredTab = mTabControl.getTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001465 if (desiredTab != null &&
1466 desiredTab != mTabControl.getCurrentTab()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001467 switchToTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001468 }
1469 break;
1470 }
1471 }
1472 }
1473 break;
1474
1475 default:
1476 if (!super.onOptionsItemSelected(item)) {
1477 return false;
1478 }
1479 // Otherwise fall through.
1480 }
1481 mCanChord = false;
1482 return true;
1483 }
1484
Cary Clark2c326e62010-08-19 18:40:57 -04001485 private boolean dialogIsUp() {
1486 return null != mFindDialog && mFindDialog.isVisible() ||
1487 null != mSelectDialog && mSelectDialog.isVisible();
1488 }
1489
1490 private boolean closeDialog(WebDialog dialog) {
1491 if (null == dialog || !dialog.isVisible()) return false;
1492 Tab currentTab = mTabControl.getCurrentTab();
1493 currentTab.closeDialog(dialog);
1494 dialog.dismiss();
1495 return true;
1496 }
1497
1498 /*
1499 * Remove the find dialog or select dialog.
1500 */
1501 public void closeDialogs() {
1502 if (!(closeDialog(mFindDialog) || closeDialog(mSelectDialog))) return;
1503 // If the Find was being performed in the main WebView, replace the
1504 // embedded title bar.
1505 Tab currentTab = mTabControl.getCurrentTab();
1506 if (currentTab.getSubWebView() == null) {
1507 WebView mainView = currentTab.getWebView();
1508 if (mainView != null) {
1509 mainView.setEmbeddedTitleBar(mTitleBar);
1510 }
1511 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001512 mMenuState = R.id.MAIN_MENU;
Cary Clark2c326e62010-08-19 18:40:57 -04001513 if (mInLoad) {
1514 // The title bar was hidden, because otherwise it would cover up the
1515 // find or select dialog. Now that the dialog has been removed,
1516 // show the fake title bar once again.
1517 showFakeTitleBar();
1518 }
1519 }
1520
1521 public void showFindDialog() {
1522 if (null == mFindDialog) {
1523 mFindDialog = new FindDialog(this);
1524 }
1525 showDialog(mFindDialog).setFindIsUp(true);
1526 }
1527
1528 public void setFindDialogText(String text) {
1529 mFindDialog.setText(text);
1530 }
1531
1532 public void showSelectDialog() {
1533 if (null == mSelectDialog) {
1534 mSelectDialog = new SelectDialog(this);
1535 }
1536 showDialog(mSelectDialog).setUpSelect();
1537 mSelectDialog.hideSoftInput();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001538 }
1539
Grace Kloba22ac16e2009-10-07 18:00:23 -07001540 @Override
1541 public boolean onPrepareOptionsMenu(Menu menu) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001542 // This happens when the user begins to hold down the menu key, so
1543 // allow them to chord to get a shortcut.
1544 mCanChord = true;
1545 // Note: setVisible will decide whether an item is visible; while
1546 // setEnabled() will decide whether an item is enabled, which also means
1547 // whether the matching shortcut key will function.
1548 super.onPrepareOptionsMenu(menu);
1549 switch (mMenuState) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001550 case EMPTY_MENU:
1551 if (mCurrentMenuState != mMenuState) {
1552 menu.setGroupVisible(R.id.MAIN_MENU, false);
1553 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1554 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001555 }
1556 break;
1557 default:
1558 if (mCurrentMenuState != mMenuState) {
1559 menu.setGroupVisible(R.id.MAIN_MENU, true);
1560 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1561 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001562 }
1563 final WebView w = getTopWindow();
1564 boolean canGoBack = false;
1565 boolean canGoForward = false;
1566 boolean isHome = false;
1567 if (w != null) {
1568 canGoBack = w.canGoBack();
1569 canGoForward = w.canGoForward();
1570 isHome = mSettings.getHomePage().equals(w.getUrl());
1571 }
1572 final MenuItem back = menu.findItem(R.id.back_menu_id);
1573 back.setEnabled(canGoBack);
1574
1575 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1576 home.setEnabled(!isHome);
1577
1578 menu.findItem(R.id.forward_menu_id)
1579 .setEnabled(canGoForward);
1580
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001581 menu.findItem(R.id.new_tab_menu_id).setEnabled(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001582 mTabControl.canCreateNewTab());
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001583
The Android Open Source Project0c908882009-03-03 19:32:16 -08001584 // decide whether to show the share link option
1585 PackageManager pm = getPackageManager();
1586 Intent send = new Intent(Intent.ACTION_SEND);
1587 send.setType("text/plain");
1588 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1589 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1590
The Android Open Source Project0c908882009-03-03 19:32:16 -08001591 boolean isNavDump = mSettings.isNavDump();
1592 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1593 nav.setVisible(isNavDump);
1594 nav.setEnabled(isNavDump);
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001595
1596 boolean showDebugSettings = mSettings.showDebugSettings();
1597 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1598 counter.setVisible(showDebugSettings);
1599 counter.setEnabled(showDebugSettings);
1600
The Android Open Source Project0c908882009-03-03 19:32:16 -08001601 break;
1602 }
1603 mCurrentMenuState = mMenuState;
1604 return true;
1605 }
1606
1607 @Override
1608 public void onCreateContextMenu(ContextMenu menu, View v,
1609 ContextMenuInfo menuInfo) {
Leon Scroggins4e9f89b2010-02-22 16:54:14 -05001610 if (v instanceof TitleBar) {
1611 return;
1612 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001613 WebView webview = (WebView) v;
1614 WebView.HitTestResult result = webview.getHitTestResult();
1615 if (result == null) {
1616 return;
1617 }
1618
1619 int type = result.getType();
1620 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1621 Log.w(LOGTAG,
1622 "We should not show context menu when nothing is touched");
1623 return;
1624 }
1625 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1626 // let TextView handles context menu
1627 return;
1628 }
1629
1630 // Note, http://b/issue?id=1106666 is requesting that
1631 // an inflated menu can be used again. This is not available
1632 // yet, so inflate each time (yuk!)
1633 MenuInflater inflater = getMenuInflater();
1634 inflater.inflate(R.menu.browsercontext, menu);
1635
1636 // Show the correct menu group
1637 String extra = result.getExtra();
1638 menu.setGroupVisible(R.id.PHONE_MENU,
1639 type == WebView.HitTestResult.PHONE_TYPE);
1640 menu.setGroupVisible(R.id.EMAIL_MENU,
1641 type == WebView.HitTestResult.EMAIL_TYPE);
1642 menu.setGroupVisible(R.id.GEO_MENU,
1643 type == WebView.HitTestResult.GEO_TYPE);
1644 menu.setGroupVisible(R.id.IMAGE_MENU,
1645 type == WebView.HitTestResult.IMAGE_TYPE
1646 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1647 menu.setGroupVisible(R.id.ANCHOR_MENU,
1648 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1649 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1650
1651 // Setup custom handling depending on the type
1652 switch (type) {
1653 case WebView.HitTestResult.PHONE_TYPE:
1654 menu.setHeaderTitle(Uri.decode(extra));
1655 menu.findItem(R.id.dial_context_menu_id).setIntent(
1656 new Intent(Intent.ACTION_VIEW, Uri
1657 .parse(WebView.SCHEME_TEL + extra)));
1658 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1659 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
Cary Clark5e335a32009-09-22 14:53:11 -04001660 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001661 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1662 addIntent);
1663 menu.findItem(R.id.copy_phone_context_menu_id).setOnMenuItemClickListener(
1664 new Copy(extra));
1665 break;
1666
1667 case WebView.HitTestResult.EMAIL_TYPE:
1668 menu.setHeaderTitle(extra);
1669 menu.findItem(R.id.email_context_menu_id).setIntent(
1670 new Intent(Intent.ACTION_VIEW, Uri
1671 .parse(WebView.SCHEME_MAILTO + extra)));
1672 menu.findItem(R.id.copy_mail_context_menu_id).setOnMenuItemClickListener(
1673 new Copy(extra));
1674 break;
1675
1676 case WebView.HitTestResult.GEO_TYPE:
1677 menu.setHeaderTitle(extra);
1678 menu.findItem(R.id.map_context_menu_id).setIntent(
1679 new Intent(Intent.ACTION_VIEW, Uri
1680 .parse(WebView.SCHEME_GEO
1681 + URLEncoder.encode(extra))));
1682 menu.findItem(R.id.copy_geo_context_menu_id).setOnMenuItemClickListener(
1683 new Copy(extra));
1684 break;
1685
1686 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1687 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
1688 TextView titleView = (TextView) LayoutInflater.from(this)
1689 .inflate(android.R.layout.browser_link_context_header,
1690 null);
1691 titleView.setText(extra);
1692 menu.setHeaderView(titleView);
1693 // decide whether to show the open link in new tab option
1694 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001695 mTabControl.canCreateNewTab());
Ben Murdochde353622009-10-12 10:29:00 +01001696 menu.findItem(R.id.bookmark_context_menu_id).setVisible(
1697 Bookmarks.urlHasAcceptableScheme(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001698 PackageManager pm = getPackageManager();
1699 Intent send = new Intent(Intent.ACTION_SEND);
1700 send.setType("text/plain");
1701 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1702 menu.findItem(R.id.share_link_context_menu_id).setVisible(ri != null);
1703 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1704 break;
1705 }
1706 // otherwise fall through to handle image part
1707 case WebView.HitTestResult.IMAGE_TYPE:
1708 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1709 menu.setHeaderTitle(extra);
1710 }
1711 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1712 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1713 menu.findItem(R.id.download_context_menu_id).
1714 setOnMenuItemClickListener(new Download(extra));
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001715 menu.findItem(R.id.set_wallpaper_context_menu_id).
1716 setOnMenuItemClickListener(new SetAsWallpaper(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001717 break;
1718
1719 default:
1720 Log.w(LOGTAG, "We should not get here.");
1721 break;
1722 }
Leon Scrogginsb2b19f52009-10-09 16:10:00 -04001723 hideFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001724 }
1725
The Android Open Source Project0c908882009-03-03 19:32:16 -08001726 // Attach the given tab to the content view.
Grace Klobac928c302009-09-17 11:51:21 -07001727 // this should only be called for the current tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728 private void attachTabToContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001729 // Attach the container that contains the main WebView and any other UI
1730 // associated with the tab.
Patrick Scottd0119532009-09-17 08:00:31 -04001731 t.attachTabToContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001732
1733 if (mShouldShowErrorConsole) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001734 ErrorConsoleView errorConsole = t.getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01001735 if (errorConsole.numberOfErrors() == 0) {
1736 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
1737 } else {
1738 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1739 }
1740
1741 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08001742 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01001743 ViewGroup.LayoutParams.WRAP_CONTENT));
1744 }
1745
Leon Scroggins39ab28e2009-09-02 21:20:30 -04001746 WebView view = t.getWebView();
Leon Scroggins55a5bc22009-09-04 17:00:08 -04001747 view.setEmbeddedTitleBar(mTitleBar);
Leon Scroggins58d56c62010-01-28 15:12:40 -05001748 if (t.isInVoiceSearchMode()) {
1749 showVoiceTitleBar(t.getVoiceDisplayTitle());
1750 } else {
1751 revertVoiceTitleBar();
1752 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001753 // Request focus on the top window.
1754 t.getTopWindow().requestFocus();
1755 }
1756
1757 // Attach a sub window to the main WebView of the given tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001758 void attachSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001759 t.attachSubWindow(mContentView);
1760 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001761 }
1762
1763 // Remove the given tab from the content view.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001764 private void removeTabFromContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001765 // Remove the container that contains the main WebView.
Patrick Scottd0119532009-09-17 08:00:31 -04001766 t.removeTabFromContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001767
Grace Kloba22ac16e2009-10-07 18:00:23 -07001768 ErrorConsoleView errorConsole = t.getErrorConsole(false);
1769 if (errorConsole != null) {
1770 mErrorConsoleContainer.removeView(errorConsole);
Ben Murdochbff2d602009-07-01 20:19:05 +01001771 }
1772
Leon Scroggins39ab28e2009-09-02 21:20:30 -04001773 WebView view = t.getWebView();
Leon Scrogginsbb85b902009-09-14 19:27:20 -04001774 if (view != null) {
1775 view.setEmbeddedTitleBar(null);
1776 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001777 }
1778
1779 // Remove the sub window if it exists. Also called by TabControl when the
1780 // user clicks the 'X' to dismiss a sub window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001781 /* package */ void dismissSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001782 t.removeSubWindow(mContentView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001783 // dismiss the subwindow. This will destroy the WebView.
1784 t.dismissSubWindow();
Patrick Scottd0119532009-09-17 08:00:31 -04001785 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001786 }
1787
Leon Scroggins1f005d32009-08-10 17:36:42 -04001788 // A wrapper function of {@link #openTabAndShow(UrlData, boolean, String)}
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07001789 // that accepts url as string.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001790 private Tab openTabAndShow(String url, boolean closeOnExit, String appId) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001791 return openTabAndShow(new UrlData(url), closeOnExit, appId);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001792 }
1793
1794 // This method does a ton of stuff. It will attempt to create a new tab
1795 // if we haven't reached MAX_TABS. Otherwise it uses the current tab. If
Leon Scroggins1f005d32009-08-10 17:36:42 -04001796 // url isn't null, it will load the given url.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001797 /* package */Tab openTabAndShow(UrlData urlData, boolean closeOnExit,
1798 String appId) {
1799 final Tab currentTab = mTabControl.getCurrentTab();
1800 if (mTabControl.canCreateNewTab()) {
1801 final Tab tab = mTabControl.createNewTab(closeOnExit, appId,
1802 urlData.mUrl);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001803 WebView webview = tab.getWebView();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001804 // If the last tab was removed from the active tabs page, currentTab
1805 // will be null.
1806 if (currentTab != null) {
1807 removeTabFromContentView(currentTab);
1808 }
Patrick Scott8bbd69f2009-08-14 13:35:53 -04001809 // We must set the new tab as the current tab to reflect the old
1810 // animation behavior.
1811 mTabControl.setCurrentTab(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001812 attachTabToContentView(tab);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001813 if (!urlData.isEmpty()) {
Patrick Scott9d53da02010-02-19 10:19:01 -05001814 loadUrlDataIn(tab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001815 }
1816 return tab;
1817 } else {
1818 // Get rid of the subwindow if it exists
1819 dismissSubWindow(currentTab);
1820 if (!urlData.isEmpty()) {
1821 // Load the given url.
Patrick Scott9d53da02010-02-19 10:19:01 -05001822 loadUrlDataIn(currentTab, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001823 }
Leon Scroggins58d56c62010-01-28 15:12:40 -05001824 return currentTab;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001825 }
1826 }
1827
Grace Kloba22ac16e2009-10-07 18:00:23 -07001828 private Tab openTab(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001829 if (mSettings.openInBackground()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001830 Tab t = mTabControl.createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001831 if (t != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001832 WebView view = t.getWebView();
Leon Scroggins92472e82010-02-17 16:32:28 -05001833 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001834 }
Grace Klobac9181842009-04-14 08:53:22 -07001835 return t;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001836 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001837 return openTabAndShow(url, false, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001838 }
1839 }
1840
1841 private class Copy implements OnMenuItemClickListener {
1842 private CharSequence mText;
1843
1844 public boolean onMenuItemClick(MenuItem item) {
1845 copy(mText);
1846 return true;
1847 }
1848
1849 public Copy(CharSequence toCopy) {
1850 mText = toCopy;
1851 }
1852 }
1853
1854 private class Download implements OnMenuItemClickListener {
1855 private String mText;
1856
1857 public boolean onMenuItemClick(MenuItem item) {
1858 onDownloadStartNoStream(mText, null, null, null, -1);
1859 return true;
1860 }
1861
1862 public Download(String toDownload) {
1863 mText = toDownload;
1864 }
1865 }
1866
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001867 private class SetAsWallpaper extends Thread implements
1868 OnMenuItemClickListener, DialogInterface.OnCancelListener {
1869 private URL mUrl;
1870 private ProgressDialog mWallpaperProgress;
1871 private boolean mCanceled = false;
1872
1873 public SetAsWallpaper(String url) {
1874 try {
1875 mUrl = new URL(url);
1876 } catch (MalformedURLException e) {
1877 mUrl = null;
1878 }
1879 }
1880
1881 public void onCancel(DialogInterface dialog) {
1882 mCanceled = true;
1883 }
1884
1885 public boolean onMenuItemClick(MenuItem item) {
1886 if (mUrl != null) {
1887 // The user may have tried to set a image with a large file size as their
1888 // background so it may take a few moments to perform the operation. Display
1889 // a progress spinner while it is working.
1890 mWallpaperProgress = new ProgressDialog(BrowserActivity.this);
1891 mWallpaperProgress.setIndeterminate(true);
1892 mWallpaperProgress.setMessage(getText(R.string.progress_dialog_setting_wallpaper));
1893 mWallpaperProgress.setCancelable(true);
1894 mWallpaperProgress.setOnCancelListener(this);
1895 mWallpaperProgress.show();
1896 start();
1897 }
1898 return true;
1899 }
1900
1901 public void run() {
1902 Drawable oldWallpaper = BrowserActivity.this.getWallpaper();
1903 try {
1904 // TODO: This will cause the resource to be downloaded again, when we
1905 // should in most cases be able to grab it from the cache. To fix this
1906 // we should query WebCore to see if we can access a cached version and
1907 // instead open an input stream on that. This pattern could also be used
1908 // in the download manager where the same problem exists.
1909 InputStream inputstream = mUrl.openStream();
1910 if (inputstream != null) {
1911 setWallpaper(inputstream);
1912 }
1913 } catch (IOException e) {
1914 Log.e(LOGTAG, "Unable to set new wallpaper");
1915 // Act as though the user canceled the operation so we try to
1916 // restore the old wallpaper.
1917 mCanceled = true;
1918 }
1919
1920 if (mCanceled) {
1921 // Restore the old wallpaper if the user cancelled whilst we were setting
1922 // the new wallpaper.
1923 int width = oldWallpaper.getIntrinsicWidth();
1924 int height = oldWallpaper.getIntrinsicHeight();
1925 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1926 Canvas canvas = new Canvas(bm);
1927 oldWallpaper.setBounds(0, 0, width, height);
1928 oldWallpaper.draw(canvas);
1929 try {
1930 setWallpaper(bm);
1931 } catch (IOException e) {
1932 Log.e(LOGTAG, "Unable to restore old wallpaper.");
1933 }
1934 mCanceled = false;
1935 }
1936
1937 if (mWallpaperProgress.isShowing()) {
1938 mWallpaperProgress.dismiss();
1939 }
1940 }
1941 }
1942
The Android Open Source Project0c908882009-03-03 19:32:16 -08001943 private void copy(CharSequence text) {
1944 try {
1945 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
1946 if (clip != null) {
1947 clip.setClipboardText(text);
1948 }
1949 } catch (android.os.RemoteException e) {
1950 Log.e(LOGTAG, "Copy failed", e);
1951 }
1952 }
1953
1954 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001955 * Resets the browser title-view to whatever it must be
1956 * (for example, if we had a loading error)
1957 * When we have a new page, we call resetTitle, when we
1958 * have to reset the titlebar to whatever it used to be
1959 * (for example, if the user chose to stop loading), we
1960 * call resetTitleAndRevertLockIcon.
1961 */
1962 /* package */ void resetTitleAndRevertLockIcon() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001963 mTabControl.getCurrentTab().revertLockIcon();
1964 updateLockIconToLatest();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001965 resetTitleIconAndProgress();
1966 }
1967
1968 /**
1969 * Reset the title, favicon, and progress.
1970 */
1971 private void resetTitleIconAndProgress() {
1972 WebView current = mTabControl.getCurrentWebView();
1973 if (current == null) {
1974 return;
1975 }
1976 resetTitleAndIcon(current);
1977 int progress = current.getProgress();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001978 current.getWebChromeClient().onProgressChanged(current, progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001979 }
1980
1981 // Reset the title and the icon based on the given item.
1982 private void resetTitleAndIcon(WebView view) {
1983 WebHistoryItem item = view.copyBackForwardList().getCurrentItem();
1984 if (item != null) {
Leon Scroggins68579392009-09-15 15:31:54 -04001985 setUrlTitle(item.getUrl(), item.getTitle());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001986 setFavicon(item.getFavicon());
1987 } else {
Leon Scroggins68579392009-09-15 15:31:54 -04001988 setUrlTitle(null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001989 setFavicon(null);
1990 }
1991 }
1992
1993 /**
1994 * Sets a title composed of the URL and the title string.
1995 * @param url The URL of the site being loaded.
1996 * @param title The title of the site being loaded.
1997 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07001998 void setUrlTitle(String url, String title) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001999 mUrl = url;
2000 mTitle = title;
2001
Leon Scroggins58d56c62010-01-28 15:12:40 -05002002 // If we are in voice search mode, the title has already been set.
2003 if (mTabControl.getCurrentTab().isInVoiceSearchMode()) return;
2004 mTitleBar.setDisplayTitle(url);
2005 mFakeTitleBar.setDisplayTitle(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002006 }
2007
2008 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08002009 * @param url The URL to build a title version of the URL from.
2010 * @return The title version of the URL or null if fails.
2011 * The title version of the URL can be either the URL hostname,
2012 * or the hostname with an "https://" prefix (for secure URLs),
2013 * or an empty string if, for example, the URL in question is a
2014 * file:// URL with no hostname.
2015 */
Leon Scroggins32e14a62009-06-11 10:26:34 -04002016 /* package */ static String buildTitleUrl(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002017 String titleUrl = null;
2018
2019 if (url != null) {
2020 try {
2021 // parse the url string
2022 URL urlObj = new URL(url);
2023 if (urlObj != null) {
2024 titleUrl = "";
2025
2026 String protocol = urlObj.getProtocol();
2027 String host = urlObj.getHost();
2028
2029 if (host != null && 0 < host.length()) {
2030 titleUrl = host;
2031 if (protocol != null) {
2032 // if a secure site, add an "https://" prefix!
2033 if (protocol.equalsIgnoreCase("https")) {
2034 titleUrl = protocol + "://" + host;
2035 }
2036 }
2037 }
2038 }
2039 } catch (MalformedURLException e) {}
2040 }
2041
2042 return titleUrl;
2043 }
2044
2045 // Set the favicon in the title bar.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002046 void setFavicon(Bitmap icon) {
Leon Scroggins68579392009-09-15 15:31:54 -04002047 mTitleBar.setFavicon(icon);
Leon Scrogginsfe87bd32009-10-06 10:10:00 -04002048 mFakeTitleBar.setFavicon(icon);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002049 }
2050
2051 /**
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002052 * Close the tab, remove its associated title bar, and adjust mTabControl's
2053 * current tab to a valid value.
Leon Scroggins1f005d32009-08-10 17:36:42 -04002054 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002055 /* package */ void closeTab(Tab t) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002056 int currentIndex = mTabControl.getCurrentIndex();
2057 int removeIndex = mTabControl.getTabIndex(t);
Leon Scroggins1f005d32009-08-10 17:36:42 -04002058 mTabControl.removeTab(t);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002059 if (currentIndex >= removeIndex && currentIndex != 0) {
2060 currentIndex--;
2061 }
2062 mTabControl.setCurrentTab(mTabControl.getTab(currentIndex));
Andrei Popescua5bf1de2009-09-23 16:39:23 +01002063 resetTitleIconAndProgress();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002064 }
2065
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05002066 /* package */ void goBackOnePageOrQuit() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002067 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002068 if (current == null) {
2069 /*
2070 * Instead of finishing the activity, simply push this to the back
2071 * of the stack and let ActivityManager to choose the foreground
2072 * activity. As BrowserActivity is singleTask, it will be always the
2073 * root of the task. So we can use either true or false for
2074 * moveTaskToBack().
2075 */
2076 moveTaskToBack(true);
Grace Kloba00d85e72009-09-23 18:50:05 -07002077 return;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002078 }
2079 WebView w = current.getWebView();
2080 if (w.canGoBack()) {
2081 w.goBack();
2082 } else {
2083 // Check to see if we are closing a window that was created by
2084 // another window. If so, we switch back to that window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002085 Tab parent = current.getParentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002086 if (parent != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04002087 switchToTab(mTabControl.getTabIndex(parent));
2088 // Now we close the other tab
2089 closeTab(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002090 } else {
2091 if (current.closeOnExit()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002092 // force the tab's inLoad() to be false as we are going to
2093 // either finish the activity or remove the tab. This will
2094 // ensure pauseWebViewTimers() taking action.
2095 mTabControl.getCurrentTab().clearInLoad();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002096 if (mTabControl.getTabCount() == 1) {
2097 finish();
2098 return;
2099 }
Mike Reed7bfa63b2009-05-28 11:08:32 -04002100 // call pauseWebViewTimers() now, we won't be able to call
2101 // it in onPause() as the WebView won't be valid.
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002102 // Temporarily change mActivityInPause to be true as
2103 // pauseWebViewTimers() will do nothing if mActivityInPause
2104 // is false.
Grace Kloba918e1d72009-08-13 14:55:06 -07002105 boolean savedState = mActivityInPause;
2106 if (savedState) {
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002107 Log.e(LOGTAG, "BrowserActivity is already paused "
2108 + "while handing goBackOnePageOrQuit.");
Grace Kloba918e1d72009-08-13 14:55:06 -07002109 }
2110 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -04002111 pauseWebViewTimers();
Grace Kloba918e1d72009-08-13 14:55:06 -07002112 mActivityInPause = savedState;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002113 removeTabFromContentView(current);
2114 mTabControl.removeTab(current);
2115 }
2116 /*
2117 * Instead of finishing the activity, simply push this to the back
2118 * of the stack and let ActivityManager to choose the foreground
2119 * activity. As BrowserActivity is singleTask, it will be always the
2120 * root of the task. So we can use either true or false for
2121 * moveTaskToBack().
2122 */
2123 moveTaskToBack(true);
2124 }
2125 }
2126 }
2127
Grace Kloba22ac16e2009-10-07 18:00:23 -07002128 boolean isMenuDown() {
2129 return mMenuIsDown;
2130 }
2131
Grace Kloba5942df02009-09-18 11:48:29 -07002132 @Override
2133 public boolean onKeyDown(int keyCode, KeyEvent event) {
Leon Scrogginsf65b50d2009-12-08 10:44:28 -05002134 // Even if MENU is already held down, we need to call to super to open
2135 // the IME on long press.
2136 if (KeyEvent.KEYCODE_MENU == keyCode) {
2137 mMenuIsDown = true;
2138 return super.onKeyDown(keyCode, event);
2139 }
Grace Kloba5942df02009-09-18 11:48:29 -07002140 // The default key mode is DEFAULT_KEYS_SEARCH_LOCAL. As the MENU is
2141 // still down, we don't want to trigger the search. Pretend to consume
2142 // the key and do nothing.
2143 if (mMenuIsDown) return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002144
Grace Kloba5942df02009-09-18 11:48:29 -07002145 switch(keyCode) {
Grace Kloba5942df02009-09-18 11:48:29 -07002146 case KeyEvent.KEYCODE_SPACE:
Grace Klobada0fe552009-09-22 18:17:24 -07002147 // WebView/WebTextView handle the keys in the KeyDown. As
2148 // the Activity's shortcut keys are only handled when WebView
2149 // doesn't, have to do it in onKeyDown instead of onKeyUp.
2150 if (event.isShiftPressed()) {
2151 getTopWindow().pageUp(false);
2152 } else {
2153 getTopWindow().pageDown(false);
2154 }
Grace Kloba5942df02009-09-18 11:48:29 -07002155 return true;
2156 case KeyEvent.KEYCODE_BACK:
2157 if (event.getRepeatCount() == 0) {
2158 event.startTracking();
2159 return true;
2160 } else if (mCustomView == null && mActiveTabsPage == null
2161 && event.isLongPress()) {
2162 bookmarksOrHistoryPicker(true);
2163 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002164 }
Grace Kloba5942df02009-09-18 11:48:29 -07002165 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002166 }
Grace Kloba5942df02009-09-18 11:48:29 -07002167 return super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002168 }
2169
Grace Kloba5942df02009-09-18 11:48:29 -07002170 @Override
2171 public boolean onKeyUp(int keyCode, KeyEvent event) {
2172 switch(keyCode) {
2173 case KeyEvent.KEYCODE_MENU:
2174 mMenuIsDown = false;
2175 break;
Grace Kloba5942df02009-09-18 11:48:29 -07002176 case KeyEvent.KEYCODE_BACK:
2177 if (event.isTracking() && !event.isCanceled()) {
2178 if (mCustomView != null) {
2179 // if a custom view is showing, hide it
Grace Kloba22ac16e2009-10-07 18:00:23 -07002180 mTabControl.getCurrentWebView().getWebChromeClient()
2181 .onHideCustomView();
Grace Kloba5942df02009-09-18 11:48:29 -07002182 } else if (mActiveTabsPage != null) {
2183 // if tab page is showing, hide it
2184 removeActiveTabPage(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002185 } else {
Grace Kloba5942df02009-09-18 11:48:29 -07002186 WebView subwindow = mTabControl.getCurrentSubWindow();
2187 if (subwindow != null) {
2188 if (subwindow.canGoBack()) {
2189 subwindow.goBack();
2190 } else {
2191 dismissSubWindow(mTabControl.getCurrentTab());
2192 }
2193 } else {
2194 goBackOnePageOrQuit();
2195 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002196 }
Grace Kloba5942df02009-09-18 11:48:29 -07002197 return true;
2198 }
2199 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002200 }
Grace Kloba5942df02009-09-18 11:48:29 -07002201 return super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002202 }
2203
Leon Scroggins68579392009-09-15 15:31:54 -04002204 /* package */ void stopLoading() {
Ben Murdochb7cc8b42009-09-28 10:59:09 +01002205 mDidStopLoad = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002206 resetTitleAndRevertLockIcon();
2207 WebView w = getTopWindow();
2208 w.stopLoading();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002209 // FIXME: before refactor, it is using mWebViewClient. So I keep the
2210 // same logic here. But for subwindow case, should we call into the main
2211 // WebView's onPageFinished as we never call its onPageStarted and if
2212 // the page finishes itself, we don't call onPageFinished.
2213 mTabControl.getCurrentWebView().getWebViewClient().onPageFinished(w,
2214 w.getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002215
2216 cancelStopToast();
2217 mStopToast = Toast
2218 .makeText(this, R.string.stopping, Toast.LENGTH_SHORT);
2219 mStopToast.show();
2220 }
2221
Grace Kloba22ac16e2009-10-07 18:00:23 -07002222 boolean didUserStopLoading() {
2223 return mDidStopLoad;
2224 }
2225
The Android Open Source Project0c908882009-03-03 19:32:16 -08002226 private void cancelStopToast() {
2227 if (mStopToast != null) {
2228 mStopToast.cancel();
2229 mStopToast = null;
2230 }
2231 }
2232
Grace Kloba22ac16e2009-10-07 18:00:23 -07002233 // called by a UI or non-UI thread to post the message
2234 public void postMessage(int what, int arg1, int arg2, Object obj,
2235 long delayMillis) {
2236 mHandler.sendMessageDelayed(mHandler.obtainMessage(what, arg1, arg2,
2237 obj), delayMillis);
2238 }
2239
2240 // called by a UI or non-UI thread to remove the message
2241 void removeMessages(int what, Object object) {
2242 mHandler.removeMessages(what, object);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002243 }
2244
2245 // public message ids
2246 public final static int LOAD_URL = 1001;
2247 public final static int STOP_LOAD = 1002;
2248
2249 // Message Ids
2250 private static final int FOCUS_NODE_HREF = 102;
Grace Kloba92c18a52009-07-31 23:48:32 -07002251 private static final int RELEASE_WAKELOCK = 107;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002252
Grace Kloba22ac16e2009-10-07 18:00:23 -07002253 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
Ben Murdoch2694e232009-09-29 09:41:11 +01002254
The Android Open Source Project0c908882009-03-03 19:32:16 -08002255 // Private handler for handling javascript and saving passwords
2256 private Handler mHandler = new Handler() {
2257
2258 public void handleMessage(Message msg) {
2259 switch (msg.what) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002260 case FOCUS_NODE_HREF:
Ben Murdoch2694e232009-09-29 09:41:11 +01002261 {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002262 String url = (String) msg.getData().get("url");
Ben Murdoch90d088c2009-11-17 18:14:04 +00002263 String title = (String) msg.getData().get("title");
The Android Open Source Project0c908882009-03-03 19:32:16 -08002264 if (url == null || url.length() == 0) {
2265 break;
2266 }
2267 HashMap focusNodeMap = (HashMap) msg.obj;
2268 WebView view = (WebView) focusNodeMap.get("webview");
2269 // Only apply the action if the top window did not change.
2270 if (getTopWindow() != view) {
2271 break;
2272 }
2273 switch (msg.arg1) {
2274 case R.id.open_context_menu_id:
2275 case R.id.view_image_context_menu_id:
Leon Scroggins92472e82010-02-17 16:32:28 -05002276 loadUrlFromContext(getTopWindow(), url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002277 break;
2278 case R.id.open_newtab_context_menu_id:
Grace Kloba22ac16e2009-10-07 18:00:23 -07002279 final Tab parent = mTabControl.getCurrentTab();
2280 final Tab newTab = openTab(url);
Grace Klobac9181842009-04-14 08:53:22 -07002281 if (newTab != parent) {
2282 parent.addChildTab(newTab);
2283 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002284 break;
2285 case R.id.bookmark_context_menu_id:
2286 Intent intent = new Intent(BrowserActivity.this,
2287 AddBookmarkPage.class);
2288 intent.putExtra("url", url);
Ben Murdoch90d088c2009-11-17 18:14:04 +00002289 intent.putExtra("title", title);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002290 startActivity(intent);
2291 break;
2292 case R.id.share_link_context_menu_id:
Leon Scroggins96afcb12009-12-10 12:35:56 -05002293 // See if this site has been visited before
2294 StringBuilder sb = new StringBuilder(
2295 Browser.BookmarkColumns.URL + " = ");
2296 DatabaseUtils.appendEscapedSQLString(sb, url);
2297 Cursor c = mResolver.query(Browser.BOOKMARKS_URI,
2298 Browser.HISTORY_PROJECTION,
2299 sb.toString(),
2300 null,
2301 null);
2302 if (c.moveToFirst()) {
2303 // The site has been visited before, so grab the
2304 // info from the database.
2305 Bitmap favicon = null;
2306 Bitmap thumbnail = null;
2307 String linkTitle = c.getString(Browser.
2308 HISTORY_PROJECTION_TITLE_INDEX);
2309 byte[] data = c.getBlob(Browser.
2310 HISTORY_PROJECTION_FAVICON_INDEX);
2311 if (data != null) {
2312 favicon = BitmapFactory.decodeByteArray(
2313 data, 0, data.length);
2314 }
2315 data = c.getBlob(Browser.
2316 HISTORY_PROJECTION_THUMBNAIL_INDEX);
2317 if (data != null) {
2318 thumbnail = BitmapFactory.decodeByteArray(
2319 data, 0, data.length);
2320 }
2321 sharePage(BrowserActivity.this,
2322 linkTitle, url, favicon, thumbnail);
2323 } else {
2324 Browser.sendString(BrowserActivity.this, url,
2325 getString(
2326 R.string.choosertitle_sharevia));
2327 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002328 break;
2329 case R.id.copy_link_context_menu_id:
2330 copy(url);
2331 break;
2332 case R.id.save_link_context_menu_id:
2333 case R.id.download_context_menu_id:
2334 onDownloadStartNoStream(url, null, null, null, -1);
2335 break;
2336 }
2337 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002338 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002339
2340 case LOAD_URL:
Leon Scroggins92472e82010-02-17 16:32:28 -05002341 loadUrlFromContext(getTopWindow(), (String) msg.obj);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002342 break;
2343
2344 case STOP_LOAD:
2345 stopLoading();
2346 break;
2347
The Android Open Source Project0c908882009-03-03 19:32:16 -08002348 case RELEASE_WAKELOCK:
2349 if (mWakeLock.isHeld()) {
2350 mWakeLock.release();
Grace Kloba5d0e02e2009-10-05 15:15:36 -07002351 // if we reach here, Browser should be still in the
2352 // background loading after WAKELOCK_TIMEOUT (5-min).
2353 // To avoid burning the battery, stop loading.
2354 mTabControl.stopAllLoading();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002355 }
2356 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002357
2358 case UPDATE_BOOKMARK_THUMBNAIL:
2359 WebView view = (WebView) msg.obj;
2360 if (view != null) {
2361 updateScreenshot(view);
2362 }
2363 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002364 }
2365 }
2366 };
2367
Leon Scroggins96afcb12009-12-10 12:35:56 -05002368 /**
2369 * Share a page, providing the title, url, favicon, and a screenshot. Uses
2370 * an {@link Intent} to launch the Activity chooser.
2371 * @param c Context used to launch a new Activity.
2372 * @param title Title of the page. Stored in the Intent with
Paul Westbrook03e6d392010-02-12 10:33:29 -08002373 * {@link Intent#EXTRA_SUBJECT}
Leon Scroggins96afcb12009-12-10 12:35:56 -05002374 * @param url URL of the page. Stored in the Intent with
2375 * {@link Intent#EXTRA_TEXT}
2376 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
2377 * with {@link Browser#EXTRA_SHARE_FAVICON}
2378 * @param screenshot Bitmap of a screenshot of the page. Stored in the
2379 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
2380 */
2381 public static final void sharePage(Context c, String title, String url,
2382 Bitmap favicon, Bitmap screenshot) {
2383 Intent send = new Intent(Intent.ACTION_SEND);
2384 send.setType("text/plain");
2385 send.putExtra(Intent.EXTRA_TEXT, url);
Paul Westbrook03e6d392010-02-12 10:33:29 -08002386 send.putExtra(Intent.EXTRA_SUBJECT, title);
Leon Scroggins96afcb12009-12-10 12:35:56 -05002387 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
2388 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
2389 try {
2390 c.startActivity(Intent.createChooser(send, c.getString(
2391 R.string.choosertitle_sharevia)));
2392 } catch(android.content.ActivityNotFoundException ex) {
2393 // if no app handles it, do nothing
2394 }
2395 }
2396
Leon Scroggins89c6d362009-07-15 16:54:37 -04002397 private void updateScreenshot(WebView view) {
2398 // If this is a bookmarked site, add a screenshot to the database.
2399 // FIXME: When should we update? Every time?
2400 // FIXME: Would like to make sure there is actually something to
2401 // draw, but the API for that (WebViewCore.pictureReady()) is not
2402 // currently accessible here.
Ben Murdochaac7aa62009-09-17 16:57:40 +01002403
Patrick Scottcb192b52010-04-14 14:38:55 -04002404 final Bitmap bm = createScreenshot(view);
2405 if (bm == null) {
2406 return;
Leon Scroggins89c6d362009-07-15 16:54:37 -04002407 }
Patrick Scottcb192b52010-04-14 14:38:55 -04002408
2409 final ContentResolver cr = getContentResolver();
2410 final String url = view.getUrl();
2411 final String originalUrl = view.getOriginalUrl();
2412
2413 new AsyncTask<Void, Void, Void>() {
2414 @Override
2415 protected Void doInBackground(Void... unused) {
2416 Cursor c = null;
2417 try {
2418 c = BrowserBookmarksAdapter.queryBookmarksForUrl(
2419 cr, originalUrl, url, true);
2420 if (c != null) {
2421 if (c.moveToFirst()) {
2422 ContentValues values = new ContentValues();
2423 final ByteArrayOutputStream os
2424 = new ByteArrayOutputStream();
2425 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
2426 values.put(Browser.BookmarkColumns.THUMBNAIL,
2427 os.toByteArray());
2428 do {
2429 cr.update(ContentUris.withAppendedId(
2430 Browser.BOOKMARKS_URI, c.getInt(0)),
2431 values, null, null);
2432 } while (c.moveToNext());
2433 }
2434 }
2435 } catch (IllegalStateException e) {
2436 // Ignore
2437 } finally {
2438 if (c != null) c.close();
2439 }
2440 return null;
2441 }
2442 }.execute();
Leon Scroggins89c6d362009-07-15 16:54:37 -04002443 }
2444
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002445 /**
Leon Scrogginsf8551612009-09-24 16:06:02 -04002446 * Values for the size of the thumbnail created when taking a screenshot.
2447 * Lazily initialized. Instead of using these directly, use
2448 * getDesiredThumbnailWidth() or getDesiredThumbnailHeight().
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002449 */
Leon Scrogginsf8551612009-09-24 16:06:02 -04002450 private static int THUMBNAIL_WIDTH = 0;
2451 private static int THUMBNAIL_HEIGHT = 0;
2452
2453 /**
2454 * Return the desired width for thumbnail screenshots, which are stored in
2455 * the database, and used on the bookmarks screen.
2456 * @param context Context for finding out the density of the screen.
2457 * @return int desired width for thumbnail screenshot.
2458 */
2459 /* package */ static int getDesiredThumbnailWidth(Context context) {
2460 if (THUMBNAIL_WIDTH == 0) {
2461 float density = context.getResources().getDisplayMetrics().density;
2462 THUMBNAIL_WIDTH = (int) (90 * density);
2463 THUMBNAIL_HEIGHT = (int) (80 * density);
2464 }
2465 return THUMBNAIL_WIDTH;
2466 }
2467
2468 /**
2469 * Return the desired height for thumbnail screenshots, which are stored in
2470 * the database, and used on the bookmarks screen.
2471 * @param context Context for finding out the density of the screen.
2472 * @return int desired height for thumbnail screenshot.
2473 */
2474 /* package */ static int getDesiredThumbnailHeight(Context context) {
2475 // To ensure that they are both initialized.
2476 getDesiredThumbnailWidth(context);
2477 return THUMBNAIL_HEIGHT;
2478 }
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002479
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002480 private Bitmap createScreenshot(WebView view) {
2481 Picture thumbnail = view.capturePicture();
Leon Scroggins45800572009-09-29 16:38:47 -04002482 if (thumbnail == null) {
2483 return null;
2484 }
Leon Scrogginsf8551612009-09-24 16:06:02 -04002485 Bitmap bm = Bitmap.createBitmap(getDesiredThumbnailWidth(this),
Cary Clarkab168ba2010-04-08 09:11:28 -04002486 getDesiredThumbnailHeight(this), Bitmap.Config.RGB_565);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002487 Canvas canvas = new Canvas(bm);
2488 // May need to tweak these values to determine what is the
2489 // best scale factor
Ben Murdoch2694e232009-09-29 09:41:11 +01002490 int thumbnailWidth = thumbnail.getWidth();
Ben Murdochae59c3f2009-10-20 18:30:28 +01002491 int thumbnailHeight = thumbnail.getHeight();
2492 float scaleFactorX = 1.0f;
2493 float scaleFactorY = 1.0f;
Ben Murdoch2694e232009-09-29 09:41:11 +01002494 if (thumbnailWidth > 0) {
Ben Murdochae59c3f2009-10-20 18:30:28 +01002495 scaleFactorX = (float) getDesiredThumbnailWidth(this) /
Ben Murdoch2694e232009-09-29 09:41:11 +01002496 (float)thumbnailWidth;
Ben Murdochae59c3f2009-10-20 18:30:28 +01002497 } else {
2498 return null;
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002499 }
Ben Murdochae59c3f2009-10-20 18:30:28 +01002500
2501 if (view.getWidth() > view.getHeight() &&
2502 thumbnailHeight < view.getHeight() && thumbnailHeight > 0) {
2503 // If the device is in landscape and the page is shorter
2504 // than the height of the view, stretch the thumbnail to fill the
2505 // space.
2506 scaleFactorY = (float) getDesiredThumbnailHeight(this) /
2507 (float)thumbnailHeight;
2508 } else {
2509 // In the portrait case, this looks nice.
2510 scaleFactorY = scaleFactorX;
2511 }
2512
2513 canvas.scale(scaleFactorX, scaleFactorY);
2514
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002515 thumbnail.draw(canvas);
2516 return bm;
2517 }
2518
The Android Open Source Project0c908882009-03-03 19:32:16 -08002519 // -------------------------------------------------------------------------
Grace Kloba22ac16e2009-10-07 18:00:23 -07002520 // Helper function for WebViewClient.
The Android Open Source Project0c908882009-03-03 19:32:16 -08002521 //-------------------------------------------------------------------------
2522
2523 // Use in overrideUrlLoading
2524 /* package */ final static String SCHEME_WTAI = "wtai://wp/";
2525 /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
2526 /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;";
2527 /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;";
2528
Leon Scroggins92472e82010-02-17 16:32:28 -05002529 // Keep this initial progress in sync with initialProgressValue (* 100)
2530 // in ProgressTracker.cpp
2531 private final static int INITIAL_PROGRESS = 10;
2532
Grace Kloba22ac16e2009-10-07 18:00:23 -07002533 void onPageStarted(WebView view, String url, Bitmap favicon) {
2534 // when BrowserActivity just starts, onPageStarted may be called before
2535 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
2536 // to start the timer. As we won't switch tabs while an activity is in
2537 // pause state, we can ensure calling resume and pause in pair.
2538 if (mActivityInPause) resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002539
Grace Kloba22ac16e2009-10-07 18:00:23 -07002540 resetLockIcon(url);
2541 setUrlTitle(url, null);
2542 setFavicon(favicon);
Leon Scroggins8cf8f682009-11-04 11:13:50 -08002543 // Show some progress so that the user knows the page is beginning to
2544 // load
Leon Scroggins92472e82010-02-17 16:32:28 -05002545 onProgressChanged(view, INITIAL_PROGRESS);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002546 mDidStopLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07002547 if (!mIsNetworkUp) createAndShowNetworkDialog();
Cary Clark2c326e62010-08-19 18:40:57 -04002548 closeDialogs();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002549 if (mSettings.isTracing()) {
2550 String host;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002551 try {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002552 WebAddress uri = new WebAddress(url);
2553 host = uri.mHost;
2554 } catch (android.net.ParseException ex) {
2555 host = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -08002556 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002557 host = host.replace('.', '_');
2558 host += ".trace";
2559 mInTrace = true;
2560 Debug.startMethodTracing(host, 20 * 1024 * 1024);
2561 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002562
Grace Kloba22ac16e2009-10-07 18:00:23 -07002563 // Performance probe
2564 if (false) {
2565 mStart = SystemClock.uptimeMillis();
2566 mProcessStart = Process.getElapsedCpuTime();
2567 long[] sysCpu = new long[7];
2568 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2569 sysCpu, null)) {
2570 mUserStart = sysCpu[0] + sysCpu[1];
2571 mSystemStart = sysCpu[2];
2572 mIdleStart = sysCpu[3];
2573 mIrqStart = sysCpu[4] + sysCpu[5] + sysCpu[6];
2574 }
2575 mUiStart = SystemClock.currentThreadTimeMillis();
2576 }
2577 }
2578
2579 void onPageFinished(WebView view, String url) {
2580 // Reset the title and icon in case we stopped a provisional load.
2581 resetTitleAndIcon(view);
2582 // Update the lock icon image only once we are done loading
2583 updateLockIconToLatest();
2584 // pause the WebView timer and release the wake lock if it is finished
2585 // while BrowserActivity is in pause state.
2586 if (mActivityInPause && pauseWebViewTimers()) {
2587 if (mWakeLock.isHeld()) {
2588 mHandler.removeMessages(RELEASE_WAKELOCK);
2589 mWakeLock.release();
2590 }
2591 }
2592
2593 // Performance probe
2594 if (false) {
2595 long[] sysCpu = new long[7];
2596 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2597 sysCpu, null)) {
2598 String uiInfo = "UI thread used "
2599 + (SystemClock.currentThreadTimeMillis() - mUiStart)
2600 + " ms";
2601 if (LOGD_ENABLED) {
2602 Log.d(LOGTAG, uiInfo);
2603 }
2604 //The string that gets written to the log
2605 String performanceString = "It took total "
2606 + (SystemClock.uptimeMillis() - mStart)
2607 + " ms clock time to load the page."
2608 + "\nbrowser process used "
2609 + (Process.getElapsedCpuTime() - mProcessStart)
2610 + " ms, user processes used "
2611 + (sysCpu[0] + sysCpu[1] - mUserStart) * 10
2612 + " ms, kernel used "
2613 + (sysCpu[2] - mSystemStart) * 10
2614 + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10
2615 + " ms and irq took "
2616 + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart)
2617 * 10 + " ms, " + uiInfo;
2618 if (LOGD_ENABLED) {
2619 Log.d(LOGTAG, performanceString + "\nWebpage: " + url);
2620 }
2621 if (url != null) {
2622 // strip the url to maintain consistency
2623 String newUrl = new String(url);
2624 if (newUrl.startsWith("http://www.")) {
2625 newUrl = newUrl.substring(11);
2626 } else if (newUrl.startsWith("http://")) {
2627 newUrl = newUrl.substring(7);
2628 } else if (newUrl.startsWith("https://www.")) {
2629 newUrl = newUrl.substring(12);
2630 } else if (newUrl.startsWith("https://")) {
2631 newUrl = newUrl.substring(8);
2632 }
2633 if (LOGD_ENABLED) {
2634 Log.d(LOGTAG, newUrl + " loaded");
2635 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002636 }
2637 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002638 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002639
Grace Kloba22ac16e2009-10-07 18:00:23 -07002640 if (mInTrace) {
2641 mInTrace = false;
2642 Debug.stopMethodTracing();
2643 }
2644 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002645
Grace Kloba22ac16e2009-10-07 18:00:23 -07002646 boolean shouldOverrideUrlLoading(WebView view, String url) {
2647 if (url.startsWith(SCHEME_WTAI)) {
2648 // wtai://wp/mc;number
2649 // number=string(phone-number)
2650 if (url.startsWith(SCHEME_WTAI_MC)) {
2651 Intent intent = new Intent(Intent.ACTION_VIEW,
2652 Uri.parse(WebView.SCHEME_TEL +
2653 url.substring(SCHEME_WTAI_MC.length())));
2654 startActivity(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002655 return true;
2656 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002657 // wtai://wp/sd;dtmf
2658 // dtmf=string(dialstring)
2659 if (url.startsWith(SCHEME_WTAI_SD)) {
2660 // TODO: only send when there is active voice connection
2661 return false;
2662 }
2663 // wtai://wp/ap;number;name
2664 // number=string(phone-number)
2665 // name=string
2666 if (url.startsWith(SCHEME_WTAI_AP)) {
2667 // TODO
2668 return false;
2669 }
2670 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002671
Grace Kloba22ac16e2009-10-07 18:00:23 -07002672 // The "about:" schemes are internal to the browser; don't want these to
2673 // be dispatched to other apps.
2674 if (url.startsWith("about:")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002675 return false;
2676 }
2677
Grace Kloba22ac16e2009-10-07 18:00:23 -07002678 Intent intent;
2679 // perform generic parsing of the URI to turn it into an Intent.
2680 try {
2681 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
2682 } catch (URISyntaxException ex) {
2683 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
2684 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002685 }
2686
Grace Kloba22ac16e2009-10-07 18:00:23 -07002687 // check whether the intent can be resolved. If not, we will see
2688 // whether we can download it from the Market.
2689 if (getPackageManager().resolveActivity(intent, 0) == null) {
2690 String packagename = intent.getPackage();
2691 if (packagename != null) {
2692 intent = new Intent(Intent.ACTION_VIEW, Uri
2693 .parse("market://search?q=pname:" + packagename));
2694 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2695 startActivity(intent);
2696 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002697 } else {
2698 return false;
2699 }
2700 }
2701
Grace Kloba22ac16e2009-10-07 18:00:23 -07002702 // sanitize the Intent, ensuring web pages can not bypass browser
2703 // security (only access to BROWSABLE activities).
2704 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2705 intent.setComponent(null);
2706 try {
2707 if (startActivityIfNeeded(intent, -1)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002708 return true;
2709 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002710 } catch (ActivityNotFoundException ex) {
2711 // ignore the error. If no application can handle the URL,
2712 // eg about:blank, assume the browser can handle it.
2713 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002714
Grace Kloba22ac16e2009-10-07 18:00:23 -07002715 if (mMenuIsDown) {
2716 openTab(url);
2717 closeOptionsMenu();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002718 return true;
2719 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002720 return false;
2721 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002722
Grace Kloba22ac16e2009-10-07 18:00:23 -07002723 // -------------------------------------------------------------------------
2724 // Helper function for WebChromeClient
2725 // -------------------------------------------------------------------------
The Android Open Source Project0c908882009-03-03 19:32:16 -08002726
Grace Kloba22ac16e2009-10-07 18:00:23 -07002727 void onProgressChanged(WebView view, int newProgress) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002728 mFakeTitleBar.setProgress(newProgress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002729
Grace Kloba22ac16e2009-10-07 18:00:23 -07002730 if (newProgress == 100) {
2731 // onProgressChanged() may continue to be called after the main
2732 // frame has finished loading, as any remaining sub frames continue
2733 // to load. We'll only get called once though with newProgress as
2734 // 100 when everything is loaded. (onPageFinished is called once
2735 // when the main frame completes loading regardless of the state of
2736 // any sub frames so calls to onProgressChanges may continue after
2737 // onPageFinished has executed)
2738 if (mInLoad) {
2739 mInLoad = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -04002740 updateInLoadMenuItems();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002741 // If the options menu is open, leave the title bar
2742 if (!mOptionsMenuOpen || !mIconView) {
2743 hideFakeTitleBar();
The Android Open Source Projectcb9a0bb2009-03-11 12:11:58 -07002744 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002745 }
Leon Scrogginsaa37e7f2010-03-05 11:51:20 -05002746 } else {
2747 if (!mInLoad) {
2748 // onPageFinished may have already been called but a subframe is
2749 // still loading and updating the progress. Reset mInLoad and
2750 // update the menu items.
2751 mInLoad = true;
2752 updateInLoadMenuItems();
2753 }
2754 // When the page first begins to load, the Activity may still be
2755 // paused, in which case showFakeTitleBar will do nothing. Call
2756 // again as the page continues to load so that it will be shown.
2757 // (Calling it will the fake title bar is already showing will also
2758 // do nothing.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002759 if (!mOptionsMenuOpen || mIconView) {
2760 // This page has begun to load, so show the title bar
2761 showFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002762 }
2763 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002764 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002765
Grace Kloba22ac16e2009-10-07 18:00:23 -07002766 void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002767 // if a view already exists then immediately terminate the new one
2768 if (mCustomView != null) {
2769 callback.onCustomViewHidden();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002770 return;
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002771 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002772
2773 // Add the custom view to its container.
2774 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
2775 mCustomView = view;
2776 mCustomViewCallback = callback;
2777 // Save the menu state and set it to empty while the custom
2778 // view is showing.
2779 mOldMenuState = mMenuState;
2780 mMenuState = EMPTY_MENU;
2781 // Hide the content view.
2782 mContentView.setVisibility(View.GONE);
2783 // Finally show the custom view container.
Andrei Popescu163ab742009-10-20 17:58:23 +01002784 setStatusBarVisibility(false);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002785 mCustomViewContainer.setVisibility(View.VISIBLE);
2786 mCustomViewContainer.bringToFront();
2787 }
2788
2789 void onHideCustomView() {
2790 if (mCustomView == null)
2791 return;
2792
2793 // Hide the custom view.
2794 mCustomView.setVisibility(View.GONE);
2795 // Remove the custom view from its container.
2796 mCustomViewContainer.removeView(mCustomView);
2797 mCustomView = null;
2798 // Reset the old menu state.
2799 mMenuState = mOldMenuState;
2800 mOldMenuState = EMPTY_MENU;
2801 mCustomViewContainer.setVisibility(View.GONE);
2802 mCustomViewCallback.onCustomViewHidden();
2803 // Show the content view.
Andrei Popescu163ab742009-10-20 17:58:23 +01002804 setStatusBarVisibility(true);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002805 mContentView.setVisibility(View.VISIBLE);
2806 }
2807
2808 Bitmap getDefaultVideoPoster() {
2809 if (mDefaultVideoPoster == null) {
2810 mDefaultVideoPoster = BitmapFactory.decodeResource(
2811 getResources(), R.drawable.default_video_poster);
Patrick Scott3918d442009-08-04 13:22:29 -04002812 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002813 return mDefaultVideoPoster;
2814 }
Patrick Scott3918d442009-08-04 13:22:29 -04002815
Grace Kloba22ac16e2009-10-07 18:00:23 -07002816 View getVideoLoadingProgressView() {
2817 if (mVideoProgressView == null) {
2818 LayoutInflater inflater = LayoutInflater.from(BrowserActivity.this);
2819 mVideoProgressView = inflater.inflate(
2820 R.layout.video_loading_progress, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002821 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002822 return mVideoProgressView;
2823 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002824
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002825 /*
2826 * The Object used to inform the WebView of the file to upload.
2827 */
2828 private ValueCallback<Uri> mUploadMessage;
2829
Grace Kloba22ac16e2009-10-07 18:00:23 -07002830 void openFileChooser(ValueCallback<Uri> uploadMsg) {
2831 if (mUploadMessage != null) return;
2832 mUploadMessage = uploadMsg;
2833 Intent i = new Intent(Intent.ACTION_GET_CONTENT);
2834 i.addCategory(Intent.CATEGORY_OPENABLE);
2835 i.setType("*/*");
2836 BrowserActivity.this.startActivityForResult(Intent.createChooser(i,
2837 getString(R.string.choose_upload)), FILE_SELECTED);
2838 }
2839
2840 // -------------------------------------------------------------------------
2841 // Implement functions for DownloadListener
2842 // -------------------------------------------------------------------------
2843
The Android Open Source Project0c908882009-03-03 19:32:16 -08002844 /**
2845 * Notify the host application a download should be done, or that
2846 * the data should be streamed if a streaming viewer is available.
2847 * @param url The full url to the content that should be downloaded
2848 * @param contentDisposition Content-disposition http header, if
2849 * present.
2850 * @param mimetype The mimetype of the content reported by the server
2851 * @param contentLength The file size reported by the server
2852 */
2853 public void onDownloadStart(String url, String userAgent,
2854 String contentDisposition, String mimetype, long contentLength) {
2855 // if we're dealing wih A/V content that's not explicitly marked
2856 // for download, check if it's streamable.
2857 if (contentDisposition == null
Patrick Scotte1fb9662009-08-31 14:31:52 -04002858 || !contentDisposition.regionMatches(
2859 true, 0, "attachment", 0, 10)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002860 // query the package manager to see if there's a registered handler
2861 // that matches.
2862 Intent intent = new Intent(Intent.ACTION_VIEW);
2863 intent.setDataAndType(Uri.parse(url), mimetype);
Patrick Scotte1fb9662009-08-31 14:31:52 -04002864 ResolveInfo info = getPackageManager().resolveActivity(intent,
2865 PackageManager.MATCH_DEFAULT_ONLY);
2866 if (info != null) {
2867 ComponentName myName = getComponentName();
2868 // If we resolved to ourselves, we don't want to attempt to
2869 // load the url only to try and download it again.
2870 if (!myName.getPackageName().equals(
2871 info.activityInfo.packageName)
2872 || !myName.getClassName().equals(
2873 info.activityInfo.name)) {
2874 // someone (other than us) knows how to handle this mime
2875 // type with this scheme, don't download.
2876 try {
2877 startActivity(intent);
2878 return;
2879 } catch (ActivityNotFoundException ex) {
2880 if (LOGD_ENABLED) {
2881 Log.d(LOGTAG, "activity not found for " + mimetype
2882 + " over " + Uri.parse(url).getScheme(),
2883 ex);
2884 }
2885 // Best behavior is to fall back to a download in this
2886 // case
The Android Open Source Project0c908882009-03-03 19:32:16 -08002887 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002888 }
2889 }
2890 }
2891 onDownloadStartNoStream(url, userAgent, contentDisposition, mimetype, contentLength);
2892 }
2893
Kristian Monsenfa52d172010-03-25 18:29:21 +00002894 // This is to work around the fact that java.net.URI throws Exceptions
2895 // instead of just encoding URL's properly
2896 // Helper method for onDownloadStartNoStream
2897 private static String encodePath(String path) {
2898 char[] chars = path.toCharArray();
2899
2900 boolean needed = false;
2901 for (char c : chars) {
2902 if (c == '[' || c == ']') {
2903 needed = true;
2904 break;
2905 }
2906 }
2907 if (needed == false) {
2908 return path;
2909 }
2910
2911 StringBuilder sb = new StringBuilder("");
2912 for (char c : chars) {
2913 if (c == '[' || c == ']') {
2914 sb.append('%');
2915 sb.append(Integer.toHexString(c));
2916 } else {
2917 sb.append(c);
2918 }
2919 }
2920
2921 return sb.toString();
2922 }
2923
The Android Open Source Project0c908882009-03-03 19:32:16 -08002924 /**
2925 * Notify the host application a download should be done, even if there
2926 * is a streaming viewer available for thise type.
2927 * @param url The full url to the content that should be downloaded
2928 * @param contentDisposition Content-disposition http header, if
2929 * present.
2930 * @param mimetype The mimetype of the content reported by the server
2931 * @param contentLength The file size reported by the server
2932 */
2933 /*package */ void onDownloadStartNoStream(String url, String userAgent,
2934 String contentDisposition, String mimetype, long contentLength) {
2935
2936 String filename = URLUtil.guessFileName(url,
2937 contentDisposition, mimetype);
2938
2939 // Check to see if we have an SDCard
2940 String status = Environment.getExternalStorageState();
2941 if (!status.equals(Environment.MEDIA_MOUNTED)) {
2942 int title;
2943 String msg;
2944
2945 // Check to see if the SDCard is busy, same as the music app
2946 if (status.equals(Environment.MEDIA_SHARED)) {
2947 msg = getString(R.string.download_sdcard_busy_dlg_msg);
2948 title = R.string.download_sdcard_busy_dlg_title;
2949 } else {
2950 msg = getString(R.string.download_no_sdcard_dlg_msg, filename);
2951 title = R.string.download_no_sdcard_dlg_title;
2952 }
2953
2954 new AlertDialog.Builder(this)
2955 .setTitle(title)
2956 .setIcon(android.R.drawable.ic_dialog_alert)
2957 .setMessage(msg)
2958 .setPositiveButton(R.string.ok, null)
2959 .show();
2960 return;
2961 }
2962
Kristian Monsenfa52d172010-03-25 18:29:21 +00002963 // java.net.URI is a lot stricter than KURL so we have to encode some
2964 // extra characters. Fix for b 2538060 and b 1634719
2965 WebAddress webAddress;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002966 try {
Kristian Monsenfa52d172010-03-25 18:29:21 +00002967 webAddress = new WebAddress(url);
2968 webAddress.mPath = encodePath(webAddress.mPath);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002969 } catch (Exception e) {
Kristian Monsenfa52d172010-03-25 18:29:21 +00002970 // This only happens for very bad urls, we want to chatch the
2971 // exception here
2972 Log.e(LOGTAG, "Exception trying to parse url:" + url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002973 return;
2974 }
2975
2976 // XXX: Have to use the old url since the cookies were stored using the
2977 // old percent-encoded url.
2978 String cookies = CookieManager.getInstance().getCookie(url);
2979
2980 ContentValues values = new ContentValues();
Kristian Monsenfa52d172010-03-25 18:29:21 +00002981 values.put(Downloads.Impl.COLUMN_URI, webAddress.toString());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08002982 values.put(Downloads.Impl.COLUMN_COOKIE_DATA, cookies);
2983 values.put(Downloads.Impl.COLUMN_USER_AGENT, userAgent);
2984 values.put(Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE,
The Android Open Source Project0c908882009-03-03 19:32:16 -08002985 getPackageName());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08002986 values.put(Downloads.Impl.COLUMN_NOTIFICATION_CLASS,
Leon Scrogginsa563d092010-04-19 16:53:49 -04002987 OpenDownloadReceiver.class.getCanonicalName());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08002988 values.put(Downloads.Impl.COLUMN_VISIBILITY,
2989 Downloads.Impl.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
2990 values.put(Downloads.Impl.COLUMN_MIME_TYPE, mimetype);
2991 values.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, filename);
Kristian Monsenfa52d172010-03-25 18:29:21 +00002992 values.put(Downloads.Impl.COLUMN_DESCRIPTION, webAddress.mHost);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002993 if (contentLength > 0) {
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08002994 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, contentLength);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002995 }
2996 if (mimetype == null) {
2997 // We must have long pressed on a link or image to download it. We
2998 // are not sure of the mimetype in this case, so do a head request
2999 new FetchUrlMimeType(this).execute(values);
3000 } else {
3001 final Uri contentUri =
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003002 getContentResolver().insert(Downloads.Impl.CONTENT_URI, values);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003003 }
Leon Scroggins9191a7f2010-03-18 14:44:44 -04003004 Toast.makeText(this, R.string.download_pending, Toast.LENGTH_SHORT)
3005 .show();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003006 }
3007
Grace Kloba22ac16e2009-10-07 18:00:23 -07003008 // -------------------------------------------------------------------------
3009
The Android Open Source Project0c908882009-03-03 19:32:16 -08003010 /**
3011 * Resets the lock icon. This method is called when we start a new load and
3012 * know the url to be loaded.
3013 */
3014 private void resetLockIcon(String url) {
3015 // Save the lock-icon state (we revert to it if the load gets cancelled)
Grace Kloba22ac16e2009-10-07 18:00:23 -07003016 mTabControl.getCurrentTab().resetLockIcon(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003017 updateLockIconImage(LOCK_ICON_UNSECURE);
3018 }
3019
The Android Open Source Project0c908882009-03-03 19:32:16 -08003020 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003021 * Update the lock icon to correspond to our latest state.
3022 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003023 private void updateLockIconToLatest() {
3024 updateLockIconImage(mTabControl.getCurrentTab().getLockIconType());
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003025 }
3026
3027 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08003028 * Updates the lock-icon image in the title-bar.
3029 */
3030 private void updateLockIconImage(int lockIconType) {
3031 Drawable d = null;
3032 if (lockIconType == LOCK_ICON_SECURE) {
3033 d = mSecLockIcon;
3034 } else if (lockIconType == LOCK_ICON_MIXED) {
3035 d = mMixLockIcon;
3036 }
Leon Scroggins68579392009-09-15 15:31:54 -04003037 mTitleBar.setLock(d);
Leon Scrogginsfe87bd32009-10-06 10:10:00 -04003038 mFakeTitleBar.setLock(d);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003039 }
3040
3041 /**
3042 * Displays a page-info dialog.
3043 * @param tab The tab to show info about
3044 * @param fromShowSSLCertificateOnError The flag that indicates whether
3045 * this dialog was opened from the SSL-certificate-on-error dialog or
3046 * not. This is important, since we need to know whether to return to
3047 * the parent dialog or simply dismiss.
3048 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003049 private void showPageInfo(final Tab tab,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003050 final boolean fromShowSSLCertificateOnError) {
3051 final LayoutInflater factory = LayoutInflater
3052 .from(this);
3053
3054 final View pageInfoView = factory.inflate(R.layout.page_info, null);
3055
3056 final WebView view = tab.getWebView();
3057
3058 String url = null;
3059 String title = null;
3060
3061 if (view == null) {
3062 url = tab.getUrl();
3063 title = tab.getTitle();
3064 } else if (view == mTabControl.getCurrentWebView()) {
3065 // Use the cached title and url if this is the current WebView
3066 url = mUrl;
3067 title = mTitle;
3068 } else {
3069 url = view.getUrl();
3070 title = view.getTitle();
3071 }
3072
3073 if (url == null) {
3074 url = "";
3075 }
3076 if (title == null) {
3077 title = "";
3078 }
3079
3080 ((TextView) pageInfoView.findViewById(R.id.address)).setText(url);
3081 ((TextView) pageInfoView.findViewById(R.id.title)).setText(title);
3082
3083 mPageInfoView = tab;
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05003084 mPageInfoFromShowSSLCertificateOnError = fromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003085
3086 AlertDialog.Builder alertDialogBuilder =
3087 new AlertDialog.Builder(this)
3088 .setTitle(R.string.page_info).setIcon(android.R.drawable.ic_dialog_info)
3089 .setView(pageInfoView)
3090 .setPositiveButton(
3091 R.string.ok,
3092 new DialogInterface.OnClickListener() {
3093 public void onClick(DialogInterface dialog,
3094 int whichButton) {
3095 mPageInfoDialog = null;
3096 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003097
3098 // if we came here from the SSL error dialog
3099 if (fromShowSSLCertificateOnError) {
3100 // go back to the SSL error dialog
3101 showSSLCertificateOnError(
3102 mSSLCertificateOnErrorView,
3103 mSSLCertificateOnErrorHandler,
3104 mSSLCertificateOnErrorError);
3105 }
3106 }
3107 })
3108 .setOnCancelListener(
3109 new DialogInterface.OnCancelListener() {
3110 public void onCancel(DialogInterface dialog) {
3111 mPageInfoDialog = null;
3112 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003113
3114 // if we came here from the SSL error dialog
3115 if (fromShowSSLCertificateOnError) {
3116 // go back to the SSL error dialog
3117 showSSLCertificateOnError(
3118 mSSLCertificateOnErrorView,
3119 mSSLCertificateOnErrorHandler,
3120 mSSLCertificateOnErrorError);
3121 }
3122 }
3123 });
3124
3125 // if we have a main top-level page SSL certificate set or a certificate
3126 // error
3127 if (fromShowSSLCertificateOnError ||
3128 (view != null && view.getCertificate() != null)) {
3129 // add a 'View Certificate' button
3130 alertDialogBuilder.setNeutralButton(
3131 R.string.view_certificate,
3132 new DialogInterface.OnClickListener() {
3133 public void onClick(DialogInterface dialog,
3134 int whichButton) {
3135 mPageInfoDialog = null;
3136 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003137
3138 // if we came here from the SSL error dialog
3139 if (fromShowSSLCertificateOnError) {
3140 // go back to the SSL error dialog
3141 showSSLCertificateOnError(
3142 mSSLCertificateOnErrorView,
3143 mSSLCertificateOnErrorHandler,
3144 mSSLCertificateOnErrorError);
3145 } else {
3146 // otherwise, display the top-most certificate from
3147 // the chain
3148 if (view.getCertificate() != null) {
3149 showSSLCertificate(tab);
3150 }
3151 }
3152 }
3153 });
3154 }
3155
3156 mPageInfoDialog = alertDialogBuilder.show();
3157 }
3158
3159 /**
3160 * Displays the main top-level page SSL certificate dialog
3161 * (accessible from the Page-Info dialog).
3162 * @param tab The tab to show certificate for.
3163 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003164 private void showSSLCertificate(final Tab tab) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003165 final View certificateView =
3166 inflateCertificateView(tab.getWebView().getCertificate());
3167 if (certificateView == null) {
3168 return;
3169 }
3170
3171 LayoutInflater factory = LayoutInflater.from(this);
3172
3173 final LinearLayout placeholder =
3174 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3175
3176 LinearLayout ll = (LinearLayout) factory.inflate(
3177 R.layout.ssl_success, placeholder);
3178 ((TextView)ll.findViewById(R.id.success))
3179 .setText(R.string.ssl_certificate_is_valid);
3180
3181 mSSLCertificateView = tab;
3182 mSSLCertificateDialog =
3183 new AlertDialog.Builder(this)
3184 .setTitle(R.string.ssl_certificate).setIcon(
3185 R.drawable.ic_dialog_browser_certificate_secure)
3186 .setView(certificateView)
3187 .setPositiveButton(R.string.ok,
3188 new DialogInterface.OnClickListener() {
3189 public void onClick(DialogInterface dialog,
3190 int whichButton) {
3191 mSSLCertificateDialog = null;
3192 mSSLCertificateView = null;
3193
3194 showPageInfo(tab, false);
3195 }
3196 })
3197 .setOnCancelListener(
3198 new DialogInterface.OnCancelListener() {
3199 public void onCancel(DialogInterface dialog) {
3200 mSSLCertificateDialog = null;
3201 mSSLCertificateView = null;
3202
3203 showPageInfo(tab, false);
3204 }
3205 })
3206 .show();
3207 }
3208
3209 /**
3210 * Displays the SSL error certificate dialog.
3211 * @param view The target web-view.
3212 * @param handler The SSL error handler responsible for cancelling the
3213 * connection that resulted in an SSL error or proceeding per user request.
3214 * @param error The SSL error object.
3215 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003216 void showSSLCertificateOnError(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003217 final WebView view, final SslErrorHandler handler, final SslError error) {
3218
3219 final View certificateView =
3220 inflateCertificateView(error.getCertificate());
3221 if (certificateView == null) {
3222 return;
3223 }
3224
3225 LayoutInflater factory = LayoutInflater.from(this);
3226
3227 final LinearLayout placeholder =
3228 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3229
3230 if (error.hasError(SslError.SSL_UNTRUSTED)) {
3231 LinearLayout ll = (LinearLayout)factory
3232 .inflate(R.layout.ssl_warning, placeholder);
3233 ((TextView)ll.findViewById(R.id.warning))
3234 .setText(R.string.ssl_untrusted);
3235 }
3236
3237 if (error.hasError(SslError.SSL_IDMISMATCH)) {
3238 LinearLayout ll = (LinearLayout)factory
3239 .inflate(R.layout.ssl_warning, placeholder);
3240 ((TextView)ll.findViewById(R.id.warning))
3241 .setText(R.string.ssl_mismatch);
3242 }
3243
3244 if (error.hasError(SslError.SSL_EXPIRED)) {
3245 LinearLayout ll = (LinearLayout)factory
3246 .inflate(R.layout.ssl_warning, placeholder);
3247 ((TextView)ll.findViewById(R.id.warning))
3248 .setText(R.string.ssl_expired);
3249 }
3250
3251 if (error.hasError(SslError.SSL_NOTYETVALID)) {
3252 LinearLayout ll = (LinearLayout)factory
3253 .inflate(R.layout.ssl_warning, placeholder);
3254 ((TextView)ll.findViewById(R.id.warning))
3255 .setText(R.string.ssl_not_yet_valid);
3256 }
3257
3258 mSSLCertificateOnErrorHandler = handler;
3259 mSSLCertificateOnErrorView = view;
3260 mSSLCertificateOnErrorError = error;
3261 mSSLCertificateOnErrorDialog =
3262 new AlertDialog.Builder(this)
3263 .setTitle(R.string.ssl_certificate).setIcon(
3264 R.drawable.ic_dialog_browser_certificate_partially_secure)
3265 .setView(certificateView)
3266 .setPositiveButton(R.string.ok,
3267 new DialogInterface.OnClickListener() {
3268 public void onClick(DialogInterface dialog,
3269 int whichButton) {
3270 mSSLCertificateOnErrorDialog = null;
3271 mSSLCertificateOnErrorView = null;
3272 mSSLCertificateOnErrorHandler = null;
3273 mSSLCertificateOnErrorError = null;
3274
Grace Kloba22ac16e2009-10-07 18:00:23 -07003275 view.getWebViewClient().onReceivedSslError(
3276 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003277 }
3278 })
3279 .setNeutralButton(R.string.page_info_view,
3280 new DialogInterface.OnClickListener() {
3281 public void onClick(DialogInterface dialog,
3282 int whichButton) {
3283 mSSLCertificateOnErrorDialog = null;
3284
3285 // do not clear the dialog state: we will
3286 // need to show the dialog again once the
3287 // user is done exploring the page-info details
3288
3289 showPageInfo(mTabControl.getTabFromView(view),
3290 true);
3291 }
3292 })
3293 .setOnCancelListener(
3294 new DialogInterface.OnCancelListener() {
3295 public void onCancel(DialogInterface dialog) {
3296 mSSLCertificateOnErrorDialog = null;
3297 mSSLCertificateOnErrorView = null;
3298 mSSLCertificateOnErrorHandler = null;
3299 mSSLCertificateOnErrorError = null;
3300
Grace Kloba22ac16e2009-10-07 18:00:23 -07003301 view.getWebViewClient().onReceivedSslError(
3302 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003303 }
3304 })
3305 .show();
3306 }
3307
3308 /**
3309 * Inflates the SSL certificate view (helper method).
3310 * @param certificate The SSL certificate.
3311 * @return The resultant certificate view with issued-to, issued-by,
3312 * issued-on, expires-on, and possibly other fields set.
3313 * If the input certificate is null, returns null.
3314 */
3315 private View inflateCertificateView(SslCertificate certificate) {
3316 if (certificate == null) {
3317 return null;
3318 }
3319
3320 LayoutInflater factory = LayoutInflater.from(this);
3321
3322 View certificateView = factory.inflate(
3323 R.layout.ssl_certificate, null);
3324
3325 // issued to:
3326 SslCertificate.DName issuedTo = certificate.getIssuedTo();
3327 if (issuedTo != null) {
3328 ((TextView) certificateView.findViewById(R.id.to_common))
3329 .setText(issuedTo.getCName());
3330 ((TextView) certificateView.findViewById(R.id.to_org))
3331 .setText(issuedTo.getOName());
3332 ((TextView) certificateView.findViewById(R.id.to_org_unit))
3333 .setText(issuedTo.getUName());
3334 }
3335
3336 // issued by:
3337 SslCertificate.DName issuedBy = certificate.getIssuedBy();
3338 if (issuedBy != null) {
3339 ((TextView) certificateView.findViewById(R.id.by_common))
3340 .setText(issuedBy.getCName());
3341 ((TextView) certificateView.findViewById(R.id.by_org))
3342 .setText(issuedBy.getOName());
3343 ((TextView) certificateView.findViewById(R.id.by_org_unit))
3344 .setText(issuedBy.getUName());
3345 }
3346
3347 // issued on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003348 String issuedOn = formatCertificateDate(
3349 certificate.getValidNotBeforeDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003350 ((TextView) certificateView.findViewById(R.id.issued_on))
3351 .setText(issuedOn);
3352
3353 // expires on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003354 String expiresOn = formatCertificateDate(
3355 certificate.getValidNotAfterDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003356 ((TextView) certificateView.findViewById(R.id.expires_on))
3357 .setText(expiresOn);
3358
3359 return certificateView;
3360 }
3361
3362 /**
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003363 * Formats the certificate date to a properly localized date string.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003364 * @return Properly localized version of the certificate date string and
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003365 * the "" if it fails to localize.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003366 */
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003367 private String formatCertificateDate(Date certificateDate) {
3368 if (certificateDate == null) {
3369 return "";
The Android Open Source Project0c908882009-03-03 19:32:16 -08003370 }
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003371 String formattedDate = DateFormat.getDateFormat(this).format(certificateDate);
3372 if (formattedDate == null) {
3373 return "";
3374 }
3375 return formattedDate;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003376 }
3377
3378 /**
3379 * Displays an http-authentication dialog.
3380 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003381 void showHttpAuthentication(final HttpAuthHandler handler,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003382 final String host, final String realm, final String title,
3383 final String name, final String password, int focusId) {
3384 LayoutInflater factory = LayoutInflater.from(this);
3385 final View v = factory
3386 .inflate(R.layout.http_authentication, null);
3387 if (name != null) {
3388 ((EditText) v.findViewById(R.id.username_edit)).setText(name);
3389 }
3390 if (password != null) {
3391 ((EditText) v.findViewById(R.id.password_edit)).setText(password);
3392 }
3393
3394 String titleText = title;
3395 if (titleText == null) {
3396 titleText = getText(R.string.sign_in_to).toString().replace(
3397 "%s1", host).replace("%s2", realm);
3398 }
3399
3400 mHttpAuthHandler = handler;
3401 AlertDialog dialog = new AlertDialog.Builder(this)
3402 .setTitle(titleText)
3403 .setIcon(android.R.drawable.ic_dialog_alert)
3404 .setView(v)
3405 .setPositiveButton(R.string.action,
3406 new DialogInterface.OnClickListener() {
3407 public void onClick(DialogInterface dialog,
3408 int whichButton) {
3409 String nm = ((EditText) v
3410 .findViewById(R.id.username_edit))
3411 .getText().toString();
3412 String pw = ((EditText) v
3413 .findViewById(R.id.password_edit))
3414 .getText().toString();
3415 BrowserActivity.this.setHttpAuthUsernamePassword
3416 (host, realm, nm, pw);
3417 handler.proceed(nm, pw);
3418 mHttpAuthenticationDialog = null;
3419 mHttpAuthHandler = null;
3420 }})
3421 .setNegativeButton(R.string.cancel,
3422 new DialogInterface.OnClickListener() {
3423 public void onClick(DialogInterface dialog,
3424 int whichButton) {
3425 handler.cancel();
3426 BrowserActivity.this.resetTitleAndRevertLockIcon();
3427 mHttpAuthenticationDialog = null;
3428 mHttpAuthHandler = null;
3429 }})
3430 .setOnCancelListener(new DialogInterface.OnCancelListener() {
3431 public void onCancel(DialogInterface dialog) {
3432 handler.cancel();
3433 BrowserActivity.this.resetTitleAndRevertLockIcon();
3434 mHttpAuthenticationDialog = null;
3435 mHttpAuthHandler = null;
3436 }})
3437 .create();
3438 // Make the IME appear when the dialog is displayed if applicable.
3439 dialog.getWindow().setSoftInputMode(
3440 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
3441 dialog.show();
3442 if (focusId != 0) {
3443 dialog.findViewById(focusId).requestFocus();
3444 } else {
3445 v.findViewById(R.id.username_edit).requestFocus();
3446 }
3447 mHttpAuthenticationDialog = dialog;
3448 }
3449
3450 public int getProgress() {
3451 WebView w = mTabControl.getCurrentWebView();
3452 if (w != null) {
3453 return w.getProgress();
3454 } else {
3455 return 100;
3456 }
3457 }
3458
3459 /**
3460 * Set HTTP authentication password.
3461 *
3462 * @param host The host for the password
3463 * @param realm The realm for the password
3464 * @param username The username for the password. If it is null, it means
3465 * password can't be saved.
3466 * @param password The password
3467 */
3468 public void setHttpAuthUsernamePassword(String host, String realm,
3469 String username,
3470 String password) {
Steve Block95a53b22010-03-25 17:24:58 +00003471 WebView w = getTopWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003472 if (w != null) {
3473 w.setHttpAuthUsernamePassword(host, realm, username, password);
3474 }
3475 }
3476
3477 /**
3478 * connectivity manager says net has come or gone... inform the user
3479 * @param up true if net has come up, false if net has gone down
3480 */
3481 public void onNetworkToggle(boolean up) {
3482 if (up == mIsNetworkUp) {
3483 return;
3484 } else if (up) {
3485 mIsNetworkUp = true;
3486 if (mAlertDialog != null) {
3487 mAlertDialog.cancel();
3488 mAlertDialog = null;
3489 }
3490 } else {
3491 mIsNetworkUp = false;
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003492 if (mInLoad) {
3493 createAndShowNetworkDialog();
3494 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003495 }
3496 WebView w = mTabControl.getCurrentWebView();
3497 if (w != null) {
3498 w.setNetworkAvailable(up);
3499 }
3500 }
3501
Grace Kloba22ac16e2009-10-07 18:00:23 -07003502 boolean isNetworkUp() {
3503 return mIsNetworkUp;
3504 }
3505
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003506 // This method shows the network dialog alerting the user that the net is
3507 // down. It will only show the dialog if mAlertDialog is null.
3508 private void createAndShowNetworkDialog() {
3509 if (mAlertDialog == null) {
3510 mAlertDialog = new AlertDialog.Builder(this)
3511 .setTitle(R.string.loadSuspendedTitle)
3512 .setMessage(R.string.loadSuspended)
3513 .setPositiveButton(R.string.ok, null)
3514 .show();
3515 }
3516 }
3517
The Android Open Source Project0c908882009-03-03 19:32:16 -08003518 @Override
3519 protected void onActivityResult(int requestCode, int resultCode,
3520 Intent intent) {
Grace Klobabb394f32009-11-19 10:26:37 -08003521 if (getTopWindow() == null) return;
3522
The Android Open Source Project0c908882009-03-03 19:32:16 -08003523 switch (requestCode) {
3524 case COMBO_PAGE:
3525 if (resultCode == RESULT_OK && intent != null) {
3526 String data = intent.getAction();
3527 Bundle extras = intent.getExtras();
3528 if (extras != null && extras.getBoolean("new_window", false)) {
Leon Scroggins25d35472009-09-15 11:37:27 -04003529 openTab(data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003530 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -07003531 final Tab currentTab =
The Android Open Source Project0c908882009-03-03 19:32:16 -08003532 mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04003533 dismissSubWindow(currentTab);
3534 if (data != null && data.length() != 0) {
Leon Scroggins92472e82010-02-17 16:32:28 -05003535 loadUrl(getTopWindow(), data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003536 }
3537 }
3538 }
Leon Scrogginsfde97462010-01-11 13:06:21 -05003539 // Deliberately fall through to PREFERENCES_PAGE, since the
3540 // same extra may be attached to the COMBO_PAGE
3541 case PREFERENCES_PAGE:
3542 if (resultCode == RESULT_OK && intent != null) {
3543 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
3544 if (BrowserSettings.PREF_CLEAR_HISTORY.equals(action)) {
3545 mTabControl.removeParentChildRelationShips();
3546 }
3547 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003548 break;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003549 // Choose a file from the file picker.
3550 case FILE_SELECTED:
3551 if (null == mUploadMessage) break;
3552 Uri result = intent == null || resultCode != RESULT_OK ? null
3553 : intent.getData();
3554 mUploadMessage.onReceiveValue(result);
3555 mUploadMessage = null;
3556 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003557 default:
3558 break;
3559 }
Leon Scroggins30444232009-09-04 18:36:20 -04003560 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003561 }
3562
3563 /*
3564 * This method is called as a result of the user selecting the options
Leon Scrogginsf08809b2010-01-21 09:27:46 -05003565 * menu to see the download window. It shows the download window on top of
3566 * the current window.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003567 */
Leon Scrogginsf08809b2010-01-21 09:27:46 -05003568 private void viewDownloads(Uri downloadRecord) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003569 Intent intent = new Intent(this,
3570 BrowserDownloadPage.class);
3571 intent.setData(downloadRecord);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003572 startActivityForResult(intent, BrowserActivity.DOWNLOAD_PAGE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003573
3574 }
3575
Leon Scroggins160a7e72009-08-14 18:28:01 -04003576 /**
3577 * Open the Go page.
3578 * @param startWithHistory If true, open starting on the history tab.
3579 * Otherwise, start with the bookmarks tab.
Leon Scroggins160a7e72009-08-14 18:28:01 -04003580 */
Leon Scroggins30444232009-09-04 18:36:20 -04003581 /* package */ void bookmarksOrHistoryPicker(boolean startWithHistory) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003582 WebView current = mTabControl.getCurrentWebView();
3583 if (current == null) {
3584 return;
3585 }
3586 Intent intent = new Intent(this,
3587 CombinedBookmarkHistoryActivity.class);
3588 String title = current.getTitle();
3589 String url = current.getUrl();
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003590 Bitmap thumbnail = createScreenshot(current);
3591
The Android Open Source Project0c908882009-03-03 19:32:16 -08003592 // Just in case the user opens bookmarks before a page finishes loading
3593 // so the current history item, and therefore the page, is null.
3594 if (null == url) {
3595 url = mLastEnteredUrl;
3596 // This can happen.
3597 if (null == url) {
3598 url = mSettings.getHomePage();
3599 }
3600 }
3601 // In case the web page has not yet received its associated title.
3602 if (title == null) {
3603 title = url;
3604 }
3605 intent.putExtra("title", title);
3606 intent.putExtra("url", url);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003607 intent.putExtra("thumbnail", thumbnail);
Leon Scroggins30444232009-09-04 18:36:20 -04003608 // Disable opening in a new window if we have maxed out the windows
Grace Kloba22ac16e2009-10-07 18:00:23 -07003609 intent.putExtra("disable_new_window", !mTabControl.canCreateNewTab());
Patrick Scott3918d442009-08-04 13:22:29 -04003610 intent.putExtra("touch_icon_url", current.getTouchIconUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003611 if (startWithHistory) {
3612 intent.putExtra(CombinedBookmarkHistoryActivity.STARTING_TAB,
3613 CombinedBookmarkHistoryActivity.HISTORY_TAB);
3614 }
3615 startActivityForResult(intent, COMBO_PAGE);
3616 }
3617
3618 // Called when loading from context menu or LOAD_URL message
Leon Scroggins92472e82010-02-17 16:32:28 -05003619 private void loadUrlFromContext(WebView view, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003620 // In case the user enters nothing.
3621 if (url != null && url.length() != 0 && view != null) {
3622 url = smartUrlFilter(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003623 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
Leon Scroggins92472e82010-02-17 16:32:28 -05003624 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003625 }
3626 }
3627 }
3628
Leon Scroggins92472e82010-02-17 16:32:28 -05003629 /**
3630 * Load the URL into the given WebView and update the title bar
3631 * to reflect the new load. Call this instead of WebView.loadUrl
3632 * directly.
3633 * @param view The WebView used to load url.
3634 * @param url The URL to load.
3635 */
3636 private void loadUrl(WebView view, String url) {
3637 updateTitleBarForNewLoad(view, url);
3638 view.loadUrl(url);
3639 }
3640
3641 /**
3642 * Load UrlData into a Tab and update the title bar to reflect the new
3643 * load. Call this instead of UrlData.loadIn directly.
3644 * @param t The Tab used to load.
3645 * @param data The UrlData being loaded.
3646 */
3647 private void loadUrlDataIn(Tab t, UrlData data) {
3648 updateTitleBarForNewLoad(t.getWebView(), data.mUrl);
3649 data.loadIn(t);
3650 }
3651
3652 /**
3653 * If the WebView is the top window, update the title bar to reflect
3654 * loading the new URL. i.e. set its text, clear the favicon (which
3655 * will be set once the page begins loading), and set the progress to
3656 * INITIAL_PROGRESS to show that the page has begun to load. Called
3657 * by loadUrl and loadUrlDataIn.
3658 * @param view The WebView that is starting a load.
3659 * @param url The URL that is being loaded.
3660 */
3661 private void updateTitleBarForNewLoad(WebView view, String url) {
3662 if (view == getTopWindow()) {
3663 setUrlTitle(url, null);
3664 setFavicon(null);
3665 onProgressChanged(view, INITIAL_PROGRESS);
3666 }
3667 }
3668
The Android Open Source Project0c908882009-03-03 19:32:16 -08003669 private String smartUrlFilter(Uri inUri) {
3670 if (inUri != null) {
3671 return smartUrlFilter(inUri.toString());
3672 }
3673 return null;
3674 }
3675
Feng Qianb34f87a2009-03-24 21:27:26 -07003676 protected static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003677 "(?i)" + // switch on case insensitive matching
3678 "(" + // begin group for schema
3679 "(?:http|https|file):\\/\\/" +
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003680 "|(?:inline|data|about|content|javascript):" +
The Android Open Source Project0c908882009-03-03 19:32:16 -08003681 ")" +
3682 "(.*)" );
3683
3684 /**
3685 * Attempts to determine whether user input is a URL or search
3686 * terms. Anything with a space is passed to search.
3687 *
3688 * Converts to lowercase any mistakenly uppercased schema (i.e.,
3689 * "Http://" converts to "http://"
3690 *
3691 * @return Original or modified URL
3692 *
3693 */
3694 String smartUrlFilter(String url) {
3695
3696 String inUrl = url.trim();
3697 boolean hasSpace = inUrl.indexOf(' ') != -1;
3698
3699 Matcher matcher = ACCEPTED_URI_SCHEMA.matcher(inUrl);
3700 if (matcher.matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003701 // force scheme to lowercase
3702 String scheme = matcher.group(1);
3703 String lcScheme = scheme.toLowerCase();
3704 if (!lcScheme.equals(scheme)) {
Mitsuru Oshima123ecfb2009-05-18 19:11:14 -07003705 inUrl = lcScheme + matcher.group(2);
3706 }
3707 if (hasSpace) {
3708 inUrl = inUrl.replace(" ", "%20");
The Android Open Source Project0c908882009-03-03 19:32:16 -08003709 }
3710 return inUrl;
3711 }
3712 if (hasSpace) {
Satish Sampath565505b2009-05-29 15:37:27 +01003713 // FIXME: Is this the correct place to add to searches?
3714 // what if someone else calls this function?
3715 int shortcut = parseUrlShortcut(inUrl);
3716 if (shortcut != SHORTCUT_INVALID) {
3717 Browser.addSearchUrl(mResolver, inUrl);
3718 String query = inUrl.substring(2);
3719 switch (shortcut) {
3720 case SHORTCUT_GOOGLE_SEARCH:
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003721 return URLUtil.composeSearchUrl(query, QuickSearch_G, QUERY_PLACE_HOLDER);
Satish Sampath565505b2009-05-29 15:37:27 +01003722 case SHORTCUT_WIKIPEDIA_SEARCH:
3723 return URLUtil.composeSearchUrl(query, QuickSearch_W, QUERY_PLACE_HOLDER);
3724 case SHORTCUT_DICTIONARY_SEARCH:
3725 return URLUtil.composeSearchUrl(query, QuickSearch_D, QUERY_PLACE_HOLDER);
3726 case SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH:
The Android Open Source Project0c908882009-03-03 19:32:16 -08003727 // FIXME: we need location in this case
Satish Sampath565505b2009-05-29 15:37:27 +01003728 return URLUtil.composeSearchUrl(query, QuickSearch_L, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003729 }
3730 }
3731 } else {
Dan Egnor5ee906c2009-11-18 12:11:49 -08003732 if (Patterns.WEB_URL.matcher(inUrl).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003733 return URLUtil.guessUrl(inUrl);
3734 }
3735 }
3736
3737 Browser.addSearchUrl(mResolver, inUrl);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003738 return URLUtil.composeSearchUrl(inUrl, QuickSearch_G, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003739 }
3740
Ben Murdochbff2d602009-07-01 20:19:05 +01003741 /* package */ void setShouldShowErrorConsole(boolean flag) {
3742 if (flag == mShouldShowErrorConsole) {
3743 // Nothing to do.
3744 return;
3745 }
Leon Scroggins IIIa9e35b62010-09-16 15:30:15 -04003746 Tab t = mTabControl.getCurrentTab();
3747 if (t == null) {
3748 // There is no current tab so we cannot toggle the error console
3749 return;
3750 }
Ben Murdochbff2d602009-07-01 20:19:05 +01003751
3752 mShouldShowErrorConsole = flag;
3753
Leon Scroggins IIIa9e35b62010-09-16 15:30:15 -04003754 ErrorConsoleView errorConsole = t.getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01003755
3756 if (flag) {
3757 // Setting the show state of the console will cause it's the layout to be inflated.
3758 if (errorConsole.numberOfErrors() > 0) {
3759 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
3760 } else {
3761 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
3762 }
3763
3764 // Now we can add it to the main view.
3765 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08003766 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01003767 ViewGroup.LayoutParams.WRAP_CONTENT));
3768 } else {
3769 mErrorConsoleContainer.removeView(errorConsole);
3770 }
3771
3772 }
3773
Grace Kloba22ac16e2009-10-07 18:00:23 -07003774 boolean shouldShowErrorConsole() {
3775 return mShouldShowErrorConsole;
3776 }
3777
Andrei Popescu163ab742009-10-20 17:58:23 +01003778 private void setStatusBarVisibility(boolean visible) {
3779 int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
3780 getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
3781 }
3782
Andrei Popescu56199cc2010-01-12 22:39:16 +00003783
3784 private void sendNetworkType(String type, String subtype) {
3785 WebView w = mTabControl.getCurrentWebView();
3786 if (w != null) {
3787 w.setNetworkType(type, subtype);
3788 }
3789 }
3790
Andrei Popescu30995e72010-02-09 16:59:58 +00003791 private void packageChanged(String packageName, boolean wasAdded) {
3792 WebView w = mTabControl.getCurrentWebView();
3793 if (w == null) {
3794 return;
3795 }
3796
3797 if (wasAdded) {
3798 w.addPackageName(packageName);
3799 } else {
3800 w.removePackageName(packageName);
3801 }
3802 }
3803
3804 private void addPackageNames(Set<String> packageNames) {
3805 WebView w = mTabControl.getCurrentWebView();
3806 if (w == null) {
3807 return;
3808 }
3809
3810 w.addPackageNames(packageNames);
3811 }
3812
3813 private void getInstalledPackages() {
3814 AsyncTask<Void, Void, Set<String> > task =
3815 new AsyncTask<Void, Void, Set<String> >() {
3816 protected Set<String> doInBackground(Void... unused) {
3817 Set<String> installedPackages = new HashSet<String>();
3818 PackageManager pm = BrowserActivity.this.getPackageManager();
3819 if (pm != null) {
3820 List<PackageInfo> packages = pm.getInstalledPackages(0);
3821 for (PackageInfo p : packages) {
3822 if (BrowserActivity.this.sGoogleApps.contains(p.packageName)) {
3823 installedPackages.add(p.packageName);
3824 }
3825 }
3826 }
3827
3828 return installedPackages;
3829 }
3830
3831 // Executes on the UI thread
3832 protected void onPostExecute(Set<String> installedPackages) {
3833 addPackageNames(installedPackages);
3834 }
3835 };
3836 task.execute();
3837 }
3838
Grace Klobaeb6eef42009-09-15 17:56:32 -07003839 final static int LOCK_ICON_UNSECURE = 0;
3840 final static int LOCK_ICON_SECURE = 1;
3841 final static int LOCK_ICON_MIXED = 2;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003842
The Android Open Source Project0c908882009-03-03 19:32:16 -08003843 private BrowserSettings mSettings;
3844 private TabControl mTabControl;
3845 private ContentResolver mResolver;
3846 private FrameLayout mContentView;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003847 private View mCustomView;
3848 private FrameLayout mCustomViewContainer;
Andrei Popescuc9b55562009-07-07 10:51:15 +01003849 private WebChromeClient.CustomViewCallback mCustomViewCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003850
3851 // FIXME, temp address onPrepareMenu performance problem. When we move everything out of
3852 // view, we should rewrite this.
3853 private int mCurrentMenuState = 0;
3854 private int mMenuState = R.id.MAIN_MENU;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003855 private int mOldMenuState = EMPTY_MENU;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003856 private static final int EMPTY_MENU = -1;
3857 private Menu mMenu;
3858
3859 private FindDialog mFindDialog;
Cary Clark2c326e62010-08-19 18:40:57 -04003860 private SelectDialog mSelectDialog;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003861 // Used to prevent chording to result in firing two shortcuts immediately
3862 // one after another. Fixes bug 1211714.
3863 boolean mCanChord;
3864
3865 private boolean mInLoad;
3866 private boolean mIsNetworkUp;
Ben Murdochb7cc8b42009-09-28 10:59:09 +01003867 private boolean mDidStopLoad;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003868
Cary Clark1f10cbf2010-03-22 11:45:23 -04003869 /* package */ boolean mActivityInPause = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003870
3871 private boolean mMenuIsDown;
3872
The Android Open Source Project0c908882009-03-03 19:32:16 -08003873 private static boolean mInTrace;
3874
3875 // Performance probe
3876 private static final int[] SYSTEM_CPU_FORMAT = new int[] {
3877 Process.PROC_SPACE_TERM | Process.PROC_COMBINE,
3878 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time
3879 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time
3880 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time
3881 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time
3882 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time
3883 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time
3884 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG // 7: softirq time
3885 };
3886
3887 private long mStart;
3888 private long mProcessStart;
3889 private long mUserStart;
3890 private long mSystemStart;
3891 private long mIdleStart;
3892 private long mIrqStart;
3893
3894 private long mUiStart;
3895
3896 private Drawable mMixLockIcon;
3897 private Drawable mSecLockIcon;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003898
3899 /* hold a ref so we can auto-cancel if necessary */
3900 private AlertDialog mAlertDialog;
3901
The Android Open Source Project0c908882009-03-03 19:32:16 -08003902 // The up-to-date URL and title (these can be different from those stored
3903 // in WebView, since it takes some time for the information in WebView to
3904 // get updated)
3905 private String mUrl;
3906 private String mTitle;
3907
3908 // As PageInfo has different style for landscape / portrait, we have
3909 // to re-open it when configuration changed
3910 private AlertDialog mPageInfoDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07003911 private Tab mPageInfoView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003912 // If the Page-Info dialog is launched from the SSL-certificate-on-error
3913 // dialog, we should not just dismiss it, but should get back to the
3914 // SSL-certificate-on-error dialog. This flag is used to store this state
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05003915 private boolean mPageInfoFromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003916
3917 // as SSLCertificateOnError has different style for landscape / portrait,
3918 // we have to re-open it when configuration changed
3919 private AlertDialog mSSLCertificateOnErrorDialog;
3920 private WebView mSSLCertificateOnErrorView;
3921 private SslErrorHandler mSSLCertificateOnErrorHandler;
3922 private SslError mSSLCertificateOnErrorError;
3923
3924 // as SSLCertificate has different style for landscape / portrait, we
3925 // have to re-open it when configuration changed
3926 private AlertDialog mSSLCertificateDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07003927 private Tab mSSLCertificateView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003928
3929 // as HttpAuthentication has different style for landscape / portrait, we
3930 // have to re-open it when configuration changed
3931 private AlertDialog mHttpAuthenticationDialog;
3932 private HttpAuthHandler mHttpAuthHandler;
3933
3934 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
3935 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08003936 ViewGroup.LayoutParams.MATCH_PARENT,
3937 ViewGroup.LayoutParams.MATCH_PARENT);
Andrei Popescuadc008d2009-06-26 14:11:30 +01003938 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
3939 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08003940 ViewGroup.LayoutParams.MATCH_PARENT,
3941 ViewGroup.LayoutParams.MATCH_PARENT,
Andrei Popescuadc008d2009-06-26 14:11:30 +01003942 Gravity.CENTER);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003943 // Google search
3944 final static String QuickSearch_G = "http://www.google.com/m?q=%s";
The Android Open Source Project0c908882009-03-03 19:32:16 -08003945 // Wikipedia search
3946 final static String QuickSearch_W = "http://en.wikipedia.org/w/index.php?search=%s&go=Go";
3947 // Dictionary search
3948 final static String QuickSearch_D = "http://dictionary.reference.com/search?q=%s";
3949 // Google Mobile Local search
3950 final static String QuickSearch_L = "http://www.google.com/m/search?site=local&q=%s&near=mountain+view";
3951
3952 final static String QUERY_PLACE_HOLDER = "%s";
3953
3954 // "source" parameter for Google search through search key
3955 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
3956 // "source" parameter for Google search through goto menu
3957 final static String GOOGLE_SEARCH_SOURCE_GOTO = "browser-goto";
3958 // "source" parameter for Google search through simplily type
3959 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
3960 // "source" parameter for Google search suggested by the browser
3961 final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
3962 // "source" parameter for Google search from unknown source
3963 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown";
3964
3965 private final static String LOGTAG = "browser";
3966
The Android Open Source Project0c908882009-03-03 19:32:16 -08003967 private String mLastEnteredUrl;
3968
3969 private PowerManager.WakeLock mWakeLock;
3970 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
3971
3972 private Toast mStopToast;
3973
Leon Scroggins68579392009-09-15 15:31:54 -04003974 private TitleBar mTitleBar;
Leon Scroggins81db3662009-06-04 17:45:11 -04003975
Ben Murdochbff2d602009-07-01 20:19:05 +01003976 private LinearLayout mErrorConsoleContainer = null;
3977 private boolean mShouldShowErrorConsole = false;
3978
The Android Open Source Project0c908882009-03-03 19:32:16 -08003979 // As the ids are dynamically created, we can't guarantee that they will
3980 // be in sequence, so this static array maps ids to a window number.
3981 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
3982 { R.id.window_one_menu_id, R.id.window_two_menu_id, R.id.window_three_menu_id,
3983 R.id.window_four_menu_id, R.id.window_five_menu_id, R.id.window_six_menu_id,
3984 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
3985
3986 // monitor platform changes
3987 private IntentFilter mNetworkStateChangedFilter;
3988 private BroadcastReceiver mNetworkStateIntentReceiver;
3989
Grace Klobab4da0ad2009-05-14 14:45:40 -07003990 private BroadcastReceiver mPackageInstallationReceiver;
3991
Bjorn Bringerta7611812010-03-24 11:12:02 +00003992 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
3993
The Android Open Source Project0c908882009-03-03 19:32:16 -08003994 // activity requestCode
Nicolas Roard78a98e42009-05-11 13:34:17 +01003995 final static int COMBO_PAGE = 1;
3996 final static int DOWNLOAD_PAGE = 2;
3997 final static int PREFERENCES_PAGE = 3;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003998 final static int FILE_SELECTED = 4;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003999
Andrei Popescu540035d2009-09-18 18:59:20 +01004000 // the default <video> poster
4001 private Bitmap mDefaultVideoPoster;
4002 // the video progress view
4003 private View mVideoProgressView;
4004
Andrei Popescu30995e72010-02-09 16:59:58 +00004005 // The Google packages we monitor for the navigator.isApplicationInstalled()
4006 // API. Add as needed.
4007 private static Set<String> sGoogleApps;
4008 static {
4009 sGoogleApps = new HashSet<String>();
4010 sGoogleApps.add("com.google.android.youtube");
4011 }
4012
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004013 /**
4014 * A UrlData class to abstract how the content will be set to WebView.
4015 * This base class uses loadUrl to show the content.
4016 */
Leon Scroggins6eac63e2010-03-15 18:19:14 -04004017 /* package */ static class UrlData {
Grace Kloba068e48b2010-01-26 18:11:27 -08004018 final String mUrl;
4019 final Map<String, String> mHeaders;
Leon Scroggins58d56c62010-01-28 15:12:40 -05004020 final Intent mVoiceIntent;
Grace Kloba60e095c2009-06-16 11:50:55 -07004021
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004022 UrlData(String url) {
4023 this.mUrl = url;
Grace Kloba068e48b2010-01-26 18:11:27 -08004024 this.mHeaders = null;
Leon Scroggins58d56c62010-01-28 15:12:40 -05004025 this.mVoiceIntent = null;
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004026 }
Grace Kloba60e095c2009-06-16 11:50:55 -07004027
Leon Scroggins58d56c62010-01-28 15:12:40 -05004028 UrlData(String url, Map<String, String> headers, Intent intent) {
Grace Kloba068e48b2010-01-26 18:11:27 -08004029 this.mUrl = url;
4030 this.mHeaders = headers;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -05004031 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
4032 .equals(intent.getAction())) {
Leon Scroggins58d56c62010-01-28 15:12:40 -05004033 this.mVoiceIntent = intent;
4034 } else {
4035 this.mVoiceIntent = null;
4036 }
Grace Kloba60e095c2009-06-16 11:50:55 -07004037 }
4038
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004039 boolean isEmpty() {
Leon Scroggins58d56c62010-01-28 15:12:40 -05004040 return mVoiceIntent == null && (mUrl == null || mUrl.length() == 0);
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004041 }
4042
Leon Scroggins92472e82010-02-17 16:32:28 -05004043 /**
4044 * Load this UrlData into the given Tab. Use loadUrlDataIn to update
4045 * the title bar as well.
4046 */
Leon Scroggins58d56c62010-01-28 15:12:40 -05004047 public void loadIn(Tab t) {
4048 if (mVoiceIntent != null) {
4049 t.activateVoiceSearchMode(mVoiceIntent);
4050 } else {
4051 t.getWebView().loadUrl(mUrl, mHeaders);
4052 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004053 }
4054 };
4055
Leon Scroggins1f005d32009-08-10 17:36:42 -04004056 /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08004057}