blob: 406132f5eaf4d33ebdff175891414d319930c694 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.app.Activity;
The Android Open Source Project0c908882009-03-03 19:32:16 -080020import android.app.AlertDialog;
21import android.app.ProgressDialog;
22import android.app.SearchManager;
23import android.content.ActivityNotFoundException;
24import android.content.BroadcastReceiver;
25import android.content.ComponentName;
Leon Scroggins58d56c62010-01-28 15:12:40 -050026import android.content.ContentProvider;
27import android.content.ContentProviderClient;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.content.ContentResolver;
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -040029import android.content.ContentUris;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.content.ContentValues;
31import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
34import android.content.IntentFilter;
Grace Klobab4da0ad2009-05-14 14:45:40 -070035import android.content.pm.PackageInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.content.pm.PackageManager;
37import android.content.pm.ResolveInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.content.res.Configuration;
39import android.content.res.Resources;
40import android.database.Cursor;
Leon Scroggins96afcb12009-12-10 12:35:56 -050041import android.database.DatabaseUtils;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042import android.graphics.Bitmap;
Andrei Popescu540035d2009-09-18 18:59:20 +010043import android.graphics.BitmapFactory;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import android.graphics.Canvas;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.graphics.Picture;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040046import android.graphics.PixelFormat;
47import android.graphics.Rect;
The Android Open Source Project0c908882009-03-03 19:32:16 -080048import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049import android.net.ConnectivityManager;
Andrei Popescu56199cc2010-01-12 22:39:16 +000050import android.net.NetworkInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051import android.net.Uri;
52import android.net.WebAddress;
The Android Open Source Project0c908882009-03-03 19:32:16 -080053import android.net.http.SslCertificate;
54import android.net.http.SslError;
55import android.os.AsyncTask;
56import android.os.Bundle;
57import android.os.Debug;
58import android.os.Environment;
59import android.os.Handler;
The Android Open Source Project0c908882009-03-03 19:32:16 -080060import android.os.Message;
61import android.os.PowerManager;
62import android.os.Process;
The Android Open Source Project0c908882009-03-03 19:32:16 -080063import android.os.ServiceManager;
64import android.os.SystemClock;
The Android Open Source Project0c908882009-03-03 19:32:16 -080065import android.provider.Browser;
Cary Clark5e335a32009-09-22 14:53:11 -040066import android.provider.ContactsContract;
67import android.provider.ContactsContract.Intents.Insert;
The Android Open Source Project0c908882009-03-03 19:32:16 -080068import android.provider.Downloads;
69import android.provider.MediaStore;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050070import android.speech.RecognizerResultsIntent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080071import android.text.IClipboard;
72import android.text.TextUtils;
73import android.text.format.DateFormat;
Leon Scrogginsb94bf272009-09-25 15:22:08 -040074import android.util.AttributeSet;
The Android Open Source Project0c908882009-03-03 19:32:16 -080075import android.util.Log;
Dianne Hackborn385effd2010-02-24 20:03:04 -080076import android.util.Patterns;
The Android Open Source Project0c908882009-03-03 19:32:16 -080077import android.view.ContextMenu;
78import android.view.Gravity;
79import android.view.KeyEvent;
80import android.view.LayoutInflater;
81import android.view.Menu;
82import android.view.MenuInflater;
83import android.view.MenuItem;
84import android.view.View;
85import android.view.ViewGroup;
86import android.view.Window;
87import android.view.WindowManager;
88import android.view.ContextMenu.ContextMenuInfo;
89import android.view.MenuItem.OnMenuItemClickListener;
The Android Open Source Project0c908882009-03-03 19:32:16 -080090import android.webkit.CookieManager;
91import android.webkit.CookieSyncManager;
92import android.webkit.DownloadListener;
93import android.webkit.HttpAuthHandler;
Grace Klobab4da0ad2009-05-14 14:45:40 -070094import android.webkit.PluginManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080095import android.webkit.SslErrorHandler;
96import android.webkit.URLUtil;
Leon Clarkecb6cc862009-09-29 18:35:13 +010097import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080098import android.webkit.WebChromeClient;
99import android.webkit.WebHistoryItem;
100import android.webkit.WebIconDatabase;
101import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800102import android.widget.EditText;
103import android.widget.FrameLayout;
104import android.widget.LinearLayout;
105import android.widget.TextView;
106import android.widget.Toast;
Fred Quintana752b6562009-12-18 10:18:26 -0800107import android.accounts.Account;
108import android.accounts.AccountManager;
109import android.accounts.AccountManagerFuture;
110import android.accounts.AuthenticatorException;
111import android.accounts.OperationCanceledException;
112import android.accounts.AccountManagerCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800113
Bjorn Bringert10d1cca2010-02-10 14:22:12 +0000114import com.android.common.Search;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500115import com.android.common.speech.LoggingEvents;
Dan Egnor5ee906c2009-11-18 12:11:49 -0800116
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400117import java.io.ByteArrayOutputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118import java.io.File;
Ben Murdoch4f75ba22009-10-27 11:48:28 +0000119import java.io.IOException;
120import java.io.InputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121import java.net.MalformedURLException;
122import java.net.URI;
Dianne Hackborn99189432009-06-17 18:06:18 -0700123import java.net.URISyntaxException;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124import java.net.URL;
125import java.net.URLEncoder;
126import java.text.ParseException;
127import java.util.Date;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800128import java.util.HashMap;
Andrei Popescu30995e72010-02-09 16:59:58 +0000129import java.util.HashSet;
Grace Kloba00f54c52010-01-27 14:53:51 -0800130import java.util.Iterator;
Andrei Popescu30995e72010-02-09 16:59:58 +0000131import java.util.List;
Grace Kloba068e48b2010-01-26 18:11:27 -0800132import java.util.Map;
Andrei Popescu30995e72010-02-09 16:59:58 +0000133import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800134import java.util.regex.Matcher;
135import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800136
137public class BrowserActivity extends Activity
Shimeng (Simon) Wang98d5fce2010-03-16 13:23:39 -0700138 implements View.OnCreateContextMenuListener, DownloadListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800139
Dave Bort31a6d1c2009-04-13 15:56:49 -0700140 /* Define some aliases to make these debugging flags easier to refer to.
141 * This file imports android.provider.Browser, so we can't just refer to "Browser.DEBUG".
142 */
143 private final static boolean DEBUG = com.android.browser.Browser.DEBUG;
144 private final static boolean LOGV_ENABLED = com.android.browser.Browser.LOGV_ENABLED;
145 private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
146
Satish Sampath565505b2009-05-29 15:37:27 +0100147 // These are single-character shortcuts for searching popular sources.
148 private static final int SHORTCUT_INVALID = 0;
149 private static final int SHORTCUT_GOOGLE_SEARCH = 1;
150 private static final int SHORTCUT_WIKIPEDIA_SEARCH = 2;
151 private static final int SHORTCUT_DICTIONARY_SEARCH = 3;
152 private static final int SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH = 4;
153
Cary Clarka9771242009-08-11 16:42:26 -0400154 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800155 @Override
156 public Void doInBackground(File... files) {
157 if (files != null) {
158 for (File f : files) {
Cary Clarkd6be1752009-08-12 12:56:42 -0400159 if (!f.delete()) {
160 Log.e(LOGTAG, f.getPath() + " was not deleted");
161 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800162 }
163 }
164 return null;
165 }
166 }
167
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400168 /**
169 * This layout holds everything you see below the status bar, including the
170 * error console, the custom view container, and the webviews.
171 */
172 private FrameLayout mBrowserFrameLayout;
Leon Scroggins81db3662009-06-04 17:45:11 -0400173
Grace Kloba22ac16e2009-10-07 18:00:23 -0700174 @Override
175 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700176 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800177 Log.v(LOGTAG, this + " onStart");
178 }
179 super.onCreate(icicle);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800180 // test the browser in OpenGL
181 // requestWindowFeature(Window.FEATURE_OPENGL);
182
Mike Reedd334bf52010-01-26 15:21:44 -0500183 // enable this to test the browser in 32bit
184 if (false) {
185 getWindow().setFormat(PixelFormat.RGBX_8888);
186 BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
187 }
188
The Android Open Source Project0c908882009-03-03 19:32:16 -0800189 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
190
191 mResolver = getContentResolver();
192
Grace Kloba0923d692009-09-23 21:37:25 -0700193 // If this was a web search request, pass it on to the default web
194 // search provider and finish this activity.
195 if (handleWebSearchIntent(getIntent())) {
196 finish();
197 return;
198 }
199
The Android Open Source Project0c908882009-03-03 19:32:16 -0800200 mSecLockIcon = Resources.getSystem().getDrawable(
201 android.R.drawable.ic_secure);
202 mMixLockIcon = Resources.getSystem().getDrawable(
203 android.R.drawable.ic_partial_secure);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800204
Leon Scroggins81db3662009-06-04 17:45:11 -0400205 FrameLayout frameLayout = (FrameLayout) getWindow().getDecorView()
206 .findViewById(com.android.internal.R.id.content);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400207 mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(this)
208 .inflate(R.layout.custom_screen, null);
209 mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(
210 R.id.main_content);
211 mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout
212 .findViewById(R.id.error_console);
213 mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
214 .findViewById(R.id.fullscreen_custom_content);
215 frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
Leon Scroggins68579392009-09-15 15:31:54 -0400216 mTitleBar = new TitleBar(this);
Grace Kloba1542f742010-03-17 21:11:15 -0700217 // mTitleBar will be always shown in the fully loaded mode
218 mTitleBar.setProgress(100);
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400219 mFakeTitleBar = new TitleBar(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800220
221 // Create the tab control and our initial tab
222 mTabControl = new TabControl(this);
223
224 // Open the icon database and retain all the bookmark urls for favicons
225 retainIconsOnStartup();
226
227 // Keep a settings instance handy.
228 mSettings = BrowserSettings.getInstance();
229 mSettings.setTabControl(mTabControl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230
231 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
232 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
233
Patrick Scott6adacc92010-03-05 08:24:51 -0500234 // Find out if the network is currently up.
235 ConnectivityManager cm = (ConnectivityManager) getSystemService(
236 Context.CONNECTIVITY_SERVICE);
237 NetworkInfo info = cm.getActiveNetworkInfo();
238 if (info != null) {
239 mIsNetworkUp = info.isAvailable();
240 }
241
Grace Klobaa34f6862009-07-31 16:28:17 -0700242 /* enables registration for changes in network status from
243 http stack */
244 mNetworkStateChangedFilter = new IntentFilter();
245 mNetworkStateChangedFilter.addAction(
246 ConnectivityManager.CONNECTIVITY_ACTION);
247 mNetworkStateIntentReceiver = new BroadcastReceiver() {
248 @Override
249 public void onReceive(Context context, Intent intent) {
250 if (intent.getAction().equals(
251 ConnectivityManager.CONNECTIVITY_ACTION)) {
Andrei Popescue4c98462010-02-19 15:44:13 +0000252
253 NetworkInfo info = intent.getParcelableExtra(
254 ConnectivityManager.EXTRA_NETWORK_INFO);
255 String typeName = info.getTypeName();
256 String subtypeName = info.getSubtypeName();
257 sendNetworkType(typeName.toLowerCase(),
258 (subtypeName != null ? subtypeName.toLowerCase() : ""));
259
260 onNetworkToggle(info.isAvailable());
Grace Klobaa34f6862009-07-31 16:28:17 -0700261 }
262 }
263 };
264
Grace Kloba615c6c92009-08-03 10:22:44 -0700265 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
266 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
267 filter.addDataScheme("package");
268 mPackageInstallationReceiver = new BroadcastReceiver() {
269 @Override
270 public void onReceive(Context context, Intent intent) {
271 final String action = intent.getAction();
272 final String packageName = intent.getData()
273 .getSchemeSpecificPart();
274 final boolean replacing = intent.getBooleanExtra(
275 Intent.EXTRA_REPLACING, false);
276 if (Intent.ACTION_PACKAGE_REMOVED.equals(action) && replacing) {
277 // if it is replacing, refreshPlugins() when adding
278 return;
279 }
Andrei Popescu30995e72010-02-09 16:59:58 +0000280
281 if (sGoogleApps.contains(packageName)) {
282 BrowserActivity.this.packageChanged(packageName,
283 Intent.ACTION_PACKAGE_ADDED.equals(action));
284 }
285
Grace Kloba615c6c92009-08-03 10:22:44 -0700286 PackageManager pm = BrowserActivity.this.getPackageManager();
287 PackageInfo pkgInfo = null;
288 try {
289 pkgInfo = pm.getPackageInfo(packageName,
290 PackageManager.GET_PERMISSIONS);
291 } catch (PackageManager.NameNotFoundException e) {
292 return;
293 }
294 if (pkgInfo != null) {
295 String permissions[] = pkgInfo.requestedPermissions;
296 if (permissions == null) {
297 return;
298 }
299 boolean permissionOk = false;
300 for (String permit : permissions) {
301 if (PluginManager.PLUGIN_PERMISSION.equals(permit)) {
302 permissionOk = true;
303 break;
304 }
305 }
306 if (permissionOk) {
307 PluginManager.getInstance(BrowserActivity.this)
308 .refreshPlugins(
309 Intent.ACTION_PACKAGE_ADDED
310 .equals(action));
311 }
312 }
313 }
314 };
315 registerReceiver(mPackageInstallationReceiver, filter);
316
The Android Open Source Project0c908882009-03-03 19:32:16 -0800317 if (!mTabControl.restoreState(icicle)) {
318 // clear up the thumbnail directory if we can't restore the state as
319 // none of the files in the directory are referenced any more.
320 new ClearThumbnails().execute(
321 mTabControl.getThumbnailDir().listFiles());
Grace Klobaaab3f092009-07-30 12:29:51 -0700322 // there is no quit on Android. But if we can't restore the state,
323 // we can treat it as a new Browser, remove the old session cookies.
324 CookieManager.getInstance().removeSessionCookie();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800325 final Intent intent = getIntent();
326 final Bundle extra = intent.getExtras();
327 // Create an initial tab.
328 // If the intent is ACTION_VIEW and data is not null, the Browser is
329 // invoked to view the content by another application. In this case,
330 // the tab will be close when exit.
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700331 UrlData urlData = getUrlDataFromIntent(intent);
332
Leon Scroggins58d56c62010-01-28 15:12:40 -0500333 String action = intent.getAction();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700334 final Tab t = mTabControl.createNewTab(
Leon Scroggins58d56c62010-01-28 15:12:40 -0500335 (Intent.ACTION_VIEW.equals(action) &&
336 intent.getData() != null)
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500337 || RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
338 .equals(action),
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700339 intent.getStringExtra(Browser.EXTRA_APPLICATION_ID), urlData.mUrl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800340 mTabControl.setCurrentTab(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800341 attachTabToContentView(t);
342 WebView webView = t.getWebView();
343 if (extra != null) {
344 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
345 if (scale > 0 && scale <= 1000) {
346 webView.setInitialScale(scale);
347 }
348 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800349
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700350 if (urlData.isEmpty()) {
Shimeng (Simon) Wang98d5fce2010-03-16 13:23:39 -0700351 loadUrl(webView, mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800352 } else {
Patrick Scott9d53da02010-02-19 10:19:01 -0500353 loadUrlDataIn(t, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800354 }
355 } else {
356 // TabControl.restoreState() will create a new tab even if
Leon Scroggins1f005d32009-08-10 17:36:42 -0400357 // restoring the state fails.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800358 attachTabToContentView(mTabControl.getCurrentTab());
359 }
Grace Kloba615c6c92009-08-03 10:22:44 -0700360
Feng Qianb3c02da2009-06-29 15:58:08 -0700361 // Read JavaScript flags if it exists.
362 String jsFlags = mSettings.getJsFlags();
363 if (jsFlags.trim().length() != 0) {
364 mTabControl.getCurrentWebView().setJsFlags(jsFlags);
365 }
Andrei Popescu30995e72010-02-09 16:59:58 +0000366 // Work out which packages are installed on the system.
367 getInstalledPackages();
Bjorn Bringerta7611812010-03-24 11:12:02 +0000368
369 // Start watching the default geolocation permissions
370 mSystemAllowGeolocationOrigins
371 = new SystemAllowGeolocationOrigins(getApplicationContext());
372 mSystemAllowGeolocationOrigins.start();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800373 }
374
Leon Scroggins58d56c62010-01-28 15:12:40 -0500375 /**
376 * Feed the previously stored results strings to the BrowserProvider so that
377 * the SearchDialog will show them instead of the standard searches.
378 * @param result String to show on the editable line of the SearchDialog.
379 */
380 /* package */ void showVoiceSearchResults(String result) {
381 ContentProviderClient client = mResolver.acquireContentProviderClient(
382 Browser.BOOKMARKS_URI);
383 ContentProvider prov = client.getLocalContentProvider();
384 BrowserProvider bp = (BrowserProvider) prov;
385 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
386 client.release();
387
Leon Scrogginsfbb3f152010-03-09 17:26:56 -0500388 Bundle bundle = createGoogleSearchSourceBundle(
389 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
390 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
391 startSearch(result, false, bundle, false);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500392 }
393
The Android Open Source Project0c908882009-03-03 19:32:16 -0800394 @Override
395 protected void onNewIntent(Intent intent) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700396 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800397 // When a tab is closed on exit, the current tab index is set to -1.
398 // Reset before proceed as Browser requires the current tab to be set.
399 if (current == null) {
400 // Try to reset the tab in case the index was incorrect.
401 current = mTabControl.getTab(0);
402 if (current == null) {
403 // No tabs at all so just ignore this intent.
404 return;
405 }
406 mTabControl.setCurrentTab(current);
407 attachTabToContentView(current);
408 resetTitleAndIcon(current.getWebView());
409 }
410 final String action = intent.getAction();
411 final int flags = intent.getFlags();
412 if (Intent.ACTION_MAIN.equals(action) ||
413 (flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
414 // just resume the browser
415 return;
416 }
Leon Scrogginsb8a844d2010-03-18 15:06:15 -0400417 // In case the SearchDialog is open.
418 ((SearchManager) getSystemService(Context.SEARCH_SERVICE))
419 .stopSearch();
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500420 boolean activateVoiceSearch = RecognizerResultsIntent
421 .ACTION_VOICE_SEARCH_RESULTS.equals(action);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800422 if (Intent.ACTION_VIEW.equals(action)
423 || Intent.ACTION_SEARCH.equals(action)
424 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
Leon Scroggins58d56c62010-01-28 15:12:40 -0500425 || Intent.ACTION_WEB_SEARCH.equals(action)
426 || activateVoiceSearch) {
Leon Scroggins3b18ce32010-02-08 17:35:59 -0500427 if (current.isInVoiceSearchMode()) {
428 String title = current.getVoiceDisplayTitle();
429 if (title != null && title.equals(intent.getStringExtra(
430 SearchManager.QUERY))) {
431 // The user submitted the same search as the last voice
432 // search, so do nothing.
433 return;
434 }
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500435 if (Intent.ACTION_SEARCH.equals(action)
436 && current.voiceSearchSourceIsGoogle()) {
437 Intent logIntent = new Intent(
438 LoggingEvents.ACTION_LOG_EVENT);
439 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
440 LoggingEvents.VoiceSearch.QUERY_UPDATED);
441 logIntent.putExtra(
442 LoggingEvents.VoiceSearch.EXTRA_QUERY_UPDATED_VALUE,
443 intent.getDataString());
444 sendBroadcast(logIntent);
445 // Note, onPageStarted will revert the voice title bar
446 // When http://b/issue?id=2379215 is fixed, we should update
447 // the title bar here.
448 }
Leon Scroggins3b18ce32010-02-08 17:35:59 -0500449 }
Satish Sampath565505b2009-05-29 15:37:27 +0100450 // If this was a search request (e.g. search query directly typed into the address bar),
451 // pass it on to the default web search provider.
452 if (handleWebSearchIntent(intent)) {
453 return;
454 }
455
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700456 UrlData urlData = getUrlDataFromIntent(intent);
457 if (urlData.isEmpty()) {
458 urlData = new UrlData(mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800459 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700460
Grace Klobacc634032009-07-28 15:58:19 -0700461 final String appId = intent
462 .getStringExtra(Browser.EXTRA_APPLICATION_ID);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500463 if ((Intent.ACTION_VIEW.equals(action) || activateVoiceSearch)
Grace Klobacc634032009-07-28 15:58:19 -0700464 && !getPackageName().equals(appId)
465 && (flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700466 Tab appTab = mTabControl.getTabFromId(appId);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700467 if (appTab != null) {
468 Log.i(LOGTAG, "Reusing tab for " + appId);
469 // Dismiss the subwindow if applicable.
470 dismissSubWindow(appTab);
471 // Since we might kill the WebView, remove it from the
472 // content view first.
473 removeTabFromContentView(appTab);
474 // Recreate the main WebView after destroying the old one.
475 // If the WebView has the same original url and is on that
476 // page, it can be reused.
477 boolean needsLoad =
Leon Scroggins6eac63e2010-03-15 18:19:14 -0400478 mTabControl.recreateWebView(appTab, urlData);
Ben Murdochbff2d602009-07-01 20:19:05 +0100479
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700480 if (current != appTab) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400481 switchToTab(mTabControl.getTabIndex(appTab));
482 if (needsLoad) {
Patrick Scott9d53da02010-02-19 10:19:01 -0500483 loadUrlDataIn(appTab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400484 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700485 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400486 // If the tab was the current tab, we have to attach
487 // it to the view system again.
488 attachTabToContentView(appTab);
489 if (needsLoad) {
Patrick Scott9d53da02010-02-19 10:19:01 -0500490 loadUrlDataIn(appTab, urlData);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700491 }
492 }
493 return;
Patrick Scottcd115892009-07-16 09:42:58 -0400494 } else {
495 // No matching application tab, try to find a regular tab
496 // with a matching url.
497 appTab = mTabControl.findUnusedTabWithUrl(urlData.mUrl);
Leon Scroggins25515f82009-08-19 15:31:58 -0400498 if (appTab != null) {
499 if (current != appTab) {
500 switchToTab(mTabControl.getTabIndex(appTab));
501 }
502 // Otherwise, we are already viewing the correct tab.
Patrick Scottcd115892009-07-16 09:42:58 -0400503 } else {
504 // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
505 // will be opened in a new tab unless we have reached
506 // MAX_TABS. Then the url will be opened in the current
507 // tab. If a new tab is created, it will have "true" for
508 // exit on close.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400509 openTabAndShow(urlData, true, appId);
Patrick Scottcd115892009-07-16 09:42:58 -0400510 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700511 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800512 } else {
Grace Kloba638d3f42009-11-23 10:35:04 -0800513 if (!urlData.isEmpty()
514 && urlData.mUrl.startsWith("about:debug")) {
515 if ("about:debug.dom".equals(urlData.mUrl)) {
516 current.getWebView().dumpDomTree(false);
517 } else if ("about:debug.dom.file".equals(urlData.mUrl)) {
518 current.getWebView().dumpDomTree(true);
519 } else if ("about:debug.render".equals(urlData.mUrl)) {
520 current.getWebView().dumpRenderTree(false);
521 } else if ("about:debug.render.file".equals(urlData.mUrl)) {
522 current.getWebView().dumpRenderTree(true);
523 } else if ("about:debug.display".equals(urlData.mUrl)) {
524 current.getWebView().dumpDisplayTree();
Mike Reed9b78e1d2010-01-13 14:40:52 -0800525 } else if (urlData.mUrl.startsWith("about:debug.drag")) {
526 int index = urlData.mUrl.codePointAt(16) - '0';
527 if (index <= 0 || index > 9) {
528 current.getWebView().setDragTracker(null);
529 } else {
530 current.getWebView().setDragTracker(new MeshTracker(index));
531 }
Grace Kloba638d3f42009-11-23 10:35:04 -0800532 } else {
533 mSettings.toggleDebugSettings();
534 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800535 return;
536 }
Leon Scroggins1f005d32009-08-10 17:36:42 -0400537 // Get rid of the subwindow if it exists
538 dismissSubWindow(current);
Patrick Scott9d53da02010-02-19 10:19:01 -0500539 loadUrlDataIn(current, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800540 }
541 }
542 }
543
Satish Sampath565505b2009-05-29 15:37:27 +0100544 private int parseUrlShortcut(String url) {
545 if (url == null) return SHORTCUT_INVALID;
546
547 // FIXME: quick search, need to be customized by setting
548 if (url.length() > 2 && url.charAt(1) == ' ') {
549 switch (url.charAt(0)) {
550 case 'g': return SHORTCUT_GOOGLE_SEARCH;
551 case 'w': return SHORTCUT_WIKIPEDIA_SEARCH;
552 case 'd': return SHORTCUT_DICTIONARY_SEARCH;
553 case 'l': return SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH;
554 }
555 }
556 return SHORTCUT_INVALID;
557 }
558
559 /**
560 * Launches the default web search activity with the query parameters if the given intent's data
561 * are identified as plain search terms and not URLs/shortcuts.
562 * @return true if the intent was handled and web search activity was launched, false if not.
563 */
564 private boolean handleWebSearchIntent(Intent intent) {
565 if (intent == null) return false;
566
567 String url = null;
568 final String action = intent.getAction();
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500569 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS.equals(
570 action)) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500571 return false;
572 }
Satish Sampath565505b2009-05-29 15:37:27 +0100573 if (Intent.ACTION_VIEW.equals(action)) {
Grace Kloba1e705052009-09-29 13:13:36 -0700574 Uri data = intent.getData();
575 if (data != null) url = data.toString();
Satish Sampath565505b2009-05-29 15:37:27 +0100576 } else if (Intent.ACTION_SEARCH.equals(action)
577 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
578 || Intent.ACTION_WEB_SEARCH.equals(action)) {
579 url = intent.getStringExtra(SearchManager.QUERY);
580 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100581 return handleWebSearchRequest(url, intent.getBundleExtra(SearchManager.APP_DATA),
582 intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
Satish Sampath565505b2009-05-29 15:37:27 +0100583 }
584
585 /**
586 * Launches the default web search activity with the query parameters if the given url string
587 * was identified as plain search terms and not URL/shortcut.
588 * @return true if the request was handled and web search activity was launched, false if not.
589 */
Bjorn Bringert04851702009-09-22 10:36:01 +0100590 private boolean handleWebSearchRequest(String inUrl, Bundle appData, String extraData) {
Satish Sampath565505b2009-05-29 15:37:27 +0100591 if (inUrl == null) return false;
592
593 // In general, we shouldn't modify URL from Intent.
594 // But currently, we get the user-typed URL from search box as well.
595 String url = fixUrl(inUrl).trim();
596
597 // URLs and site specific search shortcuts are handled by the regular flow of control, so
598 // return early.
Dan Egnor5ee906c2009-11-18 12:11:49 -0800599 if (Patterns.WEB_URL.matcher(url).matches()
Satish Sampathbc5b9f32009-06-04 18:21:40 +0100600 || ACCEPTED_URI_SCHEMA.matcher(url).matches()
Satish Sampath565505b2009-05-29 15:37:27 +0100601 || parseUrlShortcut(url) != SHORTCUT_INVALID) {
602 return false;
603 }
604
Leon Scroggins8d06e362010-03-24 14:45:57 -0400605 final ContentResolver cr = mResolver;
606 final String newUrl = url;
607 new AsyncTask<Void, Void, Void>() {
608 protected Void doInBackground(Void... unused) {
609 Browser.updateVisitedHistory(cr, newUrl, false);
610 Browser.addSearchUrl(cr, newUrl);
611 return null;
612 }
613 }.execute();
Satish Sampath565505b2009-05-29 15:37:27 +0100614
615 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
616 intent.addCategory(Intent.CATEGORY_DEFAULT);
617 intent.putExtra(SearchManager.QUERY, url);
Satish Sampath15e9f2d2009-06-23 22:29:49 +0100618 if (appData != null) {
619 intent.putExtra(SearchManager.APP_DATA, appData);
620 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100621 if (extraData != null) {
622 intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
623 }
Grace Klobacc634032009-07-28 15:58:19 -0700624 intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName());
Satish Sampath565505b2009-05-29 15:37:27 +0100625 startActivity(intent);
626
627 return true;
628 }
629
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700630 private UrlData getUrlDataFromIntent(Intent intent) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500631 String url = "";
Grace Kloba068e48b2010-01-26 18:11:27 -0800632 Map<String, String> headers = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800633 if (intent != null) {
634 final String action = intent.getAction();
635 if (Intent.ACTION_VIEW.equals(action)) {
636 url = smartUrlFilter(intent.getData());
637 if (url != null && url.startsWith("content:")) {
638 /* Append mimetype so webview knows how to display */
639 String mimeType = intent.resolveType(getContentResolver());
640 if (mimeType != null) {
641 url += "?" + mimeType;
642 }
643 }
Grace Kloba068e48b2010-01-26 18:11:27 -0800644 if (url != null && url.startsWith("http")) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800645 final Bundle pairs = intent
646 .getBundleExtra(Browser.EXTRA_HEADERS);
Grace Kloba2d508ed2010-01-28 11:39:15 -0800647 if (pairs != null && !pairs.isEmpty()) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800648 Iterator<String> iter = pairs.keySet().iterator();
Grace Kloba068e48b2010-01-26 18:11:27 -0800649 headers = new HashMap<String, String>();
Grace Kloba00f54c52010-01-27 14:53:51 -0800650 while (iter.hasNext()) {
651 String key = iter.next();
652 headers.put(key, pairs.getString(key));
Grace Kloba068e48b2010-01-26 18:11:27 -0800653 }
654 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700655 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800656 } else if (Intent.ACTION_SEARCH.equals(action)
657 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
658 || Intent.ACTION_WEB_SEARCH.equals(action)) {
659 url = intent.getStringExtra(SearchManager.QUERY);
660 if (url != null) {
661 mLastEnteredUrl = url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800662 // In general, we shouldn't modify URL from Intent.
663 // But currently, we get the user-typed URL from search box as well.
664 url = fixUrl(url);
665 url = smartUrlFilter(url);
Leon Scroggins8d06e362010-03-24 14:45:57 -0400666 final ContentResolver cr = mResolver;
667 final String newUrl = url;
668 new AsyncTask<Void, Void, Void>() {
669 protected Void doInBackground(Void... unused) {
670 Browser.updateVisitedHistory(cr, newUrl, false);
671 return null;
672 }
673 }.execute();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
675 if (url.contains(searchSource)) {
676 String source = null;
677 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
678 if (appData != null) {
Bjorn Bringert10d1cca2010-02-10 14:22:12 +0000679 source = appData.getString(Search.SOURCE);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800680 }
681 if (TextUtils.isEmpty(source)) {
682 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
683 }
684 url = url.replace(searchSource, "&source=android-"+source+"&");
685 }
686 }
687 }
688 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500689 return new UrlData(url, headers, intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800690 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500691 /* package */ void showVoiceTitleBar(String title) {
692 mTitleBar.setInVoiceMode(true);
693 mFakeTitleBar.setInVoiceMode(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800694
Leon Scroggins58d56c62010-01-28 15:12:40 -0500695 mTitleBar.setDisplayTitle(title);
696 mFakeTitleBar.setDisplayTitle(title);
697 }
698 /* package */ void revertVoiceTitleBar() {
699 mTitleBar.setInVoiceMode(false);
700 mFakeTitleBar.setInVoiceMode(false);
701
Leon Scroggins003a5dd2010-03-10 12:13:14 -0500702 mTitleBar.setDisplayTitle(mUrl);
703 mFakeTitleBar.setDisplayTitle(mUrl);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500704 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800705 /* package */ static String fixUrl(String inUrl) {
Cary Clark652ff872009-09-10 13:34:44 -0400706 // FIXME: Converting the url to lower case
707 // duplicates functionality in smartUrlFilter().
708 // However, changing all current callers of fixUrl to
709 // call smartUrlFilter in addition may have unwanted
710 // consequences, and is deferred for now.
711 int colon = inUrl.indexOf(':');
712 boolean allLower = true;
713 for (int index = 0; index < colon; index++) {
714 char ch = inUrl.charAt(index);
715 if (!Character.isLetter(ch)) {
716 break;
717 }
718 allLower &= Character.isLowerCase(ch);
719 if (index == colon - 1 && !allLower) {
720 inUrl = inUrl.substring(0, colon).toLowerCase()
721 + inUrl.substring(colon);
722 }
723 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800724 if (inUrl.startsWith("http://") || inUrl.startsWith("https://"))
725 return inUrl;
726 if (inUrl.startsWith("http:") ||
727 inUrl.startsWith("https:")) {
728 if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) {
729 inUrl = inUrl.replaceFirst("/", "//");
730 } else inUrl = inUrl.replaceFirst(":", "://");
731 }
732 return inUrl;
733 }
734
Grace Kloba22ac16e2009-10-07 18:00:23 -0700735 @Override
736 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800737 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700738 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800739 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
740 }
741
742 if (!mActivityInPause) {
743 Log.e(LOGTAG, "BrowserActivity is already resumed.");
744 return;
745 }
746
Mike Reed7bfa63b2009-05-28 11:08:32 -0400747 mTabControl.resumeCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800748 mActivityInPause = false;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400749 resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800750
751 if (mWakeLock.isHeld()) {
752 mHandler.removeMessages(RELEASE_WAKELOCK);
753 mWakeLock.release();
754 }
755
The Android Open Source Project0c908882009-03-03 19:32:16 -0800756 registerReceiver(mNetworkStateIntentReceiver,
757 mNetworkStateChangedFilter);
758 WebView.enablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800759 }
760
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400761 /**
762 * Since the actual title bar is embedded in the WebView, and removing it
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400763 * would change its appearance, use a different TitleBar to show overlayed
764 * at the top of the screen, when the menu is open or the page is loading.
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400765 */
766 private TitleBar mFakeTitleBar;
767
768 /**
769 * Keeps track of whether the options menu is open. This is important in
770 * determining whether to show or hide the title bar overlay.
771 */
772 private boolean mOptionsMenuOpen;
773
774 /**
775 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
776 * of whether the configuration has changed. The first onMenuOpened call
777 * after a configuration change is simply a reopening of the same menu
778 * (i.e. mIconView did not change).
779 */
780 private boolean mConfigChanged;
781
782 /**
783 * Whether or not the options menu is in its smaller, icon menu form. When
784 * true, we want the title bar overlay to be up. When false, we do not.
785 * Only meaningful if mOptionsMenuOpen is true.
786 */
787 private boolean mIconView;
788
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400789 @Override
790 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400791 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
792 if (mOptionsMenuOpen) {
793 if (mConfigChanged) {
794 // We do not need to make any changes to the state of the
795 // title bar, since the only thing that happened was a
796 // change in orientation
797 mConfigChanged = false;
798 } else {
799 if (mIconView) {
800 // Switching the menu to expanded view, so hide the
801 // title bar.
802 hideFakeTitleBar();
803 mIconView = false;
804 } else {
805 // Switching the menu back to icon view, so show the
806 // title bar once again.
807 showFakeTitleBar();
808 mIconView = true;
809 }
810 }
811 } else {
812 // The options menu is closed, so open it, and show the title
813 showFakeTitleBar();
814 mOptionsMenuOpen = true;
815 mConfigChanged = false;
816 mIconView = true;
817 }
818 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400819 return true;
820 }
821
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400822 private void showFakeTitleBar() {
Patrick Scottf8de8ec2009-09-21 16:37:29 -0400823 final View decor = getWindow().peekDecorView();
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400824 if (mFakeTitleBar.getParent() == null && mActiveTabsPage == null
Patrick Scottf8de8ec2009-09-21 16:37:29 -0400825 && !mActivityInPause && decor != null
826 && decor.getWindowToken() != null) {
Cary Clarka0464552009-09-29 13:00:45 -0400827 Rect visRect = new Rect();
828 if (!mBrowserFrameLayout.getGlobalVisibleRect(visRect)) {
829 if (LOGD_ENABLED) {
830 Log.d(LOGTAG, "showFakeTitleBar visRect failed");
831 }
832 return;
833 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400834
835 WindowManager manager
836 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
837
838 // Add the title bar to the window manager so it can receive touches
839 // while the menu is up
840 WindowManager.LayoutParams params
841 = new WindowManager.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -0800842 ViewGroup.LayoutParams.MATCH_PARENT,
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400843 ViewGroup.LayoutParams.WRAP_CONTENT,
844 WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL,
845 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
Leon Scroggins68549862009-09-21 16:02:01 -0400846 PixelFormat.TRANSLUCENT);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400847 params.gravity = Gravity.TOP;
Leon Scrogginsa27ff192009-09-14 12:58:04 -0400848 WebView mainView = mTabControl.getCurrentWebView();
Leon Scroggins68549862009-09-21 16:02:01 -0400849 boolean atTop = mainView != null && mainView.getScrollY() == 0;
Leon Scroggins83932c72009-09-30 11:55:54 -0400850 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400851 // XXX : Without providing an offset, the fake title bar will be
852 // placed underneath the status bar. Use the global visible rect
853 // of mBrowserFrameLayout to determine the bottom of the status bar
Cary Clarka0464552009-09-29 13:00:45 -0400854 params.y = visRect.top;
Grace Kloba13f9ace2010-03-18 21:44:14 -0700855 manager.addView(mFakeTitleBar, params);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400856 }
857 }
858
859 @Override
860 public void onOptionsMenuClosed(Menu menu) {
861 mOptionsMenuOpen = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -0400862 if (!mInLoad) {
863 hideFakeTitleBar();
864 } else if (!mIconView) {
865 // The page is currently loading, and we are in expanded mode, so
866 // we were not showing the menu. Show it once again. It will be
867 // removed when the page finishes.
868 showFakeTitleBar();
869 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400870 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700871
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400872 private void hideFakeTitleBar() {
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400873 if (mFakeTitleBar.getParent() == null) return;
Leon Scroggins20329572009-09-23 17:42:41 -0400874 WindowManager.LayoutParams params = (WindowManager.LayoutParams)
Grace Kloba13f9ace2010-03-18 21:44:14 -0700875 mFakeTitleBar.getLayoutParams();
Leon Scroggins20329572009-09-23 17:42:41 -0400876 WebView mainView = mTabControl.getCurrentWebView();
877 // Although we decided whether or not to animate based on the current
878 // scroll position, the scroll position may have changed since the
879 // fake title bar was displayed. Make sure it has the appropriate
880 // animation/lack thereof before removing.
881 params.windowAnimations = mainView != null && mainView.getScrollY() == 0
Leon Scroggins83932c72009-09-30 11:55:54 -0400882 ? 0 : R.style.TitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400883 WindowManager manager
884 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Grace Kloba13f9ace2010-03-18 21:44:14 -0700885 manager.updateViewLayout(mFakeTitleBar, params);
886 manager.removeView(mFakeTitleBar);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400887 }
888
The Android Open Source Project0c908882009-03-03 19:32:16 -0800889 /**
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400890 * Special method for the fake title bar to call when displaying its context
891 * menu, since it is in its own Window, and its parent does not show a
892 * context menu.
893 */
894 /* package */ void showTitleBarContextMenu() {
Cary Clark65f4a3c2009-09-28 17:05:06 -0400895 if (null == mTitleBar.getParent()) {
896 return;
897 }
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400898 openContextMenu(mTitleBar);
899 }
900
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400901 @Override
902 public void onContextMenuClosed(Menu menu) {
903 super.onContextMenuClosed(menu);
904 if (mInLoad) {
905 showFakeTitleBar();
906 }
907 }
908
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400909 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800910 * onSaveInstanceState(Bundle map)
911 * onSaveInstanceState is called right before onStop(). The map contains
912 * the saved state.
913 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700914 @Override
915 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700916 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800917 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
918 }
919 // the default implementation requires each view to have an id. As the
920 // browser handles the state itself and it doesn't use id for the views,
921 // don't call the default implementation. Otherwise it will trigger the
922 // warning like this, "couldn't save which view has focus because the
923 // focused view XXX has no id".
924
925 // Save all the tabs
926 mTabControl.saveState(outState);
927 }
928
Grace Kloba22ac16e2009-10-07 18:00:23 -0700929 @Override
930 protected void onPause() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800931 super.onPause();
932
933 if (mActivityInPause) {
934 Log.e(LOGTAG, "BrowserActivity is already paused.");
935 return;
936 }
937
Mike Reed7bfa63b2009-05-28 11:08:32 -0400938 mTabControl.pauseCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800939 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400940 if (mTabControl.getCurrentIndex() >= 0 && !pauseWebViewTimers()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800941 mWakeLock.acquire();
942 mHandler.sendMessageDelayed(mHandler
943 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
944 }
945
Leon Scrogginsa2ab6a72009-09-11 11:49:52 -0400946 // FIXME: This removes the active tabs page and resets the menu to
947 // MAIN_MENU. A better solution might be to do this work in onNewIntent
948 // but then we would need to save it in onSaveInstanceState and restore
949 // it in onCreate/onRestoreInstanceState
950 if (mActiveTabsPage != null) {
951 removeActiveTabPage(true);
952 }
953
The Android Open Source Project0c908882009-03-03 19:32:16 -0800954 cancelStopToast();
955
956 // unregister network state listener
957 unregisterReceiver(mNetworkStateIntentReceiver);
958 WebView.disablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800959 }
960
Grace Kloba22ac16e2009-10-07 18:00:23 -0700961 @Override
962 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700963 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800964 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
965 }
966 super.onDestroy();
Grace Kloba0923d692009-09-23 21:37:25 -0700967
Leon Scroggins8d5fa432009-10-02 15:55:59 -0400968 if (mUploadMessage != null) {
969 mUploadMessage.onReceiveValue(null);
970 mUploadMessage = null;
971 }
972
Grace Kloba0923d692009-09-23 21:37:25 -0700973 if (mTabControl == null) return;
974
Grace Kloba1fc98a32009-10-21 13:23:08 -0700975 // Remove the fake title bar if it is there
976 hideFakeTitleBar();
977
The Android Open Source Project0c908882009-03-03 19:32:16 -0800978 // Remove the current tab and sub window
Grace Kloba22ac16e2009-10-07 18:00:23 -0700979 Tab t = mTabControl.getCurrentTab();
Patrick Scottfb5e77f2009-04-08 19:17:37 -0700980 if (t != null) {
981 dismissSubWindow(t);
982 removeTabFromContentView(t);
983 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800984 // Destroy all the tabs
985 mTabControl.destroy();
986 WebIconDatabase.getInstance().close();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800987
Grace Klobab4da0ad2009-05-14 14:45:40 -0700988 unregisterReceiver(mPackageInstallationReceiver);
Bjorn Bringerta7611812010-03-24 11:12:02 +0000989
990 // Stop watching the default geolocation permissions
991 mSystemAllowGeolocationOrigins.stop();
992 mSystemAllowGeolocationOrigins = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800993 }
994
995 @Override
996 public void onConfigurationChanged(Configuration newConfig) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400997 mConfigChanged = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800998 super.onConfigurationChanged(newConfig);
999
1000 if (mPageInfoDialog != null) {
1001 mPageInfoDialog.dismiss();
1002 showPageInfo(
1003 mPageInfoView,
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05001004 mPageInfoFromShowSSLCertificateOnError);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001005 }
1006 if (mSSLCertificateDialog != null) {
1007 mSSLCertificateDialog.dismiss();
1008 showSSLCertificate(
1009 mSSLCertificateView);
1010 }
1011 if (mSSLCertificateOnErrorDialog != null) {
1012 mSSLCertificateOnErrorDialog.dismiss();
1013 showSSLCertificateOnError(
1014 mSSLCertificateOnErrorView,
1015 mSSLCertificateOnErrorHandler,
1016 mSSLCertificateOnErrorError);
1017 }
1018 if (mHttpAuthenticationDialog != null) {
1019 String title = ((TextView) mHttpAuthenticationDialog
1020 .findViewById(com.android.internal.R.id.alertTitle)).getText()
1021 .toString();
1022 String name = ((TextView) mHttpAuthenticationDialog
1023 .findViewById(R.id.username_edit)).getText().toString();
1024 String password = ((TextView) mHttpAuthenticationDialog
1025 .findViewById(R.id.password_edit)).getText().toString();
1026 int focusId = mHttpAuthenticationDialog.getCurrentFocus()
1027 .getId();
1028 mHttpAuthenticationDialog.dismiss();
1029 showHttpAuthentication(mHttpAuthHandler, null, null, title,
1030 name, password, focusId);
1031 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001032 }
1033
Grace Kloba22ac16e2009-10-07 18:00:23 -07001034 @Override
1035 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001036 super.onLowMemory();
1037 mTabControl.freeMemory();
1038 }
1039
Mike Reed7bfa63b2009-05-28 11:08:32 -04001040 private boolean resumeWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001041 Tab tab = mTabControl.getCurrentTab();
1042 boolean inLoad = tab.inLoad();
1043 if ((!mActivityInPause && !inLoad) || (mActivityInPause && inLoad)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001044 CookieSyncManager.getInstance().startSync();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001045 WebView w = tab.getWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001046 if (w != null) {
1047 w.resumeTimers();
1048 }
1049 return true;
1050 } else {
1051 return false;
1052 }
1053 }
1054
Mike Reed7bfa63b2009-05-28 11:08:32 -04001055 private boolean pauseWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001056 Tab tab = mTabControl.getCurrentTab();
1057 boolean inLoad = tab.inLoad();
1058 if (mActivityInPause && !inLoad) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001059 CookieSyncManager.getInstance().stopSync();
1060 WebView w = mTabControl.getCurrentWebView();
1061 if (w != null) {
1062 w.pauseTimers();
1063 }
1064 return true;
1065 } else {
1066 return false;
1067 }
1068 }
1069
The Android Open Source Project0c908882009-03-03 19:32:16 -08001070 // Open the icon database and retain all the icons for visited sites.
1071 private void retainIconsOnStartup() {
1072 final WebIconDatabase db = WebIconDatabase.getInstance();
1073 db.open(getDir("icons", 0).getPath());
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001074 Cursor c = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001075 try {
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001076 c = Browser.getAllBookmarks(mResolver);
1077 if (c.moveToFirst()) {
1078 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
1079 do {
1080 String url = c.getString(urlIndex);
1081 db.retainIconForPageUrl(url);
1082 } while (c.moveToNext());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001083 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001084 } catch (IllegalStateException e) {
1085 Log.e(LOGTAG, "retainIconsOnStartup", e);
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001086 } finally {
1087 if (c!= null) c.close();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001088 }
1089 }
1090
1091 // Helper method for getting the top window.
1092 WebView getTopWindow() {
1093 return mTabControl.getCurrentTopWebView();
1094 }
1095
Grace Kloba22ac16e2009-10-07 18:00:23 -07001096 TabControl getTabControl() {
1097 return mTabControl;
1098 }
1099
The Android Open Source Project0c908882009-03-03 19:32:16 -08001100 @Override
1101 public boolean onCreateOptionsMenu(Menu menu) {
1102 super.onCreateOptionsMenu(menu);
1103
1104 MenuInflater inflater = getMenuInflater();
1105 inflater.inflate(R.menu.browser, menu);
1106 mMenu = menu;
1107 updateInLoadMenuItems();
1108 return true;
1109 }
1110
1111 /**
1112 * As the menu can be open when loading state changes
1113 * we must manually update the state of the stop/reload menu
1114 * item
1115 */
1116 private void updateInLoadMenuItems() {
1117 if (mMenu == null) {
1118 return;
1119 }
1120 MenuItem src = mInLoad ?
1121 mMenu.findItem(R.id.stop_menu_id):
1122 mMenu.findItem(R.id.reload_menu_id);
1123 MenuItem dest = mMenu.findItem(R.id.stop_reload_menu_id);
1124 dest.setIcon(src.getIcon());
1125 dest.setTitle(src.getTitle());
1126 }
1127
1128 @Override
1129 public boolean onContextItemSelected(MenuItem item) {
1130 // chording is not an issue with context menus, but we use the same
1131 // options selector, so set mCanChord to true so we can access them.
1132 mCanChord = true;
1133 int id = item.getItemId();
Leon Scroggins96afcb12009-12-10 12:35:56 -05001134 boolean result = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001135 switch (id) {
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001136 // For the context menu from the title bar
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001137 case R.id.title_bar_copy_page_url:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001138 Tab currentTab = mTabControl.getCurrentTab();
1139 if (null == currentTab) {
1140 result = false;
1141 break;
1142 }
1143 WebView mainView = currentTab.getWebView();
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001144 if (null == mainView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001145 result = false;
1146 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001147 }
Leon Scroggins96afcb12009-12-10 12:35:56 -05001148 copy(mainView.getUrl());
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001149 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001150 // -- Browser context menu
1151 case R.id.open_context_menu_id:
1152 case R.id.open_newtab_context_menu_id:
1153 case R.id.bookmark_context_menu_id:
1154 case R.id.save_link_context_menu_id:
1155 case R.id.share_link_context_menu_id:
1156 case R.id.copy_link_context_menu_id:
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001157 final WebView webView = getTopWindow();
1158 if (null == webView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001159 result = false;
1160 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001161 }
1162 final HashMap hrefMap = new HashMap();
1163 hrefMap.put("webview", webView);
1164 final Message msg = mHandler.obtainMessage(
1165 FOCUS_NODE_HREF, id, 0, hrefMap);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001166 webView.requestFocusNodeHref(msg);
1167 break;
1168
1169 default:
1170 // For other context menus
Leon Scroggins96afcb12009-12-10 12:35:56 -05001171 result = onOptionsItemSelected(item);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001172 }
1173 mCanChord = false;
Leon Scroggins96afcb12009-12-10 12:35:56 -05001174 return result;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001175 }
1176
1177 private Bundle createGoogleSearchSourceBundle(String source) {
1178 Bundle bundle = new Bundle();
Bjorn Bringert10d1cca2010-02-10 14:22:12 +00001179 bundle.putString(Search.SOURCE, source);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001180 return bundle;
1181 }
1182
Leon Scroggins8ad29922010-02-16 12:33:55 -05001183 /* package */ void editUrl() {
Leon Scroggins68579392009-09-15 15:31:54 -04001184 if (mOptionsMenuOpen) closeOptionsMenu();
Leon Scroggins5bbe9802009-07-31 13:10:55 -04001185 String url = (getTopWindow() == null) ? null : getTopWindow().getUrl();
Grace Kloba83f47342009-07-20 10:44:31 -07001186 startSearch(mSettings.getHomePage().equals(url) ? null : url, true,
Leon Scroggins8ad29922010-02-16 12:33:55 -05001187 null, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001188 }
1189
Leon Scroggins8ad29922010-02-16 12:33:55 -05001190 /**
1191 * Overriding this to insert a local information bundle
1192 */
The Android Open Source Project0c908882009-03-03 19:32:16 -08001193 @Override
1194 public void startSearch(String initialQuery, boolean selectInitialQuery,
1195 Bundle appSearchData, boolean globalSearch) {
1196 if (appSearchData == null) {
1197 appSearchData = createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_TYPE);
1198 }
1199 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1200 }
1201
Leon Scroggins1f005d32009-08-10 17:36:42 -04001202 /**
1203 * Switch tabs. Called by the TitleBarSet when sliding the title bar
1204 * results in changing tabs.
Leon Scroggins160a7e72009-08-14 18:28:01 -04001205 * @param index Index of the tab to change to, as defined by
1206 * mTabControl.getTabIndex(Tab t).
1207 * @return boolean True if we successfully switched to a different tab. If
1208 * the indexth tab is null, or if that tab is the same as
1209 * the current one, return false.
Leon Scroggins1f005d32009-08-10 17:36:42 -04001210 */
Leon Scroggins160a7e72009-08-14 18:28:01 -04001211 /* package */ boolean switchToTab(int index) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001212 Tab tab = mTabControl.getTab(index);
1213 Tab currentTab = mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001214 if (tab == null || tab == currentTab) {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001215 return false;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001216 }
1217 if (currentTab != null) {
1218 // currentTab may be null if it was just removed. In that case,
1219 // we do not need to remove it
1220 removeTabFromContentView(currentTab);
1221 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001222 mTabControl.setCurrentTab(tab);
1223 attachTabToContentView(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001224 resetTitleIconAndProgress();
1225 updateLockIconToLatest();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001226 return true;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001227 }
1228
Grace Kloba22ac16e2009-10-07 18:00:23 -07001229 /* package */ Tab openTabToHomePage() {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001230 return openTabAndShow(mSettings.getHomePage(), false, null);
1231 }
1232
Leon Scroggins1f005d32009-08-10 17:36:42 -04001233 /* package */ void closeCurrentWindow() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001234 final Tab current = mTabControl.getCurrentTab();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001235 if (mTabControl.getTabCount() == 1) {
Leon Scroggins30444232009-09-04 18:36:20 -04001236 // This is the last tab. Open a new one, with the home
1237 // page and close the current one.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001238 openTabToHomePage();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001239 closeTab(current);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001240 return;
1241 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001242 final Tab parent = current.getParentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001243 int indexToShow = -1;
1244 if (parent != null) {
1245 indexToShow = mTabControl.getTabIndex(parent);
1246 } else {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001247 final int currentIndex = mTabControl.getCurrentIndex();
1248 // Try to move to the tab to the right
1249 indexToShow = currentIndex + 1;
1250 if (indexToShow > mTabControl.getTabCount() - 1) {
1251 // Try to move to the tab to the left
1252 indexToShow = currentIndex - 1;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001253 }
1254 }
Leon Scroggins160a7e72009-08-14 18:28:01 -04001255 if (switchToTab(indexToShow)) {
1256 // Close window
1257 closeTab(current);
1258 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001259 }
1260
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001261 private ActiveTabsPage mActiveTabsPage;
1262
1263 /**
1264 * Remove the active tabs page.
1265 * @param needToAttach If true, the active tabs page did not attach a tab
1266 * to the content view, so we need to do that here.
1267 */
1268 /* package */ void removeActiveTabPage(boolean needToAttach) {
1269 mContentView.removeView(mActiveTabsPage);
1270 mActiveTabsPage = null;
1271 mMenuState = R.id.MAIN_MENU;
1272 if (needToAttach) {
1273 attachTabToContentView(mTabControl.getCurrentTab());
1274 }
1275 getTopWindow().requestFocus();
1276 }
1277
The Android Open Source Project0c908882009-03-03 19:32:16 -08001278 @Override
1279 public boolean onOptionsItemSelected(MenuItem item) {
1280 if (!mCanChord) {
1281 // The user has already fired a shortcut with this hold down of the
1282 // menu key.
1283 return false;
1284 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001285 if (null == getTopWindow()) {
Leon Scroggins0d7ae0e2009-06-05 11:04:45 -04001286 return false;
1287 }
Grace Kloba6ee9c492009-07-13 10:04:34 -07001288 if (mMenuIsDown) {
1289 // The shortcut action consumes the MENU. Even if it is still down,
1290 // it won't trigger the next shortcut action. In the case of the
1291 // shortcut action triggering a new activity, like Bookmarks, we
1292 // won't get onKeyUp for MENU. So it is important to reset it here.
1293 mMenuIsDown = false;
1294 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001295 switch (item.getItemId()) {
1296 // -- Main menu
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001297 case R.id.new_tab_menu_id:
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001298 openTabToHomePage();
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001299 break;
1300
Leon Scroggins64b80f32009-08-07 12:03:34 -04001301 case R.id.goto_menu_id:
Leon Scroggins8ad29922010-02-16 12:33:55 -05001302 editUrl();
Leon Scrogginsb3a5bed2009-09-28 11:21:56 -04001303 break;
1304
1305 case R.id.bookmarks_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001306 bookmarksOrHistoryPicker(false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001307 break;
1308
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001309 case R.id.active_tabs_menu_id:
1310 mActiveTabsPage = new ActiveTabsPage(this, mTabControl);
1311 removeTabFromContentView(mTabControl.getCurrentTab());
Leon Scroggins43de6162009-09-14 19:59:58 -04001312 hideFakeTitleBar();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001313 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
1314 mActiveTabsPage.requestFocus();
1315 mMenuState = EMPTY_MENU;
1316 break;
1317
Leon Scroggins1f005d32009-08-10 17:36:42 -04001318 case R.id.add_bookmark_menu_id:
1319 Intent i = new Intent(BrowserActivity.this,
1320 AddBookmarkPage.class);
1321 WebView w = getTopWindow();
1322 i.putExtra("url", w.getUrl());
1323 i.putExtra("title", w.getTitle());
Grace Kloba83cdb2c2009-09-16 00:48:57 -07001324 i.putExtra("touch_icon_url", w.getTouchIconUrl());
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01001325 i.putExtra("thumbnail", createScreenshot(w));
Leon Scroggins1f005d32009-08-10 17:36:42 -04001326 startActivity(i);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001327 break;
1328
1329 case R.id.stop_reload_menu_id:
1330 if (mInLoad) {
1331 stopLoading();
1332 } else {
1333 getTopWindow().reload();
1334 }
1335 break;
1336
1337 case R.id.back_menu_id:
1338 getTopWindow().goBack();
1339 break;
1340
1341 case R.id.forward_menu_id:
1342 getTopWindow().goForward();
1343 break;
1344
1345 case R.id.close_menu_id:
1346 // Close the subwindow if it exists.
1347 if (mTabControl.getCurrentSubWindow() != null) {
1348 dismissSubWindow(mTabControl.getCurrentTab());
1349 break;
1350 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001351 closeCurrentWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001352 break;
1353
1354 case R.id.homepage_menu_id:
Grace Kloba22ac16e2009-10-07 18:00:23 -07001355 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001356 if (current != null) {
1357 dismissSubWindow(current);
Leon Scroggins92472e82010-02-17 16:32:28 -05001358 loadUrl(current.getWebView(), mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001359 }
1360 break;
1361
1362 case R.id.preferences_menu_id:
1363 Intent intent = new Intent(this,
1364 BrowserPreferencesPage.class);
Leon Scrogginsd5304942009-12-10 16:11:39 -05001365 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1366 getTopWindow().getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001367 startActivityForResult(intent, PREFERENCES_PAGE);
1368 break;
1369
1370 case R.id.find_menu_id:
1371 if (null == mFindDialog) {
1372 mFindDialog = new FindDialog(this);
1373 }
1374 mFindDialog.setWebView(getTopWindow());
1375 mFindDialog.show();
Cary Clark7d3ac792010-03-03 10:11:44 -05001376 getTopWindow().setFindIsUp(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001377 mMenuState = EMPTY_MENU;
1378 break;
1379
1380 case R.id.select_text_id:
1381 getTopWindow().emulateShiftHeld();
1382 break;
1383 case R.id.page_info_menu_id:
1384 showPageInfo(mTabControl.getCurrentTab(), false);
1385 break;
1386
1387 case R.id.classic_history_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001388 bookmarksOrHistoryPicker(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001389 break;
1390
Leon Scroggins96afcb12009-12-10 12:35:56 -05001391 case R.id.title_bar_share_page_url:
The Android Open Source Project0c908882009-03-03 19:32:16 -08001392 case R.id.share_page_menu_id:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001393 Tab currentTab = mTabControl.getCurrentTab();
1394 if (null == currentTab) {
1395 mCanChord = false;
1396 return false;
1397 }
1398 currentTab.populatePickerData();
1399 sharePage(this, currentTab.getTitle(),
1400 currentTab.getUrl(), currentTab.getFavicon(),
1401 createScreenshot(currentTab.getWebView()));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001402 break;
1403
1404 case R.id.dump_nav_menu_id:
1405 getTopWindow().debugDump();
1406 break;
1407
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001408 case R.id.dump_counters_menu_id:
1409 getTopWindow().dumpV8Counters();
1410 break;
1411
The Android Open Source Project0c908882009-03-03 19:32:16 -08001412 case R.id.zoom_in_menu_id:
1413 getTopWindow().zoomIn();
1414 break;
1415
1416 case R.id.zoom_out_menu_id:
1417 getTopWindow().zoomOut();
1418 break;
1419
1420 case R.id.view_downloads_menu_id:
1421 viewDownloads(null);
1422 break;
1423
The Android Open Source Project0c908882009-03-03 19:32:16 -08001424 case R.id.window_one_menu_id:
1425 case R.id.window_two_menu_id:
1426 case R.id.window_three_menu_id:
1427 case R.id.window_four_menu_id:
1428 case R.id.window_five_menu_id:
1429 case R.id.window_six_menu_id:
1430 case R.id.window_seven_menu_id:
1431 case R.id.window_eight_menu_id:
1432 {
1433 int menuid = item.getItemId();
1434 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1435 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001436 Tab desiredTab = mTabControl.getTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001437 if (desiredTab != null &&
1438 desiredTab != mTabControl.getCurrentTab()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001439 switchToTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001440 }
1441 break;
1442 }
1443 }
1444 }
1445 break;
1446
1447 default:
1448 if (!super.onOptionsItemSelected(item)) {
1449 return false;
1450 }
1451 // Otherwise fall through.
1452 }
1453 mCanChord = false;
1454 return true;
1455 }
1456
1457 public void closeFind() {
1458 mMenuState = R.id.MAIN_MENU;
1459 }
1460
Grace Kloba22ac16e2009-10-07 18:00:23 -07001461 @Override
1462 public boolean onPrepareOptionsMenu(Menu menu) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001463 // This happens when the user begins to hold down the menu key, so
1464 // allow them to chord to get a shortcut.
1465 mCanChord = true;
1466 // Note: setVisible will decide whether an item is visible; while
1467 // setEnabled() will decide whether an item is enabled, which also means
1468 // whether the matching shortcut key will function.
1469 super.onPrepareOptionsMenu(menu);
1470 switch (mMenuState) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001471 case EMPTY_MENU:
1472 if (mCurrentMenuState != mMenuState) {
1473 menu.setGroupVisible(R.id.MAIN_MENU, false);
1474 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1475 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001476 }
1477 break;
1478 default:
1479 if (mCurrentMenuState != mMenuState) {
1480 menu.setGroupVisible(R.id.MAIN_MENU, true);
1481 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1482 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001483 }
1484 final WebView w = getTopWindow();
1485 boolean canGoBack = false;
1486 boolean canGoForward = false;
1487 boolean isHome = false;
1488 if (w != null) {
1489 canGoBack = w.canGoBack();
1490 canGoForward = w.canGoForward();
1491 isHome = mSettings.getHomePage().equals(w.getUrl());
1492 }
1493 final MenuItem back = menu.findItem(R.id.back_menu_id);
1494 back.setEnabled(canGoBack);
1495
1496 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1497 home.setEnabled(!isHome);
1498
1499 menu.findItem(R.id.forward_menu_id)
1500 .setEnabled(canGoForward);
1501
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001502 menu.findItem(R.id.new_tab_menu_id).setEnabled(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001503 mTabControl.canCreateNewTab());
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001504
The Android Open Source Project0c908882009-03-03 19:32:16 -08001505 // decide whether to show the share link option
1506 PackageManager pm = getPackageManager();
1507 Intent send = new Intent(Intent.ACTION_SEND);
1508 send.setType("text/plain");
1509 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1510 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1511
The Android Open Source Project0c908882009-03-03 19:32:16 -08001512 boolean isNavDump = mSettings.isNavDump();
1513 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1514 nav.setVisible(isNavDump);
1515 nav.setEnabled(isNavDump);
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001516
1517 boolean showDebugSettings = mSettings.showDebugSettings();
1518 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1519 counter.setVisible(showDebugSettings);
1520 counter.setEnabled(showDebugSettings);
1521
The Android Open Source Project0c908882009-03-03 19:32:16 -08001522 break;
1523 }
1524 mCurrentMenuState = mMenuState;
1525 return true;
1526 }
1527
1528 @Override
1529 public void onCreateContextMenu(ContextMenu menu, View v,
1530 ContextMenuInfo menuInfo) {
Leon Scroggins4e9f89b2010-02-22 16:54:14 -05001531 if (v instanceof TitleBar) {
1532 return;
1533 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001534 WebView webview = (WebView) v;
1535 WebView.HitTestResult result = webview.getHitTestResult();
1536 if (result == null) {
1537 return;
1538 }
1539
1540 int type = result.getType();
1541 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1542 Log.w(LOGTAG,
1543 "We should not show context menu when nothing is touched");
1544 return;
1545 }
1546 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1547 // let TextView handles context menu
1548 return;
1549 }
1550
1551 // Note, http://b/issue?id=1106666 is requesting that
1552 // an inflated menu can be used again. This is not available
1553 // yet, so inflate each time (yuk!)
1554 MenuInflater inflater = getMenuInflater();
1555 inflater.inflate(R.menu.browsercontext, menu);
1556
1557 // Show the correct menu group
1558 String extra = result.getExtra();
1559 menu.setGroupVisible(R.id.PHONE_MENU,
1560 type == WebView.HitTestResult.PHONE_TYPE);
1561 menu.setGroupVisible(R.id.EMAIL_MENU,
1562 type == WebView.HitTestResult.EMAIL_TYPE);
1563 menu.setGroupVisible(R.id.GEO_MENU,
1564 type == WebView.HitTestResult.GEO_TYPE);
1565 menu.setGroupVisible(R.id.IMAGE_MENU,
1566 type == WebView.HitTestResult.IMAGE_TYPE
1567 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1568 menu.setGroupVisible(R.id.ANCHOR_MENU,
1569 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1570 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1571
1572 // Setup custom handling depending on the type
1573 switch (type) {
1574 case WebView.HitTestResult.PHONE_TYPE:
1575 menu.setHeaderTitle(Uri.decode(extra));
1576 menu.findItem(R.id.dial_context_menu_id).setIntent(
1577 new Intent(Intent.ACTION_VIEW, Uri
1578 .parse(WebView.SCHEME_TEL + extra)));
1579 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1580 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
Cary Clark5e335a32009-09-22 14:53:11 -04001581 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001582 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1583 addIntent);
1584 menu.findItem(R.id.copy_phone_context_menu_id).setOnMenuItemClickListener(
1585 new Copy(extra));
1586 break;
1587
1588 case WebView.HitTestResult.EMAIL_TYPE:
1589 menu.setHeaderTitle(extra);
1590 menu.findItem(R.id.email_context_menu_id).setIntent(
1591 new Intent(Intent.ACTION_VIEW, Uri
1592 .parse(WebView.SCHEME_MAILTO + extra)));
1593 menu.findItem(R.id.copy_mail_context_menu_id).setOnMenuItemClickListener(
1594 new Copy(extra));
1595 break;
1596
1597 case WebView.HitTestResult.GEO_TYPE:
1598 menu.setHeaderTitle(extra);
1599 menu.findItem(R.id.map_context_menu_id).setIntent(
1600 new Intent(Intent.ACTION_VIEW, Uri
1601 .parse(WebView.SCHEME_GEO
1602 + URLEncoder.encode(extra))));
1603 menu.findItem(R.id.copy_geo_context_menu_id).setOnMenuItemClickListener(
1604 new Copy(extra));
1605 break;
1606
1607 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1608 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
1609 TextView titleView = (TextView) LayoutInflater.from(this)
1610 .inflate(android.R.layout.browser_link_context_header,
1611 null);
1612 titleView.setText(extra);
1613 menu.setHeaderView(titleView);
1614 // decide whether to show the open link in new tab option
1615 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001616 mTabControl.canCreateNewTab());
Ben Murdochde353622009-10-12 10:29:00 +01001617 menu.findItem(R.id.bookmark_context_menu_id).setVisible(
1618 Bookmarks.urlHasAcceptableScheme(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001619 PackageManager pm = getPackageManager();
1620 Intent send = new Intent(Intent.ACTION_SEND);
1621 send.setType("text/plain");
1622 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1623 menu.findItem(R.id.share_link_context_menu_id).setVisible(ri != null);
1624 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1625 break;
1626 }
1627 // otherwise fall through to handle image part
1628 case WebView.HitTestResult.IMAGE_TYPE:
1629 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1630 menu.setHeaderTitle(extra);
1631 }
1632 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1633 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1634 menu.findItem(R.id.download_context_menu_id).
1635 setOnMenuItemClickListener(new Download(extra));
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001636 menu.findItem(R.id.set_wallpaper_context_menu_id).
1637 setOnMenuItemClickListener(new SetAsWallpaper(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001638 break;
1639
1640 default:
1641 Log.w(LOGTAG, "We should not get here.");
1642 break;
1643 }
Leon Scrogginsb2b19f52009-10-09 16:10:00 -04001644 hideFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001645 }
1646
The Android Open Source Project0c908882009-03-03 19:32:16 -08001647 // Attach the given tab to the content view.
Grace Klobac928c302009-09-17 11:51:21 -07001648 // this should only be called for the current tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001649 private void attachTabToContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001650 // Attach the container that contains the main WebView and any other UI
1651 // associated with the tab.
Patrick Scottd0119532009-09-17 08:00:31 -04001652 t.attachTabToContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001653
1654 if (mShouldShowErrorConsole) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001655 ErrorConsoleView errorConsole = t.getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01001656 if (errorConsole.numberOfErrors() == 0) {
1657 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
1658 } else {
1659 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1660 }
1661
1662 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08001663 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01001664 ViewGroup.LayoutParams.WRAP_CONTENT));
1665 }
1666
Leon Scroggins39ab28e2009-09-02 21:20:30 -04001667 WebView view = t.getWebView();
Leon Scroggins55a5bc22009-09-04 17:00:08 -04001668 view.setEmbeddedTitleBar(mTitleBar);
Leon Scroggins58d56c62010-01-28 15:12:40 -05001669 if (t.isInVoiceSearchMode()) {
1670 showVoiceTitleBar(t.getVoiceDisplayTitle());
1671 } else {
1672 revertVoiceTitleBar();
1673 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001674 // Request focus on the top window.
1675 t.getTopWindow().requestFocus();
1676 }
1677
1678 // Attach a sub window to the main WebView of the given tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001679 void attachSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001680 t.attachSubWindow(mContentView);
1681 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001682 }
1683
1684 // Remove the given tab from the content view.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001685 private void removeTabFromContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001686 // Remove the container that contains the main WebView.
Patrick Scottd0119532009-09-17 08:00:31 -04001687 t.removeTabFromContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001688
Grace Kloba22ac16e2009-10-07 18:00:23 -07001689 ErrorConsoleView errorConsole = t.getErrorConsole(false);
1690 if (errorConsole != null) {
1691 mErrorConsoleContainer.removeView(errorConsole);
Ben Murdochbff2d602009-07-01 20:19:05 +01001692 }
1693
Leon Scroggins39ab28e2009-09-02 21:20:30 -04001694 WebView view = t.getWebView();
Leon Scrogginsbb85b902009-09-14 19:27:20 -04001695 if (view != null) {
1696 view.setEmbeddedTitleBar(null);
1697 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001698 }
1699
1700 // Remove the sub window if it exists. Also called by TabControl when the
1701 // user clicks the 'X' to dismiss a sub window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001702 /* package */ void dismissSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001703 t.removeSubWindow(mContentView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 // dismiss the subwindow. This will destroy the WebView.
1705 t.dismissSubWindow();
Patrick Scottd0119532009-09-17 08:00:31 -04001706 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001707 }
1708
Leon Scroggins1f005d32009-08-10 17:36:42 -04001709 // A wrapper function of {@link #openTabAndShow(UrlData, boolean, String)}
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07001710 // that accepts url as string.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001711 private Tab openTabAndShow(String url, boolean closeOnExit, String appId) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001712 return openTabAndShow(new UrlData(url), closeOnExit, appId);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001713 }
1714
1715 // This method does a ton of stuff. It will attempt to create a new tab
1716 // if we haven't reached MAX_TABS. Otherwise it uses the current tab. If
Leon Scroggins1f005d32009-08-10 17:36:42 -04001717 // url isn't null, it will load the given url.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001718 /* package */Tab openTabAndShow(UrlData urlData, boolean closeOnExit,
1719 String appId) {
1720 final Tab currentTab = mTabControl.getCurrentTab();
1721 if (mTabControl.canCreateNewTab()) {
1722 final Tab tab = mTabControl.createNewTab(closeOnExit, appId,
1723 urlData.mUrl);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001724 WebView webview = tab.getWebView();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001725 // If the last tab was removed from the active tabs page, currentTab
1726 // will be null.
1727 if (currentTab != null) {
1728 removeTabFromContentView(currentTab);
1729 }
Patrick Scott8bbd69f2009-08-14 13:35:53 -04001730 // We must set the new tab as the current tab to reflect the old
1731 // animation behavior.
1732 mTabControl.setCurrentTab(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001733 attachTabToContentView(tab);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001734 if (!urlData.isEmpty()) {
Patrick Scott9d53da02010-02-19 10:19:01 -05001735 loadUrlDataIn(tab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001736 }
1737 return tab;
1738 } else {
1739 // Get rid of the subwindow if it exists
1740 dismissSubWindow(currentTab);
1741 if (!urlData.isEmpty()) {
1742 // Load the given url.
Patrick Scott9d53da02010-02-19 10:19:01 -05001743 loadUrlDataIn(currentTab, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001744 }
Leon Scroggins58d56c62010-01-28 15:12:40 -05001745 return currentTab;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001746 }
1747 }
1748
Grace Kloba22ac16e2009-10-07 18:00:23 -07001749 private Tab openTab(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001750 if (mSettings.openInBackground()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 Tab t = mTabControl.createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001752 if (t != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001753 WebView view = t.getWebView();
Leon Scroggins92472e82010-02-17 16:32:28 -05001754 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001755 }
Grace Klobac9181842009-04-14 08:53:22 -07001756 return t;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001757 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001758 return openTabAndShow(url, false, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001759 }
1760 }
1761
1762 private class Copy implements OnMenuItemClickListener {
1763 private CharSequence mText;
1764
1765 public boolean onMenuItemClick(MenuItem item) {
1766 copy(mText);
1767 return true;
1768 }
1769
1770 public Copy(CharSequence toCopy) {
1771 mText = toCopy;
1772 }
1773 }
1774
1775 private class Download implements OnMenuItemClickListener {
1776 private String mText;
1777
1778 public boolean onMenuItemClick(MenuItem item) {
1779 onDownloadStartNoStream(mText, null, null, null, -1);
1780 return true;
1781 }
1782
1783 public Download(String toDownload) {
1784 mText = toDownload;
1785 }
1786 }
1787
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001788 private class SetAsWallpaper extends Thread implements
1789 OnMenuItemClickListener, DialogInterface.OnCancelListener {
1790 private URL mUrl;
1791 private ProgressDialog mWallpaperProgress;
1792 private boolean mCanceled = false;
1793
1794 public SetAsWallpaper(String url) {
1795 try {
1796 mUrl = new URL(url);
1797 } catch (MalformedURLException e) {
1798 mUrl = null;
1799 }
1800 }
1801
1802 public void onCancel(DialogInterface dialog) {
1803 mCanceled = true;
1804 }
1805
1806 public boolean onMenuItemClick(MenuItem item) {
1807 if (mUrl != null) {
1808 // The user may have tried to set a image with a large file size as their
1809 // background so it may take a few moments to perform the operation. Display
1810 // a progress spinner while it is working.
1811 mWallpaperProgress = new ProgressDialog(BrowserActivity.this);
1812 mWallpaperProgress.setIndeterminate(true);
1813 mWallpaperProgress.setMessage(getText(R.string.progress_dialog_setting_wallpaper));
1814 mWallpaperProgress.setCancelable(true);
1815 mWallpaperProgress.setOnCancelListener(this);
1816 mWallpaperProgress.show();
1817 start();
1818 }
1819 return true;
1820 }
1821
1822 public void run() {
1823 Drawable oldWallpaper = BrowserActivity.this.getWallpaper();
1824 try {
1825 // TODO: This will cause the resource to be downloaded again, when we
1826 // should in most cases be able to grab it from the cache. To fix this
1827 // we should query WebCore to see if we can access a cached version and
1828 // instead open an input stream on that. This pattern could also be used
1829 // in the download manager where the same problem exists.
1830 InputStream inputstream = mUrl.openStream();
1831 if (inputstream != null) {
1832 setWallpaper(inputstream);
1833 }
1834 } catch (IOException e) {
1835 Log.e(LOGTAG, "Unable to set new wallpaper");
1836 // Act as though the user canceled the operation so we try to
1837 // restore the old wallpaper.
1838 mCanceled = true;
1839 }
1840
1841 if (mCanceled) {
1842 // Restore the old wallpaper if the user cancelled whilst we were setting
1843 // the new wallpaper.
1844 int width = oldWallpaper.getIntrinsicWidth();
1845 int height = oldWallpaper.getIntrinsicHeight();
1846 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1847 Canvas canvas = new Canvas(bm);
1848 oldWallpaper.setBounds(0, 0, width, height);
1849 oldWallpaper.draw(canvas);
1850 try {
1851 setWallpaper(bm);
1852 } catch (IOException e) {
1853 Log.e(LOGTAG, "Unable to restore old wallpaper.");
1854 }
1855 mCanceled = false;
1856 }
1857
1858 if (mWallpaperProgress.isShowing()) {
1859 mWallpaperProgress.dismiss();
1860 }
1861 }
1862 }
1863
The Android Open Source Project0c908882009-03-03 19:32:16 -08001864 private void copy(CharSequence text) {
1865 try {
1866 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
1867 if (clip != null) {
1868 clip.setClipboardText(text);
1869 }
1870 } catch (android.os.RemoteException e) {
1871 Log.e(LOGTAG, "Copy failed", e);
1872 }
1873 }
1874
1875 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001876 * Resets the browser title-view to whatever it must be
1877 * (for example, if we had a loading error)
1878 * When we have a new page, we call resetTitle, when we
1879 * have to reset the titlebar to whatever it used to be
1880 * (for example, if the user chose to stop loading), we
1881 * call resetTitleAndRevertLockIcon.
1882 */
1883 /* package */ void resetTitleAndRevertLockIcon() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001884 mTabControl.getCurrentTab().revertLockIcon();
1885 updateLockIconToLatest();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001886 resetTitleIconAndProgress();
1887 }
1888
1889 /**
1890 * Reset the title, favicon, and progress.
1891 */
1892 private void resetTitleIconAndProgress() {
1893 WebView current = mTabControl.getCurrentWebView();
1894 if (current == null) {
1895 return;
1896 }
1897 resetTitleAndIcon(current);
1898 int progress = current.getProgress();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001899 current.getWebChromeClient().onProgressChanged(current, progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001900 }
1901
1902 // Reset the title and the icon based on the given item.
1903 private void resetTitleAndIcon(WebView view) {
1904 WebHistoryItem item = view.copyBackForwardList().getCurrentItem();
1905 if (item != null) {
Leon Scroggins68579392009-09-15 15:31:54 -04001906 setUrlTitle(item.getUrl(), item.getTitle());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001907 setFavicon(item.getFavicon());
1908 } else {
Leon Scroggins68579392009-09-15 15:31:54 -04001909 setUrlTitle(null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001910 setFavicon(null);
1911 }
1912 }
1913
1914 /**
1915 * Sets a title composed of the URL and the title string.
1916 * @param url The URL of the site being loaded.
1917 * @param title The title of the site being loaded.
1918 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07001919 void setUrlTitle(String url, String title) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001920 mUrl = url;
1921 mTitle = title;
1922
Leon Scroggins58d56c62010-01-28 15:12:40 -05001923 // If we are in voice search mode, the title has already been set.
1924 if (mTabControl.getCurrentTab().isInVoiceSearchMode()) return;
1925 mTitleBar.setDisplayTitle(url);
1926 mFakeTitleBar.setDisplayTitle(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001927 }
1928
1929 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001930 * @param url The URL to build a title version of the URL from.
1931 * @return The title version of the URL or null if fails.
1932 * The title version of the URL can be either the URL hostname,
1933 * or the hostname with an "https://" prefix (for secure URLs),
1934 * or an empty string if, for example, the URL in question is a
1935 * file:// URL with no hostname.
1936 */
Leon Scroggins32e14a62009-06-11 10:26:34 -04001937 /* package */ static String buildTitleUrl(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001938 String titleUrl = null;
1939
1940 if (url != null) {
1941 try {
1942 // parse the url string
1943 URL urlObj = new URL(url);
1944 if (urlObj != null) {
1945 titleUrl = "";
1946
1947 String protocol = urlObj.getProtocol();
1948 String host = urlObj.getHost();
1949
1950 if (host != null && 0 < host.length()) {
1951 titleUrl = host;
1952 if (protocol != null) {
1953 // if a secure site, add an "https://" prefix!
1954 if (protocol.equalsIgnoreCase("https")) {
1955 titleUrl = protocol + "://" + host;
1956 }
1957 }
1958 }
1959 }
1960 } catch (MalformedURLException e) {}
1961 }
1962
1963 return titleUrl;
1964 }
1965
1966 // Set the favicon in the title bar.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001967 void setFavicon(Bitmap icon) {
Leon Scroggins68579392009-09-15 15:31:54 -04001968 mTitleBar.setFavicon(icon);
Leon Scrogginsfe87bd32009-10-06 10:10:00 -04001969 mFakeTitleBar.setFavicon(icon);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001970 }
1971
1972 /**
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001973 * Close the tab, remove its associated title bar, and adjust mTabControl's
1974 * current tab to a valid value.
Leon Scroggins1f005d32009-08-10 17:36:42 -04001975 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07001976 /* package */ void closeTab(Tab t) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001977 int currentIndex = mTabControl.getCurrentIndex();
1978 int removeIndex = mTabControl.getTabIndex(t);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001979 mTabControl.removeTab(t);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001980 if (currentIndex >= removeIndex && currentIndex != 0) {
1981 currentIndex--;
1982 }
1983 mTabControl.setCurrentTab(mTabControl.getTab(currentIndex));
Andrei Popescua5bf1de2009-09-23 16:39:23 +01001984 resetTitleIconAndProgress();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001985 }
1986
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001987 /* package */ void goBackOnePageOrQuit() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001988 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001989 if (current == null) {
1990 /*
1991 * Instead of finishing the activity, simply push this to the back
1992 * of the stack and let ActivityManager to choose the foreground
1993 * activity. As BrowserActivity is singleTask, it will be always the
1994 * root of the task. So we can use either true or false for
1995 * moveTaskToBack().
1996 */
1997 moveTaskToBack(true);
Grace Kloba00d85e72009-09-23 18:50:05 -07001998 return;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001999 }
2000 WebView w = current.getWebView();
2001 if (w.canGoBack()) {
2002 w.goBack();
2003 } else {
2004 // Check to see if we are closing a window that was created by
2005 // another window. If so, we switch back to that window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002006 Tab parent = current.getParentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002007 if (parent != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04002008 switchToTab(mTabControl.getTabIndex(parent));
2009 // Now we close the other tab
2010 closeTab(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002011 } else {
2012 if (current.closeOnExit()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002013 // force the tab's inLoad() to be false as we are going to
2014 // either finish the activity or remove the tab. This will
2015 // ensure pauseWebViewTimers() taking action.
2016 mTabControl.getCurrentTab().clearInLoad();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002017 if (mTabControl.getTabCount() == 1) {
2018 finish();
2019 return;
2020 }
Mike Reed7bfa63b2009-05-28 11:08:32 -04002021 // call pauseWebViewTimers() now, we won't be able to call
2022 // it in onPause() as the WebView won't be valid.
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002023 // Temporarily change mActivityInPause to be true as
2024 // pauseWebViewTimers() will do nothing if mActivityInPause
2025 // is false.
Grace Kloba918e1d72009-08-13 14:55:06 -07002026 boolean savedState = mActivityInPause;
2027 if (savedState) {
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002028 Log.e(LOGTAG, "BrowserActivity is already paused "
2029 + "while handing goBackOnePageOrQuit.");
Grace Kloba918e1d72009-08-13 14:55:06 -07002030 }
2031 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -04002032 pauseWebViewTimers();
Grace Kloba918e1d72009-08-13 14:55:06 -07002033 mActivityInPause = savedState;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002034 removeTabFromContentView(current);
2035 mTabControl.removeTab(current);
2036 }
2037 /*
2038 * Instead of finishing the activity, simply push this to the back
2039 * of the stack and let ActivityManager to choose the foreground
2040 * activity. As BrowserActivity is singleTask, it will be always the
2041 * root of the task. So we can use either true or false for
2042 * moveTaskToBack().
2043 */
2044 moveTaskToBack(true);
2045 }
2046 }
2047 }
2048
Grace Kloba22ac16e2009-10-07 18:00:23 -07002049 boolean isMenuDown() {
2050 return mMenuIsDown;
2051 }
2052
Grace Kloba5942df02009-09-18 11:48:29 -07002053 @Override
2054 public boolean onKeyDown(int keyCode, KeyEvent event) {
Leon Scrogginsf65b50d2009-12-08 10:44:28 -05002055 // Even if MENU is already held down, we need to call to super to open
2056 // the IME on long press.
2057 if (KeyEvent.KEYCODE_MENU == keyCode) {
2058 mMenuIsDown = true;
2059 return super.onKeyDown(keyCode, event);
2060 }
Grace Kloba5942df02009-09-18 11:48:29 -07002061 // The default key mode is DEFAULT_KEYS_SEARCH_LOCAL. As the MENU is
2062 // still down, we don't want to trigger the search. Pretend to consume
2063 // the key and do nothing.
2064 if (mMenuIsDown) return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002065
Grace Kloba5942df02009-09-18 11:48:29 -07002066 switch(keyCode) {
Grace Kloba5942df02009-09-18 11:48:29 -07002067 case KeyEvent.KEYCODE_SPACE:
Grace Klobada0fe552009-09-22 18:17:24 -07002068 // WebView/WebTextView handle the keys in the KeyDown. As
2069 // the Activity's shortcut keys are only handled when WebView
2070 // doesn't, have to do it in onKeyDown instead of onKeyUp.
2071 if (event.isShiftPressed()) {
2072 getTopWindow().pageUp(false);
2073 } else {
2074 getTopWindow().pageDown(false);
2075 }
Grace Kloba5942df02009-09-18 11:48:29 -07002076 return true;
2077 case KeyEvent.KEYCODE_BACK:
2078 if (event.getRepeatCount() == 0) {
2079 event.startTracking();
2080 return true;
2081 } else if (mCustomView == null && mActiveTabsPage == null
2082 && event.isLongPress()) {
2083 bookmarksOrHistoryPicker(true);
2084 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002085 }
Grace Kloba5942df02009-09-18 11:48:29 -07002086 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002087 }
Grace Kloba5942df02009-09-18 11:48:29 -07002088 return super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002089 }
2090
Grace Kloba5942df02009-09-18 11:48:29 -07002091 @Override
2092 public boolean onKeyUp(int keyCode, KeyEvent event) {
2093 switch(keyCode) {
2094 case KeyEvent.KEYCODE_MENU:
2095 mMenuIsDown = false;
2096 break;
Grace Kloba5942df02009-09-18 11:48:29 -07002097 case KeyEvent.KEYCODE_BACK:
2098 if (event.isTracking() && !event.isCanceled()) {
2099 if (mCustomView != null) {
2100 // if a custom view is showing, hide it
Grace Kloba22ac16e2009-10-07 18:00:23 -07002101 mTabControl.getCurrentWebView().getWebChromeClient()
2102 .onHideCustomView();
Grace Kloba5942df02009-09-18 11:48:29 -07002103 } else if (mActiveTabsPage != null) {
2104 // if tab page is showing, hide it
2105 removeActiveTabPage(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002106 } else {
Grace Kloba5942df02009-09-18 11:48:29 -07002107 WebView subwindow = mTabControl.getCurrentSubWindow();
2108 if (subwindow != null) {
2109 if (subwindow.canGoBack()) {
2110 subwindow.goBack();
2111 } else {
2112 dismissSubWindow(mTabControl.getCurrentTab());
2113 }
2114 } else {
2115 goBackOnePageOrQuit();
2116 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002117 }
Grace Kloba5942df02009-09-18 11:48:29 -07002118 return true;
2119 }
2120 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002121 }
Grace Kloba5942df02009-09-18 11:48:29 -07002122 return super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002123 }
2124
Leon Scroggins68579392009-09-15 15:31:54 -04002125 /* package */ void stopLoading() {
Ben Murdochb7cc8b42009-09-28 10:59:09 +01002126 mDidStopLoad = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002127 resetTitleAndRevertLockIcon();
2128 WebView w = getTopWindow();
2129 w.stopLoading();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002130 // FIXME: before refactor, it is using mWebViewClient. So I keep the
2131 // same logic here. But for subwindow case, should we call into the main
2132 // WebView's onPageFinished as we never call its onPageStarted and if
2133 // the page finishes itself, we don't call onPageFinished.
2134 mTabControl.getCurrentWebView().getWebViewClient().onPageFinished(w,
2135 w.getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002136
2137 cancelStopToast();
2138 mStopToast = Toast
2139 .makeText(this, R.string.stopping, Toast.LENGTH_SHORT);
2140 mStopToast.show();
2141 }
2142
Grace Kloba22ac16e2009-10-07 18:00:23 -07002143 boolean didUserStopLoading() {
2144 return mDidStopLoad;
2145 }
2146
The Android Open Source Project0c908882009-03-03 19:32:16 -08002147 private void cancelStopToast() {
2148 if (mStopToast != null) {
2149 mStopToast.cancel();
2150 mStopToast = null;
2151 }
2152 }
2153
Grace Kloba22ac16e2009-10-07 18:00:23 -07002154 // called by a UI or non-UI thread to post the message
2155 public void postMessage(int what, int arg1, int arg2, Object obj,
2156 long delayMillis) {
2157 mHandler.sendMessageDelayed(mHandler.obtainMessage(what, arg1, arg2,
2158 obj), delayMillis);
2159 }
2160
2161 // called by a UI or non-UI thread to remove the message
2162 void removeMessages(int what, Object object) {
2163 mHandler.removeMessages(what, object);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002164 }
2165
2166 // public message ids
2167 public final static int LOAD_URL = 1001;
2168 public final static int STOP_LOAD = 1002;
2169
2170 // Message Ids
2171 private static final int FOCUS_NODE_HREF = 102;
Grace Kloba92c18a52009-07-31 23:48:32 -07002172 private static final int RELEASE_WAKELOCK = 107;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002173
Grace Kloba22ac16e2009-10-07 18:00:23 -07002174 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
Ben Murdoch2694e232009-09-29 09:41:11 +01002175
The Android Open Source Project0c908882009-03-03 19:32:16 -08002176 // Private handler for handling javascript and saving passwords
2177 private Handler mHandler = new Handler() {
2178
2179 public void handleMessage(Message msg) {
2180 switch (msg.what) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002181 case FOCUS_NODE_HREF:
Ben Murdoch2694e232009-09-29 09:41:11 +01002182 {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002183 String url = (String) msg.getData().get("url");
Ben Murdoch90d088c2009-11-17 18:14:04 +00002184 String title = (String) msg.getData().get("title");
The Android Open Source Project0c908882009-03-03 19:32:16 -08002185 if (url == null || url.length() == 0) {
2186 break;
2187 }
2188 HashMap focusNodeMap = (HashMap) msg.obj;
2189 WebView view = (WebView) focusNodeMap.get("webview");
2190 // Only apply the action if the top window did not change.
2191 if (getTopWindow() != view) {
2192 break;
2193 }
2194 switch (msg.arg1) {
2195 case R.id.open_context_menu_id:
2196 case R.id.view_image_context_menu_id:
Leon Scroggins92472e82010-02-17 16:32:28 -05002197 loadUrlFromContext(getTopWindow(), url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002198 break;
2199 case R.id.open_newtab_context_menu_id:
Grace Kloba22ac16e2009-10-07 18:00:23 -07002200 final Tab parent = mTabControl.getCurrentTab();
2201 final Tab newTab = openTab(url);
Grace Klobac9181842009-04-14 08:53:22 -07002202 if (newTab != parent) {
2203 parent.addChildTab(newTab);
2204 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002205 break;
2206 case R.id.bookmark_context_menu_id:
2207 Intent intent = new Intent(BrowserActivity.this,
2208 AddBookmarkPage.class);
2209 intent.putExtra("url", url);
Ben Murdoch90d088c2009-11-17 18:14:04 +00002210 intent.putExtra("title", title);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002211 startActivity(intent);
2212 break;
2213 case R.id.share_link_context_menu_id:
Leon Scroggins96afcb12009-12-10 12:35:56 -05002214 // See if this site has been visited before
2215 StringBuilder sb = new StringBuilder(
2216 Browser.BookmarkColumns.URL + " = ");
2217 DatabaseUtils.appendEscapedSQLString(sb, url);
2218 Cursor c = mResolver.query(Browser.BOOKMARKS_URI,
2219 Browser.HISTORY_PROJECTION,
2220 sb.toString(),
2221 null,
2222 null);
2223 if (c.moveToFirst()) {
2224 // The site has been visited before, so grab the
2225 // info from the database.
2226 Bitmap favicon = null;
2227 Bitmap thumbnail = null;
2228 String linkTitle = c.getString(Browser.
2229 HISTORY_PROJECTION_TITLE_INDEX);
2230 byte[] data = c.getBlob(Browser.
2231 HISTORY_PROJECTION_FAVICON_INDEX);
2232 if (data != null) {
2233 favicon = BitmapFactory.decodeByteArray(
2234 data, 0, data.length);
2235 }
2236 data = c.getBlob(Browser.
2237 HISTORY_PROJECTION_THUMBNAIL_INDEX);
2238 if (data != null) {
2239 thumbnail = BitmapFactory.decodeByteArray(
2240 data, 0, data.length);
2241 }
2242 sharePage(BrowserActivity.this,
2243 linkTitle, url, favicon, thumbnail);
2244 } else {
2245 Browser.sendString(BrowserActivity.this, url,
2246 getString(
2247 R.string.choosertitle_sharevia));
2248 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002249 break;
2250 case R.id.copy_link_context_menu_id:
2251 copy(url);
2252 break;
2253 case R.id.save_link_context_menu_id:
2254 case R.id.download_context_menu_id:
2255 onDownloadStartNoStream(url, null, null, null, -1);
2256 break;
2257 }
2258 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002259 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002260
2261 case LOAD_URL:
Leon Scroggins92472e82010-02-17 16:32:28 -05002262 loadUrlFromContext(getTopWindow(), (String) msg.obj);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002263 break;
2264
2265 case STOP_LOAD:
2266 stopLoading();
2267 break;
2268
The Android Open Source Project0c908882009-03-03 19:32:16 -08002269 case RELEASE_WAKELOCK:
2270 if (mWakeLock.isHeld()) {
2271 mWakeLock.release();
Grace Kloba5d0e02e2009-10-05 15:15:36 -07002272 // if we reach here, Browser should be still in the
2273 // background loading after WAKELOCK_TIMEOUT (5-min).
2274 // To avoid burning the battery, stop loading.
2275 mTabControl.stopAllLoading();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002276 }
2277 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002278
2279 case UPDATE_BOOKMARK_THUMBNAIL:
2280 WebView view = (WebView) msg.obj;
2281 if (view != null) {
2282 updateScreenshot(view);
2283 }
2284 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002285 }
2286 }
2287 };
2288
Leon Scroggins96afcb12009-12-10 12:35:56 -05002289 /**
2290 * Share a page, providing the title, url, favicon, and a screenshot. Uses
2291 * an {@link Intent} to launch the Activity chooser.
2292 * @param c Context used to launch a new Activity.
2293 * @param title Title of the page. Stored in the Intent with
Paul Westbrook03e6d392010-02-12 10:33:29 -08002294 * {@link Intent#EXTRA_SUBJECT}
Leon Scroggins96afcb12009-12-10 12:35:56 -05002295 * @param url URL of the page. Stored in the Intent with
2296 * {@link Intent#EXTRA_TEXT}
2297 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
2298 * with {@link Browser#EXTRA_SHARE_FAVICON}
2299 * @param screenshot Bitmap of a screenshot of the page. Stored in the
2300 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
2301 */
2302 public static final void sharePage(Context c, String title, String url,
2303 Bitmap favicon, Bitmap screenshot) {
2304 Intent send = new Intent(Intent.ACTION_SEND);
2305 send.setType("text/plain");
2306 send.putExtra(Intent.EXTRA_TEXT, url);
Paul Westbrook03e6d392010-02-12 10:33:29 -08002307 send.putExtra(Intent.EXTRA_SUBJECT, title);
Leon Scroggins96afcb12009-12-10 12:35:56 -05002308 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
2309 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
2310 try {
2311 c.startActivity(Intent.createChooser(send, c.getString(
2312 R.string.choosertitle_sharevia)));
2313 } catch(android.content.ActivityNotFoundException ex) {
2314 // if no app handles it, do nothing
2315 }
2316 }
2317
Leon Scroggins89c6d362009-07-15 16:54:37 -04002318 private void updateScreenshot(WebView view) {
2319 // If this is a bookmarked site, add a screenshot to the database.
2320 // FIXME: When should we update? Every time?
2321 // FIXME: Would like to make sure there is actually something to
2322 // draw, but the API for that (WebViewCore.pictureReady()) is not
2323 // currently accessible here.
Ben Murdochaac7aa62009-09-17 16:57:40 +01002324
Patrick Scott3918d442009-08-04 13:22:29 -04002325 ContentResolver cr = getContentResolver();
2326 final Cursor c = BrowserBookmarksAdapter.queryBookmarksForUrl(
Ben Murdochaac7aa62009-09-17 16:57:40 +01002327 cr, view.getOriginalUrl(), view.getUrl(), true);
Patrick Scott3918d442009-08-04 13:22:29 -04002328 if (c != null) {
Leon Scroggins89c6d362009-07-15 16:54:37 -04002329 boolean succeed = c.moveToFirst();
2330 ContentValues values = null;
2331 while (succeed) {
2332 if (values == null) {
2333 final ByteArrayOutputStream os
2334 = new ByteArrayOutputStream();
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002335 Bitmap bm = createScreenshot(view);
Leon Scroggins45800572009-09-29 16:38:47 -04002336 if (bm == null) {
2337 c.close();
2338 return;
2339 }
Leon Scroggins89c6d362009-07-15 16:54:37 -04002340 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
2341 values = new ContentValues();
2342 values.put(Browser.BookmarkColumns.THUMBNAIL,
2343 os.toByteArray());
2344 }
2345 cr.update(ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
2346 c.getInt(0)), values, null, null);
2347 succeed = c.moveToNext();
2348 }
2349 c.close();
2350 }
2351 }
2352
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002353 /**
Leon Scrogginsf8551612009-09-24 16:06:02 -04002354 * Values for the size of the thumbnail created when taking a screenshot.
2355 * Lazily initialized. Instead of using these directly, use
2356 * getDesiredThumbnailWidth() or getDesiredThumbnailHeight().
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002357 */
Leon Scrogginsf8551612009-09-24 16:06:02 -04002358 private static int THUMBNAIL_WIDTH = 0;
2359 private static int THUMBNAIL_HEIGHT = 0;
2360
2361 /**
2362 * Return the desired width for thumbnail screenshots, which are stored in
2363 * the database, and used on the bookmarks screen.
2364 * @param context Context for finding out the density of the screen.
2365 * @return int desired width for thumbnail screenshot.
2366 */
2367 /* package */ static int getDesiredThumbnailWidth(Context context) {
2368 if (THUMBNAIL_WIDTH == 0) {
2369 float density = context.getResources().getDisplayMetrics().density;
2370 THUMBNAIL_WIDTH = (int) (90 * density);
2371 THUMBNAIL_HEIGHT = (int) (80 * density);
2372 }
2373 return THUMBNAIL_WIDTH;
2374 }
2375
2376 /**
2377 * Return the desired height for thumbnail screenshots, which are stored in
2378 * the database, and used on the bookmarks screen.
2379 * @param context Context for finding out the density of the screen.
2380 * @return int desired height for thumbnail screenshot.
2381 */
2382 /* package */ static int getDesiredThumbnailHeight(Context context) {
2383 // To ensure that they are both initialized.
2384 getDesiredThumbnailWidth(context);
2385 return THUMBNAIL_HEIGHT;
2386 }
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002387
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002388 private Bitmap createScreenshot(WebView view) {
2389 Picture thumbnail = view.capturePicture();
Leon Scroggins45800572009-09-29 16:38:47 -04002390 if (thumbnail == null) {
2391 return null;
2392 }
Leon Scrogginsf8551612009-09-24 16:06:02 -04002393 Bitmap bm = Bitmap.createBitmap(getDesiredThumbnailWidth(this),
2394 getDesiredThumbnailHeight(this), Bitmap.Config.ARGB_4444);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002395 Canvas canvas = new Canvas(bm);
2396 // May need to tweak these values to determine what is the
2397 // best scale factor
Ben Murdoch2694e232009-09-29 09:41:11 +01002398 int thumbnailWidth = thumbnail.getWidth();
Ben Murdochae59c3f2009-10-20 18:30:28 +01002399 int thumbnailHeight = thumbnail.getHeight();
2400 float scaleFactorX = 1.0f;
2401 float scaleFactorY = 1.0f;
Ben Murdoch2694e232009-09-29 09:41:11 +01002402 if (thumbnailWidth > 0) {
Ben Murdochae59c3f2009-10-20 18:30:28 +01002403 scaleFactorX = (float) getDesiredThumbnailWidth(this) /
Ben Murdoch2694e232009-09-29 09:41:11 +01002404 (float)thumbnailWidth;
Ben Murdochae59c3f2009-10-20 18:30:28 +01002405 } else {
2406 return null;
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002407 }
Ben Murdochae59c3f2009-10-20 18:30:28 +01002408
2409 if (view.getWidth() > view.getHeight() &&
2410 thumbnailHeight < view.getHeight() && thumbnailHeight > 0) {
2411 // If the device is in landscape and the page is shorter
2412 // than the height of the view, stretch the thumbnail to fill the
2413 // space.
2414 scaleFactorY = (float) getDesiredThumbnailHeight(this) /
2415 (float)thumbnailHeight;
2416 } else {
2417 // In the portrait case, this looks nice.
2418 scaleFactorY = scaleFactorX;
2419 }
2420
2421 canvas.scale(scaleFactorX, scaleFactorY);
2422
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002423 thumbnail.draw(canvas);
2424 return bm;
2425 }
2426
The Android Open Source Project0c908882009-03-03 19:32:16 -08002427 // -------------------------------------------------------------------------
Grace Kloba22ac16e2009-10-07 18:00:23 -07002428 // Helper function for WebViewClient.
The Android Open Source Project0c908882009-03-03 19:32:16 -08002429 //-------------------------------------------------------------------------
2430
2431 // Use in overrideUrlLoading
2432 /* package */ final static String SCHEME_WTAI = "wtai://wp/";
2433 /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
2434 /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;";
2435 /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;";
2436
Leon Scroggins92472e82010-02-17 16:32:28 -05002437 // Keep this initial progress in sync with initialProgressValue (* 100)
2438 // in ProgressTracker.cpp
2439 private final static int INITIAL_PROGRESS = 10;
2440
Grace Kloba22ac16e2009-10-07 18:00:23 -07002441 void onPageStarted(WebView view, String url, Bitmap favicon) {
2442 // when BrowserActivity just starts, onPageStarted may be called before
2443 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
2444 // to start the timer. As we won't switch tabs while an activity is in
2445 // pause state, we can ensure calling resume and pause in pair.
2446 if (mActivityInPause) resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002447
Grace Kloba22ac16e2009-10-07 18:00:23 -07002448 resetLockIcon(url);
2449 setUrlTitle(url, null);
2450 setFavicon(favicon);
Leon Scroggins8cf8f682009-11-04 11:13:50 -08002451 // Show some progress so that the user knows the page is beginning to
2452 // load
Leon Scroggins92472e82010-02-17 16:32:28 -05002453 onProgressChanged(view, INITIAL_PROGRESS);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002454 mDidStopLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07002455 if (!mIsNetworkUp) createAndShowNetworkDialog();
Patrick Scott15525d42009-09-21 13:39:37 -04002456
Grace Kloba22ac16e2009-10-07 18:00:23 -07002457 if (mSettings.isTracing()) {
2458 String host;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002459 try {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002460 WebAddress uri = new WebAddress(url);
2461 host = uri.mHost;
2462 } catch (android.net.ParseException ex) {
2463 host = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -08002464 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002465 host = host.replace('.', '_');
2466 host += ".trace";
2467 mInTrace = true;
2468 Debug.startMethodTracing(host, 20 * 1024 * 1024);
2469 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002470
Grace Kloba22ac16e2009-10-07 18:00:23 -07002471 // Performance probe
2472 if (false) {
2473 mStart = SystemClock.uptimeMillis();
2474 mProcessStart = Process.getElapsedCpuTime();
2475 long[] sysCpu = new long[7];
2476 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2477 sysCpu, null)) {
2478 mUserStart = sysCpu[0] + sysCpu[1];
2479 mSystemStart = sysCpu[2];
2480 mIdleStart = sysCpu[3];
2481 mIrqStart = sysCpu[4] + sysCpu[5] + sysCpu[6];
2482 }
2483 mUiStart = SystemClock.currentThreadTimeMillis();
2484 }
2485 }
2486
2487 void onPageFinished(WebView view, String url) {
2488 // Reset the title and icon in case we stopped a provisional load.
2489 resetTitleAndIcon(view);
2490 // Update the lock icon image only once we are done loading
2491 updateLockIconToLatest();
2492 // pause the WebView timer and release the wake lock if it is finished
2493 // while BrowserActivity is in pause state.
2494 if (mActivityInPause && pauseWebViewTimers()) {
2495 if (mWakeLock.isHeld()) {
2496 mHandler.removeMessages(RELEASE_WAKELOCK);
2497 mWakeLock.release();
2498 }
2499 }
2500
2501 // Performance probe
2502 if (false) {
2503 long[] sysCpu = new long[7];
2504 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2505 sysCpu, null)) {
2506 String uiInfo = "UI thread used "
2507 + (SystemClock.currentThreadTimeMillis() - mUiStart)
2508 + " ms";
2509 if (LOGD_ENABLED) {
2510 Log.d(LOGTAG, uiInfo);
2511 }
2512 //The string that gets written to the log
2513 String performanceString = "It took total "
2514 + (SystemClock.uptimeMillis() - mStart)
2515 + " ms clock time to load the page."
2516 + "\nbrowser process used "
2517 + (Process.getElapsedCpuTime() - mProcessStart)
2518 + " ms, user processes used "
2519 + (sysCpu[0] + sysCpu[1] - mUserStart) * 10
2520 + " ms, kernel used "
2521 + (sysCpu[2] - mSystemStart) * 10
2522 + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10
2523 + " ms and irq took "
2524 + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart)
2525 * 10 + " ms, " + uiInfo;
2526 if (LOGD_ENABLED) {
2527 Log.d(LOGTAG, performanceString + "\nWebpage: " + url);
2528 }
2529 if (url != null) {
2530 // strip the url to maintain consistency
2531 String newUrl = new String(url);
2532 if (newUrl.startsWith("http://www.")) {
2533 newUrl = newUrl.substring(11);
2534 } else if (newUrl.startsWith("http://")) {
2535 newUrl = newUrl.substring(7);
2536 } else if (newUrl.startsWith("https://www.")) {
2537 newUrl = newUrl.substring(12);
2538 } else if (newUrl.startsWith("https://")) {
2539 newUrl = newUrl.substring(8);
2540 }
2541 if (LOGD_ENABLED) {
2542 Log.d(LOGTAG, newUrl + " loaded");
2543 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002544 }
2545 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002546 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002547
Grace Kloba22ac16e2009-10-07 18:00:23 -07002548 if (mInTrace) {
2549 mInTrace = false;
2550 Debug.stopMethodTracing();
2551 }
2552 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002553
Grace Kloba22ac16e2009-10-07 18:00:23 -07002554 boolean shouldOverrideUrlLoading(WebView view, String url) {
2555 if (url.startsWith(SCHEME_WTAI)) {
2556 // wtai://wp/mc;number
2557 // number=string(phone-number)
2558 if (url.startsWith(SCHEME_WTAI_MC)) {
2559 Intent intent = new Intent(Intent.ACTION_VIEW,
2560 Uri.parse(WebView.SCHEME_TEL +
2561 url.substring(SCHEME_WTAI_MC.length())));
2562 startActivity(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002563 return true;
2564 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002565 // wtai://wp/sd;dtmf
2566 // dtmf=string(dialstring)
2567 if (url.startsWith(SCHEME_WTAI_SD)) {
2568 // TODO: only send when there is active voice connection
2569 return false;
2570 }
2571 // wtai://wp/ap;number;name
2572 // number=string(phone-number)
2573 // name=string
2574 if (url.startsWith(SCHEME_WTAI_AP)) {
2575 // TODO
2576 return false;
2577 }
2578 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002579
Grace Kloba22ac16e2009-10-07 18:00:23 -07002580 // The "about:" schemes are internal to the browser; don't want these to
2581 // be dispatched to other apps.
2582 if (url.startsWith("about:")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002583 return false;
2584 }
2585
Grace Kloba22ac16e2009-10-07 18:00:23 -07002586 Intent intent;
2587 // perform generic parsing of the URI to turn it into an Intent.
2588 try {
2589 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
2590 } catch (URISyntaxException ex) {
2591 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
2592 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002593 }
2594
Grace Kloba22ac16e2009-10-07 18:00:23 -07002595 // check whether the intent can be resolved. If not, we will see
2596 // whether we can download it from the Market.
2597 if (getPackageManager().resolveActivity(intent, 0) == null) {
2598 String packagename = intent.getPackage();
2599 if (packagename != null) {
2600 intent = new Intent(Intent.ACTION_VIEW, Uri
2601 .parse("market://search?q=pname:" + packagename));
2602 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2603 startActivity(intent);
2604 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002605 } else {
2606 return false;
2607 }
2608 }
2609
Grace Kloba22ac16e2009-10-07 18:00:23 -07002610 // sanitize the Intent, ensuring web pages can not bypass browser
2611 // security (only access to BROWSABLE activities).
2612 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2613 intent.setComponent(null);
2614 try {
2615 if (startActivityIfNeeded(intent, -1)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002616 return true;
2617 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002618 } catch (ActivityNotFoundException ex) {
2619 // ignore the error. If no application can handle the URL,
2620 // eg about:blank, assume the browser can handle it.
2621 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002622
Grace Kloba22ac16e2009-10-07 18:00:23 -07002623 if (mMenuIsDown) {
2624 openTab(url);
2625 closeOptionsMenu();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002626 return true;
2627 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002628 return false;
2629 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002630
Grace Kloba22ac16e2009-10-07 18:00:23 -07002631 // -------------------------------------------------------------------------
2632 // Helper function for WebChromeClient
2633 // -------------------------------------------------------------------------
The Android Open Source Project0c908882009-03-03 19:32:16 -08002634
Grace Kloba22ac16e2009-10-07 18:00:23 -07002635 void onProgressChanged(WebView view, int newProgress) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002636 mFakeTitleBar.setProgress(newProgress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002637
Grace Kloba22ac16e2009-10-07 18:00:23 -07002638 if (newProgress == 100) {
2639 // onProgressChanged() may continue to be called after the main
2640 // frame has finished loading, as any remaining sub frames continue
2641 // to load. We'll only get called once though with newProgress as
2642 // 100 when everything is loaded. (onPageFinished is called once
2643 // when the main frame completes loading regardless of the state of
2644 // any sub frames so calls to onProgressChanges may continue after
2645 // onPageFinished has executed)
2646 if (mInLoad) {
2647 mInLoad = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -04002648 updateInLoadMenuItems();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002649 // If the options menu is open, leave the title bar
2650 if (!mOptionsMenuOpen || !mIconView) {
2651 hideFakeTitleBar();
The Android Open Source Projectcb9a0bb2009-03-11 12:11:58 -07002652 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002653 }
Leon Scrogginsaa37e7f2010-03-05 11:51:20 -05002654 } else {
2655 if (!mInLoad) {
2656 // onPageFinished may have already been called but a subframe is
2657 // still loading and updating the progress. Reset mInLoad and
2658 // update the menu items.
2659 mInLoad = true;
2660 updateInLoadMenuItems();
2661 }
2662 // When the page first begins to load, the Activity may still be
2663 // paused, in which case showFakeTitleBar will do nothing. Call
2664 // again as the page continues to load so that it will be shown.
2665 // (Calling it will the fake title bar is already showing will also
2666 // do nothing.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002667 if (!mOptionsMenuOpen || mIconView) {
2668 // This page has begun to load, so show the title bar
2669 showFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002670 }
2671 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002672 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002673
Grace Kloba22ac16e2009-10-07 18:00:23 -07002674 void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002675 // if a view already exists then immediately terminate the new one
2676 if (mCustomView != null) {
2677 callback.onCustomViewHidden();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002678 return;
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002679 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002680
2681 // Add the custom view to its container.
2682 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
2683 mCustomView = view;
2684 mCustomViewCallback = callback;
2685 // Save the menu state and set it to empty while the custom
2686 // view is showing.
2687 mOldMenuState = mMenuState;
2688 mMenuState = EMPTY_MENU;
2689 // Hide the content view.
2690 mContentView.setVisibility(View.GONE);
2691 // Finally show the custom view container.
Andrei Popescu163ab742009-10-20 17:58:23 +01002692 setStatusBarVisibility(false);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002693 mCustomViewContainer.setVisibility(View.VISIBLE);
2694 mCustomViewContainer.bringToFront();
2695 }
2696
2697 void onHideCustomView() {
2698 if (mCustomView == null)
2699 return;
2700
2701 // Hide the custom view.
2702 mCustomView.setVisibility(View.GONE);
2703 // Remove the custom view from its container.
2704 mCustomViewContainer.removeView(mCustomView);
2705 mCustomView = null;
2706 // Reset the old menu state.
2707 mMenuState = mOldMenuState;
2708 mOldMenuState = EMPTY_MENU;
2709 mCustomViewContainer.setVisibility(View.GONE);
2710 mCustomViewCallback.onCustomViewHidden();
2711 // Show the content view.
Andrei Popescu163ab742009-10-20 17:58:23 +01002712 setStatusBarVisibility(true);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002713 mContentView.setVisibility(View.VISIBLE);
2714 }
2715
2716 Bitmap getDefaultVideoPoster() {
2717 if (mDefaultVideoPoster == null) {
2718 mDefaultVideoPoster = BitmapFactory.decodeResource(
2719 getResources(), R.drawable.default_video_poster);
Patrick Scott3918d442009-08-04 13:22:29 -04002720 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002721 return mDefaultVideoPoster;
2722 }
Patrick Scott3918d442009-08-04 13:22:29 -04002723
Grace Kloba22ac16e2009-10-07 18:00:23 -07002724 View getVideoLoadingProgressView() {
2725 if (mVideoProgressView == null) {
2726 LayoutInflater inflater = LayoutInflater.from(BrowserActivity.this);
2727 mVideoProgressView = inflater.inflate(
2728 R.layout.video_loading_progress, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002729 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002730 return mVideoProgressView;
2731 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002732
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002733 /*
2734 * The Object used to inform the WebView of the file to upload.
2735 */
2736 private ValueCallback<Uri> mUploadMessage;
2737
Grace Kloba22ac16e2009-10-07 18:00:23 -07002738 void openFileChooser(ValueCallback<Uri> uploadMsg) {
2739 if (mUploadMessage != null) return;
2740 mUploadMessage = uploadMsg;
2741 Intent i = new Intent(Intent.ACTION_GET_CONTENT);
2742 i.addCategory(Intent.CATEGORY_OPENABLE);
2743 i.setType("*/*");
2744 BrowserActivity.this.startActivityForResult(Intent.createChooser(i,
2745 getString(R.string.choose_upload)), FILE_SELECTED);
2746 }
2747
2748 // -------------------------------------------------------------------------
2749 // Implement functions for DownloadListener
2750 // -------------------------------------------------------------------------
2751
The Android Open Source Project0c908882009-03-03 19:32:16 -08002752 /**
2753 * Notify the host application a download should be done, or that
2754 * the data should be streamed if a streaming viewer is available.
2755 * @param url The full url to the content that should be downloaded
2756 * @param contentDisposition Content-disposition http header, if
2757 * present.
2758 * @param mimetype The mimetype of the content reported by the server
2759 * @param contentLength The file size reported by the server
2760 */
2761 public void onDownloadStart(String url, String userAgent,
2762 String contentDisposition, String mimetype, long contentLength) {
2763 // if we're dealing wih A/V content that's not explicitly marked
2764 // for download, check if it's streamable.
2765 if (contentDisposition == null
Patrick Scotte1fb9662009-08-31 14:31:52 -04002766 || !contentDisposition.regionMatches(
2767 true, 0, "attachment", 0, 10)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002768 // query the package manager to see if there's a registered handler
2769 // that matches.
2770 Intent intent = new Intent(Intent.ACTION_VIEW);
2771 intent.setDataAndType(Uri.parse(url), mimetype);
Patrick Scotte1fb9662009-08-31 14:31:52 -04002772 ResolveInfo info = getPackageManager().resolveActivity(intent,
2773 PackageManager.MATCH_DEFAULT_ONLY);
2774 if (info != null) {
2775 ComponentName myName = getComponentName();
2776 // If we resolved to ourselves, we don't want to attempt to
2777 // load the url only to try and download it again.
2778 if (!myName.getPackageName().equals(
2779 info.activityInfo.packageName)
2780 || !myName.getClassName().equals(
2781 info.activityInfo.name)) {
2782 // someone (other than us) knows how to handle this mime
2783 // type with this scheme, don't download.
2784 try {
2785 startActivity(intent);
2786 return;
2787 } catch (ActivityNotFoundException ex) {
2788 if (LOGD_ENABLED) {
2789 Log.d(LOGTAG, "activity not found for " + mimetype
2790 + " over " + Uri.parse(url).getScheme(),
2791 ex);
2792 }
2793 // Best behavior is to fall back to a download in this
2794 // case
The Android Open Source Project0c908882009-03-03 19:32:16 -08002795 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002796 }
2797 }
2798 }
2799 onDownloadStartNoStream(url, userAgent, contentDisposition, mimetype, contentLength);
2800 }
2801
2802 /**
2803 * Notify the host application a download should be done, even if there
2804 * is a streaming viewer available for thise type.
2805 * @param url The full url to the content that should be downloaded
2806 * @param contentDisposition Content-disposition http header, if
2807 * present.
2808 * @param mimetype The mimetype of the content reported by the server
2809 * @param contentLength The file size reported by the server
2810 */
2811 /*package */ void onDownloadStartNoStream(String url, String userAgent,
2812 String contentDisposition, String mimetype, long contentLength) {
2813
2814 String filename = URLUtil.guessFileName(url,
2815 contentDisposition, mimetype);
2816
2817 // Check to see if we have an SDCard
2818 String status = Environment.getExternalStorageState();
2819 if (!status.equals(Environment.MEDIA_MOUNTED)) {
2820 int title;
2821 String msg;
2822
2823 // Check to see if the SDCard is busy, same as the music app
2824 if (status.equals(Environment.MEDIA_SHARED)) {
2825 msg = getString(R.string.download_sdcard_busy_dlg_msg);
2826 title = R.string.download_sdcard_busy_dlg_title;
2827 } else {
2828 msg = getString(R.string.download_no_sdcard_dlg_msg, filename);
2829 title = R.string.download_no_sdcard_dlg_title;
2830 }
2831
2832 new AlertDialog.Builder(this)
2833 .setTitle(title)
2834 .setIcon(android.R.drawable.ic_dialog_alert)
2835 .setMessage(msg)
2836 .setPositiveButton(R.string.ok, null)
2837 .show();
2838 return;
2839 }
2840
2841 // java.net.URI is a lot stricter than KURL so we have to undo
2842 // KURL's percent-encoding and redo the encoding using java.net.URI.
2843 URI uri = null;
2844 try {
2845 // Undo the percent-encoding that KURL may have done.
2846 String newUrl = new String(URLUtil.decode(url.getBytes()));
2847 // Parse the url into pieces
2848 WebAddress w = new WebAddress(newUrl);
2849 String frag = null;
2850 String query = null;
2851 String path = w.mPath;
2852 // Break the path into path, query, and fragment
2853 if (path.length() > 0) {
2854 // Strip the fragment
2855 int idx = path.lastIndexOf('#');
2856 if (idx != -1) {
2857 frag = path.substring(idx + 1);
2858 path = path.substring(0, idx);
2859 }
2860 idx = path.lastIndexOf('?');
2861 if (idx != -1) {
2862 query = path.substring(idx + 1);
2863 path = path.substring(0, idx);
2864 }
2865 }
2866 uri = new URI(w.mScheme, w.mAuthInfo, w.mHost, w.mPort, path,
2867 query, frag);
2868 } catch (Exception e) {
2869 Log.e(LOGTAG, "Could not parse url for download: " + url, e);
2870 return;
2871 }
2872
2873 // XXX: Have to use the old url since the cookies were stored using the
2874 // old percent-encoded url.
2875 String cookies = CookieManager.getInstance().getCookie(url);
2876
2877 ContentValues values = new ContentValues();
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08002878 values.put(Downloads.Impl.COLUMN_URI, uri.toString());
2879 values.put(Downloads.Impl.COLUMN_COOKIE_DATA, cookies);
2880 values.put(Downloads.Impl.COLUMN_USER_AGENT, userAgent);
2881 values.put(Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE,
The Android Open Source Project0c908882009-03-03 19:32:16 -08002882 getPackageName());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08002883 values.put(Downloads.Impl.COLUMN_NOTIFICATION_CLASS,
The Android Open Source Project0c908882009-03-03 19:32:16 -08002884 BrowserDownloadPage.class.getCanonicalName());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08002885 values.put(Downloads.Impl.COLUMN_VISIBILITY,
2886 Downloads.Impl.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
2887 values.put(Downloads.Impl.COLUMN_MIME_TYPE, mimetype);
2888 values.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, filename);
2889 values.put(Downloads.Impl.COLUMN_DESCRIPTION, uri.getHost());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002890 if (contentLength > 0) {
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08002891 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, contentLength);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002892 }
2893 if (mimetype == null) {
2894 // We must have long pressed on a link or image to download it. We
2895 // are not sure of the mimetype in this case, so do a head request
2896 new FetchUrlMimeType(this).execute(values);
2897 } else {
2898 final Uri contentUri =
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08002899 getContentResolver().insert(Downloads.Impl.CONTENT_URI, values);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002900 }
Leon Scroggins9191a7f2010-03-18 14:44:44 -04002901 Toast.makeText(this, R.string.download_pending, Toast.LENGTH_SHORT)
2902 .show();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002903 }
2904
Grace Kloba22ac16e2009-10-07 18:00:23 -07002905 // -------------------------------------------------------------------------
2906
The Android Open Source Project0c908882009-03-03 19:32:16 -08002907 /**
2908 * Resets the lock icon. This method is called when we start a new load and
2909 * know the url to be loaded.
2910 */
2911 private void resetLockIcon(String url) {
2912 // Save the lock-icon state (we revert to it if the load gets cancelled)
Grace Kloba22ac16e2009-10-07 18:00:23 -07002913 mTabControl.getCurrentTab().resetLockIcon(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002914 updateLockIconImage(LOCK_ICON_UNSECURE);
2915 }
2916
The Android Open Source Project0c908882009-03-03 19:32:16 -08002917 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04002918 * Update the lock icon to correspond to our latest state.
2919 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002920 private void updateLockIconToLatest() {
2921 updateLockIconImage(mTabControl.getCurrentTab().getLockIconType());
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04002922 }
2923
2924 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08002925 * Updates the lock-icon image in the title-bar.
2926 */
2927 private void updateLockIconImage(int lockIconType) {
2928 Drawable d = null;
2929 if (lockIconType == LOCK_ICON_SECURE) {
2930 d = mSecLockIcon;
2931 } else if (lockIconType == LOCK_ICON_MIXED) {
2932 d = mMixLockIcon;
2933 }
Leon Scroggins68579392009-09-15 15:31:54 -04002934 mTitleBar.setLock(d);
Leon Scrogginsfe87bd32009-10-06 10:10:00 -04002935 mFakeTitleBar.setLock(d);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002936 }
2937
2938 /**
2939 * Displays a page-info dialog.
2940 * @param tab The tab to show info about
2941 * @param fromShowSSLCertificateOnError The flag that indicates whether
2942 * this dialog was opened from the SSL-certificate-on-error dialog or
2943 * not. This is important, since we need to know whether to return to
2944 * the parent dialog or simply dismiss.
2945 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002946 private void showPageInfo(final Tab tab,
The Android Open Source Project0c908882009-03-03 19:32:16 -08002947 final boolean fromShowSSLCertificateOnError) {
2948 final LayoutInflater factory = LayoutInflater
2949 .from(this);
2950
2951 final View pageInfoView = factory.inflate(R.layout.page_info, null);
2952
2953 final WebView view = tab.getWebView();
2954
2955 String url = null;
2956 String title = null;
2957
2958 if (view == null) {
2959 url = tab.getUrl();
2960 title = tab.getTitle();
2961 } else if (view == mTabControl.getCurrentWebView()) {
2962 // Use the cached title and url if this is the current WebView
2963 url = mUrl;
2964 title = mTitle;
2965 } else {
2966 url = view.getUrl();
2967 title = view.getTitle();
2968 }
2969
2970 if (url == null) {
2971 url = "";
2972 }
2973 if (title == null) {
2974 title = "";
2975 }
2976
2977 ((TextView) pageInfoView.findViewById(R.id.address)).setText(url);
2978 ((TextView) pageInfoView.findViewById(R.id.title)).setText(title);
2979
2980 mPageInfoView = tab;
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05002981 mPageInfoFromShowSSLCertificateOnError = fromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002982
2983 AlertDialog.Builder alertDialogBuilder =
2984 new AlertDialog.Builder(this)
2985 .setTitle(R.string.page_info).setIcon(android.R.drawable.ic_dialog_info)
2986 .setView(pageInfoView)
2987 .setPositiveButton(
2988 R.string.ok,
2989 new DialogInterface.OnClickListener() {
2990 public void onClick(DialogInterface dialog,
2991 int whichButton) {
2992 mPageInfoDialog = null;
2993 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002994
2995 // if we came here from the SSL error dialog
2996 if (fromShowSSLCertificateOnError) {
2997 // go back to the SSL error dialog
2998 showSSLCertificateOnError(
2999 mSSLCertificateOnErrorView,
3000 mSSLCertificateOnErrorHandler,
3001 mSSLCertificateOnErrorError);
3002 }
3003 }
3004 })
3005 .setOnCancelListener(
3006 new DialogInterface.OnCancelListener() {
3007 public void onCancel(DialogInterface dialog) {
3008 mPageInfoDialog = null;
3009 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003010
3011 // if we came here from the SSL error dialog
3012 if (fromShowSSLCertificateOnError) {
3013 // go back to the SSL error dialog
3014 showSSLCertificateOnError(
3015 mSSLCertificateOnErrorView,
3016 mSSLCertificateOnErrorHandler,
3017 mSSLCertificateOnErrorError);
3018 }
3019 }
3020 });
3021
3022 // if we have a main top-level page SSL certificate set or a certificate
3023 // error
3024 if (fromShowSSLCertificateOnError ||
3025 (view != null && view.getCertificate() != null)) {
3026 // add a 'View Certificate' button
3027 alertDialogBuilder.setNeutralButton(
3028 R.string.view_certificate,
3029 new DialogInterface.OnClickListener() {
3030 public void onClick(DialogInterface dialog,
3031 int whichButton) {
3032 mPageInfoDialog = null;
3033 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003034
3035 // if we came here from the SSL error dialog
3036 if (fromShowSSLCertificateOnError) {
3037 // go back to the SSL error dialog
3038 showSSLCertificateOnError(
3039 mSSLCertificateOnErrorView,
3040 mSSLCertificateOnErrorHandler,
3041 mSSLCertificateOnErrorError);
3042 } else {
3043 // otherwise, display the top-most certificate from
3044 // the chain
3045 if (view.getCertificate() != null) {
3046 showSSLCertificate(tab);
3047 }
3048 }
3049 }
3050 });
3051 }
3052
3053 mPageInfoDialog = alertDialogBuilder.show();
3054 }
3055
3056 /**
3057 * Displays the main top-level page SSL certificate dialog
3058 * (accessible from the Page-Info dialog).
3059 * @param tab The tab to show certificate for.
3060 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003061 private void showSSLCertificate(final Tab tab) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003062 final View certificateView =
3063 inflateCertificateView(tab.getWebView().getCertificate());
3064 if (certificateView == null) {
3065 return;
3066 }
3067
3068 LayoutInflater factory = LayoutInflater.from(this);
3069
3070 final LinearLayout placeholder =
3071 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3072
3073 LinearLayout ll = (LinearLayout) factory.inflate(
3074 R.layout.ssl_success, placeholder);
3075 ((TextView)ll.findViewById(R.id.success))
3076 .setText(R.string.ssl_certificate_is_valid);
3077
3078 mSSLCertificateView = tab;
3079 mSSLCertificateDialog =
3080 new AlertDialog.Builder(this)
3081 .setTitle(R.string.ssl_certificate).setIcon(
3082 R.drawable.ic_dialog_browser_certificate_secure)
3083 .setView(certificateView)
3084 .setPositiveButton(R.string.ok,
3085 new DialogInterface.OnClickListener() {
3086 public void onClick(DialogInterface dialog,
3087 int whichButton) {
3088 mSSLCertificateDialog = null;
3089 mSSLCertificateView = null;
3090
3091 showPageInfo(tab, false);
3092 }
3093 })
3094 .setOnCancelListener(
3095 new DialogInterface.OnCancelListener() {
3096 public void onCancel(DialogInterface dialog) {
3097 mSSLCertificateDialog = null;
3098 mSSLCertificateView = null;
3099
3100 showPageInfo(tab, false);
3101 }
3102 })
3103 .show();
3104 }
3105
3106 /**
3107 * Displays the SSL error certificate dialog.
3108 * @param view The target web-view.
3109 * @param handler The SSL error handler responsible for cancelling the
3110 * connection that resulted in an SSL error or proceeding per user request.
3111 * @param error The SSL error object.
3112 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003113 void showSSLCertificateOnError(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003114 final WebView view, final SslErrorHandler handler, final SslError error) {
3115
3116 final View certificateView =
3117 inflateCertificateView(error.getCertificate());
3118 if (certificateView == null) {
3119 return;
3120 }
3121
3122 LayoutInflater factory = LayoutInflater.from(this);
3123
3124 final LinearLayout placeholder =
3125 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3126
3127 if (error.hasError(SslError.SSL_UNTRUSTED)) {
3128 LinearLayout ll = (LinearLayout)factory
3129 .inflate(R.layout.ssl_warning, placeholder);
3130 ((TextView)ll.findViewById(R.id.warning))
3131 .setText(R.string.ssl_untrusted);
3132 }
3133
3134 if (error.hasError(SslError.SSL_IDMISMATCH)) {
3135 LinearLayout ll = (LinearLayout)factory
3136 .inflate(R.layout.ssl_warning, placeholder);
3137 ((TextView)ll.findViewById(R.id.warning))
3138 .setText(R.string.ssl_mismatch);
3139 }
3140
3141 if (error.hasError(SslError.SSL_EXPIRED)) {
3142 LinearLayout ll = (LinearLayout)factory
3143 .inflate(R.layout.ssl_warning, placeholder);
3144 ((TextView)ll.findViewById(R.id.warning))
3145 .setText(R.string.ssl_expired);
3146 }
3147
3148 if (error.hasError(SslError.SSL_NOTYETVALID)) {
3149 LinearLayout ll = (LinearLayout)factory
3150 .inflate(R.layout.ssl_warning, placeholder);
3151 ((TextView)ll.findViewById(R.id.warning))
3152 .setText(R.string.ssl_not_yet_valid);
3153 }
3154
3155 mSSLCertificateOnErrorHandler = handler;
3156 mSSLCertificateOnErrorView = view;
3157 mSSLCertificateOnErrorError = error;
3158 mSSLCertificateOnErrorDialog =
3159 new AlertDialog.Builder(this)
3160 .setTitle(R.string.ssl_certificate).setIcon(
3161 R.drawable.ic_dialog_browser_certificate_partially_secure)
3162 .setView(certificateView)
3163 .setPositiveButton(R.string.ok,
3164 new DialogInterface.OnClickListener() {
3165 public void onClick(DialogInterface dialog,
3166 int whichButton) {
3167 mSSLCertificateOnErrorDialog = null;
3168 mSSLCertificateOnErrorView = null;
3169 mSSLCertificateOnErrorHandler = null;
3170 mSSLCertificateOnErrorError = null;
3171
Grace Kloba22ac16e2009-10-07 18:00:23 -07003172 view.getWebViewClient().onReceivedSslError(
3173 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003174 }
3175 })
3176 .setNeutralButton(R.string.page_info_view,
3177 new DialogInterface.OnClickListener() {
3178 public void onClick(DialogInterface dialog,
3179 int whichButton) {
3180 mSSLCertificateOnErrorDialog = null;
3181
3182 // do not clear the dialog state: we will
3183 // need to show the dialog again once the
3184 // user is done exploring the page-info details
3185
3186 showPageInfo(mTabControl.getTabFromView(view),
3187 true);
3188 }
3189 })
3190 .setOnCancelListener(
3191 new DialogInterface.OnCancelListener() {
3192 public void onCancel(DialogInterface dialog) {
3193 mSSLCertificateOnErrorDialog = null;
3194 mSSLCertificateOnErrorView = null;
3195 mSSLCertificateOnErrorHandler = null;
3196 mSSLCertificateOnErrorError = null;
3197
Grace Kloba22ac16e2009-10-07 18:00:23 -07003198 view.getWebViewClient().onReceivedSslError(
3199 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003200 }
3201 })
3202 .show();
3203 }
3204
3205 /**
3206 * Inflates the SSL certificate view (helper method).
3207 * @param certificate The SSL certificate.
3208 * @return The resultant certificate view with issued-to, issued-by,
3209 * issued-on, expires-on, and possibly other fields set.
3210 * If the input certificate is null, returns null.
3211 */
3212 private View inflateCertificateView(SslCertificate certificate) {
3213 if (certificate == null) {
3214 return null;
3215 }
3216
3217 LayoutInflater factory = LayoutInflater.from(this);
3218
3219 View certificateView = factory.inflate(
3220 R.layout.ssl_certificate, null);
3221
3222 // issued to:
3223 SslCertificate.DName issuedTo = certificate.getIssuedTo();
3224 if (issuedTo != null) {
3225 ((TextView) certificateView.findViewById(R.id.to_common))
3226 .setText(issuedTo.getCName());
3227 ((TextView) certificateView.findViewById(R.id.to_org))
3228 .setText(issuedTo.getOName());
3229 ((TextView) certificateView.findViewById(R.id.to_org_unit))
3230 .setText(issuedTo.getUName());
3231 }
3232
3233 // issued by:
3234 SslCertificate.DName issuedBy = certificate.getIssuedBy();
3235 if (issuedBy != null) {
3236 ((TextView) certificateView.findViewById(R.id.by_common))
3237 .setText(issuedBy.getCName());
3238 ((TextView) certificateView.findViewById(R.id.by_org))
3239 .setText(issuedBy.getOName());
3240 ((TextView) certificateView.findViewById(R.id.by_org_unit))
3241 .setText(issuedBy.getUName());
3242 }
3243
3244 // issued on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003245 String issuedOn = formatCertificateDate(
3246 certificate.getValidNotBeforeDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003247 ((TextView) certificateView.findViewById(R.id.issued_on))
3248 .setText(issuedOn);
3249
3250 // expires on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003251 String expiresOn = formatCertificateDate(
3252 certificate.getValidNotAfterDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003253 ((TextView) certificateView.findViewById(R.id.expires_on))
3254 .setText(expiresOn);
3255
3256 return certificateView;
3257 }
3258
3259 /**
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003260 * Formats the certificate date to a properly localized date string.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003261 * @return Properly localized version of the certificate date string and
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003262 * the "" if it fails to localize.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003263 */
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003264 private String formatCertificateDate(Date certificateDate) {
3265 if (certificateDate == null) {
3266 return "";
The Android Open Source Project0c908882009-03-03 19:32:16 -08003267 }
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003268 String formattedDate = DateFormat.getDateFormat(this).format(certificateDate);
3269 if (formattedDate == null) {
3270 return "";
3271 }
3272 return formattedDate;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003273 }
3274
3275 /**
3276 * Displays an http-authentication dialog.
3277 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003278 void showHttpAuthentication(final HttpAuthHandler handler,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003279 final String host, final String realm, final String title,
3280 final String name, final String password, int focusId) {
3281 LayoutInflater factory = LayoutInflater.from(this);
3282 final View v = factory
3283 .inflate(R.layout.http_authentication, null);
3284 if (name != null) {
3285 ((EditText) v.findViewById(R.id.username_edit)).setText(name);
3286 }
3287 if (password != null) {
3288 ((EditText) v.findViewById(R.id.password_edit)).setText(password);
3289 }
3290
3291 String titleText = title;
3292 if (titleText == null) {
3293 titleText = getText(R.string.sign_in_to).toString().replace(
3294 "%s1", host).replace("%s2", realm);
3295 }
3296
3297 mHttpAuthHandler = handler;
3298 AlertDialog dialog = new AlertDialog.Builder(this)
3299 .setTitle(titleText)
3300 .setIcon(android.R.drawable.ic_dialog_alert)
3301 .setView(v)
3302 .setPositiveButton(R.string.action,
3303 new DialogInterface.OnClickListener() {
3304 public void onClick(DialogInterface dialog,
3305 int whichButton) {
3306 String nm = ((EditText) v
3307 .findViewById(R.id.username_edit))
3308 .getText().toString();
3309 String pw = ((EditText) v
3310 .findViewById(R.id.password_edit))
3311 .getText().toString();
3312 BrowserActivity.this.setHttpAuthUsernamePassword
3313 (host, realm, nm, pw);
3314 handler.proceed(nm, pw);
3315 mHttpAuthenticationDialog = null;
3316 mHttpAuthHandler = null;
3317 }})
3318 .setNegativeButton(R.string.cancel,
3319 new DialogInterface.OnClickListener() {
3320 public void onClick(DialogInterface dialog,
3321 int whichButton) {
3322 handler.cancel();
3323 BrowserActivity.this.resetTitleAndRevertLockIcon();
3324 mHttpAuthenticationDialog = null;
3325 mHttpAuthHandler = null;
3326 }})
3327 .setOnCancelListener(new DialogInterface.OnCancelListener() {
3328 public void onCancel(DialogInterface dialog) {
3329 handler.cancel();
3330 BrowserActivity.this.resetTitleAndRevertLockIcon();
3331 mHttpAuthenticationDialog = null;
3332 mHttpAuthHandler = null;
3333 }})
3334 .create();
3335 // Make the IME appear when the dialog is displayed if applicable.
3336 dialog.getWindow().setSoftInputMode(
3337 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
3338 dialog.show();
3339 if (focusId != 0) {
3340 dialog.findViewById(focusId).requestFocus();
3341 } else {
3342 v.findViewById(R.id.username_edit).requestFocus();
3343 }
3344 mHttpAuthenticationDialog = dialog;
3345 }
3346
3347 public int getProgress() {
3348 WebView w = mTabControl.getCurrentWebView();
3349 if (w != null) {
3350 return w.getProgress();
3351 } else {
3352 return 100;
3353 }
3354 }
3355
3356 /**
3357 * Set HTTP authentication password.
3358 *
3359 * @param host The host for the password
3360 * @param realm The realm for the password
3361 * @param username The username for the password. If it is null, it means
3362 * password can't be saved.
3363 * @param password The password
3364 */
3365 public void setHttpAuthUsernamePassword(String host, String realm,
3366 String username,
3367 String password) {
3368 WebView w = mTabControl.getCurrentWebView();
3369 if (w != null) {
3370 w.setHttpAuthUsernamePassword(host, realm, username, password);
3371 }
3372 }
3373
3374 /**
3375 * connectivity manager says net has come or gone... inform the user
3376 * @param up true if net has come up, false if net has gone down
3377 */
3378 public void onNetworkToggle(boolean up) {
3379 if (up == mIsNetworkUp) {
3380 return;
3381 } else if (up) {
3382 mIsNetworkUp = true;
3383 if (mAlertDialog != null) {
3384 mAlertDialog.cancel();
3385 mAlertDialog = null;
3386 }
3387 } else {
3388 mIsNetworkUp = false;
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003389 if (mInLoad) {
3390 createAndShowNetworkDialog();
3391 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003392 }
3393 WebView w = mTabControl.getCurrentWebView();
3394 if (w != null) {
3395 w.setNetworkAvailable(up);
3396 }
3397 }
3398
Grace Kloba22ac16e2009-10-07 18:00:23 -07003399 boolean isNetworkUp() {
3400 return mIsNetworkUp;
3401 }
3402
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003403 // This method shows the network dialog alerting the user that the net is
3404 // down. It will only show the dialog if mAlertDialog is null.
3405 private void createAndShowNetworkDialog() {
3406 if (mAlertDialog == null) {
3407 mAlertDialog = new AlertDialog.Builder(this)
3408 .setTitle(R.string.loadSuspendedTitle)
3409 .setMessage(R.string.loadSuspended)
3410 .setPositiveButton(R.string.ok, null)
3411 .show();
3412 }
3413 }
3414
The Android Open Source Project0c908882009-03-03 19:32:16 -08003415 @Override
3416 protected void onActivityResult(int requestCode, int resultCode,
3417 Intent intent) {
Grace Klobabb394f32009-11-19 10:26:37 -08003418 if (getTopWindow() == null) return;
3419
The Android Open Source Project0c908882009-03-03 19:32:16 -08003420 switch (requestCode) {
3421 case COMBO_PAGE:
3422 if (resultCode == RESULT_OK && intent != null) {
3423 String data = intent.getAction();
3424 Bundle extras = intent.getExtras();
3425 if (extras != null && extras.getBoolean("new_window", false)) {
Leon Scroggins25d35472009-09-15 11:37:27 -04003426 openTab(data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003427 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -07003428 final Tab currentTab =
The Android Open Source Project0c908882009-03-03 19:32:16 -08003429 mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04003430 dismissSubWindow(currentTab);
3431 if (data != null && data.length() != 0) {
Leon Scroggins92472e82010-02-17 16:32:28 -05003432 loadUrl(getTopWindow(), data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003433 }
3434 }
3435 }
Leon Scrogginsfde97462010-01-11 13:06:21 -05003436 // Deliberately fall through to PREFERENCES_PAGE, since the
3437 // same extra may be attached to the COMBO_PAGE
3438 case PREFERENCES_PAGE:
3439 if (resultCode == RESULT_OK && intent != null) {
3440 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
3441 if (BrowserSettings.PREF_CLEAR_HISTORY.equals(action)) {
3442 mTabControl.removeParentChildRelationShips();
3443 }
3444 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003445 break;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003446 // Choose a file from the file picker.
3447 case FILE_SELECTED:
3448 if (null == mUploadMessage) break;
3449 Uri result = intent == null || resultCode != RESULT_OK ? null
3450 : intent.getData();
3451 mUploadMessage.onReceiveValue(result);
3452 mUploadMessage = null;
3453 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003454 default:
3455 break;
3456 }
Leon Scroggins30444232009-09-04 18:36:20 -04003457 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003458 }
3459
3460 /*
3461 * This method is called as a result of the user selecting the options
Leon Scrogginsf08809b2010-01-21 09:27:46 -05003462 * menu to see the download window. It shows the download window on top of
3463 * the current window.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003464 */
Leon Scrogginsf08809b2010-01-21 09:27:46 -05003465 private void viewDownloads(Uri downloadRecord) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003466 Intent intent = new Intent(this,
3467 BrowserDownloadPage.class);
3468 intent.setData(downloadRecord);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003469 startActivityForResult(intent, BrowserActivity.DOWNLOAD_PAGE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003470
3471 }
3472
Leon Scroggins160a7e72009-08-14 18:28:01 -04003473 /**
3474 * Open the Go page.
3475 * @param startWithHistory If true, open starting on the history tab.
3476 * Otherwise, start with the bookmarks tab.
Leon Scroggins160a7e72009-08-14 18:28:01 -04003477 */
Leon Scroggins30444232009-09-04 18:36:20 -04003478 /* package */ void bookmarksOrHistoryPicker(boolean startWithHistory) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003479 WebView current = mTabControl.getCurrentWebView();
3480 if (current == null) {
3481 return;
3482 }
3483 Intent intent = new Intent(this,
3484 CombinedBookmarkHistoryActivity.class);
3485 String title = current.getTitle();
3486 String url = current.getUrl();
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003487 Bitmap thumbnail = createScreenshot(current);
3488
The Android Open Source Project0c908882009-03-03 19:32:16 -08003489 // Just in case the user opens bookmarks before a page finishes loading
3490 // so the current history item, and therefore the page, is null.
3491 if (null == url) {
3492 url = mLastEnteredUrl;
3493 // This can happen.
3494 if (null == url) {
3495 url = mSettings.getHomePage();
3496 }
3497 }
3498 // In case the web page has not yet received its associated title.
3499 if (title == null) {
3500 title = url;
3501 }
3502 intent.putExtra("title", title);
3503 intent.putExtra("url", url);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003504 intent.putExtra("thumbnail", thumbnail);
Leon Scroggins30444232009-09-04 18:36:20 -04003505 // Disable opening in a new window if we have maxed out the windows
Grace Kloba22ac16e2009-10-07 18:00:23 -07003506 intent.putExtra("disable_new_window", !mTabControl.canCreateNewTab());
Patrick Scott3918d442009-08-04 13:22:29 -04003507 intent.putExtra("touch_icon_url", current.getTouchIconUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003508 if (startWithHistory) {
3509 intent.putExtra(CombinedBookmarkHistoryActivity.STARTING_TAB,
3510 CombinedBookmarkHistoryActivity.HISTORY_TAB);
3511 }
3512 startActivityForResult(intent, COMBO_PAGE);
3513 }
3514
3515 // Called when loading from context menu or LOAD_URL message
Leon Scroggins92472e82010-02-17 16:32:28 -05003516 private void loadUrlFromContext(WebView view, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003517 // In case the user enters nothing.
3518 if (url != null && url.length() != 0 && view != null) {
3519 url = smartUrlFilter(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003520 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
Leon Scroggins92472e82010-02-17 16:32:28 -05003521 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003522 }
3523 }
3524 }
3525
Leon Scroggins92472e82010-02-17 16:32:28 -05003526 /**
3527 * Load the URL into the given WebView and update the title bar
3528 * to reflect the new load. Call this instead of WebView.loadUrl
3529 * directly.
3530 * @param view The WebView used to load url.
3531 * @param url The URL to load.
3532 */
3533 private void loadUrl(WebView view, String url) {
3534 updateTitleBarForNewLoad(view, url);
3535 view.loadUrl(url);
3536 }
3537
3538 /**
3539 * Load UrlData into a Tab and update the title bar to reflect the new
3540 * load. Call this instead of UrlData.loadIn directly.
3541 * @param t The Tab used to load.
3542 * @param data The UrlData being loaded.
3543 */
3544 private void loadUrlDataIn(Tab t, UrlData data) {
3545 updateTitleBarForNewLoad(t.getWebView(), data.mUrl);
3546 data.loadIn(t);
3547 }
3548
3549 /**
3550 * If the WebView is the top window, update the title bar to reflect
3551 * loading the new URL. i.e. set its text, clear the favicon (which
3552 * will be set once the page begins loading), and set the progress to
3553 * INITIAL_PROGRESS to show that the page has begun to load. Called
3554 * by loadUrl and loadUrlDataIn.
3555 * @param view The WebView that is starting a load.
3556 * @param url The URL that is being loaded.
3557 */
3558 private void updateTitleBarForNewLoad(WebView view, String url) {
3559 if (view == getTopWindow()) {
3560 setUrlTitle(url, null);
3561 setFavicon(null);
3562 onProgressChanged(view, INITIAL_PROGRESS);
3563 }
3564 }
3565
The Android Open Source Project0c908882009-03-03 19:32:16 -08003566 private String smartUrlFilter(Uri inUri) {
3567 if (inUri != null) {
3568 return smartUrlFilter(inUri.toString());
3569 }
3570 return null;
3571 }
3572
Feng Qianb34f87a2009-03-24 21:27:26 -07003573 protected static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003574 "(?i)" + // switch on case insensitive matching
3575 "(" + // begin group for schema
3576 "(?:http|https|file):\\/\\/" +
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003577 "|(?:inline|data|about|content|javascript):" +
The Android Open Source Project0c908882009-03-03 19:32:16 -08003578 ")" +
3579 "(.*)" );
3580
3581 /**
3582 * Attempts to determine whether user input is a URL or search
3583 * terms. Anything with a space is passed to search.
3584 *
3585 * Converts to lowercase any mistakenly uppercased schema (i.e.,
3586 * "Http://" converts to "http://"
3587 *
3588 * @return Original or modified URL
3589 *
3590 */
3591 String smartUrlFilter(String url) {
3592
3593 String inUrl = url.trim();
3594 boolean hasSpace = inUrl.indexOf(' ') != -1;
3595
3596 Matcher matcher = ACCEPTED_URI_SCHEMA.matcher(inUrl);
3597 if (matcher.matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003598 // force scheme to lowercase
3599 String scheme = matcher.group(1);
3600 String lcScheme = scheme.toLowerCase();
3601 if (!lcScheme.equals(scheme)) {
Mitsuru Oshima123ecfb2009-05-18 19:11:14 -07003602 inUrl = lcScheme + matcher.group(2);
3603 }
3604 if (hasSpace) {
3605 inUrl = inUrl.replace(" ", "%20");
The Android Open Source Project0c908882009-03-03 19:32:16 -08003606 }
3607 return inUrl;
3608 }
3609 if (hasSpace) {
Satish Sampath565505b2009-05-29 15:37:27 +01003610 // FIXME: Is this the correct place to add to searches?
3611 // what if someone else calls this function?
3612 int shortcut = parseUrlShortcut(inUrl);
3613 if (shortcut != SHORTCUT_INVALID) {
3614 Browser.addSearchUrl(mResolver, inUrl);
3615 String query = inUrl.substring(2);
3616 switch (shortcut) {
3617 case SHORTCUT_GOOGLE_SEARCH:
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003618 return URLUtil.composeSearchUrl(query, QuickSearch_G, QUERY_PLACE_HOLDER);
Satish Sampath565505b2009-05-29 15:37:27 +01003619 case SHORTCUT_WIKIPEDIA_SEARCH:
3620 return URLUtil.composeSearchUrl(query, QuickSearch_W, QUERY_PLACE_HOLDER);
3621 case SHORTCUT_DICTIONARY_SEARCH:
3622 return URLUtil.composeSearchUrl(query, QuickSearch_D, QUERY_PLACE_HOLDER);
3623 case SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH:
The Android Open Source Project0c908882009-03-03 19:32:16 -08003624 // FIXME: we need location in this case
Satish Sampath565505b2009-05-29 15:37:27 +01003625 return URLUtil.composeSearchUrl(query, QuickSearch_L, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003626 }
3627 }
3628 } else {
Dan Egnor5ee906c2009-11-18 12:11:49 -08003629 if (Patterns.WEB_URL.matcher(inUrl).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003630 return URLUtil.guessUrl(inUrl);
3631 }
3632 }
3633
3634 Browser.addSearchUrl(mResolver, inUrl);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003635 return URLUtil.composeSearchUrl(inUrl, QuickSearch_G, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003636 }
3637
Ben Murdochbff2d602009-07-01 20:19:05 +01003638 /* package */ void setShouldShowErrorConsole(boolean flag) {
3639 if (flag == mShouldShowErrorConsole) {
3640 // Nothing to do.
3641 return;
3642 }
3643
3644 mShouldShowErrorConsole = flag;
3645
Grace Kloba22ac16e2009-10-07 18:00:23 -07003646 ErrorConsoleView errorConsole = mTabControl.getCurrentTab()
3647 .getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01003648
3649 if (flag) {
3650 // Setting the show state of the console will cause it's the layout to be inflated.
3651 if (errorConsole.numberOfErrors() > 0) {
3652 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
3653 } else {
3654 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
3655 }
3656
3657 // Now we can add it to the main view.
3658 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08003659 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01003660 ViewGroup.LayoutParams.WRAP_CONTENT));
3661 } else {
3662 mErrorConsoleContainer.removeView(errorConsole);
3663 }
3664
3665 }
3666
Grace Kloba22ac16e2009-10-07 18:00:23 -07003667 boolean shouldShowErrorConsole() {
3668 return mShouldShowErrorConsole;
3669 }
3670
Andrei Popescu163ab742009-10-20 17:58:23 +01003671 private void setStatusBarVisibility(boolean visible) {
3672 int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
3673 getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
3674 }
3675
Andrei Popescu56199cc2010-01-12 22:39:16 +00003676
3677 private void sendNetworkType(String type, String subtype) {
3678 WebView w = mTabControl.getCurrentWebView();
3679 if (w != null) {
3680 w.setNetworkType(type, subtype);
3681 }
3682 }
3683
Andrei Popescu30995e72010-02-09 16:59:58 +00003684 private void packageChanged(String packageName, boolean wasAdded) {
3685 WebView w = mTabControl.getCurrentWebView();
3686 if (w == null) {
3687 return;
3688 }
3689
3690 if (wasAdded) {
3691 w.addPackageName(packageName);
3692 } else {
3693 w.removePackageName(packageName);
3694 }
3695 }
3696
3697 private void addPackageNames(Set<String> packageNames) {
3698 WebView w = mTabControl.getCurrentWebView();
3699 if (w == null) {
3700 return;
3701 }
3702
3703 w.addPackageNames(packageNames);
3704 }
3705
3706 private void getInstalledPackages() {
3707 AsyncTask<Void, Void, Set<String> > task =
3708 new AsyncTask<Void, Void, Set<String> >() {
3709 protected Set<String> doInBackground(Void... unused) {
3710 Set<String> installedPackages = new HashSet<String>();
3711 PackageManager pm = BrowserActivity.this.getPackageManager();
3712 if (pm != null) {
3713 List<PackageInfo> packages = pm.getInstalledPackages(0);
3714 for (PackageInfo p : packages) {
3715 if (BrowserActivity.this.sGoogleApps.contains(p.packageName)) {
3716 installedPackages.add(p.packageName);
3717 }
3718 }
3719 }
3720
3721 return installedPackages;
3722 }
3723
3724 // Executes on the UI thread
3725 protected void onPostExecute(Set<String> installedPackages) {
3726 addPackageNames(installedPackages);
3727 }
3728 };
3729 task.execute();
3730 }
3731
Grace Klobaeb6eef42009-09-15 17:56:32 -07003732 final static int LOCK_ICON_UNSECURE = 0;
3733 final static int LOCK_ICON_SECURE = 1;
3734 final static int LOCK_ICON_MIXED = 2;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003735
The Android Open Source Project0c908882009-03-03 19:32:16 -08003736 private BrowserSettings mSettings;
3737 private TabControl mTabControl;
3738 private ContentResolver mResolver;
3739 private FrameLayout mContentView;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003740 private View mCustomView;
3741 private FrameLayout mCustomViewContainer;
Andrei Popescuc9b55562009-07-07 10:51:15 +01003742 private WebChromeClient.CustomViewCallback mCustomViewCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003743
3744 // FIXME, temp address onPrepareMenu performance problem. When we move everything out of
3745 // view, we should rewrite this.
3746 private int mCurrentMenuState = 0;
3747 private int mMenuState = R.id.MAIN_MENU;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003748 private int mOldMenuState = EMPTY_MENU;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003749 private static final int EMPTY_MENU = -1;
3750 private Menu mMenu;
3751
3752 private FindDialog mFindDialog;
3753 // Used to prevent chording to result in firing two shortcuts immediately
3754 // one after another. Fixes bug 1211714.
3755 boolean mCanChord;
3756
3757 private boolean mInLoad;
3758 private boolean mIsNetworkUp;
Ben Murdochb7cc8b42009-09-28 10:59:09 +01003759 private boolean mDidStopLoad;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003760
Cary Clark1f10cbf2010-03-22 11:45:23 -04003761 /* package */ boolean mActivityInPause = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003762
3763 private boolean mMenuIsDown;
3764
The Android Open Source Project0c908882009-03-03 19:32:16 -08003765 private static boolean mInTrace;
3766
3767 // Performance probe
3768 private static final int[] SYSTEM_CPU_FORMAT = new int[] {
3769 Process.PROC_SPACE_TERM | Process.PROC_COMBINE,
3770 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time
3771 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time
3772 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time
3773 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time
3774 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time
3775 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time
3776 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG // 7: softirq time
3777 };
3778
3779 private long mStart;
3780 private long mProcessStart;
3781 private long mUserStart;
3782 private long mSystemStart;
3783 private long mIdleStart;
3784 private long mIrqStart;
3785
3786 private long mUiStart;
3787
3788 private Drawable mMixLockIcon;
3789 private Drawable mSecLockIcon;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003790
3791 /* hold a ref so we can auto-cancel if necessary */
3792 private AlertDialog mAlertDialog;
3793
The Android Open Source Project0c908882009-03-03 19:32:16 -08003794 // The up-to-date URL and title (these can be different from those stored
3795 // in WebView, since it takes some time for the information in WebView to
3796 // get updated)
3797 private String mUrl;
3798 private String mTitle;
3799
3800 // As PageInfo has different style for landscape / portrait, we have
3801 // to re-open it when configuration changed
3802 private AlertDialog mPageInfoDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07003803 private Tab mPageInfoView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003804 // If the Page-Info dialog is launched from the SSL-certificate-on-error
3805 // dialog, we should not just dismiss it, but should get back to the
3806 // SSL-certificate-on-error dialog. This flag is used to store this state
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05003807 private boolean mPageInfoFromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003808
3809 // as SSLCertificateOnError has different style for landscape / portrait,
3810 // we have to re-open it when configuration changed
3811 private AlertDialog mSSLCertificateOnErrorDialog;
3812 private WebView mSSLCertificateOnErrorView;
3813 private SslErrorHandler mSSLCertificateOnErrorHandler;
3814 private SslError mSSLCertificateOnErrorError;
3815
3816 // as SSLCertificate has different style for landscape / portrait, we
3817 // have to re-open it when configuration changed
3818 private AlertDialog mSSLCertificateDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07003819 private Tab mSSLCertificateView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003820
3821 // as HttpAuthentication has different style for landscape / portrait, we
3822 // have to re-open it when configuration changed
3823 private AlertDialog mHttpAuthenticationDialog;
3824 private HttpAuthHandler mHttpAuthHandler;
3825
3826 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
3827 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08003828 ViewGroup.LayoutParams.MATCH_PARENT,
3829 ViewGroup.LayoutParams.MATCH_PARENT);
Andrei Popescuadc008d2009-06-26 14:11:30 +01003830 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
3831 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08003832 ViewGroup.LayoutParams.MATCH_PARENT,
3833 ViewGroup.LayoutParams.MATCH_PARENT,
Andrei Popescuadc008d2009-06-26 14:11:30 +01003834 Gravity.CENTER);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003835 // Google search
3836 final static String QuickSearch_G = "http://www.google.com/m?q=%s";
The Android Open Source Project0c908882009-03-03 19:32:16 -08003837 // Wikipedia search
3838 final static String QuickSearch_W = "http://en.wikipedia.org/w/index.php?search=%s&go=Go";
3839 // Dictionary search
3840 final static String QuickSearch_D = "http://dictionary.reference.com/search?q=%s";
3841 // Google Mobile Local search
3842 final static String QuickSearch_L = "http://www.google.com/m/search?site=local&q=%s&near=mountain+view";
3843
3844 final static String QUERY_PLACE_HOLDER = "%s";
3845
3846 // "source" parameter for Google search through search key
3847 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
3848 // "source" parameter for Google search through goto menu
3849 final static String GOOGLE_SEARCH_SOURCE_GOTO = "browser-goto";
3850 // "source" parameter for Google search through simplily type
3851 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
3852 // "source" parameter for Google search suggested by the browser
3853 final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
3854 // "source" parameter for Google search from unknown source
3855 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown";
3856
3857 private final static String LOGTAG = "browser";
3858
The Android Open Source Project0c908882009-03-03 19:32:16 -08003859 private String mLastEnteredUrl;
3860
3861 private PowerManager.WakeLock mWakeLock;
3862 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
3863
3864 private Toast mStopToast;
3865
Leon Scroggins68579392009-09-15 15:31:54 -04003866 private TitleBar mTitleBar;
Leon Scroggins81db3662009-06-04 17:45:11 -04003867
Ben Murdochbff2d602009-07-01 20:19:05 +01003868 private LinearLayout mErrorConsoleContainer = null;
3869 private boolean mShouldShowErrorConsole = false;
3870
The Android Open Source Project0c908882009-03-03 19:32:16 -08003871 // As the ids are dynamically created, we can't guarantee that they will
3872 // be in sequence, so this static array maps ids to a window number.
3873 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
3874 { R.id.window_one_menu_id, R.id.window_two_menu_id, R.id.window_three_menu_id,
3875 R.id.window_four_menu_id, R.id.window_five_menu_id, R.id.window_six_menu_id,
3876 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
3877
3878 // monitor platform changes
3879 private IntentFilter mNetworkStateChangedFilter;
3880 private BroadcastReceiver mNetworkStateIntentReceiver;
3881
Grace Klobab4da0ad2009-05-14 14:45:40 -07003882 private BroadcastReceiver mPackageInstallationReceiver;
3883
Bjorn Bringerta7611812010-03-24 11:12:02 +00003884 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
3885
The Android Open Source Project0c908882009-03-03 19:32:16 -08003886 // activity requestCode
Nicolas Roard78a98e42009-05-11 13:34:17 +01003887 final static int COMBO_PAGE = 1;
3888 final static int DOWNLOAD_PAGE = 2;
3889 final static int PREFERENCES_PAGE = 3;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003890 final static int FILE_SELECTED = 4;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003891
Andrei Popescu540035d2009-09-18 18:59:20 +01003892 // the default <video> poster
3893 private Bitmap mDefaultVideoPoster;
3894 // the video progress view
3895 private View mVideoProgressView;
3896
Andrei Popescu30995e72010-02-09 16:59:58 +00003897 // The Google packages we monitor for the navigator.isApplicationInstalled()
3898 // API. Add as needed.
3899 private static Set<String> sGoogleApps;
3900 static {
3901 sGoogleApps = new HashSet<String>();
3902 sGoogleApps.add("com.google.android.youtube");
3903 }
3904
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003905 /**
3906 * A UrlData class to abstract how the content will be set to WebView.
3907 * This base class uses loadUrl to show the content.
3908 */
Leon Scroggins6eac63e2010-03-15 18:19:14 -04003909 /* package */ static class UrlData {
Grace Kloba068e48b2010-01-26 18:11:27 -08003910 final String mUrl;
3911 final Map<String, String> mHeaders;
Leon Scroggins58d56c62010-01-28 15:12:40 -05003912 final Intent mVoiceIntent;
Grace Kloba60e095c2009-06-16 11:50:55 -07003913
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003914 UrlData(String url) {
3915 this.mUrl = url;
Grace Kloba068e48b2010-01-26 18:11:27 -08003916 this.mHeaders = null;
Leon Scroggins58d56c62010-01-28 15:12:40 -05003917 this.mVoiceIntent = null;
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003918 }
Grace Kloba60e095c2009-06-16 11:50:55 -07003919
Leon Scroggins58d56c62010-01-28 15:12:40 -05003920 UrlData(String url, Map<String, String> headers, Intent intent) {
Grace Kloba068e48b2010-01-26 18:11:27 -08003921 this.mUrl = url;
3922 this.mHeaders = headers;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -05003923 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
3924 .equals(intent.getAction())) {
Leon Scroggins58d56c62010-01-28 15:12:40 -05003925 this.mVoiceIntent = intent;
3926 } else {
3927 this.mVoiceIntent = null;
3928 }
Grace Kloba60e095c2009-06-16 11:50:55 -07003929 }
3930
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003931 boolean isEmpty() {
Leon Scroggins58d56c62010-01-28 15:12:40 -05003932 return mVoiceIntent == null && (mUrl == null || mUrl.length() == 0);
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003933 }
3934
Leon Scroggins92472e82010-02-17 16:32:28 -05003935 /**
3936 * Load this UrlData into the given Tab. Use loadUrlDataIn to update
3937 * the title bar as well.
3938 */
Leon Scroggins58d56c62010-01-28 15:12:40 -05003939 public void loadIn(Tab t) {
3940 if (mVoiceIntent != null) {
3941 t.activateVoiceSearchMode(mVoiceIntent);
3942 } else {
3943 t.getWebView().loadUrl(mUrl, mHeaders);
3944 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003945 }
3946 };
3947
Leon Scroggins1f005d32009-08-10 17:36:42 -04003948 /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003949}