blob: aeb8c460de5ce8749c7b33f11250417757482b7c [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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041import android.graphics.Bitmap;
Andrei Popescu540035d2009-09-18 18:59:20 +010042import android.graphics.BitmapFactory;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import android.graphics.Canvas;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import android.graphics.Picture;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040045import android.graphics.PixelFormat;
The Android Open Source Project0c908882009-03-03 19:32:16 -080046import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047import android.net.ConnectivityManager;
Andrei Popescu56199cc2010-01-12 22:39:16 +000048import android.net.NetworkInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049import android.net.Uri;
50import android.net.WebAddress;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051import android.net.http.SslCertificate;
52import android.net.http.SslError;
53import android.os.AsyncTask;
54import android.os.Bundle;
55import android.os.Debug;
56import android.os.Environment;
57import android.os.Handler;
The Android Open Source Project0c908882009-03-03 19:32:16 -080058import android.os.Message;
59import android.os.PowerManager;
60import android.os.Process;
The Android Open Source Project0c908882009-03-03 19:32:16 -080061import android.os.ServiceManager;
62import android.os.SystemClock;
The Android Open Source Project0c908882009-03-03 19:32:16 -080063import android.provider.Browser;
Cary Clark5e335a32009-09-22 14:53:11 -040064import android.provider.ContactsContract;
The Android Open Source Project0c908882009-03-03 19:32:16 -080065import android.provider.Downloads;
66import android.provider.MediaStore;
Michael Kolbe0a36662010-06-29 10:37:12 -070067import android.provider.ContactsContract.Intents.Insert;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050068import android.speech.RecognizerResultsIntent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080069import android.text.IClipboard;
70import android.text.TextUtils;
71import android.text.format.DateFormat;
The Android Open Source Project0c908882009-03-03 19:32:16 -080072import android.util.Log;
Dianne Hackborn385effd2010-02-24 20:03:04 -080073import android.util.Patterns;
The Android Open Source Project0c908882009-03-03 19:32:16 -080074import android.view.ContextMenu;
75import android.view.Gravity;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
79import android.view.MenuInflater;
80import android.view.MenuItem;
81import android.view.View;
82import android.view.ViewGroup;
83import android.view.Window;
84import android.view.WindowManager;
85import android.view.ContextMenu.ContextMenuInfo;
86import android.view.MenuItem.OnMenuItemClickListener;
Svetoslav Ganov2b345992010-05-06 06:13:54 -070087import android.view.accessibility.AccessibilityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080088import android.webkit.CookieManager;
89import android.webkit.CookieSyncManager;
90import android.webkit.DownloadListener;
91import android.webkit.HttpAuthHandler;
Grace Klobab4da0ad2009-05-14 14:45:40 -070092import android.webkit.PluginManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080093import android.webkit.SslErrorHandler;
94import android.webkit.URLUtil;
Leon Clarkecb6cc862009-09-29 18:35:13 +010095import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080096import android.webkit.WebChromeClient;
97import android.webkit.WebHistoryItem;
98import android.webkit.WebIconDatabase;
99import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800100import android.widget.EditText;
101import android.widget.FrameLayout;
102import android.widget.LinearLayout;
103import android.widget.TextView;
104import android.widget.Toast;
105
Bjorn Bringert10d1cca2010-02-10 14:22:12 +0000106import com.android.common.Search;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500107import com.android.common.speech.LoggingEvents;
Dan Egnor5ee906c2009-11-18 12:11:49 -0800108
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400109import java.io.ByteArrayOutputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110import java.io.File;
Ben Murdoch4f75ba22009-10-27 11:48:28 +0000111import java.io.IOException;
112import java.io.InputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800113import java.net.MalformedURLException;
Dianne Hackborn99189432009-06-17 18:06:18 -0700114import java.net.URISyntaxException;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800115import java.net.URL;
116import java.net.URLEncoder;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800117import java.util.Date;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118import java.util.HashMap;
Andrei Popescu30995e72010-02-09 16:59:58 +0000119import java.util.HashSet;
Grace Kloba00f54c52010-01-27 14:53:51 -0800120import java.util.Iterator;
Andrei Popescu30995e72010-02-09 16:59:58 +0000121import java.util.List;
Grace Kloba068e48b2010-01-26 18:11:27 -0800122import java.util.Map;
Andrei Popescu30995e72010-02-09 16:59:58 +0000123import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124import java.util.regex.Matcher;
125import java.util.regex.Pattern;
Ben Murdoch6af492a2010-06-15 12:38:17 +0100126import java.util.Vector;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800127
128public class BrowserActivity extends Activity
Shimeng (Simon) Wang98d5fce2010-03-16 13:23:39 -0700129 implements View.OnCreateContextMenuListener, DownloadListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800130
Dave Bort31a6d1c2009-04-13 15:56:49 -0700131 /* Define some aliases to make these debugging flags easier to refer to.
132 * This file imports android.provider.Browser, so we can't just refer to "Browser.DEBUG".
133 */
134 private final static boolean DEBUG = com.android.browser.Browser.DEBUG;
135 private final static boolean LOGV_ENABLED = com.android.browser.Browser.LOGV_ENABLED;
136 private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
137
Satish Sampath565505b2009-05-29 15:37:27 +0100138 // These are single-character shortcuts for searching popular sources.
139 private static final int SHORTCUT_INVALID = 0;
140 private static final int SHORTCUT_GOOGLE_SEARCH = 1;
141 private static final int SHORTCUT_WIKIPEDIA_SEARCH = 2;
142 private static final int SHORTCUT_DICTIONARY_SEARCH = 3;
143 private static final int SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH = 4;
144
Cary Clarka9771242009-08-11 16:42:26 -0400145 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 @Override
147 public Void doInBackground(File... files) {
148 if (files != null) {
149 for (File f : files) {
Cary Clarkd6be1752009-08-12 12:56:42 -0400150 if (!f.delete()) {
151 Log.e(LOGTAG, f.getPath() + " was not deleted");
152 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800153 }
154 }
155 return null;
156 }
157 }
158
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400159 /**
160 * This layout holds everything you see below the status bar, including the
161 * error console, the custom view container, and the webviews.
162 */
163 private FrameLayout mBrowserFrameLayout;
Leon Scroggins81db3662009-06-04 17:45:11 -0400164
Leon Scrogginsd746a942010-05-19 13:21:44 -0400165 private boolean mXLargeScreenSize;
166
Grace Kloba22ac16e2009-10-07 18:00:23 -0700167 @Override
168 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700169 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800170 Log.v(LOGTAG, this + " onStart");
171 }
172 super.onCreate(icicle);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800173 // test the browser in OpenGL
174 // requestWindowFeature(Window.FEATURE_OPENGL);
175
Mike Reedd334bf52010-01-26 15:21:44 -0500176 // enable this to test the browser in 32bit
177 if (false) {
178 getWindow().setFormat(PixelFormat.RGBX_8888);
179 BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
180 }
181
Svetoslav Ganov2b345992010-05-06 06:13:54 -0700182 if (AccessibilityManager.getInstance(this).isEnabled()) {
183 setDefaultKeyMode(DEFAULT_KEYS_DISABLE);
184 } else {
185 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
186 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800187
188 mResolver = getContentResolver();
189
Grace Kloba0923d692009-09-23 21:37:25 -0700190 // If this was a web search request, pass it on to the default web
191 // search provider and finish this activity.
192 if (handleWebSearchIntent(getIntent())) {
193 finish();
194 return;
195 }
196
The Android Open Source Project0c908882009-03-03 19:32:16 -0800197 mSecLockIcon = Resources.getSystem().getDrawable(
198 android.R.drawable.ic_secure);
199 mMixLockIcon = Resources.getSystem().getDrawable(
200 android.R.drawable.ic_partial_secure);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800201
Leon Scroggins81db3662009-06-04 17:45:11 -0400202 FrameLayout frameLayout = (FrameLayout) getWindow().getDecorView()
203 .findViewById(com.android.internal.R.id.content);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400204 mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(this)
205 .inflate(R.layout.custom_screen, null);
206 mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(
207 R.id.main_content);
208 mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout
209 .findViewById(R.id.error_console);
210 mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
211 .findViewById(R.id.fullscreen_custom_content);
212 frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400213 mXLargeScreenSize = (getResources().getConfiguration().screenLayout
214 & Configuration.SCREENLAYOUT_SIZE_MASK)
215 == Configuration.SCREENLAYOUT_SIZE_XLARGE;
Michael Kolbe0a36662010-06-29 10:37:12 -0700216
Leon Scrogginsd746a942010-05-19 13:21:44 -0400217 if (mXLargeScreenSize) {
Leon Scroggins571b3762010-05-26 10:25:01 -0400218 mTitleBar = new TitleBarXLarge(this);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400219 LinearLayout layout = (LinearLayout) mBrowserFrameLayout.
220 findViewById(R.id.vertical_layout);
221 layout.addView(mTitleBar, 0, new LinearLayout.LayoutParams(
222 ViewGroup.LayoutParams.MATCH_PARENT,
223 ViewGroup.LayoutParams.WRAP_CONTENT));
224 } else {
Leon Scroggins571b3762010-05-26 10:25:01 -0400225 mTitleBar = new TitleBar(this);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400226 // mTitleBar will be always be shown in the fully loaded mode on
227 // phone
228 mTitleBar.setProgress(100);
229 // Fake title bar is not needed in xlarge layout
230 mFakeTitleBar = new TitleBar(this);
231 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800232
233 // Create the tab control and our initial tab
234 mTabControl = new TabControl(this);
235
236 // Open the icon database and retain all the bookmark urls for favicons
237 retainIconsOnStartup();
238
239 // Keep a settings instance handy.
240 mSettings = BrowserSettings.getInstance();
241 mSettings.setTabControl(mTabControl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800242
243 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
244 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
245
Patrick Scott6adacc92010-03-05 08:24:51 -0500246 // Find out if the network is currently up.
247 ConnectivityManager cm = (ConnectivityManager) getSystemService(
248 Context.CONNECTIVITY_SERVICE);
249 NetworkInfo info = cm.getActiveNetworkInfo();
250 if (info != null) {
251 mIsNetworkUp = info.isAvailable();
252 }
253
Grace Klobaa34f6862009-07-31 16:28:17 -0700254 /* enables registration for changes in network status from
255 http stack */
256 mNetworkStateChangedFilter = new IntentFilter();
257 mNetworkStateChangedFilter.addAction(
258 ConnectivityManager.CONNECTIVITY_ACTION);
259 mNetworkStateIntentReceiver = new BroadcastReceiver() {
260 @Override
261 public void onReceive(Context context, Intent intent) {
262 if (intent.getAction().equals(
263 ConnectivityManager.CONNECTIVITY_ACTION)) {
Andrei Popescue4c98462010-02-19 15:44:13 +0000264
265 NetworkInfo info = intent.getParcelableExtra(
266 ConnectivityManager.EXTRA_NETWORK_INFO);
267 String typeName = info.getTypeName();
268 String subtypeName = info.getSubtypeName();
269 sendNetworkType(typeName.toLowerCase(),
270 (subtypeName != null ? subtypeName.toLowerCase() : ""));
271
272 onNetworkToggle(info.isAvailable());
Grace Klobaa34f6862009-07-31 16:28:17 -0700273 }
274 }
275 };
276
Grace Kloba615c6c92009-08-03 10:22:44 -0700277 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
278 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
279 filter.addDataScheme("package");
280 mPackageInstallationReceiver = new BroadcastReceiver() {
281 @Override
282 public void onReceive(Context context, Intent intent) {
283 final String action = intent.getAction();
284 final String packageName = intent.getData()
285 .getSchemeSpecificPart();
286 final boolean replacing = intent.getBooleanExtra(
287 Intent.EXTRA_REPLACING, false);
288 if (Intent.ACTION_PACKAGE_REMOVED.equals(action) && replacing) {
289 // if it is replacing, refreshPlugins() when adding
290 return;
291 }
Andrei Popescu30995e72010-02-09 16:59:58 +0000292
293 if (sGoogleApps.contains(packageName)) {
294 BrowserActivity.this.packageChanged(packageName,
295 Intent.ACTION_PACKAGE_ADDED.equals(action));
296 }
297
Grace Kloba615c6c92009-08-03 10:22:44 -0700298 PackageManager pm = BrowserActivity.this.getPackageManager();
299 PackageInfo pkgInfo = null;
300 try {
301 pkgInfo = pm.getPackageInfo(packageName,
302 PackageManager.GET_PERMISSIONS);
303 } catch (PackageManager.NameNotFoundException e) {
304 return;
305 }
306 if (pkgInfo != null) {
307 String permissions[] = pkgInfo.requestedPermissions;
308 if (permissions == null) {
309 return;
310 }
311 boolean permissionOk = false;
312 for (String permit : permissions) {
313 if (PluginManager.PLUGIN_PERMISSION.equals(permit)) {
314 permissionOk = true;
315 break;
316 }
317 }
318 if (permissionOk) {
319 PluginManager.getInstance(BrowserActivity.this)
Grace Klobae56a0f22010-05-26 17:31:02 -0700320 .refreshPlugins(true);
Grace Kloba615c6c92009-08-03 10:22:44 -0700321 }
322 }
323 }
324 };
325 registerReceiver(mPackageInstallationReceiver, filter);
326
The Android Open Source Project0c908882009-03-03 19:32:16 -0800327 if (!mTabControl.restoreState(icicle)) {
328 // clear up the thumbnail directory if we can't restore the state as
329 // none of the files in the directory are referenced any more.
330 new ClearThumbnails().execute(
331 mTabControl.getThumbnailDir().listFiles());
Grace Klobaaab3f092009-07-30 12:29:51 -0700332 // there is no quit on Android. But if we can't restore the state,
333 // we can treat it as a new Browser, remove the old session cookies.
334 CookieManager.getInstance().removeSessionCookie();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800335 final Intent intent = getIntent();
336 final Bundle extra = intent.getExtras();
337 // Create an initial tab.
338 // If the intent is ACTION_VIEW and data is not null, the Browser is
339 // invoked to view the content by another application. In this case,
340 // the tab will be close when exit.
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700341 UrlData urlData = getUrlDataFromIntent(intent);
342
Leon Scroggins58d56c62010-01-28 15:12:40 -0500343 String action = intent.getAction();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700344 final Tab t = mTabControl.createNewTab(
Leon Scroggins58d56c62010-01-28 15:12:40 -0500345 (Intent.ACTION_VIEW.equals(action) &&
346 intent.getData() != null)
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500347 || RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
348 .equals(action),
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700349 intent.getStringExtra(Browser.EXTRA_APPLICATION_ID), urlData.mUrl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800350 mTabControl.setCurrentTab(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800351 attachTabToContentView(t);
352 WebView webView = t.getWebView();
353 if (extra != null) {
354 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
355 if (scale > 0 && scale <= 1000) {
356 webView.setInitialScale(scale);
357 }
358 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800359
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700360 if (urlData.isEmpty()) {
Shimeng (Simon) Wang98d5fce2010-03-16 13:23:39 -0700361 loadUrl(webView, mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800362 } else {
Patrick Scott9d53da02010-02-19 10:19:01 -0500363 loadUrlDataIn(t, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800364 }
365 } else {
366 // TabControl.restoreState() will create a new tab even if
Leon Scroggins1f005d32009-08-10 17:36:42 -0400367 // restoring the state fails.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800368 attachTabToContentView(mTabControl.getCurrentTab());
369 }
Grace Kloba615c6c92009-08-03 10:22:44 -0700370
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>() {
Michael Kolbe0a36662010-06-29 10:37:12 -0700625 @Override
Leon Scroggins8d06e362010-03-24 14:45:57 -0400626 protected Void doInBackground(Void... unused) {
627 Browser.updateVisitedHistory(cr, newUrl, false);
628 Browser.addSearchUrl(cr, newUrl);
629 return null;
630 }
631 }.execute();
Satish Sampath565505b2009-05-29 15:37:27 +0100632
633 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
634 intent.addCategory(Intent.CATEGORY_DEFAULT);
635 intent.putExtra(SearchManager.QUERY, url);
Satish Sampath15e9f2d2009-06-23 22:29:49 +0100636 if (appData != null) {
637 intent.putExtra(SearchManager.APP_DATA, appData);
638 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100639 if (extraData != null) {
640 intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
641 }
Grace Klobacc634032009-07-28 15:58:19 -0700642 intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName());
Satish Sampath565505b2009-05-29 15:37:27 +0100643 startActivity(intent);
644
645 return true;
646 }
647
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700648 private UrlData getUrlDataFromIntent(Intent intent) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500649 String url = "";
Grace Kloba068e48b2010-01-26 18:11:27 -0800650 Map<String, String> headers = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800651 if (intent != null) {
652 final String action = intent.getAction();
653 if (Intent.ACTION_VIEW.equals(action)) {
654 url = smartUrlFilter(intent.getData());
655 if (url != null && url.startsWith("content:")) {
656 /* Append mimetype so webview knows how to display */
657 String mimeType = intent.resolveType(getContentResolver());
658 if (mimeType != null) {
659 url += "?" + mimeType;
660 }
661 }
Grace Kloba068e48b2010-01-26 18:11:27 -0800662 if (url != null && url.startsWith("http")) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800663 final Bundle pairs = intent
664 .getBundleExtra(Browser.EXTRA_HEADERS);
Grace Kloba2d508ed2010-01-28 11:39:15 -0800665 if (pairs != null && !pairs.isEmpty()) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800666 Iterator<String> iter = pairs.keySet().iterator();
Grace Kloba068e48b2010-01-26 18:11:27 -0800667 headers = new HashMap<String, String>();
Grace Kloba00f54c52010-01-27 14:53:51 -0800668 while (iter.hasNext()) {
669 String key = iter.next();
670 headers.put(key, pairs.getString(key));
Grace Kloba068e48b2010-01-26 18:11:27 -0800671 }
672 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700673 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674 } else if (Intent.ACTION_SEARCH.equals(action)
675 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
676 || Intent.ACTION_WEB_SEARCH.equals(action)) {
677 url = intent.getStringExtra(SearchManager.QUERY);
678 if (url != null) {
679 mLastEnteredUrl = url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800680 // In general, we shouldn't modify URL from Intent.
681 // But currently, we get the user-typed URL from search box as well.
682 url = fixUrl(url);
683 url = smartUrlFilter(url);
Leon Scroggins8d06e362010-03-24 14:45:57 -0400684 final ContentResolver cr = mResolver;
685 final String newUrl = url;
686 new AsyncTask<Void, Void, Void>() {
Michael Kolbe0a36662010-06-29 10:37:12 -0700687 @Override
Leon Scroggins8d06e362010-03-24 14:45:57 -0400688 protected Void doInBackground(Void... unused) {
689 Browser.updateVisitedHistory(cr, newUrl, false);
690 return null;
691 }
692 }.execute();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800693 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
694 if (url.contains(searchSource)) {
695 String source = null;
696 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
697 if (appData != null) {
Bjorn Bringert10d1cca2010-02-10 14:22:12 +0000698 source = appData.getString(Search.SOURCE);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800699 }
700 if (TextUtils.isEmpty(source)) {
701 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
702 }
703 url = url.replace(searchSource, "&source=android-"+source+"&");
704 }
705 }
706 }
707 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500708 return new UrlData(url, headers, intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800709 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500710 /* package */ void showVoiceTitleBar(String title) {
711 mTitleBar.setInVoiceMode(true);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500712 mTitleBar.setDisplayTitle(title);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400713
714 if (!mXLargeScreenSize) {
715 mFakeTitleBar.setInVoiceMode(true);
716 mFakeTitleBar.setDisplayTitle(title);
717 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500718 }
719 /* package */ void revertVoiceTitleBar() {
720 mTitleBar.setInVoiceMode(false);
Leon Scroggins003a5dd2010-03-10 12:13:14 -0500721 mTitleBar.setDisplayTitle(mUrl);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400722
723 if (!mXLargeScreenSize) {
724 mFakeTitleBar.setInVoiceMode(false);
725 mFakeTitleBar.setDisplayTitle(mUrl);
726 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500727 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800728 /* package */ static String fixUrl(String inUrl) {
Cary Clark652ff872009-09-10 13:34:44 -0400729 // FIXME: Converting the url to lower case
730 // duplicates functionality in smartUrlFilter().
731 // However, changing all current callers of fixUrl to
732 // call smartUrlFilter in addition may have unwanted
733 // consequences, and is deferred for now.
734 int colon = inUrl.indexOf(':');
735 boolean allLower = true;
736 for (int index = 0; index < colon; index++) {
737 char ch = inUrl.charAt(index);
738 if (!Character.isLetter(ch)) {
739 break;
740 }
741 allLower &= Character.isLowerCase(ch);
742 if (index == colon - 1 && !allLower) {
743 inUrl = inUrl.substring(0, colon).toLowerCase()
744 + inUrl.substring(colon);
745 }
746 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800747 if (inUrl.startsWith("http://") || inUrl.startsWith("https://"))
748 return inUrl;
749 if (inUrl.startsWith("http:") ||
750 inUrl.startsWith("https:")) {
751 if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) {
752 inUrl = inUrl.replaceFirst("/", "//");
753 } else inUrl = inUrl.replaceFirst(":", "://");
754 }
755 return inUrl;
756 }
757
Grace Kloba22ac16e2009-10-07 18:00:23 -0700758 @Override
759 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800760 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700761 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800762 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
763 }
764
765 if (!mActivityInPause) {
766 Log.e(LOGTAG, "BrowserActivity is already resumed.");
767 return;
768 }
769
Mike Reed7bfa63b2009-05-28 11:08:32 -0400770 mTabControl.resumeCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800771 mActivityInPause = false;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400772 resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800773
774 if (mWakeLock.isHeld()) {
775 mHandler.removeMessages(RELEASE_WAKELOCK);
776 mWakeLock.release();
777 }
778
The Android Open Source Project0c908882009-03-03 19:32:16 -0800779 registerReceiver(mNetworkStateIntentReceiver,
780 mNetworkStateChangedFilter);
781 WebView.enablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800782 }
783
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400784 /**
785 * Since the actual title bar is embedded in the WebView, and removing it
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400786 * would change its appearance, use a different TitleBar to show overlayed
787 * at the top of the screen, when the menu is open or the page is loading.
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400788 */
789 private TitleBar mFakeTitleBar;
790
791 /**
792 * Keeps track of whether the options menu is open. This is important in
793 * determining whether to show or hide the title bar overlay.
794 */
795 private boolean mOptionsMenuOpen;
796
797 /**
798 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
799 * of whether the configuration has changed. The first onMenuOpened call
800 * after a configuration change is simply a reopening of the same menu
801 * (i.e. mIconView did not change).
802 */
803 private boolean mConfigChanged;
804
805 /**
806 * Whether or not the options menu is in its smaller, icon menu form. When
807 * true, we want the title bar overlay to be up. When false, we do not.
808 * Only meaningful if mOptionsMenuOpen is true.
809 */
810 private boolean mIconView;
811
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400812 @Override
813 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400814 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
815 if (mOptionsMenuOpen) {
816 if (mConfigChanged) {
817 // We do not need to make any changes to the state of the
818 // title bar, since the only thing that happened was a
819 // change in orientation
820 mConfigChanged = false;
821 } else {
822 if (mIconView) {
823 // Switching the menu to expanded view, so hide the
824 // title bar.
825 hideFakeTitleBar();
826 mIconView = false;
827 } else {
828 // Switching the menu back to icon view, so show the
829 // title bar once again.
830 showFakeTitleBar();
831 mIconView = true;
832 }
833 }
834 } else {
835 // The options menu is closed, so open it, and show the title
836 showFakeTitleBar();
837 mOptionsMenuOpen = true;
838 mConfigChanged = false;
839 mIconView = true;
840 }
841 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400842 return true;
843 }
844
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400845 private void showFakeTitleBar() {
Leon Scrogginsd746a942010-05-19 13:21:44 -0400846 if (mXLargeScreenSize) return;
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400847 if (mFakeTitleBar.getParent() == null && mActiveTabsPage == null
Grace Kloba847c25b2010-03-30 16:00:26 -0700848 && !mActivityInPause) {
849 WebView mainView = mTabControl.getCurrentWebView();
850 // if there is no current WebView, don't show the faked title bar;
Grace Kloba65190702010-04-02 23:37:26 -0700851 if (mainView == null) {
Cary Clarka0464552009-09-29 13:00:45 -0400852 return;
853 }
Leon Scroggins79e36d92010-04-29 16:01:46 +0100854 // Do not need to check for null, since the current tab will have
855 // at least a main WebView, or we would have returned above.
Cary Clark01cfcdd2010-06-04 16:36:45 -0400856 if (dialogIsUp()) {
Leon Scroggins79e36d92010-04-29 16:01:46 +0100857 // Do not show the fake title bar, which would cover up the
Cary Clark01cfcdd2010-06-04 16:36:45 -0400858 // find or select dialog.
Leon Scroggins79e36d92010-04-29 16:01:46 +0100859 return;
860 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400861
862 WindowManager manager
863 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
864
865 // Add the title bar to the window manager so it can receive touches
866 // while the menu is up
867 WindowManager.LayoutParams params
868 = new WindowManager.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -0800869 ViewGroup.LayoutParams.MATCH_PARENT,
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400870 ViewGroup.LayoutParams.WRAP_CONTENT,
Grace Kloba847c25b2010-03-30 16:00:26 -0700871 WindowManager.LayoutParams.TYPE_APPLICATION,
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400872 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
Leon Scroggins68549862009-09-21 16:02:01 -0400873 PixelFormat.TRANSLUCENT);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400874 params.gravity = Gravity.TOP;
Grace Kloba847c25b2010-03-30 16:00:26 -0700875 boolean atTop = mainView.getScrollY() == 0;
Leon Scroggins83932c72009-09-30 11:55:54 -0400876 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
Grace Kloba13f9ace2010-03-18 21:44:14 -0700877 manager.addView(mFakeTitleBar, params);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400878 }
879 }
880
881 @Override
882 public void onOptionsMenuClosed(Menu menu) {
883 mOptionsMenuOpen = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -0400884 if (!mInLoad) {
885 hideFakeTitleBar();
886 } else if (!mIconView) {
887 // The page is currently loading, and we are in expanded mode, so
888 // we were not showing the menu. Show it once again. It will be
889 // removed when the page finishes.
890 showFakeTitleBar();
891 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400892 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700893
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400894 private void hideFakeTitleBar() {
Leon Scrogginsd746a942010-05-19 13:21:44 -0400895 if (mXLargeScreenSize || mFakeTitleBar.getParent() == null) return;
Leon Scroggins20329572009-09-23 17:42:41 -0400896 WindowManager.LayoutParams params = (WindowManager.LayoutParams)
Grace Kloba13f9ace2010-03-18 21:44:14 -0700897 mFakeTitleBar.getLayoutParams();
Leon Scroggins20329572009-09-23 17:42:41 -0400898 WebView mainView = mTabControl.getCurrentWebView();
899 // Although we decided whether or not to animate based on the current
900 // scroll position, the scroll position may have changed since the
901 // fake title bar was displayed. Make sure it has the appropriate
902 // animation/lack thereof before removing.
903 params.windowAnimations = mainView != null && mainView.getScrollY() == 0
Leon Scroggins83932c72009-09-30 11:55:54 -0400904 ? 0 : R.style.TitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400905 WindowManager manager
906 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Grace Kloba13f9ace2010-03-18 21:44:14 -0700907 manager.updateViewLayout(mFakeTitleBar, params);
908 manager.removeView(mFakeTitleBar);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400909 }
910
The Android Open Source Project0c908882009-03-03 19:32:16 -0800911 /**
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400912 * Special method for the fake title bar to call when displaying its context
913 * menu, since it is in its own Window, and its parent does not show a
914 * context menu.
915 */
916 /* package */ void showTitleBarContextMenu() {
Cary Clark65f4a3c2009-09-28 17:05:06 -0400917 if (null == mTitleBar.getParent()) {
918 return;
919 }
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400920 openContextMenu(mTitleBar);
921 }
922
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400923 @Override
924 public void onContextMenuClosed(Menu menu) {
925 super.onContextMenuClosed(menu);
926 if (mInLoad) {
927 showFakeTitleBar();
928 }
929 }
930
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400931 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800932 * onSaveInstanceState(Bundle map)
933 * onSaveInstanceState is called right before onStop(). The map contains
934 * the saved state.
935 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700936 @Override
937 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700938 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800939 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
940 }
941 // the default implementation requires each view to have an id. As the
942 // browser handles the state itself and it doesn't use id for the views,
943 // don't call the default implementation. Otherwise it will trigger the
944 // warning like this, "couldn't save which view has focus because the
945 // focused view XXX has no id".
946
947 // Save all the tabs
948 mTabControl.saveState(outState);
949 }
950
Grace Kloba22ac16e2009-10-07 18:00:23 -0700951 @Override
952 protected void onPause() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800953 super.onPause();
954
955 if (mActivityInPause) {
956 Log.e(LOGTAG, "BrowserActivity is already paused.");
957 return;
958 }
959
Mike Reed7bfa63b2009-05-28 11:08:32 -0400960 mTabControl.pauseCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800961 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400962 if (mTabControl.getCurrentIndex() >= 0 && !pauseWebViewTimers()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800963 mWakeLock.acquire();
964 mHandler.sendMessageDelayed(mHandler
965 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
966 }
967
Leon Scrogginsa2ab6a72009-09-11 11:49:52 -0400968 // FIXME: This removes the active tabs page and resets the menu to
969 // MAIN_MENU. A better solution might be to do this work in onNewIntent
970 // but then we would need to save it in onSaveInstanceState and restore
971 // it in onCreate/onRestoreInstanceState
972 if (mActiveTabsPage != null) {
973 removeActiveTabPage(true);
974 }
975
The Android Open Source Project0c908882009-03-03 19:32:16 -0800976 cancelStopToast();
977
978 // unregister network state listener
979 unregisterReceiver(mNetworkStateIntentReceiver);
980 WebView.disablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800981 }
982
Grace Kloba22ac16e2009-10-07 18:00:23 -0700983 @Override
984 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700985 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800986 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
987 }
988 super.onDestroy();
Grace Kloba0923d692009-09-23 21:37:25 -0700989
Leon Scroggins8d5fa432009-10-02 15:55:59 -0400990 if (mUploadMessage != null) {
991 mUploadMessage.onReceiveValue(null);
992 mUploadMessage = null;
993 }
994
Grace Kloba0923d692009-09-23 21:37:25 -0700995 if (mTabControl == null) return;
996
Grace Kloba1fc98a32009-10-21 13:23:08 -0700997 // Remove the fake title bar if it is there
998 hideFakeTitleBar();
999
The Android Open Source Project0c908882009-03-03 19:32:16 -08001000 // Remove the current tab and sub window
Grace Kloba22ac16e2009-10-07 18:00:23 -07001001 Tab t = mTabControl.getCurrentTab();
Patrick Scottfb5e77f2009-04-08 19:17:37 -07001002 if (t != null) {
1003 dismissSubWindow(t);
1004 removeTabFromContentView(t);
1005 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001006 // Destroy all the tabs
1007 mTabControl.destroy();
1008 WebIconDatabase.getInstance().close();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001009
Grace Klobab4da0ad2009-05-14 14:45:40 -07001010 unregisterReceiver(mPackageInstallationReceiver);
Bjorn Bringerta7611812010-03-24 11:12:02 +00001011
1012 // Stop watching the default geolocation permissions
1013 mSystemAllowGeolocationOrigins.stop();
1014 mSystemAllowGeolocationOrigins = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001015 }
1016
1017 @Override
1018 public void onConfigurationChanged(Configuration newConfig) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04001019 mConfigChanged = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001020 super.onConfigurationChanged(newConfig);
1021
1022 if (mPageInfoDialog != null) {
1023 mPageInfoDialog.dismiss();
1024 showPageInfo(
1025 mPageInfoView,
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05001026 mPageInfoFromShowSSLCertificateOnError);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001027 }
1028 if (mSSLCertificateDialog != null) {
1029 mSSLCertificateDialog.dismiss();
1030 showSSLCertificate(
1031 mSSLCertificateView);
1032 }
1033 if (mSSLCertificateOnErrorDialog != null) {
1034 mSSLCertificateOnErrorDialog.dismiss();
1035 showSSLCertificateOnError(
1036 mSSLCertificateOnErrorView,
1037 mSSLCertificateOnErrorHandler,
1038 mSSLCertificateOnErrorError);
1039 }
1040 if (mHttpAuthenticationDialog != null) {
1041 String title = ((TextView) mHttpAuthenticationDialog
1042 .findViewById(com.android.internal.R.id.alertTitle)).getText()
1043 .toString();
1044 String name = ((TextView) mHttpAuthenticationDialog
1045 .findViewById(R.id.username_edit)).getText().toString();
1046 String password = ((TextView) mHttpAuthenticationDialog
1047 .findViewById(R.id.password_edit)).getText().toString();
1048 int focusId = mHttpAuthenticationDialog.getCurrentFocus()
1049 .getId();
1050 mHttpAuthenticationDialog.dismiss();
1051 showHttpAuthentication(mHttpAuthHandler, null, null, title,
1052 name, password, focusId);
1053 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001054 }
1055
Grace Kloba22ac16e2009-10-07 18:00:23 -07001056 @Override
1057 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001058 super.onLowMemory();
1059 mTabControl.freeMemory();
1060 }
1061
Cary Clarkff4d92c2010-03-25 11:17:03 -04001062 private void resumeWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001063 Tab tab = mTabControl.getCurrentTab();
Cary Clarkff4d92c2010-03-25 11:17:03 -04001064 if (tab == null) return; // monkey can trigger this
Grace Kloba22ac16e2009-10-07 18:00:23 -07001065 boolean inLoad = tab.inLoad();
1066 if ((!mActivityInPause && !inLoad) || (mActivityInPause && inLoad)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001067 CookieSyncManager.getInstance().startSync();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001068 WebView w = tab.getWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001069 if (w != null) {
1070 w.resumeTimers();
1071 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001072 }
1073 }
1074
Mike Reed7bfa63b2009-05-28 11:08:32 -04001075 private boolean pauseWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001076 Tab tab = mTabControl.getCurrentTab();
1077 boolean inLoad = tab.inLoad();
1078 if (mActivityInPause && !inLoad) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001079 CookieSyncManager.getInstance().stopSync();
1080 WebView w = mTabControl.getCurrentWebView();
1081 if (w != null) {
1082 w.pauseTimers();
1083 }
1084 return true;
1085 } else {
1086 return false;
1087 }
1088 }
1089
The Android Open Source Project0c908882009-03-03 19:32:16 -08001090 // Open the icon database and retain all the icons for visited sites.
1091 private void retainIconsOnStartup() {
1092 final WebIconDatabase db = WebIconDatabase.getInstance();
1093 db.open(getDir("icons", 0).getPath());
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001094 Cursor c = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001095 try {
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001096 c = Browser.getAllBookmarks(mResolver);
1097 if (c.moveToFirst()) {
1098 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
1099 do {
1100 String url = c.getString(urlIndex);
1101 db.retainIconForPageUrl(url);
1102 } while (c.moveToNext());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001103 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001104 } catch (IllegalStateException e) {
1105 Log.e(LOGTAG, "retainIconsOnStartup", e);
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001106 } finally {
1107 if (c!= null) c.close();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001108 }
1109 }
1110
1111 // Helper method for getting the top window.
1112 WebView getTopWindow() {
1113 return mTabControl.getCurrentTopWebView();
1114 }
1115
Grace Kloba22ac16e2009-10-07 18:00:23 -07001116 TabControl getTabControl() {
1117 return mTabControl;
1118 }
1119
The Android Open Source Project0c908882009-03-03 19:32:16 -08001120 @Override
1121 public boolean onCreateOptionsMenu(Menu menu) {
1122 super.onCreateOptionsMenu(menu);
1123
1124 MenuInflater inflater = getMenuInflater();
1125 inflater.inflate(R.menu.browser, menu);
1126 mMenu = menu;
1127 updateInLoadMenuItems();
1128 return true;
1129 }
1130
1131 /**
1132 * As the menu can be open when loading state changes
1133 * we must manually update the state of the stop/reload menu
1134 * item
1135 */
1136 private void updateInLoadMenuItems() {
1137 if (mMenu == null) {
1138 return;
1139 }
Michael Kolbe0a36662010-06-29 10:37:12 -07001140 MenuItem dest = mMenu.findItem(R.id.stop_reload_menu_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001141 MenuItem src = mInLoad ?
1142 mMenu.findItem(R.id.stop_menu_id):
Michael Kolbe0a36662010-06-29 10:37:12 -07001143 mMenu.findItem(R.id.reload_menu_id);
1144 if (src != null) {
1145 dest.setIcon(src.getIcon());
1146 dest.setTitle(src.getTitle());
1147 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001148 }
1149
1150 @Override
1151 public boolean onContextItemSelected(MenuItem item) {
1152 // chording is not an issue with context menus, but we use the same
1153 // options selector, so set mCanChord to true so we can access them.
1154 mCanChord = true;
1155 int id = item.getItemId();
Leon Scroggins96afcb12009-12-10 12:35:56 -05001156 boolean result = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001157 switch (id) {
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001158 // For the context menu from the title bar
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001159 case R.id.title_bar_copy_page_url:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001160 Tab currentTab = mTabControl.getCurrentTab();
1161 if (null == currentTab) {
1162 result = false;
1163 break;
1164 }
1165 WebView mainView = currentTab.getWebView();
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001166 if (null == mainView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001167 result = false;
1168 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001169 }
Leon Scroggins96afcb12009-12-10 12:35:56 -05001170 copy(mainView.getUrl());
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001171 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001172 // -- Browser context menu
1173 case R.id.open_context_menu_id:
The Android Open Source Project0c908882009-03-03 19:32:16 -08001174 case R.id.bookmark_context_menu_id:
1175 case R.id.save_link_context_menu_id:
1176 case R.id.share_link_context_menu_id:
1177 case R.id.copy_link_context_menu_id:
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001178 final WebView webView = getTopWindow();
1179 if (null == webView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001180 result = false;
1181 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001182 }
1183 final HashMap hrefMap = new HashMap();
1184 hrefMap.put("webview", webView);
1185 final Message msg = mHandler.obtainMessage(
1186 FOCUS_NODE_HREF, id, 0, hrefMap);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001187 webView.requestFocusNodeHref(msg);
1188 break;
1189
1190 default:
1191 // For other context menus
Leon Scroggins96afcb12009-12-10 12:35:56 -05001192 result = onOptionsItemSelected(item);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001193 }
1194 mCanChord = false;
Leon Scroggins96afcb12009-12-10 12:35:56 -05001195 return result;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001196 }
1197
1198 private Bundle createGoogleSearchSourceBundle(String source) {
1199 Bundle bundle = new Bundle();
Bjorn Bringert10d1cca2010-02-10 14:22:12 +00001200 bundle.putString(Search.SOURCE, source);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001201 return bundle;
1202 }
1203
Leon Scroggins8ad29922010-02-16 12:33:55 -05001204 /* package */ void editUrl() {
Leon Scroggins68579392009-09-15 15:31:54 -04001205 if (mOptionsMenuOpen) closeOptionsMenu();
Leon Scroggins5bbe9802009-07-31 13:10:55 -04001206 String url = (getTopWindow() == null) ? null : getTopWindow().getUrl();
Grace Kloba83f47342009-07-20 10:44:31 -07001207 startSearch(mSettings.getHomePage().equals(url) ? null : url, true,
Leon Scroggins8ad29922010-02-16 12:33:55 -05001208 null, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001209 }
1210
Leon Scroggins8ad29922010-02-16 12:33:55 -05001211 /**
1212 * Overriding this to insert a local information bundle
1213 */
The Android Open Source Project0c908882009-03-03 19:32:16 -08001214 @Override
1215 public void startSearch(String initialQuery, boolean selectInitialQuery,
1216 Bundle appSearchData, boolean globalSearch) {
1217 if (appSearchData == null) {
1218 appSearchData = createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_TYPE);
1219 }
1220 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1221 }
1222
Leon Scroggins1f005d32009-08-10 17:36:42 -04001223 /**
1224 * Switch tabs. Called by the TitleBarSet when sliding the title bar
1225 * results in changing tabs.
Leon Scroggins160a7e72009-08-14 18:28:01 -04001226 * @param index Index of the tab to change to, as defined by
1227 * mTabControl.getTabIndex(Tab t).
1228 * @return boolean True if we successfully switched to a different tab. If
1229 * the indexth tab is null, or if that tab is the same as
1230 * the current one, return false.
Leon Scroggins1f005d32009-08-10 17:36:42 -04001231 */
Leon Scroggins160a7e72009-08-14 18:28:01 -04001232 /* package */ boolean switchToTab(int index) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001233 Tab tab = mTabControl.getTab(index);
1234 Tab currentTab = mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001235 if (tab == null || tab == currentTab) {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001236 return false;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001237 }
1238 if (currentTab != null) {
1239 // currentTab may be null if it was just removed. In that case,
1240 // we do not need to remove it
1241 removeTabFromContentView(currentTab);
1242 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001243 mTabControl.setCurrentTab(tab);
1244 attachTabToContentView(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001245 resetTitleIconAndProgress();
1246 updateLockIconToLatest();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001247 return true;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001248 }
1249
Grace Kloba22ac16e2009-10-07 18:00:23 -07001250 /* package */ Tab openTabToHomePage() {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001251 return openTabAndShow(mSettings.getHomePage(), false, null);
1252 }
1253
Leon Scroggins1f005d32009-08-10 17:36:42 -04001254 /* package */ void closeCurrentWindow() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001255 final Tab current = mTabControl.getCurrentTab();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001256 if (mTabControl.getTabCount() == 1) {
Leon Scroggins30444232009-09-04 18:36:20 -04001257 // This is the last tab. Open a new one, with the home
1258 // page and close the current one.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001259 openTabToHomePage();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001260 closeTab(current);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001261 return;
1262 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001263 final Tab parent = current.getParentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001264 int indexToShow = -1;
1265 if (parent != null) {
1266 indexToShow = mTabControl.getTabIndex(parent);
1267 } else {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001268 final int currentIndex = mTabControl.getCurrentIndex();
1269 // Try to move to the tab to the right
1270 indexToShow = currentIndex + 1;
1271 if (indexToShow > mTabControl.getTabCount() - 1) {
1272 // Try to move to the tab to the left
1273 indexToShow = currentIndex - 1;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001274 }
1275 }
Leon Scroggins160a7e72009-08-14 18:28:01 -04001276 if (switchToTab(indexToShow)) {
1277 // Close window
1278 closeTab(current);
1279 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001280 }
1281
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001282 private ActiveTabsPage mActiveTabsPage;
1283
1284 /**
1285 * Remove the active tabs page.
1286 * @param needToAttach If true, the active tabs page did not attach a tab
1287 * to the content view, so we need to do that here.
1288 */
1289 /* package */ void removeActiveTabPage(boolean needToAttach) {
1290 mContentView.removeView(mActiveTabsPage);
Leon Scrogginsd746a942010-05-19 13:21:44 -04001291 mTitleBar.setVisibility(View.VISIBLE);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001292 mActiveTabsPage = null;
1293 mMenuState = R.id.MAIN_MENU;
1294 if (needToAttach) {
1295 attachTabToContentView(mTabControl.getCurrentTab());
1296 }
1297 getTopWindow().requestFocus();
1298 }
1299
Cary Clark01cfcdd2010-06-04 16:36:45 -04001300 private WebView showDialog(WebDialog dialog) {
1301 // Need to do something special for Tablet
1302 Tab tab = mTabControl.getCurrentTab();
1303 if (tab.getSubWebView() == null) {
1304 // If the find or select is being performed on the main webview,
1305 // remove the embedded title bar.
1306 WebView mainView = tab.getWebView();
1307 if (mainView != null) {
1308 mainView.setEmbeddedTitleBar(null);
1309 }
1310 }
1311 hideFakeTitleBar();
1312 mMenuState = EMPTY_MENU;
1313 return tab.showDialog(dialog);
1314 }
1315
The Android Open Source Project0c908882009-03-03 19:32:16 -08001316 @Override
1317 public boolean onOptionsItemSelected(MenuItem item) {
1318 if (!mCanChord) {
1319 // The user has already fired a shortcut with this hold down of the
1320 // menu key.
1321 return false;
1322 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001323 if (null == getTopWindow()) {
Leon Scroggins0d7ae0e2009-06-05 11:04:45 -04001324 return false;
1325 }
Grace Kloba6ee9c492009-07-13 10:04:34 -07001326 if (mMenuIsDown) {
1327 // The shortcut action consumes the MENU. Even if it is still down,
1328 // it won't trigger the next shortcut action. In the case of the
1329 // shortcut action triggering a new activity, like Bookmarks, we
1330 // won't get onKeyUp for MENU. So it is important to reset it here.
1331 mMenuIsDown = false;
1332 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001333 switch (item.getItemId()) {
1334 // -- Main menu
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001335 case R.id.new_tab_menu_id:
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001336 openTabToHomePage();
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001337 break;
1338
Leon Scroggins64b80f32009-08-07 12:03:34 -04001339 case R.id.goto_menu_id:
Leon Scroggins8ad29922010-02-16 12:33:55 -05001340 editUrl();
Leon Scrogginsb3a5bed2009-09-28 11:21:56 -04001341 break;
1342
1343 case R.id.bookmarks_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001344 bookmarksOrHistoryPicker(false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001345 break;
1346
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001347 case R.id.active_tabs_menu_id:
1348 mActiveTabsPage = new ActiveTabsPage(this, mTabControl);
1349 removeTabFromContentView(mTabControl.getCurrentTab());
Leon Scrogginsd746a942010-05-19 13:21:44 -04001350 mTitleBar.setVisibility(View.GONE);
Leon Scroggins43de6162009-09-14 19:59:58 -04001351 hideFakeTitleBar();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001352 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
1353 mActiveTabsPage.requestFocus();
1354 mMenuState = EMPTY_MENU;
1355 break;
1356
Leon Scroggins1f005d32009-08-10 17:36:42 -04001357 case R.id.add_bookmark_menu_id:
Leon Scroggins571b3762010-05-26 10:25:01 -04001358 bookmarkCurrentPage();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001359 break;
1360
1361 case R.id.stop_reload_menu_id:
1362 if (mInLoad) {
1363 stopLoading();
1364 } else {
1365 getTopWindow().reload();
1366 }
1367 break;
1368
1369 case R.id.back_menu_id:
1370 getTopWindow().goBack();
1371 break;
1372
1373 case R.id.forward_menu_id:
1374 getTopWindow().goForward();
1375 break;
1376
1377 case R.id.close_menu_id:
1378 // Close the subwindow if it exists.
1379 if (mTabControl.getCurrentSubWindow() != null) {
1380 dismissSubWindow(mTabControl.getCurrentTab());
1381 break;
1382 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001383 closeCurrentWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001384 break;
1385
1386 case R.id.homepage_menu_id:
Grace Kloba22ac16e2009-10-07 18:00:23 -07001387 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001388 if (current != null) {
1389 dismissSubWindow(current);
Leon Scroggins92472e82010-02-17 16:32:28 -05001390 loadUrl(current.getWebView(), mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001391 }
1392 break;
1393
1394 case R.id.preferences_menu_id:
1395 Intent intent = new Intent(this,
1396 BrowserPreferencesPage.class);
Leon Scrogginsd5304942009-12-10 16:11:39 -05001397 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1398 getTopWindow().getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001399 startActivityForResult(intent, PREFERENCES_PAGE);
1400 break;
1401
1402 case R.id.find_menu_id:
Cary Clark01cfcdd2010-06-04 16:36:45 -04001403 showFindDialog();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001404 break;
1405
The Android Open Source Project0c908882009-03-03 19:32:16 -08001406 case R.id.page_info_menu_id:
1407 showPageInfo(mTabControl.getCurrentTab(), false);
1408 break;
1409
1410 case R.id.classic_history_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001411 bookmarksOrHistoryPicker(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001412 break;
1413
Leon Scroggins96afcb12009-12-10 12:35:56 -05001414 case R.id.title_bar_share_page_url:
The Android Open Source Project0c908882009-03-03 19:32:16 -08001415 case R.id.share_page_menu_id:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001416 Tab currentTab = mTabControl.getCurrentTab();
1417 if (null == currentTab) {
1418 mCanChord = false;
1419 return false;
1420 }
1421 currentTab.populatePickerData();
1422 sharePage(this, currentTab.getTitle(),
1423 currentTab.getUrl(), currentTab.getFavicon(),
Ben Murdoch87cc65d2010-06-29 20:34:10 +01001424 createScreenshot(currentTab.getWebView(), getDesiredThumbnailWidth(this),
1425 getDesiredThumbnailHeight(this)));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001426 break;
1427
1428 case R.id.dump_nav_menu_id:
1429 getTopWindow().debugDump();
1430 break;
1431
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001432 case R.id.dump_counters_menu_id:
1433 getTopWindow().dumpV8Counters();
1434 break;
1435
The Android Open Source Project0c908882009-03-03 19:32:16 -08001436 case R.id.zoom_in_menu_id:
1437 getTopWindow().zoomIn();
1438 break;
1439
1440 case R.id.zoom_out_menu_id:
1441 getTopWindow().zoomOut();
1442 break;
1443
1444 case R.id.view_downloads_menu_id:
1445 viewDownloads(null);
1446 break;
1447
The Android Open Source Project0c908882009-03-03 19:32:16 -08001448 case R.id.window_one_menu_id:
1449 case R.id.window_two_menu_id:
1450 case R.id.window_three_menu_id:
1451 case R.id.window_four_menu_id:
1452 case R.id.window_five_menu_id:
1453 case R.id.window_six_menu_id:
1454 case R.id.window_seven_menu_id:
1455 case R.id.window_eight_menu_id:
1456 {
1457 int menuid = item.getItemId();
1458 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1459 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001460 Tab desiredTab = mTabControl.getTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001461 if (desiredTab != null &&
1462 desiredTab != mTabControl.getCurrentTab()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001463 switchToTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001464 }
1465 break;
1466 }
1467 }
1468 }
1469 break;
1470
1471 default:
1472 if (!super.onOptionsItemSelected(item)) {
1473 return false;
1474 }
1475 // Otherwise fall through.
1476 }
1477 mCanChord = false;
1478 return true;
1479 }
1480
Leon Scroggins571b3762010-05-26 10:25:01 -04001481 /* package */ void bookmarkCurrentPage() {
1482 Intent i = new Intent(BrowserActivity.this,
1483 AddBookmarkPage.class);
1484 WebView w = getTopWindow();
1485 i.putExtra("url", w.getUrl());
1486 i.putExtra("title", w.getTitle());
1487 i.putExtra("touch_icon_url", w.getTouchIconUrl());
Ben Murdoch87cc65d2010-06-29 20:34:10 +01001488 i.putExtra("thumbnail", createScreenshot(w, getDesiredThumbnailWidth(this),
1489 getDesiredThumbnailHeight(this)));
Ben Murdocheecb4e62010-07-06 16:30:38 +01001490 i.putExtra("url_editable", false);
Leon Scroggins571b3762010-05-26 10:25:01 -04001491 startActivity(i);
1492 }
1493
Cary Clark01cfcdd2010-06-04 16:36:45 -04001494 private boolean dialogIsUp() {
1495 return null != mFindDialog && mFindDialog.isVisible() ||
1496 null != mSelectDialog && mSelectDialog.isVisible();
1497 }
1498
1499 private boolean closeDialog(WebDialog dialog) {
1500 if (null == dialog || !dialog.isVisible()) return false;
Leon Scroggins79e36d92010-04-29 16:01:46 +01001501 Tab currentTab = mTabControl.getCurrentTab();
Cary Clark01cfcdd2010-06-04 16:36:45 -04001502 currentTab.closeDialog(dialog);
1503 dialog.dismiss();
1504 return true;
1505 }
1506
1507 /*
1508 * Remove the find dialog or select dialog.
1509 */
1510 public void closeDialogs() {
1511 if (!(closeDialog(mFindDialog) || closeDialog(mSelectDialog))) return;
Leon Scrogginsd746a942010-05-19 13:21:44 -04001512 if (!mXLargeScreenSize) {
1513 // If the Find was being performed in the main WebView, replace the
1514 // embedded title bar.
Cary Clark01cfcdd2010-06-04 16:36:45 -04001515 Tab currentTab = mTabControl.getCurrentTab();
Leon Scrogginsd746a942010-05-19 13:21:44 -04001516 if (currentTab.getSubWebView() == null) {
1517 WebView mainView = currentTab.getWebView();
1518 if (mainView != null) {
1519 mainView.setEmbeddedTitleBar(mTitleBar);
1520 }
Leon Scroggins79e36d92010-04-29 16:01:46 +01001521 }
1522 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001523 mMenuState = R.id.MAIN_MENU;
Leon Scroggins79e36d92010-04-29 16:01:46 +01001524 if (mInLoad) {
1525 // The title bar was hidden, because otherwise it would cover up the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001526 // find or select dialog. Now that the dialog has been removed,
1527 // show the fake title bar once again.
Leon Scroggins79e36d92010-04-29 16:01:46 +01001528 showFakeTitleBar();
1529 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001530 }
1531
Cary Clark01cfcdd2010-06-04 16:36:45 -04001532 public void showFindDialog() {
1533 if (null == mFindDialog) {
1534 mFindDialog = new FindDialog(this);
1535 }
1536 showDialog(mFindDialog).setFindIsUp(true);
1537 }
1538
1539 public void setFindDialogText(String text) {
1540 mFindDialog.setText(text);
1541 }
1542
1543 public void showSelectDialog() {
1544 if (null == mSelectDialog) {
1545 mSelectDialog = new SelectDialog(this);
1546 }
1547 showDialog(mSelectDialog).setUpSelect();
1548 mSelectDialog.hideSoftInput();
1549 }
1550
Grace Kloba22ac16e2009-10-07 18:00:23 -07001551 @Override
1552 public boolean onPrepareOptionsMenu(Menu menu) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001553 // This happens when the user begins to hold down the menu key, so
1554 // allow them to chord to get a shortcut.
1555 mCanChord = true;
1556 // Note: setVisible will decide whether an item is visible; while
1557 // setEnabled() will decide whether an item is enabled, which also means
1558 // whether the matching shortcut key will function.
1559 super.onPrepareOptionsMenu(menu);
1560 switch (mMenuState) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001561 case EMPTY_MENU:
1562 if (mCurrentMenuState != mMenuState) {
1563 menu.setGroupVisible(R.id.MAIN_MENU, false);
1564 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1565 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001566 }
1567 break;
1568 default:
1569 if (mCurrentMenuState != mMenuState) {
1570 menu.setGroupVisible(R.id.MAIN_MENU, true);
1571 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1572 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001573 }
1574 final WebView w = getTopWindow();
1575 boolean canGoBack = false;
1576 boolean canGoForward = false;
1577 boolean isHome = false;
1578 if (w != null) {
1579 canGoBack = w.canGoBack();
1580 canGoForward = w.canGoForward();
1581 isHome = mSettings.getHomePage().equals(w.getUrl());
1582 }
1583 final MenuItem back = menu.findItem(R.id.back_menu_id);
1584 back.setEnabled(canGoBack);
1585
1586 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1587 home.setEnabled(!isHome);
1588
Michael Kolbe0a36662010-06-29 10:37:12 -07001589 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1590 forward.setEnabled(canGoForward);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001591
Michael Kolbe0a36662010-06-29 10:37:12 -07001592 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
1593 newtab.setEnabled(mTabControl.canCreateNewTab());
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001594
The Android Open Source Project0c908882009-03-03 19:32:16 -08001595 // decide whether to show the share link option
1596 PackageManager pm = getPackageManager();
1597 Intent send = new Intent(Intent.ACTION_SEND);
1598 send.setType("text/plain");
1599 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1600 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1601
The Android Open Source Project0c908882009-03-03 19:32:16 -08001602 boolean isNavDump = mSettings.isNavDump();
1603 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1604 nav.setVisible(isNavDump);
1605 nav.setEnabled(isNavDump);
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001606
1607 boolean showDebugSettings = mSettings.showDebugSettings();
1608 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1609 counter.setVisible(showDebugSettings);
1610 counter.setEnabled(showDebugSettings);
1611
The Android Open Source Project0c908882009-03-03 19:32:16 -08001612 break;
1613 }
1614 mCurrentMenuState = mMenuState;
1615 return true;
1616 }
1617
1618 @Override
1619 public void onCreateContextMenu(ContextMenu menu, View v,
1620 ContextMenuInfo menuInfo) {
Leon Scroggins571b3762010-05-26 10:25:01 -04001621 if (v instanceof TitleBarBase) {
Leon Scroggins4e9f89b2010-02-22 16:54:14 -05001622 return;
1623 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001624 WebView webview = (WebView) v;
1625 WebView.HitTestResult result = webview.getHitTestResult();
1626 if (result == null) {
1627 return;
1628 }
1629
1630 int type = result.getType();
1631 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1632 Log.w(LOGTAG,
1633 "We should not show context menu when nothing is touched");
1634 return;
1635 }
1636 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1637 // let TextView handles context menu
1638 return;
1639 }
1640
1641 // Note, http://b/issue?id=1106666 is requesting that
1642 // an inflated menu can be used again. This is not available
1643 // yet, so inflate each time (yuk!)
1644 MenuInflater inflater = getMenuInflater();
1645 inflater.inflate(R.menu.browsercontext, menu);
1646
1647 // Show the correct menu group
Leon Scroggins III9e997c72010-05-26 13:25:16 -04001648 final String extra = result.getExtra();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001649 menu.setGroupVisible(R.id.PHONE_MENU,
1650 type == WebView.HitTestResult.PHONE_TYPE);
1651 menu.setGroupVisible(R.id.EMAIL_MENU,
1652 type == WebView.HitTestResult.EMAIL_TYPE);
1653 menu.setGroupVisible(R.id.GEO_MENU,
1654 type == WebView.HitTestResult.GEO_TYPE);
1655 menu.setGroupVisible(R.id.IMAGE_MENU,
1656 type == WebView.HitTestResult.IMAGE_TYPE
1657 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1658 menu.setGroupVisible(R.id.ANCHOR_MENU,
1659 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1660 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1661
1662 // Setup custom handling depending on the type
1663 switch (type) {
1664 case WebView.HitTestResult.PHONE_TYPE:
1665 menu.setHeaderTitle(Uri.decode(extra));
1666 menu.findItem(R.id.dial_context_menu_id).setIntent(
1667 new Intent(Intent.ACTION_VIEW, Uri
1668 .parse(WebView.SCHEME_TEL + extra)));
1669 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1670 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
Cary Clark5e335a32009-09-22 14:53:11 -04001671 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001672 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1673 addIntent);
1674 menu.findItem(R.id.copy_phone_context_menu_id).setOnMenuItemClickListener(
1675 new Copy(extra));
1676 break;
1677
1678 case WebView.HitTestResult.EMAIL_TYPE:
1679 menu.setHeaderTitle(extra);
1680 menu.findItem(R.id.email_context_menu_id).setIntent(
1681 new Intent(Intent.ACTION_VIEW, Uri
1682 .parse(WebView.SCHEME_MAILTO + extra)));
1683 menu.findItem(R.id.copy_mail_context_menu_id).setOnMenuItemClickListener(
1684 new Copy(extra));
1685 break;
1686
1687 case WebView.HitTestResult.GEO_TYPE:
1688 menu.setHeaderTitle(extra);
1689 menu.findItem(R.id.map_context_menu_id).setIntent(
1690 new Intent(Intent.ACTION_VIEW, Uri
1691 .parse(WebView.SCHEME_GEO
1692 + URLEncoder.encode(extra))));
1693 menu.findItem(R.id.copy_geo_context_menu_id).setOnMenuItemClickListener(
1694 new Copy(extra));
1695 break;
1696
1697 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1698 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
1699 TextView titleView = (TextView) LayoutInflater.from(this)
1700 .inflate(android.R.layout.browser_link_context_header,
1701 null);
1702 titleView.setText(extra);
1703 menu.setHeaderView(titleView);
1704 // decide whether to show the open link in new tab option
Leon Scroggins III9e997c72010-05-26 13:25:16 -04001705 boolean showNewTab = mTabControl.canCreateNewTab();
1706 MenuItem newTabItem
1707 = menu.findItem(R.id.open_newtab_context_menu_id);
1708 newTabItem.setVisible(showNewTab);
1709 if (showNewTab) {
1710 newTabItem.setOnMenuItemClickListener(
1711 new MenuItem.OnMenuItemClickListener() {
1712 public boolean onMenuItemClick(MenuItem item) {
1713 final Tab parent = mTabControl.getCurrentTab();
1714 final Tab newTab = openTab(extra);
1715 if (newTab != parent) {
1716 parent.addChildTab(newTab);
1717 }
1718 return true;
1719 }
1720 });
1721 }
Ben Murdochde353622009-10-12 10:29:00 +01001722 menu.findItem(R.id.bookmark_context_menu_id).setVisible(
1723 Bookmarks.urlHasAcceptableScheme(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001724 PackageManager pm = getPackageManager();
1725 Intent send = new Intent(Intent.ACTION_SEND);
1726 send.setType("text/plain");
1727 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1728 menu.findItem(R.id.share_link_context_menu_id).setVisible(ri != null);
1729 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1730 break;
1731 }
1732 // otherwise fall through to handle image part
1733 case WebView.HitTestResult.IMAGE_TYPE:
1734 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1735 menu.setHeaderTitle(extra);
1736 }
1737 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1738 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1739 menu.findItem(R.id.download_context_menu_id).
1740 setOnMenuItemClickListener(new Download(extra));
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001741 menu.findItem(R.id.set_wallpaper_context_menu_id).
1742 setOnMenuItemClickListener(new SetAsWallpaper(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001743 break;
1744
1745 default:
1746 Log.w(LOGTAG, "We should not get here.");
1747 break;
1748 }
Leon Scrogginsb2b19f52009-10-09 16:10:00 -04001749 hideFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001750 }
1751
The Android Open Source Project0c908882009-03-03 19:32:16 -08001752 // Attach the given tab to the content view.
Grace Klobac928c302009-09-17 11:51:21 -07001753 // this should only be called for the current tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001754 private void attachTabToContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001755 // Attach the container that contains the main WebView and any other UI
1756 // associated with the tab.
Patrick Scottd0119532009-09-17 08:00:31 -04001757 t.attachTabToContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001758
1759 if (mShouldShowErrorConsole) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001760 ErrorConsoleView errorConsole = t.getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01001761 if (errorConsole.numberOfErrors() == 0) {
1762 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
1763 } else {
1764 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1765 }
1766
1767 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08001768 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01001769 ViewGroup.LayoutParams.WRAP_CONTENT));
1770 }
1771
Leon Scrogginsd746a942010-05-19 13:21:44 -04001772 if (!mXLargeScreenSize){
1773 WebView view = t.getWebView();
1774 view.setEmbeddedTitleBar(mTitleBar);
1775 }
Leon Scroggins58d56c62010-01-28 15:12:40 -05001776 if (t.isInVoiceSearchMode()) {
1777 showVoiceTitleBar(t.getVoiceDisplayTitle());
1778 } else {
1779 revertVoiceTitleBar();
1780 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001781 // Request focus on the top window.
1782 t.getTopWindow().requestFocus();
1783 }
1784
1785 // Attach a sub window to the main WebView of the given tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001786 void attachSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001787 t.attachSubWindow(mContentView);
1788 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001789 }
1790
1791 // Remove the given tab from the content view.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001792 private void removeTabFromContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001793 // Remove the container that contains the main WebView.
Patrick Scottd0119532009-09-17 08:00:31 -04001794 t.removeTabFromContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001795
Grace Kloba22ac16e2009-10-07 18:00:23 -07001796 ErrorConsoleView errorConsole = t.getErrorConsole(false);
1797 if (errorConsole != null) {
1798 mErrorConsoleContainer.removeView(errorConsole);
Ben Murdochbff2d602009-07-01 20:19:05 +01001799 }
1800
Leon Scrogginsd746a942010-05-19 13:21:44 -04001801 if (!mXLargeScreenSize) {
1802 WebView view = t.getWebView();
1803 if (view != null) {
1804 view.setEmbeddedTitleBar(null);
1805 }
Leon Scrogginsbb85b902009-09-14 19:27:20 -04001806 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001807 }
1808
1809 // Remove the sub window if it exists. Also called by TabControl when the
1810 // user clicks the 'X' to dismiss a sub window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001811 /* package */ void dismissSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001812 t.removeSubWindow(mContentView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001813 // dismiss the subwindow. This will destroy the WebView.
1814 t.dismissSubWindow();
Patrick Scottd0119532009-09-17 08:00:31 -04001815 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001816 }
1817
Leon Scroggins1f005d32009-08-10 17:36:42 -04001818 // A wrapper function of {@link #openTabAndShow(UrlData, boolean, String)}
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07001819 // that accepts url as string.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001820 private Tab openTabAndShow(String url, boolean closeOnExit, String appId) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001821 return openTabAndShow(new UrlData(url), closeOnExit, appId);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001822 }
1823
1824 // This method does a ton of stuff. It will attempt to create a new tab
1825 // if we haven't reached MAX_TABS. Otherwise it uses the current tab. If
Leon Scroggins1f005d32009-08-10 17:36:42 -04001826 // url isn't null, it will load the given url.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001827 /* package */Tab openTabAndShow(UrlData urlData, boolean closeOnExit,
1828 String appId) {
1829 final Tab currentTab = mTabControl.getCurrentTab();
1830 if (mTabControl.canCreateNewTab()) {
1831 final Tab tab = mTabControl.createNewTab(closeOnExit, appId,
1832 urlData.mUrl);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001833 WebView webview = tab.getWebView();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001834 // If the last tab was removed from the active tabs page, currentTab
1835 // will be null.
1836 if (currentTab != null) {
1837 removeTabFromContentView(currentTab);
1838 }
Patrick Scott8bbd69f2009-08-14 13:35:53 -04001839 // We must set the new tab as the current tab to reflect the old
1840 // animation behavior.
1841 mTabControl.setCurrentTab(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001842 attachTabToContentView(tab);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001843 if (!urlData.isEmpty()) {
Patrick Scott9d53da02010-02-19 10:19:01 -05001844 loadUrlDataIn(tab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001845 }
1846 return tab;
1847 } else {
1848 // Get rid of the subwindow if it exists
1849 dismissSubWindow(currentTab);
1850 if (!urlData.isEmpty()) {
1851 // Load the given url.
Patrick Scott9d53da02010-02-19 10:19:01 -05001852 loadUrlDataIn(currentTab, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001853 }
Leon Scroggins58d56c62010-01-28 15:12:40 -05001854 return currentTab;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001855 }
1856 }
1857
Grace Kloba22ac16e2009-10-07 18:00:23 -07001858 private Tab openTab(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001859 if (mSettings.openInBackground()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001860 Tab t = mTabControl.createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001861 if (t != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001862 WebView view = t.getWebView();
Leon Scroggins92472e82010-02-17 16:32:28 -05001863 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001864 }
Grace Klobac9181842009-04-14 08:53:22 -07001865 return t;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001866 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001867 return openTabAndShow(url, false, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001868 }
1869 }
1870
1871 private class Copy implements OnMenuItemClickListener {
1872 private CharSequence mText;
1873
1874 public boolean onMenuItemClick(MenuItem item) {
1875 copy(mText);
1876 return true;
1877 }
1878
1879 public Copy(CharSequence toCopy) {
1880 mText = toCopy;
1881 }
1882 }
1883
1884 private class Download implements OnMenuItemClickListener {
1885 private String mText;
1886
1887 public boolean onMenuItemClick(MenuItem item) {
1888 onDownloadStartNoStream(mText, null, null, null, -1);
1889 return true;
1890 }
1891
1892 public Download(String toDownload) {
1893 mText = toDownload;
1894 }
1895 }
1896
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001897 private class SetAsWallpaper extends Thread implements
1898 OnMenuItemClickListener, DialogInterface.OnCancelListener {
1899 private URL mUrl;
1900 private ProgressDialog mWallpaperProgress;
1901 private boolean mCanceled = false;
1902
1903 public SetAsWallpaper(String url) {
1904 try {
1905 mUrl = new URL(url);
1906 } catch (MalformedURLException e) {
1907 mUrl = null;
1908 }
1909 }
1910
1911 public void onCancel(DialogInterface dialog) {
1912 mCanceled = true;
1913 }
1914
1915 public boolean onMenuItemClick(MenuItem item) {
1916 if (mUrl != null) {
1917 // The user may have tried to set a image with a large file size as their
1918 // background so it may take a few moments to perform the operation. Display
1919 // a progress spinner while it is working.
1920 mWallpaperProgress = new ProgressDialog(BrowserActivity.this);
1921 mWallpaperProgress.setIndeterminate(true);
1922 mWallpaperProgress.setMessage(getText(R.string.progress_dialog_setting_wallpaper));
1923 mWallpaperProgress.setCancelable(true);
1924 mWallpaperProgress.setOnCancelListener(this);
1925 mWallpaperProgress.show();
1926 start();
1927 }
1928 return true;
1929 }
1930
Michael Kolbe0a36662010-06-29 10:37:12 -07001931 @Override
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001932 public void run() {
1933 Drawable oldWallpaper = BrowserActivity.this.getWallpaper();
1934 try {
1935 // TODO: This will cause the resource to be downloaded again, when we
1936 // should in most cases be able to grab it from the cache. To fix this
1937 // we should query WebCore to see if we can access a cached version and
1938 // instead open an input stream on that. This pattern could also be used
1939 // in the download manager where the same problem exists.
1940 InputStream inputstream = mUrl.openStream();
1941 if (inputstream != null) {
1942 setWallpaper(inputstream);
1943 }
1944 } catch (IOException e) {
1945 Log.e(LOGTAG, "Unable to set new wallpaper");
1946 // Act as though the user canceled the operation so we try to
1947 // restore the old wallpaper.
1948 mCanceled = true;
1949 }
1950
1951 if (mCanceled) {
1952 // Restore the old wallpaper if the user cancelled whilst we were setting
1953 // the new wallpaper.
1954 int width = oldWallpaper.getIntrinsicWidth();
1955 int height = oldWallpaper.getIntrinsicHeight();
1956 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1957 Canvas canvas = new Canvas(bm);
1958 oldWallpaper.setBounds(0, 0, width, height);
1959 oldWallpaper.draw(canvas);
1960 try {
1961 setWallpaper(bm);
1962 } catch (IOException e) {
1963 Log.e(LOGTAG, "Unable to restore old wallpaper.");
1964 }
1965 mCanceled = false;
1966 }
1967
1968 if (mWallpaperProgress.isShowing()) {
1969 mWallpaperProgress.dismiss();
1970 }
1971 }
1972 }
1973
The Android Open Source Project0c908882009-03-03 19:32:16 -08001974 private void copy(CharSequence text) {
1975 try {
1976 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
1977 if (clip != null) {
1978 clip.setClipboardText(text);
1979 }
1980 } catch (android.os.RemoteException e) {
1981 Log.e(LOGTAG, "Copy failed", e);
1982 }
1983 }
1984
1985 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001986 * Resets the browser title-view to whatever it must be
1987 * (for example, if we had a loading error)
1988 * When we have a new page, we call resetTitle, when we
1989 * have to reset the titlebar to whatever it used to be
1990 * (for example, if the user chose to stop loading), we
1991 * call resetTitleAndRevertLockIcon.
1992 */
1993 /* package */ void resetTitleAndRevertLockIcon() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001994 mTabControl.getCurrentTab().revertLockIcon();
1995 updateLockIconToLatest();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001996 resetTitleIconAndProgress();
1997 }
1998
1999 /**
2000 * Reset the title, favicon, and progress.
2001 */
2002 private void resetTitleIconAndProgress() {
2003 WebView current = mTabControl.getCurrentWebView();
2004 if (current == null) {
2005 return;
2006 }
2007 resetTitleAndIcon(current);
2008 int progress = current.getProgress();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002009 current.getWebChromeClient().onProgressChanged(current, progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002010 }
2011
2012 // Reset the title and the icon based on the given item.
2013 private void resetTitleAndIcon(WebView view) {
2014 WebHistoryItem item = view.copyBackForwardList().getCurrentItem();
2015 if (item != null) {
Leon Scroggins68579392009-09-15 15:31:54 -04002016 setUrlTitle(item.getUrl(), item.getTitle());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002017 setFavicon(item.getFavicon());
2018 } else {
Leon Scroggins68579392009-09-15 15:31:54 -04002019 setUrlTitle(null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002020 setFavicon(null);
2021 }
2022 }
2023
2024 /**
2025 * Sets a title composed of the URL and the title string.
2026 * @param url The URL of the site being loaded.
2027 * @param title The title of the site being loaded.
2028 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002029 void setUrlTitle(String url, String title) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002030 mUrl = url;
2031 mTitle = title;
2032
Leon Scroggins58d56c62010-01-28 15:12:40 -05002033 // If we are in voice search mode, the title has already been set.
2034 if (mTabControl.getCurrentTab().isInVoiceSearchMode()) return;
2035 mTitleBar.setDisplayTitle(url);
Leon Scrogginsd746a942010-05-19 13:21:44 -04002036 if (!mXLargeScreenSize) {
2037 mFakeTitleBar.setDisplayTitle(url);
2038 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002039 }
2040
2041 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08002042 * @param url The URL to build a title version of the URL from.
2043 * @return The title version of the URL or null if fails.
2044 * The title version of the URL can be either the URL hostname,
2045 * or the hostname with an "https://" prefix (for secure URLs),
2046 * or an empty string if, for example, the URL in question is a
2047 * file:// URL with no hostname.
2048 */
Leon Scroggins32e14a62009-06-11 10:26:34 -04002049 /* package */ static String buildTitleUrl(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002050 String titleUrl = null;
2051
2052 if (url != null) {
2053 try {
2054 // parse the url string
2055 URL urlObj = new URL(url);
2056 if (urlObj != null) {
2057 titleUrl = "";
2058
2059 String protocol = urlObj.getProtocol();
2060 String host = urlObj.getHost();
2061
2062 if (host != null && 0 < host.length()) {
2063 titleUrl = host;
2064 if (protocol != null) {
2065 // if a secure site, add an "https://" prefix!
2066 if (protocol.equalsIgnoreCase("https")) {
2067 titleUrl = protocol + "://" + host;
2068 }
2069 }
2070 }
2071 }
2072 } catch (MalformedURLException e) {}
2073 }
2074
2075 return titleUrl;
2076 }
2077
2078 // Set the favicon in the title bar.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002079 void setFavicon(Bitmap icon) {
Leon Scroggins68579392009-09-15 15:31:54 -04002080 mTitleBar.setFavicon(icon);
Leon Scrogginsd746a942010-05-19 13:21:44 -04002081 if (!mXLargeScreenSize) {
2082 mFakeTitleBar.setFavicon(icon);
2083 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002084 }
2085
2086 /**
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002087 * Close the tab, remove its associated title bar, and adjust mTabControl's
2088 * current tab to a valid value.
Leon Scroggins1f005d32009-08-10 17:36:42 -04002089 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002090 /* package */ void closeTab(Tab t) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002091 int currentIndex = mTabControl.getCurrentIndex();
2092 int removeIndex = mTabControl.getTabIndex(t);
Leon Scroggins1f005d32009-08-10 17:36:42 -04002093 mTabControl.removeTab(t);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002094 if (currentIndex >= removeIndex && currentIndex != 0) {
2095 currentIndex--;
2096 }
2097 mTabControl.setCurrentTab(mTabControl.getTab(currentIndex));
Andrei Popescua5bf1de2009-09-23 16:39:23 +01002098 resetTitleIconAndProgress();
Leon Scroggins654899b2010-06-25 16:25:23 -04002099 updateLockIconToLatest();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002100 }
2101
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05002102 /* package */ void goBackOnePageOrQuit() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002103 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002104 if (current == null) {
2105 /*
2106 * Instead of finishing the activity, simply push this to the back
2107 * of the stack and let ActivityManager to choose the foreground
2108 * activity. As BrowserActivity is singleTask, it will be always the
2109 * root of the task. So we can use either true or false for
2110 * moveTaskToBack().
2111 */
2112 moveTaskToBack(true);
Grace Kloba00d85e72009-09-23 18:50:05 -07002113 return;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002114 }
2115 WebView w = current.getWebView();
2116 if (w.canGoBack()) {
2117 w.goBack();
2118 } else {
2119 // Check to see if we are closing a window that was created by
2120 // another window. If so, we switch back to that window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002121 Tab parent = current.getParentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002122 if (parent != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04002123 switchToTab(mTabControl.getTabIndex(parent));
2124 // Now we close the other tab
2125 closeTab(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002126 } else {
2127 if (current.closeOnExit()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002128 // force the tab's inLoad() to be false as we are going to
2129 // either finish the activity or remove the tab. This will
2130 // ensure pauseWebViewTimers() taking action.
2131 mTabControl.getCurrentTab().clearInLoad();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002132 if (mTabControl.getTabCount() == 1) {
2133 finish();
2134 return;
2135 }
Mike Reed7bfa63b2009-05-28 11:08:32 -04002136 // call pauseWebViewTimers() now, we won't be able to call
2137 // it in onPause() as the WebView won't be valid.
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002138 // Temporarily change mActivityInPause to be true as
2139 // pauseWebViewTimers() will do nothing if mActivityInPause
2140 // is false.
Grace Kloba918e1d72009-08-13 14:55:06 -07002141 boolean savedState = mActivityInPause;
2142 if (savedState) {
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002143 Log.e(LOGTAG, "BrowserActivity is already paused "
2144 + "while handing goBackOnePageOrQuit.");
Grace Kloba918e1d72009-08-13 14:55:06 -07002145 }
2146 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -04002147 pauseWebViewTimers();
Grace Kloba918e1d72009-08-13 14:55:06 -07002148 mActivityInPause = savedState;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002149 removeTabFromContentView(current);
2150 mTabControl.removeTab(current);
2151 }
2152 /*
2153 * Instead of finishing the activity, simply push this to the back
2154 * of the stack and let ActivityManager to choose the foreground
2155 * activity. As BrowserActivity is singleTask, it will be always the
2156 * root of the task. So we can use either true or false for
2157 * moveTaskToBack().
2158 */
2159 moveTaskToBack(true);
2160 }
2161 }
2162 }
2163
Grace Kloba22ac16e2009-10-07 18:00:23 -07002164 boolean isMenuDown() {
2165 return mMenuIsDown;
2166 }
2167
Grace Kloba5942df02009-09-18 11:48:29 -07002168 @Override
2169 public boolean onKeyDown(int keyCode, KeyEvent event) {
Leon Scrogginsf65b50d2009-12-08 10:44:28 -05002170 // Even if MENU is already held down, we need to call to super to open
2171 // the IME on long press.
2172 if (KeyEvent.KEYCODE_MENU == keyCode) {
2173 mMenuIsDown = true;
2174 return super.onKeyDown(keyCode, event);
2175 }
Grace Kloba5942df02009-09-18 11:48:29 -07002176 // The default key mode is DEFAULT_KEYS_SEARCH_LOCAL. As the MENU is
2177 // still down, we don't want to trigger the search. Pretend to consume
2178 // the key and do nothing.
2179 if (mMenuIsDown) return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002180
Grace Kloba5942df02009-09-18 11:48:29 -07002181 switch(keyCode) {
Grace Kloba5942df02009-09-18 11:48:29 -07002182 case KeyEvent.KEYCODE_SPACE:
Grace Klobada0fe552009-09-22 18:17:24 -07002183 // WebView/WebTextView handle the keys in the KeyDown. As
2184 // the Activity's shortcut keys are only handled when WebView
2185 // doesn't, have to do it in onKeyDown instead of onKeyUp.
2186 if (event.isShiftPressed()) {
2187 getTopWindow().pageUp(false);
2188 } else {
2189 getTopWindow().pageDown(false);
2190 }
Grace Kloba5942df02009-09-18 11:48:29 -07002191 return true;
2192 case KeyEvent.KEYCODE_BACK:
2193 if (event.getRepeatCount() == 0) {
2194 event.startTracking();
2195 return true;
2196 } else if (mCustomView == null && mActiveTabsPage == null
2197 && event.isLongPress()) {
2198 bookmarksOrHistoryPicker(true);
2199 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002200 }
Grace Kloba5942df02009-09-18 11:48:29 -07002201 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002202 }
Grace Kloba5942df02009-09-18 11:48:29 -07002203 return super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002204 }
2205
Grace Kloba5942df02009-09-18 11:48:29 -07002206 @Override
2207 public boolean onKeyUp(int keyCode, KeyEvent event) {
2208 switch(keyCode) {
2209 case KeyEvent.KEYCODE_MENU:
2210 mMenuIsDown = false;
2211 break;
Grace Kloba5942df02009-09-18 11:48:29 -07002212 case KeyEvent.KEYCODE_BACK:
2213 if (event.isTracking() && !event.isCanceled()) {
2214 if (mCustomView != null) {
2215 // if a custom view is showing, hide it
Grace Kloba22ac16e2009-10-07 18:00:23 -07002216 mTabControl.getCurrentWebView().getWebChromeClient()
2217 .onHideCustomView();
Grace Kloba5942df02009-09-18 11:48:29 -07002218 } else if (mActiveTabsPage != null) {
2219 // if tab page is showing, hide it
2220 removeActiveTabPage(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002221 } else {
Grace Kloba5942df02009-09-18 11:48:29 -07002222 WebView subwindow = mTabControl.getCurrentSubWindow();
2223 if (subwindow != null) {
2224 if (subwindow.canGoBack()) {
2225 subwindow.goBack();
2226 } else {
2227 dismissSubWindow(mTabControl.getCurrentTab());
2228 }
2229 } else {
2230 goBackOnePageOrQuit();
2231 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002232 }
Grace Kloba5942df02009-09-18 11:48:29 -07002233 return true;
2234 }
2235 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002236 }
Grace Kloba5942df02009-09-18 11:48:29 -07002237 return super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002238 }
2239
Leon Scroggins68579392009-09-15 15:31:54 -04002240 /* package */ void stopLoading() {
Ben Murdochb7cc8b42009-09-28 10:59:09 +01002241 mDidStopLoad = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002242 resetTitleAndRevertLockIcon();
2243 WebView w = getTopWindow();
2244 w.stopLoading();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002245 // FIXME: before refactor, it is using mWebViewClient. So I keep the
2246 // same logic here. But for subwindow case, should we call into the main
2247 // WebView's onPageFinished as we never call its onPageStarted and if
2248 // the page finishes itself, we don't call onPageFinished.
2249 mTabControl.getCurrentWebView().getWebViewClient().onPageFinished(w,
2250 w.getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002251
2252 cancelStopToast();
2253 mStopToast = Toast
2254 .makeText(this, R.string.stopping, Toast.LENGTH_SHORT);
2255 mStopToast.show();
2256 }
2257
Grace Kloba22ac16e2009-10-07 18:00:23 -07002258 boolean didUserStopLoading() {
2259 return mDidStopLoad;
2260 }
2261
The Android Open Source Project0c908882009-03-03 19:32:16 -08002262 private void cancelStopToast() {
2263 if (mStopToast != null) {
2264 mStopToast.cancel();
2265 mStopToast = null;
2266 }
2267 }
2268
Grace Kloba22ac16e2009-10-07 18:00:23 -07002269 // called by a UI or non-UI thread to post the message
2270 public void postMessage(int what, int arg1, int arg2, Object obj,
2271 long delayMillis) {
2272 mHandler.sendMessageDelayed(mHandler.obtainMessage(what, arg1, arg2,
2273 obj), delayMillis);
2274 }
2275
2276 // called by a UI or non-UI thread to remove the message
2277 void removeMessages(int what, Object object) {
2278 mHandler.removeMessages(what, object);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002279 }
2280
2281 // public message ids
2282 public final static int LOAD_URL = 1001;
2283 public final static int STOP_LOAD = 1002;
2284
2285 // Message Ids
2286 private static final int FOCUS_NODE_HREF = 102;
Grace Kloba92c18a52009-07-31 23:48:32 -07002287 private static final int RELEASE_WAKELOCK = 107;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002288
Grace Kloba22ac16e2009-10-07 18:00:23 -07002289 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
Ben Murdoch2694e232009-09-29 09:41:11 +01002290
Ben Murdocheecb4e62010-07-06 16:30:38 +01002291 private static final int TOUCH_ICON_DOWNLOADED = 109;
2292
The Android Open Source Project0c908882009-03-03 19:32:16 -08002293 // Private handler for handling javascript and saving passwords
2294 private Handler mHandler = new Handler() {
2295
Michael Kolbe0a36662010-06-29 10:37:12 -07002296 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -08002297 public void handleMessage(Message msg) {
2298 switch (msg.what) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002299 case FOCUS_NODE_HREF:
Ben Murdoch2694e232009-09-29 09:41:11 +01002300 {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002301 String url = (String) msg.getData().get("url");
Ben Murdoch90d088c2009-11-17 18:14:04 +00002302 String title = (String) msg.getData().get("title");
The Android Open Source Project0c908882009-03-03 19:32:16 -08002303 if (url == null || url.length() == 0) {
2304 break;
2305 }
2306 HashMap focusNodeMap = (HashMap) msg.obj;
2307 WebView view = (WebView) focusNodeMap.get("webview");
2308 // Only apply the action if the top window did not change.
2309 if (getTopWindow() != view) {
2310 break;
2311 }
2312 switch (msg.arg1) {
2313 case R.id.open_context_menu_id:
2314 case R.id.view_image_context_menu_id:
Leon Scroggins92472e82010-02-17 16:32:28 -05002315 loadUrlFromContext(getTopWindow(), url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002316 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002317 case R.id.bookmark_context_menu_id:
2318 Intent intent = new Intent(BrowserActivity.this,
2319 AddBookmarkPage.class);
2320 intent.putExtra("url", url);
Ben Murdoch90d088c2009-11-17 18:14:04 +00002321 intent.putExtra("title", title);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002322 startActivity(intent);
2323 break;
2324 case R.id.share_link_context_menu_id:
Leon Scroggins3e204452010-05-10 11:06:03 -04002325 sharePage(BrowserActivity.this, title, url, null,
Leon Scroggins96afcb12009-12-10 12:35:56 -05002326 null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002327 break;
2328 case R.id.copy_link_context_menu_id:
2329 copy(url);
2330 break;
2331 case R.id.save_link_context_menu_id:
2332 case R.id.download_context_menu_id:
2333 onDownloadStartNoStream(url, null, null, null, -1);
2334 break;
2335 }
2336 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002337 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002338
2339 case LOAD_URL:
Leon Scroggins92472e82010-02-17 16:32:28 -05002340 loadUrlFromContext(getTopWindow(), (String) msg.obj);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002341 break;
2342
2343 case STOP_LOAD:
2344 stopLoading();
2345 break;
2346
The Android Open Source Project0c908882009-03-03 19:32:16 -08002347 case RELEASE_WAKELOCK:
2348 if (mWakeLock.isHeld()) {
2349 mWakeLock.release();
Grace Kloba5d0e02e2009-10-05 15:15:36 -07002350 // if we reach here, Browser should be still in the
2351 // background loading after WAKELOCK_TIMEOUT (5-min).
2352 // To avoid burning the battery, stop loading.
2353 mTabControl.stopAllLoading();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002354 }
2355 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002356
2357 case UPDATE_BOOKMARK_THUMBNAIL:
2358 WebView view = (WebView) msg.obj;
2359 if (view != null) {
2360 updateScreenshot(view);
2361 }
2362 break;
Ben Murdocheecb4e62010-07-06 16:30:38 +01002363
2364 case TOUCH_ICON_DOWNLOADED:
2365 Bundle b = msg.getData();
2366 showSaveToHomescreenDialog(b.getString("url"),
2367 b.getString("title"),
2368 (Bitmap) b.getParcelable("touchIcon"),
2369 (Bitmap) b.getParcelable("favicon"));
2370 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002371 }
2372 }
2373 };
2374
Leon Scroggins96afcb12009-12-10 12:35:56 -05002375 /**
2376 * Share a page, providing the title, url, favicon, and a screenshot. Uses
2377 * an {@link Intent} to launch the Activity chooser.
2378 * @param c Context used to launch a new Activity.
2379 * @param title Title of the page. Stored in the Intent with
Paul Westbrook03e6d392010-02-12 10:33:29 -08002380 * {@link Intent#EXTRA_SUBJECT}
Leon Scroggins96afcb12009-12-10 12:35:56 -05002381 * @param url URL of the page. Stored in the Intent with
2382 * {@link Intent#EXTRA_TEXT}
2383 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
2384 * with {@link Browser#EXTRA_SHARE_FAVICON}
2385 * @param screenshot Bitmap of a screenshot of the page. Stored in the
2386 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
2387 */
2388 public static final void sharePage(Context c, String title, String url,
2389 Bitmap favicon, Bitmap screenshot) {
2390 Intent send = new Intent(Intent.ACTION_SEND);
2391 send.setType("text/plain");
2392 send.putExtra(Intent.EXTRA_TEXT, url);
Paul Westbrook03e6d392010-02-12 10:33:29 -08002393 send.putExtra(Intent.EXTRA_SUBJECT, title);
Leon Scroggins96afcb12009-12-10 12:35:56 -05002394 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
2395 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
2396 try {
2397 c.startActivity(Intent.createChooser(send, c.getString(
2398 R.string.choosertitle_sharevia)));
2399 } catch(android.content.ActivityNotFoundException ex) {
2400 // if no app handles it, do nothing
2401 }
2402 }
2403
Leon Scroggins89c6d362009-07-15 16:54:37 -04002404 private void updateScreenshot(WebView view) {
2405 // If this is a bookmarked site, add a screenshot to the database.
2406 // FIXME: When should we update? Every time?
2407 // FIXME: Would like to make sure there is actually something to
2408 // draw, but the API for that (WebViewCore.pictureReady()) is not
2409 // currently accessible here.
Ben Murdochaac7aa62009-09-17 16:57:40 +01002410
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002411 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(this),
2412 getDesiredThumbnailHeight(this));
Patrick Scottcb192b52010-04-14 14:38:55 -04002413 if (bm == null) {
2414 return;
Leon Scroggins89c6d362009-07-15 16:54:37 -04002415 }
Patrick Scottcb192b52010-04-14 14:38:55 -04002416
2417 final ContentResolver cr = getContentResolver();
2418 final String url = view.getUrl();
2419 final String originalUrl = view.getOriginalUrl();
2420
2421 new AsyncTask<Void, Void, Void>() {
2422 @Override
2423 protected Void doInBackground(Void... unused) {
2424 Cursor c = null;
2425 try {
2426 c = BrowserBookmarksAdapter.queryBookmarksForUrl(
2427 cr, originalUrl, url, true);
2428 if (c != null) {
2429 if (c.moveToFirst()) {
2430 ContentValues values = new ContentValues();
2431 final ByteArrayOutputStream os
2432 = new ByteArrayOutputStream();
2433 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
2434 values.put(Browser.BookmarkColumns.THUMBNAIL,
2435 os.toByteArray());
2436 do {
2437 cr.update(ContentUris.withAppendedId(
2438 Browser.BOOKMARKS_URI, c.getInt(0)),
2439 values, null, null);
2440 } while (c.moveToNext());
2441 }
2442 }
2443 } catch (IllegalStateException e) {
2444 // Ignore
2445 } finally {
2446 if (c != null) c.close();
2447 }
2448 return null;
2449 }
2450 }.execute();
Leon Scroggins89c6d362009-07-15 16:54:37 -04002451 }
2452
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002453 /**
Leon Scrogginsf8551612009-09-24 16:06:02 -04002454 * Values for the size of the thumbnail created when taking a screenshot.
2455 * Lazily initialized. Instead of using these directly, use
2456 * getDesiredThumbnailWidth() or getDesiredThumbnailHeight().
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002457 */
Leon Scrogginsf8551612009-09-24 16:06:02 -04002458 private static int THUMBNAIL_WIDTH = 0;
2459 private static int THUMBNAIL_HEIGHT = 0;
2460
2461 /**
2462 * Return the desired width for thumbnail screenshots, which are stored in
2463 * the database, and used on the bookmarks screen.
2464 * @param context Context for finding out the density of the screen.
2465 * @return int desired width for thumbnail screenshot.
2466 */
2467 /* package */ static int getDesiredThumbnailWidth(Context context) {
2468 if (THUMBNAIL_WIDTH == 0) {
2469 float density = context.getResources().getDisplayMetrics().density;
2470 THUMBNAIL_WIDTH = (int) (90 * density);
2471 THUMBNAIL_HEIGHT = (int) (80 * density);
2472 }
2473 return THUMBNAIL_WIDTH;
2474 }
2475
2476 /**
2477 * Return the desired height for thumbnail screenshots, which are stored in
2478 * the database, and used on the bookmarks screen.
2479 * @param context Context for finding out the density of the screen.
2480 * @return int desired height for thumbnail screenshot.
2481 */
2482 /* package */ static int getDesiredThumbnailHeight(Context context) {
2483 // To ensure that they are both initialized.
2484 getDesiredThumbnailWidth(context);
2485 return THUMBNAIL_HEIGHT;
2486 }
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002487
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002488 private Bitmap createScreenshot(WebView view, int width, int height) {
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002489 Picture thumbnail = view.capturePicture();
Leon Scroggins45800572009-09-29 16:38:47 -04002490 if (thumbnail == null) {
2491 return null;
2492 }
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002493 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002494 Canvas canvas = new Canvas(bm);
2495 // May need to tweak these values to determine what is the
2496 // best scale factor
Ben Murdoch2694e232009-09-29 09:41:11 +01002497 int thumbnailWidth = thumbnail.getWidth();
Ben Murdochae59c3f2009-10-20 18:30:28 +01002498 int thumbnailHeight = thumbnail.getHeight();
2499 float scaleFactorX = 1.0f;
2500 float scaleFactorY = 1.0f;
Ben Murdoch2694e232009-09-29 09:41:11 +01002501 if (thumbnailWidth > 0) {
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002502 scaleFactorX = (float) width / (float)thumbnailWidth;
Ben Murdochae59c3f2009-10-20 18:30:28 +01002503 } else {
2504 return null;
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002505 }
Ben Murdochae59c3f2009-10-20 18:30:28 +01002506
2507 if (view.getWidth() > view.getHeight() &&
2508 thumbnailHeight < view.getHeight() && thumbnailHeight > 0) {
2509 // If the device is in landscape and the page is shorter
2510 // than the height of the view, stretch the thumbnail to fill the
2511 // space.
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002512 scaleFactorY = (float) height / (float)thumbnailHeight;
Ben Murdochae59c3f2009-10-20 18:30:28 +01002513 } else {
2514 // In the portrait case, this looks nice.
2515 scaleFactorY = scaleFactorX;
2516 }
2517
2518 canvas.scale(scaleFactorX, scaleFactorY);
2519
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002520 thumbnail.draw(canvas);
2521 return bm;
2522 }
2523
The Android Open Source Project0c908882009-03-03 19:32:16 -08002524 // -------------------------------------------------------------------------
Grace Kloba22ac16e2009-10-07 18:00:23 -07002525 // Helper function for WebViewClient.
The Android Open Source Project0c908882009-03-03 19:32:16 -08002526 //-------------------------------------------------------------------------
2527
2528 // Use in overrideUrlLoading
2529 /* package */ final static String SCHEME_WTAI = "wtai://wp/";
2530 /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
2531 /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;";
2532 /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;";
2533
Leon Scroggins92472e82010-02-17 16:32:28 -05002534 // Keep this initial progress in sync with initialProgressValue (* 100)
2535 // in ProgressTracker.cpp
2536 private final static int INITIAL_PROGRESS = 10;
2537
Grace Kloba22ac16e2009-10-07 18:00:23 -07002538 void onPageStarted(WebView view, String url, Bitmap favicon) {
2539 // when BrowserActivity just starts, onPageStarted may be called before
2540 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
2541 // to start the timer. As we won't switch tabs while an activity is in
2542 // pause state, we can ensure calling resume and pause in pair.
2543 if (mActivityInPause) resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002544
Grace Kloba22ac16e2009-10-07 18:00:23 -07002545 resetLockIcon(url);
2546 setUrlTitle(url, null);
2547 setFavicon(favicon);
Leon Scroggins8cf8f682009-11-04 11:13:50 -08002548 // Show some progress so that the user knows the page is beginning to
2549 // load
Leon Scroggins92472e82010-02-17 16:32:28 -05002550 onProgressChanged(view, INITIAL_PROGRESS);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002551 mDidStopLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07002552 if (!mIsNetworkUp) createAndShowNetworkDialog();
Cary Clark01cfcdd2010-06-04 16:36:45 -04002553 closeDialogs();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002554 if (mSettings.isTracing()) {
2555 String host;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002556 try {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002557 WebAddress uri = new WebAddress(url);
2558 host = uri.mHost;
2559 } catch (android.net.ParseException ex) {
2560 host = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -08002561 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002562 host = host.replace('.', '_');
2563 host += ".trace";
2564 mInTrace = true;
2565 Debug.startMethodTracing(host, 20 * 1024 * 1024);
2566 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002567
Grace Kloba22ac16e2009-10-07 18:00:23 -07002568 // Performance probe
2569 if (false) {
2570 mStart = SystemClock.uptimeMillis();
2571 mProcessStart = Process.getElapsedCpuTime();
2572 long[] sysCpu = new long[7];
2573 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2574 sysCpu, null)) {
2575 mUserStart = sysCpu[0] + sysCpu[1];
2576 mSystemStart = sysCpu[2];
2577 mIdleStart = sysCpu[3];
2578 mIrqStart = sysCpu[4] + sysCpu[5] + sysCpu[6];
2579 }
2580 mUiStart = SystemClock.currentThreadTimeMillis();
2581 }
2582 }
2583
2584 void onPageFinished(WebView view, String url) {
2585 // Reset the title and icon in case we stopped a provisional load.
2586 resetTitleAndIcon(view);
2587 // Update the lock icon image only once we are done loading
2588 updateLockIconToLatest();
2589 // pause the WebView timer and release the wake lock if it is finished
2590 // while BrowserActivity is in pause state.
2591 if (mActivityInPause && pauseWebViewTimers()) {
2592 if (mWakeLock.isHeld()) {
2593 mHandler.removeMessages(RELEASE_WAKELOCK);
2594 mWakeLock.release();
2595 }
2596 }
2597
2598 // Performance probe
2599 if (false) {
2600 long[] sysCpu = new long[7];
2601 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2602 sysCpu, null)) {
2603 String uiInfo = "UI thread used "
2604 + (SystemClock.currentThreadTimeMillis() - mUiStart)
2605 + " ms";
2606 if (LOGD_ENABLED) {
2607 Log.d(LOGTAG, uiInfo);
2608 }
2609 //The string that gets written to the log
2610 String performanceString = "It took total "
2611 + (SystemClock.uptimeMillis() - mStart)
2612 + " ms clock time to load the page."
2613 + "\nbrowser process used "
2614 + (Process.getElapsedCpuTime() - mProcessStart)
2615 + " ms, user processes used "
2616 + (sysCpu[0] + sysCpu[1] - mUserStart) * 10
2617 + " ms, kernel used "
2618 + (sysCpu[2] - mSystemStart) * 10
2619 + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10
2620 + " ms and irq took "
2621 + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart)
2622 * 10 + " ms, " + uiInfo;
2623 if (LOGD_ENABLED) {
2624 Log.d(LOGTAG, performanceString + "\nWebpage: " + url);
2625 }
2626 if (url != null) {
2627 // strip the url to maintain consistency
2628 String newUrl = new String(url);
2629 if (newUrl.startsWith("http://www.")) {
2630 newUrl = newUrl.substring(11);
2631 } else if (newUrl.startsWith("http://")) {
2632 newUrl = newUrl.substring(7);
2633 } else if (newUrl.startsWith("https://www.")) {
2634 newUrl = newUrl.substring(12);
2635 } else if (newUrl.startsWith("https://")) {
2636 newUrl = newUrl.substring(8);
2637 }
2638 if (LOGD_ENABLED) {
2639 Log.d(LOGTAG, newUrl + " loaded");
2640 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002641 }
2642 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002643 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002644
Grace Kloba22ac16e2009-10-07 18:00:23 -07002645 if (mInTrace) {
2646 mInTrace = false;
2647 Debug.stopMethodTracing();
2648 }
2649 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002650
Grace Klobae7fe26b2010-06-28 16:23:33 -07002651 private void closeEmptyChildTab() {
2652 Tab current = mTabControl.getCurrentTab();
2653 if (current != null
2654 && current.getWebView().copyBackForwardList().getSize() == 0) {
2655 Tab parent = current.getParentTab();
2656 if (parent != null) {
2657 switchToTab(mTabControl.getTabIndex(parent));
2658 closeTab(current);
2659 }
2660 }
2661 }
2662
Grace Kloba22ac16e2009-10-07 18:00:23 -07002663 boolean shouldOverrideUrlLoading(WebView view, String url) {
2664 if (url.startsWith(SCHEME_WTAI)) {
2665 // wtai://wp/mc;number
2666 // number=string(phone-number)
2667 if (url.startsWith(SCHEME_WTAI_MC)) {
2668 Intent intent = new Intent(Intent.ACTION_VIEW,
2669 Uri.parse(WebView.SCHEME_TEL +
2670 url.substring(SCHEME_WTAI_MC.length())));
2671 startActivity(intent);
Grace Klobae7fe26b2010-06-28 16:23:33 -07002672 // before leaving BrowserActivity, close the empty child tab.
2673 // If a new tab is created through JavaScript open to load this
2674 // url, we would like to close it as we will load this url in a
2675 // different Activity.
2676 closeEmptyChildTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002677 return true;
2678 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002679 // wtai://wp/sd;dtmf
2680 // dtmf=string(dialstring)
2681 if (url.startsWith(SCHEME_WTAI_SD)) {
2682 // TODO: only send when there is active voice connection
2683 return false;
2684 }
2685 // wtai://wp/ap;number;name
2686 // number=string(phone-number)
2687 // name=string
2688 if (url.startsWith(SCHEME_WTAI_AP)) {
2689 // TODO
2690 return false;
2691 }
2692 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002693
Grace Kloba22ac16e2009-10-07 18:00:23 -07002694 // The "about:" schemes are internal to the browser; don't want these to
2695 // be dispatched to other apps.
2696 if (url.startsWith("about:")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002697 return false;
2698 }
2699
Grace Kloba22ac16e2009-10-07 18:00:23 -07002700 Intent intent;
2701 // perform generic parsing of the URI to turn it into an Intent.
2702 try {
2703 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
2704 } catch (URISyntaxException ex) {
2705 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
2706 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002707 }
2708
Grace Kloba22ac16e2009-10-07 18:00:23 -07002709 // check whether the intent can be resolved. If not, we will see
2710 // whether we can download it from the Market.
2711 if (getPackageManager().resolveActivity(intent, 0) == null) {
2712 String packagename = intent.getPackage();
2713 if (packagename != null) {
2714 intent = new Intent(Intent.ACTION_VIEW, Uri
2715 .parse("market://search?q=pname:" + packagename));
2716 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2717 startActivity(intent);
Grace Klobae7fe26b2010-06-28 16:23:33 -07002718 // before leaving BrowserActivity, close the empty child tab.
2719 // If a new tab is created through JavaScript open to load this
2720 // url, we would like to close it as we will load this url in a
2721 // different Activity.
2722 closeEmptyChildTab();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002723 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002724 } else {
2725 return false;
2726 }
2727 }
2728
Grace Kloba22ac16e2009-10-07 18:00:23 -07002729 // sanitize the Intent, ensuring web pages can not bypass browser
2730 // security (only access to BROWSABLE activities).
2731 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2732 intent.setComponent(null);
2733 try {
2734 if (startActivityIfNeeded(intent, -1)) {
Grace Klobae7fe26b2010-06-28 16:23:33 -07002735 // before leaving BrowserActivity, close the empty child tab.
2736 // If a new tab is created through JavaScript open to load this
2737 // url, we would like to close it as we will load this url in a
2738 // different Activity.
2739 closeEmptyChildTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002740 return true;
2741 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002742 } catch (ActivityNotFoundException ex) {
2743 // ignore the error. If no application can handle the URL,
2744 // eg about:blank, assume the browser can handle it.
2745 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002746
Grace Kloba22ac16e2009-10-07 18:00:23 -07002747 if (mMenuIsDown) {
2748 openTab(url);
2749 closeOptionsMenu();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002750 return true;
2751 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002752 return false;
2753 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002754
Grace Kloba22ac16e2009-10-07 18:00:23 -07002755 // -------------------------------------------------------------------------
2756 // Helper function for WebChromeClient
2757 // -------------------------------------------------------------------------
The Android Open Source Project0c908882009-03-03 19:32:16 -08002758
Grace Kloba22ac16e2009-10-07 18:00:23 -07002759 void onProgressChanged(WebView view, int newProgress) {
Leon Scrogginsd746a942010-05-19 13:21:44 -04002760 if (mXLargeScreenSize) {
2761 mTitleBar.setProgress(newProgress);
2762 } else {
2763 // On the phone, the fake title bar will always cover up the
2764 // regular title bar (or the regular one is offscreen), so only the
2765 // fake title bar needs to change its progress
2766 mFakeTitleBar.setProgress(newProgress);
2767 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002768
Grace Kloba22ac16e2009-10-07 18:00:23 -07002769 if (newProgress == 100) {
2770 // onProgressChanged() may continue to be called after the main
2771 // frame has finished loading, as any remaining sub frames continue
2772 // to load. We'll only get called once though with newProgress as
2773 // 100 when everything is loaded. (onPageFinished is called once
2774 // when the main frame completes loading regardless of the state of
2775 // any sub frames so calls to onProgressChanges may continue after
2776 // onPageFinished has executed)
2777 if (mInLoad) {
2778 mInLoad = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -04002779 updateInLoadMenuItems();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002780 // If the options menu is open, leave the title bar
2781 if (!mOptionsMenuOpen || !mIconView) {
2782 hideFakeTitleBar();
The Android Open Source Projectcb9a0bb2009-03-11 12:11:58 -07002783 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002784 }
Leon Scrogginsaa37e7f2010-03-05 11:51:20 -05002785 } else {
2786 if (!mInLoad) {
2787 // onPageFinished may have already been called but a subframe is
2788 // still loading and updating the progress. Reset mInLoad and
2789 // update the menu items.
2790 mInLoad = true;
2791 updateInLoadMenuItems();
2792 }
2793 // When the page first begins to load, the Activity may still be
2794 // paused, in which case showFakeTitleBar will do nothing. Call
2795 // again as the page continues to load so that it will be shown.
2796 // (Calling it will the fake title bar is already showing will also
2797 // do nothing.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002798 if (!mOptionsMenuOpen || mIconView) {
2799 // This page has begun to load, so show the title bar
2800 showFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002801 }
2802 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002803 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002804
Grace Kloba22ac16e2009-10-07 18:00:23 -07002805 void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002806 // if a view already exists then immediately terminate the new one
2807 if (mCustomView != null) {
2808 callback.onCustomViewHidden();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002809 return;
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002810 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002811
2812 // Add the custom view to its container.
2813 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
2814 mCustomView = view;
2815 mCustomViewCallback = callback;
2816 // Save the menu state and set it to empty while the custom
2817 // view is showing.
2818 mOldMenuState = mMenuState;
2819 mMenuState = EMPTY_MENU;
2820 // Hide the content view.
2821 mContentView.setVisibility(View.GONE);
2822 // Finally show the custom view container.
Andrei Popescu163ab742009-10-20 17:58:23 +01002823 setStatusBarVisibility(false);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002824 mCustomViewContainer.setVisibility(View.VISIBLE);
2825 mCustomViewContainer.bringToFront();
2826 }
2827
2828 void onHideCustomView() {
2829 if (mCustomView == null)
2830 return;
2831
2832 // Hide the custom view.
2833 mCustomView.setVisibility(View.GONE);
2834 // Remove the custom view from its container.
2835 mCustomViewContainer.removeView(mCustomView);
2836 mCustomView = null;
2837 // Reset the old menu state.
2838 mMenuState = mOldMenuState;
2839 mOldMenuState = EMPTY_MENU;
2840 mCustomViewContainer.setVisibility(View.GONE);
2841 mCustomViewCallback.onCustomViewHidden();
2842 // Show the content view.
Andrei Popescu163ab742009-10-20 17:58:23 +01002843 setStatusBarVisibility(true);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002844 mContentView.setVisibility(View.VISIBLE);
2845 }
2846
2847 Bitmap getDefaultVideoPoster() {
2848 if (mDefaultVideoPoster == null) {
2849 mDefaultVideoPoster = BitmapFactory.decodeResource(
2850 getResources(), R.drawable.default_video_poster);
Patrick Scott3918d442009-08-04 13:22:29 -04002851 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002852 return mDefaultVideoPoster;
2853 }
Patrick Scott3918d442009-08-04 13:22:29 -04002854
Grace Kloba22ac16e2009-10-07 18:00:23 -07002855 View getVideoLoadingProgressView() {
2856 if (mVideoProgressView == null) {
2857 LayoutInflater inflater = LayoutInflater.from(BrowserActivity.this);
2858 mVideoProgressView = inflater.inflate(
2859 R.layout.video_loading_progress, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002860 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002861 return mVideoProgressView;
2862 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002863
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002864 /*
2865 * The Object used to inform the WebView of the file to upload.
2866 */
2867 private ValueCallback<Uri> mUploadMessage;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002868 private String mCameraFilePath;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002869
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002870 void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
2871
2872 final String imageMimeType = "image/*";
Ben Murdoch6af492a2010-06-15 12:38:17 +01002873 final String videoMimeType = "video/*";
2874 final String mediaSourceKey = "source";
2875 final String mediaSourceValueCamera = "camera";
2876 final String mediaSourceValueGallery = "gallery";
2877 final String mediaSourceValueCamcorder = "camcorder";
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002878
Ben Murdoch6af492a2010-06-15 12:38:17 +01002879 // media source can be 'gallery' or 'camera' or 'camcorder'
2880 String mediaSource = "";
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002881
Ben Murdoch6af492a2010-06-15 12:38:17 +01002882 // We add the camera intent if there was no accept type (or '*/*' or 'image/*').
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002883 boolean addCameraIntent = true;
Ben Murdoch6af492a2010-06-15 12:38:17 +01002884 // We add the camcorder intent if there was no accept type (or '*/*' or 'video/*').
2885 boolean addCamcorderIntent = true;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002886
Ben Murdoch6af492a2010-06-15 12:38:17 +01002887 if (mUploadMessage != null) {
2888 // Already a file picker operation in progress.
2889 return;
2890 }
2891
Grace Kloba22ac16e2009-10-07 18:00:23 -07002892 mUploadMessage = uploadMsg;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002893
2894 // Parse the accept type.
2895 String params[] = acceptType.split(";");
2896 String mimeType = params[0];
2897
2898 for (String p : params) {
2899 String[] keyValue = p.split("=");
2900 if (keyValue.length == 2) {
2901 // Process key=value parameters.
Ben Murdoch6af492a2010-06-15 12:38:17 +01002902 if (mediaSourceKey.equals(keyValue[0])) {
2903 mediaSource = keyValue[1];
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002904 }
2905 }
2906 }
2907
2908 // This intent will display the standard OPENABLE file picker.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002909 Intent i = new Intent(Intent.ACTION_GET_CONTENT);
2910 i.addCategory(Intent.CATEGORY_OPENABLE);
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002911
2912 // Create an intent to add to the standard file picker that will
2913 // capture an image from the camera. We'll combine this intent with
2914 // the standard OPENABLE picker unless the web developer specifically
2915 // requested the camera or gallery be opened by passing a parameter
2916 // in the accept type.
2917 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
2918 File externalDataDir = Environment.getExternalStoragePublicDirectory(
2919 Environment.DIRECTORY_DCIM);
2920 File cameraDataDir = new File(externalDataDir.getAbsolutePath() +
2921 File.separator + "browser-photos");
2922 cameraDataDir.mkdirs();
2923 mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator +
2924 System.currentTimeMillis() + ".jpg";
2925 cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCameraFilePath)));
2926
Ben Murdoch6af492a2010-06-15 12:38:17 +01002927 Intent camcorderIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
2928
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002929 if (mimeType.equals(imageMimeType)) {
2930 i.setType(imageMimeType);
Ben Murdoch6af492a2010-06-15 12:38:17 +01002931 addCamcorderIntent = false;
2932 if (mediaSource.equals(mediaSourceValueCamera)) {
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002933 // Specified 'image/*' and requested the camera, so go ahead and launch the camera
2934 // directly.
2935 BrowserActivity.this.startActivityForResult(cameraIntent, FILE_SELECTED);
2936 return;
Ben Murdoch6af492a2010-06-15 12:38:17 +01002937 } else if (mediaSource.equals(mediaSourceValueGallery)) {
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002938 // Specified gallery as the source, so don't want to consider the camera.
2939 addCameraIntent = false;
2940 }
Ben Murdoch6af492a2010-06-15 12:38:17 +01002941 } else if (mimeType.equals(videoMimeType)) {
2942 i.setType(videoMimeType);
2943 addCameraIntent = false;
2944 // The camcorder saves it's own file and returns it to us in the intent, so
2945 // we don't need to generate one here.
2946 mCameraFilePath = null;
2947
2948 if (mediaSource.equals(mediaSourceValueCamcorder)) {
2949 // Specified 'video/*' and requested the camcorder, so go ahead and launch the camcorder
2950 // directly.
2951 BrowserActivity.this.startActivityForResult(camcorderIntent, FILE_SELECTED);
2952 return;
2953 } else if (mediaSource.equals(mediaSourceValueGallery)) {
2954 // Specified gallery as the source, so don't want to consider the camcorder.
2955 addCamcorderIntent = false;
2956 }
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002957 } else {
2958 i.setType("*/*");
2959 }
2960
Ben Murdoch6af492a2010-06-15 12:38:17 +01002961 // Combine the chooser and the extra choices (like camera or camcorder)
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002962 Intent chooser = new Intent(Intent.ACTION_CHOOSER);
2963 chooser.putExtra(Intent.EXTRA_INTENT, i);
2964
Ben Murdoch6af492a2010-06-15 12:38:17 +01002965 Vector<Intent> extraInitialIntents = new Vector<Intent>(0);
2966
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002967 if (addCameraIntent) {
Ben Murdoch6af492a2010-06-15 12:38:17 +01002968 extraInitialIntents.add(cameraIntent);
2969 }
2970
2971 if (addCamcorderIntent) {
2972 extraInitialIntents.add(camcorderIntent);
2973 }
2974
2975 if (extraInitialIntents.size() > 0) {
2976 Intent[] extraIntents = new Intent[extraInitialIntents.size()];
2977 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraInitialIntents.toArray(extraIntents));
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002978 }
2979
2980 chooser.putExtra(Intent.EXTRA_TITLE, getString(R.string.choose_upload));
2981 BrowserActivity.this.startActivityForResult(chooser, FILE_SELECTED);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002982 }
2983
2984 // -------------------------------------------------------------------------
2985 // Implement functions for DownloadListener
2986 // -------------------------------------------------------------------------
2987
The Android Open Source Project0c908882009-03-03 19:32:16 -08002988 /**
2989 * Notify the host application a download should be done, or that
2990 * the data should be streamed if a streaming viewer is available.
2991 * @param url The full url to the content that should be downloaded
2992 * @param contentDisposition Content-disposition http header, if
2993 * present.
2994 * @param mimetype The mimetype of the content reported by the server
2995 * @param contentLength The file size reported by the server
2996 */
2997 public void onDownloadStart(String url, String userAgent,
2998 String contentDisposition, String mimetype, long contentLength) {
2999 // if we're dealing wih A/V content that's not explicitly marked
3000 // for download, check if it's streamable.
3001 if (contentDisposition == null
Patrick Scotte1fb9662009-08-31 14:31:52 -04003002 || !contentDisposition.regionMatches(
3003 true, 0, "attachment", 0, 10)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003004 // query the package manager to see if there's a registered handler
3005 // that matches.
3006 Intent intent = new Intent(Intent.ACTION_VIEW);
3007 intent.setDataAndType(Uri.parse(url), mimetype);
Patrick Scotte1fb9662009-08-31 14:31:52 -04003008 ResolveInfo info = getPackageManager().resolveActivity(intent,
3009 PackageManager.MATCH_DEFAULT_ONLY);
3010 if (info != null) {
3011 ComponentName myName = getComponentName();
3012 // If we resolved to ourselves, we don't want to attempt to
3013 // load the url only to try and download it again.
3014 if (!myName.getPackageName().equals(
3015 info.activityInfo.packageName)
3016 || !myName.getClassName().equals(
3017 info.activityInfo.name)) {
3018 // someone (other than us) knows how to handle this mime
3019 // type with this scheme, don't download.
3020 try {
3021 startActivity(intent);
3022 return;
3023 } catch (ActivityNotFoundException ex) {
3024 if (LOGD_ENABLED) {
3025 Log.d(LOGTAG, "activity not found for " + mimetype
3026 + " over " + Uri.parse(url).getScheme(),
3027 ex);
3028 }
3029 // Best behavior is to fall back to a download in this
3030 // case
The Android Open Source Project0c908882009-03-03 19:32:16 -08003031 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003032 }
3033 }
3034 }
3035 onDownloadStartNoStream(url, userAgent, contentDisposition, mimetype, contentLength);
3036 }
3037
Kristian Monsenfa52d172010-03-25 18:29:21 +00003038 // This is to work around the fact that java.net.URI throws Exceptions
3039 // instead of just encoding URL's properly
3040 // Helper method for onDownloadStartNoStream
3041 private static String encodePath(String path) {
3042 char[] chars = path.toCharArray();
3043
3044 boolean needed = false;
3045 for (char c : chars) {
3046 if (c == '[' || c == ']') {
3047 needed = true;
3048 break;
3049 }
3050 }
3051 if (needed == false) {
3052 return path;
3053 }
3054
3055 StringBuilder sb = new StringBuilder("");
3056 for (char c : chars) {
3057 if (c == '[' || c == ']') {
3058 sb.append('%');
3059 sb.append(Integer.toHexString(c));
3060 } else {
3061 sb.append(c);
3062 }
3063 }
3064
3065 return sb.toString();
3066 }
3067
The Android Open Source Project0c908882009-03-03 19:32:16 -08003068 /**
3069 * Notify the host application a download should be done, even if there
3070 * is a streaming viewer available for thise type.
3071 * @param url The full url to the content that should be downloaded
3072 * @param contentDisposition Content-disposition http header, if
3073 * present.
3074 * @param mimetype The mimetype of the content reported by the server
3075 * @param contentLength The file size reported by the server
3076 */
3077 /*package */ void onDownloadStartNoStream(String url, String userAgent,
3078 String contentDisposition, String mimetype, long contentLength) {
3079
3080 String filename = URLUtil.guessFileName(url,
3081 contentDisposition, mimetype);
3082
3083 // Check to see if we have an SDCard
3084 String status = Environment.getExternalStorageState();
3085 if (!status.equals(Environment.MEDIA_MOUNTED)) {
3086 int title;
3087 String msg;
3088
3089 // Check to see if the SDCard is busy, same as the music app
3090 if (status.equals(Environment.MEDIA_SHARED)) {
3091 msg = getString(R.string.download_sdcard_busy_dlg_msg);
3092 title = R.string.download_sdcard_busy_dlg_title;
3093 } else {
3094 msg = getString(R.string.download_no_sdcard_dlg_msg, filename);
3095 title = R.string.download_no_sdcard_dlg_title;
3096 }
3097
3098 new AlertDialog.Builder(this)
3099 .setTitle(title)
3100 .setIcon(android.R.drawable.ic_dialog_alert)
3101 .setMessage(msg)
3102 .setPositiveButton(R.string.ok, null)
3103 .show();
3104 return;
3105 }
3106
Kristian Monsenfa52d172010-03-25 18:29:21 +00003107 // java.net.URI is a lot stricter than KURL so we have to encode some
3108 // extra characters. Fix for b 2538060 and b 1634719
3109 WebAddress webAddress;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003110 try {
Kristian Monsenfa52d172010-03-25 18:29:21 +00003111 webAddress = new WebAddress(url);
3112 webAddress.mPath = encodePath(webAddress.mPath);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003113 } catch (Exception e) {
Kristian Monsenfa52d172010-03-25 18:29:21 +00003114 // This only happens for very bad urls, we want to chatch the
3115 // exception here
3116 Log.e(LOGTAG, "Exception trying to parse url:" + url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003117 return;
3118 }
3119
3120 // XXX: Have to use the old url since the cookies were stored using the
3121 // old percent-encoded url.
3122 String cookies = CookieManager.getInstance().getCookie(url);
3123
3124 ContentValues values = new ContentValues();
Kristian Monsenfa52d172010-03-25 18:29:21 +00003125 values.put(Downloads.Impl.COLUMN_URI, webAddress.toString());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003126 values.put(Downloads.Impl.COLUMN_COOKIE_DATA, cookies);
3127 values.put(Downloads.Impl.COLUMN_USER_AGENT, userAgent);
3128 values.put(Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003129 getPackageName());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003130 values.put(Downloads.Impl.COLUMN_NOTIFICATION_CLASS,
Leon Scrogginsa563d092010-04-19 16:53:49 -04003131 OpenDownloadReceiver.class.getCanonicalName());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003132 values.put(Downloads.Impl.COLUMN_VISIBILITY,
3133 Downloads.Impl.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
3134 values.put(Downloads.Impl.COLUMN_MIME_TYPE, mimetype);
3135 values.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, filename);
Kristian Monsenfa52d172010-03-25 18:29:21 +00003136 values.put(Downloads.Impl.COLUMN_DESCRIPTION, webAddress.mHost);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003137 if (contentLength > 0) {
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003138 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, contentLength);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003139 }
3140 if (mimetype == null) {
3141 // We must have long pressed on a link or image to download it. We
3142 // are not sure of the mimetype in this case, so do a head request
3143 new FetchUrlMimeType(this).execute(values);
3144 } else {
3145 final Uri contentUri =
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003146 getContentResolver().insert(Downloads.Impl.CONTENT_URI, values);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003147 }
Leon Scroggins9191a7f2010-03-18 14:44:44 -04003148 Toast.makeText(this, R.string.download_pending, Toast.LENGTH_SHORT)
3149 .show();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003150 }
3151
Grace Kloba22ac16e2009-10-07 18:00:23 -07003152 // -------------------------------------------------------------------------
3153
The Android Open Source Project0c908882009-03-03 19:32:16 -08003154 /**
3155 * Resets the lock icon. This method is called when we start a new load and
3156 * know the url to be loaded.
3157 */
3158 private void resetLockIcon(String url) {
3159 // Save the lock-icon state (we revert to it if the load gets cancelled)
Grace Kloba22ac16e2009-10-07 18:00:23 -07003160 mTabControl.getCurrentTab().resetLockIcon(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003161 updateLockIconImage(LOCK_ICON_UNSECURE);
3162 }
3163
The Android Open Source Project0c908882009-03-03 19:32:16 -08003164 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003165 * Update the lock icon to correspond to our latest state.
3166 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003167 private void updateLockIconToLatest() {
Leon Scroggins91f65fc2010-06-29 13:52:39 -04003168 Tab t = mTabControl.getCurrentTab();
3169 if (t != null) {
3170 updateLockIconImage(t.getLockIconType());
3171 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003172 }
3173
3174 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08003175 * Updates the lock-icon image in the title-bar.
3176 */
3177 private void updateLockIconImage(int lockIconType) {
3178 Drawable d = null;
3179 if (lockIconType == LOCK_ICON_SECURE) {
3180 d = mSecLockIcon;
3181 } else if (lockIconType == LOCK_ICON_MIXED) {
3182 d = mMixLockIcon;
3183 }
Leon Scroggins68579392009-09-15 15:31:54 -04003184 mTitleBar.setLock(d);
Leon Scrogginsd746a942010-05-19 13:21:44 -04003185 if (!mXLargeScreenSize) {
3186 mFakeTitleBar.setLock(d);
3187 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003188 }
3189
3190 /**
3191 * Displays a page-info dialog.
3192 * @param tab The tab to show info about
3193 * @param fromShowSSLCertificateOnError The flag that indicates whether
3194 * this dialog was opened from the SSL-certificate-on-error dialog or
3195 * not. This is important, since we need to know whether to return to
3196 * the parent dialog or simply dismiss.
3197 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003198 private void showPageInfo(final Tab tab,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003199 final boolean fromShowSSLCertificateOnError) {
3200 final LayoutInflater factory = LayoutInflater
3201 .from(this);
3202
3203 final View pageInfoView = factory.inflate(R.layout.page_info, null);
3204
3205 final WebView view = tab.getWebView();
3206
3207 String url = null;
3208 String title = null;
3209
3210 if (view == null) {
3211 url = tab.getUrl();
3212 title = tab.getTitle();
3213 } else if (view == mTabControl.getCurrentWebView()) {
3214 // Use the cached title and url if this is the current WebView
3215 url = mUrl;
3216 title = mTitle;
3217 } else {
3218 url = view.getUrl();
3219 title = view.getTitle();
3220 }
3221
3222 if (url == null) {
3223 url = "";
3224 }
3225 if (title == null) {
3226 title = "";
3227 }
3228
3229 ((TextView) pageInfoView.findViewById(R.id.address)).setText(url);
3230 ((TextView) pageInfoView.findViewById(R.id.title)).setText(title);
3231
3232 mPageInfoView = tab;
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05003233 mPageInfoFromShowSSLCertificateOnError = fromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003234
3235 AlertDialog.Builder alertDialogBuilder =
3236 new AlertDialog.Builder(this)
3237 .setTitle(R.string.page_info).setIcon(android.R.drawable.ic_dialog_info)
3238 .setView(pageInfoView)
3239 .setPositiveButton(
3240 R.string.ok,
3241 new DialogInterface.OnClickListener() {
3242 public void onClick(DialogInterface dialog,
3243 int whichButton) {
3244 mPageInfoDialog = null;
3245 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003246
3247 // if we came here from the SSL error dialog
3248 if (fromShowSSLCertificateOnError) {
3249 // go back to the SSL error dialog
3250 showSSLCertificateOnError(
3251 mSSLCertificateOnErrorView,
3252 mSSLCertificateOnErrorHandler,
3253 mSSLCertificateOnErrorError);
3254 }
3255 }
3256 })
3257 .setOnCancelListener(
3258 new DialogInterface.OnCancelListener() {
3259 public void onCancel(DialogInterface dialog) {
3260 mPageInfoDialog = null;
3261 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003262
3263 // if we came here from the SSL error dialog
3264 if (fromShowSSLCertificateOnError) {
3265 // go back to the SSL error dialog
3266 showSSLCertificateOnError(
3267 mSSLCertificateOnErrorView,
3268 mSSLCertificateOnErrorHandler,
3269 mSSLCertificateOnErrorError);
3270 }
3271 }
3272 });
3273
3274 // if we have a main top-level page SSL certificate set or a certificate
3275 // error
3276 if (fromShowSSLCertificateOnError ||
3277 (view != null && view.getCertificate() != null)) {
3278 // add a 'View Certificate' button
3279 alertDialogBuilder.setNeutralButton(
3280 R.string.view_certificate,
3281 new DialogInterface.OnClickListener() {
3282 public void onClick(DialogInterface dialog,
3283 int whichButton) {
3284 mPageInfoDialog = null;
3285 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003286
3287 // if we came here from the SSL error dialog
3288 if (fromShowSSLCertificateOnError) {
3289 // go back to the SSL error dialog
3290 showSSLCertificateOnError(
3291 mSSLCertificateOnErrorView,
3292 mSSLCertificateOnErrorHandler,
3293 mSSLCertificateOnErrorError);
3294 } else {
3295 // otherwise, display the top-most certificate from
3296 // the chain
3297 if (view.getCertificate() != null) {
3298 showSSLCertificate(tab);
3299 }
3300 }
3301 }
3302 });
3303 }
3304
3305 mPageInfoDialog = alertDialogBuilder.show();
3306 }
3307
3308 /**
3309 * Displays the main top-level page SSL certificate dialog
3310 * (accessible from the Page-Info dialog).
3311 * @param tab The tab to show certificate for.
3312 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003313 private void showSSLCertificate(final Tab tab) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003314 final View certificateView =
3315 inflateCertificateView(tab.getWebView().getCertificate());
3316 if (certificateView == null) {
3317 return;
3318 }
3319
3320 LayoutInflater factory = LayoutInflater.from(this);
3321
3322 final LinearLayout placeholder =
3323 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3324
3325 LinearLayout ll = (LinearLayout) factory.inflate(
3326 R.layout.ssl_success, placeholder);
3327 ((TextView)ll.findViewById(R.id.success))
3328 .setText(R.string.ssl_certificate_is_valid);
3329
3330 mSSLCertificateView = tab;
3331 mSSLCertificateDialog =
3332 new AlertDialog.Builder(this)
3333 .setTitle(R.string.ssl_certificate).setIcon(
3334 R.drawable.ic_dialog_browser_certificate_secure)
3335 .setView(certificateView)
3336 .setPositiveButton(R.string.ok,
3337 new DialogInterface.OnClickListener() {
3338 public void onClick(DialogInterface dialog,
3339 int whichButton) {
3340 mSSLCertificateDialog = null;
3341 mSSLCertificateView = null;
3342
3343 showPageInfo(tab, false);
3344 }
3345 })
3346 .setOnCancelListener(
3347 new DialogInterface.OnCancelListener() {
3348 public void onCancel(DialogInterface dialog) {
3349 mSSLCertificateDialog = null;
3350 mSSLCertificateView = null;
3351
3352 showPageInfo(tab, false);
3353 }
3354 })
3355 .show();
3356 }
3357
3358 /**
3359 * Displays the SSL error certificate dialog.
3360 * @param view The target web-view.
3361 * @param handler The SSL error handler responsible for cancelling the
3362 * connection that resulted in an SSL error or proceeding per user request.
3363 * @param error The SSL error object.
3364 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003365 void showSSLCertificateOnError(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003366 final WebView view, final SslErrorHandler handler, final SslError error) {
3367
3368 final View certificateView =
3369 inflateCertificateView(error.getCertificate());
3370 if (certificateView == null) {
3371 return;
3372 }
3373
3374 LayoutInflater factory = LayoutInflater.from(this);
3375
3376 final LinearLayout placeholder =
3377 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3378
3379 if (error.hasError(SslError.SSL_UNTRUSTED)) {
3380 LinearLayout ll = (LinearLayout)factory
3381 .inflate(R.layout.ssl_warning, placeholder);
3382 ((TextView)ll.findViewById(R.id.warning))
3383 .setText(R.string.ssl_untrusted);
3384 }
3385
3386 if (error.hasError(SslError.SSL_IDMISMATCH)) {
3387 LinearLayout ll = (LinearLayout)factory
3388 .inflate(R.layout.ssl_warning, placeholder);
3389 ((TextView)ll.findViewById(R.id.warning))
3390 .setText(R.string.ssl_mismatch);
3391 }
3392
3393 if (error.hasError(SslError.SSL_EXPIRED)) {
3394 LinearLayout ll = (LinearLayout)factory
3395 .inflate(R.layout.ssl_warning, placeholder);
3396 ((TextView)ll.findViewById(R.id.warning))
3397 .setText(R.string.ssl_expired);
3398 }
3399
3400 if (error.hasError(SslError.SSL_NOTYETVALID)) {
3401 LinearLayout ll = (LinearLayout)factory
3402 .inflate(R.layout.ssl_warning, placeholder);
3403 ((TextView)ll.findViewById(R.id.warning))
3404 .setText(R.string.ssl_not_yet_valid);
3405 }
3406
3407 mSSLCertificateOnErrorHandler = handler;
3408 mSSLCertificateOnErrorView = view;
3409 mSSLCertificateOnErrorError = error;
3410 mSSLCertificateOnErrorDialog =
3411 new AlertDialog.Builder(this)
3412 .setTitle(R.string.ssl_certificate).setIcon(
3413 R.drawable.ic_dialog_browser_certificate_partially_secure)
3414 .setView(certificateView)
3415 .setPositiveButton(R.string.ok,
3416 new DialogInterface.OnClickListener() {
3417 public void onClick(DialogInterface dialog,
3418 int whichButton) {
3419 mSSLCertificateOnErrorDialog = null;
3420 mSSLCertificateOnErrorView = null;
3421 mSSLCertificateOnErrorHandler = null;
3422 mSSLCertificateOnErrorError = null;
3423
Grace Kloba22ac16e2009-10-07 18:00:23 -07003424 view.getWebViewClient().onReceivedSslError(
3425 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003426 }
3427 })
3428 .setNeutralButton(R.string.page_info_view,
3429 new DialogInterface.OnClickListener() {
3430 public void onClick(DialogInterface dialog,
3431 int whichButton) {
3432 mSSLCertificateOnErrorDialog = null;
3433
3434 // do not clear the dialog state: we will
3435 // need to show the dialog again once the
3436 // user is done exploring the page-info details
3437
3438 showPageInfo(mTabControl.getTabFromView(view),
3439 true);
3440 }
3441 })
3442 .setOnCancelListener(
3443 new DialogInterface.OnCancelListener() {
3444 public void onCancel(DialogInterface dialog) {
3445 mSSLCertificateOnErrorDialog = null;
3446 mSSLCertificateOnErrorView = null;
3447 mSSLCertificateOnErrorHandler = null;
3448 mSSLCertificateOnErrorError = null;
3449
Grace Kloba22ac16e2009-10-07 18:00:23 -07003450 view.getWebViewClient().onReceivedSslError(
3451 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003452 }
3453 })
3454 .show();
3455 }
3456
3457 /**
3458 * Inflates the SSL certificate view (helper method).
3459 * @param certificate The SSL certificate.
3460 * @return The resultant certificate view with issued-to, issued-by,
3461 * issued-on, expires-on, and possibly other fields set.
3462 * If the input certificate is null, returns null.
3463 */
3464 private View inflateCertificateView(SslCertificate certificate) {
3465 if (certificate == null) {
3466 return null;
3467 }
3468
3469 LayoutInflater factory = LayoutInflater.from(this);
3470
3471 View certificateView = factory.inflate(
3472 R.layout.ssl_certificate, null);
3473
3474 // issued to:
3475 SslCertificate.DName issuedTo = certificate.getIssuedTo();
3476 if (issuedTo != null) {
3477 ((TextView) certificateView.findViewById(R.id.to_common))
3478 .setText(issuedTo.getCName());
3479 ((TextView) certificateView.findViewById(R.id.to_org))
3480 .setText(issuedTo.getOName());
3481 ((TextView) certificateView.findViewById(R.id.to_org_unit))
3482 .setText(issuedTo.getUName());
3483 }
3484
3485 // issued by:
3486 SslCertificate.DName issuedBy = certificate.getIssuedBy();
3487 if (issuedBy != null) {
3488 ((TextView) certificateView.findViewById(R.id.by_common))
3489 .setText(issuedBy.getCName());
3490 ((TextView) certificateView.findViewById(R.id.by_org))
3491 .setText(issuedBy.getOName());
3492 ((TextView) certificateView.findViewById(R.id.by_org_unit))
3493 .setText(issuedBy.getUName());
3494 }
3495
3496 // issued on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003497 String issuedOn = formatCertificateDate(
3498 certificate.getValidNotBeforeDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003499 ((TextView) certificateView.findViewById(R.id.issued_on))
3500 .setText(issuedOn);
3501
3502 // expires on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003503 String expiresOn = formatCertificateDate(
3504 certificate.getValidNotAfterDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003505 ((TextView) certificateView.findViewById(R.id.expires_on))
3506 .setText(expiresOn);
3507
3508 return certificateView;
3509 }
3510
3511 /**
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003512 * Formats the certificate date to a properly localized date string.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003513 * @return Properly localized version of the certificate date string and
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003514 * the "" if it fails to localize.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003515 */
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003516 private String formatCertificateDate(Date certificateDate) {
3517 if (certificateDate == null) {
3518 return "";
The Android Open Source Project0c908882009-03-03 19:32:16 -08003519 }
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003520 String formattedDate = DateFormat.getDateFormat(this).format(certificateDate);
3521 if (formattedDate == null) {
3522 return "";
3523 }
3524 return formattedDate;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003525 }
3526
3527 /**
3528 * Displays an http-authentication dialog.
3529 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003530 void showHttpAuthentication(final HttpAuthHandler handler,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003531 final String host, final String realm, final String title,
3532 final String name, final String password, int focusId) {
3533 LayoutInflater factory = LayoutInflater.from(this);
3534 final View v = factory
3535 .inflate(R.layout.http_authentication, null);
3536 if (name != null) {
3537 ((EditText) v.findViewById(R.id.username_edit)).setText(name);
3538 }
3539 if (password != null) {
3540 ((EditText) v.findViewById(R.id.password_edit)).setText(password);
3541 }
3542
3543 String titleText = title;
3544 if (titleText == null) {
3545 titleText = getText(R.string.sign_in_to).toString().replace(
3546 "%s1", host).replace("%s2", realm);
3547 }
3548
3549 mHttpAuthHandler = handler;
3550 AlertDialog dialog = new AlertDialog.Builder(this)
3551 .setTitle(titleText)
3552 .setIcon(android.R.drawable.ic_dialog_alert)
3553 .setView(v)
3554 .setPositiveButton(R.string.action,
3555 new DialogInterface.OnClickListener() {
3556 public void onClick(DialogInterface dialog,
3557 int whichButton) {
3558 String nm = ((EditText) v
3559 .findViewById(R.id.username_edit))
3560 .getText().toString();
3561 String pw = ((EditText) v
3562 .findViewById(R.id.password_edit))
3563 .getText().toString();
3564 BrowserActivity.this.setHttpAuthUsernamePassword
3565 (host, realm, nm, pw);
3566 handler.proceed(nm, pw);
3567 mHttpAuthenticationDialog = null;
3568 mHttpAuthHandler = null;
3569 }})
3570 .setNegativeButton(R.string.cancel,
3571 new DialogInterface.OnClickListener() {
3572 public void onClick(DialogInterface dialog,
3573 int whichButton) {
3574 handler.cancel();
3575 BrowserActivity.this.resetTitleAndRevertLockIcon();
3576 mHttpAuthenticationDialog = null;
3577 mHttpAuthHandler = null;
3578 }})
3579 .setOnCancelListener(new DialogInterface.OnCancelListener() {
3580 public void onCancel(DialogInterface dialog) {
3581 handler.cancel();
3582 BrowserActivity.this.resetTitleAndRevertLockIcon();
3583 mHttpAuthenticationDialog = null;
3584 mHttpAuthHandler = null;
3585 }})
3586 .create();
3587 // Make the IME appear when the dialog is displayed if applicable.
3588 dialog.getWindow().setSoftInputMode(
3589 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
3590 dialog.show();
3591 if (focusId != 0) {
3592 dialog.findViewById(focusId).requestFocus();
3593 } else {
3594 v.findViewById(R.id.username_edit).requestFocus();
3595 }
3596 mHttpAuthenticationDialog = dialog;
3597 }
3598
3599 public int getProgress() {
3600 WebView w = mTabControl.getCurrentWebView();
3601 if (w != null) {
3602 return w.getProgress();
3603 } else {
3604 return 100;
3605 }
3606 }
3607
3608 /**
3609 * Set HTTP authentication password.
3610 *
3611 * @param host The host for the password
3612 * @param realm The realm for the password
3613 * @param username The username for the password. If it is null, it means
3614 * password can't be saved.
3615 * @param password The password
3616 */
3617 public void setHttpAuthUsernamePassword(String host, String realm,
3618 String username,
3619 String password) {
Steve Block95a53b22010-03-25 17:24:58 +00003620 WebView w = getTopWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003621 if (w != null) {
3622 w.setHttpAuthUsernamePassword(host, realm, username, password);
3623 }
3624 }
3625
3626 /**
3627 * connectivity manager says net has come or gone... inform the user
3628 * @param up true if net has come up, false if net has gone down
3629 */
3630 public void onNetworkToggle(boolean up) {
3631 if (up == mIsNetworkUp) {
3632 return;
3633 } else if (up) {
3634 mIsNetworkUp = true;
3635 if (mAlertDialog != null) {
3636 mAlertDialog.cancel();
3637 mAlertDialog = null;
3638 }
3639 } else {
3640 mIsNetworkUp = false;
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003641 if (mInLoad) {
3642 createAndShowNetworkDialog();
3643 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003644 }
3645 WebView w = mTabControl.getCurrentWebView();
3646 if (w != null) {
3647 w.setNetworkAvailable(up);
3648 }
3649 }
3650
Grace Kloba22ac16e2009-10-07 18:00:23 -07003651 boolean isNetworkUp() {
3652 return mIsNetworkUp;
3653 }
3654
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003655 // This method shows the network dialog alerting the user that the net is
3656 // down. It will only show the dialog if mAlertDialog is null.
3657 private void createAndShowNetworkDialog() {
3658 if (mAlertDialog == null) {
3659 mAlertDialog = new AlertDialog.Builder(this)
3660 .setTitle(R.string.loadSuspendedTitle)
3661 .setMessage(R.string.loadSuspended)
3662 .setPositiveButton(R.string.ok, null)
3663 .show();
3664 }
3665 }
3666
The Android Open Source Project0c908882009-03-03 19:32:16 -08003667 @Override
3668 protected void onActivityResult(int requestCode, int resultCode,
3669 Intent intent) {
Grace Klobabb394f32009-11-19 10:26:37 -08003670 if (getTopWindow() == null) return;
3671
The Android Open Source Project0c908882009-03-03 19:32:16 -08003672 switch (requestCode) {
3673 case COMBO_PAGE:
3674 if (resultCode == RESULT_OK && intent != null) {
3675 String data = intent.getAction();
3676 Bundle extras = intent.getExtras();
3677 if (extras != null && extras.getBoolean("new_window", false)) {
Leon Scroggins25d35472009-09-15 11:37:27 -04003678 openTab(data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003679 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -07003680 final Tab currentTab =
The Android Open Source Project0c908882009-03-03 19:32:16 -08003681 mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04003682 dismissSubWindow(currentTab);
3683 if (data != null && data.length() != 0) {
Leon Scroggins92472e82010-02-17 16:32:28 -05003684 loadUrl(getTopWindow(), data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003685 }
3686 }
3687 }
Leon Scrogginsfde97462010-01-11 13:06:21 -05003688 // Deliberately fall through to PREFERENCES_PAGE, since the
3689 // same extra may be attached to the COMBO_PAGE
3690 case PREFERENCES_PAGE:
3691 if (resultCode == RESULT_OK && intent != null) {
3692 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
3693 if (BrowserSettings.PREF_CLEAR_HISTORY.equals(action)) {
3694 mTabControl.removeParentChildRelationShips();
3695 }
3696 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003697 break;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003698 // Choose a file from the file picker.
3699 case FILE_SELECTED:
3700 if (null == mUploadMessage) break;
3701 Uri result = intent == null || resultCode != RESULT_OK ? null
3702 : intent.getData();
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003703
3704 // As we ask the camera to save the result of the user taking
3705 // a picture, the camera application does not return anything other
3706 // than RESULT_OK. So we need to check whether the file we expected
3707 // was written to disk in the in the case that we
3708 // did not get an intent returned but did get a RESULT_OK. If it was,
3709 // we assume that this result has came back from the camera.
3710 if (result == null && intent == null && resultCode == RESULT_OK) {
3711 File cameraFile = new File(mCameraFilePath);
3712 if (cameraFile.exists()) {
3713 result = Uri.fromFile(cameraFile);
Ben Murdoch07d34732010-05-27 18:34:46 +01003714 // Broadcast to the media scanner that we have a new photo
3715 // so it will be added into the gallery for the user.
3716 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003717 }
3718 }
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003719 mUploadMessage.onReceiveValue(result);
3720 mUploadMessage = null;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003721 mCameraFilePath = null;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003722 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003723 default:
3724 break;
3725 }
Leon Scroggins30444232009-09-04 18:36:20 -04003726 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003727 }
3728
3729 /*
3730 * This method is called as a result of the user selecting the options
Leon Scrogginsf08809b2010-01-21 09:27:46 -05003731 * menu to see the download window. It shows the download window on top of
3732 * the current window.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003733 */
Leon Scrogginsf08809b2010-01-21 09:27:46 -05003734 private void viewDownloads(Uri downloadRecord) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003735 Intent intent = new Intent(this,
3736 BrowserDownloadPage.class);
3737 intent.setData(downloadRecord);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003738 startActivityForResult(intent, BrowserActivity.DOWNLOAD_PAGE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003739
3740 }
3741
Ben Murdocheecb4e62010-07-06 16:30:38 +01003742 /* package*/ void promptAddOrInstallBookmark() {
3743 final Tab current = mTabControl.getCurrentTab();
3744 Resources resources = getResources();
3745 CharSequence[] choices = {
3746 resources.getString(R.string.save_to_bookmarks),
3747 resources.getString(R.string.create_shortcut_bookmark)
3748 };
3749
3750 AlertDialog.Builder builder = new AlertDialog.Builder(this);
3751 builder.setTitle(R.string.add_new_bookmark);
3752 builder.setItems(choices, new DialogInterface.OnClickListener() {
3753 public void onClick(DialogInterface dialog, int item) {
3754 if (item == 0) {
3755 bookmarkCurrentPage();
3756 } else if (item == 1) {
3757 current.populatePickerData();
3758 String touchIconUrl = mTabControl.getCurrentWebView().getTouchIconUrl();
3759 if (touchIconUrl != null) {
3760 // Download the touch icon for this site then save it to the
3761 // homescreen.
3762 Bundle b = new Bundle();
3763 b.putString("url", current.getUrl());
3764 b.putString("title", current.getTitle());
3765 b.putParcelable("favicon", current.getFavicon());
3766 Message msg = mHandler.obtainMessage(TOUCH_ICON_DOWNLOADED);
3767 msg.setData(b);
3768 new DownloadTouchIcon(msg,
3769 mTabControl.getCurrentWebView().getSettings()
3770 .getUserAgentString()).execute(touchIconUrl);
3771 } else {
3772 // add to homescreen, can do it immediately as there is no touch
3773 // icon.
3774 showSaveToHomescreenDialog(current.getUrl(), current.getTitle(),
3775 null, current.getFavicon());
3776 }
3777 }
3778 }
3779 });
3780 builder.create().show();
3781 }
3782
Leon Scroggins160a7e72009-08-14 18:28:01 -04003783 /**
3784 * Open the Go page.
3785 * @param startWithHistory If true, open starting on the history tab.
3786 * Otherwise, start with the bookmarks tab.
Leon Scroggins160a7e72009-08-14 18:28:01 -04003787 */
Leon Scroggins30444232009-09-04 18:36:20 -04003788 /* package */ void bookmarksOrHistoryPicker(boolean startWithHistory) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003789 WebView current = mTabControl.getCurrentWebView();
3790 if (current == null) {
3791 return;
3792 }
3793 Intent intent = new Intent(this,
3794 CombinedBookmarkHistoryActivity.class);
3795 String title = current.getTitle();
3796 String url = current.getUrl();
Ben Murdoch87cc65d2010-06-29 20:34:10 +01003797 Bitmap thumbnail = createScreenshot(current, getDesiredThumbnailWidth(this),
3798 getDesiredThumbnailHeight(this));
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003799
The Android Open Source Project0c908882009-03-03 19:32:16 -08003800 // Just in case the user opens bookmarks before a page finishes loading
3801 // so the current history item, and therefore the page, is null.
3802 if (null == url) {
3803 url = mLastEnteredUrl;
3804 // This can happen.
3805 if (null == url) {
3806 url = mSettings.getHomePage();
3807 }
3808 }
3809 // In case the web page has not yet received its associated title.
3810 if (title == null) {
3811 title = url;
3812 }
3813 intent.putExtra("title", title);
3814 intent.putExtra("url", url);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003815 intent.putExtra("thumbnail", thumbnail);
Leon Scroggins30444232009-09-04 18:36:20 -04003816 // Disable opening in a new window if we have maxed out the windows
Grace Kloba22ac16e2009-10-07 18:00:23 -07003817 intent.putExtra("disable_new_window", !mTabControl.canCreateNewTab());
Patrick Scott3918d442009-08-04 13:22:29 -04003818 intent.putExtra("touch_icon_url", current.getTouchIconUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003819 if (startWithHistory) {
3820 intent.putExtra(CombinedBookmarkHistoryActivity.STARTING_TAB,
3821 CombinedBookmarkHistoryActivity.HISTORY_TAB);
3822 }
3823 startActivityForResult(intent, COMBO_PAGE);
3824 }
3825
Ben Murdocheecb4e62010-07-06 16:30:38 +01003826 private void showSaveToHomescreenDialog(String url, String title, Bitmap touchIcon,
3827 Bitmap favicon) {
3828 Intent intent = new Intent(this, SaveToHomescreenDialog.class);
3829
3830 // Just in case the user tries to save before a page finishes loading
3831 // so the current history item, and therefore the page, is null.
3832 if (null == url) {
3833 url = mLastEnteredUrl;
3834 // This can happen.
3835 if (null == url) {
3836 url = mSettings.getHomePage();
3837 }
3838 }
3839
3840 // In case the web page has not yet received its associated title.
3841 if (title == null) {
3842 title = url;
3843 }
3844
3845 intent.putExtra("title", title);
3846 intent.putExtra("url", url);
3847 intent.putExtra("favicon", favicon);
3848 intent.putExtra("touchIcon", touchIcon);
3849 startActivity(intent);
3850 }
3851
3852
The Android Open Source Project0c908882009-03-03 19:32:16 -08003853 // Called when loading from context menu or LOAD_URL message
Leon Scroggins92472e82010-02-17 16:32:28 -05003854 private void loadUrlFromContext(WebView view, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003855 // In case the user enters nothing.
3856 if (url != null && url.length() != 0 && view != null) {
3857 url = smartUrlFilter(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003858 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
Leon Scroggins92472e82010-02-17 16:32:28 -05003859 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003860 }
3861 }
3862 }
3863
Leon Scroggins92472e82010-02-17 16:32:28 -05003864 /**
3865 * Load the URL into the given WebView and update the title bar
3866 * to reflect the new load. Call this instead of WebView.loadUrl
3867 * directly.
3868 * @param view The WebView used to load url.
3869 * @param url The URL to load.
3870 */
3871 private void loadUrl(WebView view, String url) {
3872 updateTitleBarForNewLoad(view, url);
3873 view.loadUrl(url);
3874 }
3875
3876 /**
3877 * Load UrlData into a Tab and update the title bar to reflect the new
3878 * load. Call this instead of UrlData.loadIn directly.
3879 * @param t The Tab used to load.
3880 * @param data The UrlData being loaded.
3881 */
3882 private void loadUrlDataIn(Tab t, UrlData data) {
3883 updateTitleBarForNewLoad(t.getWebView(), data.mUrl);
3884 data.loadIn(t);
3885 }
3886
3887 /**
3888 * If the WebView is the top window, update the title bar to reflect
3889 * loading the new URL. i.e. set its text, clear the favicon (which
3890 * will be set once the page begins loading), and set the progress to
3891 * INITIAL_PROGRESS to show that the page has begun to load. Called
3892 * by loadUrl and loadUrlDataIn.
3893 * @param view The WebView that is starting a load.
3894 * @param url The URL that is being loaded.
3895 */
3896 private void updateTitleBarForNewLoad(WebView view, String url) {
3897 if (view == getTopWindow()) {
3898 setUrlTitle(url, null);
3899 setFavicon(null);
3900 onProgressChanged(view, INITIAL_PROGRESS);
3901 }
3902 }
3903
The Android Open Source Project0c908882009-03-03 19:32:16 -08003904 private String smartUrlFilter(Uri inUri) {
3905 if (inUri != null) {
3906 return smartUrlFilter(inUri.toString());
3907 }
3908 return null;
3909 }
3910
Feng Qianb34f87a2009-03-24 21:27:26 -07003911 protected static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003912 "(?i)" + // switch on case insensitive matching
3913 "(" + // begin group for schema
3914 "(?:http|https|file):\\/\\/" +
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003915 "|(?:inline|data|about|content|javascript):" +
The Android Open Source Project0c908882009-03-03 19:32:16 -08003916 ")" +
3917 "(.*)" );
3918
3919 /**
3920 * Attempts to determine whether user input is a URL or search
3921 * terms. Anything with a space is passed to search.
3922 *
3923 * Converts to lowercase any mistakenly uppercased schema (i.e.,
3924 * "Http://" converts to "http://"
3925 *
3926 * @return Original or modified URL
3927 *
3928 */
3929 String smartUrlFilter(String url) {
3930
3931 String inUrl = url.trim();
3932 boolean hasSpace = inUrl.indexOf(' ') != -1;
3933
3934 Matcher matcher = ACCEPTED_URI_SCHEMA.matcher(inUrl);
3935 if (matcher.matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003936 // force scheme to lowercase
3937 String scheme = matcher.group(1);
3938 String lcScheme = scheme.toLowerCase();
3939 if (!lcScheme.equals(scheme)) {
Mitsuru Oshima123ecfb2009-05-18 19:11:14 -07003940 inUrl = lcScheme + matcher.group(2);
3941 }
3942 if (hasSpace) {
3943 inUrl = inUrl.replace(" ", "%20");
The Android Open Source Project0c908882009-03-03 19:32:16 -08003944 }
3945 return inUrl;
3946 }
3947 if (hasSpace) {
Satish Sampath565505b2009-05-29 15:37:27 +01003948 // FIXME: Is this the correct place to add to searches?
3949 // what if someone else calls this function?
3950 int shortcut = parseUrlShortcut(inUrl);
3951 if (shortcut != SHORTCUT_INVALID) {
3952 Browser.addSearchUrl(mResolver, inUrl);
3953 String query = inUrl.substring(2);
3954 switch (shortcut) {
3955 case SHORTCUT_GOOGLE_SEARCH:
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003956 return URLUtil.composeSearchUrl(query, QuickSearch_G, QUERY_PLACE_HOLDER);
Satish Sampath565505b2009-05-29 15:37:27 +01003957 case SHORTCUT_WIKIPEDIA_SEARCH:
3958 return URLUtil.composeSearchUrl(query, QuickSearch_W, QUERY_PLACE_HOLDER);
3959 case SHORTCUT_DICTIONARY_SEARCH:
3960 return URLUtil.composeSearchUrl(query, QuickSearch_D, QUERY_PLACE_HOLDER);
3961 case SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH:
The Android Open Source Project0c908882009-03-03 19:32:16 -08003962 // FIXME: we need location in this case
Satish Sampath565505b2009-05-29 15:37:27 +01003963 return URLUtil.composeSearchUrl(query, QuickSearch_L, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003964 }
3965 }
3966 } else {
Dan Egnor5ee906c2009-11-18 12:11:49 -08003967 if (Patterns.WEB_URL.matcher(inUrl).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003968 return URLUtil.guessUrl(inUrl);
3969 }
3970 }
3971
3972 Browser.addSearchUrl(mResolver, inUrl);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003973 return URLUtil.composeSearchUrl(inUrl, QuickSearch_G, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003974 }
3975
Ben Murdochbff2d602009-07-01 20:19:05 +01003976 /* package */ void setShouldShowErrorConsole(boolean flag) {
3977 if (flag == mShouldShowErrorConsole) {
3978 // Nothing to do.
3979 return;
3980 }
3981
3982 mShouldShowErrorConsole = flag;
3983
Grace Kloba22ac16e2009-10-07 18:00:23 -07003984 ErrorConsoleView errorConsole = mTabControl.getCurrentTab()
3985 .getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01003986
3987 if (flag) {
3988 // Setting the show state of the console will cause it's the layout to be inflated.
3989 if (errorConsole.numberOfErrors() > 0) {
3990 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
3991 } else {
3992 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
3993 }
3994
3995 // Now we can add it to the main view.
3996 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08003997 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01003998 ViewGroup.LayoutParams.WRAP_CONTENT));
3999 } else {
4000 mErrorConsoleContainer.removeView(errorConsole);
4001 }
4002
4003 }
4004
Grace Kloba22ac16e2009-10-07 18:00:23 -07004005 boolean shouldShowErrorConsole() {
4006 return mShouldShowErrorConsole;
4007 }
4008
Andrei Popescu163ab742009-10-20 17:58:23 +01004009 private void setStatusBarVisibility(boolean visible) {
4010 int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
4011 getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
4012 }
4013
Andrei Popescu56199cc2010-01-12 22:39:16 +00004014
4015 private void sendNetworkType(String type, String subtype) {
4016 WebView w = mTabControl.getCurrentWebView();
4017 if (w != null) {
4018 w.setNetworkType(type, subtype);
4019 }
4020 }
4021
Andrei Popescu30995e72010-02-09 16:59:58 +00004022 private void packageChanged(String packageName, boolean wasAdded) {
4023 WebView w = mTabControl.getCurrentWebView();
4024 if (w == null) {
4025 return;
4026 }
4027
4028 if (wasAdded) {
4029 w.addPackageName(packageName);
4030 } else {
4031 w.removePackageName(packageName);
4032 }
4033 }
4034
4035 private void addPackageNames(Set<String> packageNames) {
4036 WebView w = mTabControl.getCurrentWebView();
4037 if (w == null) {
4038 return;
4039 }
4040
4041 w.addPackageNames(packageNames);
4042 }
4043
4044 private void getInstalledPackages() {
4045 AsyncTask<Void, Void, Set<String> > task =
4046 new AsyncTask<Void, Void, Set<String> >() {
Michael Kolbe0a36662010-06-29 10:37:12 -07004047 @Override
Andrei Popescu30995e72010-02-09 16:59:58 +00004048 protected Set<String> doInBackground(Void... unused) {
4049 Set<String> installedPackages = new HashSet<String>();
4050 PackageManager pm = BrowserActivity.this.getPackageManager();
4051 if (pm != null) {
4052 List<PackageInfo> packages = pm.getInstalledPackages(0);
4053 for (PackageInfo p : packages) {
4054 if (BrowserActivity.this.sGoogleApps.contains(p.packageName)) {
4055 installedPackages.add(p.packageName);
4056 }
4057 }
4058 }
4059
4060 return installedPackages;
4061 }
4062
4063 // Executes on the UI thread
Michael Kolbe0a36662010-06-29 10:37:12 -07004064 @Override
Andrei Popescu30995e72010-02-09 16:59:58 +00004065 protected void onPostExecute(Set<String> installedPackages) {
4066 addPackageNames(installedPackages);
4067 }
4068 };
4069 task.execute();
4070 }
4071
Grace Klobaeb6eef42009-09-15 17:56:32 -07004072 final static int LOCK_ICON_UNSECURE = 0;
4073 final static int LOCK_ICON_SECURE = 1;
4074 final static int LOCK_ICON_MIXED = 2;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004075
The Android Open Source Project0c908882009-03-03 19:32:16 -08004076 private BrowserSettings mSettings;
4077 private TabControl mTabControl;
4078 private ContentResolver mResolver;
4079 private FrameLayout mContentView;
Andrei Popescuadc008d2009-06-26 14:11:30 +01004080 private View mCustomView;
4081 private FrameLayout mCustomViewContainer;
Andrei Popescuc9b55562009-07-07 10:51:15 +01004082 private WebChromeClient.CustomViewCallback mCustomViewCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004083
4084 // FIXME, temp address onPrepareMenu performance problem. When we move everything out of
4085 // view, we should rewrite this.
4086 private int mCurrentMenuState = 0;
4087 private int mMenuState = R.id.MAIN_MENU;
Andrei Popescuadc008d2009-06-26 14:11:30 +01004088 private int mOldMenuState = EMPTY_MENU;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004089 private static final int EMPTY_MENU = -1;
4090 private Menu mMenu;
4091
4092 private FindDialog mFindDialog;
Cary Clark01cfcdd2010-06-04 16:36:45 -04004093 private SelectDialog mSelectDialog;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004094 // Used to prevent chording to result in firing two shortcuts immediately
4095 // one after another. Fixes bug 1211714.
4096 boolean mCanChord;
4097
4098 private boolean mInLoad;
4099 private boolean mIsNetworkUp;
Ben Murdochb7cc8b42009-09-28 10:59:09 +01004100 private boolean mDidStopLoad;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004101
Cary Clark1f10cbf2010-03-22 11:45:23 -04004102 /* package */ boolean mActivityInPause = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004103
4104 private boolean mMenuIsDown;
4105
The Android Open Source Project0c908882009-03-03 19:32:16 -08004106 private static boolean mInTrace;
4107
4108 // Performance probe
4109 private static final int[] SYSTEM_CPU_FORMAT = new int[] {
4110 Process.PROC_SPACE_TERM | Process.PROC_COMBINE,
4111 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time
4112 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time
4113 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time
4114 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time
4115 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time
4116 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time
4117 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG // 7: softirq time
4118 };
4119
4120 private long mStart;
4121 private long mProcessStart;
4122 private long mUserStart;
4123 private long mSystemStart;
4124 private long mIdleStart;
4125 private long mIrqStart;
4126
4127 private long mUiStart;
4128
4129 private Drawable mMixLockIcon;
4130 private Drawable mSecLockIcon;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004131
4132 /* hold a ref so we can auto-cancel if necessary */
4133 private AlertDialog mAlertDialog;
4134
The Android Open Source Project0c908882009-03-03 19:32:16 -08004135 // The up-to-date URL and title (these can be different from those stored
4136 // in WebView, since it takes some time for the information in WebView to
4137 // get updated)
4138 private String mUrl;
4139 private String mTitle;
4140
4141 // As PageInfo has different style for landscape / portrait, we have
4142 // to re-open it when configuration changed
4143 private AlertDialog mPageInfoDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07004144 private Tab mPageInfoView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004145 // If the Page-Info dialog is launched from the SSL-certificate-on-error
4146 // dialog, we should not just dismiss it, but should get back to the
4147 // SSL-certificate-on-error dialog. This flag is used to store this state
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05004148 private boolean mPageInfoFromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004149
4150 // as SSLCertificateOnError has different style for landscape / portrait,
4151 // we have to re-open it when configuration changed
4152 private AlertDialog mSSLCertificateOnErrorDialog;
4153 private WebView mSSLCertificateOnErrorView;
4154 private SslErrorHandler mSSLCertificateOnErrorHandler;
4155 private SslError mSSLCertificateOnErrorError;
4156
4157 // as SSLCertificate has different style for landscape / portrait, we
4158 // have to re-open it when configuration changed
4159 private AlertDialog mSSLCertificateDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07004160 private Tab mSSLCertificateView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004161
4162 // as HttpAuthentication has different style for landscape / portrait, we
4163 // have to re-open it when configuration changed
4164 private AlertDialog mHttpAuthenticationDialog;
4165 private HttpAuthHandler mHttpAuthHandler;
4166
4167 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
4168 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08004169 ViewGroup.LayoutParams.MATCH_PARENT,
4170 ViewGroup.LayoutParams.MATCH_PARENT);
Andrei Popescuadc008d2009-06-26 14:11:30 +01004171 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
4172 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08004173 ViewGroup.LayoutParams.MATCH_PARENT,
4174 ViewGroup.LayoutParams.MATCH_PARENT,
Andrei Popescuadc008d2009-06-26 14:11:30 +01004175 Gravity.CENTER);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07004176 // Google search
4177 final static String QuickSearch_G = "http://www.google.com/m?q=%s";
The Android Open Source Project0c908882009-03-03 19:32:16 -08004178 // Wikipedia search
4179 final static String QuickSearch_W = "http://en.wikipedia.org/w/index.php?search=%s&go=Go";
4180 // Dictionary search
4181 final static String QuickSearch_D = "http://dictionary.reference.com/search?q=%s";
4182 // Google Mobile Local search
4183 final static String QuickSearch_L = "http://www.google.com/m/search?site=local&q=%s&near=mountain+view";
4184
4185 final static String QUERY_PLACE_HOLDER = "%s";
4186
4187 // "source" parameter for Google search through search key
4188 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
4189 // "source" parameter for Google search through goto menu
4190 final static String GOOGLE_SEARCH_SOURCE_GOTO = "browser-goto";
4191 // "source" parameter for Google search through simplily type
4192 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
4193 // "source" parameter for Google search suggested by the browser
4194 final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
4195 // "source" parameter for Google search from unknown source
4196 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown";
4197
4198 private final static String LOGTAG = "browser";
4199
The Android Open Source Project0c908882009-03-03 19:32:16 -08004200 private String mLastEnteredUrl;
4201
4202 private PowerManager.WakeLock mWakeLock;
4203 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
4204
4205 private Toast mStopToast;
4206
Leon Scroggins571b3762010-05-26 10:25:01 -04004207 private TitleBarBase mTitleBar;
Leon Scroggins81db3662009-06-04 17:45:11 -04004208
Ben Murdochbff2d602009-07-01 20:19:05 +01004209 private LinearLayout mErrorConsoleContainer = null;
4210 private boolean mShouldShowErrorConsole = false;
4211
The Android Open Source Project0c908882009-03-03 19:32:16 -08004212 // As the ids are dynamically created, we can't guarantee that they will
4213 // be in sequence, so this static array maps ids to a window number.
4214 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
4215 { R.id.window_one_menu_id, R.id.window_two_menu_id, R.id.window_three_menu_id,
4216 R.id.window_four_menu_id, R.id.window_five_menu_id, R.id.window_six_menu_id,
4217 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
4218
4219 // monitor platform changes
4220 private IntentFilter mNetworkStateChangedFilter;
4221 private BroadcastReceiver mNetworkStateIntentReceiver;
4222
Grace Klobab4da0ad2009-05-14 14:45:40 -07004223 private BroadcastReceiver mPackageInstallationReceiver;
4224
Bjorn Bringerta7611812010-03-24 11:12:02 +00004225 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
4226
The Android Open Source Project0c908882009-03-03 19:32:16 -08004227 // activity requestCode
Nicolas Roard78a98e42009-05-11 13:34:17 +01004228 final static int COMBO_PAGE = 1;
4229 final static int DOWNLOAD_PAGE = 2;
4230 final static int PREFERENCES_PAGE = 3;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04004231 final static int FILE_SELECTED = 4;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004232
Andrei Popescu540035d2009-09-18 18:59:20 +01004233 // the default <video> poster
4234 private Bitmap mDefaultVideoPoster;
4235 // the video progress view
4236 private View mVideoProgressView;
4237
Andrei Popescu30995e72010-02-09 16:59:58 +00004238 // The Google packages we monitor for the navigator.isApplicationInstalled()
4239 // API. Add as needed.
4240 private static Set<String> sGoogleApps;
4241 static {
4242 sGoogleApps = new HashSet<String>();
4243 sGoogleApps.add("com.google.android.youtube");
4244 }
4245
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004246 /**
4247 * A UrlData class to abstract how the content will be set to WebView.
4248 * This base class uses loadUrl to show the content.
4249 */
Leon Scroggins6eac63e2010-03-15 18:19:14 -04004250 /* package */ static class UrlData {
Grace Kloba068e48b2010-01-26 18:11:27 -08004251 final String mUrl;
4252 final Map<String, String> mHeaders;
Leon Scroggins58d56c62010-01-28 15:12:40 -05004253 final Intent mVoiceIntent;
Grace Kloba60e095c2009-06-16 11:50:55 -07004254
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004255 UrlData(String url) {
4256 this.mUrl = url;
Grace Kloba068e48b2010-01-26 18:11:27 -08004257 this.mHeaders = null;
Leon Scroggins58d56c62010-01-28 15:12:40 -05004258 this.mVoiceIntent = null;
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004259 }
Grace Kloba60e095c2009-06-16 11:50:55 -07004260
Leon Scroggins58d56c62010-01-28 15:12:40 -05004261 UrlData(String url, Map<String, String> headers, Intent intent) {
Grace Kloba068e48b2010-01-26 18:11:27 -08004262 this.mUrl = url;
4263 this.mHeaders = headers;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -05004264 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
4265 .equals(intent.getAction())) {
Leon Scroggins58d56c62010-01-28 15:12:40 -05004266 this.mVoiceIntent = intent;
4267 } else {
4268 this.mVoiceIntent = null;
4269 }
Grace Kloba60e095c2009-06-16 11:50:55 -07004270 }
4271
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004272 boolean isEmpty() {
Leon Scroggins58d56c62010-01-28 15:12:40 -05004273 return mVoiceIntent == null && (mUrl == null || mUrl.length() == 0);
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004274 }
4275
Leon Scroggins92472e82010-02-17 16:32:28 -05004276 /**
4277 * Load this UrlData into the given Tab. Use loadUrlDataIn to update
4278 * the title bar as well.
4279 */
Leon Scroggins58d56c62010-01-28 15:12:40 -05004280 public void loadIn(Tab t) {
4281 if (mVoiceIntent != null) {
4282 t.activateVoiceSearchMode(mVoiceIntent);
4283 } else {
4284 t.getWebView().loadUrl(mUrl, mHeaders);
4285 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004286 }
4287 };
4288
Leon Scroggins1f005d32009-08-10 17:36:42 -04004289 /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08004290}