blob: 1f151860238adcbe8b2f4e0898ba2e5943b0bd88 [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();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800368 }
369
Leon Scroggins58d56c62010-01-28 15:12:40 -0500370 /**
371 * Feed the previously stored results strings to the BrowserProvider so that
372 * the SearchDialog will show them instead of the standard searches.
373 * @param result String to show on the editable line of the SearchDialog.
374 */
375 /* package */ void showVoiceSearchResults(String result) {
376 ContentProviderClient client = mResolver.acquireContentProviderClient(
377 Browser.BOOKMARKS_URI);
378 ContentProvider prov = client.getLocalContentProvider();
379 BrowserProvider bp = (BrowserProvider) prov;
380 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
381 client.release();
382
Leon Scrogginsfbb3f152010-03-09 17:26:56 -0500383 Bundle bundle = createGoogleSearchSourceBundle(
384 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
385 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
386 startSearch(result, false, bundle, false);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500387 }
388
The Android Open Source Project0c908882009-03-03 19:32:16 -0800389 @Override
390 protected void onNewIntent(Intent intent) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700391 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800392 // When a tab is closed on exit, the current tab index is set to -1.
393 // Reset before proceed as Browser requires the current tab to be set.
394 if (current == null) {
395 // Try to reset the tab in case the index was incorrect.
396 current = mTabControl.getTab(0);
397 if (current == null) {
398 // No tabs at all so just ignore this intent.
399 return;
400 }
401 mTabControl.setCurrentTab(current);
402 attachTabToContentView(current);
403 resetTitleAndIcon(current.getWebView());
404 }
405 final String action = intent.getAction();
406 final int flags = intent.getFlags();
407 if (Intent.ACTION_MAIN.equals(action) ||
408 (flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
409 // just resume the browser
410 return;
411 }
Leon Scrogginsb8a844d2010-03-18 15:06:15 -0400412 // In case the SearchDialog is open.
413 ((SearchManager) getSystemService(Context.SEARCH_SERVICE))
414 .stopSearch();
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500415 boolean activateVoiceSearch = RecognizerResultsIntent
416 .ACTION_VOICE_SEARCH_RESULTS.equals(action);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800417 if (Intent.ACTION_VIEW.equals(action)
418 || Intent.ACTION_SEARCH.equals(action)
419 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
Leon Scroggins58d56c62010-01-28 15:12:40 -0500420 || Intent.ACTION_WEB_SEARCH.equals(action)
421 || activateVoiceSearch) {
Leon Scroggins3b18ce32010-02-08 17:35:59 -0500422 if (current.isInVoiceSearchMode()) {
423 String title = current.getVoiceDisplayTitle();
424 if (title != null && title.equals(intent.getStringExtra(
425 SearchManager.QUERY))) {
426 // The user submitted the same search as the last voice
427 // search, so do nothing.
428 return;
429 }
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500430 if (Intent.ACTION_SEARCH.equals(action)
431 && current.voiceSearchSourceIsGoogle()) {
432 Intent logIntent = new Intent(
433 LoggingEvents.ACTION_LOG_EVENT);
434 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
435 LoggingEvents.VoiceSearch.QUERY_UPDATED);
436 logIntent.putExtra(
437 LoggingEvents.VoiceSearch.EXTRA_QUERY_UPDATED_VALUE,
438 intent.getDataString());
439 sendBroadcast(logIntent);
440 // Note, onPageStarted will revert the voice title bar
441 // When http://b/issue?id=2379215 is fixed, we should update
442 // the title bar here.
443 }
Leon Scroggins3b18ce32010-02-08 17:35:59 -0500444 }
Satish Sampath565505b2009-05-29 15:37:27 +0100445 // If this was a search request (e.g. search query directly typed into the address bar),
446 // pass it on to the default web search provider.
447 if (handleWebSearchIntent(intent)) {
448 return;
449 }
450
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700451 UrlData urlData = getUrlDataFromIntent(intent);
452 if (urlData.isEmpty()) {
453 urlData = new UrlData(mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800454 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700455
Grace Klobacc634032009-07-28 15:58:19 -0700456 final String appId = intent
457 .getStringExtra(Browser.EXTRA_APPLICATION_ID);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500458 if ((Intent.ACTION_VIEW.equals(action) || activateVoiceSearch)
Grace Klobacc634032009-07-28 15:58:19 -0700459 && !getPackageName().equals(appId)
460 && (flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700461 Tab appTab = mTabControl.getTabFromId(appId);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700462 if (appTab != null) {
463 Log.i(LOGTAG, "Reusing tab for " + appId);
464 // Dismiss the subwindow if applicable.
465 dismissSubWindow(appTab);
466 // Since we might kill the WebView, remove it from the
467 // content view first.
468 removeTabFromContentView(appTab);
469 // Recreate the main WebView after destroying the old one.
470 // If the WebView has the same original url and is on that
471 // page, it can be reused.
472 boolean needsLoad =
Leon Scroggins6eac63e2010-03-15 18:19:14 -0400473 mTabControl.recreateWebView(appTab, urlData);
Ben Murdochbff2d602009-07-01 20:19:05 +0100474
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700475 if (current != appTab) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400476 switchToTab(mTabControl.getTabIndex(appTab));
477 if (needsLoad) {
Patrick Scott9d53da02010-02-19 10:19:01 -0500478 loadUrlDataIn(appTab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400479 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700480 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400481 // If the tab was the current tab, we have to attach
482 // it to the view system again.
483 attachTabToContentView(appTab);
484 if (needsLoad) {
Patrick Scott9d53da02010-02-19 10:19:01 -0500485 loadUrlDataIn(appTab, urlData);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700486 }
487 }
488 return;
Patrick Scottcd115892009-07-16 09:42:58 -0400489 } else {
490 // No matching application tab, try to find a regular tab
491 // with a matching url.
492 appTab = mTabControl.findUnusedTabWithUrl(urlData.mUrl);
Leon Scroggins25515f82009-08-19 15:31:58 -0400493 if (appTab != null) {
494 if (current != appTab) {
495 switchToTab(mTabControl.getTabIndex(appTab));
496 }
497 // Otherwise, we are already viewing the correct tab.
Patrick Scottcd115892009-07-16 09:42:58 -0400498 } else {
499 // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
500 // will be opened in a new tab unless we have reached
501 // MAX_TABS. Then the url will be opened in the current
502 // tab. If a new tab is created, it will have "true" for
503 // exit on close.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400504 openTabAndShow(urlData, true, appId);
Patrick Scottcd115892009-07-16 09:42:58 -0400505 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700506 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800507 } else {
Grace Kloba638d3f42009-11-23 10:35:04 -0800508 if (!urlData.isEmpty()
509 && urlData.mUrl.startsWith("about:debug")) {
510 if ("about:debug.dom".equals(urlData.mUrl)) {
511 current.getWebView().dumpDomTree(false);
512 } else if ("about:debug.dom.file".equals(urlData.mUrl)) {
513 current.getWebView().dumpDomTree(true);
514 } else if ("about:debug.render".equals(urlData.mUrl)) {
515 current.getWebView().dumpRenderTree(false);
516 } else if ("about:debug.render.file".equals(urlData.mUrl)) {
517 current.getWebView().dumpRenderTree(true);
518 } else if ("about:debug.display".equals(urlData.mUrl)) {
519 current.getWebView().dumpDisplayTree();
Mike Reed9b78e1d2010-01-13 14:40:52 -0800520 } else if (urlData.mUrl.startsWith("about:debug.drag")) {
521 int index = urlData.mUrl.codePointAt(16) - '0';
522 if (index <= 0 || index > 9) {
523 current.getWebView().setDragTracker(null);
524 } else {
525 current.getWebView().setDragTracker(new MeshTracker(index));
526 }
Grace Kloba638d3f42009-11-23 10:35:04 -0800527 } else {
528 mSettings.toggleDebugSettings();
529 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800530 return;
531 }
Leon Scroggins1f005d32009-08-10 17:36:42 -0400532 // Get rid of the subwindow if it exists
533 dismissSubWindow(current);
Patrick Scott9d53da02010-02-19 10:19:01 -0500534 loadUrlDataIn(current, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800535 }
536 }
537 }
538
Satish Sampath565505b2009-05-29 15:37:27 +0100539 private int parseUrlShortcut(String url) {
540 if (url == null) return SHORTCUT_INVALID;
541
542 // FIXME: quick search, need to be customized by setting
543 if (url.length() > 2 && url.charAt(1) == ' ') {
544 switch (url.charAt(0)) {
545 case 'g': return SHORTCUT_GOOGLE_SEARCH;
546 case 'w': return SHORTCUT_WIKIPEDIA_SEARCH;
547 case 'd': return SHORTCUT_DICTIONARY_SEARCH;
548 case 'l': return SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH;
549 }
550 }
551 return SHORTCUT_INVALID;
552 }
553
554 /**
555 * Launches the default web search activity with the query parameters if the given intent's data
556 * are identified as plain search terms and not URLs/shortcuts.
557 * @return true if the intent was handled and web search activity was launched, false if not.
558 */
559 private boolean handleWebSearchIntent(Intent intent) {
560 if (intent == null) return false;
561
562 String url = null;
563 final String action = intent.getAction();
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500564 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS.equals(
565 action)) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500566 return false;
567 }
Satish Sampath565505b2009-05-29 15:37:27 +0100568 if (Intent.ACTION_VIEW.equals(action)) {
Grace Kloba1e705052009-09-29 13:13:36 -0700569 Uri data = intent.getData();
570 if (data != null) url = data.toString();
Satish Sampath565505b2009-05-29 15:37:27 +0100571 } else if (Intent.ACTION_SEARCH.equals(action)
572 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
573 || Intent.ACTION_WEB_SEARCH.equals(action)) {
574 url = intent.getStringExtra(SearchManager.QUERY);
575 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100576 return handleWebSearchRequest(url, intent.getBundleExtra(SearchManager.APP_DATA),
577 intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
Satish Sampath565505b2009-05-29 15:37:27 +0100578 }
579
580 /**
581 * Launches the default web search activity with the query parameters if the given url string
582 * was identified as plain search terms and not URL/shortcut.
583 * @return true if the request was handled and web search activity was launched, false if not.
584 */
Bjorn Bringert04851702009-09-22 10:36:01 +0100585 private boolean handleWebSearchRequest(String inUrl, Bundle appData, String extraData) {
Satish Sampath565505b2009-05-29 15:37:27 +0100586 if (inUrl == null) return false;
587
588 // In general, we shouldn't modify URL from Intent.
589 // But currently, we get the user-typed URL from search box as well.
590 String url = fixUrl(inUrl).trim();
591
592 // URLs and site specific search shortcuts are handled by the regular flow of control, so
593 // return early.
Dan Egnor5ee906c2009-11-18 12:11:49 -0800594 if (Patterns.WEB_URL.matcher(url).matches()
Satish Sampathbc5b9f32009-06-04 18:21:40 +0100595 || ACCEPTED_URI_SCHEMA.matcher(url).matches()
Satish Sampath565505b2009-05-29 15:37:27 +0100596 || parseUrlShortcut(url) != SHORTCUT_INVALID) {
597 return false;
598 }
599
600 Browser.updateVisitedHistory(mResolver, url, false);
601 Browser.addSearchUrl(mResolver, url);
602
603 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
604 intent.addCategory(Intent.CATEGORY_DEFAULT);
605 intent.putExtra(SearchManager.QUERY, url);
Satish Sampath15e9f2d2009-06-23 22:29:49 +0100606 if (appData != null) {
607 intent.putExtra(SearchManager.APP_DATA, appData);
608 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100609 if (extraData != null) {
610 intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
611 }
Grace Klobacc634032009-07-28 15:58:19 -0700612 intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName());
Satish Sampath565505b2009-05-29 15:37:27 +0100613 startActivity(intent);
614
615 return true;
616 }
617
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700618 private UrlData getUrlDataFromIntent(Intent intent) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500619 String url = "";
Grace Kloba068e48b2010-01-26 18:11:27 -0800620 Map<String, String> headers = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800621 if (intent != null) {
622 final String action = intent.getAction();
623 if (Intent.ACTION_VIEW.equals(action)) {
624 url = smartUrlFilter(intent.getData());
625 if (url != null && url.startsWith("content:")) {
626 /* Append mimetype so webview knows how to display */
627 String mimeType = intent.resolveType(getContentResolver());
628 if (mimeType != null) {
629 url += "?" + mimeType;
630 }
631 }
Grace Kloba068e48b2010-01-26 18:11:27 -0800632 if (url != null && url.startsWith("http")) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800633 final Bundle pairs = intent
634 .getBundleExtra(Browser.EXTRA_HEADERS);
Grace Kloba2d508ed2010-01-28 11:39:15 -0800635 if (pairs != null && !pairs.isEmpty()) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800636 Iterator<String> iter = pairs.keySet().iterator();
Grace Kloba068e48b2010-01-26 18:11:27 -0800637 headers = new HashMap<String, String>();
Grace Kloba00f54c52010-01-27 14:53:51 -0800638 while (iter.hasNext()) {
639 String key = iter.next();
640 headers.put(key, pairs.getString(key));
Grace Kloba068e48b2010-01-26 18:11:27 -0800641 }
642 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700643 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800644 } else if (Intent.ACTION_SEARCH.equals(action)
645 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
646 || Intent.ACTION_WEB_SEARCH.equals(action)) {
647 url = intent.getStringExtra(SearchManager.QUERY);
648 if (url != null) {
649 mLastEnteredUrl = url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800650 // In general, we shouldn't modify URL from Intent.
651 // But currently, we get the user-typed URL from search box as well.
652 url = fixUrl(url);
653 url = smartUrlFilter(url);
Leon Scrogginsda3e0302010-03-02 16:01:49 -0500654 Browser.updateVisitedHistory(mResolver, url, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800655 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
656 if (url.contains(searchSource)) {
657 String source = null;
658 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
659 if (appData != null) {
Bjorn Bringert10d1cca2010-02-10 14:22:12 +0000660 source = appData.getString(Search.SOURCE);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800661 }
662 if (TextUtils.isEmpty(source)) {
663 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
664 }
665 url = url.replace(searchSource, "&source=android-"+source+"&");
666 }
667 }
668 }
669 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500670 return new UrlData(url, headers, intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800671 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500672 /* package */ void showVoiceTitleBar(String title) {
673 mTitleBar.setInVoiceMode(true);
674 mFakeTitleBar.setInVoiceMode(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800675
Leon Scroggins58d56c62010-01-28 15:12:40 -0500676 mTitleBar.setDisplayTitle(title);
677 mFakeTitleBar.setDisplayTitle(title);
678 }
679 /* package */ void revertVoiceTitleBar() {
680 mTitleBar.setInVoiceMode(false);
681 mFakeTitleBar.setInVoiceMode(false);
682
Leon Scroggins003a5dd2010-03-10 12:13:14 -0500683 mTitleBar.setDisplayTitle(mUrl);
684 mFakeTitleBar.setDisplayTitle(mUrl);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500685 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800686 /* package */ static String fixUrl(String inUrl) {
Cary Clark652ff872009-09-10 13:34:44 -0400687 // FIXME: Converting the url to lower case
688 // duplicates functionality in smartUrlFilter().
689 // However, changing all current callers of fixUrl to
690 // call smartUrlFilter in addition may have unwanted
691 // consequences, and is deferred for now.
692 int colon = inUrl.indexOf(':');
693 boolean allLower = true;
694 for (int index = 0; index < colon; index++) {
695 char ch = inUrl.charAt(index);
696 if (!Character.isLetter(ch)) {
697 break;
698 }
699 allLower &= Character.isLowerCase(ch);
700 if (index == colon - 1 && !allLower) {
701 inUrl = inUrl.substring(0, colon).toLowerCase()
702 + inUrl.substring(colon);
703 }
704 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800705 if (inUrl.startsWith("http://") || inUrl.startsWith("https://"))
706 return inUrl;
707 if (inUrl.startsWith("http:") ||
708 inUrl.startsWith("https:")) {
709 if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) {
710 inUrl = inUrl.replaceFirst("/", "//");
711 } else inUrl = inUrl.replaceFirst(":", "://");
712 }
713 return inUrl;
714 }
715
Grace Kloba22ac16e2009-10-07 18:00:23 -0700716 @Override
717 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800718 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700719 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800720 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
721 }
722
723 if (!mActivityInPause) {
724 Log.e(LOGTAG, "BrowserActivity is already resumed.");
725 return;
726 }
727
Mike Reed7bfa63b2009-05-28 11:08:32 -0400728 mTabControl.resumeCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800729 mActivityInPause = false;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400730 resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800731
732 if (mWakeLock.isHeld()) {
733 mHandler.removeMessages(RELEASE_WAKELOCK);
734 mWakeLock.release();
735 }
736
The Android Open Source Project0c908882009-03-03 19:32:16 -0800737 registerReceiver(mNetworkStateIntentReceiver,
738 mNetworkStateChangedFilter);
739 WebView.enablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800740 }
741
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400742 /**
743 * Since the actual title bar is embedded in the WebView, and removing it
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400744 * would change its appearance, use a different TitleBar to show overlayed
745 * at the top of the screen, when the menu is open or the page is loading.
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400746 */
747 private TitleBar mFakeTitleBar;
748
749 /**
750 * Keeps track of whether the options menu is open. This is important in
751 * determining whether to show or hide the title bar overlay.
752 */
753 private boolean mOptionsMenuOpen;
754
755 /**
756 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
757 * of whether the configuration has changed. The first onMenuOpened call
758 * after a configuration change is simply a reopening of the same menu
759 * (i.e. mIconView did not change).
760 */
761 private boolean mConfigChanged;
762
763 /**
764 * Whether or not the options menu is in its smaller, icon menu form. When
765 * true, we want the title bar overlay to be up. When false, we do not.
766 * Only meaningful if mOptionsMenuOpen is true.
767 */
768 private boolean mIconView;
769
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400770 @Override
771 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400772 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
773 if (mOptionsMenuOpen) {
774 if (mConfigChanged) {
775 // We do not need to make any changes to the state of the
776 // title bar, since the only thing that happened was a
777 // change in orientation
778 mConfigChanged = false;
779 } else {
780 if (mIconView) {
781 // Switching the menu to expanded view, so hide the
782 // title bar.
783 hideFakeTitleBar();
784 mIconView = false;
785 } else {
786 // Switching the menu back to icon view, so show the
787 // title bar once again.
788 showFakeTitleBar();
789 mIconView = true;
790 }
791 }
792 } else {
793 // The options menu is closed, so open it, and show the title
794 showFakeTitleBar();
795 mOptionsMenuOpen = true;
796 mConfigChanged = false;
797 mIconView = true;
798 }
799 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400800 return true;
801 }
802
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400803 private void showFakeTitleBar() {
Patrick Scottf8de8ec2009-09-21 16:37:29 -0400804 final View decor = getWindow().peekDecorView();
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400805 if (mFakeTitleBar.getParent() == null && mActiveTabsPage == null
Patrick Scottf8de8ec2009-09-21 16:37:29 -0400806 && !mActivityInPause && decor != null
807 && decor.getWindowToken() != null) {
Cary Clarka0464552009-09-29 13:00:45 -0400808 Rect visRect = new Rect();
809 if (!mBrowserFrameLayout.getGlobalVisibleRect(visRect)) {
810 if (LOGD_ENABLED) {
811 Log.d(LOGTAG, "showFakeTitleBar visRect failed");
812 }
813 return;
814 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400815
816 WindowManager manager
817 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
818
819 // Add the title bar to the window manager so it can receive touches
820 // while the menu is up
821 WindowManager.LayoutParams params
822 = new WindowManager.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -0800823 ViewGroup.LayoutParams.MATCH_PARENT,
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400824 ViewGroup.LayoutParams.WRAP_CONTENT,
825 WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL,
826 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
Leon Scroggins68549862009-09-21 16:02:01 -0400827 PixelFormat.TRANSLUCENT);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400828 params.gravity = Gravity.TOP;
Leon Scrogginsa27ff192009-09-14 12:58:04 -0400829 WebView mainView = mTabControl.getCurrentWebView();
Leon Scroggins68549862009-09-21 16:02:01 -0400830 boolean atTop = mainView != null && mainView.getScrollY() == 0;
Leon Scroggins83932c72009-09-30 11:55:54 -0400831 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400832 // XXX : Without providing an offset, the fake title bar will be
833 // placed underneath the status bar. Use the global visible rect
834 // of mBrowserFrameLayout to determine the bottom of the status bar
Cary Clarka0464552009-09-29 13:00:45 -0400835 params.y = visRect.top;
Grace Kloba13f9ace2010-03-18 21:44:14 -0700836 manager.addView(mFakeTitleBar, params);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400837 }
838 }
839
840 @Override
841 public void onOptionsMenuClosed(Menu menu) {
842 mOptionsMenuOpen = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -0400843 if (!mInLoad) {
844 hideFakeTitleBar();
845 } else if (!mIconView) {
846 // The page is currently loading, and we are in expanded mode, so
847 // we were not showing the menu. Show it once again. It will be
848 // removed when the page finishes.
849 showFakeTitleBar();
850 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400851 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700852
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400853 private void hideFakeTitleBar() {
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400854 if (mFakeTitleBar.getParent() == null) return;
Leon Scroggins20329572009-09-23 17:42:41 -0400855 WindowManager.LayoutParams params = (WindowManager.LayoutParams)
Grace Kloba13f9ace2010-03-18 21:44:14 -0700856 mFakeTitleBar.getLayoutParams();
Leon Scroggins20329572009-09-23 17:42:41 -0400857 WebView mainView = mTabControl.getCurrentWebView();
858 // Although we decided whether or not to animate based on the current
859 // scroll position, the scroll position may have changed since the
860 // fake title bar was displayed. Make sure it has the appropriate
861 // animation/lack thereof before removing.
862 params.windowAnimations = mainView != null && mainView.getScrollY() == 0
Leon Scroggins83932c72009-09-30 11:55:54 -0400863 ? 0 : R.style.TitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400864 WindowManager manager
865 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Grace Kloba13f9ace2010-03-18 21:44:14 -0700866 manager.updateViewLayout(mFakeTitleBar, params);
867 manager.removeView(mFakeTitleBar);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400868 }
869
The Android Open Source Project0c908882009-03-03 19:32:16 -0800870 /**
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400871 * Special method for the fake title bar to call when displaying its context
872 * menu, since it is in its own Window, and its parent does not show a
873 * context menu.
874 */
875 /* package */ void showTitleBarContextMenu() {
Cary Clark65f4a3c2009-09-28 17:05:06 -0400876 if (null == mTitleBar.getParent()) {
877 return;
878 }
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400879 openContextMenu(mTitleBar);
880 }
881
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400882 @Override
883 public void onContextMenuClosed(Menu menu) {
884 super.onContextMenuClosed(menu);
885 if (mInLoad) {
886 showFakeTitleBar();
887 }
888 }
889
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400890 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800891 * onSaveInstanceState(Bundle map)
892 * onSaveInstanceState is called right before onStop(). The map contains
893 * the saved state.
894 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700895 @Override
896 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700897 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800898 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
899 }
900 // the default implementation requires each view to have an id. As the
901 // browser handles the state itself and it doesn't use id for the views,
902 // don't call the default implementation. Otherwise it will trigger the
903 // warning like this, "couldn't save which view has focus because the
904 // focused view XXX has no id".
905
906 // Save all the tabs
907 mTabControl.saveState(outState);
908 }
909
Grace Kloba22ac16e2009-10-07 18:00:23 -0700910 @Override
911 protected void onPause() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800912 super.onPause();
913
914 if (mActivityInPause) {
915 Log.e(LOGTAG, "BrowserActivity is already paused.");
916 return;
917 }
918
Mike Reed7bfa63b2009-05-28 11:08:32 -0400919 mTabControl.pauseCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800920 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400921 if (mTabControl.getCurrentIndex() >= 0 && !pauseWebViewTimers()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800922 mWakeLock.acquire();
923 mHandler.sendMessageDelayed(mHandler
924 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
925 }
926
Leon Scrogginsa2ab6a72009-09-11 11:49:52 -0400927 // FIXME: This removes the active tabs page and resets the menu to
928 // MAIN_MENU. A better solution might be to do this work in onNewIntent
929 // but then we would need to save it in onSaveInstanceState and restore
930 // it in onCreate/onRestoreInstanceState
931 if (mActiveTabsPage != null) {
932 removeActiveTabPage(true);
933 }
934
The Android Open Source Project0c908882009-03-03 19:32:16 -0800935 cancelStopToast();
936
937 // unregister network state listener
938 unregisterReceiver(mNetworkStateIntentReceiver);
939 WebView.disablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800940 }
941
Grace Kloba22ac16e2009-10-07 18:00:23 -0700942 @Override
943 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700944 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800945 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
946 }
947 super.onDestroy();
Grace Kloba0923d692009-09-23 21:37:25 -0700948
Leon Scroggins8d5fa432009-10-02 15:55:59 -0400949 if (mUploadMessage != null) {
950 mUploadMessage.onReceiveValue(null);
951 mUploadMessage = null;
952 }
953
Grace Kloba0923d692009-09-23 21:37:25 -0700954 if (mTabControl == null) return;
955
Grace Kloba1fc98a32009-10-21 13:23:08 -0700956 // Remove the fake title bar if it is there
957 hideFakeTitleBar();
958
The Android Open Source Project0c908882009-03-03 19:32:16 -0800959 // Remove the current tab and sub window
Grace Kloba22ac16e2009-10-07 18:00:23 -0700960 Tab t = mTabControl.getCurrentTab();
Patrick Scottfb5e77f2009-04-08 19:17:37 -0700961 if (t != null) {
962 dismissSubWindow(t);
963 removeTabFromContentView(t);
964 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800965 // Destroy all the tabs
966 mTabControl.destroy();
967 WebIconDatabase.getInstance().close();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800968
Grace Klobab4da0ad2009-05-14 14:45:40 -0700969 unregisterReceiver(mPackageInstallationReceiver);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800970 }
971
972 @Override
973 public void onConfigurationChanged(Configuration newConfig) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400974 mConfigChanged = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800975 super.onConfigurationChanged(newConfig);
976
977 if (mPageInfoDialog != null) {
978 mPageInfoDialog.dismiss();
979 showPageInfo(
980 mPageInfoView,
Leon Scrogginsc7b92f82010-01-11 18:17:31 -0500981 mPageInfoFromShowSSLCertificateOnError);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800982 }
983 if (mSSLCertificateDialog != null) {
984 mSSLCertificateDialog.dismiss();
985 showSSLCertificate(
986 mSSLCertificateView);
987 }
988 if (mSSLCertificateOnErrorDialog != null) {
989 mSSLCertificateOnErrorDialog.dismiss();
990 showSSLCertificateOnError(
991 mSSLCertificateOnErrorView,
992 mSSLCertificateOnErrorHandler,
993 mSSLCertificateOnErrorError);
994 }
995 if (mHttpAuthenticationDialog != null) {
996 String title = ((TextView) mHttpAuthenticationDialog
997 .findViewById(com.android.internal.R.id.alertTitle)).getText()
998 .toString();
999 String name = ((TextView) mHttpAuthenticationDialog
1000 .findViewById(R.id.username_edit)).getText().toString();
1001 String password = ((TextView) mHttpAuthenticationDialog
1002 .findViewById(R.id.password_edit)).getText().toString();
1003 int focusId = mHttpAuthenticationDialog.getCurrentFocus()
1004 .getId();
1005 mHttpAuthenticationDialog.dismiss();
1006 showHttpAuthentication(mHttpAuthHandler, null, null, title,
1007 name, password, focusId);
1008 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001009 }
1010
Grace Kloba22ac16e2009-10-07 18:00:23 -07001011 @Override
1012 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001013 super.onLowMemory();
1014 mTabControl.freeMemory();
1015 }
1016
Mike Reed7bfa63b2009-05-28 11:08:32 -04001017 private boolean resumeWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001018 Tab tab = mTabControl.getCurrentTab();
1019 boolean inLoad = tab.inLoad();
1020 if ((!mActivityInPause && !inLoad) || (mActivityInPause && inLoad)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001021 CookieSyncManager.getInstance().startSync();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001022 WebView w = tab.getWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001023 if (w != null) {
1024 w.resumeTimers();
1025 }
1026 return true;
1027 } else {
1028 return false;
1029 }
1030 }
1031
Mike Reed7bfa63b2009-05-28 11:08:32 -04001032 private boolean pauseWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001033 Tab tab = mTabControl.getCurrentTab();
1034 boolean inLoad = tab.inLoad();
1035 if (mActivityInPause && !inLoad) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001036 CookieSyncManager.getInstance().stopSync();
1037 WebView w = mTabControl.getCurrentWebView();
1038 if (w != null) {
1039 w.pauseTimers();
1040 }
1041 return true;
1042 } else {
1043 return false;
1044 }
1045 }
1046
The Android Open Source Project0c908882009-03-03 19:32:16 -08001047 // Open the icon database and retain all the icons for visited sites.
1048 private void retainIconsOnStartup() {
1049 final WebIconDatabase db = WebIconDatabase.getInstance();
1050 db.open(getDir("icons", 0).getPath());
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001051 Cursor c = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001052 try {
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001053 c = Browser.getAllBookmarks(mResolver);
1054 if (c.moveToFirst()) {
1055 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
1056 do {
1057 String url = c.getString(urlIndex);
1058 db.retainIconForPageUrl(url);
1059 } while (c.moveToNext());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001060 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001061 } catch (IllegalStateException e) {
1062 Log.e(LOGTAG, "retainIconsOnStartup", e);
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001063 } finally {
1064 if (c!= null) c.close();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001065 }
1066 }
1067
1068 // Helper method for getting the top window.
1069 WebView getTopWindow() {
1070 return mTabControl.getCurrentTopWebView();
1071 }
1072
Grace Kloba22ac16e2009-10-07 18:00:23 -07001073 TabControl getTabControl() {
1074 return mTabControl;
1075 }
1076
The Android Open Source Project0c908882009-03-03 19:32:16 -08001077 @Override
1078 public boolean onCreateOptionsMenu(Menu menu) {
1079 super.onCreateOptionsMenu(menu);
1080
1081 MenuInflater inflater = getMenuInflater();
1082 inflater.inflate(R.menu.browser, menu);
1083 mMenu = menu;
1084 updateInLoadMenuItems();
1085 return true;
1086 }
1087
1088 /**
1089 * As the menu can be open when loading state changes
1090 * we must manually update the state of the stop/reload menu
1091 * item
1092 */
1093 private void updateInLoadMenuItems() {
1094 if (mMenu == null) {
1095 return;
1096 }
1097 MenuItem src = mInLoad ?
1098 mMenu.findItem(R.id.stop_menu_id):
1099 mMenu.findItem(R.id.reload_menu_id);
1100 MenuItem dest = mMenu.findItem(R.id.stop_reload_menu_id);
1101 dest.setIcon(src.getIcon());
1102 dest.setTitle(src.getTitle());
1103 }
1104
1105 @Override
1106 public boolean onContextItemSelected(MenuItem item) {
1107 // chording is not an issue with context menus, but we use the same
1108 // options selector, so set mCanChord to true so we can access them.
1109 mCanChord = true;
1110 int id = item.getItemId();
Leon Scroggins96afcb12009-12-10 12:35:56 -05001111 boolean result = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001112 switch (id) {
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001113 // For the context menu from the title bar
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001114 case R.id.title_bar_copy_page_url:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001115 Tab currentTab = mTabControl.getCurrentTab();
1116 if (null == currentTab) {
1117 result = false;
1118 break;
1119 }
1120 WebView mainView = currentTab.getWebView();
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001121 if (null == mainView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001122 result = false;
1123 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001124 }
Leon Scroggins96afcb12009-12-10 12:35:56 -05001125 copy(mainView.getUrl());
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001126 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001127 // -- Browser context menu
1128 case R.id.open_context_menu_id:
1129 case R.id.open_newtab_context_menu_id:
1130 case R.id.bookmark_context_menu_id:
1131 case R.id.save_link_context_menu_id:
1132 case R.id.share_link_context_menu_id:
1133 case R.id.copy_link_context_menu_id:
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001134 final WebView webView = getTopWindow();
1135 if (null == webView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001136 result = false;
1137 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001138 }
1139 final HashMap hrefMap = new HashMap();
1140 hrefMap.put("webview", webView);
1141 final Message msg = mHandler.obtainMessage(
1142 FOCUS_NODE_HREF, id, 0, hrefMap);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001143 webView.requestFocusNodeHref(msg);
1144 break;
1145
1146 default:
1147 // For other context menus
Leon Scroggins96afcb12009-12-10 12:35:56 -05001148 result = onOptionsItemSelected(item);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001149 }
1150 mCanChord = false;
Leon Scroggins96afcb12009-12-10 12:35:56 -05001151 return result;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001152 }
1153
1154 private Bundle createGoogleSearchSourceBundle(String source) {
1155 Bundle bundle = new Bundle();
Bjorn Bringert10d1cca2010-02-10 14:22:12 +00001156 bundle.putString(Search.SOURCE, source);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001157 return bundle;
1158 }
1159
Leon Scroggins8ad29922010-02-16 12:33:55 -05001160 /* package */ void editUrl() {
Leon Scroggins68579392009-09-15 15:31:54 -04001161 if (mOptionsMenuOpen) closeOptionsMenu();
Leon Scroggins5bbe9802009-07-31 13:10:55 -04001162 String url = (getTopWindow() == null) ? null : getTopWindow().getUrl();
Grace Kloba83f47342009-07-20 10:44:31 -07001163 startSearch(mSettings.getHomePage().equals(url) ? null : url, true,
Leon Scroggins8ad29922010-02-16 12:33:55 -05001164 null, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001165 }
1166
Leon Scroggins8ad29922010-02-16 12:33:55 -05001167 /**
1168 * Overriding this to insert a local information bundle
1169 */
The Android Open Source Project0c908882009-03-03 19:32:16 -08001170 @Override
1171 public void startSearch(String initialQuery, boolean selectInitialQuery,
1172 Bundle appSearchData, boolean globalSearch) {
1173 if (appSearchData == null) {
1174 appSearchData = createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_TYPE);
1175 }
1176 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1177 }
1178
Leon Scroggins1f005d32009-08-10 17:36:42 -04001179 /**
1180 * Switch tabs. Called by the TitleBarSet when sliding the title bar
1181 * results in changing tabs.
Leon Scroggins160a7e72009-08-14 18:28:01 -04001182 * @param index Index of the tab to change to, as defined by
1183 * mTabControl.getTabIndex(Tab t).
1184 * @return boolean True if we successfully switched to a different tab. If
1185 * the indexth tab is null, or if that tab is the same as
1186 * the current one, return false.
Leon Scroggins1f005d32009-08-10 17:36:42 -04001187 */
Leon Scroggins160a7e72009-08-14 18:28:01 -04001188 /* package */ boolean switchToTab(int index) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001189 Tab tab = mTabControl.getTab(index);
1190 Tab currentTab = mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001191 if (tab == null || tab == currentTab) {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001192 return false;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001193 }
1194 if (currentTab != null) {
1195 // currentTab may be null if it was just removed. In that case,
1196 // we do not need to remove it
1197 removeTabFromContentView(currentTab);
1198 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001199 mTabControl.setCurrentTab(tab);
1200 attachTabToContentView(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001201 resetTitleIconAndProgress();
1202 updateLockIconToLatest();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001203 return true;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001204 }
1205
Grace Kloba22ac16e2009-10-07 18:00:23 -07001206 /* package */ Tab openTabToHomePage() {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001207 return openTabAndShow(mSettings.getHomePage(), false, null);
1208 }
1209
Leon Scroggins1f005d32009-08-10 17:36:42 -04001210 /* package */ void closeCurrentWindow() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001211 final Tab current = mTabControl.getCurrentTab();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001212 if (mTabControl.getTabCount() == 1) {
Leon Scroggins30444232009-09-04 18:36:20 -04001213 // This is the last tab. Open a new one, with the home
1214 // page and close the current one.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001215 openTabToHomePage();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001216 closeTab(current);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001217 return;
1218 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001219 final Tab parent = current.getParentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001220 int indexToShow = -1;
1221 if (parent != null) {
1222 indexToShow = mTabControl.getTabIndex(parent);
1223 } else {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001224 final int currentIndex = mTabControl.getCurrentIndex();
1225 // Try to move to the tab to the right
1226 indexToShow = currentIndex + 1;
1227 if (indexToShow > mTabControl.getTabCount() - 1) {
1228 // Try to move to the tab to the left
1229 indexToShow = currentIndex - 1;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001230 }
1231 }
Leon Scroggins160a7e72009-08-14 18:28:01 -04001232 if (switchToTab(indexToShow)) {
1233 // Close window
1234 closeTab(current);
1235 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001236 }
1237
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001238 private ActiveTabsPage mActiveTabsPage;
1239
1240 /**
1241 * Remove the active tabs page.
1242 * @param needToAttach If true, the active tabs page did not attach a tab
1243 * to the content view, so we need to do that here.
1244 */
1245 /* package */ void removeActiveTabPage(boolean needToAttach) {
1246 mContentView.removeView(mActiveTabsPage);
1247 mActiveTabsPage = null;
1248 mMenuState = R.id.MAIN_MENU;
1249 if (needToAttach) {
1250 attachTabToContentView(mTabControl.getCurrentTab());
1251 }
1252 getTopWindow().requestFocus();
1253 }
1254
The Android Open Source Project0c908882009-03-03 19:32:16 -08001255 @Override
1256 public boolean onOptionsItemSelected(MenuItem item) {
1257 if (!mCanChord) {
1258 // The user has already fired a shortcut with this hold down of the
1259 // menu key.
1260 return false;
1261 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001262 if (null == getTopWindow()) {
Leon Scroggins0d7ae0e2009-06-05 11:04:45 -04001263 return false;
1264 }
Grace Kloba6ee9c492009-07-13 10:04:34 -07001265 if (mMenuIsDown) {
1266 // The shortcut action consumes the MENU. Even if it is still down,
1267 // it won't trigger the next shortcut action. In the case of the
1268 // shortcut action triggering a new activity, like Bookmarks, we
1269 // won't get onKeyUp for MENU. So it is important to reset it here.
1270 mMenuIsDown = false;
1271 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001272 switch (item.getItemId()) {
1273 // -- Main menu
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001274 case R.id.new_tab_menu_id:
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001275 openTabToHomePage();
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001276 break;
1277
Leon Scroggins64b80f32009-08-07 12:03:34 -04001278 case R.id.goto_menu_id:
Leon Scroggins8ad29922010-02-16 12:33:55 -05001279 editUrl();
Leon Scrogginsb3a5bed2009-09-28 11:21:56 -04001280 break;
1281
1282 case R.id.bookmarks_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001283 bookmarksOrHistoryPicker(false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001284 break;
1285
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001286 case R.id.active_tabs_menu_id:
1287 mActiveTabsPage = new ActiveTabsPage(this, mTabControl);
1288 removeTabFromContentView(mTabControl.getCurrentTab());
Leon Scroggins43de6162009-09-14 19:59:58 -04001289 hideFakeTitleBar();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001290 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
1291 mActiveTabsPage.requestFocus();
1292 mMenuState = EMPTY_MENU;
1293 break;
1294
Leon Scroggins1f005d32009-08-10 17:36:42 -04001295 case R.id.add_bookmark_menu_id:
1296 Intent i = new Intent(BrowserActivity.this,
1297 AddBookmarkPage.class);
1298 WebView w = getTopWindow();
1299 i.putExtra("url", w.getUrl());
1300 i.putExtra("title", w.getTitle());
Grace Kloba83cdb2c2009-09-16 00:48:57 -07001301 i.putExtra("touch_icon_url", w.getTouchIconUrl());
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01001302 i.putExtra("thumbnail", createScreenshot(w));
Leon Scroggins1f005d32009-08-10 17:36:42 -04001303 startActivity(i);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001304 break;
1305
1306 case R.id.stop_reload_menu_id:
1307 if (mInLoad) {
1308 stopLoading();
1309 } else {
1310 getTopWindow().reload();
1311 }
1312 break;
1313
1314 case R.id.back_menu_id:
1315 getTopWindow().goBack();
1316 break;
1317
1318 case R.id.forward_menu_id:
1319 getTopWindow().goForward();
1320 break;
1321
1322 case R.id.close_menu_id:
1323 // Close the subwindow if it exists.
1324 if (mTabControl.getCurrentSubWindow() != null) {
1325 dismissSubWindow(mTabControl.getCurrentTab());
1326 break;
1327 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001328 closeCurrentWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001329 break;
1330
1331 case R.id.homepage_menu_id:
Grace Kloba22ac16e2009-10-07 18:00:23 -07001332 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001333 if (current != null) {
1334 dismissSubWindow(current);
Leon Scroggins92472e82010-02-17 16:32:28 -05001335 loadUrl(current.getWebView(), mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001336 }
1337 break;
1338
1339 case R.id.preferences_menu_id:
1340 Intent intent = new Intent(this,
1341 BrowserPreferencesPage.class);
Leon Scrogginsd5304942009-12-10 16:11:39 -05001342 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1343 getTopWindow().getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001344 startActivityForResult(intent, PREFERENCES_PAGE);
1345 break;
1346
1347 case R.id.find_menu_id:
1348 if (null == mFindDialog) {
1349 mFindDialog = new FindDialog(this);
1350 }
1351 mFindDialog.setWebView(getTopWindow());
1352 mFindDialog.show();
Cary Clark7d3ac792010-03-03 10:11:44 -05001353 getTopWindow().setFindIsUp(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001354 mMenuState = EMPTY_MENU;
1355 break;
1356
1357 case R.id.select_text_id:
1358 getTopWindow().emulateShiftHeld();
1359 break;
1360 case R.id.page_info_menu_id:
1361 showPageInfo(mTabControl.getCurrentTab(), false);
1362 break;
1363
1364 case R.id.classic_history_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001365 bookmarksOrHistoryPicker(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001366 break;
1367
Leon Scroggins96afcb12009-12-10 12:35:56 -05001368 case R.id.title_bar_share_page_url:
The Android Open Source Project0c908882009-03-03 19:32:16 -08001369 case R.id.share_page_menu_id:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001370 Tab currentTab = mTabControl.getCurrentTab();
1371 if (null == currentTab) {
1372 mCanChord = false;
1373 return false;
1374 }
1375 currentTab.populatePickerData();
1376 sharePage(this, currentTab.getTitle(),
1377 currentTab.getUrl(), currentTab.getFavicon(),
1378 createScreenshot(currentTab.getWebView()));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001379 break;
1380
1381 case R.id.dump_nav_menu_id:
1382 getTopWindow().debugDump();
1383 break;
1384
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001385 case R.id.dump_counters_menu_id:
1386 getTopWindow().dumpV8Counters();
1387 break;
1388
The Android Open Source Project0c908882009-03-03 19:32:16 -08001389 case R.id.zoom_in_menu_id:
1390 getTopWindow().zoomIn();
1391 break;
1392
1393 case R.id.zoom_out_menu_id:
1394 getTopWindow().zoomOut();
1395 break;
1396
1397 case R.id.view_downloads_menu_id:
1398 viewDownloads(null);
1399 break;
1400
The Android Open Source Project0c908882009-03-03 19:32:16 -08001401 case R.id.window_one_menu_id:
1402 case R.id.window_two_menu_id:
1403 case R.id.window_three_menu_id:
1404 case R.id.window_four_menu_id:
1405 case R.id.window_five_menu_id:
1406 case R.id.window_six_menu_id:
1407 case R.id.window_seven_menu_id:
1408 case R.id.window_eight_menu_id:
1409 {
1410 int menuid = item.getItemId();
1411 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1412 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001413 Tab desiredTab = mTabControl.getTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001414 if (desiredTab != null &&
1415 desiredTab != mTabControl.getCurrentTab()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001416 switchToTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001417 }
1418 break;
1419 }
1420 }
1421 }
1422 break;
1423
1424 default:
1425 if (!super.onOptionsItemSelected(item)) {
1426 return false;
1427 }
1428 // Otherwise fall through.
1429 }
1430 mCanChord = false;
1431 return true;
1432 }
1433
1434 public void closeFind() {
1435 mMenuState = R.id.MAIN_MENU;
1436 }
1437
Grace Kloba22ac16e2009-10-07 18:00:23 -07001438 @Override
1439 public boolean onPrepareOptionsMenu(Menu menu) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001440 // This happens when the user begins to hold down the menu key, so
1441 // allow them to chord to get a shortcut.
1442 mCanChord = true;
1443 // Note: setVisible will decide whether an item is visible; while
1444 // setEnabled() will decide whether an item is enabled, which also means
1445 // whether the matching shortcut key will function.
1446 super.onPrepareOptionsMenu(menu);
1447 switch (mMenuState) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001448 case EMPTY_MENU:
1449 if (mCurrentMenuState != mMenuState) {
1450 menu.setGroupVisible(R.id.MAIN_MENU, false);
1451 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1452 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001453 }
1454 break;
1455 default:
1456 if (mCurrentMenuState != mMenuState) {
1457 menu.setGroupVisible(R.id.MAIN_MENU, true);
1458 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1459 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001460 }
1461 final WebView w = getTopWindow();
1462 boolean canGoBack = false;
1463 boolean canGoForward = false;
1464 boolean isHome = false;
1465 if (w != null) {
1466 canGoBack = w.canGoBack();
1467 canGoForward = w.canGoForward();
1468 isHome = mSettings.getHomePage().equals(w.getUrl());
1469 }
1470 final MenuItem back = menu.findItem(R.id.back_menu_id);
1471 back.setEnabled(canGoBack);
1472
1473 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1474 home.setEnabled(!isHome);
1475
1476 menu.findItem(R.id.forward_menu_id)
1477 .setEnabled(canGoForward);
1478
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001479 menu.findItem(R.id.new_tab_menu_id).setEnabled(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001480 mTabControl.canCreateNewTab());
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001481
The Android Open Source Project0c908882009-03-03 19:32:16 -08001482 // decide whether to show the share link option
1483 PackageManager pm = getPackageManager();
1484 Intent send = new Intent(Intent.ACTION_SEND);
1485 send.setType("text/plain");
1486 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1487 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1488
The Android Open Source Project0c908882009-03-03 19:32:16 -08001489 boolean isNavDump = mSettings.isNavDump();
1490 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1491 nav.setVisible(isNavDump);
1492 nav.setEnabled(isNavDump);
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001493
1494 boolean showDebugSettings = mSettings.showDebugSettings();
1495 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1496 counter.setVisible(showDebugSettings);
1497 counter.setEnabled(showDebugSettings);
1498
The Android Open Source Project0c908882009-03-03 19:32:16 -08001499 break;
1500 }
1501 mCurrentMenuState = mMenuState;
1502 return true;
1503 }
1504
1505 @Override
1506 public void onCreateContextMenu(ContextMenu menu, View v,
1507 ContextMenuInfo menuInfo) {
Leon Scroggins4e9f89b2010-02-22 16:54:14 -05001508 if (v instanceof TitleBar) {
1509 return;
1510 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001511 WebView webview = (WebView) v;
1512 WebView.HitTestResult result = webview.getHitTestResult();
1513 if (result == null) {
1514 return;
1515 }
1516
1517 int type = result.getType();
1518 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1519 Log.w(LOGTAG,
1520 "We should not show context menu when nothing is touched");
1521 return;
1522 }
1523 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1524 // let TextView handles context menu
1525 return;
1526 }
1527
1528 // Note, http://b/issue?id=1106666 is requesting that
1529 // an inflated menu can be used again. This is not available
1530 // yet, so inflate each time (yuk!)
1531 MenuInflater inflater = getMenuInflater();
1532 inflater.inflate(R.menu.browsercontext, menu);
1533
1534 // Show the correct menu group
1535 String extra = result.getExtra();
1536 menu.setGroupVisible(R.id.PHONE_MENU,
1537 type == WebView.HitTestResult.PHONE_TYPE);
1538 menu.setGroupVisible(R.id.EMAIL_MENU,
1539 type == WebView.HitTestResult.EMAIL_TYPE);
1540 menu.setGroupVisible(R.id.GEO_MENU,
1541 type == WebView.HitTestResult.GEO_TYPE);
1542 menu.setGroupVisible(R.id.IMAGE_MENU,
1543 type == WebView.HitTestResult.IMAGE_TYPE
1544 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1545 menu.setGroupVisible(R.id.ANCHOR_MENU,
1546 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1547 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1548
1549 // Setup custom handling depending on the type
1550 switch (type) {
1551 case WebView.HitTestResult.PHONE_TYPE:
1552 menu.setHeaderTitle(Uri.decode(extra));
1553 menu.findItem(R.id.dial_context_menu_id).setIntent(
1554 new Intent(Intent.ACTION_VIEW, Uri
1555 .parse(WebView.SCHEME_TEL + extra)));
1556 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1557 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
Cary Clark5e335a32009-09-22 14:53:11 -04001558 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001559 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1560 addIntent);
1561 menu.findItem(R.id.copy_phone_context_menu_id).setOnMenuItemClickListener(
1562 new Copy(extra));
1563 break;
1564
1565 case WebView.HitTestResult.EMAIL_TYPE:
1566 menu.setHeaderTitle(extra);
1567 menu.findItem(R.id.email_context_menu_id).setIntent(
1568 new Intent(Intent.ACTION_VIEW, Uri
1569 .parse(WebView.SCHEME_MAILTO + extra)));
1570 menu.findItem(R.id.copy_mail_context_menu_id).setOnMenuItemClickListener(
1571 new Copy(extra));
1572 break;
1573
1574 case WebView.HitTestResult.GEO_TYPE:
1575 menu.setHeaderTitle(extra);
1576 menu.findItem(R.id.map_context_menu_id).setIntent(
1577 new Intent(Intent.ACTION_VIEW, Uri
1578 .parse(WebView.SCHEME_GEO
1579 + URLEncoder.encode(extra))));
1580 menu.findItem(R.id.copy_geo_context_menu_id).setOnMenuItemClickListener(
1581 new Copy(extra));
1582 break;
1583
1584 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1585 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
1586 TextView titleView = (TextView) LayoutInflater.from(this)
1587 .inflate(android.R.layout.browser_link_context_header,
1588 null);
1589 titleView.setText(extra);
1590 menu.setHeaderView(titleView);
1591 // decide whether to show the open link in new tab option
1592 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001593 mTabControl.canCreateNewTab());
Ben Murdochde353622009-10-12 10:29:00 +01001594 menu.findItem(R.id.bookmark_context_menu_id).setVisible(
1595 Bookmarks.urlHasAcceptableScheme(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001596 PackageManager pm = getPackageManager();
1597 Intent send = new Intent(Intent.ACTION_SEND);
1598 send.setType("text/plain");
1599 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1600 menu.findItem(R.id.share_link_context_menu_id).setVisible(ri != null);
1601 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1602 break;
1603 }
1604 // otherwise fall through to handle image part
1605 case WebView.HitTestResult.IMAGE_TYPE:
1606 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1607 menu.setHeaderTitle(extra);
1608 }
1609 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1610 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1611 menu.findItem(R.id.download_context_menu_id).
1612 setOnMenuItemClickListener(new Download(extra));
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001613 menu.findItem(R.id.set_wallpaper_context_menu_id).
1614 setOnMenuItemClickListener(new SetAsWallpaper(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001615 break;
1616
1617 default:
1618 Log.w(LOGTAG, "We should not get here.");
1619 break;
1620 }
Leon Scrogginsb2b19f52009-10-09 16:10:00 -04001621 hideFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001622 }
1623
The Android Open Source Project0c908882009-03-03 19:32:16 -08001624 // Attach the given tab to the content view.
Grace Klobac928c302009-09-17 11:51:21 -07001625 // this should only be called for the current tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001626 private void attachTabToContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001627 // Attach the container that contains the main WebView and any other UI
1628 // associated with the tab.
Patrick Scottd0119532009-09-17 08:00:31 -04001629 t.attachTabToContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001630
1631 if (mShouldShowErrorConsole) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001632 ErrorConsoleView errorConsole = t.getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01001633 if (errorConsole.numberOfErrors() == 0) {
1634 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
1635 } else {
1636 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1637 }
1638
1639 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08001640 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01001641 ViewGroup.LayoutParams.WRAP_CONTENT));
1642 }
1643
Leon Scroggins39ab28e2009-09-02 21:20:30 -04001644 WebView view = t.getWebView();
Leon Scroggins55a5bc22009-09-04 17:00:08 -04001645 view.setEmbeddedTitleBar(mTitleBar);
Leon Scroggins58d56c62010-01-28 15:12:40 -05001646 if (t.isInVoiceSearchMode()) {
1647 showVoiceTitleBar(t.getVoiceDisplayTitle());
1648 } else {
1649 revertVoiceTitleBar();
1650 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001651 // Request focus on the top window.
1652 t.getTopWindow().requestFocus();
1653 }
1654
1655 // Attach a sub window to the main WebView of the given tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001656 void attachSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001657 t.attachSubWindow(mContentView);
1658 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001659 }
1660
1661 // Remove the given tab from the content view.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001662 private void removeTabFromContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001663 // Remove the container that contains the main WebView.
Patrick Scottd0119532009-09-17 08:00:31 -04001664 t.removeTabFromContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001665
Grace Kloba22ac16e2009-10-07 18:00:23 -07001666 ErrorConsoleView errorConsole = t.getErrorConsole(false);
1667 if (errorConsole != null) {
1668 mErrorConsoleContainer.removeView(errorConsole);
Ben Murdochbff2d602009-07-01 20:19:05 +01001669 }
1670
Leon Scroggins39ab28e2009-09-02 21:20:30 -04001671 WebView view = t.getWebView();
Leon Scrogginsbb85b902009-09-14 19:27:20 -04001672 if (view != null) {
1673 view.setEmbeddedTitleBar(null);
1674 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001675 }
1676
1677 // Remove the sub window if it exists. Also called by TabControl when the
1678 // user clicks the 'X' to dismiss a sub window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001679 /* package */ void dismissSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001680 t.removeSubWindow(mContentView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001681 // dismiss the subwindow. This will destroy the WebView.
1682 t.dismissSubWindow();
Patrick Scottd0119532009-09-17 08:00:31 -04001683 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001684 }
1685
Leon Scroggins1f005d32009-08-10 17:36:42 -04001686 // A wrapper function of {@link #openTabAndShow(UrlData, boolean, String)}
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07001687 // that accepts url as string.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001688 private Tab openTabAndShow(String url, boolean closeOnExit, String appId) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001689 return openTabAndShow(new UrlData(url), closeOnExit, appId);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001690 }
1691
1692 // This method does a ton of stuff. It will attempt to create a new tab
1693 // if we haven't reached MAX_TABS. Otherwise it uses the current tab. If
Leon Scroggins1f005d32009-08-10 17:36:42 -04001694 // url isn't null, it will load the given url.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001695 /* package */Tab openTabAndShow(UrlData urlData, boolean closeOnExit,
1696 String appId) {
1697 final Tab currentTab = mTabControl.getCurrentTab();
1698 if (mTabControl.canCreateNewTab()) {
1699 final Tab tab = mTabControl.createNewTab(closeOnExit, appId,
1700 urlData.mUrl);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001701 WebView webview = tab.getWebView();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001702 // If the last tab was removed from the active tabs page, currentTab
1703 // will be null.
1704 if (currentTab != null) {
1705 removeTabFromContentView(currentTab);
1706 }
Patrick Scott8bbd69f2009-08-14 13:35:53 -04001707 // We must set the new tab as the current tab to reflect the old
1708 // animation behavior.
1709 mTabControl.setCurrentTab(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001710 attachTabToContentView(tab);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001711 if (!urlData.isEmpty()) {
Patrick Scott9d53da02010-02-19 10:19:01 -05001712 loadUrlDataIn(tab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001713 }
1714 return tab;
1715 } else {
1716 // Get rid of the subwindow if it exists
1717 dismissSubWindow(currentTab);
1718 if (!urlData.isEmpty()) {
1719 // Load the given url.
Patrick Scott9d53da02010-02-19 10:19:01 -05001720 loadUrlDataIn(currentTab, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001721 }
Leon Scroggins58d56c62010-01-28 15:12:40 -05001722 return currentTab;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001723 }
1724 }
1725
Grace Kloba22ac16e2009-10-07 18:00:23 -07001726 private Tab openTab(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001727 if (mSettings.openInBackground()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728 Tab t = mTabControl.createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001729 if (t != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001730 WebView view = t.getWebView();
Leon Scroggins92472e82010-02-17 16:32:28 -05001731 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001732 }
Grace Klobac9181842009-04-14 08:53:22 -07001733 return t;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001734 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001735 return openTabAndShow(url, false, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001736 }
1737 }
1738
1739 private class Copy implements OnMenuItemClickListener {
1740 private CharSequence mText;
1741
1742 public boolean onMenuItemClick(MenuItem item) {
1743 copy(mText);
1744 return true;
1745 }
1746
1747 public Copy(CharSequence toCopy) {
1748 mText = toCopy;
1749 }
1750 }
1751
1752 private class Download implements OnMenuItemClickListener {
1753 private String mText;
1754
1755 public boolean onMenuItemClick(MenuItem item) {
1756 onDownloadStartNoStream(mText, null, null, null, -1);
1757 return true;
1758 }
1759
1760 public Download(String toDownload) {
1761 mText = toDownload;
1762 }
1763 }
1764
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001765 private class SetAsWallpaper extends Thread implements
1766 OnMenuItemClickListener, DialogInterface.OnCancelListener {
1767 private URL mUrl;
1768 private ProgressDialog mWallpaperProgress;
1769 private boolean mCanceled = false;
1770
1771 public SetAsWallpaper(String url) {
1772 try {
1773 mUrl = new URL(url);
1774 } catch (MalformedURLException e) {
1775 mUrl = null;
1776 }
1777 }
1778
1779 public void onCancel(DialogInterface dialog) {
1780 mCanceled = true;
1781 }
1782
1783 public boolean onMenuItemClick(MenuItem item) {
1784 if (mUrl != null) {
1785 // The user may have tried to set a image with a large file size as their
1786 // background so it may take a few moments to perform the operation. Display
1787 // a progress spinner while it is working.
1788 mWallpaperProgress = new ProgressDialog(BrowserActivity.this);
1789 mWallpaperProgress.setIndeterminate(true);
1790 mWallpaperProgress.setMessage(getText(R.string.progress_dialog_setting_wallpaper));
1791 mWallpaperProgress.setCancelable(true);
1792 mWallpaperProgress.setOnCancelListener(this);
1793 mWallpaperProgress.show();
1794 start();
1795 }
1796 return true;
1797 }
1798
1799 public void run() {
1800 Drawable oldWallpaper = BrowserActivity.this.getWallpaper();
1801 try {
1802 // TODO: This will cause the resource to be downloaded again, when we
1803 // should in most cases be able to grab it from the cache. To fix this
1804 // we should query WebCore to see if we can access a cached version and
1805 // instead open an input stream on that. This pattern could also be used
1806 // in the download manager where the same problem exists.
1807 InputStream inputstream = mUrl.openStream();
1808 if (inputstream != null) {
1809 setWallpaper(inputstream);
1810 }
1811 } catch (IOException e) {
1812 Log.e(LOGTAG, "Unable to set new wallpaper");
1813 // Act as though the user canceled the operation so we try to
1814 // restore the old wallpaper.
1815 mCanceled = true;
1816 }
1817
1818 if (mCanceled) {
1819 // Restore the old wallpaper if the user cancelled whilst we were setting
1820 // the new wallpaper.
1821 int width = oldWallpaper.getIntrinsicWidth();
1822 int height = oldWallpaper.getIntrinsicHeight();
1823 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1824 Canvas canvas = new Canvas(bm);
1825 oldWallpaper.setBounds(0, 0, width, height);
1826 oldWallpaper.draw(canvas);
1827 try {
1828 setWallpaper(bm);
1829 } catch (IOException e) {
1830 Log.e(LOGTAG, "Unable to restore old wallpaper.");
1831 }
1832 mCanceled = false;
1833 }
1834
1835 if (mWallpaperProgress.isShowing()) {
1836 mWallpaperProgress.dismiss();
1837 }
1838 }
1839 }
1840
The Android Open Source Project0c908882009-03-03 19:32:16 -08001841 private void copy(CharSequence text) {
1842 try {
1843 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
1844 if (clip != null) {
1845 clip.setClipboardText(text);
1846 }
1847 } catch (android.os.RemoteException e) {
1848 Log.e(LOGTAG, "Copy failed", e);
1849 }
1850 }
1851
1852 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001853 * Resets the browser title-view to whatever it must be
1854 * (for example, if we had a loading error)
1855 * When we have a new page, we call resetTitle, when we
1856 * have to reset the titlebar to whatever it used to be
1857 * (for example, if the user chose to stop loading), we
1858 * call resetTitleAndRevertLockIcon.
1859 */
1860 /* package */ void resetTitleAndRevertLockIcon() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001861 mTabControl.getCurrentTab().revertLockIcon();
1862 updateLockIconToLatest();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001863 resetTitleIconAndProgress();
1864 }
1865
1866 /**
1867 * Reset the title, favicon, and progress.
1868 */
1869 private void resetTitleIconAndProgress() {
1870 WebView current = mTabControl.getCurrentWebView();
1871 if (current == null) {
1872 return;
1873 }
1874 resetTitleAndIcon(current);
1875 int progress = current.getProgress();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001876 current.getWebChromeClient().onProgressChanged(current, progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001877 }
1878
1879 // Reset the title and the icon based on the given item.
1880 private void resetTitleAndIcon(WebView view) {
1881 WebHistoryItem item = view.copyBackForwardList().getCurrentItem();
1882 if (item != null) {
Leon Scroggins68579392009-09-15 15:31:54 -04001883 setUrlTitle(item.getUrl(), item.getTitle());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001884 setFavicon(item.getFavicon());
1885 } else {
Leon Scroggins68579392009-09-15 15:31:54 -04001886 setUrlTitle(null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001887 setFavicon(null);
1888 }
1889 }
1890
1891 /**
1892 * Sets a title composed of the URL and the title string.
1893 * @param url The URL of the site being loaded.
1894 * @param title The title of the site being loaded.
1895 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07001896 void setUrlTitle(String url, String title) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001897 mUrl = url;
1898 mTitle = title;
1899
Leon Scroggins58d56c62010-01-28 15:12:40 -05001900 // If we are in voice search mode, the title has already been set.
1901 if (mTabControl.getCurrentTab().isInVoiceSearchMode()) return;
1902 mTitleBar.setDisplayTitle(url);
1903 mFakeTitleBar.setDisplayTitle(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001904 }
1905
1906 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001907 * @param url The URL to build a title version of the URL from.
1908 * @return The title version of the URL or null if fails.
1909 * The title version of the URL can be either the URL hostname,
1910 * or the hostname with an "https://" prefix (for secure URLs),
1911 * or an empty string if, for example, the URL in question is a
1912 * file:// URL with no hostname.
1913 */
Leon Scroggins32e14a62009-06-11 10:26:34 -04001914 /* package */ static String buildTitleUrl(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001915 String titleUrl = null;
1916
1917 if (url != null) {
1918 try {
1919 // parse the url string
1920 URL urlObj = new URL(url);
1921 if (urlObj != null) {
1922 titleUrl = "";
1923
1924 String protocol = urlObj.getProtocol();
1925 String host = urlObj.getHost();
1926
1927 if (host != null && 0 < host.length()) {
1928 titleUrl = host;
1929 if (protocol != null) {
1930 // if a secure site, add an "https://" prefix!
1931 if (protocol.equalsIgnoreCase("https")) {
1932 titleUrl = protocol + "://" + host;
1933 }
1934 }
1935 }
1936 }
1937 } catch (MalformedURLException e) {}
1938 }
1939
1940 return titleUrl;
1941 }
1942
1943 // Set the favicon in the title bar.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001944 void setFavicon(Bitmap icon) {
Leon Scroggins68579392009-09-15 15:31:54 -04001945 mTitleBar.setFavicon(icon);
Leon Scrogginsfe87bd32009-10-06 10:10:00 -04001946 mFakeTitleBar.setFavicon(icon);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001947 }
1948
1949 /**
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001950 * Close the tab, remove its associated title bar, and adjust mTabControl's
1951 * current tab to a valid value.
Leon Scroggins1f005d32009-08-10 17:36:42 -04001952 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07001953 /* package */ void closeTab(Tab t) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001954 int currentIndex = mTabControl.getCurrentIndex();
1955 int removeIndex = mTabControl.getTabIndex(t);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001956 mTabControl.removeTab(t);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001957 if (currentIndex >= removeIndex && currentIndex != 0) {
1958 currentIndex--;
1959 }
1960 mTabControl.setCurrentTab(mTabControl.getTab(currentIndex));
Andrei Popescua5bf1de2009-09-23 16:39:23 +01001961 resetTitleIconAndProgress();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001962 }
1963
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001964 /* package */ void goBackOnePageOrQuit() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001965 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001966 if (current == null) {
1967 /*
1968 * Instead of finishing the activity, simply push this to the back
1969 * of the stack and let ActivityManager to choose the foreground
1970 * activity. As BrowserActivity is singleTask, it will be always the
1971 * root of the task. So we can use either true or false for
1972 * moveTaskToBack().
1973 */
1974 moveTaskToBack(true);
Grace Kloba00d85e72009-09-23 18:50:05 -07001975 return;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001976 }
1977 WebView w = current.getWebView();
1978 if (w.canGoBack()) {
1979 w.goBack();
1980 } else {
1981 // Check to see if we are closing a window that was created by
1982 // another window. If so, we switch back to that window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001983 Tab parent = current.getParentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001984 if (parent != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001985 switchToTab(mTabControl.getTabIndex(parent));
1986 // Now we close the other tab
1987 closeTab(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001988 } else {
1989 if (current.closeOnExit()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001990 // force the tab's inLoad() to be false as we are going to
1991 // either finish the activity or remove the tab. This will
1992 // ensure pauseWebViewTimers() taking action.
1993 mTabControl.getCurrentTab().clearInLoad();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001994 if (mTabControl.getTabCount() == 1) {
1995 finish();
1996 return;
1997 }
Mike Reed7bfa63b2009-05-28 11:08:32 -04001998 // call pauseWebViewTimers() now, we won't be able to call
1999 // it in onPause() as the WebView won't be valid.
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002000 // Temporarily change mActivityInPause to be true as
2001 // pauseWebViewTimers() will do nothing if mActivityInPause
2002 // is false.
Grace Kloba918e1d72009-08-13 14:55:06 -07002003 boolean savedState = mActivityInPause;
2004 if (savedState) {
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002005 Log.e(LOGTAG, "BrowserActivity is already paused "
2006 + "while handing goBackOnePageOrQuit.");
Grace Kloba918e1d72009-08-13 14:55:06 -07002007 }
2008 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -04002009 pauseWebViewTimers();
Grace Kloba918e1d72009-08-13 14:55:06 -07002010 mActivityInPause = savedState;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002011 removeTabFromContentView(current);
2012 mTabControl.removeTab(current);
2013 }
2014 /*
2015 * Instead of finishing the activity, simply push this to the back
2016 * of the stack and let ActivityManager to choose the foreground
2017 * activity. As BrowserActivity is singleTask, it will be always the
2018 * root of the task. So we can use either true or false for
2019 * moveTaskToBack().
2020 */
2021 moveTaskToBack(true);
2022 }
2023 }
2024 }
2025
Grace Kloba22ac16e2009-10-07 18:00:23 -07002026 boolean isMenuDown() {
2027 return mMenuIsDown;
2028 }
2029
Grace Kloba5942df02009-09-18 11:48:29 -07002030 @Override
2031 public boolean onKeyDown(int keyCode, KeyEvent event) {
Leon Scrogginsf65b50d2009-12-08 10:44:28 -05002032 // Even if MENU is already held down, we need to call to super to open
2033 // the IME on long press.
2034 if (KeyEvent.KEYCODE_MENU == keyCode) {
2035 mMenuIsDown = true;
2036 return super.onKeyDown(keyCode, event);
2037 }
Grace Kloba5942df02009-09-18 11:48:29 -07002038 // The default key mode is DEFAULT_KEYS_SEARCH_LOCAL. As the MENU is
2039 // still down, we don't want to trigger the search. Pretend to consume
2040 // the key and do nothing.
2041 if (mMenuIsDown) return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002042
Grace Kloba5942df02009-09-18 11:48:29 -07002043 switch(keyCode) {
Grace Kloba5942df02009-09-18 11:48:29 -07002044 case KeyEvent.KEYCODE_SPACE:
Grace Klobada0fe552009-09-22 18:17:24 -07002045 // WebView/WebTextView handle the keys in the KeyDown. As
2046 // the Activity's shortcut keys are only handled when WebView
2047 // doesn't, have to do it in onKeyDown instead of onKeyUp.
2048 if (event.isShiftPressed()) {
2049 getTopWindow().pageUp(false);
2050 } else {
2051 getTopWindow().pageDown(false);
2052 }
Grace Kloba5942df02009-09-18 11:48:29 -07002053 return true;
2054 case KeyEvent.KEYCODE_BACK:
2055 if (event.getRepeatCount() == 0) {
2056 event.startTracking();
2057 return true;
2058 } else if (mCustomView == null && mActiveTabsPage == null
2059 && event.isLongPress()) {
2060 bookmarksOrHistoryPicker(true);
2061 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002062 }
Grace Kloba5942df02009-09-18 11:48:29 -07002063 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002064 }
Grace Kloba5942df02009-09-18 11:48:29 -07002065 return super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002066 }
2067
Grace Kloba5942df02009-09-18 11:48:29 -07002068 @Override
2069 public boolean onKeyUp(int keyCode, KeyEvent event) {
2070 switch(keyCode) {
2071 case KeyEvent.KEYCODE_MENU:
2072 mMenuIsDown = false;
2073 break;
Grace Kloba5942df02009-09-18 11:48:29 -07002074 case KeyEvent.KEYCODE_BACK:
2075 if (event.isTracking() && !event.isCanceled()) {
2076 if (mCustomView != null) {
2077 // if a custom view is showing, hide it
Grace Kloba22ac16e2009-10-07 18:00:23 -07002078 mTabControl.getCurrentWebView().getWebChromeClient()
2079 .onHideCustomView();
Grace Kloba5942df02009-09-18 11:48:29 -07002080 } else if (mActiveTabsPage != null) {
2081 // if tab page is showing, hide it
2082 removeActiveTabPage(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002083 } else {
Grace Kloba5942df02009-09-18 11:48:29 -07002084 WebView subwindow = mTabControl.getCurrentSubWindow();
2085 if (subwindow != null) {
2086 if (subwindow.canGoBack()) {
2087 subwindow.goBack();
2088 } else {
2089 dismissSubWindow(mTabControl.getCurrentTab());
2090 }
2091 } else {
2092 goBackOnePageOrQuit();
2093 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002094 }
Grace Kloba5942df02009-09-18 11:48:29 -07002095 return true;
2096 }
2097 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002098 }
Grace Kloba5942df02009-09-18 11:48:29 -07002099 return super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002100 }
2101
Leon Scroggins68579392009-09-15 15:31:54 -04002102 /* package */ void stopLoading() {
Ben Murdochb7cc8b42009-09-28 10:59:09 +01002103 mDidStopLoad = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002104 resetTitleAndRevertLockIcon();
2105 WebView w = getTopWindow();
2106 w.stopLoading();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002107 // FIXME: before refactor, it is using mWebViewClient. So I keep the
2108 // same logic here. But for subwindow case, should we call into the main
2109 // WebView's onPageFinished as we never call its onPageStarted and if
2110 // the page finishes itself, we don't call onPageFinished.
2111 mTabControl.getCurrentWebView().getWebViewClient().onPageFinished(w,
2112 w.getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002113
2114 cancelStopToast();
2115 mStopToast = Toast
2116 .makeText(this, R.string.stopping, Toast.LENGTH_SHORT);
2117 mStopToast.show();
2118 }
2119
Grace Kloba22ac16e2009-10-07 18:00:23 -07002120 boolean didUserStopLoading() {
2121 return mDidStopLoad;
2122 }
2123
The Android Open Source Project0c908882009-03-03 19:32:16 -08002124 private void cancelStopToast() {
2125 if (mStopToast != null) {
2126 mStopToast.cancel();
2127 mStopToast = null;
2128 }
2129 }
2130
Grace Kloba22ac16e2009-10-07 18:00:23 -07002131 // called by a UI or non-UI thread to post the message
2132 public void postMessage(int what, int arg1, int arg2, Object obj,
2133 long delayMillis) {
2134 mHandler.sendMessageDelayed(mHandler.obtainMessage(what, arg1, arg2,
2135 obj), delayMillis);
2136 }
2137
2138 // called by a UI or non-UI thread to remove the message
2139 void removeMessages(int what, Object object) {
2140 mHandler.removeMessages(what, object);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002141 }
2142
2143 // public message ids
2144 public final static int LOAD_URL = 1001;
2145 public final static int STOP_LOAD = 1002;
2146
2147 // Message Ids
2148 private static final int FOCUS_NODE_HREF = 102;
Grace Kloba92c18a52009-07-31 23:48:32 -07002149 private static final int RELEASE_WAKELOCK = 107;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002150
Grace Kloba22ac16e2009-10-07 18:00:23 -07002151 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
Ben Murdoch2694e232009-09-29 09:41:11 +01002152
The Android Open Source Project0c908882009-03-03 19:32:16 -08002153 // Private handler for handling javascript and saving passwords
2154 private Handler mHandler = new Handler() {
2155
2156 public void handleMessage(Message msg) {
2157 switch (msg.what) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002158 case FOCUS_NODE_HREF:
Ben Murdoch2694e232009-09-29 09:41:11 +01002159 {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002160 String url = (String) msg.getData().get("url");
Ben Murdoch90d088c2009-11-17 18:14:04 +00002161 String title = (String) msg.getData().get("title");
The Android Open Source Project0c908882009-03-03 19:32:16 -08002162 if (url == null || url.length() == 0) {
2163 break;
2164 }
2165 HashMap focusNodeMap = (HashMap) msg.obj;
2166 WebView view = (WebView) focusNodeMap.get("webview");
2167 // Only apply the action if the top window did not change.
2168 if (getTopWindow() != view) {
2169 break;
2170 }
2171 switch (msg.arg1) {
2172 case R.id.open_context_menu_id:
2173 case R.id.view_image_context_menu_id:
Leon Scroggins92472e82010-02-17 16:32:28 -05002174 loadUrlFromContext(getTopWindow(), url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002175 break;
2176 case R.id.open_newtab_context_menu_id:
Grace Kloba22ac16e2009-10-07 18:00:23 -07002177 final Tab parent = mTabControl.getCurrentTab();
2178 final Tab newTab = openTab(url);
Grace Klobac9181842009-04-14 08:53:22 -07002179 if (newTab != parent) {
2180 parent.addChildTab(newTab);
2181 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002182 break;
2183 case R.id.bookmark_context_menu_id:
2184 Intent intent = new Intent(BrowserActivity.this,
2185 AddBookmarkPage.class);
2186 intent.putExtra("url", url);
Ben Murdoch90d088c2009-11-17 18:14:04 +00002187 intent.putExtra("title", title);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002188 startActivity(intent);
2189 break;
2190 case R.id.share_link_context_menu_id:
Leon Scroggins96afcb12009-12-10 12:35:56 -05002191 // See if this site has been visited before
2192 StringBuilder sb = new StringBuilder(
2193 Browser.BookmarkColumns.URL + " = ");
2194 DatabaseUtils.appendEscapedSQLString(sb, url);
2195 Cursor c = mResolver.query(Browser.BOOKMARKS_URI,
2196 Browser.HISTORY_PROJECTION,
2197 sb.toString(),
2198 null,
2199 null);
2200 if (c.moveToFirst()) {
2201 // The site has been visited before, so grab the
2202 // info from the database.
2203 Bitmap favicon = null;
2204 Bitmap thumbnail = null;
2205 String linkTitle = c.getString(Browser.
2206 HISTORY_PROJECTION_TITLE_INDEX);
2207 byte[] data = c.getBlob(Browser.
2208 HISTORY_PROJECTION_FAVICON_INDEX);
2209 if (data != null) {
2210 favicon = BitmapFactory.decodeByteArray(
2211 data, 0, data.length);
2212 }
2213 data = c.getBlob(Browser.
2214 HISTORY_PROJECTION_THUMBNAIL_INDEX);
2215 if (data != null) {
2216 thumbnail = BitmapFactory.decodeByteArray(
2217 data, 0, data.length);
2218 }
2219 sharePage(BrowserActivity.this,
2220 linkTitle, url, favicon, thumbnail);
2221 } else {
2222 Browser.sendString(BrowserActivity.this, url,
2223 getString(
2224 R.string.choosertitle_sharevia));
2225 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002226 break;
2227 case R.id.copy_link_context_menu_id:
2228 copy(url);
2229 break;
2230 case R.id.save_link_context_menu_id:
2231 case R.id.download_context_menu_id:
2232 onDownloadStartNoStream(url, null, null, null, -1);
2233 break;
2234 }
2235 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002236 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002237
2238 case LOAD_URL:
Leon Scroggins92472e82010-02-17 16:32:28 -05002239 loadUrlFromContext(getTopWindow(), (String) msg.obj);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002240 break;
2241
2242 case STOP_LOAD:
2243 stopLoading();
2244 break;
2245
The Android Open Source Project0c908882009-03-03 19:32:16 -08002246 case RELEASE_WAKELOCK:
2247 if (mWakeLock.isHeld()) {
2248 mWakeLock.release();
Grace Kloba5d0e02e2009-10-05 15:15:36 -07002249 // if we reach here, Browser should be still in the
2250 // background loading after WAKELOCK_TIMEOUT (5-min).
2251 // To avoid burning the battery, stop loading.
2252 mTabControl.stopAllLoading();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002253 }
2254 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002255
2256 case UPDATE_BOOKMARK_THUMBNAIL:
2257 WebView view = (WebView) msg.obj;
2258 if (view != null) {
2259 updateScreenshot(view);
2260 }
2261 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002262 }
2263 }
2264 };
2265
Leon Scroggins96afcb12009-12-10 12:35:56 -05002266 /**
2267 * Share a page, providing the title, url, favicon, and a screenshot. Uses
2268 * an {@link Intent} to launch the Activity chooser.
2269 * @param c Context used to launch a new Activity.
2270 * @param title Title of the page. Stored in the Intent with
Paul Westbrook03e6d392010-02-12 10:33:29 -08002271 * {@link Intent#EXTRA_SUBJECT}
Leon Scroggins96afcb12009-12-10 12:35:56 -05002272 * @param url URL of the page. Stored in the Intent with
2273 * {@link Intent#EXTRA_TEXT}
2274 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
2275 * with {@link Browser#EXTRA_SHARE_FAVICON}
2276 * @param screenshot Bitmap of a screenshot of the page. Stored in the
2277 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
2278 */
2279 public static final void sharePage(Context c, String title, String url,
2280 Bitmap favicon, Bitmap screenshot) {
2281 Intent send = new Intent(Intent.ACTION_SEND);
2282 send.setType("text/plain");
2283 send.putExtra(Intent.EXTRA_TEXT, url);
Paul Westbrook03e6d392010-02-12 10:33:29 -08002284 send.putExtra(Intent.EXTRA_SUBJECT, title);
Leon Scroggins96afcb12009-12-10 12:35:56 -05002285 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
2286 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
2287 try {
2288 c.startActivity(Intent.createChooser(send, c.getString(
2289 R.string.choosertitle_sharevia)));
2290 } catch(android.content.ActivityNotFoundException ex) {
2291 // if no app handles it, do nothing
2292 }
2293 }
2294
Leon Scroggins89c6d362009-07-15 16:54:37 -04002295 private void updateScreenshot(WebView view) {
2296 // If this is a bookmarked site, add a screenshot to the database.
2297 // FIXME: When should we update? Every time?
2298 // FIXME: Would like to make sure there is actually something to
2299 // draw, but the API for that (WebViewCore.pictureReady()) is not
2300 // currently accessible here.
Ben Murdochaac7aa62009-09-17 16:57:40 +01002301
Patrick Scott3918d442009-08-04 13:22:29 -04002302 ContentResolver cr = getContentResolver();
2303 final Cursor c = BrowserBookmarksAdapter.queryBookmarksForUrl(
Ben Murdochaac7aa62009-09-17 16:57:40 +01002304 cr, view.getOriginalUrl(), view.getUrl(), true);
Patrick Scott3918d442009-08-04 13:22:29 -04002305 if (c != null) {
Leon Scroggins89c6d362009-07-15 16:54:37 -04002306 boolean succeed = c.moveToFirst();
2307 ContentValues values = null;
2308 while (succeed) {
2309 if (values == null) {
2310 final ByteArrayOutputStream os
2311 = new ByteArrayOutputStream();
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002312 Bitmap bm = createScreenshot(view);
Leon Scroggins45800572009-09-29 16:38:47 -04002313 if (bm == null) {
2314 c.close();
2315 return;
2316 }
Leon Scroggins89c6d362009-07-15 16:54:37 -04002317 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
2318 values = new ContentValues();
2319 values.put(Browser.BookmarkColumns.THUMBNAIL,
2320 os.toByteArray());
2321 }
2322 cr.update(ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
2323 c.getInt(0)), values, null, null);
2324 succeed = c.moveToNext();
2325 }
2326 c.close();
2327 }
2328 }
2329
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002330 /**
Leon Scrogginsf8551612009-09-24 16:06:02 -04002331 * Values for the size of the thumbnail created when taking a screenshot.
2332 * Lazily initialized. Instead of using these directly, use
2333 * getDesiredThumbnailWidth() or getDesiredThumbnailHeight().
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002334 */
Leon Scrogginsf8551612009-09-24 16:06:02 -04002335 private static int THUMBNAIL_WIDTH = 0;
2336 private static int THUMBNAIL_HEIGHT = 0;
2337
2338 /**
2339 * Return the desired width for thumbnail screenshots, which are stored in
2340 * the database, and used on the bookmarks screen.
2341 * @param context Context for finding out the density of the screen.
2342 * @return int desired width for thumbnail screenshot.
2343 */
2344 /* package */ static int getDesiredThumbnailWidth(Context context) {
2345 if (THUMBNAIL_WIDTH == 0) {
2346 float density = context.getResources().getDisplayMetrics().density;
2347 THUMBNAIL_WIDTH = (int) (90 * density);
2348 THUMBNAIL_HEIGHT = (int) (80 * density);
2349 }
2350 return THUMBNAIL_WIDTH;
2351 }
2352
2353 /**
2354 * Return the desired height for thumbnail screenshots, which are stored in
2355 * the database, and used on the bookmarks screen.
2356 * @param context Context for finding out the density of the screen.
2357 * @return int desired height for thumbnail screenshot.
2358 */
2359 /* package */ static int getDesiredThumbnailHeight(Context context) {
2360 // To ensure that they are both initialized.
2361 getDesiredThumbnailWidth(context);
2362 return THUMBNAIL_HEIGHT;
2363 }
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002364
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002365 private Bitmap createScreenshot(WebView view) {
2366 Picture thumbnail = view.capturePicture();
Leon Scroggins45800572009-09-29 16:38:47 -04002367 if (thumbnail == null) {
2368 return null;
2369 }
Leon Scrogginsf8551612009-09-24 16:06:02 -04002370 Bitmap bm = Bitmap.createBitmap(getDesiredThumbnailWidth(this),
2371 getDesiredThumbnailHeight(this), Bitmap.Config.ARGB_4444);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002372 Canvas canvas = new Canvas(bm);
2373 // May need to tweak these values to determine what is the
2374 // best scale factor
Ben Murdoch2694e232009-09-29 09:41:11 +01002375 int thumbnailWidth = thumbnail.getWidth();
Ben Murdochae59c3f2009-10-20 18:30:28 +01002376 int thumbnailHeight = thumbnail.getHeight();
2377 float scaleFactorX = 1.0f;
2378 float scaleFactorY = 1.0f;
Ben Murdoch2694e232009-09-29 09:41:11 +01002379 if (thumbnailWidth > 0) {
Ben Murdochae59c3f2009-10-20 18:30:28 +01002380 scaleFactorX = (float) getDesiredThumbnailWidth(this) /
Ben Murdoch2694e232009-09-29 09:41:11 +01002381 (float)thumbnailWidth;
Ben Murdochae59c3f2009-10-20 18:30:28 +01002382 } else {
2383 return null;
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002384 }
Ben Murdochae59c3f2009-10-20 18:30:28 +01002385
2386 if (view.getWidth() > view.getHeight() &&
2387 thumbnailHeight < view.getHeight() && thumbnailHeight > 0) {
2388 // If the device is in landscape and the page is shorter
2389 // than the height of the view, stretch the thumbnail to fill the
2390 // space.
2391 scaleFactorY = (float) getDesiredThumbnailHeight(this) /
2392 (float)thumbnailHeight;
2393 } else {
2394 // In the portrait case, this looks nice.
2395 scaleFactorY = scaleFactorX;
2396 }
2397
2398 canvas.scale(scaleFactorX, scaleFactorY);
2399
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002400 thumbnail.draw(canvas);
2401 return bm;
2402 }
2403
The Android Open Source Project0c908882009-03-03 19:32:16 -08002404 // -------------------------------------------------------------------------
Grace Kloba22ac16e2009-10-07 18:00:23 -07002405 // Helper function for WebViewClient.
The Android Open Source Project0c908882009-03-03 19:32:16 -08002406 //-------------------------------------------------------------------------
2407
2408 // Use in overrideUrlLoading
2409 /* package */ final static String SCHEME_WTAI = "wtai://wp/";
2410 /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
2411 /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;";
2412 /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;";
2413
Leon Scroggins92472e82010-02-17 16:32:28 -05002414 // Keep this initial progress in sync with initialProgressValue (* 100)
2415 // in ProgressTracker.cpp
2416 private final static int INITIAL_PROGRESS = 10;
2417
Grace Kloba22ac16e2009-10-07 18:00:23 -07002418 void onPageStarted(WebView view, String url, Bitmap favicon) {
2419 // when BrowserActivity just starts, onPageStarted may be called before
2420 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
2421 // to start the timer. As we won't switch tabs while an activity is in
2422 // pause state, we can ensure calling resume and pause in pair.
2423 if (mActivityInPause) resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002424
Grace Kloba22ac16e2009-10-07 18:00:23 -07002425 resetLockIcon(url);
2426 setUrlTitle(url, null);
2427 setFavicon(favicon);
Leon Scroggins8cf8f682009-11-04 11:13:50 -08002428 // Show some progress so that the user knows the page is beginning to
2429 // load
Leon Scroggins92472e82010-02-17 16:32:28 -05002430 onProgressChanged(view, INITIAL_PROGRESS);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002431 mDidStopLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07002432 if (!mIsNetworkUp) createAndShowNetworkDialog();
Patrick Scott15525d42009-09-21 13:39:37 -04002433
Grace Kloba22ac16e2009-10-07 18:00:23 -07002434 if (mSettings.isTracing()) {
2435 String host;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002436 try {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002437 WebAddress uri = new WebAddress(url);
2438 host = uri.mHost;
2439 } catch (android.net.ParseException ex) {
2440 host = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -08002441 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002442 host = host.replace('.', '_');
2443 host += ".trace";
2444 mInTrace = true;
2445 Debug.startMethodTracing(host, 20 * 1024 * 1024);
2446 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002447
Grace Kloba22ac16e2009-10-07 18:00:23 -07002448 // Performance probe
2449 if (false) {
2450 mStart = SystemClock.uptimeMillis();
2451 mProcessStart = Process.getElapsedCpuTime();
2452 long[] sysCpu = new long[7];
2453 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2454 sysCpu, null)) {
2455 mUserStart = sysCpu[0] + sysCpu[1];
2456 mSystemStart = sysCpu[2];
2457 mIdleStart = sysCpu[3];
2458 mIrqStart = sysCpu[4] + sysCpu[5] + sysCpu[6];
2459 }
2460 mUiStart = SystemClock.currentThreadTimeMillis();
2461 }
2462 }
2463
2464 void onPageFinished(WebView view, String url) {
2465 // Reset the title and icon in case we stopped a provisional load.
2466 resetTitleAndIcon(view);
2467 // Update the lock icon image only once we are done loading
2468 updateLockIconToLatest();
2469 // pause the WebView timer and release the wake lock if it is finished
2470 // while BrowserActivity is in pause state.
2471 if (mActivityInPause && pauseWebViewTimers()) {
2472 if (mWakeLock.isHeld()) {
2473 mHandler.removeMessages(RELEASE_WAKELOCK);
2474 mWakeLock.release();
2475 }
2476 }
2477
2478 // Performance probe
2479 if (false) {
2480 long[] sysCpu = new long[7];
2481 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2482 sysCpu, null)) {
2483 String uiInfo = "UI thread used "
2484 + (SystemClock.currentThreadTimeMillis() - mUiStart)
2485 + " ms";
2486 if (LOGD_ENABLED) {
2487 Log.d(LOGTAG, uiInfo);
2488 }
2489 //The string that gets written to the log
2490 String performanceString = "It took total "
2491 + (SystemClock.uptimeMillis() - mStart)
2492 + " ms clock time to load the page."
2493 + "\nbrowser process used "
2494 + (Process.getElapsedCpuTime() - mProcessStart)
2495 + " ms, user processes used "
2496 + (sysCpu[0] + sysCpu[1] - mUserStart) * 10
2497 + " ms, kernel used "
2498 + (sysCpu[2] - mSystemStart) * 10
2499 + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10
2500 + " ms and irq took "
2501 + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart)
2502 * 10 + " ms, " + uiInfo;
2503 if (LOGD_ENABLED) {
2504 Log.d(LOGTAG, performanceString + "\nWebpage: " + url);
2505 }
2506 if (url != null) {
2507 // strip the url to maintain consistency
2508 String newUrl = new String(url);
2509 if (newUrl.startsWith("http://www.")) {
2510 newUrl = newUrl.substring(11);
2511 } else if (newUrl.startsWith("http://")) {
2512 newUrl = newUrl.substring(7);
2513 } else if (newUrl.startsWith("https://www.")) {
2514 newUrl = newUrl.substring(12);
2515 } else if (newUrl.startsWith("https://")) {
2516 newUrl = newUrl.substring(8);
2517 }
2518 if (LOGD_ENABLED) {
2519 Log.d(LOGTAG, newUrl + " loaded");
2520 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002521 }
2522 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002523 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002524
Grace Kloba22ac16e2009-10-07 18:00:23 -07002525 if (mInTrace) {
2526 mInTrace = false;
2527 Debug.stopMethodTracing();
2528 }
2529 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002530
Grace Kloba22ac16e2009-10-07 18:00:23 -07002531 boolean shouldOverrideUrlLoading(WebView view, String url) {
2532 if (url.startsWith(SCHEME_WTAI)) {
2533 // wtai://wp/mc;number
2534 // number=string(phone-number)
2535 if (url.startsWith(SCHEME_WTAI_MC)) {
2536 Intent intent = new Intent(Intent.ACTION_VIEW,
2537 Uri.parse(WebView.SCHEME_TEL +
2538 url.substring(SCHEME_WTAI_MC.length())));
2539 startActivity(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002540 return true;
2541 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002542 // wtai://wp/sd;dtmf
2543 // dtmf=string(dialstring)
2544 if (url.startsWith(SCHEME_WTAI_SD)) {
2545 // TODO: only send when there is active voice connection
2546 return false;
2547 }
2548 // wtai://wp/ap;number;name
2549 // number=string(phone-number)
2550 // name=string
2551 if (url.startsWith(SCHEME_WTAI_AP)) {
2552 // TODO
2553 return false;
2554 }
2555 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002556
Grace Kloba22ac16e2009-10-07 18:00:23 -07002557 // The "about:" schemes are internal to the browser; don't want these to
2558 // be dispatched to other apps.
2559 if (url.startsWith("about:")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002560 return false;
2561 }
2562
Grace Kloba22ac16e2009-10-07 18:00:23 -07002563 Intent intent;
2564 // perform generic parsing of the URI to turn it into an Intent.
2565 try {
2566 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
2567 } catch (URISyntaxException ex) {
2568 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
2569 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002570 }
2571
Grace Kloba22ac16e2009-10-07 18:00:23 -07002572 // check whether the intent can be resolved. If not, we will see
2573 // whether we can download it from the Market.
2574 if (getPackageManager().resolveActivity(intent, 0) == null) {
2575 String packagename = intent.getPackage();
2576 if (packagename != null) {
2577 intent = new Intent(Intent.ACTION_VIEW, Uri
2578 .parse("market://search?q=pname:" + packagename));
2579 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2580 startActivity(intent);
2581 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002582 } else {
2583 return false;
2584 }
2585 }
2586
Grace Kloba22ac16e2009-10-07 18:00:23 -07002587 // sanitize the Intent, ensuring web pages can not bypass browser
2588 // security (only access to BROWSABLE activities).
2589 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2590 intent.setComponent(null);
2591 try {
2592 if (startActivityIfNeeded(intent, -1)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002593 return true;
2594 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002595 } catch (ActivityNotFoundException ex) {
2596 // ignore the error. If no application can handle the URL,
2597 // eg about:blank, assume the browser can handle it.
2598 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002599
Grace Kloba22ac16e2009-10-07 18:00:23 -07002600 if (mMenuIsDown) {
2601 openTab(url);
2602 closeOptionsMenu();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002603 return true;
2604 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002605 return false;
2606 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002607
Grace Kloba22ac16e2009-10-07 18:00:23 -07002608 // -------------------------------------------------------------------------
2609 // Helper function for WebChromeClient
2610 // -------------------------------------------------------------------------
The Android Open Source Project0c908882009-03-03 19:32:16 -08002611
Grace Kloba22ac16e2009-10-07 18:00:23 -07002612 void onProgressChanged(WebView view, int newProgress) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002613 mFakeTitleBar.setProgress(newProgress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002614
Grace Kloba22ac16e2009-10-07 18:00:23 -07002615 if (newProgress == 100) {
2616 // onProgressChanged() may continue to be called after the main
2617 // frame has finished loading, as any remaining sub frames continue
2618 // to load. We'll only get called once though with newProgress as
2619 // 100 when everything is loaded. (onPageFinished is called once
2620 // when the main frame completes loading regardless of the state of
2621 // any sub frames so calls to onProgressChanges may continue after
2622 // onPageFinished has executed)
2623 if (mInLoad) {
2624 mInLoad = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -04002625 updateInLoadMenuItems();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002626 // If the options menu is open, leave the title bar
2627 if (!mOptionsMenuOpen || !mIconView) {
2628 hideFakeTitleBar();
The Android Open Source Projectcb9a0bb2009-03-11 12:11:58 -07002629 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002630 }
Leon Scrogginsaa37e7f2010-03-05 11:51:20 -05002631 } else {
2632 if (!mInLoad) {
2633 // onPageFinished may have already been called but a subframe is
2634 // still loading and updating the progress. Reset mInLoad and
2635 // update the menu items.
2636 mInLoad = true;
2637 updateInLoadMenuItems();
2638 }
2639 // When the page first begins to load, the Activity may still be
2640 // paused, in which case showFakeTitleBar will do nothing. Call
2641 // again as the page continues to load so that it will be shown.
2642 // (Calling it will the fake title bar is already showing will also
2643 // do nothing.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002644 if (!mOptionsMenuOpen || mIconView) {
2645 // This page has begun to load, so show the title bar
2646 showFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002647 }
2648 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002649 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002650
Grace Kloba22ac16e2009-10-07 18:00:23 -07002651 void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002652 // if a view already exists then immediately terminate the new one
2653 if (mCustomView != null) {
2654 callback.onCustomViewHidden();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002655 return;
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002656 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002657
2658 // Add the custom view to its container.
2659 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
2660 mCustomView = view;
2661 mCustomViewCallback = callback;
2662 // Save the menu state and set it to empty while the custom
2663 // view is showing.
2664 mOldMenuState = mMenuState;
2665 mMenuState = EMPTY_MENU;
2666 // Hide the content view.
2667 mContentView.setVisibility(View.GONE);
2668 // Finally show the custom view container.
Andrei Popescu163ab742009-10-20 17:58:23 +01002669 setStatusBarVisibility(false);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002670 mCustomViewContainer.setVisibility(View.VISIBLE);
2671 mCustomViewContainer.bringToFront();
2672 }
2673
2674 void onHideCustomView() {
2675 if (mCustomView == null)
2676 return;
2677
2678 // Hide the custom view.
2679 mCustomView.setVisibility(View.GONE);
2680 // Remove the custom view from its container.
2681 mCustomViewContainer.removeView(mCustomView);
2682 mCustomView = null;
2683 // Reset the old menu state.
2684 mMenuState = mOldMenuState;
2685 mOldMenuState = EMPTY_MENU;
2686 mCustomViewContainer.setVisibility(View.GONE);
2687 mCustomViewCallback.onCustomViewHidden();
2688 // Show the content view.
Andrei Popescu163ab742009-10-20 17:58:23 +01002689 setStatusBarVisibility(true);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002690 mContentView.setVisibility(View.VISIBLE);
2691 }
2692
2693 Bitmap getDefaultVideoPoster() {
2694 if (mDefaultVideoPoster == null) {
2695 mDefaultVideoPoster = BitmapFactory.decodeResource(
2696 getResources(), R.drawable.default_video_poster);
Patrick Scott3918d442009-08-04 13:22:29 -04002697 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002698 return mDefaultVideoPoster;
2699 }
Patrick Scott3918d442009-08-04 13:22:29 -04002700
Grace Kloba22ac16e2009-10-07 18:00:23 -07002701 View getVideoLoadingProgressView() {
2702 if (mVideoProgressView == null) {
2703 LayoutInflater inflater = LayoutInflater.from(BrowserActivity.this);
2704 mVideoProgressView = inflater.inflate(
2705 R.layout.video_loading_progress, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002706 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002707 return mVideoProgressView;
2708 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002709
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002710 /*
2711 * The Object used to inform the WebView of the file to upload.
2712 */
2713 private ValueCallback<Uri> mUploadMessage;
2714
Grace Kloba22ac16e2009-10-07 18:00:23 -07002715 void openFileChooser(ValueCallback<Uri> uploadMsg) {
2716 if (mUploadMessage != null) return;
2717 mUploadMessage = uploadMsg;
2718 Intent i = new Intent(Intent.ACTION_GET_CONTENT);
2719 i.addCategory(Intent.CATEGORY_OPENABLE);
2720 i.setType("*/*");
2721 BrowserActivity.this.startActivityForResult(Intent.createChooser(i,
2722 getString(R.string.choose_upload)), FILE_SELECTED);
2723 }
2724
2725 // -------------------------------------------------------------------------
2726 // Implement functions for DownloadListener
2727 // -------------------------------------------------------------------------
2728
The Android Open Source Project0c908882009-03-03 19:32:16 -08002729 /**
2730 * Notify the host application a download should be done, or that
2731 * the data should be streamed if a streaming viewer is available.
2732 * @param url The full url to the content that should be downloaded
2733 * @param contentDisposition Content-disposition http header, if
2734 * present.
2735 * @param mimetype The mimetype of the content reported by the server
2736 * @param contentLength The file size reported by the server
2737 */
2738 public void onDownloadStart(String url, String userAgent,
2739 String contentDisposition, String mimetype, long contentLength) {
2740 // if we're dealing wih A/V content that's not explicitly marked
2741 // for download, check if it's streamable.
2742 if (contentDisposition == null
Patrick Scotte1fb9662009-08-31 14:31:52 -04002743 || !contentDisposition.regionMatches(
2744 true, 0, "attachment", 0, 10)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002745 // query the package manager to see if there's a registered handler
2746 // that matches.
2747 Intent intent = new Intent(Intent.ACTION_VIEW);
2748 intent.setDataAndType(Uri.parse(url), mimetype);
Patrick Scotte1fb9662009-08-31 14:31:52 -04002749 ResolveInfo info = getPackageManager().resolveActivity(intent,
2750 PackageManager.MATCH_DEFAULT_ONLY);
2751 if (info != null) {
2752 ComponentName myName = getComponentName();
2753 // If we resolved to ourselves, we don't want to attempt to
2754 // load the url only to try and download it again.
2755 if (!myName.getPackageName().equals(
2756 info.activityInfo.packageName)
2757 || !myName.getClassName().equals(
2758 info.activityInfo.name)) {
2759 // someone (other than us) knows how to handle this mime
2760 // type with this scheme, don't download.
2761 try {
2762 startActivity(intent);
2763 return;
2764 } catch (ActivityNotFoundException ex) {
2765 if (LOGD_ENABLED) {
2766 Log.d(LOGTAG, "activity not found for " + mimetype
2767 + " over " + Uri.parse(url).getScheme(),
2768 ex);
2769 }
2770 // Best behavior is to fall back to a download in this
2771 // case
The Android Open Source Project0c908882009-03-03 19:32:16 -08002772 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002773 }
2774 }
2775 }
2776 onDownloadStartNoStream(url, userAgent, contentDisposition, mimetype, contentLength);
2777 }
2778
2779 /**
2780 * Notify the host application a download should be done, even if there
2781 * is a streaming viewer available for thise type.
2782 * @param url The full url to the content that should be downloaded
2783 * @param contentDisposition Content-disposition http header, if
2784 * present.
2785 * @param mimetype The mimetype of the content reported by the server
2786 * @param contentLength The file size reported by the server
2787 */
2788 /*package */ void onDownloadStartNoStream(String url, String userAgent,
2789 String contentDisposition, String mimetype, long contentLength) {
2790
2791 String filename = URLUtil.guessFileName(url,
2792 contentDisposition, mimetype);
2793
2794 // Check to see if we have an SDCard
2795 String status = Environment.getExternalStorageState();
2796 if (!status.equals(Environment.MEDIA_MOUNTED)) {
2797 int title;
2798 String msg;
2799
2800 // Check to see if the SDCard is busy, same as the music app
2801 if (status.equals(Environment.MEDIA_SHARED)) {
2802 msg = getString(R.string.download_sdcard_busy_dlg_msg);
2803 title = R.string.download_sdcard_busy_dlg_title;
2804 } else {
2805 msg = getString(R.string.download_no_sdcard_dlg_msg, filename);
2806 title = R.string.download_no_sdcard_dlg_title;
2807 }
2808
2809 new AlertDialog.Builder(this)
2810 .setTitle(title)
2811 .setIcon(android.R.drawable.ic_dialog_alert)
2812 .setMessage(msg)
2813 .setPositiveButton(R.string.ok, null)
2814 .show();
2815 return;
2816 }
2817
2818 // java.net.URI is a lot stricter than KURL so we have to undo
2819 // KURL's percent-encoding and redo the encoding using java.net.URI.
2820 URI uri = null;
2821 try {
2822 // Undo the percent-encoding that KURL may have done.
2823 String newUrl = new String(URLUtil.decode(url.getBytes()));
2824 // Parse the url into pieces
2825 WebAddress w = new WebAddress(newUrl);
2826 String frag = null;
2827 String query = null;
2828 String path = w.mPath;
2829 // Break the path into path, query, and fragment
2830 if (path.length() > 0) {
2831 // Strip the fragment
2832 int idx = path.lastIndexOf('#');
2833 if (idx != -1) {
2834 frag = path.substring(idx + 1);
2835 path = path.substring(0, idx);
2836 }
2837 idx = path.lastIndexOf('?');
2838 if (idx != -1) {
2839 query = path.substring(idx + 1);
2840 path = path.substring(0, idx);
2841 }
2842 }
2843 uri = new URI(w.mScheme, w.mAuthInfo, w.mHost, w.mPort, path,
2844 query, frag);
2845 } catch (Exception e) {
2846 Log.e(LOGTAG, "Could not parse url for download: " + url, e);
2847 return;
2848 }
2849
2850 // XXX: Have to use the old url since the cookies were stored using the
2851 // old percent-encoded url.
2852 String cookies = CookieManager.getInstance().getCookie(url);
2853
2854 ContentValues values = new ContentValues();
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08002855 values.put(Downloads.Impl.COLUMN_URI, uri.toString());
2856 values.put(Downloads.Impl.COLUMN_COOKIE_DATA, cookies);
2857 values.put(Downloads.Impl.COLUMN_USER_AGENT, userAgent);
2858 values.put(Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE,
The Android Open Source Project0c908882009-03-03 19:32:16 -08002859 getPackageName());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08002860 values.put(Downloads.Impl.COLUMN_NOTIFICATION_CLASS,
The Android Open Source Project0c908882009-03-03 19:32:16 -08002861 BrowserDownloadPage.class.getCanonicalName());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08002862 values.put(Downloads.Impl.COLUMN_VISIBILITY,
2863 Downloads.Impl.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
2864 values.put(Downloads.Impl.COLUMN_MIME_TYPE, mimetype);
2865 values.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, filename);
2866 values.put(Downloads.Impl.COLUMN_DESCRIPTION, uri.getHost());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002867 if (contentLength > 0) {
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08002868 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, contentLength);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002869 }
2870 if (mimetype == null) {
2871 // We must have long pressed on a link or image to download it. We
2872 // are not sure of the mimetype in this case, so do a head request
2873 new FetchUrlMimeType(this).execute(values);
2874 } else {
2875 final Uri contentUri =
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08002876 getContentResolver().insert(Downloads.Impl.CONTENT_URI, values);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002877 }
Leon Scroggins9191a7f2010-03-18 14:44:44 -04002878 Toast.makeText(this, R.string.download_pending, Toast.LENGTH_SHORT)
2879 .show();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002880 }
2881
Grace Kloba22ac16e2009-10-07 18:00:23 -07002882 // -------------------------------------------------------------------------
2883
The Android Open Source Project0c908882009-03-03 19:32:16 -08002884 /**
2885 * Resets the lock icon. This method is called when we start a new load and
2886 * know the url to be loaded.
2887 */
2888 private void resetLockIcon(String url) {
2889 // Save the lock-icon state (we revert to it if the load gets cancelled)
Grace Kloba22ac16e2009-10-07 18:00:23 -07002890 mTabControl.getCurrentTab().resetLockIcon(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002891 updateLockIconImage(LOCK_ICON_UNSECURE);
2892 }
2893
The Android Open Source Project0c908882009-03-03 19:32:16 -08002894 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04002895 * Update the lock icon to correspond to our latest state.
2896 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002897 private void updateLockIconToLatest() {
2898 updateLockIconImage(mTabControl.getCurrentTab().getLockIconType());
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04002899 }
2900
2901 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08002902 * Updates the lock-icon image in the title-bar.
2903 */
2904 private void updateLockIconImage(int lockIconType) {
2905 Drawable d = null;
2906 if (lockIconType == LOCK_ICON_SECURE) {
2907 d = mSecLockIcon;
2908 } else if (lockIconType == LOCK_ICON_MIXED) {
2909 d = mMixLockIcon;
2910 }
Leon Scroggins68579392009-09-15 15:31:54 -04002911 mTitleBar.setLock(d);
Leon Scrogginsfe87bd32009-10-06 10:10:00 -04002912 mFakeTitleBar.setLock(d);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002913 }
2914
2915 /**
2916 * Displays a page-info dialog.
2917 * @param tab The tab to show info about
2918 * @param fromShowSSLCertificateOnError The flag that indicates whether
2919 * this dialog was opened from the SSL-certificate-on-error dialog or
2920 * not. This is important, since we need to know whether to return to
2921 * the parent dialog or simply dismiss.
2922 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002923 private void showPageInfo(final Tab tab,
The Android Open Source Project0c908882009-03-03 19:32:16 -08002924 final boolean fromShowSSLCertificateOnError) {
2925 final LayoutInflater factory = LayoutInflater
2926 .from(this);
2927
2928 final View pageInfoView = factory.inflate(R.layout.page_info, null);
2929
2930 final WebView view = tab.getWebView();
2931
2932 String url = null;
2933 String title = null;
2934
2935 if (view == null) {
2936 url = tab.getUrl();
2937 title = tab.getTitle();
2938 } else if (view == mTabControl.getCurrentWebView()) {
2939 // Use the cached title and url if this is the current WebView
2940 url = mUrl;
2941 title = mTitle;
2942 } else {
2943 url = view.getUrl();
2944 title = view.getTitle();
2945 }
2946
2947 if (url == null) {
2948 url = "";
2949 }
2950 if (title == null) {
2951 title = "";
2952 }
2953
2954 ((TextView) pageInfoView.findViewById(R.id.address)).setText(url);
2955 ((TextView) pageInfoView.findViewById(R.id.title)).setText(title);
2956
2957 mPageInfoView = tab;
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05002958 mPageInfoFromShowSSLCertificateOnError = fromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002959
2960 AlertDialog.Builder alertDialogBuilder =
2961 new AlertDialog.Builder(this)
2962 .setTitle(R.string.page_info).setIcon(android.R.drawable.ic_dialog_info)
2963 .setView(pageInfoView)
2964 .setPositiveButton(
2965 R.string.ok,
2966 new DialogInterface.OnClickListener() {
2967 public void onClick(DialogInterface dialog,
2968 int whichButton) {
2969 mPageInfoDialog = null;
2970 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002971
2972 // if we came here from the SSL error dialog
2973 if (fromShowSSLCertificateOnError) {
2974 // go back to the SSL error dialog
2975 showSSLCertificateOnError(
2976 mSSLCertificateOnErrorView,
2977 mSSLCertificateOnErrorHandler,
2978 mSSLCertificateOnErrorError);
2979 }
2980 }
2981 })
2982 .setOnCancelListener(
2983 new DialogInterface.OnCancelListener() {
2984 public void onCancel(DialogInterface dialog) {
2985 mPageInfoDialog = null;
2986 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002987
2988 // if we came here from the SSL error dialog
2989 if (fromShowSSLCertificateOnError) {
2990 // go back to the SSL error dialog
2991 showSSLCertificateOnError(
2992 mSSLCertificateOnErrorView,
2993 mSSLCertificateOnErrorHandler,
2994 mSSLCertificateOnErrorError);
2995 }
2996 }
2997 });
2998
2999 // if we have a main top-level page SSL certificate set or a certificate
3000 // error
3001 if (fromShowSSLCertificateOnError ||
3002 (view != null && view.getCertificate() != null)) {
3003 // add a 'View Certificate' button
3004 alertDialogBuilder.setNeutralButton(
3005 R.string.view_certificate,
3006 new DialogInterface.OnClickListener() {
3007 public void onClick(DialogInterface dialog,
3008 int whichButton) {
3009 mPageInfoDialog = null;
3010 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003011
3012 // if we came here from the SSL error dialog
3013 if (fromShowSSLCertificateOnError) {
3014 // go back to the SSL error dialog
3015 showSSLCertificateOnError(
3016 mSSLCertificateOnErrorView,
3017 mSSLCertificateOnErrorHandler,
3018 mSSLCertificateOnErrorError);
3019 } else {
3020 // otherwise, display the top-most certificate from
3021 // the chain
3022 if (view.getCertificate() != null) {
3023 showSSLCertificate(tab);
3024 }
3025 }
3026 }
3027 });
3028 }
3029
3030 mPageInfoDialog = alertDialogBuilder.show();
3031 }
3032
3033 /**
3034 * Displays the main top-level page SSL certificate dialog
3035 * (accessible from the Page-Info dialog).
3036 * @param tab The tab to show certificate for.
3037 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003038 private void showSSLCertificate(final Tab tab) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003039 final View certificateView =
3040 inflateCertificateView(tab.getWebView().getCertificate());
3041 if (certificateView == null) {
3042 return;
3043 }
3044
3045 LayoutInflater factory = LayoutInflater.from(this);
3046
3047 final LinearLayout placeholder =
3048 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3049
3050 LinearLayout ll = (LinearLayout) factory.inflate(
3051 R.layout.ssl_success, placeholder);
3052 ((TextView)ll.findViewById(R.id.success))
3053 .setText(R.string.ssl_certificate_is_valid);
3054
3055 mSSLCertificateView = tab;
3056 mSSLCertificateDialog =
3057 new AlertDialog.Builder(this)
3058 .setTitle(R.string.ssl_certificate).setIcon(
3059 R.drawable.ic_dialog_browser_certificate_secure)
3060 .setView(certificateView)
3061 .setPositiveButton(R.string.ok,
3062 new DialogInterface.OnClickListener() {
3063 public void onClick(DialogInterface dialog,
3064 int whichButton) {
3065 mSSLCertificateDialog = null;
3066 mSSLCertificateView = null;
3067
3068 showPageInfo(tab, false);
3069 }
3070 })
3071 .setOnCancelListener(
3072 new DialogInterface.OnCancelListener() {
3073 public void onCancel(DialogInterface dialog) {
3074 mSSLCertificateDialog = null;
3075 mSSLCertificateView = null;
3076
3077 showPageInfo(tab, false);
3078 }
3079 })
3080 .show();
3081 }
3082
3083 /**
3084 * Displays the SSL error certificate dialog.
3085 * @param view The target web-view.
3086 * @param handler The SSL error handler responsible for cancelling the
3087 * connection that resulted in an SSL error or proceeding per user request.
3088 * @param error The SSL error object.
3089 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003090 void showSSLCertificateOnError(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003091 final WebView view, final SslErrorHandler handler, final SslError error) {
3092
3093 final View certificateView =
3094 inflateCertificateView(error.getCertificate());
3095 if (certificateView == null) {
3096 return;
3097 }
3098
3099 LayoutInflater factory = LayoutInflater.from(this);
3100
3101 final LinearLayout placeholder =
3102 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3103
3104 if (error.hasError(SslError.SSL_UNTRUSTED)) {
3105 LinearLayout ll = (LinearLayout)factory
3106 .inflate(R.layout.ssl_warning, placeholder);
3107 ((TextView)ll.findViewById(R.id.warning))
3108 .setText(R.string.ssl_untrusted);
3109 }
3110
3111 if (error.hasError(SslError.SSL_IDMISMATCH)) {
3112 LinearLayout ll = (LinearLayout)factory
3113 .inflate(R.layout.ssl_warning, placeholder);
3114 ((TextView)ll.findViewById(R.id.warning))
3115 .setText(R.string.ssl_mismatch);
3116 }
3117
3118 if (error.hasError(SslError.SSL_EXPIRED)) {
3119 LinearLayout ll = (LinearLayout)factory
3120 .inflate(R.layout.ssl_warning, placeholder);
3121 ((TextView)ll.findViewById(R.id.warning))
3122 .setText(R.string.ssl_expired);
3123 }
3124
3125 if (error.hasError(SslError.SSL_NOTYETVALID)) {
3126 LinearLayout ll = (LinearLayout)factory
3127 .inflate(R.layout.ssl_warning, placeholder);
3128 ((TextView)ll.findViewById(R.id.warning))
3129 .setText(R.string.ssl_not_yet_valid);
3130 }
3131
3132 mSSLCertificateOnErrorHandler = handler;
3133 mSSLCertificateOnErrorView = view;
3134 mSSLCertificateOnErrorError = error;
3135 mSSLCertificateOnErrorDialog =
3136 new AlertDialog.Builder(this)
3137 .setTitle(R.string.ssl_certificate).setIcon(
3138 R.drawable.ic_dialog_browser_certificate_partially_secure)
3139 .setView(certificateView)
3140 .setPositiveButton(R.string.ok,
3141 new DialogInterface.OnClickListener() {
3142 public void onClick(DialogInterface dialog,
3143 int whichButton) {
3144 mSSLCertificateOnErrorDialog = null;
3145 mSSLCertificateOnErrorView = null;
3146 mSSLCertificateOnErrorHandler = null;
3147 mSSLCertificateOnErrorError = null;
3148
Grace Kloba22ac16e2009-10-07 18:00:23 -07003149 view.getWebViewClient().onReceivedSslError(
3150 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003151 }
3152 })
3153 .setNeutralButton(R.string.page_info_view,
3154 new DialogInterface.OnClickListener() {
3155 public void onClick(DialogInterface dialog,
3156 int whichButton) {
3157 mSSLCertificateOnErrorDialog = null;
3158
3159 // do not clear the dialog state: we will
3160 // need to show the dialog again once the
3161 // user is done exploring the page-info details
3162
3163 showPageInfo(mTabControl.getTabFromView(view),
3164 true);
3165 }
3166 })
3167 .setOnCancelListener(
3168 new DialogInterface.OnCancelListener() {
3169 public void onCancel(DialogInterface dialog) {
3170 mSSLCertificateOnErrorDialog = null;
3171 mSSLCertificateOnErrorView = null;
3172 mSSLCertificateOnErrorHandler = null;
3173 mSSLCertificateOnErrorError = null;
3174
Grace Kloba22ac16e2009-10-07 18:00:23 -07003175 view.getWebViewClient().onReceivedSslError(
3176 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003177 }
3178 })
3179 .show();
3180 }
3181
3182 /**
3183 * Inflates the SSL certificate view (helper method).
3184 * @param certificate The SSL certificate.
3185 * @return The resultant certificate view with issued-to, issued-by,
3186 * issued-on, expires-on, and possibly other fields set.
3187 * If the input certificate is null, returns null.
3188 */
3189 private View inflateCertificateView(SslCertificate certificate) {
3190 if (certificate == null) {
3191 return null;
3192 }
3193
3194 LayoutInflater factory = LayoutInflater.from(this);
3195
3196 View certificateView = factory.inflate(
3197 R.layout.ssl_certificate, null);
3198
3199 // issued to:
3200 SslCertificate.DName issuedTo = certificate.getIssuedTo();
3201 if (issuedTo != null) {
3202 ((TextView) certificateView.findViewById(R.id.to_common))
3203 .setText(issuedTo.getCName());
3204 ((TextView) certificateView.findViewById(R.id.to_org))
3205 .setText(issuedTo.getOName());
3206 ((TextView) certificateView.findViewById(R.id.to_org_unit))
3207 .setText(issuedTo.getUName());
3208 }
3209
3210 // issued by:
3211 SslCertificate.DName issuedBy = certificate.getIssuedBy();
3212 if (issuedBy != null) {
3213 ((TextView) certificateView.findViewById(R.id.by_common))
3214 .setText(issuedBy.getCName());
3215 ((TextView) certificateView.findViewById(R.id.by_org))
3216 .setText(issuedBy.getOName());
3217 ((TextView) certificateView.findViewById(R.id.by_org_unit))
3218 .setText(issuedBy.getUName());
3219 }
3220
3221 // issued on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003222 String issuedOn = formatCertificateDate(
3223 certificate.getValidNotBeforeDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003224 ((TextView) certificateView.findViewById(R.id.issued_on))
3225 .setText(issuedOn);
3226
3227 // expires on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003228 String expiresOn = formatCertificateDate(
3229 certificate.getValidNotAfterDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003230 ((TextView) certificateView.findViewById(R.id.expires_on))
3231 .setText(expiresOn);
3232
3233 return certificateView;
3234 }
3235
3236 /**
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003237 * Formats the certificate date to a properly localized date string.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003238 * @return Properly localized version of the certificate date string and
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003239 * the "" if it fails to localize.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003240 */
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003241 private String formatCertificateDate(Date certificateDate) {
3242 if (certificateDate == null) {
3243 return "";
The Android Open Source Project0c908882009-03-03 19:32:16 -08003244 }
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003245 String formattedDate = DateFormat.getDateFormat(this).format(certificateDate);
3246 if (formattedDate == null) {
3247 return "";
3248 }
3249 return formattedDate;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003250 }
3251
3252 /**
3253 * Displays an http-authentication dialog.
3254 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003255 void showHttpAuthentication(final HttpAuthHandler handler,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003256 final String host, final String realm, final String title,
3257 final String name, final String password, int focusId) {
3258 LayoutInflater factory = LayoutInflater.from(this);
3259 final View v = factory
3260 .inflate(R.layout.http_authentication, null);
3261 if (name != null) {
3262 ((EditText) v.findViewById(R.id.username_edit)).setText(name);
3263 }
3264 if (password != null) {
3265 ((EditText) v.findViewById(R.id.password_edit)).setText(password);
3266 }
3267
3268 String titleText = title;
3269 if (titleText == null) {
3270 titleText = getText(R.string.sign_in_to).toString().replace(
3271 "%s1", host).replace("%s2", realm);
3272 }
3273
3274 mHttpAuthHandler = handler;
3275 AlertDialog dialog = new AlertDialog.Builder(this)
3276 .setTitle(titleText)
3277 .setIcon(android.R.drawable.ic_dialog_alert)
3278 .setView(v)
3279 .setPositiveButton(R.string.action,
3280 new DialogInterface.OnClickListener() {
3281 public void onClick(DialogInterface dialog,
3282 int whichButton) {
3283 String nm = ((EditText) v
3284 .findViewById(R.id.username_edit))
3285 .getText().toString();
3286 String pw = ((EditText) v
3287 .findViewById(R.id.password_edit))
3288 .getText().toString();
3289 BrowserActivity.this.setHttpAuthUsernamePassword
3290 (host, realm, nm, pw);
3291 handler.proceed(nm, pw);
3292 mHttpAuthenticationDialog = null;
3293 mHttpAuthHandler = null;
3294 }})
3295 .setNegativeButton(R.string.cancel,
3296 new DialogInterface.OnClickListener() {
3297 public void onClick(DialogInterface dialog,
3298 int whichButton) {
3299 handler.cancel();
3300 BrowserActivity.this.resetTitleAndRevertLockIcon();
3301 mHttpAuthenticationDialog = null;
3302 mHttpAuthHandler = null;
3303 }})
3304 .setOnCancelListener(new DialogInterface.OnCancelListener() {
3305 public void onCancel(DialogInterface dialog) {
3306 handler.cancel();
3307 BrowserActivity.this.resetTitleAndRevertLockIcon();
3308 mHttpAuthenticationDialog = null;
3309 mHttpAuthHandler = null;
3310 }})
3311 .create();
3312 // Make the IME appear when the dialog is displayed if applicable.
3313 dialog.getWindow().setSoftInputMode(
3314 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
3315 dialog.show();
3316 if (focusId != 0) {
3317 dialog.findViewById(focusId).requestFocus();
3318 } else {
3319 v.findViewById(R.id.username_edit).requestFocus();
3320 }
3321 mHttpAuthenticationDialog = dialog;
3322 }
3323
3324 public int getProgress() {
3325 WebView w = mTabControl.getCurrentWebView();
3326 if (w != null) {
3327 return w.getProgress();
3328 } else {
3329 return 100;
3330 }
3331 }
3332
3333 /**
3334 * Set HTTP authentication password.
3335 *
3336 * @param host The host for the password
3337 * @param realm The realm for the password
3338 * @param username The username for the password. If it is null, it means
3339 * password can't be saved.
3340 * @param password The password
3341 */
3342 public void setHttpAuthUsernamePassword(String host, String realm,
3343 String username,
3344 String password) {
3345 WebView w = mTabControl.getCurrentWebView();
3346 if (w != null) {
3347 w.setHttpAuthUsernamePassword(host, realm, username, password);
3348 }
3349 }
3350
3351 /**
3352 * connectivity manager says net has come or gone... inform the user
3353 * @param up true if net has come up, false if net has gone down
3354 */
3355 public void onNetworkToggle(boolean up) {
3356 if (up == mIsNetworkUp) {
3357 return;
3358 } else if (up) {
3359 mIsNetworkUp = true;
3360 if (mAlertDialog != null) {
3361 mAlertDialog.cancel();
3362 mAlertDialog = null;
3363 }
3364 } else {
3365 mIsNetworkUp = false;
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003366 if (mInLoad) {
3367 createAndShowNetworkDialog();
3368 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003369 }
3370 WebView w = mTabControl.getCurrentWebView();
3371 if (w != null) {
3372 w.setNetworkAvailable(up);
3373 }
3374 }
3375
Grace Kloba22ac16e2009-10-07 18:00:23 -07003376 boolean isNetworkUp() {
3377 return mIsNetworkUp;
3378 }
3379
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003380 // This method shows the network dialog alerting the user that the net is
3381 // down. It will only show the dialog if mAlertDialog is null.
3382 private void createAndShowNetworkDialog() {
3383 if (mAlertDialog == null) {
3384 mAlertDialog = new AlertDialog.Builder(this)
3385 .setTitle(R.string.loadSuspendedTitle)
3386 .setMessage(R.string.loadSuspended)
3387 .setPositiveButton(R.string.ok, null)
3388 .show();
3389 }
3390 }
3391
The Android Open Source Project0c908882009-03-03 19:32:16 -08003392 @Override
3393 protected void onActivityResult(int requestCode, int resultCode,
3394 Intent intent) {
Grace Klobabb394f32009-11-19 10:26:37 -08003395 if (getTopWindow() == null) return;
3396
The Android Open Source Project0c908882009-03-03 19:32:16 -08003397 switch (requestCode) {
3398 case COMBO_PAGE:
3399 if (resultCode == RESULT_OK && intent != null) {
3400 String data = intent.getAction();
3401 Bundle extras = intent.getExtras();
3402 if (extras != null && extras.getBoolean("new_window", false)) {
Leon Scroggins25d35472009-09-15 11:37:27 -04003403 openTab(data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003404 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -07003405 final Tab currentTab =
The Android Open Source Project0c908882009-03-03 19:32:16 -08003406 mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04003407 dismissSubWindow(currentTab);
3408 if (data != null && data.length() != 0) {
Leon Scroggins92472e82010-02-17 16:32:28 -05003409 loadUrl(getTopWindow(), data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003410 }
3411 }
3412 }
Leon Scrogginsfde97462010-01-11 13:06:21 -05003413 // Deliberately fall through to PREFERENCES_PAGE, since the
3414 // same extra may be attached to the COMBO_PAGE
3415 case PREFERENCES_PAGE:
3416 if (resultCode == RESULT_OK && intent != null) {
3417 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
3418 if (BrowserSettings.PREF_CLEAR_HISTORY.equals(action)) {
3419 mTabControl.removeParentChildRelationShips();
3420 }
3421 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003422 break;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003423 // Choose a file from the file picker.
3424 case FILE_SELECTED:
3425 if (null == mUploadMessage) break;
3426 Uri result = intent == null || resultCode != RESULT_OK ? null
3427 : intent.getData();
3428 mUploadMessage.onReceiveValue(result);
3429 mUploadMessage = null;
3430 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003431 default:
3432 break;
3433 }
Leon Scroggins30444232009-09-04 18:36:20 -04003434 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003435 }
3436
3437 /*
3438 * This method is called as a result of the user selecting the options
Leon Scrogginsf08809b2010-01-21 09:27:46 -05003439 * menu to see the download window. It shows the download window on top of
3440 * the current window.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003441 */
Leon Scrogginsf08809b2010-01-21 09:27:46 -05003442 private void viewDownloads(Uri downloadRecord) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003443 Intent intent = new Intent(this,
3444 BrowserDownloadPage.class);
3445 intent.setData(downloadRecord);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003446 startActivityForResult(intent, BrowserActivity.DOWNLOAD_PAGE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003447
3448 }
3449
Leon Scroggins160a7e72009-08-14 18:28:01 -04003450 /**
3451 * Open the Go page.
3452 * @param startWithHistory If true, open starting on the history tab.
3453 * Otherwise, start with the bookmarks tab.
Leon Scroggins160a7e72009-08-14 18:28:01 -04003454 */
Leon Scroggins30444232009-09-04 18:36:20 -04003455 /* package */ void bookmarksOrHistoryPicker(boolean startWithHistory) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003456 WebView current = mTabControl.getCurrentWebView();
3457 if (current == null) {
3458 return;
3459 }
3460 Intent intent = new Intent(this,
3461 CombinedBookmarkHistoryActivity.class);
3462 String title = current.getTitle();
3463 String url = current.getUrl();
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003464 Bitmap thumbnail = createScreenshot(current);
3465
The Android Open Source Project0c908882009-03-03 19:32:16 -08003466 // Just in case the user opens bookmarks before a page finishes loading
3467 // so the current history item, and therefore the page, is null.
3468 if (null == url) {
3469 url = mLastEnteredUrl;
3470 // This can happen.
3471 if (null == url) {
3472 url = mSettings.getHomePage();
3473 }
3474 }
3475 // In case the web page has not yet received its associated title.
3476 if (title == null) {
3477 title = url;
3478 }
3479 intent.putExtra("title", title);
3480 intent.putExtra("url", url);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003481 intent.putExtra("thumbnail", thumbnail);
Leon Scroggins30444232009-09-04 18:36:20 -04003482 // Disable opening in a new window if we have maxed out the windows
Grace Kloba22ac16e2009-10-07 18:00:23 -07003483 intent.putExtra("disable_new_window", !mTabControl.canCreateNewTab());
Patrick Scott3918d442009-08-04 13:22:29 -04003484 intent.putExtra("touch_icon_url", current.getTouchIconUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003485 if (startWithHistory) {
3486 intent.putExtra(CombinedBookmarkHistoryActivity.STARTING_TAB,
3487 CombinedBookmarkHistoryActivity.HISTORY_TAB);
3488 }
3489 startActivityForResult(intent, COMBO_PAGE);
3490 }
3491
3492 // Called when loading from context menu or LOAD_URL message
Leon Scroggins92472e82010-02-17 16:32:28 -05003493 private void loadUrlFromContext(WebView view, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003494 // In case the user enters nothing.
3495 if (url != null && url.length() != 0 && view != null) {
3496 url = smartUrlFilter(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003497 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
Leon Scroggins92472e82010-02-17 16:32:28 -05003498 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003499 }
3500 }
3501 }
3502
Leon Scroggins92472e82010-02-17 16:32:28 -05003503 /**
3504 * Load the URL into the given WebView and update the title bar
3505 * to reflect the new load. Call this instead of WebView.loadUrl
3506 * directly.
3507 * @param view The WebView used to load url.
3508 * @param url The URL to load.
3509 */
3510 private void loadUrl(WebView view, String url) {
3511 updateTitleBarForNewLoad(view, url);
3512 view.loadUrl(url);
3513 }
3514
3515 /**
3516 * Load UrlData into a Tab and update the title bar to reflect the new
3517 * load. Call this instead of UrlData.loadIn directly.
3518 * @param t The Tab used to load.
3519 * @param data The UrlData being loaded.
3520 */
3521 private void loadUrlDataIn(Tab t, UrlData data) {
3522 updateTitleBarForNewLoad(t.getWebView(), data.mUrl);
3523 data.loadIn(t);
3524 }
3525
3526 /**
3527 * If the WebView is the top window, update the title bar to reflect
3528 * loading the new URL. i.e. set its text, clear the favicon (which
3529 * will be set once the page begins loading), and set the progress to
3530 * INITIAL_PROGRESS to show that the page has begun to load. Called
3531 * by loadUrl and loadUrlDataIn.
3532 * @param view The WebView that is starting a load.
3533 * @param url The URL that is being loaded.
3534 */
3535 private void updateTitleBarForNewLoad(WebView view, String url) {
3536 if (view == getTopWindow()) {
3537 setUrlTitle(url, null);
3538 setFavicon(null);
3539 onProgressChanged(view, INITIAL_PROGRESS);
3540 }
3541 }
3542
The Android Open Source Project0c908882009-03-03 19:32:16 -08003543 private String smartUrlFilter(Uri inUri) {
3544 if (inUri != null) {
3545 return smartUrlFilter(inUri.toString());
3546 }
3547 return null;
3548 }
3549
Feng Qianb34f87a2009-03-24 21:27:26 -07003550 protected static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003551 "(?i)" + // switch on case insensitive matching
3552 "(" + // begin group for schema
3553 "(?:http|https|file):\\/\\/" +
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003554 "|(?:inline|data|about|content|javascript):" +
The Android Open Source Project0c908882009-03-03 19:32:16 -08003555 ")" +
3556 "(.*)" );
3557
3558 /**
3559 * Attempts to determine whether user input is a URL or search
3560 * terms. Anything with a space is passed to search.
3561 *
3562 * Converts to lowercase any mistakenly uppercased schema (i.e.,
3563 * "Http://" converts to "http://"
3564 *
3565 * @return Original or modified URL
3566 *
3567 */
3568 String smartUrlFilter(String url) {
3569
3570 String inUrl = url.trim();
3571 boolean hasSpace = inUrl.indexOf(' ') != -1;
3572
3573 Matcher matcher = ACCEPTED_URI_SCHEMA.matcher(inUrl);
3574 if (matcher.matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003575 // force scheme to lowercase
3576 String scheme = matcher.group(1);
3577 String lcScheme = scheme.toLowerCase();
3578 if (!lcScheme.equals(scheme)) {
Mitsuru Oshima123ecfb2009-05-18 19:11:14 -07003579 inUrl = lcScheme + matcher.group(2);
3580 }
3581 if (hasSpace) {
3582 inUrl = inUrl.replace(" ", "%20");
The Android Open Source Project0c908882009-03-03 19:32:16 -08003583 }
3584 return inUrl;
3585 }
3586 if (hasSpace) {
Satish Sampath565505b2009-05-29 15:37:27 +01003587 // FIXME: Is this the correct place to add to searches?
3588 // what if someone else calls this function?
3589 int shortcut = parseUrlShortcut(inUrl);
3590 if (shortcut != SHORTCUT_INVALID) {
3591 Browser.addSearchUrl(mResolver, inUrl);
3592 String query = inUrl.substring(2);
3593 switch (shortcut) {
3594 case SHORTCUT_GOOGLE_SEARCH:
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003595 return URLUtil.composeSearchUrl(query, QuickSearch_G, QUERY_PLACE_HOLDER);
Satish Sampath565505b2009-05-29 15:37:27 +01003596 case SHORTCUT_WIKIPEDIA_SEARCH:
3597 return URLUtil.composeSearchUrl(query, QuickSearch_W, QUERY_PLACE_HOLDER);
3598 case SHORTCUT_DICTIONARY_SEARCH:
3599 return URLUtil.composeSearchUrl(query, QuickSearch_D, QUERY_PLACE_HOLDER);
3600 case SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH:
The Android Open Source Project0c908882009-03-03 19:32:16 -08003601 // FIXME: we need location in this case
Satish Sampath565505b2009-05-29 15:37:27 +01003602 return URLUtil.composeSearchUrl(query, QuickSearch_L, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003603 }
3604 }
3605 } else {
Dan Egnor5ee906c2009-11-18 12:11:49 -08003606 if (Patterns.WEB_URL.matcher(inUrl).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003607 return URLUtil.guessUrl(inUrl);
3608 }
3609 }
3610
3611 Browser.addSearchUrl(mResolver, inUrl);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003612 return URLUtil.composeSearchUrl(inUrl, QuickSearch_G, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003613 }
3614
Ben Murdochbff2d602009-07-01 20:19:05 +01003615 /* package */ void setShouldShowErrorConsole(boolean flag) {
3616 if (flag == mShouldShowErrorConsole) {
3617 // Nothing to do.
3618 return;
3619 }
3620
3621 mShouldShowErrorConsole = flag;
3622
Grace Kloba22ac16e2009-10-07 18:00:23 -07003623 ErrorConsoleView errorConsole = mTabControl.getCurrentTab()
3624 .getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01003625
3626 if (flag) {
3627 // Setting the show state of the console will cause it's the layout to be inflated.
3628 if (errorConsole.numberOfErrors() > 0) {
3629 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
3630 } else {
3631 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
3632 }
3633
3634 // Now we can add it to the main view.
3635 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08003636 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01003637 ViewGroup.LayoutParams.WRAP_CONTENT));
3638 } else {
3639 mErrorConsoleContainer.removeView(errorConsole);
3640 }
3641
3642 }
3643
Grace Kloba22ac16e2009-10-07 18:00:23 -07003644 boolean shouldShowErrorConsole() {
3645 return mShouldShowErrorConsole;
3646 }
3647
Andrei Popescu163ab742009-10-20 17:58:23 +01003648 private void setStatusBarVisibility(boolean visible) {
3649 int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
3650 getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
3651 }
3652
Andrei Popescu56199cc2010-01-12 22:39:16 +00003653
3654 private void sendNetworkType(String type, String subtype) {
3655 WebView w = mTabControl.getCurrentWebView();
3656 if (w != null) {
3657 w.setNetworkType(type, subtype);
3658 }
3659 }
3660
Andrei Popescu30995e72010-02-09 16:59:58 +00003661 private void packageChanged(String packageName, boolean wasAdded) {
3662 WebView w = mTabControl.getCurrentWebView();
3663 if (w == null) {
3664 return;
3665 }
3666
3667 if (wasAdded) {
3668 w.addPackageName(packageName);
3669 } else {
3670 w.removePackageName(packageName);
3671 }
3672 }
3673
3674 private void addPackageNames(Set<String> packageNames) {
3675 WebView w = mTabControl.getCurrentWebView();
3676 if (w == null) {
3677 return;
3678 }
3679
3680 w.addPackageNames(packageNames);
3681 }
3682
3683 private void getInstalledPackages() {
3684 AsyncTask<Void, Void, Set<String> > task =
3685 new AsyncTask<Void, Void, Set<String> >() {
3686 protected Set<String> doInBackground(Void... unused) {
3687 Set<String> installedPackages = new HashSet<String>();
3688 PackageManager pm = BrowserActivity.this.getPackageManager();
3689 if (pm != null) {
3690 List<PackageInfo> packages = pm.getInstalledPackages(0);
3691 for (PackageInfo p : packages) {
3692 if (BrowserActivity.this.sGoogleApps.contains(p.packageName)) {
3693 installedPackages.add(p.packageName);
3694 }
3695 }
3696 }
3697
3698 return installedPackages;
3699 }
3700
3701 // Executes on the UI thread
3702 protected void onPostExecute(Set<String> installedPackages) {
3703 addPackageNames(installedPackages);
3704 }
3705 };
3706 task.execute();
3707 }
3708
Grace Klobaeb6eef42009-09-15 17:56:32 -07003709 final static int LOCK_ICON_UNSECURE = 0;
3710 final static int LOCK_ICON_SECURE = 1;
3711 final static int LOCK_ICON_MIXED = 2;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003712
The Android Open Source Project0c908882009-03-03 19:32:16 -08003713 private BrowserSettings mSettings;
3714 private TabControl mTabControl;
3715 private ContentResolver mResolver;
3716 private FrameLayout mContentView;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003717 private View mCustomView;
3718 private FrameLayout mCustomViewContainer;
Andrei Popescuc9b55562009-07-07 10:51:15 +01003719 private WebChromeClient.CustomViewCallback mCustomViewCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003720
3721 // FIXME, temp address onPrepareMenu performance problem. When we move everything out of
3722 // view, we should rewrite this.
3723 private int mCurrentMenuState = 0;
3724 private int mMenuState = R.id.MAIN_MENU;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003725 private int mOldMenuState = EMPTY_MENU;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003726 private static final int EMPTY_MENU = -1;
3727 private Menu mMenu;
3728
3729 private FindDialog mFindDialog;
3730 // Used to prevent chording to result in firing two shortcuts immediately
3731 // one after another. Fixes bug 1211714.
3732 boolean mCanChord;
3733
3734 private boolean mInLoad;
3735 private boolean mIsNetworkUp;
Ben Murdochb7cc8b42009-09-28 10:59:09 +01003736 private boolean mDidStopLoad;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003737
Cary Clark1f10cbf2010-03-22 11:45:23 -04003738 /* package */ boolean mActivityInPause = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003739
3740 private boolean mMenuIsDown;
3741
The Android Open Source Project0c908882009-03-03 19:32:16 -08003742 private static boolean mInTrace;
3743
3744 // Performance probe
3745 private static final int[] SYSTEM_CPU_FORMAT = new int[] {
3746 Process.PROC_SPACE_TERM | Process.PROC_COMBINE,
3747 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time
3748 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time
3749 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time
3750 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time
3751 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time
3752 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time
3753 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG // 7: softirq time
3754 };
3755
3756 private long mStart;
3757 private long mProcessStart;
3758 private long mUserStart;
3759 private long mSystemStart;
3760 private long mIdleStart;
3761 private long mIrqStart;
3762
3763 private long mUiStart;
3764
3765 private Drawable mMixLockIcon;
3766 private Drawable mSecLockIcon;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003767
3768 /* hold a ref so we can auto-cancel if necessary */
3769 private AlertDialog mAlertDialog;
3770
The Android Open Source Project0c908882009-03-03 19:32:16 -08003771 // The up-to-date URL and title (these can be different from those stored
3772 // in WebView, since it takes some time for the information in WebView to
3773 // get updated)
3774 private String mUrl;
3775 private String mTitle;
3776
3777 // As PageInfo has different style for landscape / portrait, we have
3778 // to re-open it when configuration changed
3779 private AlertDialog mPageInfoDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07003780 private Tab mPageInfoView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003781 // If the Page-Info dialog is launched from the SSL-certificate-on-error
3782 // dialog, we should not just dismiss it, but should get back to the
3783 // SSL-certificate-on-error dialog. This flag is used to store this state
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05003784 private boolean mPageInfoFromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003785
3786 // as SSLCertificateOnError has different style for landscape / portrait,
3787 // we have to re-open it when configuration changed
3788 private AlertDialog mSSLCertificateOnErrorDialog;
3789 private WebView mSSLCertificateOnErrorView;
3790 private SslErrorHandler mSSLCertificateOnErrorHandler;
3791 private SslError mSSLCertificateOnErrorError;
3792
3793 // as SSLCertificate has different style for landscape / portrait, we
3794 // have to re-open it when configuration changed
3795 private AlertDialog mSSLCertificateDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07003796 private Tab mSSLCertificateView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003797
3798 // as HttpAuthentication has different style for landscape / portrait, we
3799 // have to re-open it when configuration changed
3800 private AlertDialog mHttpAuthenticationDialog;
3801 private HttpAuthHandler mHttpAuthHandler;
3802
3803 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
3804 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08003805 ViewGroup.LayoutParams.MATCH_PARENT,
3806 ViewGroup.LayoutParams.MATCH_PARENT);
Andrei Popescuadc008d2009-06-26 14:11:30 +01003807 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
3808 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08003809 ViewGroup.LayoutParams.MATCH_PARENT,
3810 ViewGroup.LayoutParams.MATCH_PARENT,
Andrei Popescuadc008d2009-06-26 14:11:30 +01003811 Gravity.CENTER);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003812 // Google search
3813 final static String QuickSearch_G = "http://www.google.com/m?q=%s";
The Android Open Source Project0c908882009-03-03 19:32:16 -08003814 // Wikipedia search
3815 final static String QuickSearch_W = "http://en.wikipedia.org/w/index.php?search=%s&go=Go";
3816 // Dictionary search
3817 final static String QuickSearch_D = "http://dictionary.reference.com/search?q=%s";
3818 // Google Mobile Local search
3819 final static String QuickSearch_L = "http://www.google.com/m/search?site=local&q=%s&near=mountain+view";
3820
3821 final static String QUERY_PLACE_HOLDER = "%s";
3822
3823 // "source" parameter for Google search through search key
3824 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
3825 // "source" parameter for Google search through goto menu
3826 final static String GOOGLE_SEARCH_SOURCE_GOTO = "browser-goto";
3827 // "source" parameter for Google search through simplily type
3828 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
3829 // "source" parameter for Google search suggested by the browser
3830 final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
3831 // "source" parameter for Google search from unknown source
3832 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown";
3833
3834 private final static String LOGTAG = "browser";
3835
The Android Open Source Project0c908882009-03-03 19:32:16 -08003836 private String mLastEnteredUrl;
3837
3838 private PowerManager.WakeLock mWakeLock;
3839 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
3840
3841 private Toast mStopToast;
3842
Leon Scroggins68579392009-09-15 15:31:54 -04003843 private TitleBar mTitleBar;
Leon Scroggins81db3662009-06-04 17:45:11 -04003844
Ben Murdochbff2d602009-07-01 20:19:05 +01003845 private LinearLayout mErrorConsoleContainer = null;
3846 private boolean mShouldShowErrorConsole = false;
3847
The Android Open Source Project0c908882009-03-03 19:32:16 -08003848 // As the ids are dynamically created, we can't guarantee that they will
3849 // be in sequence, so this static array maps ids to a window number.
3850 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
3851 { R.id.window_one_menu_id, R.id.window_two_menu_id, R.id.window_three_menu_id,
3852 R.id.window_four_menu_id, R.id.window_five_menu_id, R.id.window_six_menu_id,
3853 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
3854
3855 // monitor platform changes
3856 private IntentFilter mNetworkStateChangedFilter;
3857 private BroadcastReceiver mNetworkStateIntentReceiver;
3858
Grace Klobab4da0ad2009-05-14 14:45:40 -07003859 private BroadcastReceiver mPackageInstallationReceiver;
3860
The Android Open Source Project0c908882009-03-03 19:32:16 -08003861 // activity requestCode
Nicolas Roard78a98e42009-05-11 13:34:17 +01003862 final static int COMBO_PAGE = 1;
3863 final static int DOWNLOAD_PAGE = 2;
3864 final static int PREFERENCES_PAGE = 3;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003865 final static int FILE_SELECTED = 4;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003866
Andrei Popescu540035d2009-09-18 18:59:20 +01003867 // the default <video> poster
3868 private Bitmap mDefaultVideoPoster;
3869 // the video progress view
3870 private View mVideoProgressView;
3871
Andrei Popescu30995e72010-02-09 16:59:58 +00003872 // The Google packages we monitor for the navigator.isApplicationInstalled()
3873 // API. Add as needed.
3874 private static Set<String> sGoogleApps;
3875 static {
3876 sGoogleApps = new HashSet<String>();
3877 sGoogleApps.add("com.google.android.youtube");
3878 }
3879
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003880 /**
3881 * A UrlData class to abstract how the content will be set to WebView.
3882 * This base class uses loadUrl to show the content.
3883 */
Leon Scroggins6eac63e2010-03-15 18:19:14 -04003884 /* package */ static class UrlData {
Grace Kloba068e48b2010-01-26 18:11:27 -08003885 final String mUrl;
3886 final Map<String, String> mHeaders;
Leon Scroggins58d56c62010-01-28 15:12:40 -05003887 final Intent mVoiceIntent;
Grace Kloba60e095c2009-06-16 11:50:55 -07003888
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003889 UrlData(String url) {
3890 this.mUrl = url;
Grace Kloba068e48b2010-01-26 18:11:27 -08003891 this.mHeaders = null;
Leon Scroggins58d56c62010-01-28 15:12:40 -05003892 this.mVoiceIntent = null;
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003893 }
Grace Kloba60e095c2009-06-16 11:50:55 -07003894
Leon Scroggins58d56c62010-01-28 15:12:40 -05003895 UrlData(String url, Map<String, String> headers, Intent intent) {
Grace Kloba068e48b2010-01-26 18:11:27 -08003896 this.mUrl = url;
3897 this.mHeaders = headers;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -05003898 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
3899 .equals(intent.getAction())) {
Leon Scroggins58d56c62010-01-28 15:12:40 -05003900 this.mVoiceIntent = intent;
3901 } else {
3902 this.mVoiceIntent = null;
3903 }
Grace Kloba60e095c2009-06-16 11:50:55 -07003904 }
3905
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003906 boolean isEmpty() {
Leon Scroggins58d56c62010-01-28 15:12:40 -05003907 return mVoiceIntent == null && (mUrl == null || mUrl.length() == 0);
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003908 }
3909
Leon Scroggins92472e82010-02-17 16:32:28 -05003910 /**
3911 * Load this UrlData into the given Tab. Use loadUrlDataIn to update
3912 * the title bar as well.
3913 */
Leon Scroggins58d56c62010-01-28 15:12:40 -05003914 public void loadIn(Tab t) {
3915 if (mVoiceIntent != null) {
3916 t.activateVoiceSearchMode(mVoiceIntent);
3917 } else {
3918 t.getWebView().loadUrl(mUrl, mHeaders);
3919 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003920 }
3921 };
3922
Leon Scroggins1f005d32009-08-10 17:36:42 -04003923 /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003924}