blob: 4b7afd25c3822f1d227189b56d91faf294f7b6f8 [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;
Svetoslav Ganov2b345992010-05-06 06:13:54 -070090import android.view.accessibility.AccessibilityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080091import android.webkit.CookieManager;
92import android.webkit.CookieSyncManager;
93import android.webkit.DownloadListener;
94import android.webkit.HttpAuthHandler;
Grace Klobab4da0ad2009-05-14 14:45:40 -070095import android.webkit.PluginManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080096import android.webkit.SslErrorHandler;
97import android.webkit.URLUtil;
Leon Clarkecb6cc862009-09-29 18:35:13 +010098import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080099import android.webkit.WebChromeClient;
100import android.webkit.WebHistoryItem;
101import android.webkit.WebIconDatabase;
102import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800103import android.widget.EditText;
104import android.widget.FrameLayout;
105import android.widget.LinearLayout;
106import android.widget.TextView;
107import android.widget.Toast;
Fred Quintana752b6562009-12-18 10:18:26 -0800108import android.accounts.Account;
109import android.accounts.AccountManager;
110import android.accounts.AccountManagerFuture;
111import android.accounts.AuthenticatorException;
112import android.accounts.OperationCanceledException;
113import android.accounts.AccountManagerCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114
Bjorn Bringert10d1cca2010-02-10 14:22:12 +0000115import com.android.common.Search;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500116import com.android.common.speech.LoggingEvents;
Dan Egnor5ee906c2009-11-18 12:11:49 -0800117
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400118import java.io.ByteArrayOutputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119import java.io.File;
Ben Murdoch4f75ba22009-10-27 11:48:28 +0000120import java.io.IOException;
121import java.io.InputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800122import java.net.MalformedURLException;
123import java.net.URI;
Dianne Hackborn99189432009-06-17 18:06:18 -0700124import java.net.URISyntaxException;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800125import java.net.URL;
126import java.net.URLEncoder;
127import java.text.ParseException;
128import java.util.Date;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800129import java.util.HashMap;
Andrei Popescu30995e72010-02-09 16:59:58 +0000130import java.util.HashSet;
Grace Kloba00f54c52010-01-27 14:53:51 -0800131import java.util.Iterator;
Andrei Popescu30995e72010-02-09 16:59:58 +0000132import java.util.List;
Grace Kloba068e48b2010-01-26 18:11:27 -0800133import java.util.Map;
Andrei Popescu30995e72010-02-09 16:59:58 +0000134import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800135import java.util.regex.Matcher;
136import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800137
138public class BrowserActivity extends Activity
Shimeng (Simon) Wang98d5fce2010-03-16 13:23:39 -0700139 implements View.OnCreateContextMenuListener, DownloadListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800140
Dave Bort31a6d1c2009-04-13 15:56:49 -0700141 /* Define some aliases to make these debugging flags easier to refer to.
142 * This file imports android.provider.Browser, so we can't just refer to "Browser.DEBUG".
143 */
144 private final static boolean DEBUG = com.android.browser.Browser.DEBUG;
145 private final static boolean LOGV_ENABLED = com.android.browser.Browser.LOGV_ENABLED;
146 private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
147
Satish Sampath565505b2009-05-29 15:37:27 +0100148 // These are single-character shortcuts for searching popular sources.
149 private static final int SHORTCUT_INVALID = 0;
150 private static final int SHORTCUT_GOOGLE_SEARCH = 1;
151 private static final int SHORTCUT_WIKIPEDIA_SEARCH = 2;
152 private static final int SHORTCUT_DICTIONARY_SEARCH = 3;
153 private static final int SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH = 4;
154
Cary Clarka9771242009-08-11 16:42:26 -0400155 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800156 @Override
157 public Void doInBackground(File... files) {
158 if (files != null) {
159 for (File f : files) {
Cary Clarkd6be1752009-08-12 12:56:42 -0400160 if (!f.delete()) {
161 Log.e(LOGTAG, f.getPath() + " was not deleted");
162 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800163 }
164 }
165 return null;
166 }
167 }
168
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400169 /**
170 * This layout holds everything you see below the status bar, including the
171 * error console, the custom view container, and the webviews.
172 */
173 private FrameLayout mBrowserFrameLayout;
Leon Scroggins81db3662009-06-04 17:45:11 -0400174
Leon Scrogginsd746a942010-05-19 13:21:44 -0400175 private boolean mXLargeScreenSize;
176
Grace Kloba22ac16e2009-10-07 18:00:23 -0700177 @Override
178 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700179 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800180 Log.v(LOGTAG, this + " onStart");
181 }
182 super.onCreate(icicle);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800183 // test the browser in OpenGL
184 // requestWindowFeature(Window.FEATURE_OPENGL);
185
Mike Reedd334bf52010-01-26 15:21:44 -0500186 // enable this to test the browser in 32bit
187 if (false) {
188 getWindow().setFormat(PixelFormat.RGBX_8888);
189 BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
190 }
191
Svetoslav Ganov2b345992010-05-06 06:13:54 -0700192 if (AccessibilityManager.getInstance(this).isEnabled()) {
193 setDefaultKeyMode(DEFAULT_KEYS_DISABLE);
194 } else {
195 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
196 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800197
198 mResolver = getContentResolver();
199
Grace Kloba0923d692009-09-23 21:37:25 -0700200 // If this was a web search request, pass it on to the default web
201 // search provider and finish this activity.
202 if (handleWebSearchIntent(getIntent())) {
203 finish();
204 return;
205 }
206
The Android Open Source Project0c908882009-03-03 19:32:16 -0800207 mSecLockIcon = Resources.getSystem().getDrawable(
208 android.R.drawable.ic_secure);
209 mMixLockIcon = Resources.getSystem().getDrawable(
210 android.R.drawable.ic_partial_secure);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800211
Leon Scroggins81db3662009-06-04 17:45:11 -0400212 FrameLayout frameLayout = (FrameLayout) getWindow().getDecorView()
213 .findViewById(com.android.internal.R.id.content);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400214 mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(this)
215 .inflate(R.layout.custom_screen, null);
216 mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(
217 R.id.main_content);
218 mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout
219 .findViewById(R.id.error_console);
220 mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
221 .findViewById(R.id.fullscreen_custom_content);
222 frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400223 mXLargeScreenSize = (getResources().getConfiguration().screenLayout
224 & Configuration.SCREENLAYOUT_SIZE_MASK)
225 == Configuration.SCREENLAYOUT_SIZE_XLARGE;
226 if (mXLargeScreenSize) {
Leon Scroggins571b3762010-05-26 10:25:01 -0400227 mTitleBar = new TitleBarXLarge(this);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400228 LinearLayout layout = (LinearLayout) mBrowserFrameLayout.
229 findViewById(R.id.vertical_layout);
230 layout.addView(mTitleBar, 0, new LinearLayout.LayoutParams(
231 ViewGroup.LayoutParams.MATCH_PARENT,
232 ViewGroup.LayoutParams.WRAP_CONTENT));
233 } else {
Leon Scroggins571b3762010-05-26 10:25:01 -0400234 mTitleBar = new TitleBar(this);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400235 // mTitleBar will be always be shown in the fully loaded mode on
236 // phone
237 mTitleBar.setProgress(100);
238 // Fake title bar is not needed in xlarge layout
239 mFakeTitleBar = new TitleBar(this);
240 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800241
242 // Create the tab control and our initial tab
243 mTabControl = new TabControl(this);
244
245 // Open the icon database and retain all the bookmark urls for favicons
246 retainIconsOnStartup();
247
248 // Keep a settings instance handy.
249 mSettings = BrowserSettings.getInstance();
250 mSettings.setTabControl(mTabControl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800251
252 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
253 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
254
Patrick Scott6adacc92010-03-05 08:24:51 -0500255 // Find out if the network is currently up.
256 ConnectivityManager cm = (ConnectivityManager) getSystemService(
257 Context.CONNECTIVITY_SERVICE);
258 NetworkInfo info = cm.getActiveNetworkInfo();
259 if (info != null) {
260 mIsNetworkUp = info.isAvailable();
261 }
262
Grace Klobaa34f6862009-07-31 16:28:17 -0700263 /* enables registration for changes in network status from
264 http stack */
265 mNetworkStateChangedFilter = new IntentFilter();
266 mNetworkStateChangedFilter.addAction(
267 ConnectivityManager.CONNECTIVITY_ACTION);
268 mNetworkStateIntentReceiver = new BroadcastReceiver() {
269 @Override
270 public void onReceive(Context context, Intent intent) {
271 if (intent.getAction().equals(
272 ConnectivityManager.CONNECTIVITY_ACTION)) {
Andrei Popescue4c98462010-02-19 15:44:13 +0000273
274 NetworkInfo info = intent.getParcelableExtra(
275 ConnectivityManager.EXTRA_NETWORK_INFO);
276 String typeName = info.getTypeName();
277 String subtypeName = info.getSubtypeName();
278 sendNetworkType(typeName.toLowerCase(),
279 (subtypeName != null ? subtypeName.toLowerCase() : ""));
280
281 onNetworkToggle(info.isAvailable());
Grace Klobaa34f6862009-07-31 16:28:17 -0700282 }
283 }
284 };
285
Grace Kloba615c6c92009-08-03 10:22:44 -0700286 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
287 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
288 filter.addDataScheme("package");
289 mPackageInstallationReceiver = new BroadcastReceiver() {
290 @Override
291 public void onReceive(Context context, Intent intent) {
292 final String action = intent.getAction();
293 final String packageName = intent.getData()
294 .getSchemeSpecificPart();
295 final boolean replacing = intent.getBooleanExtra(
296 Intent.EXTRA_REPLACING, false);
297 if (Intent.ACTION_PACKAGE_REMOVED.equals(action) && replacing) {
298 // if it is replacing, refreshPlugins() when adding
299 return;
300 }
Andrei Popescu30995e72010-02-09 16:59:58 +0000301
302 if (sGoogleApps.contains(packageName)) {
303 BrowserActivity.this.packageChanged(packageName,
304 Intent.ACTION_PACKAGE_ADDED.equals(action));
305 }
306
Grace Kloba615c6c92009-08-03 10:22:44 -0700307 PackageManager pm = BrowserActivity.this.getPackageManager();
308 PackageInfo pkgInfo = null;
309 try {
310 pkgInfo = pm.getPackageInfo(packageName,
311 PackageManager.GET_PERMISSIONS);
312 } catch (PackageManager.NameNotFoundException e) {
313 return;
314 }
315 if (pkgInfo != null) {
316 String permissions[] = pkgInfo.requestedPermissions;
317 if (permissions == null) {
318 return;
319 }
320 boolean permissionOk = false;
321 for (String permit : permissions) {
322 if (PluginManager.PLUGIN_PERMISSION.equals(permit)) {
323 permissionOk = true;
324 break;
325 }
326 }
327 if (permissionOk) {
328 PluginManager.getInstance(BrowserActivity.this)
Grace Klobae56a0f22010-05-26 17:31:02 -0700329 .refreshPlugins(true);
Grace Kloba615c6c92009-08-03 10:22:44 -0700330 }
331 }
332 }
333 };
334 registerReceiver(mPackageInstallationReceiver, filter);
335
The Android Open Source Project0c908882009-03-03 19:32:16 -0800336 if (!mTabControl.restoreState(icicle)) {
337 // clear up the thumbnail directory if we can't restore the state as
338 // none of the files in the directory are referenced any more.
339 new ClearThumbnails().execute(
340 mTabControl.getThumbnailDir().listFiles());
Grace Klobaaab3f092009-07-30 12:29:51 -0700341 // there is no quit on Android. But if we can't restore the state,
342 // we can treat it as a new Browser, remove the old session cookies.
343 CookieManager.getInstance().removeSessionCookie();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800344 final Intent intent = getIntent();
345 final Bundle extra = intent.getExtras();
346 // Create an initial tab.
347 // If the intent is ACTION_VIEW and data is not null, the Browser is
348 // invoked to view the content by another application. In this case,
349 // the tab will be close when exit.
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700350 UrlData urlData = getUrlDataFromIntent(intent);
351
Leon Scroggins58d56c62010-01-28 15:12:40 -0500352 String action = intent.getAction();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700353 final Tab t = mTabControl.createNewTab(
Leon Scroggins58d56c62010-01-28 15:12:40 -0500354 (Intent.ACTION_VIEW.equals(action) &&
355 intent.getData() != null)
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500356 || RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
357 .equals(action),
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700358 intent.getStringExtra(Browser.EXTRA_APPLICATION_ID), urlData.mUrl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800359 mTabControl.setCurrentTab(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800360 attachTabToContentView(t);
361 WebView webView = t.getWebView();
362 if (extra != null) {
363 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
364 if (scale > 0 && scale <= 1000) {
365 webView.setInitialScale(scale);
366 }
367 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800368
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700369 if (urlData.isEmpty()) {
Shimeng (Simon) Wang98d5fce2010-03-16 13:23:39 -0700370 loadUrl(webView, mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800371 } else {
Patrick Scott9d53da02010-02-19 10:19:01 -0500372 loadUrlDataIn(t, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800373 }
374 } else {
375 // TabControl.restoreState() will create a new tab even if
Leon Scroggins1f005d32009-08-10 17:36:42 -0400376 // restoring the state fails.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800377 attachTabToContentView(mTabControl.getCurrentTab());
378 }
Grace Kloba615c6c92009-08-03 10:22:44 -0700379
Feng Qianb3c02da2009-06-29 15:58:08 -0700380 // Read JavaScript flags if it exists.
381 String jsFlags = mSettings.getJsFlags();
382 if (jsFlags.trim().length() != 0) {
383 mTabControl.getCurrentWebView().setJsFlags(jsFlags);
384 }
Andrei Popescu30995e72010-02-09 16:59:58 +0000385 // Work out which packages are installed on the system.
386 getInstalledPackages();
Bjorn Bringerta7611812010-03-24 11:12:02 +0000387
388 // Start watching the default geolocation permissions
389 mSystemAllowGeolocationOrigins
390 = new SystemAllowGeolocationOrigins(getApplicationContext());
391 mSystemAllowGeolocationOrigins.start();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800392 }
393
Leon Scroggins58d56c62010-01-28 15:12:40 -0500394 /**
395 * Feed the previously stored results strings to the BrowserProvider so that
396 * the SearchDialog will show them instead of the standard searches.
397 * @param result String to show on the editable line of the SearchDialog.
398 */
399 /* package */ void showVoiceSearchResults(String result) {
400 ContentProviderClient client = mResolver.acquireContentProviderClient(
401 Browser.BOOKMARKS_URI);
402 ContentProvider prov = client.getLocalContentProvider();
403 BrowserProvider bp = (BrowserProvider) prov;
404 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
405 client.release();
406
Leon Scrogginsfbb3f152010-03-09 17:26:56 -0500407 Bundle bundle = createGoogleSearchSourceBundle(
408 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
409 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
410 startSearch(result, false, bundle, false);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500411 }
412
The Android Open Source Project0c908882009-03-03 19:32:16 -0800413 @Override
414 protected void onNewIntent(Intent intent) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700415 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800416 // When a tab is closed on exit, the current tab index is set to -1.
417 // Reset before proceed as Browser requires the current tab to be set.
418 if (current == null) {
419 // Try to reset the tab in case the index was incorrect.
420 current = mTabControl.getTab(0);
421 if (current == null) {
422 // No tabs at all so just ignore this intent.
423 return;
424 }
425 mTabControl.setCurrentTab(current);
426 attachTabToContentView(current);
427 resetTitleAndIcon(current.getWebView());
428 }
429 final String action = intent.getAction();
430 final int flags = intent.getFlags();
431 if (Intent.ACTION_MAIN.equals(action) ||
432 (flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
433 // just resume the browser
434 return;
435 }
Leon Scrogginsb8a844d2010-03-18 15:06:15 -0400436 // In case the SearchDialog is open.
437 ((SearchManager) getSystemService(Context.SEARCH_SERVICE))
438 .stopSearch();
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500439 boolean activateVoiceSearch = RecognizerResultsIntent
440 .ACTION_VOICE_SEARCH_RESULTS.equals(action);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800441 if (Intent.ACTION_VIEW.equals(action)
442 || Intent.ACTION_SEARCH.equals(action)
443 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
Leon Scroggins58d56c62010-01-28 15:12:40 -0500444 || Intent.ACTION_WEB_SEARCH.equals(action)
445 || activateVoiceSearch) {
Leon Scroggins3b18ce32010-02-08 17:35:59 -0500446 if (current.isInVoiceSearchMode()) {
447 String title = current.getVoiceDisplayTitle();
448 if (title != null && title.equals(intent.getStringExtra(
449 SearchManager.QUERY))) {
450 // The user submitted the same search as the last voice
451 // search, so do nothing.
452 return;
453 }
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500454 if (Intent.ACTION_SEARCH.equals(action)
455 && current.voiceSearchSourceIsGoogle()) {
456 Intent logIntent = new Intent(
457 LoggingEvents.ACTION_LOG_EVENT);
458 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
459 LoggingEvents.VoiceSearch.QUERY_UPDATED);
460 logIntent.putExtra(
461 LoggingEvents.VoiceSearch.EXTRA_QUERY_UPDATED_VALUE,
462 intent.getDataString());
463 sendBroadcast(logIntent);
464 // Note, onPageStarted will revert the voice title bar
465 // When http://b/issue?id=2379215 is fixed, we should update
466 // the title bar here.
467 }
Leon Scroggins3b18ce32010-02-08 17:35:59 -0500468 }
Satish Sampath565505b2009-05-29 15:37:27 +0100469 // If this was a search request (e.g. search query directly typed into the address bar),
470 // pass it on to the default web search provider.
471 if (handleWebSearchIntent(intent)) {
472 return;
473 }
474
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700475 UrlData urlData = getUrlDataFromIntent(intent);
476 if (urlData.isEmpty()) {
477 urlData = new UrlData(mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800478 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700479
Grace Klobacc634032009-07-28 15:58:19 -0700480 final String appId = intent
481 .getStringExtra(Browser.EXTRA_APPLICATION_ID);
Leon Scroggins47208682010-04-07 17:59:48 -0400482 if ((Intent.ACTION_VIEW.equals(action)
483 // If a voice search has no appId, it means that it came
484 // from the browser. In that case, reuse the current tab.
485 || (activateVoiceSearch && appId != null))
Grace Klobacc634032009-07-28 15:58:19 -0700486 && !getPackageName().equals(appId)
487 && (flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700488 Tab appTab = mTabControl.getTabFromId(appId);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700489 if (appTab != null) {
490 Log.i(LOGTAG, "Reusing tab for " + appId);
491 // Dismiss the subwindow if applicable.
492 dismissSubWindow(appTab);
493 // Since we might kill the WebView, remove it from the
494 // content view first.
495 removeTabFromContentView(appTab);
496 // Recreate the main WebView after destroying the old one.
497 // If the WebView has the same original url and is on that
498 // page, it can be reused.
499 boolean needsLoad =
Leon Scroggins6eac63e2010-03-15 18:19:14 -0400500 mTabControl.recreateWebView(appTab, urlData);
Ben Murdochbff2d602009-07-01 20:19:05 +0100501
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700502 if (current != appTab) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400503 switchToTab(mTabControl.getTabIndex(appTab));
504 if (needsLoad) {
Patrick Scott9d53da02010-02-19 10:19:01 -0500505 loadUrlDataIn(appTab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400506 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700507 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400508 // If the tab was the current tab, we have to attach
509 // it to the view system again.
510 attachTabToContentView(appTab);
511 if (needsLoad) {
Patrick Scott9d53da02010-02-19 10:19:01 -0500512 loadUrlDataIn(appTab, urlData);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700513 }
514 }
515 return;
Patrick Scottcd115892009-07-16 09:42:58 -0400516 } else {
517 // No matching application tab, try to find a regular tab
518 // with a matching url.
519 appTab = mTabControl.findUnusedTabWithUrl(urlData.mUrl);
Leon Scroggins25515f82009-08-19 15:31:58 -0400520 if (appTab != null) {
521 if (current != appTab) {
522 switchToTab(mTabControl.getTabIndex(appTab));
523 }
524 // Otherwise, we are already viewing the correct tab.
Patrick Scottcd115892009-07-16 09:42:58 -0400525 } else {
526 // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
527 // will be opened in a new tab unless we have reached
528 // MAX_TABS. Then the url will be opened in the current
529 // tab. If a new tab is created, it will have "true" for
530 // exit on close.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400531 openTabAndShow(urlData, true, appId);
Patrick Scottcd115892009-07-16 09:42:58 -0400532 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700533 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800534 } else {
Grace Kloba638d3f42009-11-23 10:35:04 -0800535 if (!urlData.isEmpty()
536 && urlData.mUrl.startsWith("about:debug")) {
537 if ("about:debug.dom".equals(urlData.mUrl)) {
538 current.getWebView().dumpDomTree(false);
539 } else if ("about:debug.dom.file".equals(urlData.mUrl)) {
540 current.getWebView().dumpDomTree(true);
541 } else if ("about:debug.render".equals(urlData.mUrl)) {
542 current.getWebView().dumpRenderTree(false);
543 } else if ("about:debug.render.file".equals(urlData.mUrl)) {
544 current.getWebView().dumpRenderTree(true);
545 } else if ("about:debug.display".equals(urlData.mUrl)) {
546 current.getWebView().dumpDisplayTree();
Mike Reed9b78e1d2010-01-13 14:40:52 -0800547 } else if (urlData.mUrl.startsWith("about:debug.drag")) {
548 int index = urlData.mUrl.codePointAt(16) - '0';
549 if (index <= 0 || index > 9) {
550 current.getWebView().setDragTracker(null);
551 } else {
552 current.getWebView().setDragTracker(new MeshTracker(index));
553 }
Grace Kloba638d3f42009-11-23 10:35:04 -0800554 } else {
555 mSettings.toggleDebugSettings();
556 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800557 return;
558 }
Leon Scroggins1f005d32009-08-10 17:36:42 -0400559 // Get rid of the subwindow if it exists
560 dismissSubWindow(current);
Leon Scroggins8588d152010-04-15 11:01:53 -0400561 // If the current Tab is being used as an application tab,
562 // remove the association, since the new Intent means that it is
563 // no longer associated with that application.
564 current.setAppId(null);
Patrick Scott9d53da02010-02-19 10:19:01 -0500565 loadUrlDataIn(current, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800566 }
567 }
568 }
569
Satish Sampath565505b2009-05-29 15:37:27 +0100570 private int parseUrlShortcut(String url) {
571 if (url == null) return SHORTCUT_INVALID;
572
573 // FIXME: quick search, need to be customized by setting
574 if (url.length() > 2 && url.charAt(1) == ' ') {
575 switch (url.charAt(0)) {
576 case 'g': return SHORTCUT_GOOGLE_SEARCH;
577 case 'w': return SHORTCUT_WIKIPEDIA_SEARCH;
578 case 'd': return SHORTCUT_DICTIONARY_SEARCH;
579 case 'l': return SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH;
580 }
581 }
582 return SHORTCUT_INVALID;
583 }
584
585 /**
586 * Launches the default web search activity with the query parameters if the given intent's data
587 * are identified as plain search terms and not URLs/shortcuts.
588 * @return true if the intent was handled and web search activity was launched, false if not.
589 */
590 private boolean handleWebSearchIntent(Intent intent) {
591 if (intent == null) return false;
592
593 String url = null;
594 final String action = intent.getAction();
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500595 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS.equals(
596 action)) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500597 return false;
598 }
Satish Sampath565505b2009-05-29 15:37:27 +0100599 if (Intent.ACTION_VIEW.equals(action)) {
Grace Kloba1e705052009-09-29 13:13:36 -0700600 Uri data = intent.getData();
601 if (data != null) url = data.toString();
Satish Sampath565505b2009-05-29 15:37:27 +0100602 } else if (Intent.ACTION_SEARCH.equals(action)
603 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
604 || Intent.ACTION_WEB_SEARCH.equals(action)) {
605 url = intent.getStringExtra(SearchManager.QUERY);
606 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100607 return handleWebSearchRequest(url, intent.getBundleExtra(SearchManager.APP_DATA),
608 intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
Satish Sampath565505b2009-05-29 15:37:27 +0100609 }
610
611 /**
612 * Launches the default web search activity with the query parameters if the given url string
613 * was identified as plain search terms and not URL/shortcut.
614 * @return true if the request was handled and web search activity was launched, false if not.
615 */
Bjorn Bringert04851702009-09-22 10:36:01 +0100616 private boolean handleWebSearchRequest(String inUrl, Bundle appData, String extraData) {
Satish Sampath565505b2009-05-29 15:37:27 +0100617 if (inUrl == null) return false;
618
619 // In general, we shouldn't modify URL from Intent.
620 // But currently, we get the user-typed URL from search box as well.
621 String url = fixUrl(inUrl).trim();
622
623 // URLs and site specific search shortcuts are handled by the regular flow of control, so
624 // return early.
Dan Egnor5ee906c2009-11-18 12:11:49 -0800625 if (Patterns.WEB_URL.matcher(url).matches()
Satish Sampathbc5b9f32009-06-04 18:21:40 +0100626 || ACCEPTED_URI_SCHEMA.matcher(url).matches()
Satish Sampath565505b2009-05-29 15:37:27 +0100627 || parseUrlShortcut(url) != SHORTCUT_INVALID) {
628 return false;
629 }
630
Leon Scroggins8d06e362010-03-24 14:45:57 -0400631 final ContentResolver cr = mResolver;
632 final String newUrl = url;
633 new AsyncTask<Void, Void, Void>() {
634 protected Void doInBackground(Void... unused) {
635 Browser.updateVisitedHistory(cr, newUrl, false);
636 Browser.addSearchUrl(cr, newUrl);
637 return null;
638 }
639 }.execute();
Satish Sampath565505b2009-05-29 15:37:27 +0100640
641 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
642 intent.addCategory(Intent.CATEGORY_DEFAULT);
643 intent.putExtra(SearchManager.QUERY, url);
Satish Sampath15e9f2d2009-06-23 22:29:49 +0100644 if (appData != null) {
645 intent.putExtra(SearchManager.APP_DATA, appData);
646 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100647 if (extraData != null) {
648 intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
649 }
Grace Klobacc634032009-07-28 15:58:19 -0700650 intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName());
Satish Sampath565505b2009-05-29 15:37:27 +0100651 startActivity(intent);
652
653 return true;
654 }
655
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700656 private UrlData getUrlDataFromIntent(Intent intent) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500657 String url = "";
Grace Kloba068e48b2010-01-26 18:11:27 -0800658 Map<String, String> headers = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800659 if (intent != null) {
660 final String action = intent.getAction();
661 if (Intent.ACTION_VIEW.equals(action)) {
662 url = smartUrlFilter(intent.getData());
663 if (url != null && url.startsWith("content:")) {
664 /* Append mimetype so webview knows how to display */
665 String mimeType = intent.resolveType(getContentResolver());
666 if (mimeType != null) {
667 url += "?" + mimeType;
668 }
669 }
Grace Kloba068e48b2010-01-26 18:11:27 -0800670 if (url != null && url.startsWith("http")) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800671 final Bundle pairs = intent
672 .getBundleExtra(Browser.EXTRA_HEADERS);
Grace Kloba2d508ed2010-01-28 11:39:15 -0800673 if (pairs != null && !pairs.isEmpty()) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800674 Iterator<String> iter = pairs.keySet().iterator();
Grace Kloba068e48b2010-01-26 18:11:27 -0800675 headers = new HashMap<String, String>();
Grace Kloba00f54c52010-01-27 14:53:51 -0800676 while (iter.hasNext()) {
677 String key = iter.next();
678 headers.put(key, pairs.getString(key));
Grace Kloba068e48b2010-01-26 18:11:27 -0800679 }
680 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700681 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800682 } else if (Intent.ACTION_SEARCH.equals(action)
683 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
684 || Intent.ACTION_WEB_SEARCH.equals(action)) {
685 url = intent.getStringExtra(SearchManager.QUERY);
686 if (url != null) {
687 mLastEnteredUrl = url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800688 // In general, we shouldn't modify URL from Intent.
689 // But currently, we get the user-typed URL from search box as well.
690 url = fixUrl(url);
691 url = smartUrlFilter(url);
Leon Scroggins8d06e362010-03-24 14:45:57 -0400692 final ContentResolver cr = mResolver;
693 final String newUrl = url;
694 new AsyncTask<Void, Void, Void>() {
695 protected Void doInBackground(Void... unused) {
696 Browser.updateVisitedHistory(cr, newUrl, false);
697 return null;
698 }
699 }.execute();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800700 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
701 if (url.contains(searchSource)) {
702 String source = null;
703 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
704 if (appData != null) {
Bjorn Bringert10d1cca2010-02-10 14:22:12 +0000705 source = appData.getString(Search.SOURCE);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800706 }
707 if (TextUtils.isEmpty(source)) {
708 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
709 }
710 url = url.replace(searchSource, "&source=android-"+source+"&");
711 }
712 }
713 }
714 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500715 return new UrlData(url, headers, intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800716 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500717 /* package */ void showVoiceTitleBar(String title) {
718 mTitleBar.setInVoiceMode(true);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500719 mTitleBar.setDisplayTitle(title);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400720
721 if (!mXLargeScreenSize) {
722 mFakeTitleBar.setInVoiceMode(true);
723 mFakeTitleBar.setDisplayTitle(title);
724 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500725 }
726 /* package */ void revertVoiceTitleBar() {
727 mTitleBar.setInVoiceMode(false);
Leon Scroggins003a5dd2010-03-10 12:13:14 -0500728 mTitleBar.setDisplayTitle(mUrl);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400729
730 if (!mXLargeScreenSize) {
731 mFakeTitleBar.setInVoiceMode(false);
732 mFakeTitleBar.setDisplayTitle(mUrl);
733 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500734 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800735 /* package */ static String fixUrl(String inUrl) {
Cary Clark652ff872009-09-10 13:34:44 -0400736 // FIXME: Converting the url to lower case
737 // duplicates functionality in smartUrlFilter().
738 // However, changing all current callers of fixUrl to
739 // call smartUrlFilter in addition may have unwanted
740 // consequences, and is deferred for now.
741 int colon = inUrl.indexOf(':');
742 boolean allLower = true;
743 for (int index = 0; index < colon; index++) {
744 char ch = inUrl.charAt(index);
745 if (!Character.isLetter(ch)) {
746 break;
747 }
748 allLower &= Character.isLowerCase(ch);
749 if (index == colon - 1 && !allLower) {
750 inUrl = inUrl.substring(0, colon).toLowerCase()
751 + inUrl.substring(colon);
752 }
753 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800754 if (inUrl.startsWith("http://") || inUrl.startsWith("https://"))
755 return inUrl;
756 if (inUrl.startsWith("http:") ||
757 inUrl.startsWith("https:")) {
758 if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) {
759 inUrl = inUrl.replaceFirst("/", "//");
760 } else inUrl = inUrl.replaceFirst(":", "://");
761 }
762 return inUrl;
763 }
764
Grace Kloba22ac16e2009-10-07 18:00:23 -0700765 @Override
766 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800767 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700768 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800769 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
770 }
771
772 if (!mActivityInPause) {
773 Log.e(LOGTAG, "BrowserActivity is already resumed.");
774 return;
775 }
776
Mike Reed7bfa63b2009-05-28 11:08:32 -0400777 mTabControl.resumeCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800778 mActivityInPause = false;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400779 resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800780
781 if (mWakeLock.isHeld()) {
782 mHandler.removeMessages(RELEASE_WAKELOCK);
783 mWakeLock.release();
784 }
785
The Android Open Source Project0c908882009-03-03 19:32:16 -0800786 registerReceiver(mNetworkStateIntentReceiver,
787 mNetworkStateChangedFilter);
788 WebView.enablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800789 }
790
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400791 /**
792 * Since the actual title bar is embedded in the WebView, and removing it
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400793 * would change its appearance, use a different TitleBar to show overlayed
794 * at the top of the screen, when the menu is open or the page is loading.
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400795 */
796 private TitleBar mFakeTitleBar;
797
798 /**
799 * Keeps track of whether the options menu is open. This is important in
800 * determining whether to show or hide the title bar overlay.
801 */
802 private boolean mOptionsMenuOpen;
803
804 /**
805 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
806 * of whether the configuration has changed. The first onMenuOpened call
807 * after a configuration change is simply a reopening of the same menu
808 * (i.e. mIconView did not change).
809 */
810 private boolean mConfigChanged;
811
812 /**
813 * Whether or not the options menu is in its smaller, icon menu form. When
814 * true, we want the title bar overlay to be up. When false, we do not.
815 * Only meaningful if mOptionsMenuOpen is true.
816 */
817 private boolean mIconView;
818
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400819 @Override
820 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400821 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
822 if (mOptionsMenuOpen) {
823 if (mConfigChanged) {
824 // We do not need to make any changes to the state of the
825 // title bar, since the only thing that happened was a
826 // change in orientation
827 mConfigChanged = false;
828 } else {
829 if (mIconView) {
830 // Switching the menu to expanded view, so hide the
831 // title bar.
832 hideFakeTitleBar();
833 mIconView = false;
834 } else {
835 // Switching the menu back to icon view, so show the
836 // title bar once again.
837 showFakeTitleBar();
838 mIconView = true;
839 }
840 }
841 } else {
842 // The options menu is closed, so open it, and show the title
843 showFakeTitleBar();
844 mOptionsMenuOpen = true;
845 mConfigChanged = false;
846 mIconView = true;
847 }
848 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400849 return true;
850 }
851
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400852 private void showFakeTitleBar() {
Leon Scrogginsd746a942010-05-19 13:21:44 -0400853 if (mXLargeScreenSize) return;
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400854 if (mFakeTitleBar.getParent() == null && mActiveTabsPage == null
Grace Kloba847c25b2010-03-30 16:00:26 -0700855 && !mActivityInPause) {
856 WebView mainView = mTabControl.getCurrentWebView();
857 // if there is no current WebView, don't show the faked title bar;
Grace Kloba65190702010-04-02 23:37:26 -0700858 if (mainView == null) {
Cary Clarka0464552009-09-29 13:00:45 -0400859 return;
860 }
Leon Scroggins79e36d92010-04-29 16:01:46 +0100861 // Do not need to check for null, since the current tab will have
862 // at least a main WebView, or we would have returned above.
Cary Clark01cfcdd2010-06-04 16:36:45 -0400863 if (dialogIsUp()) {
Leon Scroggins79e36d92010-04-29 16:01:46 +0100864 // Do not show the fake title bar, which would cover up the
Cary Clark01cfcdd2010-06-04 16:36:45 -0400865 // find or select dialog.
Leon Scroggins79e36d92010-04-29 16:01:46 +0100866 return;
867 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400868
869 WindowManager manager
870 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
871
872 // Add the title bar to the window manager so it can receive touches
873 // while the menu is up
874 WindowManager.LayoutParams params
875 = new WindowManager.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -0800876 ViewGroup.LayoutParams.MATCH_PARENT,
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400877 ViewGroup.LayoutParams.WRAP_CONTENT,
Grace Kloba847c25b2010-03-30 16:00:26 -0700878 WindowManager.LayoutParams.TYPE_APPLICATION,
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400879 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
Leon Scroggins68549862009-09-21 16:02:01 -0400880 PixelFormat.TRANSLUCENT);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400881 params.gravity = Gravity.TOP;
Grace Kloba847c25b2010-03-30 16:00:26 -0700882 boolean atTop = mainView.getScrollY() == 0;
Leon Scroggins83932c72009-09-30 11:55:54 -0400883 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
Grace Kloba13f9ace2010-03-18 21:44:14 -0700884 manager.addView(mFakeTitleBar, params);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400885 }
886 }
887
888 @Override
889 public void onOptionsMenuClosed(Menu menu) {
890 mOptionsMenuOpen = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -0400891 if (!mInLoad) {
892 hideFakeTitleBar();
893 } else if (!mIconView) {
894 // The page is currently loading, and we are in expanded mode, so
895 // we were not showing the menu. Show it once again. It will be
896 // removed when the page finishes.
897 showFakeTitleBar();
898 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400899 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700900
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400901 private void hideFakeTitleBar() {
Leon Scrogginsd746a942010-05-19 13:21:44 -0400902 if (mXLargeScreenSize || mFakeTitleBar.getParent() == null) return;
Leon Scroggins20329572009-09-23 17:42:41 -0400903 WindowManager.LayoutParams params = (WindowManager.LayoutParams)
Grace Kloba13f9ace2010-03-18 21:44:14 -0700904 mFakeTitleBar.getLayoutParams();
Leon Scroggins20329572009-09-23 17:42:41 -0400905 WebView mainView = mTabControl.getCurrentWebView();
906 // Although we decided whether or not to animate based on the current
907 // scroll position, the scroll position may have changed since the
908 // fake title bar was displayed. Make sure it has the appropriate
909 // animation/lack thereof before removing.
910 params.windowAnimations = mainView != null && mainView.getScrollY() == 0
Leon Scroggins83932c72009-09-30 11:55:54 -0400911 ? 0 : R.style.TitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400912 WindowManager manager
913 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Grace Kloba13f9ace2010-03-18 21:44:14 -0700914 manager.updateViewLayout(mFakeTitleBar, params);
915 manager.removeView(mFakeTitleBar);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400916 }
917
The Android Open Source Project0c908882009-03-03 19:32:16 -0800918 /**
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400919 * Special method for the fake title bar to call when displaying its context
920 * menu, since it is in its own Window, and its parent does not show a
921 * context menu.
922 */
923 /* package */ void showTitleBarContextMenu() {
Cary Clark65f4a3c2009-09-28 17:05:06 -0400924 if (null == mTitleBar.getParent()) {
925 return;
926 }
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400927 openContextMenu(mTitleBar);
928 }
929
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400930 @Override
931 public void onContextMenuClosed(Menu menu) {
932 super.onContextMenuClosed(menu);
933 if (mInLoad) {
934 showFakeTitleBar();
935 }
936 }
937
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400938 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800939 * onSaveInstanceState(Bundle map)
940 * onSaveInstanceState is called right before onStop(). The map contains
941 * the saved state.
942 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700943 @Override
944 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700945 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800946 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
947 }
948 // the default implementation requires each view to have an id. As the
949 // browser handles the state itself and it doesn't use id for the views,
950 // don't call the default implementation. Otherwise it will trigger the
951 // warning like this, "couldn't save which view has focus because the
952 // focused view XXX has no id".
953
954 // Save all the tabs
955 mTabControl.saveState(outState);
956 }
957
Grace Kloba22ac16e2009-10-07 18:00:23 -0700958 @Override
959 protected void onPause() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800960 super.onPause();
961
962 if (mActivityInPause) {
963 Log.e(LOGTAG, "BrowserActivity is already paused.");
964 return;
965 }
966
Mike Reed7bfa63b2009-05-28 11:08:32 -0400967 mTabControl.pauseCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800968 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400969 if (mTabControl.getCurrentIndex() >= 0 && !pauseWebViewTimers()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800970 mWakeLock.acquire();
971 mHandler.sendMessageDelayed(mHandler
972 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
973 }
974
Leon Scrogginsa2ab6a72009-09-11 11:49:52 -0400975 // FIXME: This removes the active tabs page and resets the menu to
976 // MAIN_MENU. A better solution might be to do this work in onNewIntent
977 // but then we would need to save it in onSaveInstanceState and restore
978 // it in onCreate/onRestoreInstanceState
979 if (mActiveTabsPage != null) {
980 removeActiveTabPage(true);
981 }
982
The Android Open Source Project0c908882009-03-03 19:32:16 -0800983 cancelStopToast();
984
985 // unregister network state listener
986 unregisterReceiver(mNetworkStateIntentReceiver);
987 WebView.disablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800988 }
989
Grace Kloba22ac16e2009-10-07 18:00:23 -0700990 @Override
991 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700992 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800993 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
994 }
995 super.onDestroy();
Grace Kloba0923d692009-09-23 21:37:25 -0700996
Leon Scroggins8d5fa432009-10-02 15:55:59 -0400997 if (mUploadMessage != null) {
998 mUploadMessage.onReceiveValue(null);
999 mUploadMessage = null;
1000 }
1001
Grace Kloba0923d692009-09-23 21:37:25 -07001002 if (mTabControl == null) return;
1003
Grace Kloba1fc98a32009-10-21 13:23:08 -07001004 // Remove the fake title bar if it is there
1005 hideFakeTitleBar();
1006
The Android Open Source Project0c908882009-03-03 19:32:16 -08001007 // Remove the current tab and sub window
Grace Kloba22ac16e2009-10-07 18:00:23 -07001008 Tab t = mTabControl.getCurrentTab();
Patrick Scottfb5e77f2009-04-08 19:17:37 -07001009 if (t != null) {
1010 dismissSubWindow(t);
1011 removeTabFromContentView(t);
1012 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001013 // Destroy all the tabs
1014 mTabControl.destroy();
1015 WebIconDatabase.getInstance().close();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001016
Grace Klobab4da0ad2009-05-14 14:45:40 -07001017 unregisterReceiver(mPackageInstallationReceiver);
Bjorn Bringerta7611812010-03-24 11:12:02 +00001018
1019 // Stop watching the default geolocation permissions
1020 mSystemAllowGeolocationOrigins.stop();
1021 mSystemAllowGeolocationOrigins = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001022 }
1023
1024 @Override
1025 public void onConfigurationChanged(Configuration newConfig) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04001026 mConfigChanged = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001027 super.onConfigurationChanged(newConfig);
1028
1029 if (mPageInfoDialog != null) {
1030 mPageInfoDialog.dismiss();
1031 showPageInfo(
1032 mPageInfoView,
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05001033 mPageInfoFromShowSSLCertificateOnError);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001034 }
1035 if (mSSLCertificateDialog != null) {
1036 mSSLCertificateDialog.dismiss();
1037 showSSLCertificate(
1038 mSSLCertificateView);
1039 }
1040 if (mSSLCertificateOnErrorDialog != null) {
1041 mSSLCertificateOnErrorDialog.dismiss();
1042 showSSLCertificateOnError(
1043 mSSLCertificateOnErrorView,
1044 mSSLCertificateOnErrorHandler,
1045 mSSLCertificateOnErrorError);
1046 }
1047 if (mHttpAuthenticationDialog != null) {
1048 String title = ((TextView) mHttpAuthenticationDialog
1049 .findViewById(com.android.internal.R.id.alertTitle)).getText()
1050 .toString();
1051 String name = ((TextView) mHttpAuthenticationDialog
1052 .findViewById(R.id.username_edit)).getText().toString();
1053 String password = ((TextView) mHttpAuthenticationDialog
1054 .findViewById(R.id.password_edit)).getText().toString();
1055 int focusId = mHttpAuthenticationDialog.getCurrentFocus()
1056 .getId();
1057 mHttpAuthenticationDialog.dismiss();
1058 showHttpAuthentication(mHttpAuthHandler, null, null, title,
1059 name, password, focusId);
1060 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001061 }
1062
Grace Kloba22ac16e2009-10-07 18:00:23 -07001063 @Override
1064 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001065 super.onLowMemory();
1066 mTabControl.freeMemory();
1067 }
1068
Cary Clarkff4d92c2010-03-25 11:17:03 -04001069 private void resumeWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001070 Tab tab = mTabControl.getCurrentTab();
Cary Clarkff4d92c2010-03-25 11:17:03 -04001071 if (tab == null) return; // monkey can trigger this
Grace Kloba22ac16e2009-10-07 18:00:23 -07001072 boolean inLoad = tab.inLoad();
1073 if ((!mActivityInPause && !inLoad) || (mActivityInPause && inLoad)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001074 CookieSyncManager.getInstance().startSync();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001075 WebView w = tab.getWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001076 if (w != null) {
1077 w.resumeTimers();
1078 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001079 }
1080 }
1081
Mike Reed7bfa63b2009-05-28 11:08:32 -04001082 private boolean pauseWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001083 Tab tab = mTabControl.getCurrentTab();
1084 boolean inLoad = tab.inLoad();
1085 if (mActivityInPause && !inLoad) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001086 CookieSyncManager.getInstance().stopSync();
1087 WebView w = mTabControl.getCurrentWebView();
1088 if (w != null) {
1089 w.pauseTimers();
1090 }
1091 return true;
1092 } else {
1093 return false;
1094 }
1095 }
1096
The Android Open Source Project0c908882009-03-03 19:32:16 -08001097 // Open the icon database and retain all the icons for visited sites.
1098 private void retainIconsOnStartup() {
1099 final WebIconDatabase db = WebIconDatabase.getInstance();
1100 db.open(getDir("icons", 0).getPath());
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001101 Cursor c = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001102 try {
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001103 c = Browser.getAllBookmarks(mResolver);
1104 if (c.moveToFirst()) {
1105 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
1106 do {
1107 String url = c.getString(urlIndex);
1108 db.retainIconForPageUrl(url);
1109 } while (c.moveToNext());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001110 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001111 } catch (IllegalStateException e) {
1112 Log.e(LOGTAG, "retainIconsOnStartup", e);
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001113 } finally {
1114 if (c!= null) c.close();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001115 }
1116 }
1117
1118 // Helper method for getting the top window.
1119 WebView getTopWindow() {
1120 return mTabControl.getCurrentTopWebView();
1121 }
1122
Grace Kloba22ac16e2009-10-07 18:00:23 -07001123 TabControl getTabControl() {
1124 return mTabControl;
1125 }
1126
The Android Open Source Project0c908882009-03-03 19:32:16 -08001127 @Override
1128 public boolean onCreateOptionsMenu(Menu menu) {
1129 super.onCreateOptionsMenu(menu);
1130
1131 MenuInflater inflater = getMenuInflater();
1132 inflater.inflate(R.menu.browser, menu);
1133 mMenu = menu;
1134 updateInLoadMenuItems();
1135 return true;
1136 }
1137
1138 /**
1139 * As the menu can be open when loading state changes
1140 * we must manually update the state of the stop/reload menu
1141 * item
1142 */
1143 private void updateInLoadMenuItems() {
1144 if (mMenu == null) {
1145 return;
1146 }
1147 MenuItem src = mInLoad ?
1148 mMenu.findItem(R.id.stop_menu_id):
1149 mMenu.findItem(R.id.reload_menu_id);
1150 MenuItem dest = mMenu.findItem(R.id.stop_reload_menu_id);
1151 dest.setIcon(src.getIcon());
1152 dest.setTitle(src.getTitle());
1153 }
1154
1155 @Override
1156 public boolean onContextItemSelected(MenuItem item) {
1157 // chording is not an issue with context menus, but we use the same
1158 // options selector, so set mCanChord to true so we can access them.
1159 mCanChord = true;
1160 int id = item.getItemId();
Leon Scroggins96afcb12009-12-10 12:35:56 -05001161 boolean result = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001162 switch (id) {
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001163 // For the context menu from the title bar
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001164 case R.id.title_bar_copy_page_url:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001165 Tab currentTab = mTabControl.getCurrentTab();
1166 if (null == currentTab) {
1167 result = false;
1168 break;
1169 }
1170 WebView mainView = currentTab.getWebView();
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001171 if (null == mainView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001172 result = false;
1173 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001174 }
Leon Scroggins96afcb12009-12-10 12:35:56 -05001175 copy(mainView.getUrl());
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001176 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001177 // -- Browser context menu
1178 case R.id.open_context_menu_id:
The Android Open Source Project0c908882009-03-03 19:32:16 -08001179 case R.id.bookmark_context_menu_id:
1180 case R.id.save_link_context_menu_id:
1181 case R.id.share_link_context_menu_id:
1182 case R.id.copy_link_context_menu_id:
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001183 final WebView webView = getTopWindow();
1184 if (null == webView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001185 result = false;
1186 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001187 }
1188 final HashMap hrefMap = new HashMap();
1189 hrefMap.put("webview", webView);
1190 final Message msg = mHandler.obtainMessage(
1191 FOCUS_NODE_HREF, id, 0, hrefMap);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001192 webView.requestFocusNodeHref(msg);
1193 break;
1194
1195 default:
1196 // For other context menus
Leon Scroggins96afcb12009-12-10 12:35:56 -05001197 result = onOptionsItemSelected(item);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001198 }
1199 mCanChord = false;
Leon Scroggins96afcb12009-12-10 12:35:56 -05001200 return result;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001201 }
1202
1203 private Bundle createGoogleSearchSourceBundle(String source) {
1204 Bundle bundle = new Bundle();
Bjorn Bringert10d1cca2010-02-10 14:22:12 +00001205 bundle.putString(Search.SOURCE, source);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001206 return bundle;
1207 }
1208
Leon Scroggins8ad29922010-02-16 12:33:55 -05001209 /* package */ void editUrl() {
Leon Scroggins68579392009-09-15 15:31:54 -04001210 if (mOptionsMenuOpen) closeOptionsMenu();
Leon Scroggins5bbe9802009-07-31 13:10:55 -04001211 String url = (getTopWindow() == null) ? null : getTopWindow().getUrl();
Grace Kloba83f47342009-07-20 10:44:31 -07001212 startSearch(mSettings.getHomePage().equals(url) ? null : url, true,
Leon Scroggins8ad29922010-02-16 12:33:55 -05001213 null, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001214 }
1215
Leon Scroggins8ad29922010-02-16 12:33:55 -05001216 /**
1217 * Overriding this to insert a local information bundle
1218 */
The Android Open Source Project0c908882009-03-03 19:32:16 -08001219 @Override
1220 public void startSearch(String initialQuery, boolean selectInitialQuery,
1221 Bundle appSearchData, boolean globalSearch) {
1222 if (appSearchData == null) {
1223 appSearchData = createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_TYPE);
1224 }
1225 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1226 }
1227
Leon Scroggins1f005d32009-08-10 17:36:42 -04001228 /**
1229 * Switch tabs. Called by the TitleBarSet when sliding the title bar
1230 * results in changing tabs.
Leon Scroggins160a7e72009-08-14 18:28:01 -04001231 * @param index Index of the tab to change to, as defined by
1232 * mTabControl.getTabIndex(Tab t).
1233 * @return boolean True if we successfully switched to a different tab. If
1234 * the indexth tab is null, or if that tab is the same as
1235 * the current one, return false.
Leon Scroggins1f005d32009-08-10 17:36:42 -04001236 */
Leon Scroggins160a7e72009-08-14 18:28:01 -04001237 /* package */ boolean switchToTab(int index) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001238 Tab tab = mTabControl.getTab(index);
1239 Tab currentTab = mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001240 if (tab == null || tab == currentTab) {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001241 return false;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001242 }
1243 if (currentTab != null) {
1244 // currentTab may be null if it was just removed. In that case,
1245 // we do not need to remove it
1246 removeTabFromContentView(currentTab);
1247 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001248 mTabControl.setCurrentTab(tab);
1249 attachTabToContentView(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001250 resetTitleIconAndProgress();
1251 updateLockIconToLatest();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001252 return true;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001253 }
1254
Grace Kloba22ac16e2009-10-07 18:00:23 -07001255 /* package */ Tab openTabToHomePage() {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001256 return openTabAndShow(mSettings.getHomePage(), false, null);
1257 }
1258
Leon Scroggins1f005d32009-08-10 17:36:42 -04001259 /* package */ void closeCurrentWindow() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001260 final Tab current = mTabControl.getCurrentTab();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001261 if (mTabControl.getTabCount() == 1) {
Leon Scroggins30444232009-09-04 18:36:20 -04001262 // This is the last tab. Open a new one, with the home
1263 // page and close the current one.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001264 openTabToHomePage();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001265 closeTab(current);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001266 return;
1267 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001268 final Tab parent = current.getParentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001269 int indexToShow = -1;
1270 if (parent != null) {
1271 indexToShow = mTabControl.getTabIndex(parent);
1272 } else {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001273 final int currentIndex = mTabControl.getCurrentIndex();
1274 // Try to move to the tab to the right
1275 indexToShow = currentIndex + 1;
1276 if (indexToShow > mTabControl.getTabCount() - 1) {
1277 // Try to move to the tab to the left
1278 indexToShow = currentIndex - 1;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001279 }
1280 }
Leon Scroggins160a7e72009-08-14 18:28:01 -04001281 if (switchToTab(indexToShow)) {
1282 // Close window
1283 closeTab(current);
1284 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001285 }
1286
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001287 private ActiveTabsPage mActiveTabsPage;
1288
1289 /**
1290 * Remove the active tabs page.
1291 * @param needToAttach If true, the active tabs page did not attach a tab
1292 * to the content view, so we need to do that here.
1293 */
1294 /* package */ void removeActiveTabPage(boolean needToAttach) {
1295 mContentView.removeView(mActiveTabsPage);
Leon Scrogginsd746a942010-05-19 13:21:44 -04001296 mTitleBar.setVisibility(View.VISIBLE);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001297 mActiveTabsPage = null;
1298 mMenuState = R.id.MAIN_MENU;
1299 if (needToAttach) {
1300 attachTabToContentView(mTabControl.getCurrentTab());
1301 }
1302 getTopWindow().requestFocus();
1303 }
1304
Cary Clark01cfcdd2010-06-04 16:36:45 -04001305 private WebView showDialog(WebDialog dialog) {
1306 // Need to do something special for Tablet
1307 Tab tab = mTabControl.getCurrentTab();
1308 if (tab.getSubWebView() == null) {
1309 // If the find or select is being performed on the main webview,
1310 // remove the embedded title bar.
1311 WebView mainView = tab.getWebView();
1312 if (mainView != null) {
1313 mainView.setEmbeddedTitleBar(null);
1314 }
1315 }
1316 hideFakeTitleBar();
1317 mMenuState = EMPTY_MENU;
1318 return tab.showDialog(dialog);
1319 }
1320
The Android Open Source Project0c908882009-03-03 19:32:16 -08001321 @Override
1322 public boolean onOptionsItemSelected(MenuItem item) {
1323 if (!mCanChord) {
1324 // The user has already fired a shortcut with this hold down of the
1325 // menu key.
1326 return false;
1327 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001328 if (null == getTopWindow()) {
Leon Scroggins0d7ae0e2009-06-05 11:04:45 -04001329 return false;
1330 }
Grace Kloba6ee9c492009-07-13 10:04:34 -07001331 if (mMenuIsDown) {
1332 // The shortcut action consumes the MENU. Even if it is still down,
1333 // it won't trigger the next shortcut action. In the case of the
1334 // shortcut action triggering a new activity, like Bookmarks, we
1335 // won't get onKeyUp for MENU. So it is important to reset it here.
1336 mMenuIsDown = false;
1337 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001338 switch (item.getItemId()) {
1339 // -- Main menu
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001340 case R.id.new_tab_menu_id:
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001341 openTabToHomePage();
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001342 break;
1343
Leon Scroggins64b80f32009-08-07 12:03:34 -04001344 case R.id.goto_menu_id:
Leon Scroggins8ad29922010-02-16 12:33:55 -05001345 editUrl();
Leon Scrogginsb3a5bed2009-09-28 11:21:56 -04001346 break;
1347
1348 case R.id.bookmarks_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001349 bookmarksOrHistoryPicker(false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001350 break;
1351
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001352 case R.id.active_tabs_menu_id:
1353 mActiveTabsPage = new ActiveTabsPage(this, mTabControl);
1354 removeTabFromContentView(mTabControl.getCurrentTab());
Leon Scrogginsd746a942010-05-19 13:21:44 -04001355 mTitleBar.setVisibility(View.GONE);
Leon Scroggins43de6162009-09-14 19:59:58 -04001356 hideFakeTitleBar();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001357 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
1358 mActiveTabsPage.requestFocus();
1359 mMenuState = EMPTY_MENU;
1360 break;
1361
Leon Scroggins1f005d32009-08-10 17:36:42 -04001362 case R.id.add_bookmark_menu_id:
Leon Scroggins571b3762010-05-26 10:25:01 -04001363 bookmarkCurrentPage();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001364 break;
1365
1366 case R.id.stop_reload_menu_id:
1367 if (mInLoad) {
1368 stopLoading();
1369 } else {
1370 getTopWindow().reload();
1371 }
1372 break;
1373
1374 case R.id.back_menu_id:
1375 getTopWindow().goBack();
1376 break;
1377
1378 case R.id.forward_menu_id:
1379 getTopWindow().goForward();
1380 break;
1381
1382 case R.id.close_menu_id:
1383 // Close the subwindow if it exists.
1384 if (mTabControl.getCurrentSubWindow() != null) {
1385 dismissSubWindow(mTabControl.getCurrentTab());
1386 break;
1387 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001388 closeCurrentWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001389 break;
1390
1391 case R.id.homepage_menu_id:
Grace Kloba22ac16e2009-10-07 18:00:23 -07001392 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001393 if (current != null) {
1394 dismissSubWindow(current);
Leon Scroggins92472e82010-02-17 16:32:28 -05001395 loadUrl(current.getWebView(), mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001396 }
1397 break;
1398
1399 case R.id.preferences_menu_id:
1400 Intent intent = new Intent(this,
1401 BrowserPreferencesPage.class);
Leon Scrogginsd5304942009-12-10 16:11:39 -05001402 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1403 getTopWindow().getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001404 startActivityForResult(intent, PREFERENCES_PAGE);
1405 break;
1406
1407 case R.id.find_menu_id:
Cary Clark01cfcdd2010-06-04 16:36:45 -04001408 showFindDialog();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001409 break;
1410
1411 case R.id.select_text_id:
Cary Clark01cfcdd2010-06-04 16:36:45 -04001412 showSelectDialog();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001413 break;
Cary Clark01cfcdd2010-06-04 16:36:45 -04001414
The Android Open Source Project0c908882009-03-03 19:32:16 -08001415 case R.id.page_info_menu_id:
1416 showPageInfo(mTabControl.getCurrentTab(), false);
1417 break;
1418
1419 case R.id.classic_history_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001420 bookmarksOrHistoryPicker(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001421 break;
1422
Leon Scroggins96afcb12009-12-10 12:35:56 -05001423 case R.id.title_bar_share_page_url:
The Android Open Source Project0c908882009-03-03 19:32:16 -08001424 case R.id.share_page_menu_id:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001425 Tab currentTab = mTabControl.getCurrentTab();
1426 if (null == currentTab) {
1427 mCanChord = false;
1428 return false;
1429 }
1430 currentTab.populatePickerData();
1431 sharePage(this, currentTab.getTitle(),
1432 currentTab.getUrl(), currentTab.getFavicon(),
1433 createScreenshot(currentTab.getWebView()));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001434 break;
1435
1436 case R.id.dump_nav_menu_id:
1437 getTopWindow().debugDump();
1438 break;
1439
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001440 case R.id.dump_counters_menu_id:
1441 getTopWindow().dumpV8Counters();
1442 break;
1443
The Android Open Source Project0c908882009-03-03 19:32:16 -08001444 case R.id.zoom_in_menu_id:
1445 getTopWindow().zoomIn();
1446 break;
1447
1448 case R.id.zoom_out_menu_id:
1449 getTopWindow().zoomOut();
1450 break;
1451
1452 case R.id.view_downloads_menu_id:
1453 viewDownloads(null);
1454 break;
1455
The Android Open Source Project0c908882009-03-03 19:32:16 -08001456 case R.id.window_one_menu_id:
1457 case R.id.window_two_menu_id:
1458 case R.id.window_three_menu_id:
1459 case R.id.window_four_menu_id:
1460 case R.id.window_five_menu_id:
1461 case R.id.window_six_menu_id:
1462 case R.id.window_seven_menu_id:
1463 case R.id.window_eight_menu_id:
1464 {
1465 int menuid = item.getItemId();
1466 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1467 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001468 Tab desiredTab = mTabControl.getTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001469 if (desiredTab != null &&
1470 desiredTab != mTabControl.getCurrentTab()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001471 switchToTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001472 }
1473 break;
1474 }
1475 }
1476 }
1477 break;
1478
1479 default:
1480 if (!super.onOptionsItemSelected(item)) {
1481 return false;
1482 }
1483 // Otherwise fall through.
1484 }
1485 mCanChord = false;
1486 return true;
1487 }
1488
Leon Scroggins571b3762010-05-26 10:25:01 -04001489 /* package */ void bookmarkCurrentPage() {
1490 Intent i = new Intent(BrowserActivity.this,
1491 AddBookmarkPage.class);
1492 WebView w = getTopWindow();
1493 i.putExtra("url", w.getUrl());
1494 i.putExtra("title", w.getTitle());
1495 i.putExtra("touch_icon_url", w.getTouchIconUrl());
1496 i.putExtra("thumbnail", createScreenshot(w));
1497 startActivity(i);
1498 }
1499
Cary Clark01cfcdd2010-06-04 16:36:45 -04001500 private boolean dialogIsUp() {
1501 return null != mFindDialog && mFindDialog.isVisible() ||
1502 null != mSelectDialog && mSelectDialog.isVisible();
1503 }
1504
1505 private boolean closeDialog(WebDialog dialog) {
1506 if (null == dialog || !dialog.isVisible()) return false;
Leon Scroggins79e36d92010-04-29 16:01:46 +01001507 Tab currentTab = mTabControl.getCurrentTab();
Cary Clark01cfcdd2010-06-04 16:36:45 -04001508 currentTab.closeDialog(dialog);
1509 dialog.dismiss();
1510 return true;
1511 }
1512
1513 /*
1514 * Remove the find dialog or select dialog.
1515 */
1516 public void closeDialogs() {
1517 if (!(closeDialog(mFindDialog) || closeDialog(mSelectDialog))) return;
Leon Scrogginsd746a942010-05-19 13:21:44 -04001518 if (!mXLargeScreenSize) {
1519 // If the Find was being performed in the main WebView, replace the
1520 // embedded title bar.
Cary Clark01cfcdd2010-06-04 16:36:45 -04001521 Tab currentTab = mTabControl.getCurrentTab();
Leon Scrogginsd746a942010-05-19 13:21:44 -04001522 if (currentTab.getSubWebView() == null) {
1523 WebView mainView = currentTab.getWebView();
1524 if (mainView != null) {
1525 mainView.setEmbeddedTitleBar(mTitleBar);
1526 }
Leon Scroggins79e36d92010-04-29 16:01:46 +01001527 }
1528 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001529 mMenuState = R.id.MAIN_MENU;
Leon Scroggins79e36d92010-04-29 16:01:46 +01001530 if (mInLoad) {
1531 // The title bar was hidden, because otherwise it would cover up the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001532 // find or select dialog. Now that the dialog has been removed,
1533 // show the fake title bar once again.
Leon Scroggins79e36d92010-04-29 16:01:46 +01001534 showFakeTitleBar();
1535 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001536 }
1537
Cary Clark01cfcdd2010-06-04 16:36:45 -04001538 public void showFindDialog() {
1539 if (null == mFindDialog) {
1540 mFindDialog = new FindDialog(this);
1541 }
1542 showDialog(mFindDialog).setFindIsUp(true);
1543 }
1544
1545 public void setFindDialogText(String text) {
1546 mFindDialog.setText(text);
1547 }
1548
1549 public void showSelectDialog() {
1550 if (null == mSelectDialog) {
1551 mSelectDialog = new SelectDialog(this);
1552 }
1553 showDialog(mSelectDialog).setUpSelect();
1554 mSelectDialog.hideSoftInput();
1555 }
1556
Grace Kloba22ac16e2009-10-07 18:00:23 -07001557 @Override
1558 public boolean onPrepareOptionsMenu(Menu menu) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001559 // This happens when the user begins to hold down the menu key, so
1560 // allow them to chord to get a shortcut.
1561 mCanChord = true;
1562 // Note: setVisible will decide whether an item is visible; while
1563 // setEnabled() will decide whether an item is enabled, which also means
1564 // whether the matching shortcut key will function.
1565 super.onPrepareOptionsMenu(menu);
1566 switch (mMenuState) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001567 case EMPTY_MENU:
1568 if (mCurrentMenuState != mMenuState) {
1569 menu.setGroupVisible(R.id.MAIN_MENU, false);
1570 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1571 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001572 }
1573 break;
1574 default:
1575 if (mCurrentMenuState != mMenuState) {
1576 menu.setGroupVisible(R.id.MAIN_MENU, true);
1577 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1578 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001579 }
1580 final WebView w = getTopWindow();
1581 boolean canGoBack = false;
1582 boolean canGoForward = false;
1583 boolean isHome = false;
1584 if (w != null) {
1585 canGoBack = w.canGoBack();
1586 canGoForward = w.canGoForward();
1587 isHome = mSettings.getHomePage().equals(w.getUrl());
1588 }
1589 final MenuItem back = menu.findItem(R.id.back_menu_id);
1590 back.setEnabled(canGoBack);
1591
1592 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1593 home.setEnabled(!isHome);
1594
1595 menu.findItem(R.id.forward_menu_id)
1596 .setEnabled(canGoForward);
1597
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001598 menu.findItem(R.id.new_tab_menu_id).setEnabled(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001599 mTabControl.canCreateNewTab());
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001600
The Android Open Source Project0c908882009-03-03 19:32:16 -08001601 // decide whether to show the share link option
1602 PackageManager pm = getPackageManager();
1603 Intent send = new Intent(Intent.ACTION_SEND);
1604 send.setType("text/plain");
1605 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1606 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1607
The Android Open Source Project0c908882009-03-03 19:32:16 -08001608 boolean isNavDump = mSettings.isNavDump();
1609 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1610 nav.setVisible(isNavDump);
1611 nav.setEnabled(isNavDump);
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001612
1613 boolean showDebugSettings = mSettings.showDebugSettings();
1614 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1615 counter.setVisible(showDebugSettings);
1616 counter.setEnabled(showDebugSettings);
1617
The Android Open Source Project0c908882009-03-03 19:32:16 -08001618 break;
1619 }
1620 mCurrentMenuState = mMenuState;
1621 return true;
1622 }
1623
1624 @Override
1625 public void onCreateContextMenu(ContextMenu menu, View v,
1626 ContextMenuInfo menuInfo) {
Leon Scroggins571b3762010-05-26 10:25:01 -04001627 if (v instanceof TitleBarBase) {
Leon Scroggins4e9f89b2010-02-22 16:54:14 -05001628 return;
1629 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001630 WebView webview = (WebView) v;
1631 WebView.HitTestResult result = webview.getHitTestResult();
1632 if (result == null) {
1633 return;
1634 }
1635
1636 int type = result.getType();
1637 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1638 Log.w(LOGTAG,
1639 "We should not show context menu when nothing is touched");
1640 return;
1641 }
1642 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1643 // let TextView handles context menu
1644 return;
1645 }
1646
1647 // Note, http://b/issue?id=1106666 is requesting that
1648 // an inflated menu can be used again. This is not available
1649 // yet, so inflate each time (yuk!)
1650 MenuInflater inflater = getMenuInflater();
1651 inflater.inflate(R.menu.browsercontext, menu);
1652
1653 // Show the correct menu group
Leon Scroggins III9e997c72010-05-26 13:25:16 -04001654 final String extra = result.getExtra();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001655 menu.setGroupVisible(R.id.PHONE_MENU,
1656 type == WebView.HitTestResult.PHONE_TYPE);
1657 menu.setGroupVisible(R.id.EMAIL_MENU,
1658 type == WebView.HitTestResult.EMAIL_TYPE);
1659 menu.setGroupVisible(R.id.GEO_MENU,
1660 type == WebView.HitTestResult.GEO_TYPE);
1661 menu.setGroupVisible(R.id.IMAGE_MENU,
1662 type == WebView.HitTestResult.IMAGE_TYPE
1663 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1664 menu.setGroupVisible(R.id.ANCHOR_MENU,
1665 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1666 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1667
1668 // Setup custom handling depending on the type
1669 switch (type) {
1670 case WebView.HitTestResult.PHONE_TYPE:
1671 menu.setHeaderTitle(Uri.decode(extra));
1672 menu.findItem(R.id.dial_context_menu_id).setIntent(
1673 new Intent(Intent.ACTION_VIEW, Uri
1674 .parse(WebView.SCHEME_TEL + extra)));
1675 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1676 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
Cary Clark5e335a32009-09-22 14:53:11 -04001677 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001678 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1679 addIntent);
1680 menu.findItem(R.id.copy_phone_context_menu_id).setOnMenuItemClickListener(
1681 new Copy(extra));
1682 break;
1683
1684 case WebView.HitTestResult.EMAIL_TYPE:
1685 menu.setHeaderTitle(extra);
1686 menu.findItem(R.id.email_context_menu_id).setIntent(
1687 new Intent(Intent.ACTION_VIEW, Uri
1688 .parse(WebView.SCHEME_MAILTO + extra)));
1689 menu.findItem(R.id.copy_mail_context_menu_id).setOnMenuItemClickListener(
1690 new Copy(extra));
1691 break;
1692
1693 case WebView.HitTestResult.GEO_TYPE:
1694 menu.setHeaderTitle(extra);
1695 menu.findItem(R.id.map_context_menu_id).setIntent(
1696 new Intent(Intent.ACTION_VIEW, Uri
1697 .parse(WebView.SCHEME_GEO
1698 + URLEncoder.encode(extra))));
1699 menu.findItem(R.id.copy_geo_context_menu_id).setOnMenuItemClickListener(
1700 new Copy(extra));
1701 break;
1702
1703 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1704 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
1705 TextView titleView = (TextView) LayoutInflater.from(this)
1706 .inflate(android.R.layout.browser_link_context_header,
1707 null);
1708 titleView.setText(extra);
1709 menu.setHeaderView(titleView);
1710 // decide whether to show the open link in new tab option
Leon Scroggins III9e997c72010-05-26 13:25:16 -04001711 boolean showNewTab = mTabControl.canCreateNewTab();
1712 MenuItem newTabItem
1713 = menu.findItem(R.id.open_newtab_context_menu_id);
1714 newTabItem.setVisible(showNewTab);
1715 if (showNewTab) {
1716 newTabItem.setOnMenuItemClickListener(
1717 new MenuItem.OnMenuItemClickListener() {
1718 public boolean onMenuItemClick(MenuItem item) {
1719 final Tab parent = mTabControl.getCurrentTab();
1720 final Tab newTab = openTab(extra);
1721 if (newTab != parent) {
1722 parent.addChildTab(newTab);
1723 }
1724 return true;
1725 }
1726 });
1727 }
Ben Murdochde353622009-10-12 10:29:00 +01001728 menu.findItem(R.id.bookmark_context_menu_id).setVisible(
1729 Bookmarks.urlHasAcceptableScheme(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001730 PackageManager pm = getPackageManager();
1731 Intent send = new Intent(Intent.ACTION_SEND);
1732 send.setType("text/plain");
1733 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1734 menu.findItem(R.id.share_link_context_menu_id).setVisible(ri != null);
1735 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1736 break;
1737 }
1738 // otherwise fall through to handle image part
1739 case WebView.HitTestResult.IMAGE_TYPE:
1740 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1741 menu.setHeaderTitle(extra);
1742 }
1743 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1744 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1745 menu.findItem(R.id.download_context_menu_id).
1746 setOnMenuItemClickListener(new Download(extra));
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001747 menu.findItem(R.id.set_wallpaper_context_menu_id).
1748 setOnMenuItemClickListener(new SetAsWallpaper(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001749 break;
1750
1751 default:
1752 Log.w(LOGTAG, "We should not get here.");
1753 break;
1754 }
Leon Scrogginsb2b19f52009-10-09 16:10:00 -04001755 hideFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001756 }
1757
The Android Open Source Project0c908882009-03-03 19:32:16 -08001758 // Attach the given tab to the content view.
Grace Klobac928c302009-09-17 11:51:21 -07001759 // this should only be called for the current tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001760 private void attachTabToContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001761 // Attach the container that contains the main WebView and any other UI
1762 // associated with the tab.
Patrick Scottd0119532009-09-17 08:00:31 -04001763 t.attachTabToContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001764
1765 if (mShouldShowErrorConsole) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001766 ErrorConsoleView errorConsole = t.getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01001767 if (errorConsole.numberOfErrors() == 0) {
1768 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
1769 } else {
1770 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1771 }
1772
1773 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08001774 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01001775 ViewGroup.LayoutParams.WRAP_CONTENT));
1776 }
1777
Leon Scrogginsd746a942010-05-19 13:21:44 -04001778 if (!mXLargeScreenSize){
1779 WebView view = t.getWebView();
1780 view.setEmbeddedTitleBar(mTitleBar);
1781 }
Leon Scroggins58d56c62010-01-28 15:12:40 -05001782 if (t.isInVoiceSearchMode()) {
1783 showVoiceTitleBar(t.getVoiceDisplayTitle());
1784 } else {
1785 revertVoiceTitleBar();
1786 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001787 // Request focus on the top window.
1788 t.getTopWindow().requestFocus();
1789 }
1790
1791 // Attach a sub window to the main WebView of the given tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001792 void attachSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001793 t.attachSubWindow(mContentView);
1794 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001795 }
1796
1797 // Remove the given tab from the content view.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001798 private void removeTabFromContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001799 // Remove the container that contains the main WebView.
Patrick Scottd0119532009-09-17 08:00:31 -04001800 t.removeTabFromContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001801
Grace Kloba22ac16e2009-10-07 18:00:23 -07001802 ErrorConsoleView errorConsole = t.getErrorConsole(false);
1803 if (errorConsole != null) {
1804 mErrorConsoleContainer.removeView(errorConsole);
Ben Murdochbff2d602009-07-01 20:19:05 +01001805 }
1806
Leon Scrogginsd746a942010-05-19 13:21:44 -04001807 if (!mXLargeScreenSize) {
1808 WebView view = t.getWebView();
1809 if (view != null) {
1810 view.setEmbeddedTitleBar(null);
1811 }
Leon Scrogginsbb85b902009-09-14 19:27:20 -04001812 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001813 }
1814
1815 // Remove the sub window if it exists. Also called by TabControl when the
1816 // user clicks the 'X' to dismiss a sub window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001817 /* package */ void dismissSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001818 t.removeSubWindow(mContentView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001819 // dismiss the subwindow. This will destroy the WebView.
1820 t.dismissSubWindow();
Patrick Scottd0119532009-09-17 08:00:31 -04001821 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001822 }
1823
Leon Scroggins1f005d32009-08-10 17:36:42 -04001824 // A wrapper function of {@link #openTabAndShow(UrlData, boolean, String)}
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07001825 // that accepts url as string.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001826 private Tab openTabAndShow(String url, boolean closeOnExit, String appId) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001827 return openTabAndShow(new UrlData(url), closeOnExit, appId);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001828 }
1829
1830 // This method does a ton of stuff. It will attempt to create a new tab
1831 // if we haven't reached MAX_TABS. Otherwise it uses the current tab. If
Leon Scroggins1f005d32009-08-10 17:36:42 -04001832 // url isn't null, it will load the given url.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001833 /* package */Tab openTabAndShow(UrlData urlData, boolean closeOnExit,
1834 String appId) {
1835 final Tab currentTab = mTabControl.getCurrentTab();
1836 if (mTabControl.canCreateNewTab()) {
1837 final Tab tab = mTabControl.createNewTab(closeOnExit, appId,
1838 urlData.mUrl);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001839 WebView webview = tab.getWebView();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001840 // If the last tab was removed from the active tabs page, currentTab
1841 // will be null.
1842 if (currentTab != null) {
1843 removeTabFromContentView(currentTab);
1844 }
Patrick Scott8bbd69f2009-08-14 13:35:53 -04001845 // We must set the new tab as the current tab to reflect the old
1846 // animation behavior.
1847 mTabControl.setCurrentTab(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001848 attachTabToContentView(tab);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001849 if (!urlData.isEmpty()) {
Patrick Scott9d53da02010-02-19 10:19:01 -05001850 loadUrlDataIn(tab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001851 }
1852 return tab;
1853 } else {
1854 // Get rid of the subwindow if it exists
1855 dismissSubWindow(currentTab);
1856 if (!urlData.isEmpty()) {
1857 // Load the given url.
Patrick Scott9d53da02010-02-19 10:19:01 -05001858 loadUrlDataIn(currentTab, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001859 }
Leon Scroggins58d56c62010-01-28 15:12:40 -05001860 return currentTab;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001861 }
1862 }
1863
Grace Kloba22ac16e2009-10-07 18:00:23 -07001864 private Tab openTab(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001865 if (mSettings.openInBackground()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001866 Tab t = mTabControl.createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001867 if (t != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001868 WebView view = t.getWebView();
Leon Scroggins92472e82010-02-17 16:32:28 -05001869 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001870 }
Grace Klobac9181842009-04-14 08:53:22 -07001871 return t;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001872 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001873 return openTabAndShow(url, false, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001874 }
1875 }
1876
1877 private class Copy implements OnMenuItemClickListener {
1878 private CharSequence mText;
1879
1880 public boolean onMenuItemClick(MenuItem item) {
1881 copy(mText);
1882 return true;
1883 }
1884
1885 public Copy(CharSequence toCopy) {
1886 mText = toCopy;
1887 }
1888 }
1889
1890 private class Download implements OnMenuItemClickListener {
1891 private String mText;
1892
1893 public boolean onMenuItemClick(MenuItem item) {
1894 onDownloadStartNoStream(mText, null, null, null, -1);
1895 return true;
1896 }
1897
1898 public Download(String toDownload) {
1899 mText = toDownload;
1900 }
1901 }
1902
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001903 private class SetAsWallpaper extends Thread implements
1904 OnMenuItemClickListener, DialogInterface.OnCancelListener {
1905 private URL mUrl;
1906 private ProgressDialog mWallpaperProgress;
1907 private boolean mCanceled = false;
1908
1909 public SetAsWallpaper(String url) {
1910 try {
1911 mUrl = new URL(url);
1912 } catch (MalformedURLException e) {
1913 mUrl = null;
1914 }
1915 }
1916
1917 public void onCancel(DialogInterface dialog) {
1918 mCanceled = true;
1919 }
1920
1921 public boolean onMenuItemClick(MenuItem item) {
1922 if (mUrl != null) {
1923 // The user may have tried to set a image with a large file size as their
1924 // background so it may take a few moments to perform the operation. Display
1925 // a progress spinner while it is working.
1926 mWallpaperProgress = new ProgressDialog(BrowserActivity.this);
1927 mWallpaperProgress.setIndeterminate(true);
1928 mWallpaperProgress.setMessage(getText(R.string.progress_dialog_setting_wallpaper));
1929 mWallpaperProgress.setCancelable(true);
1930 mWallpaperProgress.setOnCancelListener(this);
1931 mWallpaperProgress.show();
1932 start();
1933 }
1934 return true;
1935 }
1936
1937 public void run() {
1938 Drawable oldWallpaper = BrowserActivity.this.getWallpaper();
1939 try {
1940 // TODO: This will cause the resource to be downloaded again, when we
1941 // should in most cases be able to grab it from the cache. To fix this
1942 // we should query WebCore to see if we can access a cached version and
1943 // instead open an input stream on that. This pattern could also be used
1944 // in the download manager where the same problem exists.
1945 InputStream inputstream = mUrl.openStream();
1946 if (inputstream != null) {
1947 setWallpaper(inputstream);
1948 }
1949 } catch (IOException e) {
1950 Log.e(LOGTAG, "Unable to set new wallpaper");
1951 // Act as though the user canceled the operation so we try to
1952 // restore the old wallpaper.
1953 mCanceled = true;
1954 }
1955
1956 if (mCanceled) {
1957 // Restore the old wallpaper if the user cancelled whilst we were setting
1958 // the new wallpaper.
1959 int width = oldWallpaper.getIntrinsicWidth();
1960 int height = oldWallpaper.getIntrinsicHeight();
1961 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1962 Canvas canvas = new Canvas(bm);
1963 oldWallpaper.setBounds(0, 0, width, height);
1964 oldWallpaper.draw(canvas);
1965 try {
1966 setWallpaper(bm);
1967 } catch (IOException e) {
1968 Log.e(LOGTAG, "Unable to restore old wallpaper.");
1969 }
1970 mCanceled = false;
1971 }
1972
1973 if (mWallpaperProgress.isShowing()) {
1974 mWallpaperProgress.dismiss();
1975 }
1976 }
1977 }
1978
The Android Open Source Project0c908882009-03-03 19:32:16 -08001979 private void copy(CharSequence text) {
1980 try {
1981 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
1982 if (clip != null) {
1983 clip.setClipboardText(text);
1984 }
1985 } catch (android.os.RemoteException e) {
1986 Log.e(LOGTAG, "Copy failed", e);
1987 }
1988 }
1989
1990 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001991 * Resets the browser title-view to whatever it must be
1992 * (for example, if we had a loading error)
1993 * When we have a new page, we call resetTitle, when we
1994 * have to reset the titlebar to whatever it used to be
1995 * (for example, if the user chose to stop loading), we
1996 * call resetTitleAndRevertLockIcon.
1997 */
1998 /* package */ void resetTitleAndRevertLockIcon() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001999 mTabControl.getCurrentTab().revertLockIcon();
2000 updateLockIconToLatest();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002001 resetTitleIconAndProgress();
2002 }
2003
2004 /**
2005 * Reset the title, favicon, and progress.
2006 */
2007 private void resetTitleIconAndProgress() {
2008 WebView current = mTabControl.getCurrentWebView();
2009 if (current == null) {
2010 return;
2011 }
2012 resetTitleAndIcon(current);
2013 int progress = current.getProgress();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002014 current.getWebChromeClient().onProgressChanged(current, progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002015 }
2016
2017 // Reset the title and the icon based on the given item.
2018 private void resetTitleAndIcon(WebView view) {
2019 WebHistoryItem item = view.copyBackForwardList().getCurrentItem();
2020 if (item != null) {
Leon Scroggins68579392009-09-15 15:31:54 -04002021 setUrlTitle(item.getUrl(), item.getTitle());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002022 setFavicon(item.getFavicon());
2023 } else {
Leon Scroggins68579392009-09-15 15:31:54 -04002024 setUrlTitle(null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002025 setFavicon(null);
2026 }
2027 }
2028
2029 /**
2030 * Sets a title composed of the URL and the title string.
2031 * @param url The URL of the site being loaded.
2032 * @param title The title of the site being loaded.
2033 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002034 void setUrlTitle(String url, String title) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002035 mUrl = url;
2036 mTitle = title;
2037
Leon Scroggins58d56c62010-01-28 15:12:40 -05002038 // If we are in voice search mode, the title has already been set.
2039 if (mTabControl.getCurrentTab().isInVoiceSearchMode()) return;
2040 mTitleBar.setDisplayTitle(url);
Leon Scrogginsd746a942010-05-19 13:21:44 -04002041 if (!mXLargeScreenSize) {
2042 mFakeTitleBar.setDisplayTitle(url);
2043 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002044 }
2045
2046 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08002047 * @param url The URL to build a title version of the URL from.
2048 * @return The title version of the URL or null if fails.
2049 * The title version of the URL can be either the URL hostname,
2050 * or the hostname with an "https://" prefix (for secure URLs),
2051 * or an empty string if, for example, the URL in question is a
2052 * file:// URL with no hostname.
2053 */
Leon Scroggins32e14a62009-06-11 10:26:34 -04002054 /* package */ static String buildTitleUrl(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002055 String titleUrl = null;
2056
2057 if (url != null) {
2058 try {
2059 // parse the url string
2060 URL urlObj = new URL(url);
2061 if (urlObj != null) {
2062 titleUrl = "";
2063
2064 String protocol = urlObj.getProtocol();
2065 String host = urlObj.getHost();
2066
2067 if (host != null && 0 < host.length()) {
2068 titleUrl = host;
2069 if (protocol != null) {
2070 // if a secure site, add an "https://" prefix!
2071 if (protocol.equalsIgnoreCase("https")) {
2072 titleUrl = protocol + "://" + host;
2073 }
2074 }
2075 }
2076 }
2077 } catch (MalformedURLException e) {}
2078 }
2079
2080 return titleUrl;
2081 }
2082
2083 // Set the favicon in the title bar.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002084 void setFavicon(Bitmap icon) {
Leon Scroggins68579392009-09-15 15:31:54 -04002085 mTitleBar.setFavicon(icon);
Leon Scrogginsd746a942010-05-19 13:21:44 -04002086 if (!mXLargeScreenSize) {
2087 mFakeTitleBar.setFavicon(icon);
2088 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002089 }
2090
2091 /**
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002092 * Close the tab, remove its associated title bar, and adjust mTabControl's
2093 * current tab to a valid value.
Leon Scroggins1f005d32009-08-10 17:36:42 -04002094 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002095 /* package */ void closeTab(Tab t) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002096 int currentIndex = mTabControl.getCurrentIndex();
2097 int removeIndex = mTabControl.getTabIndex(t);
Leon Scroggins1f005d32009-08-10 17:36:42 -04002098 mTabControl.removeTab(t);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002099 if (currentIndex >= removeIndex && currentIndex != 0) {
2100 currentIndex--;
2101 }
2102 mTabControl.setCurrentTab(mTabControl.getTab(currentIndex));
Andrei Popescua5bf1de2009-09-23 16:39:23 +01002103 resetTitleIconAndProgress();
Leon Scroggins654899b2010-06-25 16:25:23 -04002104 updateLockIconToLatest();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002105 }
2106
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05002107 /* package */ void goBackOnePageOrQuit() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002108 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002109 if (current == null) {
2110 /*
2111 * Instead of finishing the activity, simply push this to the back
2112 * of the stack and let ActivityManager to choose the foreground
2113 * activity. As BrowserActivity is singleTask, it will be always the
2114 * root of the task. So we can use either true or false for
2115 * moveTaskToBack().
2116 */
2117 moveTaskToBack(true);
Grace Kloba00d85e72009-09-23 18:50:05 -07002118 return;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002119 }
2120 WebView w = current.getWebView();
2121 if (w.canGoBack()) {
2122 w.goBack();
2123 } else {
2124 // Check to see if we are closing a window that was created by
2125 // another window. If so, we switch back to that window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002126 Tab parent = current.getParentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002127 if (parent != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04002128 switchToTab(mTabControl.getTabIndex(parent));
2129 // Now we close the other tab
2130 closeTab(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002131 } else {
2132 if (current.closeOnExit()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002133 // force the tab's inLoad() to be false as we are going to
2134 // either finish the activity or remove the tab. This will
2135 // ensure pauseWebViewTimers() taking action.
2136 mTabControl.getCurrentTab().clearInLoad();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002137 if (mTabControl.getTabCount() == 1) {
2138 finish();
2139 return;
2140 }
Mike Reed7bfa63b2009-05-28 11:08:32 -04002141 // call pauseWebViewTimers() now, we won't be able to call
2142 // it in onPause() as the WebView won't be valid.
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002143 // Temporarily change mActivityInPause to be true as
2144 // pauseWebViewTimers() will do nothing if mActivityInPause
2145 // is false.
Grace Kloba918e1d72009-08-13 14:55:06 -07002146 boolean savedState = mActivityInPause;
2147 if (savedState) {
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002148 Log.e(LOGTAG, "BrowserActivity is already paused "
2149 + "while handing goBackOnePageOrQuit.");
Grace Kloba918e1d72009-08-13 14:55:06 -07002150 }
2151 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -04002152 pauseWebViewTimers();
Grace Kloba918e1d72009-08-13 14:55:06 -07002153 mActivityInPause = savedState;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002154 removeTabFromContentView(current);
2155 mTabControl.removeTab(current);
2156 }
2157 /*
2158 * Instead of finishing the activity, simply push this to the back
2159 * of the stack and let ActivityManager to choose the foreground
2160 * activity. As BrowserActivity is singleTask, it will be always the
2161 * root of the task. So we can use either true or false for
2162 * moveTaskToBack().
2163 */
2164 moveTaskToBack(true);
2165 }
2166 }
2167 }
2168
Grace Kloba22ac16e2009-10-07 18:00:23 -07002169 boolean isMenuDown() {
2170 return mMenuIsDown;
2171 }
2172
Grace Kloba5942df02009-09-18 11:48:29 -07002173 @Override
2174 public boolean onKeyDown(int keyCode, KeyEvent event) {
Leon Scrogginsf65b50d2009-12-08 10:44:28 -05002175 // Even if MENU is already held down, we need to call to super to open
2176 // the IME on long press.
2177 if (KeyEvent.KEYCODE_MENU == keyCode) {
2178 mMenuIsDown = true;
2179 return super.onKeyDown(keyCode, event);
2180 }
Grace Kloba5942df02009-09-18 11:48:29 -07002181 // The default key mode is DEFAULT_KEYS_SEARCH_LOCAL. As the MENU is
2182 // still down, we don't want to trigger the search. Pretend to consume
2183 // the key and do nothing.
2184 if (mMenuIsDown) return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002185
Grace Kloba5942df02009-09-18 11:48:29 -07002186 switch(keyCode) {
Grace Kloba5942df02009-09-18 11:48:29 -07002187 case KeyEvent.KEYCODE_SPACE:
Grace Klobada0fe552009-09-22 18:17:24 -07002188 // WebView/WebTextView handle the keys in the KeyDown. As
2189 // the Activity's shortcut keys are only handled when WebView
2190 // doesn't, have to do it in onKeyDown instead of onKeyUp.
2191 if (event.isShiftPressed()) {
2192 getTopWindow().pageUp(false);
2193 } else {
2194 getTopWindow().pageDown(false);
2195 }
Grace Kloba5942df02009-09-18 11:48:29 -07002196 return true;
2197 case KeyEvent.KEYCODE_BACK:
2198 if (event.getRepeatCount() == 0) {
2199 event.startTracking();
2200 return true;
2201 } else if (mCustomView == null && mActiveTabsPage == null
2202 && event.isLongPress()) {
2203 bookmarksOrHistoryPicker(true);
2204 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002205 }
Grace Kloba5942df02009-09-18 11:48:29 -07002206 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002207 }
Grace Kloba5942df02009-09-18 11:48:29 -07002208 return super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002209 }
2210
Grace Kloba5942df02009-09-18 11:48:29 -07002211 @Override
2212 public boolean onKeyUp(int keyCode, KeyEvent event) {
2213 switch(keyCode) {
2214 case KeyEvent.KEYCODE_MENU:
2215 mMenuIsDown = false;
2216 break;
Grace Kloba5942df02009-09-18 11:48:29 -07002217 case KeyEvent.KEYCODE_BACK:
2218 if (event.isTracking() && !event.isCanceled()) {
2219 if (mCustomView != null) {
2220 // if a custom view is showing, hide it
Grace Kloba22ac16e2009-10-07 18:00:23 -07002221 mTabControl.getCurrentWebView().getWebChromeClient()
2222 .onHideCustomView();
Grace Kloba5942df02009-09-18 11:48:29 -07002223 } else if (mActiveTabsPage != null) {
2224 // if tab page is showing, hide it
2225 removeActiveTabPage(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002226 } else {
Grace Kloba5942df02009-09-18 11:48:29 -07002227 WebView subwindow = mTabControl.getCurrentSubWindow();
2228 if (subwindow != null) {
2229 if (subwindow.canGoBack()) {
2230 subwindow.goBack();
2231 } else {
2232 dismissSubWindow(mTabControl.getCurrentTab());
2233 }
2234 } else {
2235 goBackOnePageOrQuit();
2236 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002237 }
Grace Kloba5942df02009-09-18 11:48:29 -07002238 return true;
2239 }
2240 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002241 }
Grace Kloba5942df02009-09-18 11:48:29 -07002242 return super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002243 }
2244
Leon Scroggins68579392009-09-15 15:31:54 -04002245 /* package */ void stopLoading() {
Ben Murdochb7cc8b42009-09-28 10:59:09 +01002246 mDidStopLoad = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002247 resetTitleAndRevertLockIcon();
2248 WebView w = getTopWindow();
2249 w.stopLoading();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002250 // FIXME: before refactor, it is using mWebViewClient. So I keep the
2251 // same logic here. But for subwindow case, should we call into the main
2252 // WebView's onPageFinished as we never call its onPageStarted and if
2253 // the page finishes itself, we don't call onPageFinished.
2254 mTabControl.getCurrentWebView().getWebViewClient().onPageFinished(w,
2255 w.getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002256
2257 cancelStopToast();
2258 mStopToast = Toast
2259 .makeText(this, R.string.stopping, Toast.LENGTH_SHORT);
2260 mStopToast.show();
2261 }
2262
Grace Kloba22ac16e2009-10-07 18:00:23 -07002263 boolean didUserStopLoading() {
2264 return mDidStopLoad;
2265 }
2266
The Android Open Source Project0c908882009-03-03 19:32:16 -08002267 private void cancelStopToast() {
2268 if (mStopToast != null) {
2269 mStopToast.cancel();
2270 mStopToast = null;
2271 }
2272 }
2273
Grace Kloba22ac16e2009-10-07 18:00:23 -07002274 // called by a UI or non-UI thread to post the message
2275 public void postMessage(int what, int arg1, int arg2, Object obj,
2276 long delayMillis) {
2277 mHandler.sendMessageDelayed(mHandler.obtainMessage(what, arg1, arg2,
2278 obj), delayMillis);
2279 }
2280
2281 // called by a UI or non-UI thread to remove the message
2282 void removeMessages(int what, Object object) {
2283 mHandler.removeMessages(what, object);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002284 }
2285
2286 // public message ids
2287 public final static int LOAD_URL = 1001;
2288 public final static int STOP_LOAD = 1002;
2289
2290 // Message Ids
2291 private static final int FOCUS_NODE_HREF = 102;
Grace Kloba92c18a52009-07-31 23:48:32 -07002292 private static final int RELEASE_WAKELOCK = 107;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002293
Grace Kloba22ac16e2009-10-07 18:00:23 -07002294 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
Ben Murdoch2694e232009-09-29 09:41:11 +01002295
The Android Open Source Project0c908882009-03-03 19:32:16 -08002296 // Private handler for handling javascript and saving passwords
2297 private Handler mHandler = new Handler() {
2298
2299 public void handleMessage(Message msg) {
2300 switch (msg.what) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002301 case FOCUS_NODE_HREF:
Ben Murdoch2694e232009-09-29 09:41:11 +01002302 {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002303 String url = (String) msg.getData().get("url");
Ben Murdoch90d088c2009-11-17 18:14:04 +00002304 String title = (String) msg.getData().get("title");
The Android Open Source Project0c908882009-03-03 19:32:16 -08002305 if (url == null || url.length() == 0) {
2306 break;
2307 }
2308 HashMap focusNodeMap = (HashMap) msg.obj;
2309 WebView view = (WebView) focusNodeMap.get("webview");
2310 // Only apply the action if the top window did not change.
2311 if (getTopWindow() != view) {
2312 break;
2313 }
2314 switch (msg.arg1) {
2315 case R.id.open_context_menu_id:
2316 case R.id.view_image_context_menu_id:
Leon Scroggins92472e82010-02-17 16:32:28 -05002317 loadUrlFromContext(getTopWindow(), url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002318 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002319 case R.id.bookmark_context_menu_id:
2320 Intent intent = new Intent(BrowserActivity.this,
2321 AddBookmarkPage.class);
2322 intent.putExtra("url", url);
Ben Murdoch90d088c2009-11-17 18:14:04 +00002323 intent.putExtra("title", title);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002324 startActivity(intent);
2325 break;
2326 case R.id.share_link_context_menu_id:
Leon Scroggins3e204452010-05-10 11:06:03 -04002327 sharePage(BrowserActivity.this, title, url, null,
Leon Scroggins96afcb12009-12-10 12:35:56 -05002328 null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002329 break;
2330 case R.id.copy_link_context_menu_id:
2331 copy(url);
2332 break;
2333 case R.id.save_link_context_menu_id:
2334 case R.id.download_context_menu_id:
2335 onDownloadStartNoStream(url, null, null, null, -1);
2336 break;
2337 }
2338 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002339 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002340
2341 case LOAD_URL:
Leon Scroggins92472e82010-02-17 16:32:28 -05002342 loadUrlFromContext(getTopWindow(), (String) msg.obj);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002343 break;
2344
2345 case STOP_LOAD:
2346 stopLoading();
2347 break;
2348
The Android Open Source Project0c908882009-03-03 19:32:16 -08002349 case RELEASE_WAKELOCK:
2350 if (mWakeLock.isHeld()) {
2351 mWakeLock.release();
Grace Kloba5d0e02e2009-10-05 15:15:36 -07002352 // if we reach here, Browser should be still in the
2353 // background loading after WAKELOCK_TIMEOUT (5-min).
2354 // To avoid burning the battery, stop loading.
2355 mTabControl.stopAllLoading();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002356 }
2357 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002358
2359 case UPDATE_BOOKMARK_THUMBNAIL:
2360 WebView view = (WebView) msg.obj;
2361 if (view != null) {
2362 updateScreenshot(view);
2363 }
2364 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002365 }
2366 }
2367 };
2368
Leon Scroggins96afcb12009-12-10 12:35:56 -05002369 /**
2370 * Share a page, providing the title, url, favicon, and a screenshot. Uses
2371 * an {@link Intent} to launch the Activity chooser.
2372 * @param c Context used to launch a new Activity.
2373 * @param title Title of the page. Stored in the Intent with
Paul Westbrook03e6d392010-02-12 10:33:29 -08002374 * {@link Intent#EXTRA_SUBJECT}
Leon Scroggins96afcb12009-12-10 12:35:56 -05002375 * @param url URL of the page. Stored in the Intent with
2376 * {@link Intent#EXTRA_TEXT}
2377 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
2378 * with {@link Browser#EXTRA_SHARE_FAVICON}
2379 * @param screenshot Bitmap of a screenshot of the page. Stored in the
2380 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
2381 */
2382 public static final void sharePage(Context c, String title, String url,
2383 Bitmap favicon, Bitmap screenshot) {
2384 Intent send = new Intent(Intent.ACTION_SEND);
2385 send.setType("text/plain");
2386 send.putExtra(Intent.EXTRA_TEXT, url);
Paul Westbrook03e6d392010-02-12 10:33:29 -08002387 send.putExtra(Intent.EXTRA_SUBJECT, title);
Leon Scroggins96afcb12009-12-10 12:35:56 -05002388 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
2389 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
2390 try {
2391 c.startActivity(Intent.createChooser(send, c.getString(
2392 R.string.choosertitle_sharevia)));
2393 } catch(android.content.ActivityNotFoundException ex) {
2394 // if no app handles it, do nothing
2395 }
2396 }
2397
Leon Scroggins89c6d362009-07-15 16:54:37 -04002398 private void updateScreenshot(WebView view) {
2399 // If this is a bookmarked site, add a screenshot to the database.
2400 // FIXME: When should we update? Every time?
2401 // FIXME: Would like to make sure there is actually something to
2402 // draw, but the API for that (WebViewCore.pictureReady()) is not
2403 // currently accessible here.
Ben Murdochaac7aa62009-09-17 16:57:40 +01002404
Patrick Scottcb192b52010-04-14 14:38:55 -04002405 final Bitmap bm = createScreenshot(view);
2406 if (bm == null) {
2407 return;
Leon Scroggins89c6d362009-07-15 16:54:37 -04002408 }
Patrick Scottcb192b52010-04-14 14:38:55 -04002409
2410 final ContentResolver cr = getContentResolver();
2411 final String url = view.getUrl();
2412 final String originalUrl = view.getOriginalUrl();
2413
2414 new AsyncTask<Void, Void, Void>() {
2415 @Override
2416 protected Void doInBackground(Void... unused) {
2417 Cursor c = null;
2418 try {
2419 c = BrowserBookmarksAdapter.queryBookmarksForUrl(
2420 cr, originalUrl, url, true);
2421 if (c != null) {
2422 if (c.moveToFirst()) {
2423 ContentValues values = new ContentValues();
2424 final ByteArrayOutputStream os
2425 = new ByteArrayOutputStream();
2426 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
2427 values.put(Browser.BookmarkColumns.THUMBNAIL,
2428 os.toByteArray());
2429 do {
2430 cr.update(ContentUris.withAppendedId(
2431 Browser.BOOKMARKS_URI, c.getInt(0)),
2432 values, null, null);
2433 } while (c.moveToNext());
2434 }
2435 }
2436 } catch (IllegalStateException e) {
2437 // Ignore
2438 } finally {
2439 if (c != null) c.close();
2440 }
2441 return null;
2442 }
2443 }.execute();
Leon Scroggins89c6d362009-07-15 16:54:37 -04002444 }
2445
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002446 /**
Leon Scrogginsf8551612009-09-24 16:06:02 -04002447 * Values for the size of the thumbnail created when taking a screenshot.
2448 * Lazily initialized. Instead of using these directly, use
2449 * getDesiredThumbnailWidth() or getDesiredThumbnailHeight().
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002450 */
Leon Scrogginsf8551612009-09-24 16:06:02 -04002451 private static int THUMBNAIL_WIDTH = 0;
2452 private static int THUMBNAIL_HEIGHT = 0;
2453
2454 /**
2455 * Return the desired width for thumbnail screenshots, which are stored in
2456 * the database, and used on the bookmarks screen.
2457 * @param context Context for finding out the density of the screen.
2458 * @return int desired width for thumbnail screenshot.
2459 */
2460 /* package */ static int getDesiredThumbnailWidth(Context context) {
2461 if (THUMBNAIL_WIDTH == 0) {
2462 float density = context.getResources().getDisplayMetrics().density;
2463 THUMBNAIL_WIDTH = (int) (90 * density);
2464 THUMBNAIL_HEIGHT = (int) (80 * density);
2465 }
2466 return THUMBNAIL_WIDTH;
2467 }
2468
2469 /**
2470 * Return the desired height for thumbnail screenshots, which are stored in
2471 * the database, and used on the bookmarks screen.
2472 * @param context Context for finding out the density of the screen.
2473 * @return int desired height for thumbnail screenshot.
2474 */
2475 /* package */ static int getDesiredThumbnailHeight(Context context) {
2476 // To ensure that they are both initialized.
2477 getDesiredThumbnailWidth(context);
2478 return THUMBNAIL_HEIGHT;
2479 }
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002480
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002481 private Bitmap createScreenshot(WebView view) {
2482 Picture thumbnail = view.capturePicture();
Leon Scroggins45800572009-09-29 16:38:47 -04002483 if (thumbnail == null) {
2484 return null;
2485 }
Leon Scrogginsf8551612009-09-24 16:06:02 -04002486 Bitmap bm = Bitmap.createBitmap(getDesiredThumbnailWidth(this),
Cary Clarkab168ba2010-04-08 09:11:28 -04002487 getDesiredThumbnailHeight(this), Bitmap.Config.RGB_565);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002488 Canvas canvas = new Canvas(bm);
2489 // May need to tweak these values to determine what is the
2490 // best scale factor
Ben Murdoch2694e232009-09-29 09:41:11 +01002491 int thumbnailWidth = thumbnail.getWidth();
Ben Murdochae59c3f2009-10-20 18:30:28 +01002492 int thumbnailHeight = thumbnail.getHeight();
2493 float scaleFactorX = 1.0f;
2494 float scaleFactorY = 1.0f;
Ben Murdoch2694e232009-09-29 09:41:11 +01002495 if (thumbnailWidth > 0) {
Ben Murdochae59c3f2009-10-20 18:30:28 +01002496 scaleFactorX = (float) getDesiredThumbnailWidth(this) /
Ben Murdoch2694e232009-09-29 09:41:11 +01002497 (float)thumbnailWidth;
Ben Murdochae59c3f2009-10-20 18:30:28 +01002498 } else {
2499 return null;
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002500 }
Ben Murdochae59c3f2009-10-20 18:30:28 +01002501
2502 if (view.getWidth() > view.getHeight() &&
2503 thumbnailHeight < view.getHeight() && thumbnailHeight > 0) {
2504 // If the device is in landscape and the page is shorter
2505 // than the height of the view, stretch the thumbnail to fill the
2506 // space.
2507 scaleFactorY = (float) getDesiredThumbnailHeight(this) /
2508 (float)thumbnailHeight;
2509 } else {
2510 // In the portrait case, this looks nice.
2511 scaleFactorY = scaleFactorX;
2512 }
2513
2514 canvas.scale(scaleFactorX, scaleFactorY);
2515
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002516 thumbnail.draw(canvas);
2517 return bm;
2518 }
2519
The Android Open Source Project0c908882009-03-03 19:32:16 -08002520 // -------------------------------------------------------------------------
Grace Kloba22ac16e2009-10-07 18:00:23 -07002521 // Helper function for WebViewClient.
The Android Open Source Project0c908882009-03-03 19:32:16 -08002522 //-------------------------------------------------------------------------
2523
2524 // Use in overrideUrlLoading
2525 /* package */ final static String SCHEME_WTAI = "wtai://wp/";
2526 /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
2527 /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;";
2528 /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;";
2529
Leon Scroggins92472e82010-02-17 16:32:28 -05002530 // Keep this initial progress in sync with initialProgressValue (* 100)
2531 // in ProgressTracker.cpp
2532 private final static int INITIAL_PROGRESS = 10;
2533
Grace Kloba22ac16e2009-10-07 18:00:23 -07002534 void onPageStarted(WebView view, String url, Bitmap favicon) {
2535 // when BrowserActivity just starts, onPageStarted may be called before
2536 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
2537 // to start the timer. As we won't switch tabs while an activity is in
2538 // pause state, we can ensure calling resume and pause in pair.
2539 if (mActivityInPause) resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002540
Grace Kloba22ac16e2009-10-07 18:00:23 -07002541 resetLockIcon(url);
2542 setUrlTitle(url, null);
2543 setFavicon(favicon);
Leon Scroggins8cf8f682009-11-04 11:13:50 -08002544 // Show some progress so that the user knows the page is beginning to
2545 // load
Leon Scroggins92472e82010-02-17 16:32:28 -05002546 onProgressChanged(view, INITIAL_PROGRESS);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002547 mDidStopLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07002548 if (!mIsNetworkUp) createAndShowNetworkDialog();
Cary Clark01cfcdd2010-06-04 16:36:45 -04002549 closeDialogs();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002550 if (mSettings.isTracing()) {
2551 String host;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002552 try {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002553 WebAddress uri = new WebAddress(url);
2554 host = uri.mHost;
2555 } catch (android.net.ParseException ex) {
2556 host = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -08002557 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002558 host = host.replace('.', '_');
2559 host += ".trace";
2560 mInTrace = true;
2561 Debug.startMethodTracing(host, 20 * 1024 * 1024);
2562 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002563
Grace Kloba22ac16e2009-10-07 18:00:23 -07002564 // Performance probe
2565 if (false) {
2566 mStart = SystemClock.uptimeMillis();
2567 mProcessStart = Process.getElapsedCpuTime();
2568 long[] sysCpu = new long[7];
2569 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2570 sysCpu, null)) {
2571 mUserStart = sysCpu[0] + sysCpu[1];
2572 mSystemStart = sysCpu[2];
2573 mIdleStart = sysCpu[3];
2574 mIrqStart = sysCpu[4] + sysCpu[5] + sysCpu[6];
2575 }
2576 mUiStart = SystemClock.currentThreadTimeMillis();
2577 }
2578 }
2579
2580 void onPageFinished(WebView view, String url) {
2581 // Reset the title and icon in case we stopped a provisional load.
2582 resetTitleAndIcon(view);
2583 // Update the lock icon image only once we are done loading
2584 updateLockIconToLatest();
2585 // pause the WebView timer and release the wake lock if it is finished
2586 // while BrowserActivity is in pause state.
2587 if (mActivityInPause && pauseWebViewTimers()) {
2588 if (mWakeLock.isHeld()) {
2589 mHandler.removeMessages(RELEASE_WAKELOCK);
2590 mWakeLock.release();
2591 }
2592 }
2593
2594 // Performance probe
2595 if (false) {
2596 long[] sysCpu = new long[7];
2597 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2598 sysCpu, null)) {
2599 String uiInfo = "UI thread used "
2600 + (SystemClock.currentThreadTimeMillis() - mUiStart)
2601 + " ms";
2602 if (LOGD_ENABLED) {
2603 Log.d(LOGTAG, uiInfo);
2604 }
2605 //The string that gets written to the log
2606 String performanceString = "It took total "
2607 + (SystemClock.uptimeMillis() - mStart)
2608 + " ms clock time to load the page."
2609 + "\nbrowser process used "
2610 + (Process.getElapsedCpuTime() - mProcessStart)
2611 + " ms, user processes used "
2612 + (sysCpu[0] + sysCpu[1] - mUserStart) * 10
2613 + " ms, kernel used "
2614 + (sysCpu[2] - mSystemStart) * 10
2615 + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10
2616 + " ms and irq took "
2617 + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart)
2618 * 10 + " ms, " + uiInfo;
2619 if (LOGD_ENABLED) {
2620 Log.d(LOGTAG, performanceString + "\nWebpage: " + url);
2621 }
2622 if (url != null) {
2623 // strip the url to maintain consistency
2624 String newUrl = new String(url);
2625 if (newUrl.startsWith("http://www.")) {
2626 newUrl = newUrl.substring(11);
2627 } else if (newUrl.startsWith("http://")) {
2628 newUrl = newUrl.substring(7);
2629 } else if (newUrl.startsWith("https://www.")) {
2630 newUrl = newUrl.substring(12);
2631 } else if (newUrl.startsWith("https://")) {
2632 newUrl = newUrl.substring(8);
2633 }
2634 if (LOGD_ENABLED) {
2635 Log.d(LOGTAG, newUrl + " loaded");
2636 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002637 }
2638 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002639 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002640
Grace Kloba22ac16e2009-10-07 18:00:23 -07002641 if (mInTrace) {
2642 mInTrace = false;
2643 Debug.stopMethodTracing();
2644 }
2645 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002646
Grace Klobae7fe26b2010-06-28 16:23:33 -07002647 private void closeEmptyChildTab() {
2648 Tab current = mTabControl.getCurrentTab();
2649 if (current != null
2650 && current.getWebView().copyBackForwardList().getSize() == 0) {
2651 Tab parent = current.getParentTab();
2652 if (parent != null) {
2653 switchToTab(mTabControl.getTabIndex(parent));
2654 closeTab(current);
2655 }
2656 }
2657 }
2658
Grace Kloba22ac16e2009-10-07 18:00:23 -07002659 boolean shouldOverrideUrlLoading(WebView view, String url) {
2660 if (url.startsWith(SCHEME_WTAI)) {
2661 // wtai://wp/mc;number
2662 // number=string(phone-number)
2663 if (url.startsWith(SCHEME_WTAI_MC)) {
2664 Intent intent = new Intent(Intent.ACTION_VIEW,
2665 Uri.parse(WebView.SCHEME_TEL +
2666 url.substring(SCHEME_WTAI_MC.length())));
2667 startActivity(intent);
Grace Klobae7fe26b2010-06-28 16:23:33 -07002668 // before leaving BrowserActivity, close the empty child tab.
2669 // If a new tab is created through JavaScript open to load this
2670 // url, we would like to close it as we will load this url in a
2671 // different Activity.
2672 closeEmptyChildTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002673 return true;
2674 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002675 // wtai://wp/sd;dtmf
2676 // dtmf=string(dialstring)
2677 if (url.startsWith(SCHEME_WTAI_SD)) {
2678 // TODO: only send when there is active voice connection
2679 return false;
2680 }
2681 // wtai://wp/ap;number;name
2682 // number=string(phone-number)
2683 // name=string
2684 if (url.startsWith(SCHEME_WTAI_AP)) {
2685 // TODO
2686 return false;
2687 }
2688 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002689
Grace Kloba22ac16e2009-10-07 18:00:23 -07002690 // The "about:" schemes are internal to the browser; don't want these to
2691 // be dispatched to other apps.
2692 if (url.startsWith("about:")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002693 return false;
2694 }
2695
Grace Kloba22ac16e2009-10-07 18:00:23 -07002696 Intent intent;
2697 // perform generic parsing of the URI to turn it into an Intent.
2698 try {
2699 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
2700 } catch (URISyntaxException ex) {
2701 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
2702 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002703 }
2704
Grace Kloba22ac16e2009-10-07 18:00:23 -07002705 // check whether the intent can be resolved. If not, we will see
2706 // whether we can download it from the Market.
2707 if (getPackageManager().resolveActivity(intent, 0) == null) {
2708 String packagename = intent.getPackage();
2709 if (packagename != null) {
2710 intent = new Intent(Intent.ACTION_VIEW, Uri
2711 .parse("market://search?q=pname:" + packagename));
2712 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2713 startActivity(intent);
Grace Klobae7fe26b2010-06-28 16:23:33 -07002714 // before leaving BrowserActivity, close the empty child tab.
2715 // If a new tab is created through JavaScript open to load this
2716 // url, we would like to close it as we will load this url in a
2717 // different Activity.
2718 closeEmptyChildTab();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002719 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002720 } else {
2721 return false;
2722 }
2723 }
2724
Grace Kloba22ac16e2009-10-07 18:00:23 -07002725 // sanitize the Intent, ensuring web pages can not bypass browser
2726 // security (only access to BROWSABLE activities).
2727 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2728 intent.setComponent(null);
2729 try {
2730 if (startActivityIfNeeded(intent, -1)) {
Grace Klobae7fe26b2010-06-28 16:23:33 -07002731 // before leaving BrowserActivity, close the empty child tab.
2732 // If a new tab is created through JavaScript open to load this
2733 // url, we would like to close it as we will load this url in a
2734 // different Activity.
2735 closeEmptyChildTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002736 return true;
2737 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002738 } catch (ActivityNotFoundException ex) {
2739 // ignore the error. If no application can handle the URL,
2740 // eg about:blank, assume the browser can handle it.
2741 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002742
Grace Kloba22ac16e2009-10-07 18:00:23 -07002743 if (mMenuIsDown) {
2744 openTab(url);
2745 closeOptionsMenu();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002746 return true;
2747 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002748 return false;
2749 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002750
Grace Kloba22ac16e2009-10-07 18:00:23 -07002751 // -------------------------------------------------------------------------
2752 // Helper function for WebChromeClient
2753 // -------------------------------------------------------------------------
The Android Open Source Project0c908882009-03-03 19:32:16 -08002754
Grace Kloba22ac16e2009-10-07 18:00:23 -07002755 void onProgressChanged(WebView view, int newProgress) {
Leon Scrogginsd746a942010-05-19 13:21:44 -04002756 if (mXLargeScreenSize) {
2757 mTitleBar.setProgress(newProgress);
2758 } else {
2759 // On the phone, the fake title bar will always cover up the
2760 // regular title bar (or the regular one is offscreen), so only the
2761 // fake title bar needs to change its progress
2762 mFakeTitleBar.setProgress(newProgress);
2763 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002764
Grace Kloba22ac16e2009-10-07 18:00:23 -07002765 if (newProgress == 100) {
2766 // onProgressChanged() may continue to be called after the main
2767 // frame has finished loading, as any remaining sub frames continue
2768 // to load. We'll only get called once though with newProgress as
2769 // 100 when everything is loaded. (onPageFinished is called once
2770 // when the main frame completes loading regardless of the state of
2771 // any sub frames so calls to onProgressChanges may continue after
2772 // onPageFinished has executed)
2773 if (mInLoad) {
2774 mInLoad = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -04002775 updateInLoadMenuItems();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002776 // If the options menu is open, leave the title bar
2777 if (!mOptionsMenuOpen || !mIconView) {
2778 hideFakeTitleBar();
The Android Open Source Projectcb9a0bb2009-03-11 12:11:58 -07002779 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002780 }
Leon Scrogginsaa37e7f2010-03-05 11:51:20 -05002781 } else {
2782 if (!mInLoad) {
2783 // onPageFinished may have already been called but a subframe is
2784 // still loading and updating the progress. Reset mInLoad and
2785 // update the menu items.
2786 mInLoad = true;
2787 updateInLoadMenuItems();
2788 }
2789 // When the page first begins to load, the Activity may still be
2790 // paused, in which case showFakeTitleBar will do nothing. Call
2791 // again as the page continues to load so that it will be shown.
2792 // (Calling it will the fake title bar is already showing will also
2793 // do nothing.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002794 if (!mOptionsMenuOpen || mIconView) {
2795 // This page has begun to load, so show the title bar
2796 showFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002797 }
2798 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002799 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002800
Grace Kloba22ac16e2009-10-07 18:00:23 -07002801 void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002802 // if a view already exists then immediately terminate the new one
2803 if (mCustomView != null) {
2804 callback.onCustomViewHidden();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002805 return;
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002806 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002807
2808 // Add the custom view to its container.
2809 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
2810 mCustomView = view;
2811 mCustomViewCallback = callback;
2812 // Save the menu state and set it to empty while the custom
2813 // view is showing.
2814 mOldMenuState = mMenuState;
2815 mMenuState = EMPTY_MENU;
2816 // Hide the content view.
2817 mContentView.setVisibility(View.GONE);
2818 // Finally show the custom view container.
Andrei Popescu163ab742009-10-20 17:58:23 +01002819 setStatusBarVisibility(false);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002820 mCustomViewContainer.setVisibility(View.VISIBLE);
2821 mCustomViewContainer.bringToFront();
2822 }
2823
2824 void onHideCustomView() {
2825 if (mCustomView == null)
2826 return;
2827
2828 // Hide the custom view.
2829 mCustomView.setVisibility(View.GONE);
2830 // Remove the custom view from its container.
2831 mCustomViewContainer.removeView(mCustomView);
2832 mCustomView = null;
2833 // Reset the old menu state.
2834 mMenuState = mOldMenuState;
2835 mOldMenuState = EMPTY_MENU;
2836 mCustomViewContainer.setVisibility(View.GONE);
2837 mCustomViewCallback.onCustomViewHidden();
2838 // Show the content view.
Andrei Popescu163ab742009-10-20 17:58:23 +01002839 setStatusBarVisibility(true);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002840 mContentView.setVisibility(View.VISIBLE);
2841 }
2842
2843 Bitmap getDefaultVideoPoster() {
2844 if (mDefaultVideoPoster == null) {
2845 mDefaultVideoPoster = BitmapFactory.decodeResource(
2846 getResources(), R.drawable.default_video_poster);
Patrick Scott3918d442009-08-04 13:22:29 -04002847 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002848 return mDefaultVideoPoster;
2849 }
Patrick Scott3918d442009-08-04 13:22:29 -04002850
Grace Kloba22ac16e2009-10-07 18:00:23 -07002851 View getVideoLoadingProgressView() {
2852 if (mVideoProgressView == null) {
2853 LayoutInflater inflater = LayoutInflater.from(BrowserActivity.this);
2854 mVideoProgressView = inflater.inflate(
2855 R.layout.video_loading_progress, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002856 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002857 return mVideoProgressView;
2858 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002859
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002860 /*
2861 * The Object used to inform the WebView of the file to upload.
2862 */
2863 private ValueCallback<Uri> mUploadMessage;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002864 private String mCameraFilePath;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002865
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002866 void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
2867
2868 final String imageMimeType = "image/*";
2869 final String imageSourceKey = "source";
2870 final String imageSourceValueCamera = "camera";
2871 final String imageSourceValueGallery = "gallery";
2872
2873 // image source can be 'gallery' or 'camera'.
2874 String imageSource = "";
2875
2876 // We add the camera intent if there was no accept type (or '*/*') or 'image/*'.
2877 boolean addCameraIntent = true;
2878
Grace Kloba22ac16e2009-10-07 18:00:23 -07002879 if (mUploadMessage != null) return;
2880 mUploadMessage = uploadMsg;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002881
2882 // Parse the accept type.
2883 String params[] = acceptType.split(";");
2884 String mimeType = params[0];
2885
2886 for (String p : params) {
2887 String[] keyValue = p.split("=");
2888 if (keyValue.length == 2) {
2889 // Process key=value parameters.
2890 if (imageSourceKey.equals(keyValue[0])) {
2891 imageSource = keyValue[1];
2892 }
2893 }
2894 }
2895
2896 // This intent will display the standard OPENABLE file picker.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002897 Intent i = new Intent(Intent.ACTION_GET_CONTENT);
2898 i.addCategory(Intent.CATEGORY_OPENABLE);
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002899
2900 // Create an intent to add to the standard file picker that will
2901 // capture an image from the camera. We'll combine this intent with
2902 // the standard OPENABLE picker unless the web developer specifically
2903 // requested the camera or gallery be opened by passing a parameter
2904 // in the accept type.
2905 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
2906 File externalDataDir = Environment.getExternalStoragePublicDirectory(
2907 Environment.DIRECTORY_DCIM);
2908 File cameraDataDir = new File(externalDataDir.getAbsolutePath() +
2909 File.separator + "browser-photos");
2910 cameraDataDir.mkdirs();
2911 mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator +
2912 System.currentTimeMillis() + ".jpg";
2913 cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCameraFilePath)));
2914
2915 if (mimeType.equals(imageMimeType)) {
2916 i.setType(imageMimeType);
2917 if (imageSource.equals(imageSourceValueCamera)) {
2918 // Specified 'image/*' and requested the camera, so go ahead and launch the camera
2919 // directly.
2920 BrowserActivity.this.startActivityForResult(cameraIntent, FILE_SELECTED);
2921 return;
2922 } else if (imageSource.equals(imageSourceValueGallery)) {
2923 // Specified gallery as the source, so don't want to consider the camera.
2924 addCameraIntent = false;
2925 }
2926 } else {
2927 i.setType("*/*");
2928 }
2929
2930 // Combine the chooser and the extra choices (like camera)
2931 Intent chooser = new Intent(Intent.ACTION_CHOOSER);
2932 chooser.putExtra(Intent.EXTRA_INTENT, i);
2933
2934 if (addCameraIntent) {
2935 // Add the camera Intent
2936 Intent[] choices = new Intent[1];
2937 choices[0] = cameraIntent;
2938 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, choices);
2939 }
2940
2941 chooser.putExtra(Intent.EXTRA_TITLE, getString(R.string.choose_upload));
2942 BrowserActivity.this.startActivityForResult(chooser, FILE_SELECTED);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002943 }
2944
2945 // -------------------------------------------------------------------------
2946 // Implement functions for DownloadListener
2947 // -------------------------------------------------------------------------
2948
The Android Open Source Project0c908882009-03-03 19:32:16 -08002949 /**
2950 * Notify the host application a download should be done, or that
2951 * the data should be streamed if a streaming viewer is available.
2952 * @param url The full url to the content that should be downloaded
2953 * @param contentDisposition Content-disposition http header, if
2954 * present.
2955 * @param mimetype The mimetype of the content reported by the server
2956 * @param contentLength The file size reported by the server
2957 */
2958 public void onDownloadStart(String url, String userAgent,
2959 String contentDisposition, String mimetype, long contentLength) {
2960 // if we're dealing wih A/V content that's not explicitly marked
2961 // for download, check if it's streamable.
2962 if (contentDisposition == null
Patrick Scotte1fb9662009-08-31 14:31:52 -04002963 || !contentDisposition.regionMatches(
2964 true, 0, "attachment", 0, 10)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002965 // query the package manager to see if there's a registered handler
2966 // that matches.
2967 Intent intent = new Intent(Intent.ACTION_VIEW);
2968 intent.setDataAndType(Uri.parse(url), mimetype);
Patrick Scotte1fb9662009-08-31 14:31:52 -04002969 ResolveInfo info = getPackageManager().resolveActivity(intent,
2970 PackageManager.MATCH_DEFAULT_ONLY);
2971 if (info != null) {
2972 ComponentName myName = getComponentName();
2973 // If we resolved to ourselves, we don't want to attempt to
2974 // load the url only to try and download it again.
2975 if (!myName.getPackageName().equals(
2976 info.activityInfo.packageName)
2977 || !myName.getClassName().equals(
2978 info.activityInfo.name)) {
2979 // someone (other than us) knows how to handle this mime
2980 // type with this scheme, don't download.
2981 try {
2982 startActivity(intent);
2983 return;
2984 } catch (ActivityNotFoundException ex) {
2985 if (LOGD_ENABLED) {
2986 Log.d(LOGTAG, "activity not found for " + mimetype
2987 + " over " + Uri.parse(url).getScheme(),
2988 ex);
2989 }
2990 // Best behavior is to fall back to a download in this
2991 // case
The Android Open Source Project0c908882009-03-03 19:32:16 -08002992 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002993 }
2994 }
2995 }
2996 onDownloadStartNoStream(url, userAgent, contentDisposition, mimetype, contentLength);
2997 }
2998
Kristian Monsenfa52d172010-03-25 18:29:21 +00002999 // This is to work around the fact that java.net.URI throws Exceptions
3000 // instead of just encoding URL's properly
3001 // Helper method for onDownloadStartNoStream
3002 private static String encodePath(String path) {
3003 char[] chars = path.toCharArray();
3004
3005 boolean needed = false;
3006 for (char c : chars) {
3007 if (c == '[' || c == ']') {
3008 needed = true;
3009 break;
3010 }
3011 }
3012 if (needed == false) {
3013 return path;
3014 }
3015
3016 StringBuilder sb = new StringBuilder("");
3017 for (char c : chars) {
3018 if (c == '[' || c == ']') {
3019 sb.append('%');
3020 sb.append(Integer.toHexString(c));
3021 } else {
3022 sb.append(c);
3023 }
3024 }
3025
3026 return sb.toString();
3027 }
3028
The Android Open Source Project0c908882009-03-03 19:32:16 -08003029 /**
3030 * Notify the host application a download should be done, even if there
3031 * is a streaming viewer available for thise type.
3032 * @param url The full url to the content that should be downloaded
3033 * @param contentDisposition Content-disposition http header, if
3034 * present.
3035 * @param mimetype The mimetype of the content reported by the server
3036 * @param contentLength The file size reported by the server
3037 */
3038 /*package */ void onDownloadStartNoStream(String url, String userAgent,
3039 String contentDisposition, String mimetype, long contentLength) {
3040
3041 String filename = URLUtil.guessFileName(url,
3042 contentDisposition, mimetype);
3043
3044 // Check to see if we have an SDCard
3045 String status = Environment.getExternalStorageState();
3046 if (!status.equals(Environment.MEDIA_MOUNTED)) {
3047 int title;
3048 String msg;
3049
3050 // Check to see if the SDCard is busy, same as the music app
3051 if (status.equals(Environment.MEDIA_SHARED)) {
3052 msg = getString(R.string.download_sdcard_busy_dlg_msg);
3053 title = R.string.download_sdcard_busy_dlg_title;
3054 } else {
3055 msg = getString(R.string.download_no_sdcard_dlg_msg, filename);
3056 title = R.string.download_no_sdcard_dlg_title;
3057 }
3058
3059 new AlertDialog.Builder(this)
3060 .setTitle(title)
3061 .setIcon(android.R.drawable.ic_dialog_alert)
3062 .setMessage(msg)
3063 .setPositiveButton(R.string.ok, null)
3064 .show();
3065 return;
3066 }
3067
Kristian Monsenfa52d172010-03-25 18:29:21 +00003068 // java.net.URI is a lot stricter than KURL so we have to encode some
3069 // extra characters. Fix for b 2538060 and b 1634719
3070 WebAddress webAddress;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003071 try {
Kristian Monsenfa52d172010-03-25 18:29:21 +00003072 webAddress = new WebAddress(url);
3073 webAddress.mPath = encodePath(webAddress.mPath);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003074 } catch (Exception e) {
Kristian Monsenfa52d172010-03-25 18:29:21 +00003075 // This only happens for very bad urls, we want to chatch the
3076 // exception here
3077 Log.e(LOGTAG, "Exception trying to parse url:" + url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003078 return;
3079 }
3080
3081 // XXX: Have to use the old url since the cookies were stored using the
3082 // old percent-encoded url.
3083 String cookies = CookieManager.getInstance().getCookie(url);
3084
3085 ContentValues values = new ContentValues();
Kristian Monsenfa52d172010-03-25 18:29:21 +00003086 values.put(Downloads.Impl.COLUMN_URI, webAddress.toString());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003087 values.put(Downloads.Impl.COLUMN_COOKIE_DATA, cookies);
3088 values.put(Downloads.Impl.COLUMN_USER_AGENT, userAgent);
3089 values.put(Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003090 getPackageName());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003091 values.put(Downloads.Impl.COLUMN_NOTIFICATION_CLASS,
Leon Scrogginsa563d092010-04-19 16:53:49 -04003092 OpenDownloadReceiver.class.getCanonicalName());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003093 values.put(Downloads.Impl.COLUMN_VISIBILITY,
3094 Downloads.Impl.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
3095 values.put(Downloads.Impl.COLUMN_MIME_TYPE, mimetype);
3096 values.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, filename);
Kristian Monsenfa52d172010-03-25 18:29:21 +00003097 values.put(Downloads.Impl.COLUMN_DESCRIPTION, webAddress.mHost);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003098 if (contentLength > 0) {
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003099 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, contentLength);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003100 }
3101 if (mimetype == null) {
3102 // We must have long pressed on a link or image to download it. We
3103 // are not sure of the mimetype in this case, so do a head request
3104 new FetchUrlMimeType(this).execute(values);
3105 } else {
3106 final Uri contentUri =
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003107 getContentResolver().insert(Downloads.Impl.CONTENT_URI, values);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003108 }
Leon Scroggins9191a7f2010-03-18 14:44:44 -04003109 Toast.makeText(this, R.string.download_pending, Toast.LENGTH_SHORT)
3110 .show();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003111 }
3112
Grace Kloba22ac16e2009-10-07 18:00:23 -07003113 // -------------------------------------------------------------------------
3114
The Android Open Source Project0c908882009-03-03 19:32:16 -08003115 /**
3116 * Resets the lock icon. This method is called when we start a new load and
3117 * know the url to be loaded.
3118 */
3119 private void resetLockIcon(String url) {
3120 // Save the lock-icon state (we revert to it if the load gets cancelled)
Grace Kloba22ac16e2009-10-07 18:00:23 -07003121 mTabControl.getCurrentTab().resetLockIcon(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003122 updateLockIconImage(LOCK_ICON_UNSECURE);
3123 }
3124
The Android Open Source Project0c908882009-03-03 19:32:16 -08003125 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003126 * Update the lock icon to correspond to our latest state.
3127 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003128 private void updateLockIconToLatest() {
3129 updateLockIconImage(mTabControl.getCurrentTab().getLockIconType());
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003130 }
3131
3132 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08003133 * Updates the lock-icon image in the title-bar.
3134 */
3135 private void updateLockIconImage(int lockIconType) {
3136 Drawable d = null;
3137 if (lockIconType == LOCK_ICON_SECURE) {
3138 d = mSecLockIcon;
3139 } else if (lockIconType == LOCK_ICON_MIXED) {
3140 d = mMixLockIcon;
3141 }
Leon Scroggins68579392009-09-15 15:31:54 -04003142 mTitleBar.setLock(d);
Leon Scrogginsd746a942010-05-19 13:21:44 -04003143 if (!mXLargeScreenSize) {
3144 mFakeTitleBar.setLock(d);
3145 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003146 }
3147
3148 /**
3149 * Displays a page-info dialog.
3150 * @param tab The tab to show info about
3151 * @param fromShowSSLCertificateOnError The flag that indicates whether
3152 * this dialog was opened from the SSL-certificate-on-error dialog or
3153 * not. This is important, since we need to know whether to return to
3154 * the parent dialog or simply dismiss.
3155 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003156 private void showPageInfo(final Tab tab,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003157 final boolean fromShowSSLCertificateOnError) {
3158 final LayoutInflater factory = LayoutInflater
3159 .from(this);
3160
3161 final View pageInfoView = factory.inflate(R.layout.page_info, null);
3162
3163 final WebView view = tab.getWebView();
3164
3165 String url = null;
3166 String title = null;
3167
3168 if (view == null) {
3169 url = tab.getUrl();
3170 title = tab.getTitle();
3171 } else if (view == mTabControl.getCurrentWebView()) {
3172 // Use the cached title and url if this is the current WebView
3173 url = mUrl;
3174 title = mTitle;
3175 } else {
3176 url = view.getUrl();
3177 title = view.getTitle();
3178 }
3179
3180 if (url == null) {
3181 url = "";
3182 }
3183 if (title == null) {
3184 title = "";
3185 }
3186
3187 ((TextView) pageInfoView.findViewById(R.id.address)).setText(url);
3188 ((TextView) pageInfoView.findViewById(R.id.title)).setText(title);
3189
3190 mPageInfoView = tab;
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05003191 mPageInfoFromShowSSLCertificateOnError = fromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003192
3193 AlertDialog.Builder alertDialogBuilder =
3194 new AlertDialog.Builder(this)
3195 .setTitle(R.string.page_info).setIcon(android.R.drawable.ic_dialog_info)
3196 .setView(pageInfoView)
3197 .setPositiveButton(
3198 R.string.ok,
3199 new DialogInterface.OnClickListener() {
3200 public void onClick(DialogInterface dialog,
3201 int whichButton) {
3202 mPageInfoDialog = null;
3203 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003204
3205 // if we came here from the SSL error dialog
3206 if (fromShowSSLCertificateOnError) {
3207 // go back to the SSL error dialog
3208 showSSLCertificateOnError(
3209 mSSLCertificateOnErrorView,
3210 mSSLCertificateOnErrorHandler,
3211 mSSLCertificateOnErrorError);
3212 }
3213 }
3214 })
3215 .setOnCancelListener(
3216 new DialogInterface.OnCancelListener() {
3217 public void onCancel(DialogInterface dialog) {
3218 mPageInfoDialog = null;
3219 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003220
3221 // if we came here from the SSL error dialog
3222 if (fromShowSSLCertificateOnError) {
3223 // go back to the SSL error dialog
3224 showSSLCertificateOnError(
3225 mSSLCertificateOnErrorView,
3226 mSSLCertificateOnErrorHandler,
3227 mSSLCertificateOnErrorError);
3228 }
3229 }
3230 });
3231
3232 // if we have a main top-level page SSL certificate set or a certificate
3233 // error
3234 if (fromShowSSLCertificateOnError ||
3235 (view != null && view.getCertificate() != null)) {
3236 // add a 'View Certificate' button
3237 alertDialogBuilder.setNeutralButton(
3238 R.string.view_certificate,
3239 new DialogInterface.OnClickListener() {
3240 public void onClick(DialogInterface dialog,
3241 int whichButton) {
3242 mPageInfoDialog = null;
3243 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003244
3245 // if we came here from the SSL error dialog
3246 if (fromShowSSLCertificateOnError) {
3247 // go back to the SSL error dialog
3248 showSSLCertificateOnError(
3249 mSSLCertificateOnErrorView,
3250 mSSLCertificateOnErrorHandler,
3251 mSSLCertificateOnErrorError);
3252 } else {
3253 // otherwise, display the top-most certificate from
3254 // the chain
3255 if (view.getCertificate() != null) {
3256 showSSLCertificate(tab);
3257 }
3258 }
3259 }
3260 });
3261 }
3262
3263 mPageInfoDialog = alertDialogBuilder.show();
3264 }
3265
3266 /**
3267 * Displays the main top-level page SSL certificate dialog
3268 * (accessible from the Page-Info dialog).
3269 * @param tab The tab to show certificate for.
3270 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003271 private void showSSLCertificate(final Tab tab) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003272 final View certificateView =
3273 inflateCertificateView(tab.getWebView().getCertificate());
3274 if (certificateView == null) {
3275 return;
3276 }
3277
3278 LayoutInflater factory = LayoutInflater.from(this);
3279
3280 final LinearLayout placeholder =
3281 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3282
3283 LinearLayout ll = (LinearLayout) factory.inflate(
3284 R.layout.ssl_success, placeholder);
3285 ((TextView)ll.findViewById(R.id.success))
3286 .setText(R.string.ssl_certificate_is_valid);
3287
3288 mSSLCertificateView = tab;
3289 mSSLCertificateDialog =
3290 new AlertDialog.Builder(this)
3291 .setTitle(R.string.ssl_certificate).setIcon(
3292 R.drawable.ic_dialog_browser_certificate_secure)
3293 .setView(certificateView)
3294 .setPositiveButton(R.string.ok,
3295 new DialogInterface.OnClickListener() {
3296 public void onClick(DialogInterface dialog,
3297 int whichButton) {
3298 mSSLCertificateDialog = null;
3299 mSSLCertificateView = null;
3300
3301 showPageInfo(tab, false);
3302 }
3303 })
3304 .setOnCancelListener(
3305 new DialogInterface.OnCancelListener() {
3306 public void onCancel(DialogInterface dialog) {
3307 mSSLCertificateDialog = null;
3308 mSSLCertificateView = null;
3309
3310 showPageInfo(tab, false);
3311 }
3312 })
3313 .show();
3314 }
3315
3316 /**
3317 * Displays the SSL error certificate dialog.
3318 * @param view The target web-view.
3319 * @param handler The SSL error handler responsible for cancelling the
3320 * connection that resulted in an SSL error or proceeding per user request.
3321 * @param error The SSL error object.
3322 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003323 void showSSLCertificateOnError(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003324 final WebView view, final SslErrorHandler handler, final SslError error) {
3325
3326 final View certificateView =
3327 inflateCertificateView(error.getCertificate());
3328 if (certificateView == null) {
3329 return;
3330 }
3331
3332 LayoutInflater factory = LayoutInflater.from(this);
3333
3334 final LinearLayout placeholder =
3335 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3336
3337 if (error.hasError(SslError.SSL_UNTRUSTED)) {
3338 LinearLayout ll = (LinearLayout)factory
3339 .inflate(R.layout.ssl_warning, placeholder);
3340 ((TextView)ll.findViewById(R.id.warning))
3341 .setText(R.string.ssl_untrusted);
3342 }
3343
3344 if (error.hasError(SslError.SSL_IDMISMATCH)) {
3345 LinearLayout ll = (LinearLayout)factory
3346 .inflate(R.layout.ssl_warning, placeholder);
3347 ((TextView)ll.findViewById(R.id.warning))
3348 .setText(R.string.ssl_mismatch);
3349 }
3350
3351 if (error.hasError(SslError.SSL_EXPIRED)) {
3352 LinearLayout ll = (LinearLayout)factory
3353 .inflate(R.layout.ssl_warning, placeholder);
3354 ((TextView)ll.findViewById(R.id.warning))
3355 .setText(R.string.ssl_expired);
3356 }
3357
3358 if (error.hasError(SslError.SSL_NOTYETVALID)) {
3359 LinearLayout ll = (LinearLayout)factory
3360 .inflate(R.layout.ssl_warning, placeholder);
3361 ((TextView)ll.findViewById(R.id.warning))
3362 .setText(R.string.ssl_not_yet_valid);
3363 }
3364
3365 mSSLCertificateOnErrorHandler = handler;
3366 mSSLCertificateOnErrorView = view;
3367 mSSLCertificateOnErrorError = error;
3368 mSSLCertificateOnErrorDialog =
3369 new AlertDialog.Builder(this)
3370 .setTitle(R.string.ssl_certificate).setIcon(
3371 R.drawable.ic_dialog_browser_certificate_partially_secure)
3372 .setView(certificateView)
3373 .setPositiveButton(R.string.ok,
3374 new DialogInterface.OnClickListener() {
3375 public void onClick(DialogInterface dialog,
3376 int whichButton) {
3377 mSSLCertificateOnErrorDialog = null;
3378 mSSLCertificateOnErrorView = null;
3379 mSSLCertificateOnErrorHandler = null;
3380 mSSLCertificateOnErrorError = null;
3381
Grace Kloba22ac16e2009-10-07 18:00:23 -07003382 view.getWebViewClient().onReceivedSslError(
3383 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003384 }
3385 })
3386 .setNeutralButton(R.string.page_info_view,
3387 new DialogInterface.OnClickListener() {
3388 public void onClick(DialogInterface dialog,
3389 int whichButton) {
3390 mSSLCertificateOnErrorDialog = null;
3391
3392 // do not clear the dialog state: we will
3393 // need to show the dialog again once the
3394 // user is done exploring the page-info details
3395
3396 showPageInfo(mTabControl.getTabFromView(view),
3397 true);
3398 }
3399 })
3400 .setOnCancelListener(
3401 new DialogInterface.OnCancelListener() {
3402 public void onCancel(DialogInterface dialog) {
3403 mSSLCertificateOnErrorDialog = null;
3404 mSSLCertificateOnErrorView = null;
3405 mSSLCertificateOnErrorHandler = null;
3406 mSSLCertificateOnErrorError = null;
3407
Grace Kloba22ac16e2009-10-07 18:00:23 -07003408 view.getWebViewClient().onReceivedSslError(
3409 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003410 }
3411 })
3412 .show();
3413 }
3414
3415 /**
3416 * Inflates the SSL certificate view (helper method).
3417 * @param certificate The SSL certificate.
3418 * @return The resultant certificate view with issued-to, issued-by,
3419 * issued-on, expires-on, and possibly other fields set.
3420 * If the input certificate is null, returns null.
3421 */
3422 private View inflateCertificateView(SslCertificate certificate) {
3423 if (certificate == null) {
3424 return null;
3425 }
3426
3427 LayoutInflater factory = LayoutInflater.from(this);
3428
3429 View certificateView = factory.inflate(
3430 R.layout.ssl_certificate, null);
3431
3432 // issued to:
3433 SslCertificate.DName issuedTo = certificate.getIssuedTo();
3434 if (issuedTo != null) {
3435 ((TextView) certificateView.findViewById(R.id.to_common))
3436 .setText(issuedTo.getCName());
3437 ((TextView) certificateView.findViewById(R.id.to_org))
3438 .setText(issuedTo.getOName());
3439 ((TextView) certificateView.findViewById(R.id.to_org_unit))
3440 .setText(issuedTo.getUName());
3441 }
3442
3443 // issued by:
3444 SslCertificate.DName issuedBy = certificate.getIssuedBy();
3445 if (issuedBy != null) {
3446 ((TextView) certificateView.findViewById(R.id.by_common))
3447 .setText(issuedBy.getCName());
3448 ((TextView) certificateView.findViewById(R.id.by_org))
3449 .setText(issuedBy.getOName());
3450 ((TextView) certificateView.findViewById(R.id.by_org_unit))
3451 .setText(issuedBy.getUName());
3452 }
3453
3454 // issued on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003455 String issuedOn = formatCertificateDate(
3456 certificate.getValidNotBeforeDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003457 ((TextView) certificateView.findViewById(R.id.issued_on))
3458 .setText(issuedOn);
3459
3460 // expires on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003461 String expiresOn = formatCertificateDate(
3462 certificate.getValidNotAfterDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003463 ((TextView) certificateView.findViewById(R.id.expires_on))
3464 .setText(expiresOn);
3465
3466 return certificateView;
3467 }
3468
3469 /**
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003470 * Formats the certificate date to a properly localized date string.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003471 * @return Properly localized version of the certificate date string and
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003472 * the "" if it fails to localize.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003473 */
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003474 private String formatCertificateDate(Date certificateDate) {
3475 if (certificateDate == null) {
3476 return "";
The Android Open Source Project0c908882009-03-03 19:32:16 -08003477 }
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003478 String formattedDate = DateFormat.getDateFormat(this).format(certificateDate);
3479 if (formattedDate == null) {
3480 return "";
3481 }
3482 return formattedDate;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003483 }
3484
3485 /**
3486 * Displays an http-authentication dialog.
3487 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003488 void showHttpAuthentication(final HttpAuthHandler handler,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003489 final String host, final String realm, final String title,
3490 final String name, final String password, int focusId) {
3491 LayoutInflater factory = LayoutInflater.from(this);
3492 final View v = factory
3493 .inflate(R.layout.http_authentication, null);
3494 if (name != null) {
3495 ((EditText) v.findViewById(R.id.username_edit)).setText(name);
3496 }
3497 if (password != null) {
3498 ((EditText) v.findViewById(R.id.password_edit)).setText(password);
3499 }
3500
3501 String titleText = title;
3502 if (titleText == null) {
3503 titleText = getText(R.string.sign_in_to).toString().replace(
3504 "%s1", host).replace("%s2", realm);
3505 }
3506
3507 mHttpAuthHandler = handler;
3508 AlertDialog dialog = new AlertDialog.Builder(this)
3509 .setTitle(titleText)
3510 .setIcon(android.R.drawable.ic_dialog_alert)
3511 .setView(v)
3512 .setPositiveButton(R.string.action,
3513 new DialogInterface.OnClickListener() {
3514 public void onClick(DialogInterface dialog,
3515 int whichButton) {
3516 String nm = ((EditText) v
3517 .findViewById(R.id.username_edit))
3518 .getText().toString();
3519 String pw = ((EditText) v
3520 .findViewById(R.id.password_edit))
3521 .getText().toString();
3522 BrowserActivity.this.setHttpAuthUsernamePassword
3523 (host, realm, nm, pw);
3524 handler.proceed(nm, pw);
3525 mHttpAuthenticationDialog = null;
3526 mHttpAuthHandler = null;
3527 }})
3528 .setNegativeButton(R.string.cancel,
3529 new DialogInterface.OnClickListener() {
3530 public void onClick(DialogInterface dialog,
3531 int whichButton) {
3532 handler.cancel();
3533 BrowserActivity.this.resetTitleAndRevertLockIcon();
3534 mHttpAuthenticationDialog = null;
3535 mHttpAuthHandler = null;
3536 }})
3537 .setOnCancelListener(new DialogInterface.OnCancelListener() {
3538 public void onCancel(DialogInterface dialog) {
3539 handler.cancel();
3540 BrowserActivity.this.resetTitleAndRevertLockIcon();
3541 mHttpAuthenticationDialog = null;
3542 mHttpAuthHandler = null;
3543 }})
3544 .create();
3545 // Make the IME appear when the dialog is displayed if applicable.
3546 dialog.getWindow().setSoftInputMode(
3547 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
3548 dialog.show();
3549 if (focusId != 0) {
3550 dialog.findViewById(focusId).requestFocus();
3551 } else {
3552 v.findViewById(R.id.username_edit).requestFocus();
3553 }
3554 mHttpAuthenticationDialog = dialog;
3555 }
3556
3557 public int getProgress() {
3558 WebView w = mTabControl.getCurrentWebView();
3559 if (w != null) {
3560 return w.getProgress();
3561 } else {
3562 return 100;
3563 }
3564 }
3565
3566 /**
3567 * Set HTTP authentication password.
3568 *
3569 * @param host The host for the password
3570 * @param realm The realm for the password
3571 * @param username The username for the password. If it is null, it means
3572 * password can't be saved.
3573 * @param password The password
3574 */
3575 public void setHttpAuthUsernamePassword(String host, String realm,
3576 String username,
3577 String password) {
Steve Block95a53b22010-03-25 17:24:58 +00003578 WebView w = getTopWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003579 if (w != null) {
3580 w.setHttpAuthUsernamePassword(host, realm, username, password);
3581 }
3582 }
3583
3584 /**
3585 * connectivity manager says net has come or gone... inform the user
3586 * @param up true if net has come up, false if net has gone down
3587 */
3588 public void onNetworkToggle(boolean up) {
3589 if (up == mIsNetworkUp) {
3590 return;
3591 } else if (up) {
3592 mIsNetworkUp = true;
3593 if (mAlertDialog != null) {
3594 mAlertDialog.cancel();
3595 mAlertDialog = null;
3596 }
3597 } else {
3598 mIsNetworkUp = false;
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003599 if (mInLoad) {
3600 createAndShowNetworkDialog();
3601 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003602 }
3603 WebView w = mTabControl.getCurrentWebView();
3604 if (w != null) {
3605 w.setNetworkAvailable(up);
3606 }
3607 }
3608
Grace Kloba22ac16e2009-10-07 18:00:23 -07003609 boolean isNetworkUp() {
3610 return mIsNetworkUp;
3611 }
3612
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003613 // This method shows the network dialog alerting the user that the net is
3614 // down. It will only show the dialog if mAlertDialog is null.
3615 private void createAndShowNetworkDialog() {
3616 if (mAlertDialog == null) {
3617 mAlertDialog = new AlertDialog.Builder(this)
3618 .setTitle(R.string.loadSuspendedTitle)
3619 .setMessage(R.string.loadSuspended)
3620 .setPositiveButton(R.string.ok, null)
3621 .show();
3622 }
3623 }
3624
The Android Open Source Project0c908882009-03-03 19:32:16 -08003625 @Override
3626 protected void onActivityResult(int requestCode, int resultCode,
3627 Intent intent) {
Grace Klobabb394f32009-11-19 10:26:37 -08003628 if (getTopWindow() == null) return;
3629
The Android Open Source Project0c908882009-03-03 19:32:16 -08003630 switch (requestCode) {
3631 case COMBO_PAGE:
3632 if (resultCode == RESULT_OK && intent != null) {
3633 String data = intent.getAction();
3634 Bundle extras = intent.getExtras();
3635 if (extras != null && extras.getBoolean("new_window", false)) {
Leon Scroggins25d35472009-09-15 11:37:27 -04003636 openTab(data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003637 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -07003638 final Tab currentTab =
The Android Open Source Project0c908882009-03-03 19:32:16 -08003639 mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04003640 dismissSubWindow(currentTab);
3641 if (data != null && data.length() != 0) {
Leon Scroggins92472e82010-02-17 16:32:28 -05003642 loadUrl(getTopWindow(), data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003643 }
3644 }
3645 }
Leon Scrogginsfde97462010-01-11 13:06:21 -05003646 // Deliberately fall through to PREFERENCES_PAGE, since the
3647 // same extra may be attached to the COMBO_PAGE
3648 case PREFERENCES_PAGE:
3649 if (resultCode == RESULT_OK && intent != null) {
3650 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
3651 if (BrowserSettings.PREF_CLEAR_HISTORY.equals(action)) {
3652 mTabControl.removeParentChildRelationShips();
3653 }
3654 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003655 break;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003656 // Choose a file from the file picker.
3657 case FILE_SELECTED:
3658 if (null == mUploadMessage) break;
3659 Uri result = intent == null || resultCode != RESULT_OK ? null
3660 : intent.getData();
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003661
3662 // As we ask the camera to save the result of the user taking
3663 // a picture, the camera application does not return anything other
3664 // than RESULT_OK. So we need to check whether the file we expected
3665 // was written to disk in the in the case that we
3666 // did not get an intent returned but did get a RESULT_OK. If it was,
3667 // we assume that this result has came back from the camera.
3668 if (result == null && intent == null && resultCode == RESULT_OK) {
3669 File cameraFile = new File(mCameraFilePath);
3670 if (cameraFile.exists()) {
3671 result = Uri.fromFile(cameraFile);
Ben Murdoch07d34732010-05-27 18:34:46 +01003672 // Broadcast to the media scanner that we have a new photo
3673 // so it will be added into the gallery for the user.
3674 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003675 }
3676 }
3677
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003678 mUploadMessage.onReceiveValue(result);
3679 mUploadMessage = null;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003680 mCameraFilePath = null;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003681 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003682 default:
3683 break;
3684 }
Leon Scroggins30444232009-09-04 18:36:20 -04003685 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003686 }
3687
3688 /*
3689 * This method is called as a result of the user selecting the options
Leon Scrogginsf08809b2010-01-21 09:27:46 -05003690 * menu to see the download window. It shows the download window on top of
3691 * the current window.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003692 */
Leon Scrogginsf08809b2010-01-21 09:27:46 -05003693 private void viewDownloads(Uri downloadRecord) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003694 Intent intent = new Intent(this,
3695 BrowserDownloadPage.class);
3696 intent.setData(downloadRecord);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003697 startActivityForResult(intent, BrowserActivity.DOWNLOAD_PAGE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003698
3699 }
3700
Leon Scroggins160a7e72009-08-14 18:28:01 -04003701 /**
3702 * Open the Go page.
3703 * @param startWithHistory If true, open starting on the history tab.
3704 * Otherwise, start with the bookmarks tab.
Leon Scroggins160a7e72009-08-14 18:28:01 -04003705 */
Leon Scroggins30444232009-09-04 18:36:20 -04003706 /* package */ void bookmarksOrHistoryPicker(boolean startWithHistory) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003707 WebView current = mTabControl.getCurrentWebView();
3708 if (current == null) {
3709 return;
3710 }
3711 Intent intent = new Intent(this,
3712 CombinedBookmarkHistoryActivity.class);
3713 String title = current.getTitle();
3714 String url = current.getUrl();
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003715 Bitmap thumbnail = createScreenshot(current);
3716
The Android Open Source Project0c908882009-03-03 19:32:16 -08003717 // Just in case the user opens bookmarks before a page finishes loading
3718 // so the current history item, and therefore the page, is null.
3719 if (null == url) {
3720 url = mLastEnteredUrl;
3721 // This can happen.
3722 if (null == url) {
3723 url = mSettings.getHomePage();
3724 }
3725 }
3726 // In case the web page has not yet received its associated title.
3727 if (title == null) {
3728 title = url;
3729 }
3730 intent.putExtra("title", title);
3731 intent.putExtra("url", url);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003732 intent.putExtra("thumbnail", thumbnail);
Leon Scroggins30444232009-09-04 18:36:20 -04003733 // Disable opening in a new window if we have maxed out the windows
Grace Kloba22ac16e2009-10-07 18:00:23 -07003734 intent.putExtra("disable_new_window", !mTabControl.canCreateNewTab());
Patrick Scott3918d442009-08-04 13:22:29 -04003735 intent.putExtra("touch_icon_url", current.getTouchIconUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003736 if (startWithHistory) {
3737 intent.putExtra(CombinedBookmarkHistoryActivity.STARTING_TAB,
3738 CombinedBookmarkHistoryActivity.HISTORY_TAB);
3739 }
3740 startActivityForResult(intent, COMBO_PAGE);
3741 }
3742
3743 // Called when loading from context menu or LOAD_URL message
Leon Scroggins92472e82010-02-17 16:32:28 -05003744 private void loadUrlFromContext(WebView view, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003745 // In case the user enters nothing.
3746 if (url != null && url.length() != 0 && view != null) {
3747 url = smartUrlFilter(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003748 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
Leon Scroggins92472e82010-02-17 16:32:28 -05003749 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003750 }
3751 }
3752 }
3753
Leon Scroggins92472e82010-02-17 16:32:28 -05003754 /**
3755 * Load the URL into the given WebView and update the title bar
3756 * to reflect the new load. Call this instead of WebView.loadUrl
3757 * directly.
3758 * @param view The WebView used to load url.
3759 * @param url The URL to load.
3760 */
3761 private void loadUrl(WebView view, String url) {
3762 updateTitleBarForNewLoad(view, url);
3763 view.loadUrl(url);
3764 }
3765
3766 /**
3767 * Load UrlData into a Tab and update the title bar to reflect the new
3768 * load. Call this instead of UrlData.loadIn directly.
3769 * @param t The Tab used to load.
3770 * @param data The UrlData being loaded.
3771 */
3772 private void loadUrlDataIn(Tab t, UrlData data) {
3773 updateTitleBarForNewLoad(t.getWebView(), data.mUrl);
3774 data.loadIn(t);
3775 }
3776
3777 /**
3778 * If the WebView is the top window, update the title bar to reflect
3779 * loading the new URL. i.e. set its text, clear the favicon (which
3780 * will be set once the page begins loading), and set the progress to
3781 * INITIAL_PROGRESS to show that the page has begun to load. Called
3782 * by loadUrl and loadUrlDataIn.
3783 * @param view The WebView that is starting a load.
3784 * @param url The URL that is being loaded.
3785 */
3786 private void updateTitleBarForNewLoad(WebView view, String url) {
3787 if (view == getTopWindow()) {
3788 setUrlTitle(url, null);
3789 setFavicon(null);
3790 onProgressChanged(view, INITIAL_PROGRESS);
3791 }
3792 }
3793
The Android Open Source Project0c908882009-03-03 19:32:16 -08003794 private String smartUrlFilter(Uri inUri) {
3795 if (inUri != null) {
3796 return smartUrlFilter(inUri.toString());
3797 }
3798 return null;
3799 }
3800
Feng Qianb34f87a2009-03-24 21:27:26 -07003801 protected static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003802 "(?i)" + // switch on case insensitive matching
3803 "(" + // begin group for schema
3804 "(?:http|https|file):\\/\\/" +
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003805 "|(?:inline|data|about|content|javascript):" +
The Android Open Source Project0c908882009-03-03 19:32:16 -08003806 ")" +
3807 "(.*)" );
3808
3809 /**
3810 * Attempts to determine whether user input is a URL or search
3811 * terms. Anything with a space is passed to search.
3812 *
3813 * Converts to lowercase any mistakenly uppercased schema (i.e.,
3814 * "Http://" converts to "http://"
3815 *
3816 * @return Original or modified URL
3817 *
3818 */
3819 String smartUrlFilter(String url) {
3820
3821 String inUrl = url.trim();
3822 boolean hasSpace = inUrl.indexOf(' ') != -1;
3823
3824 Matcher matcher = ACCEPTED_URI_SCHEMA.matcher(inUrl);
3825 if (matcher.matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003826 // force scheme to lowercase
3827 String scheme = matcher.group(1);
3828 String lcScheme = scheme.toLowerCase();
3829 if (!lcScheme.equals(scheme)) {
Mitsuru Oshima123ecfb2009-05-18 19:11:14 -07003830 inUrl = lcScheme + matcher.group(2);
3831 }
3832 if (hasSpace) {
3833 inUrl = inUrl.replace(" ", "%20");
The Android Open Source Project0c908882009-03-03 19:32:16 -08003834 }
3835 return inUrl;
3836 }
3837 if (hasSpace) {
Satish Sampath565505b2009-05-29 15:37:27 +01003838 // FIXME: Is this the correct place to add to searches?
3839 // what if someone else calls this function?
3840 int shortcut = parseUrlShortcut(inUrl);
3841 if (shortcut != SHORTCUT_INVALID) {
3842 Browser.addSearchUrl(mResolver, inUrl);
3843 String query = inUrl.substring(2);
3844 switch (shortcut) {
3845 case SHORTCUT_GOOGLE_SEARCH:
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003846 return URLUtil.composeSearchUrl(query, QuickSearch_G, QUERY_PLACE_HOLDER);
Satish Sampath565505b2009-05-29 15:37:27 +01003847 case SHORTCUT_WIKIPEDIA_SEARCH:
3848 return URLUtil.composeSearchUrl(query, QuickSearch_W, QUERY_PLACE_HOLDER);
3849 case SHORTCUT_DICTIONARY_SEARCH:
3850 return URLUtil.composeSearchUrl(query, QuickSearch_D, QUERY_PLACE_HOLDER);
3851 case SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH:
The Android Open Source Project0c908882009-03-03 19:32:16 -08003852 // FIXME: we need location in this case
Satish Sampath565505b2009-05-29 15:37:27 +01003853 return URLUtil.composeSearchUrl(query, QuickSearch_L, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003854 }
3855 }
3856 } else {
Dan Egnor5ee906c2009-11-18 12:11:49 -08003857 if (Patterns.WEB_URL.matcher(inUrl).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003858 return URLUtil.guessUrl(inUrl);
3859 }
3860 }
3861
3862 Browser.addSearchUrl(mResolver, inUrl);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003863 return URLUtil.composeSearchUrl(inUrl, QuickSearch_G, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003864 }
3865
Ben Murdochbff2d602009-07-01 20:19:05 +01003866 /* package */ void setShouldShowErrorConsole(boolean flag) {
3867 if (flag == mShouldShowErrorConsole) {
3868 // Nothing to do.
3869 return;
3870 }
3871
3872 mShouldShowErrorConsole = flag;
3873
Grace Kloba22ac16e2009-10-07 18:00:23 -07003874 ErrorConsoleView errorConsole = mTabControl.getCurrentTab()
3875 .getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01003876
3877 if (flag) {
3878 // Setting the show state of the console will cause it's the layout to be inflated.
3879 if (errorConsole.numberOfErrors() > 0) {
3880 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
3881 } else {
3882 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
3883 }
3884
3885 // Now we can add it to the main view.
3886 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08003887 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01003888 ViewGroup.LayoutParams.WRAP_CONTENT));
3889 } else {
3890 mErrorConsoleContainer.removeView(errorConsole);
3891 }
3892
3893 }
3894
Grace Kloba22ac16e2009-10-07 18:00:23 -07003895 boolean shouldShowErrorConsole() {
3896 return mShouldShowErrorConsole;
3897 }
3898
Andrei Popescu163ab742009-10-20 17:58:23 +01003899 private void setStatusBarVisibility(boolean visible) {
3900 int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
3901 getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
3902 }
3903
Andrei Popescu56199cc2010-01-12 22:39:16 +00003904
3905 private void sendNetworkType(String type, String subtype) {
3906 WebView w = mTabControl.getCurrentWebView();
3907 if (w != null) {
3908 w.setNetworkType(type, subtype);
3909 }
3910 }
3911
Andrei Popescu30995e72010-02-09 16:59:58 +00003912 private void packageChanged(String packageName, boolean wasAdded) {
3913 WebView w = mTabControl.getCurrentWebView();
3914 if (w == null) {
3915 return;
3916 }
3917
3918 if (wasAdded) {
3919 w.addPackageName(packageName);
3920 } else {
3921 w.removePackageName(packageName);
3922 }
3923 }
3924
3925 private void addPackageNames(Set<String> packageNames) {
3926 WebView w = mTabControl.getCurrentWebView();
3927 if (w == null) {
3928 return;
3929 }
3930
3931 w.addPackageNames(packageNames);
3932 }
3933
3934 private void getInstalledPackages() {
3935 AsyncTask<Void, Void, Set<String> > task =
3936 new AsyncTask<Void, Void, Set<String> >() {
3937 protected Set<String> doInBackground(Void... unused) {
3938 Set<String> installedPackages = new HashSet<String>();
3939 PackageManager pm = BrowserActivity.this.getPackageManager();
3940 if (pm != null) {
3941 List<PackageInfo> packages = pm.getInstalledPackages(0);
3942 for (PackageInfo p : packages) {
3943 if (BrowserActivity.this.sGoogleApps.contains(p.packageName)) {
3944 installedPackages.add(p.packageName);
3945 }
3946 }
3947 }
3948
3949 return installedPackages;
3950 }
3951
3952 // Executes on the UI thread
3953 protected void onPostExecute(Set<String> installedPackages) {
3954 addPackageNames(installedPackages);
3955 }
3956 };
3957 task.execute();
3958 }
3959
Grace Klobaeb6eef42009-09-15 17:56:32 -07003960 final static int LOCK_ICON_UNSECURE = 0;
3961 final static int LOCK_ICON_SECURE = 1;
3962 final static int LOCK_ICON_MIXED = 2;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003963
The Android Open Source Project0c908882009-03-03 19:32:16 -08003964 private BrowserSettings mSettings;
3965 private TabControl mTabControl;
3966 private ContentResolver mResolver;
3967 private FrameLayout mContentView;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003968 private View mCustomView;
3969 private FrameLayout mCustomViewContainer;
Andrei Popescuc9b55562009-07-07 10:51:15 +01003970 private WebChromeClient.CustomViewCallback mCustomViewCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003971
3972 // FIXME, temp address onPrepareMenu performance problem. When we move everything out of
3973 // view, we should rewrite this.
3974 private int mCurrentMenuState = 0;
3975 private int mMenuState = R.id.MAIN_MENU;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003976 private int mOldMenuState = EMPTY_MENU;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003977 private static final int EMPTY_MENU = -1;
3978 private Menu mMenu;
3979
3980 private FindDialog mFindDialog;
Cary Clark01cfcdd2010-06-04 16:36:45 -04003981 private SelectDialog mSelectDialog;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003982 // Used to prevent chording to result in firing two shortcuts immediately
3983 // one after another. Fixes bug 1211714.
3984 boolean mCanChord;
3985
3986 private boolean mInLoad;
3987 private boolean mIsNetworkUp;
Ben Murdochb7cc8b42009-09-28 10:59:09 +01003988 private boolean mDidStopLoad;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003989
Cary Clark1f10cbf2010-03-22 11:45:23 -04003990 /* package */ boolean mActivityInPause = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003991
3992 private boolean mMenuIsDown;
3993
The Android Open Source Project0c908882009-03-03 19:32:16 -08003994 private static boolean mInTrace;
3995
3996 // Performance probe
3997 private static final int[] SYSTEM_CPU_FORMAT = new int[] {
3998 Process.PROC_SPACE_TERM | Process.PROC_COMBINE,
3999 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time
4000 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time
4001 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time
4002 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time
4003 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time
4004 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time
4005 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG // 7: softirq time
4006 };
4007
4008 private long mStart;
4009 private long mProcessStart;
4010 private long mUserStart;
4011 private long mSystemStart;
4012 private long mIdleStart;
4013 private long mIrqStart;
4014
4015 private long mUiStart;
4016
4017 private Drawable mMixLockIcon;
4018 private Drawable mSecLockIcon;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004019
4020 /* hold a ref so we can auto-cancel if necessary */
4021 private AlertDialog mAlertDialog;
4022
The Android Open Source Project0c908882009-03-03 19:32:16 -08004023 // The up-to-date URL and title (these can be different from those stored
4024 // in WebView, since it takes some time for the information in WebView to
4025 // get updated)
4026 private String mUrl;
4027 private String mTitle;
4028
4029 // As PageInfo has different style for landscape / portrait, we have
4030 // to re-open it when configuration changed
4031 private AlertDialog mPageInfoDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07004032 private Tab mPageInfoView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004033 // If the Page-Info dialog is launched from the SSL-certificate-on-error
4034 // dialog, we should not just dismiss it, but should get back to the
4035 // SSL-certificate-on-error dialog. This flag is used to store this state
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05004036 private boolean mPageInfoFromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004037
4038 // as SSLCertificateOnError has different style for landscape / portrait,
4039 // we have to re-open it when configuration changed
4040 private AlertDialog mSSLCertificateOnErrorDialog;
4041 private WebView mSSLCertificateOnErrorView;
4042 private SslErrorHandler mSSLCertificateOnErrorHandler;
4043 private SslError mSSLCertificateOnErrorError;
4044
4045 // as SSLCertificate has different style for landscape / portrait, we
4046 // have to re-open it when configuration changed
4047 private AlertDialog mSSLCertificateDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07004048 private Tab mSSLCertificateView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004049
4050 // as HttpAuthentication has different style for landscape / portrait, we
4051 // have to re-open it when configuration changed
4052 private AlertDialog mHttpAuthenticationDialog;
4053 private HttpAuthHandler mHttpAuthHandler;
4054
4055 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
4056 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08004057 ViewGroup.LayoutParams.MATCH_PARENT,
4058 ViewGroup.LayoutParams.MATCH_PARENT);
Andrei Popescuadc008d2009-06-26 14:11:30 +01004059 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
4060 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08004061 ViewGroup.LayoutParams.MATCH_PARENT,
4062 ViewGroup.LayoutParams.MATCH_PARENT,
Andrei Popescuadc008d2009-06-26 14:11:30 +01004063 Gravity.CENTER);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07004064 // Google search
4065 final static String QuickSearch_G = "http://www.google.com/m?q=%s";
The Android Open Source Project0c908882009-03-03 19:32:16 -08004066 // Wikipedia search
4067 final static String QuickSearch_W = "http://en.wikipedia.org/w/index.php?search=%s&go=Go";
4068 // Dictionary search
4069 final static String QuickSearch_D = "http://dictionary.reference.com/search?q=%s";
4070 // Google Mobile Local search
4071 final static String QuickSearch_L = "http://www.google.com/m/search?site=local&q=%s&near=mountain+view";
4072
4073 final static String QUERY_PLACE_HOLDER = "%s";
4074
4075 // "source" parameter for Google search through search key
4076 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
4077 // "source" parameter for Google search through goto menu
4078 final static String GOOGLE_SEARCH_SOURCE_GOTO = "browser-goto";
4079 // "source" parameter for Google search through simplily type
4080 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
4081 // "source" parameter for Google search suggested by the browser
4082 final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
4083 // "source" parameter for Google search from unknown source
4084 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown";
4085
4086 private final static String LOGTAG = "browser";
4087
The Android Open Source Project0c908882009-03-03 19:32:16 -08004088 private String mLastEnteredUrl;
4089
4090 private PowerManager.WakeLock mWakeLock;
4091 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
4092
4093 private Toast mStopToast;
4094
Leon Scroggins571b3762010-05-26 10:25:01 -04004095 private TitleBarBase mTitleBar;
Leon Scroggins81db3662009-06-04 17:45:11 -04004096
Ben Murdochbff2d602009-07-01 20:19:05 +01004097 private LinearLayout mErrorConsoleContainer = null;
4098 private boolean mShouldShowErrorConsole = false;
4099
The Android Open Source Project0c908882009-03-03 19:32:16 -08004100 // As the ids are dynamically created, we can't guarantee that they will
4101 // be in sequence, so this static array maps ids to a window number.
4102 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
4103 { R.id.window_one_menu_id, R.id.window_two_menu_id, R.id.window_three_menu_id,
4104 R.id.window_four_menu_id, R.id.window_five_menu_id, R.id.window_six_menu_id,
4105 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
4106
4107 // monitor platform changes
4108 private IntentFilter mNetworkStateChangedFilter;
4109 private BroadcastReceiver mNetworkStateIntentReceiver;
4110
Grace Klobab4da0ad2009-05-14 14:45:40 -07004111 private BroadcastReceiver mPackageInstallationReceiver;
4112
Bjorn Bringerta7611812010-03-24 11:12:02 +00004113 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
4114
The Android Open Source Project0c908882009-03-03 19:32:16 -08004115 // activity requestCode
Nicolas Roard78a98e42009-05-11 13:34:17 +01004116 final static int COMBO_PAGE = 1;
4117 final static int DOWNLOAD_PAGE = 2;
4118 final static int PREFERENCES_PAGE = 3;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04004119 final static int FILE_SELECTED = 4;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004120
Andrei Popescu540035d2009-09-18 18:59:20 +01004121 // the default <video> poster
4122 private Bitmap mDefaultVideoPoster;
4123 // the video progress view
4124 private View mVideoProgressView;
4125
Andrei Popescu30995e72010-02-09 16:59:58 +00004126 // The Google packages we monitor for the navigator.isApplicationInstalled()
4127 // API. Add as needed.
4128 private static Set<String> sGoogleApps;
4129 static {
4130 sGoogleApps = new HashSet<String>();
4131 sGoogleApps.add("com.google.android.youtube");
4132 }
4133
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004134 /**
4135 * A UrlData class to abstract how the content will be set to WebView.
4136 * This base class uses loadUrl to show the content.
4137 */
Leon Scroggins6eac63e2010-03-15 18:19:14 -04004138 /* package */ static class UrlData {
Grace Kloba068e48b2010-01-26 18:11:27 -08004139 final String mUrl;
4140 final Map<String, String> mHeaders;
Leon Scroggins58d56c62010-01-28 15:12:40 -05004141 final Intent mVoiceIntent;
Grace Kloba60e095c2009-06-16 11:50:55 -07004142
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004143 UrlData(String url) {
4144 this.mUrl = url;
Grace Kloba068e48b2010-01-26 18:11:27 -08004145 this.mHeaders = null;
Leon Scroggins58d56c62010-01-28 15:12:40 -05004146 this.mVoiceIntent = null;
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004147 }
Grace Kloba60e095c2009-06-16 11:50:55 -07004148
Leon Scroggins58d56c62010-01-28 15:12:40 -05004149 UrlData(String url, Map<String, String> headers, Intent intent) {
Grace Kloba068e48b2010-01-26 18:11:27 -08004150 this.mUrl = url;
4151 this.mHeaders = headers;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -05004152 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
4153 .equals(intent.getAction())) {
Leon Scroggins58d56c62010-01-28 15:12:40 -05004154 this.mVoiceIntent = intent;
4155 } else {
4156 this.mVoiceIntent = null;
4157 }
Grace Kloba60e095c2009-06-16 11:50:55 -07004158 }
4159
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004160 boolean isEmpty() {
Leon Scroggins58d56c62010-01-28 15:12:40 -05004161 return mVoiceIntent == null && (mUrl == null || mUrl.length() == 0);
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004162 }
4163
Leon Scroggins92472e82010-02-17 16:32:28 -05004164 /**
4165 * Load this UrlData into the given Tab. Use loadUrlDataIn to update
4166 * the title bar as well.
4167 */
Leon Scroggins58d56c62010-01-28 15:12:40 -05004168 public void loadIn(Tab t) {
4169 if (mVoiceIntent != null) {
4170 t.activateVoiceSearchMode(mVoiceIntent);
4171 } else {
4172 t.getWebView().loadUrl(mUrl, mHeaders);
4173 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004174 }
4175 };
4176
Leon Scroggins1f005d32009-08-10 17:36:42 -04004177 /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08004178}