blob: 92881dd544a57ded167d8315dda301c6f52f7fef [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.app.Activity;
The Android Open Source Project0c908882009-03-03 19:32:16 -080020import android.app.AlertDialog;
21import android.app.ProgressDialog;
22import android.app.SearchManager;
23import android.content.ActivityNotFoundException;
24import android.content.BroadcastReceiver;
25import android.content.ComponentName;
Leon Scroggins58d56c62010-01-28 15:12:40 -050026import android.content.ContentProvider;
27import android.content.ContentProviderClient;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.content.ContentResolver;
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -040029import android.content.ContentUris;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.content.ContentValues;
31import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
34import android.content.IntentFilter;
Grace Klobab4da0ad2009-05-14 14:45:40 -070035import android.content.pm.PackageInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.content.pm.PackageManager;
37import android.content.pm.ResolveInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.content.res.Configuration;
39import android.content.res.Resources;
40import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041import android.graphics.Bitmap;
Andrei Popescu540035d2009-09-18 18:59:20 +010042import android.graphics.BitmapFactory;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import android.graphics.Canvas;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import android.graphics.Picture;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040045import android.graphics.PixelFormat;
The Android Open Source Project0c908882009-03-03 19:32:16 -080046import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047import android.net.ConnectivityManager;
Andrei Popescu56199cc2010-01-12 22:39:16 +000048import android.net.NetworkInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049import android.net.Uri;
50import android.net.WebAddress;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051import android.net.http.SslCertificate;
52import android.net.http.SslError;
53import android.os.AsyncTask;
54import android.os.Bundle;
55import android.os.Debug;
56import android.os.Environment;
57import android.os.Handler;
The Android Open Source Project0c908882009-03-03 19:32:16 -080058import android.os.Message;
59import android.os.PowerManager;
60import android.os.Process;
The Android Open Source Project0c908882009-03-03 19:32:16 -080061import android.os.ServiceManager;
62import android.os.SystemClock;
The Android Open Source Project0c908882009-03-03 19:32:16 -080063import android.provider.Browser;
Cary Clark5e335a32009-09-22 14:53:11 -040064import android.provider.ContactsContract;
The Android Open Source Project0c908882009-03-03 19:32:16 -080065import android.provider.Downloads;
66import android.provider.MediaStore;
Michael Kolbe0a36662010-06-29 10:37:12 -070067import android.provider.ContactsContract.Intents.Insert;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050068import android.speech.RecognizerResultsIntent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080069import android.text.IClipboard;
70import android.text.TextUtils;
71import android.text.format.DateFormat;
The Android Open Source Project0c908882009-03-03 19:32:16 -080072import android.util.Log;
Dianne Hackborn385effd2010-02-24 20:03:04 -080073import android.util.Patterns;
The Android Open Source Project0c908882009-03-03 19:32:16 -080074import android.view.ContextMenu;
75import android.view.Gravity;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
79import android.view.MenuInflater;
80import android.view.MenuItem;
81import android.view.View;
82import android.view.ViewGroup;
83import android.view.Window;
84import android.view.WindowManager;
85import android.view.ContextMenu.ContextMenuInfo;
86import android.view.MenuItem.OnMenuItemClickListener;
Svetoslav Ganov2b345992010-05-06 06:13:54 -070087import android.view.accessibility.AccessibilityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080088import android.webkit.CookieManager;
89import android.webkit.CookieSyncManager;
90import android.webkit.DownloadListener;
91import android.webkit.HttpAuthHandler;
Grace Klobab4da0ad2009-05-14 14:45:40 -070092import android.webkit.PluginManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080093import android.webkit.SslErrorHandler;
94import android.webkit.URLUtil;
Leon Clarkecb6cc862009-09-29 18:35:13 +010095import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080096import android.webkit.WebChromeClient;
97import android.webkit.WebHistoryItem;
98import android.webkit.WebIconDatabase;
99import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800100import android.widget.EditText;
101import android.widget.FrameLayout;
102import android.widget.LinearLayout;
103import android.widget.TextView;
104import android.widget.Toast;
105
Bjorn Bringert10d1cca2010-02-10 14:22:12 +0000106import com.android.common.Search;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500107import com.android.common.speech.LoggingEvents;
Dan Egnor5ee906c2009-11-18 12:11:49 -0800108
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400109import java.io.ByteArrayOutputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110import java.io.File;
Ben Murdoch4f75ba22009-10-27 11:48:28 +0000111import java.io.IOException;
112import java.io.InputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800113import java.net.MalformedURLException;
Dianne Hackborn99189432009-06-17 18:06:18 -0700114import java.net.URISyntaxException;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800115import java.net.URL;
116import java.net.URLEncoder;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800117import java.util.Date;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118import java.util.HashMap;
Andrei Popescu30995e72010-02-09 16:59:58 +0000119import java.util.HashSet;
Grace Kloba00f54c52010-01-27 14:53:51 -0800120import java.util.Iterator;
Andrei Popescu30995e72010-02-09 16:59:58 +0000121import java.util.List;
Grace Kloba068e48b2010-01-26 18:11:27 -0800122import java.util.Map;
Andrei Popescu30995e72010-02-09 16:59:58 +0000123import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124import java.util.regex.Matcher;
125import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800126
127public class BrowserActivity extends Activity
Shimeng (Simon) Wang98d5fce2010-03-16 13:23:39 -0700128 implements View.OnCreateContextMenuListener, DownloadListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800129
Dave Bort31a6d1c2009-04-13 15:56:49 -0700130 /* Define some aliases to make these debugging flags easier to refer to.
131 * This file imports android.provider.Browser, so we can't just refer to "Browser.DEBUG".
132 */
133 private final static boolean DEBUG = com.android.browser.Browser.DEBUG;
134 private final static boolean LOGV_ENABLED = com.android.browser.Browser.LOGV_ENABLED;
135 private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
136
Satish Sampath565505b2009-05-29 15:37:27 +0100137 // These are single-character shortcuts for searching popular sources.
138 private static final int SHORTCUT_INVALID = 0;
139 private static final int SHORTCUT_GOOGLE_SEARCH = 1;
140 private static final int SHORTCUT_WIKIPEDIA_SEARCH = 2;
141 private static final int SHORTCUT_DICTIONARY_SEARCH = 3;
142 private static final int SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH = 4;
143
Cary Clarka9771242009-08-11 16:42:26 -0400144 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800145 @Override
146 public Void doInBackground(File... files) {
147 if (files != null) {
148 for (File f : files) {
Cary Clarkd6be1752009-08-12 12:56:42 -0400149 if (!f.delete()) {
150 Log.e(LOGTAG, f.getPath() + " was not deleted");
151 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800152 }
153 }
154 return null;
155 }
156 }
157
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400158 /**
159 * This layout holds everything you see below the status bar, including the
160 * error console, the custom view container, and the webviews.
161 */
162 private FrameLayout mBrowserFrameLayout;
Leon Scroggins81db3662009-06-04 17:45:11 -0400163
Leon Scrogginsd746a942010-05-19 13:21:44 -0400164 private boolean mXLargeScreenSize;
165
Grace Kloba22ac16e2009-10-07 18:00:23 -0700166 @Override
167 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700168 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800169 Log.v(LOGTAG, this + " onStart");
170 }
171 super.onCreate(icicle);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800172 // test the browser in OpenGL
173 // requestWindowFeature(Window.FEATURE_OPENGL);
174
Mike Reedd334bf52010-01-26 15:21:44 -0500175 // enable this to test the browser in 32bit
176 if (false) {
177 getWindow().setFormat(PixelFormat.RGBX_8888);
178 BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
179 }
180
Svetoslav Ganov2b345992010-05-06 06:13:54 -0700181 if (AccessibilityManager.getInstance(this).isEnabled()) {
182 setDefaultKeyMode(DEFAULT_KEYS_DISABLE);
183 } else {
184 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
185 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186
187 mResolver = getContentResolver();
188
Grace Kloba0923d692009-09-23 21:37:25 -0700189 // If this was a web search request, pass it on to the default web
190 // search provider and finish this activity.
191 if (handleWebSearchIntent(getIntent())) {
192 finish();
193 return;
194 }
195
The Android Open Source Project0c908882009-03-03 19:32:16 -0800196 mSecLockIcon = Resources.getSystem().getDrawable(
197 android.R.drawable.ic_secure);
198 mMixLockIcon = Resources.getSystem().getDrawable(
199 android.R.drawable.ic_partial_secure);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800200
Leon Scroggins81db3662009-06-04 17:45:11 -0400201 FrameLayout frameLayout = (FrameLayout) getWindow().getDecorView()
202 .findViewById(com.android.internal.R.id.content);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400203 mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(this)
204 .inflate(R.layout.custom_screen, null);
205 mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(
206 R.id.main_content);
207 mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout
208 .findViewById(R.id.error_console);
209 mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
210 .findViewById(R.id.fullscreen_custom_content);
211 frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400212 mXLargeScreenSize = (getResources().getConfiguration().screenLayout
213 & Configuration.SCREENLAYOUT_SIZE_MASK)
214 == Configuration.SCREENLAYOUT_SIZE_XLARGE;
Michael Kolbe0a36662010-06-29 10:37:12 -0700215
Leon Scrogginsd746a942010-05-19 13:21:44 -0400216 if (mXLargeScreenSize) {
Leon Scroggins571b3762010-05-26 10:25:01 -0400217 mTitleBar = new TitleBarXLarge(this);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400218 LinearLayout layout = (LinearLayout) mBrowserFrameLayout.
219 findViewById(R.id.vertical_layout);
220 layout.addView(mTitleBar, 0, new LinearLayout.LayoutParams(
221 ViewGroup.LayoutParams.MATCH_PARENT,
222 ViewGroup.LayoutParams.WRAP_CONTENT));
223 } else {
Leon Scroggins571b3762010-05-26 10:25:01 -0400224 mTitleBar = new TitleBar(this);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400225 // mTitleBar will be always be shown in the fully loaded mode on
226 // phone
227 mTitleBar.setProgress(100);
228 // Fake title bar is not needed in xlarge layout
229 mFakeTitleBar = new TitleBar(this);
230 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800231
232 // Create the tab control and our initial tab
233 mTabControl = new TabControl(this);
234
235 // Open the icon database and retain all the bookmark urls for favicons
236 retainIconsOnStartup();
237
238 // Keep a settings instance handy.
239 mSettings = BrowserSettings.getInstance();
240 mSettings.setTabControl(mTabControl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800241
242 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
243 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
244
Patrick Scott6adacc92010-03-05 08:24:51 -0500245 // Find out if the network is currently up.
246 ConnectivityManager cm = (ConnectivityManager) getSystemService(
247 Context.CONNECTIVITY_SERVICE);
248 NetworkInfo info = cm.getActiveNetworkInfo();
249 if (info != null) {
250 mIsNetworkUp = info.isAvailable();
251 }
252
Grace Klobaa34f6862009-07-31 16:28:17 -0700253 /* enables registration for changes in network status from
254 http stack */
255 mNetworkStateChangedFilter = new IntentFilter();
256 mNetworkStateChangedFilter.addAction(
257 ConnectivityManager.CONNECTIVITY_ACTION);
258 mNetworkStateIntentReceiver = new BroadcastReceiver() {
259 @Override
260 public void onReceive(Context context, Intent intent) {
261 if (intent.getAction().equals(
262 ConnectivityManager.CONNECTIVITY_ACTION)) {
Andrei Popescue4c98462010-02-19 15:44:13 +0000263
264 NetworkInfo info = intent.getParcelableExtra(
265 ConnectivityManager.EXTRA_NETWORK_INFO);
266 String typeName = info.getTypeName();
267 String subtypeName = info.getSubtypeName();
268 sendNetworkType(typeName.toLowerCase(),
269 (subtypeName != null ? subtypeName.toLowerCase() : ""));
270
271 onNetworkToggle(info.isAvailable());
Grace Klobaa34f6862009-07-31 16:28:17 -0700272 }
273 }
274 };
275
Grace Kloba615c6c92009-08-03 10:22:44 -0700276 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
277 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
278 filter.addDataScheme("package");
279 mPackageInstallationReceiver = new BroadcastReceiver() {
280 @Override
281 public void onReceive(Context context, Intent intent) {
282 final String action = intent.getAction();
283 final String packageName = intent.getData()
284 .getSchemeSpecificPart();
285 final boolean replacing = intent.getBooleanExtra(
286 Intent.EXTRA_REPLACING, false);
287 if (Intent.ACTION_PACKAGE_REMOVED.equals(action) && replacing) {
288 // if it is replacing, refreshPlugins() when adding
289 return;
290 }
Andrei Popescu30995e72010-02-09 16:59:58 +0000291
292 if (sGoogleApps.contains(packageName)) {
293 BrowserActivity.this.packageChanged(packageName,
294 Intent.ACTION_PACKAGE_ADDED.equals(action));
295 }
296
Grace Kloba615c6c92009-08-03 10:22:44 -0700297 PackageManager pm = BrowserActivity.this.getPackageManager();
298 PackageInfo pkgInfo = null;
299 try {
300 pkgInfo = pm.getPackageInfo(packageName,
301 PackageManager.GET_PERMISSIONS);
302 } catch (PackageManager.NameNotFoundException e) {
303 return;
304 }
305 if (pkgInfo != null) {
306 String permissions[] = pkgInfo.requestedPermissions;
307 if (permissions == null) {
308 return;
309 }
310 boolean permissionOk = false;
311 for (String permit : permissions) {
312 if (PluginManager.PLUGIN_PERMISSION.equals(permit)) {
313 permissionOk = true;
314 break;
315 }
316 }
317 if (permissionOk) {
318 PluginManager.getInstance(BrowserActivity.this)
Grace Klobae56a0f22010-05-26 17:31:02 -0700319 .refreshPlugins(true);
Grace Kloba615c6c92009-08-03 10:22:44 -0700320 }
321 }
322 }
323 };
324 registerReceiver(mPackageInstallationReceiver, filter);
325
The Android Open Source Project0c908882009-03-03 19:32:16 -0800326 if (!mTabControl.restoreState(icicle)) {
327 // clear up the thumbnail directory if we can't restore the state as
328 // none of the files in the directory are referenced any more.
329 new ClearThumbnails().execute(
330 mTabControl.getThumbnailDir().listFiles());
Grace Klobaaab3f092009-07-30 12:29:51 -0700331 // there is no quit on Android. But if we can't restore the state,
332 // we can treat it as a new Browser, remove the old session cookies.
333 CookieManager.getInstance().removeSessionCookie();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800334 final Intent intent = getIntent();
335 final Bundle extra = intent.getExtras();
336 // Create an initial tab.
337 // If the intent is ACTION_VIEW and data is not null, the Browser is
338 // invoked to view the content by another application. In this case,
339 // the tab will be close when exit.
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700340 UrlData urlData = getUrlDataFromIntent(intent);
341
Leon Scroggins58d56c62010-01-28 15:12:40 -0500342 String action = intent.getAction();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700343 final Tab t = mTabControl.createNewTab(
Leon Scroggins58d56c62010-01-28 15:12:40 -0500344 (Intent.ACTION_VIEW.equals(action) &&
345 intent.getData() != null)
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500346 || RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
347 .equals(action),
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700348 intent.getStringExtra(Browser.EXTRA_APPLICATION_ID), urlData.mUrl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800349 mTabControl.setCurrentTab(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800350 attachTabToContentView(t);
351 WebView webView = t.getWebView();
352 if (extra != null) {
353 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
354 if (scale > 0 && scale <= 1000) {
355 webView.setInitialScale(scale);
356 }
357 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800358
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700359 if (urlData.isEmpty()) {
Shimeng (Simon) Wang98d5fce2010-03-16 13:23:39 -0700360 loadUrl(webView, mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800361 } else {
Patrick Scott9d53da02010-02-19 10:19:01 -0500362 loadUrlDataIn(t, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800363 }
364 } else {
365 // TabControl.restoreState() will create a new tab even if
Leon Scroggins1f005d32009-08-10 17:36:42 -0400366 // restoring the state fails.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800367 attachTabToContentView(mTabControl.getCurrentTab());
368 }
Grace Kloba615c6c92009-08-03 10:22:44 -0700369
Feng Qianb3c02da2009-06-29 15:58:08 -0700370 // Read JavaScript flags if it exists.
371 String jsFlags = mSettings.getJsFlags();
372 if (jsFlags.trim().length() != 0) {
373 mTabControl.getCurrentWebView().setJsFlags(jsFlags);
374 }
Andrei Popescu30995e72010-02-09 16:59:58 +0000375 // Work out which packages are installed on the system.
376 getInstalledPackages();
Bjorn Bringerta7611812010-03-24 11:12:02 +0000377
378 // Start watching the default geolocation permissions
379 mSystemAllowGeolocationOrigins
380 = new SystemAllowGeolocationOrigins(getApplicationContext());
381 mSystemAllowGeolocationOrigins.start();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800382 }
383
Leon Scroggins58d56c62010-01-28 15:12:40 -0500384 /**
385 * Feed the previously stored results strings to the BrowserProvider so that
386 * the SearchDialog will show them instead of the standard searches.
387 * @param result String to show on the editable line of the SearchDialog.
388 */
389 /* package */ void showVoiceSearchResults(String result) {
390 ContentProviderClient client = mResolver.acquireContentProviderClient(
391 Browser.BOOKMARKS_URI);
392 ContentProvider prov = client.getLocalContentProvider();
393 BrowserProvider bp = (BrowserProvider) prov;
394 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
395 client.release();
396
Leon Scrogginsfbb3f152010-03-09 17:26:56 -0500397 Bundle bundle = createGoogleSearchSourceBundle(
398 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
399 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
400 startSearch(result, false, bundle, false);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500401 }
402
The Android Open Source Project0c908882009-03-03 19:32:16 -0800403 @Override
404 protected void onNewIntent(Intent intent) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700405 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800406 // When a tab is closed on exit, the current tab index is set to -1.
407 // Reset before proceed as Browser requires the current tab to be set.
408 if (current == null) {
409 // Try to reset the tab in case the index was incorrect.
410 current = mTabControl.getTab(0);
411 if (current == null) {
412 // No tabs at all so just ignore this intent.
413 return;
414 }
415 mTabControl.setCurrentTab(current);
416 attachTabToContentView(current);
417 resetTitleAndIcon(current.getWebView());
418 }
419 final String action = intent.getAction();
420 final int flags = intent.getFlags();
421 if (Intent.ACTION_MAIN.equals(action) ||
422 (flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
423 // just resume the browser
424 return;
425 }
Leon Scrogginsb8a844d2010-03-18 15:06:15 -0400426 // In case the SearchDialog is open.
427 ((SearchManager) getSystemService(Context.SEARCH_SERVICE))
428 .stopSearch();
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500429 boolean activateVoiceSearch = RecognizerResultsIntent
430 .ACTION_VOICE_SEARCH_RESULTS.equals(action);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800431 if (Intent.ACTION_VIEW.equals(action)
432 || Intent.ACTION_SEARCH.equals(action)
433 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
Leon Scroggins58d56c62010-01-28 15:12:40 -0500434 || Intent.ACTION_WEB_SEARCH.equals(action)
435 || activateVoiceSearch) {
Leon Scroggins3b18ce32010-02-08 17:35:59 -0500436 if (current.isInVoiceSearchMode()) {
437 String title = current.getVoiceDisplayTitle();
438 if (title != null && title.equals(intent.getStringExtra(
439 SearchManager.QUERY))) {
440 // The user submitted the same search as the last voice
441 // search, so do nothing.
442 return;
443 }
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500444 if (Intent.ACTION_SEARCH.equals(action)
445 && current.voiceSearchSourceIsGoogle()) {
446 Intent logIntent = new Intent(
447 LoggingEvents.ACTION_LOG_EVENT);
448 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
449 LoggingEvents.VoiceSearch.QUERY_UPDATED);
450 logIntent.putExtra(
451 LoggingEvents.VoiceSearch.EXTRA_QUERY_UPDATED_VALUE,
452 intent.getDataString());
453 sendBroadcast(logIntent);
454 // Note, onPageStarted will revert the voice title bar
455 // When http://b/issue?id=2379215 is fixed, we should update
456 // the title bar here.
457 }
Leon Scroggins3b18ce32010-02-08 17:35:59 -0500458 }
Satish Sampath565505b2009-05-29 15:37:27 +0100459 // If this was a search request (e.g. search query directly typed into the address bar),
460 // pass it on to the default web search provider.
461 if (handleWebSearchIntent(intent)) {
462 return;
463 }
464
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700465 UrlData urlData = getUrlDataFromIntent(intent);
466 if (urlData.isEmpty()) {
467 urlData = new UrlData(mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800468 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700469
Grace Klobacc634032009-07-28 15:58:19 -0700470 final String appId = intent
471 .getStringExtra(Browser.EXTRA_APPLICATION_ID);
Leon Scroggins47208682010-04-07 17:59:48 -0400472 if ((Intent.ACTION_VIEW.equals(action)
473 // If a voice search has no appId, it means that it came
474 // from the browser. In that case, reuse the current tab.
475 || (activateVoiceSearch && appId != null))
Grace Klobacc634032009-07-28 15:58:19 -0700476 && !getPackageName().equals(appId)
477 && (flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700478 Tab appTab = mTabControl.getTabFromId(appId);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700479 if (appTab != null) {
480 Log.i(LOGTAG, "Reusing tab for " + appId);
481 // Dismiss the subwindow if applicable.
482 dismissSubWindow(appTab);
483 // Since we might kill the WebView, remove it from the
484 // content view first.
485 removeTabFromContentView(appTab);
486 // Recreate the main WebView after destroying the old one.
487 // If the WebView has the same original url and is on that
488 // page, it can be reused.
489 boolean needsLoad =
Leon Scroggins6eac63e2010-03-15 18:19:14 -0400490 mTabControl.recreateWebView(appTab, urlData);
Ben Murdochbff2d602009-07-01 20:19:05 +0100491
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700492 if (current != appTab) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400493 switchToTab(mTabControl.getTabIndex(appTab));
494 if (needsLoad) {
Patrick Scott9d53da02010-02-19 10:19:01 -0500495 loadUrlDataIn(appTab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400496 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700497 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400498 // If the tab was the current tab, we have to attach
499 // it to the view system again.
500 attachTabToContentView(appTab);
501 if (needsLoad) {
Patrick Scott9d53da02010-02-19 10:19:01 -0500502 loadUrlDataIn(appTab, urlData);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700503 }
504 }
505 return;
Patrick Scottcd115892009-07-16 09:42:58 -0400506 } else {
507 // No matching application tab, try to find a regular tab
508 // with a matching url.
509 appTab = mTabControl.findUnusedTabWithUrl(urlData.mUrl);
Leon Scroggins25515f82009-08-19 15:31:58 -0400510 if (appTab != null) {
511 if (current != appTab) {
512 switchToTab(mTabControl.getTabIndex(appTab));
513 }
514 // Otherwise, we are already viewing the correct tab.
Patrick Scottcd115892009-07-16 09:42:58 -0400515 } else {
516 // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
517 // will be opened in a new tab unless we have reached
518 // MAX_TABS. Then the url will be opened in the current
519 // tab. If a new tab is created, it will have "true" for
520 // exit on close.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400521 openTabAndShow(urlData, true, appId);
Patrick Scottcd115892009-07-16 09:42:58 -0400522 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700523 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800524 } else {
Grace Kloba638d3f42009-11-23 10:35:04 -0800525 if (!urlData.isEmpty()
526 && urlData.mUrl.startsWith("about:debug")) {
527 if ("about:debug.dom".equals(urlData.mUrl)) {
528 current.getWebView().dumpDomTree(false);
529 } else if ("about:debug.dom.file".equals(urlData.mUrl)) {
530 current.getWebView().dumpDomTree(true);
531 } else if ("about:debug.render".equals(urlData.mUrl)) {
532 current.getWebView().dumpRenderTree(false);
533 } else if ("about:debug.render.file".equals(urlData.mUrl)) {
534 current.getWebView().dumpRenderTree(true);
535 } else if ("about:debug.display".equals(urlData.mUrl)) {
536 current.getWebView().dumpDisplayTree();
Mike Reed9b78e1d2010-01-13 14:40:52 -0800537 } else if (urlData.mUrl.startsWith("about:debug.drag")) {
538 int index = urlData.mUrl.codePointAt(16) - '0';
539 if (index <= 0 || index > 9) {
540 current.getWebView().setDragTracker(null);
541 } else {
542 current.getWebView().setDragTracker(new MeshTracker(index));
543 }
Grace Kloba638d3f42009-11-23 10:35:04 -0800544 } else {
545 mSettings.toggleDebugSettings();
546 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800547 return;
548 }
Leon Scroggins1f005d32009-08-10 17:36:42 -0400549 // Get rid of the subwindow if it exists
550 dismissSubWindow(current);
Leon Scroggins8588d152010-04-15 11:01:53 -0400551 // If the current Tab is being used as an application tab,
552 // remove the association, since the new Intent means that it is
553 // no longer associated with that application.
554 current.setAppId(null);
Patrick Scott9d53da02010-02-19 10:19:01 -0500555 loadUrlDataIn(current, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800556 }
557 }
558 }
559
Satish Sampath565505b2009-05-29 15:37:27 +0100560 private int parseUrlShortcut(String url) {
561 if (url == null) return SHORTCUT_INVALID;
562
563 // FIXME: quick search, need to be customized by setting
564 if (url.length() > 2 && url.charAt(1) == ' ') {
565 switch (url.charAt(0)) {
566 case 'g': return SHORTCUT_GOOGLE_SEARCH;
567 case 'w': return SHORTCUT_WIKIPEDIA_SEARCH;
568 case 'd': return SHORTCUT_DICTIONARY_SEARCH;
569 case 'l': return SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH;
570 }
571 }
572 return SHORTCUT_INVALID;
573 }
574
575 /**
576 * Launches the default web search activity with the query parameters if the given intent's data
577 * are identified as plain search terms and not URLs/shortcuts.
578 * @return true if the intent was handled and web search activity was launched, false if not.
579 */
580 private boolean handleWebSearchIntent(Intent intent) {
581 if (intent == null) return false;
582
583 String url = null;
584 final String action = intent.getAction();
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500585 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS.equals(
586 action)) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500587 return false;
588 }
Satish Sampath565505b2009-05-29 15:37:27 +0100589 if (Intent.ACTION_VIEW.equals(action)) {
Grace Kloba1e705052009-09-29 13:13:36 -0700590 Uri data = intent.getData();
591 if (data != null) url = data.toString();
Satish Sampath565505b2009-05-29 15:37:27 +0100592 } else if (Intent.ACTION_SEARCH.equals(action)
593 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
594 || Intent.ACTION_WEB_SEARCH.equals(action)) {
595 url = intent.getStringExtra(SearchManager.QUERY);
596 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100597 return handleWebSearchRequest(url, intent.getBundleExtra(SearchManager.APP_DATA),
598 intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
Satish Sampath565505b2009-05-29 15:37:27 +0100599 }
600
601 /**
602 * Launches the default web search activity with the query parameters if the given url string
603 * was identified as plain search terms and not URL/shortcut.
604 * @return true if the request was handled and web search activity was launched, false if not.
605 */
Bjorn Bringert04851702009-09-22 10:36:01 +0100606 private boolean handleWebSearchRequest(String inUrl, Bundle appData, String extraData) {
Satish Sampath565505b2009-05-29 15:37:27 +0100607 if (inUrl == null) return false;
608
609 // In general, we shouldn't modify URL from Intent.
610 // But currently, we get the user-typed URL from search box as well.
611 String url = fixUrl(inUrl).trim();
612
613 // URLs and site specific search shortcuts are handled by the regular flow of control, so
614 // return early.
Dan Egnor5ee906c2009-11-18 12:11:49 -0800615 if (Patterns.WEB_URL.matcher(url).matches()
Satish Sampathbc5b9f32009-06-04 18:21:40 +0100616 || ACCEPTED_URI_SCHEMA.matcher(url).matches()
Satish Sampath565505b2009-05-29 15:37:27 +0100617 || parseUrlShortcut(url) != SHORTCUT_INVALID) {
618 return false;
619 }
620
Leon Scroggins8d06e362010-03-24 14:45:57 -0400621 final ContentResolver cr = mResolver;
622 final String newUrl = url;
623 new AsyncTask<Void, Void, Void>() {
Michael Kolbe0a36662010-06-29 10:37:12 -0700624 @Override
Leon Scroggins8d06e362010-03-24 14:45:57 -0400625 protected Void doInBackground(Void... unused) {
626 Browser.updateVisitedHistory(cr, newUrl, false);
627 Browser.addSearchUrl(cr, newUrl);
628 return null;
629 }
630 }.execute();
Satish Sampath565505b2009-05-29 15:37:27 +0100631
632 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
633 intent.addCategory(Intent.CATEGORY_DEFAULT);
634 intent.putExtra(SearchManager.QUERY, url);
Satish Sampath15e9f2d2009-06-23 22:29:49 +0100635 if (appData != null) {
636 intent.putExtra(SearchManager.APP_DATA, appData);
637 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100638 if (extraData != null) {
639 intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
640 }
Grace Klobacc634032009-07-28 15:58:19 -0700641 intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName());
Satish Sampath565505b2009-05-29 15:37:27 +0100642 startActivity(intent);
643
644 return true;
645 }
646
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700647 private UrlData getUrlDataFromIntent(Intent intent) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500648 String url = "";
Grace Kloba068e48b2010-01-26 18:11:27 -0800649 Map<String, String> headers = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800650 if (intent != null) {
651 final String action = intent.getAction();
652 if (Intent.ACTION_VIEW.equals(action)) {
653 url = smartUrlFilter(intent.getData());
654 if (url != null && url.startsWith("content:")) {
655 /* Append mimetype so webview knows how to display */
656 String mimeType = intent.resolveType(getContentResolver());
657 if (mimeType != null) {
658 url += "?" + mimeType;
659 }
660 }
Grace Kloba068e48b2010-01-26 18:11:27 -0800661 if (url != null && url.startsWith("http")) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800662 final Bundle pairs = intent
663 .getBundleExtra(Browser.EXTRA_HEADERS);
Grace Kloba2d508ed2010-01-28 11:39:15 -0800664 if (pairs != null && !pairs.isEmpty()) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800665 Iterator<String> iter = pairs.keySet().iterator();
Grace Kloba068e48b2010-01-26 18:11:27 -0800666 headers = new HashMap<String, String>();
Grace Kloba00f54c52010-01-27 14:53:51 -0800667 while (iter.hasNext()) {
668 String key = iter.next();
669 headers.put(key, pairs.getString(key));
Grace Kloba068e48b2010-01-26 18:11:27 -0800670 }
671 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700672 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800673 } else if (Intent.ACTION_SEARCH.equals(action)
674 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
675 || Intent.ACTION_WEB_SEARCH.equals(action)) {
676 url = intent.getStringExtra(SearchManager.QUERY);
677 if (url != null) {
678 mLastEnteredUrl = url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800679 // In general, we shouldn't modify URL from Intent.
680 // But currently, we get the user-typed URL from search box as well.
681 url = fixUrl(url);
682 url = smartUrlFilter(url);
Leon Scroggins8d06e362010-03-24 14:45:57 -0400683 final ContentResolver cr = mResolver;
684 final String newUrl = url;
685 new AsyncTask<Void, Void, Void>() {
Michael Kolbe0a36662010-06-29 10:37:12 -0700686 @Override
Leon Scroggins8d06e362010-03-24 14:45:57 -0400687 protected Void doInBackground(Void... unused) {
688 Browser.updateVisitedHistory(cr, newUrl, false);
689 return null;
690 }
691 }.execute();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800692 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
693 if (url.contains(searchSource)) {
694 String source = null;
695 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
696 if (appData != null) {
Bjorn Bringert10d1cca2010-02-10 14:22:12 +0000697 source = appData.getString(Search.SOURCE);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800698 }
699 if (TextUtils.isEmpty(source)) {
700 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
701 }
702 url = url.replace(searchSource, "&source=android-"+source+"&");
703 }
704 }
705 }
706 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500707 return new UrlData(url, headers, intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800708 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500709 /* package */ void showVoiceTitleBar(String title) {
710 mTitleBar.setInVoiceMode(true);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500711 mTitleBar.setDisplayTitle(title);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400712
713 if (!mXLargeScreenSize) {
714 mFakeTitleBar.setInVoiceMode(true);
715 mFakeTitleBar.setDisplayTitle(title);
716 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500717 }
718 /* package */ void revertVoiceTitleBar() {
719 mTitleBar.setInVoiceMode(false);
Leon Scroggins003a5dd2010-03-10 12:13:14 -0500720 mTitleBar.setDisplayTitle(mUrl);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400721
722 if (!mXLargeScreenSize) {
723 mFakeTitleBar.setInVoiceMode(false);
724 mFakeTitleBar.setDisplayTitle(mUrl);
725 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500726 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800727 /* package */ static String fixUrl(String inUrl) {
Cary Clark652ff872009-09-10 13:34:44 -0400728 // FIXME: Converting the url to lower case
729 // duplicates functionality in smartUrlFilter().
730 // However, changing all current callers of fixUrl to
731 // call smartUrlFilter in addition may have unwanted
732 // consequences, and is deferred for now.
733 int colon = inUrl.indexOf(':');
734 boolean allLower = true;
735 for (int index = 0; index < colon; index++) {
736 char ch = inUrl.charAt(index);
737 if (!Character.isLetter(ch)) {
738 break;
739 }
740 allLower &= Character.isLowerCase(ch);
741 if (index == colon - 1 && !allLower) {
742 inUrl = inUrl.substring(0, colon).toLowerCase()
743 + inUrl.substring(colon);
744 }
745 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800746 if (inUrl.startsWith("http://") || inUrl.startsWith("https://"))
747 return inUrl;
748 if (inUrl.startsWith("http:") ||
749 inUrl.startsWith("https:")) {
750 if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) {
751 inUrl = inUrl.replaceFirst("/", "//");
752 } else inUrl = inUrl.replaceFirst(":", "://");
753 }
754 return inUrl;
755 }
756
Grace Kloba22ac16e2009-10-07 18:00:23 -0700757 @Override
758 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800759 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700760 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800761 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
762 }
763
764 if (!mActivityInPause) {
765 Log.e(LOGTAG, "BrowserActivity is already resumed.");
766 return;
767 }
768
Mike Reed7bfa63b2009-05-28 11:08:32 -0400769 mTabControl.resumeCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800770 mActivityInPause = false;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400771 resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800772
773 if (mWakeLock.isHeld()) {
774 mHandler.removeMessages(RELEASE_WAKELOCK);
775 mWakeLock.release();
776 }
777
The Android Open Source Project0c908882009-03-03 19:32:16 -0800778 registerReceiver(mNetworkStateIntentReceiver,
779 mNetworkStateChangedFilter);
780 WebView.enablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800781 }
782
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400783 /**
784 * Since the actual title bar is embedded in the WebView, and removing it
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400785 * would change its appearance, use a different TitleBar to show overlayed
786 * at the top of the screen, when the menu is open or the page is loading.
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400787 */
788 private TitleBar mFakeTitleBar;
789
790 /**
791 * Keeps track of whether the options menu is open. This is important in
792 * determining whether to show or hide the title bar overlay.
793 */
794 private boolean mOptionsMenuOpen;
795
796 /**
797 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
798 * of whether the configuration has changed. The first onMenuOpened call
799 * after a configuration change is simply a reopening of the same menu
800 * (i.e. mIconView did not change).
801 */
802 private boolean mConfigChanged;
803
804 /**
805 * Whether or not the options menu is in its smaller, icon menu form. When
806 * true, we want the title bar overlay to be up. When false, we do not.
807 * Only meaningful if mOptionsMenuOpen is true.
808 */
809 private boolean mIconView;
810
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400811 @Override
812 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400813 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
814 if (mOptionsMenuOpen) {
815 if (mConfigChanged) {
816 // We do not need to make any changes to the state of the
817 // title bar, since the only thing that happened was a
818 // change in orientation
819 mConfigChanged = false;
820 } else {
821 if (mIconView) {
822 // Switching the menu to expanded view, so hide the
823 // title bar.
824 hideFakeTitleBar();
825 mIconView = false;
826 } else {
827 // Switching the menu back to icon view, so show the
828 // title bar once again.
829 showFakeTitleBar();
830 mIconView = true;
831 }
832 }
833 } else {
834 // The options menu is closed, so open it, and show the title
835 showFakeTitleBar();
836 mOptionsMenuOpen = true;
837 mConfigChanged = false;
838 mIconView = true;
839 }
840 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400841 return true;
842 }
843
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400844 private void showFakeTitleBar() {
Leon Scrogginsd746a942010-05-19 13:21:44 -0400845 if (mXLargeScreenSize) return;
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400846 if (mFakeTitleBar.getParent() == null && mActiveTabsPage == null
Grace Kloba847c25b2010-03-30 16:00:26 -0700847 && !mActivityInPause) {
848 WebView mainView = mTabControl.getCurrentWebView();
849 // if there is no current WebView, don't show the faked title bar;
Grace Kloba65190702010-04-02 23:37:26 -0700850 if (mainView == null) {
Cary Clarka0464552009-09-29 13:00:45 -0400851 return;
852 }
Leon Scroggins79e36d92010-04-29 16:01:46 +0100853 // Do not need to check for null, since the current tab will have
854 // at least a main WebView, or we would have returned above.
Cary Clark01cfcdd2010-06-04 16:36:45 -0400855 if (dialogIsUp()) {
Leon Scroggins79e36d92010-04-29 16:01:46 +0100856 // Do not show the fake title bar, which would cover up the
Cary Clark01cfcdd2010-06-04 16:36:45 -0400857 // find or select dialog.
Leon Scroggins79e36d92010-04-29 16:01:46 +0100858 return;
859 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400860
861 WindowManager manager
862 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
863
864 // Add the title bar to the window manager so it can receive touches
865 // while the menu is up
866 WindowManager.LayoutParams params
867 = new WindowManager.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -0800868 ViewGroup.LayoutParams.MATCH_PARENT,
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400869 ViewGroup.LayoutParams.WRAP_CONTENT,
Grace Kloba847c25b2010-03-30 16:00:26 -0700870 WindowManager.LayoutParams.TYPE_APPLICATION,
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400871 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
Leon Scroggins68549862009-09-21 16:02:01 -0400872 PixelFormat.TRANSLUCENT);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400873 params.gravity = Gravity.TOP;
Grace Kloba847c25b2010-03-30 16:00:26 -0700874 boolean atTop = mainView.getScrollY() == 0;
Leon Scroggins83932c72009-09-30 11:55:54 -0400875 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
Grace Kloba13f9ace2010-03-18 21:44:14 -0700876 manager.addView(mFakeTitleBar, params);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400877 }
878 }
879
880 @Override
881 public void onOptionsMenuClosed(Menu menu) {
882 mOptionsMenuOpen = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -0400883 if (!mInLoad) {
884 hideFakeTitleBar();
885 } else if (!mIconView) {
886 // The page is currently loading, and we are in expanded mode, so
887 // we were not showing the menu. Show it once again. It will be
888 // removed when the page finishes.
889 showFakeTitleBar();
890 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400891 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700892
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400893 private void hideFakeTitleBar() {
Leon Scrogginsd746a942010-05-19 13:21:44 -0400894 if (mXLargeScreenSize || mFakeTitleBar.getParent() == null) return;
Leon Scroggins20329572009-09-23 17:42:41 -0400895 WindowManager.LayoutParams params = (WindowManager.LayoutParams)
Grace Kloba13f9ace2010-03-18 21:44:14 -0700896 mFakeTitleBar.getLayoutParams();
Leon Scroggins20329572009-09-23 17:42:41 -0400897 WebView mainView = mTabControl.getCurrentWebView();
898 // Although we decided whether or not to animate based on the current
899 // scroll position, the scroll position may have changed since the
900 // fake title bar was displayed. Make sure it has the appropriate
901 // animation/lack thereof before removing.
902 params.windowAnimations = mainView != null && mainView.getScrollY() == 0
Leon Scroggins83932c72009-09-30 11:55:54 -0400903 ? 0 : R.style.TitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400904 WindowManager manager
905 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Grace Kloba13f9ace2010-03-18 21:44:14 -0700906 manager.updateViewLayout(mFakeTitleBar, params);
907 manager.removeView(mFakeTitleBar);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400908 }
909
The Android Open Source Project0c908882009-03-03 19:32:16 -0800910 /**
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400911 * Special method for the fake title bar to call when displaying its context
912 * menu, since it is in its own Window, and its parent does not show a
913 * context menu.
914 */
915 /* package */ void showTitleBarContextMenu() {
Cary Clark65f4a3c2009-09-28 17:05:06 -0400916 if (null == mTitleBar.getParent()) {
917 return;
918 }
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400919 openContextMenu(mTitleBar);
920 }
921
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400922 @Override
923 public void onContextMenuClosed(Menu menu) {
924 super.onContextMenuClosed(menu);
925 if (mInLoad) {
926 showFakeTitleBar();
927 }
928 }
929
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400930 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800931 * onSaveInstanceState(Bundle map)
932 * onSaveInstanceState is called right before onStop(). The map contains
933 * the saved state.
934 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700935 @Override
936 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700937 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800938 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
939 }
940 // the default implementation requires each view to have an id. As the
941 // browser handles the state itself and it doesn't use id for the views,
942 // don't call the default implementation. Otherwise it will trigger the
943 // warning like this, "couldn't save which view has focus because the
944 // focused view XXX has no id".
945
946 // Save all the tabs
947 mTabControl.saveState(outState);
948 }
949
Grace Kloba22ac16e2009-10-07 18:00:23 -0700950 @Override
951 protected void onPause() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800952 super.onPause();
953
954 if (mActivityInPause) {
955 Log.e(LOGTAG, "BrowserActivity is already paused.");
956 return;
957 }
958
Mike Reed7bfa63b2009-05-28 11:08:32 -0400959 mTabControl.pauseCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800960 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400961 if (mTabControl.getCurrentIndex() >= 0 && !pauseWebViewTimers()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800962 mWakeLock.acquire();
963 mHandler.sendMessageDelayed(mHandler
964 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
965 }
966
Leon Scrogginsa2ab6a72009-09-11 11:49:52 -0400967 // FIXME: This removes the active tabs page and resets the menu to
968 // MAIN_MENU. A better solution might be to do this work in onNewIntent
969 // but then we would need to save it in onSaveInstanceState and restore
970 // it in onCreate/onRestoreInstanceState
971 if (mActiveTabsPage != null) {
972 removeActiveTabPage(true);
973 }
974
The Android Open Source Project0c908882009-03-03 19:32:16 -0800975 cancelStopToast();
976
977 // unregister network state listener
978 unregisterReceiver(mNetworkStateIntentReceiver);
979 WebView.disablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800980 }
981
Grace Kloba22ac16e2009-10-07 18:00:23 -0700982 @Override
983 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700984 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800985 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
986 }
987 super.onDestroy();
Grace Kloba0923d692009-09-23 21:37:25 -0700988
Leon Scroggins8d5fa432009-10-02 15:55:59 -0400989 if (mUploadMessage != null) {
990 mUploadMessage.onReceiveValue(null);
991 mUploadMessage = null;
992 }
993
Grace Kloba0923d692009-09-23 21:37:25 -0700994 if (mTabControl == null) return;
995
Grace Kloba1fc98a32009-10-21 13:23:08 -0700996 // Remove the fake title bar if it is there
997 hideFakeTitleBar();
998
The Android Open Source Project0c908882009-03-03 19:32:16 -0800999 // Remove the current tab and sub window
Grace Kloba22ac16e2009-10-07 18:00:23 -07001000 Tab t = mTabControl.getCurrentTab();
Patrick Scottfb5e77f2009-04-08 19:17:37 -07001001 if (t != null) {
1002 dismissSubWindow(t);
1003 removeTabFromContentView(t);
1004 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001005 // Destroy all the tabs
1006 mTabControl.destroy();
1007 WebIconDatabase.getInstance().close();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001008
Grace Klobab4da0ad2009-05-14 14:45:40 -07001009 unregisterReceiver(mPackageInstallationReceiver);
Bjorn Bringerta7611812010-03-24 11:12:02 +00001010
1011 // Stop watching the default geolocation permissions
1012 mSystemAllowGeolocationOrigins.stop();
1013 mSystemAllowGeolocationOrigins = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001014 }
1015
1016 @Override
1017 public void onConfigurationChanged(Configuration newConfig) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04001018 mConfigChanged = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001019 super.onConfigurationChanged(newConfig);
1020
1021 if (mPageInfoDialog != null) {
1022 mPageInfoDialog.dismiss();
1023 showPageInfo(
1024 mPageInfoView,
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05001025 mPageInfoFromShowSSLCertificateOnError);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001026 }
1027 if (mSSLCertificateDialog != null) {
1028 mSSLCertificateDialog.dismiss();
1029 showSSLCertificate(
1030 mSSLCertificateView);
1031 }
1032 if (mSSLCertificateOnErrorDialog != null) {
1033 mSSLCertificateOnErrorDialog.dismiss();
1034 showSSLCertificateOnError(
1035 mSSLCertificateOnErrorView,
1036 mSSLCertificateOnErrorHandler,
1037 mSSLCertificateOnErrorError);
1038 }
1039 if (mHttpAuthenticationDialog != null) {
1040 String title = ((TextView) mHttpAuthenticationDialog
1041 .findViewById(com.android.internal.R.id.alertTitle)).getText()
1042 .toString();
1043 String name = ((TextView) mHttpAuthenticationDialog
1044 .findViewById(R.id.username_edit)).getText().toString();
1045 String password = ((TextView) mHttpAuthenticationDialog
1046 .findViewById(R.id.password_edit)).getText().toString();
1047 int focusId = mHttpAuthenticationDialog.getCurrentFocus()
1048 .getId();
1049 mHttpAuthenticationDialog.dismiss();
1050 showHttpAuthentication(mHttpAuthHandler, null, null, title,
1051 name, password, focusId);
1052 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001053 }
1054
Grace Kloba22ac16e2009-10-07 18:00:23 -07001055 @Override
1056 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001057 super.onLowMemory();
1058 mTabControl.freeMemory();
1059 }
1060
Cary Clarkff4d92c2010-03-25 11:17:03 -04001061 private void resumeWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001062 Tab tab = mTabControl.getCurrentTab();
Cary Clarkff4d92c2010-03-25 11:17:03 -04001063 if (tab == null) return; // monkey can trigger this
Grace Kloba22ac16e2009-10-07 18:00:23 -07001064 boolean inLoad = tab.inLoad();
1065 if ((!mActivityInPause && !inLoad) || (mActivityInPause && inLoad)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001066 CookieSyncManager.getInstance().startSync();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001067 WebView w = tab.getWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001068 if (w != null) {
1069 w.resumeTimers();
1070 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001071 }
1072 }
1073
Mike Reed7bfa63b2009-05-28 11:08:32 -04001074 private boolean pauseWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001075 Tab tab = mTabControl.getCurrentTab();
1076 boolean inLoad = tab.inLoad();
1077 if (mActivityInPause && !inLoad) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001078 CookieSyncManager.getInstance().stopSync();
1079 WebView w = mTabControl.getCurrentWebView();
1080 if (w != null) {
1081 w.pauseTimers();
1082 }
1083 return true;
1084 } else {
1085 return false;
1086 }
1087 }
1088
The Android Open Source Project0c908882009-03-03 19:32:16 -08001089 // Open the icon database and retain all the icons for visited sites.
1090 private void retainIconsOnStartup() {
1091 final WebIconDatabase db = WebIconDatabase.getInstance();
1092 db.open(getDir("icons", 0).getPath());
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001093 Cursor c = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001094 try {
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001095 c = Browser.getAllBookmarks(mResolver);
1096 if (c.moveToFirst()) {
1097 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
1098 do {
1099 String url = c.getString(urlIndex);
1100 db.retainIconForPageUrl(url);
1101 } while (c.moveToNext());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001102 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001103 } catch (IllegalStateException e) {
1104 Log.e(LOGTAG, "retainIconsOnStartup", e);
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001105 } finally {
1106 if (c!= null) c.close();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001107 }
1108 }
1109
1110 // Helper method for getting the top window.
1111 WebView getTopWindow() {
1112 return mTabControl.getCurrentTopWebView();
1113 }
1114
Grace Kloba22ac16e2009-10-07 18:00:23 -07001115 TabControl getTabControl() {
1116 return mTabControl;
1117 }
1118
The Android Open Source Project0c908882009-03-03 19:32:16 -08001119 @Override
1120 public boolean onCreateOptionsMenu(Menu menu) {
1121 super.onCreateOptionsMenu(menu);
1122
1123 MenuInflater inflater = getMenuInflater();
1124 inflater.inflate(R.menu.browser, menu);
1125 mMenu = menu;
1126 updateInLoadMenuItems();
1127 return true;
1128 }
1129
1130 /**
1131 * As the menu can be open when loading state changes
1132 * we must manually update the state of the stop/reload menu
1133 * item
1134 */
1135 private void updateInLoadMenuItems() {
1136 if (mMenu == null) {
1137 return;
1138 }
Michael Kolbe0a36662010-06-29 10:37:12 -07001139 MenuItem dest = mMenu.findItem(R.id.stop_reload_menu_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001140 MenuItem src = mInLoad ?
1141 mMenu.findItem(R.id.stop_menu_id):
Michael Kolbe0a36662010-06-29 10:37:12 -07001142 mMenu.findItem(R.id.reload_menu_id);
1143 if (src != null) {
1144 dest.setIcon(src.getIcon());
1145 dest.setTitle(src.getTitle());
1146 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001147 }
1148
1149 @Override
1150 public boolean onContextItemSelected(MenuItem item) {
1151 // chording is not an issue with context menus, but we use the same
1152 // options selector, so set mCanChord to true so we can access them.
1153 mCanChord = true;
1154 int id = item.getItemId();
Leon Scroggins96afcb12009-12-10 12:35:56 -05001155 boolean result = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001156 switch (id) {
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001157 // For the context menu from the title bar
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001158 case R.id.title_bar_copy_page_url:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001159 Tab currentTab = mTabControl.getCurrentTab();
1160 if (null == currentTab) {
1161 result = false;
1162 break;
1163 }
1164 WebView mainView = currentTab.getWebView();
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001165 if (null == mainView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001166 result = false;
1167 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001168 }
Leon Scroggins96afcb12009-12-10 12:35:56 -05001169 copy(mainView.getUrl());
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001170 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001171 // -- Browser context menu
1172 case R.id.open_context_menu_id:
The Android Open Source Project0c908882009-03-03 19:32:16 -08001173 case R.id.bookmark_context_menu_id:
1174 case R.id.save_link_context_menu_id:
1175 case R.id.share_link_context_menu_id:
1176 case R.id.copy_link_context_menu_id:
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001177 final WebView webView = getTopWindow();
1178 if (null == webView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001179 result = false;
1180 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001181 }
1182 final HashMap hrefMap = new HashMap();
1183 hrefMap.put("webview", webView);
1184 final Message msg = mHandler.obtainMessage(
1185 FOCUS_NODE_HREF, id, 0, hrefMap);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001186 webView.requestFocusNodeHref(msg);
1187 break;
1188
1189 default:
1190 // For other context menus
Leon Scroggins96afcb12009-12-10 12:35:56 -05001191 result = onOptionsItemSelected(item);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001192 }
1193 mCanChord = false;
Leon Scroggins96afcb12009-12-10 12:35:56 -05001194 return result;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001195 }
1196
1197 private Bundle createGoogleSearchSourceBundle(String source) {
1198 Bundle bundle = new Bundle();
Bjorn Bringert10d1cca2010-02-10 14:22:12 +00001199 bundle.putString(Search.SOURCE, source);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001200 return bundle;
1201 }
1202
Leon Scroggins8ad29922010-02-16 12:33:55 -05001203 /* package */ void editUrl() {
Leon Scroggins68579392009-09-15 15:31:54 -04001204 if (mOptionsMenuOpen) closeOptionsMenu();
Leon Scroggins5bbe9802009-07-31 13:10:55 -04001205 String url = (getTopWindow() == null) ? null : getTopWindow().getUrl();
Grace Kloba83f47342009-07-20 10:44:31 -07001206 startSearch(mSettings.getHomePage().equals(url) ? null : url, true,
Leon Scroggins8ad29922010-02-16 12:33:55 -05001207 null, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001208 }
1209
Leon Scroggins8ad29922010-02-16 12:33:55 -05001210 /**
1211 * Overriding this to insert a local information bundle
1212 */
The Android Open Source Project0c908882009-03-03 19:32:16 -08001213 @Override
1214 public void startSearch(String initialQuery, boolean selectInitialQuery,
1215 Bundle appSearchData, boolean globalSearch) {
1216 if (appSearchData == null) {
1217 appSearchData = createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_TYPE);
1218 }
1219 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1220 }
1221
Leon Scroggins1f005d32009-08-10 17:36:42 -04001222 /**
1223 * Switch tabs. Called by the TitleBarSet when sliding the title bar
1224 * results in changing tabs.
Leon Scroggins160a7e72009-08-14 18:28:01 -04001225 * @param index Index of the tab to change to, as defined by
1226 * mTabControl.getTabIndex(Tab t).
1227 * @return boolean True if we successfully switched to a different tab. If
1228 * the indexth tab is null, or if that tab is the same as
1229 * the current one, return false.
Leon Scroggins1f005d32009-08-10 17:36:42 -04001230 */
Leon Scroggins160a7e72009-08-14 18:28:01 -04001231 /* package */ boolean switchToTab(int index) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001232 Tab tab = mTabControl.getTab(index);
1233 Tab currentTab = mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001234 if (tab == null || tab == currentTab) {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001235 return false;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001236 }
1237 if (currentTab != null) {
1238 // currentTab may be null if it was just removed. In that case,
1239 // we do not need to remove it
1240 removeTabFromContentView(currentTab);
1241 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001242 mTabControl.setCurrentTab(tab);
1243 attachTabToContentView(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001244 resetTitleIconAndProgress();
1245 updateLockIconToLatest();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001246 return true;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001247 }
1248
Grace Kloba22ac16e2009-10-07 18:00:23 -07001249 /* package */ Tab openTabToHomePage() {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001250 return openTabAndShow(mSettings.getHomePage(), false, null);
1251 }
1252
Leon Scroggins1f005d32009-08-10 17:36:42 -04001253 /* package */ void closeCurrentWindow() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001254 final Tab current = mTabControl.getCurrentTab();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001255 if (mTabControl.getTabCount() == 1) {
Leon Scroggins30444232009-09-04 18:36:20 -04001256 // This is the last tab. Open a new one, with the home
1257 // page and close the current one.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001258 openTabToHomePage();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001259 closeTab(current);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001260 return;
1261 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001262 final Tab parent = current.getParentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001263 int indexToShow = -1;
1264 if (parent != null) {
1265 indexToShow = mTabControl.getTabIndex(parent);
1266 } else {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001267 final int currentIndex = mTabControl.getCurrentIndex();
1268 // Try to move to the tab to the right
1269 indexToShow = currentIndex + 1;
1270 if (indexToShow > mTabControl.getTabCount() - 1) {
1271 // Try to move to the tab to the left
1272 indexToShow = currentIndex - 1;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001273 }
1274 }
Leon Scroggins160a7e72009-08-14 18:28:01 -04001275 if (switchToTab(indexToShow)) {
1276 // Close window
1277 closeTab(current);
1278 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001279 }
1280
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001281 private ActiveTabsPage mActiveTabsPage;
1282
1283 /**
1284 * Remove the active tabs page.
1285 * @param needToAttach If true, the active tabs page did not attach a tab
1286 * to the content view, so we need to do that here.
1287 */
1288 /* package */ void removeActiveTabPage(boolean needToAttach) {
1289 mContentView.removeView(mActiveTabsPage);
Leon Scrogginsd746a942010-05-19 13:21:44 -04001290 mTitleBar.setVisibility(View.VISIBLE);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001291 mActiveTabsPage = null;
1292 mMenuState = R.id.MAIN_MENU;
1293 if (needToAttach) {
1294 attachTabToContentView(mTabControl.getCurrentTab());
1295 }
1296 getTopWindow().requestFocus();
1297 }
1298
Cary Clark01cfcdd2010-06-04 16:36:45 -04001299 private WebView showDialog(WebDialog dialog) {
1300 // Need to do something special for Tablet
1301 Tab tab = mTabControl.getCurrentTab();
1302 if (tab.getSubWebView() == null) {
1303 // If the find or select is being performed on the main webview,
1304 // remove the embedded title bar.
1305 WebView mainView = tab.getWebView();
1306 if (mainView != null) {
1307 mainView.setEmbeddedTitleBar(null);
1308 }
1309 }
1310 hideFakeTitleBar();
1311 mMenuState = EMPTY_MENU;
1312 return tab.showDialog(dialog);
1313 }
1314
The Android Open Source Project0c908882009-03-03 19:32:16 -08001315 @Override
1316 public boolean onOptionsItemSelected(MenuItem item) {
1317 if (!mCanChord) {
1318 // The user has already fired a shortcut with this hold down of the
1319 // menu key.
1320 return false;
1321 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001322 if (null == getTopWindow()) {
Leon Scroggins0d7ae0e2009-06-05 11:04:45 -04001323 return false;
1324 }
Grace Kloba6ee9c492009-07-13 10:04:34 -07001325 if (mMenuIsDown) {
1326 // The shortcut action consumes the MENU. Even if it is still down,
1327 // it won't trigger the next shortcut action. In the case of the
1328 // shortcut action triggering a new activity, like Bookmarks, we
1329 // won't get onKeyUp for MENU. So it is important to reset it here.
1330 mMenuIsDown = false;
1331 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001332 switch (item.getItemId()) {
1333 // -- Main menu
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001334 case R.id.new_tab_menu_id:
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001335 openTabToHomePage();
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001336 break;
1337
Leon Scroggins64b80f32009-08-07 12:03:34 -04001338 case R.id.goto_menu_id:
Leon Scroggins8ad29922010-02-16 12:33:55 -05001339 editUrl();
Leon Scrogginsb3a5bed2009-09-28 11:21:56 -04001340 break;
1341
1342 case R.id.bookmarks_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001343 bookmarksOrHistoryPicker(false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001344 break;
1345
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001346 case R.id.active_tabs_menu_id:
1347 mActiveTabsPage = new ActiveTabsPage(this, mTabControl);
1348 removeTabFromContentView(mTabControl.getCurrentTab());
Leon Scrogginsd746a942010-05-19 13:21:44 -04001349 mTitleBar.setVisibility(View.GONE);
Leon Scroggins43de6162009-09-14 19:59:58 -04001350 hideFakeTitleBar();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001351 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
1352 mActiveTabsPage.requestFocus();
1353 mMenuState = EMPTY_MENU;
1354 break;
1355
Leon Scroggins1f005d32009-08-10 17:36:42 -04001356 case R.id.add_bookmark_menu_id:
Leon Scroggins571b3762010-05-26 10:25:01 -04001357 bookmarkCurrentPage();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001358 break;
1359
1360 case R.id.stop_reload_menu_id:
1361 if (mInLoad) {
1362 stopLoading();
1363 } else {
1364 getTopWindow().reload();
1365 }
1366 break;
1367
1368 case R.id.back_menu_id:
1369 getTopWindow().goBack();
1370 break;
1371
1372 case R.id.forward_menu_id:
1373 getTopWindow().goForward();
1374 break;
1375
1376 case R.id.close_menu_id:
1377 // Close the subwindow if it exists.
1378 if (mTabControl.getCurrentSubWindow() != null) {
1379 dismissSubWindow(mTabControl.getCurrentTab());
1380 break;
1381 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001382 closeCurrentWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001383 break;
1384
1385 case R.id.homepage_menu_id:
Grace Kloba22ac16e2009-10-07 18:00:23 -07001386 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001387 if (current != null) {
1388 dismissSubWindow(current);
Leon Scroggins92472e82010-02-17 16:32:28 -05001389 loadUrl(current.getWebView(), mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001390 }
1391 break;
1392
1393 case R.id.preferences_menu_id:
1394 Intent intent = new Intent(this,
1395 BrowserPreferencesPage.class);
Leon Scrogginsd5304942009-12-10 16:11:39 -05001396 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1397 getTopWindow().getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001398 startActivityForResult(intent, PREFERENCES_PAGE);
1399 break;
1400
1401 case R.id.find_menu_id:
Cary Clark01cfcdd2010-06-04 16:36:45 -04001402 showFindDialog();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001403 break;
1404
The Android Open Source Project0c908882009-03-03 19:32:16 -08001405 case R.id.page_info_menu_id:
1406 showPageInfo(mTabControl.getCurrentTab(), false);
1407 break;
1408
1409 case R.id.classic_history_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001410 bookmarksOrHistoryPicker(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001411 break;
1412
Leon Scroggins96afcb12009-12-10 12:35:56 -05001413 case R.id.title_bar_share_page_url:
The Android Open Source Project0c908882009-03-03 19:32:16 -08001414 case R.id.share_page_menu_id:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001415 Tab currentTab = mTabControl.getCurrentTab();
1416 if (null == currentTab) {
1417 mCanChord = false;
1418 return false;
1419 }
1420 currentTab.populatePickerData();
1421 sharePage(this, currentTab.getTitle(),
1422 currentTab.getUrl(), currentTab.getFavicon(),
1423 createScreenshot(currentTab.getWebView()));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001424 break;
1425
1426 case R.id.dump_nav_menu_id:
1427 getTopWindow().debugDump();
1428 break;
1429
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001430 case R.id.dump_counters_menu_id:
1431 getTopWindow().dumpV8Counters();
1432 break;
1433
The Android Open Source Project0c908882009-03-03 19:32:16 -08001434 case R.id.zoom_in_menu_id:
1435 getTopWindow().zoomIn();
1436 break;
1437
1438 case R.id.zoom_out_menu_id:
1439 getTopWindow().zoomOut();
1440 break;
1441
1442 case R.id.view_downloads_menu_id:
1443 viewDownloads(null);
1444 break;
1445
The Android Open Source Project0c908882009-03-03 19:32:16 -08001446 case R.id.window_one_menu_id:
1447 case R.id.window_two_menu_id:
1448 case R.id.window_three_menu_id:
1449 case R.id.window_four_menu_id:
1450 case R.id.window_five_menu_id:
1451 case R.id.window_six_menu_id:
1452 case R.id.window_seven_menu_id:
1453 case R.id.window_eight_menu_id:
1454 {
1455 int menuid = item.getItemId();
1456 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1457 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001458 Tab desiredTab = mTabControl.getTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001459 if (desiredTab != null &&
1460 desiredTab != mTabControl.getCurrentTab()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001461 switchToTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001462 }
1463 break;
1464 }
1465 }
1466 }
1467 break;
1468
1469 default:
1470 if (!super.onOptionsItemSelected(item)) {
1471 return false;
1472 }
1473 // Otherwise fall through.
1474 }
1475 mCanChord = false;
1476 return true;
1477 }
1478
Leon Scroggins571b3762010-05-26 10:25:01 -04001479 /* package */ void bookmarkCurrentPage() {
1480 Intent i = new Intent(BrowserActivity.this,
1481 AddBookmarkPage.class);
1482 WebView w = getTopWindow();
1483 i.putExtra("url", w.getUrl());
1484 i.putExtra("title", w.getTitle());
1485 i.putExtra("touch_icon_url", w.getTouchIconUrl());
1486 i.putExtra("thumbnail", createScreenshot(w));
1487 startActivity(i);
1488 }
1489
Cary Clark01cfcdd2010-06-04 16:36:45 -04001490 private boolean dialogIsUp() {
1491 return null != mFindDialog && mFindDialog.isVisible() ||
1492 null != mSelectDialog && mSelectDialog.isVisible();
1493 }
1494
1495 private boolean closeDialog(WebDialog dialog) {
1496 if (null == dialog || !dialog.isVisible()) return false;
Leon Scroggins79e36d92010-04-29 16:01:46 +01001497 Tab currentTab = mTabControl.getCurrentTab();
Cary Clark01cfcdd2010-06-04 16:36:45 -04001498 currentTab.closeDialog(dialog);
1499 dialog.dismiss();
1500 return true;
1501 }
1502
1503 /*
1504 * Remove the find dialog or select dialog.
1505 */
1506 public void closeDialogs() {
1507 if (!(closeDialog(mFindDialog) || closeDialog(mSelectDialog))) return;
Leon Scrogginsd746a942010-05-19 13:21:44 -04001508 if (!mXLargeScreenSize) {
1509 // If the Find was being performed in the main WebView, replace the
1510 // embedded title bar.
Cary Clark01cfcdd2010-06-04 16:36:45 -04001511 Tab currentTab = mTabControl.getCurrentTab();
Leon Scrogginsd746a942010-05-19 13:21:44 -04001512 if (currentTab.getSubWebView() == null) {
1513 WebView mainView = currentTab.getWebView();
1514 if (mainView != null) {
1515 mainView.setEmbeddedTitleBar(mTitleBar);
1516 }
Leon Scroggins79e36d92010-04-29 16:01:46 +01001517 }
1518 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001519 mMenuState = R.id.MAIN_MENU;
Leon Scroggins79e36d92010-04-29 16:01:46 +01001520 if (mInLoad) {
1521 // The title bar was hidden, because otherwise it would cover up the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001522 // find or select dialog. Now that the dialog has been removed,
1523 // show the fake title bar once again.
Leon Scroggins79e36d92010-04-29 16:01:46 +01001524 showFakeTitleBar();
1525 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001526 }
1527
Cary Clark01cfcdd2010-06-04 16:36:45 -04001528 public void showFindDialog() {
1529 if (null == mFindDialog) {
1530 mFindDialog = new FindDialog(this);
1531 }
1532 showDialog(mFindDialog).setFindIsUp(true);
1533 }
1534
1535 public void setFindDialogText(String text) {
1536 mFindDialog.setText(text);
1537 }
1538
1539 public void showSelectDialog() {
1540 if (null == mSelectDialog) {
1541 mSelectDialog = new SelectDialog(this);
1542 }
1543 showDialog(mSelectDialog).setUpSelect();
1544 mSelectDialog.hideSoftInput();
1545 }
1546
Grace Kloba22ac16e2009-10-07 18:00:23 -07001547 @Override
1548 public boolean onPrepareOptionsMenu(Menu menu) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001549 // This happens when the user begins to hold down the menu key, so
1550 // allow them to chord to get a shortcut.
1551 mCanChord = true;
1552 // Note: setVisible will decide whether an item is visible; while
1553 // setEnabled() will decide whether an item is enabled, which also means
1554 // whether the matching shortcut key will function.
1555 super.onPrepareOptionsMenu(menu);
1556 switch (mMenuState) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001557 case EMPTY_MENU:
1558 if (mCurrentMenuState != mMenuState) {
1559 menu.setGroupVisible(R.id.MAIN_MENU, false);
1560 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1561 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001562 }
1563 break;
1564 default:
1565 if (mCurrentMenuState != mMenuState) {
1566 menu.setGroupVisible(R.id.MAIN_MENU, true);
1567 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1568 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001569 }
1570 final WebView w = getTopWindow();
1571 boolean canGoBack = false;
1572 boolean canGoForward = false;
1573 boolean isHome = false;
1574 if (w != null) {
1575 canGoBack = w.canGoBack();
1576 canGoForward = w.canGoForward();
1577 isHome = mSettings.getHomePage().equals(w.getUrl());
1578 }
1579 final MenuItem back = menu.findItem(R.id.back_menu_id);
1580 back.setEnabled(canGoBack);
1581
1582 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1583 home.setEnabled(!isHome);
1584
Michael Kolbe0a36662010-06-29 10:37:12 -07001585 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1586 forward.setEnabled(canGoForward);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001587
Michael Kolbe0a36662010-06-29 10:37:12 -07001588 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
1589 newtab.setEnabled(mTabControl.canCreateNewTab());
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001590
The Android Open Source Project0c908882009-03-03 19:32:16 -08001591 // decide whether to show the share link option
1592 PackageManager pm = getPackageManager();
1593 Intent send = new Intent(Intent.ACTION_SEND);
1594 send.setType("text/plain");
1595 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1596 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1597
The Android Open Source Project0c908882009-03-03 19:32:16 -08001598 boolean isNavDump = mSettings.isNavDump();
1599 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1600 nav.setVisible(isNavDump);
1601 nav.setEnabled(isNavDump);
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001602
1603 boolean showDebugSettings = mSettings.showDebugSettings();
1604 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1605 counter.setVisible(showDebugSettings);
1606 counter.setEnabled(showDebugSettings);
1607
The Android Open Source Project0c908882009-03-03 19:32:16 -08001608 break;
1609 }
1610 mCurrentMenuState = mMenuState;
1611 return true;
1612 }
1613
1614 @Override
1615 public void onCreateContextMenu(ContextMenu menu, View v,
1616 ContextMenuInfo menuInfo) {
Leon Scroggins571b3762010-05-26 10:25:01 -04001617 if (v instanceof TitleBarBase) {
Leon Scroggins4e9f89b2010-02-22 16:54:14 -05001618 return;
1619 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001620 WebView webview = (WebView) v;
1621 WebView.HitTestResult result = webview.getHitTestResult();
1622 if (result == null) {
1623 return;
1624 }
1625
1626 int type = result.getType();
1627 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1628 Log.w(LOGTAG,
1629 "We should not show context menu when nothing is touched");
1630 return;
1631 }
1632 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1633 // let TextView handles context menu
1634 return;
1635 }
1636
1637 // Note, http://b/issue?id=1106666 is requesting that
1638 // an inflated menu can be used again. This is not available
1639 // yet, so inflate each time (yuk!)
1640 MenuInflater inflater = getMenuInflater();
1641 inflater.inflate(R.menu.browsercontext, menu);
1642
1643 // Show the correct menu group
Leon Scroggins III9e997c72010-05-26 13:25:16 -04001644 final String extra = result.getExtra();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001645 menu.setGroupVisible(R.id.PHONE_MENU,
1646 type == WebView.HitTestResult.PHONE_TYPE);
1647 menu.setGroupVisible(R.id.EMAIL_MENU,
1648 type == WebView.HitTestResult.EMAIL_TYPE);
1649 menu.setGroupVisible(R.id.GEO_MENU,
1650 type == WebView.HitTestResult.GEO_TYPE);
1651 menu.setGroupVisible(R.id.IMAGE_MENU,
1652 type == WebView.HitTestResult.IMAGE_TYPE
1653 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1654 menu.setGroupVisible(R.id.ANCHOR_MENU,
1655 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1656 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1657
1658 // Setup custom handling depending on the type
1659 switch (type) {
1660 case WebView.HitTestResult.PHONE_TYPE:
1661 menu.setHeaderTitle(Uri.decode(extra));
1662 menu.findItem(R.id.dial_context_menu_id).setIntent(
1663 new Intent(Intent.ACTION_VIEW, Uri
1664 .parse(WebView.SCHEME_TEL + extra)));
1665 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1666 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
Cary Clark5e335a32009-09-22 14:53:11 -04001667 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001668 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1669 addIntent);
1670 menu.findItem(R.id.copy_phone_context_menu_id).setOnMenuItemClickListener(
1671 new Copy(extra));
1672 break;
1673
1674 case WebView.HitTestResult.EMAIL_TYPE:
1675 menu.setHeaderTitle(extra);
1676 menu.findItem(R.id.email_context_menu_id).setIntent(
1677 new Intent(Intent.ACTION_VIEW, Uri
1678 .parse(WebView.SCHEME_MAILTO + extra)));
1679 menu.findItem(R.id.copy_mail_context_menu_id).setOnMenuItemClickListener(
1680 new Copy(extra));
1681 break;
1682
1683 case WebView.HitTestResult.GEO_TYPE:
1684 menu.setHeaderTitle(extra);
1685 menu.findItem(R.id.map_context_menu_id).setIntent(
1686 new Intent(Intent.ACTION_VIEW, Uri
1687 .parse(WebView.SCHEME_GEO
1688 + URLEncoder.encode(extra))));
1689 menu.findItem(R.id.copy_geo_context_menu_id).setOnMenuItemClickListener(
1690 new Copy(extra));
1691 break;
1692
1693 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1694 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
1695 TextView titleView = (TextView) LayoutInflater.from(this)
1696 .inflate(android.R.layout.browser_link_context_header,
1697 null);
1698 titleView.setText(extra);
1699 menu.setHeaderView(titleView);
1700 // decide whether to show the open link in new tab option
Leon Scroggins III9e997c72010-05-26 13:25:16 -04001701 boolean showNewTab = mTabControl.canCreateNewTab();
1702 MenuItem newTabItem
1703 = menu.findItem(R.id.open_newtab_context_menu_id);
1704 newTabItem.setVisible(showNewTab);
1705 if (showNewTab) {
1706 newTabItem.setOnMenuItemClickListener(
1707 new MenuItem.OnMenuItemClickListener() {
1708 public boolean onMenuItemClick(MenuItem item) {
1709 final Tab parent = mTabControl.getCurrentTab();
1710 final Tab newTab = openTab(extra);
1711 if (newTab != parent) {
1712 parent.addChildTab(newTab);
1713 }
1714 return true;
1715 }
1716 });
1717 }
Ben Murdochde353622009-10-12 10:29:00 +01001718 menu.findItem(R.id.bookmark_context_menu_id).setVisible(
1719 Bookmarks.urlHasAcceptableScheme(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001720 PackageManager pm = getPackageManager();
1721 Intent send = new Intent(Intent.ACTION_SEND);
1722 send.setType("text/plain");
1723 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1724 menu.findItem(R.id.share_link_context_menu_id).setVisible(ri != null);
1725 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1726 break;
1727 }
1728 // otherwise fall through to handle image part
1729 case WebView.HitTestResult.IMAGE_TYPE:
1730 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1731 menu.setHeaderTitle(extra);
1732 }
1733 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1734 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1735 menu.findItem(R.id.download_context_menu_id).
1736 setOnMenuItemClickListener(new Download(extra));
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001737 menu.findItem(R.id.set_wallpaper_context_menu_id).
1738 setOnMenuItemClickListener(new SetAsWallpaper(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001739 break;
1740
1741 default:
1742 Log.w(LOGTAG, "We should not get here.");
1743 break;
1744 }
Leon Scrogginsb2b19f52009-10-09 16:10:00 -04001745 hideFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001746 }
1747
The Android Open Source Project0c908882009-03-03 19:32:16 -08001748 // Attach the given tab to the content view.
Grace Klobac928c302009-09-17 11:51:21 -07001749 // this should only be called for the current tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001750 private void attachTabToContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001751 // Attach the container that contains the main WebView and any other UI
1752 // associated with the tab.
Patrick Scottd0119532009-09-17 08:00:31 -04001753 t.attachTabToContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001754
1755 if (mShouldShowErrorConsole) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001756 ErrorConsoleView errorConsole = t.getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01001757 if (errorConsole.numberOfErrors() == 0) {
1758 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
1759 } else {
1760 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1761 }
1762
1763 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08001764 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01001765 ViewGroup.LayoutParams.WRAP_CONTENT));
1766 }
1767
Leon Scrogginsd746a942010-05-19 13:21:44 -04001768 if (!mXLargeScreenSize){
1769 WebView view = t.getWebView();
1770 view.setEmbeddedTitleBar(mTitleBar);
1771 }
Leon Scroggins58d56c62010-01-28 15:12:40 -05001772 if (t.isInVoiceSearchMode()) {
1773 showVoiceTitleBar(t.getVoiceDisplayTitle());
1774 } else {
1775 revertVoiceTitleBar();
1776 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001777 // Request focus on the top window.
1778 t.getTopWindow().requestFocus();
1779 }
1780
1781 // Attach a sub window to the main WebView of the given tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001782 void attachSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001783 t.attachSubWindow(mContentView);
1784 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001785 }
1786
1787 // Remove the given tab from the content view.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001788 private void removeTabFromContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001789 // Remove the container that contains the main WebView.
Patrick Scottd0119532009-09-17 08:00:31 -04001790 t.removeTabFromContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001791
Grace Kloba22ac16e2009-10-07 18:00:23 -07001792 ErrorConsoleView errorConsole = t.getErrorConsole(false);
1793 if (errorConsole != null) {
1794 mErrorConsoleContainer.removeView(errorConsole);
Ben Murdochbff2d602009-07-01 20:19:05 +01001795 }
1796
Leon Scrogginsd746a942010-05-19 13:21:44 -04001797 if (!mXLargeScreenSize) {
1798 WebView view = t.getWebView();
1799 if (view != null) {
1800 view.setEmbeddedTitleBar(null);
1801 }
Leon Scrogginsbb85b902009-09-14 19:27:20 -04001802 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001803 }
1804
1805 // Remove the sub window if it exists. Also called by TabControl when the
1806 // user clicks the 'X' to dismiss a sub window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001807 /* package */ void dismissSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001808 t.removeSubWindow(mContentView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001809 // dismiss the subwindow. This will destroy the WebView.
1810 t.dismissSubWindow();
Patrick Scottd0119532009-09-17 08:00:31 -04001811 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001812 }
1813
Leon Scroggins1f005d32009-08-10 17:36:42 -04001814 // A wrapper function of {@link #openTabAndShow(UrlData, boolean, String)}
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07001815 // that accepts url as string.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001816 private Tab openTabAndShow(String url, boolean closeOnExit, String appId) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001817 return openTabAndShow(new UrlData(url), closeOnExit, appId);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001818 }
1819
1820 // This method does a ton of stuff. It will attempt to create a new tab
1821 // if we haven't reached MAX_TABS. Otherwise it uses the current tab. If
Leon Scroggins1f005d32009-08-10 17:36:42 -04001822 // url isn't null, it will load the given url.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823 /* package */Tab openTabAndShow(UrlData urlData, boolean closeOnExit,
1824 String appId) {
1825 final Tab currentTab = mTabControl.getCurrentTab();
1826 if (mTabControl.canCreateNewTab()) {
1827 final Tab tab = mTabControl.createNewTab(closeOnExit, appId,
1828 urlData.mUrl);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001829 WebView webview = tab.getWebView();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001830 // If the last tab was removed from the active tabs page, currentTab
1831 // will be null.
1832 if (currentTab != null) {
1833 removeTabFromContentView(currentTab);
1834 }
Patrick Scott8bbd69f2009-08-14 13:35:53 -04001835 // We must set the new tab as the current tab to reflect the old
1836 // animation behavior.
1837 mTabControl.setCurrentTab(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001838 attachTabToContentView(tab);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001839 if (!urlData.isEmpty()) {
Patrick Scott9d53da02010-02-19 10:19:01 -05001840 loadUrlDataIn(tab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001841 }
1842 return tab;
1843 } else {
1844 // Get rid of the subwindow if it exists
1845 dismissSubWindow(currentTab);
1846 if (!urlData.isEmpty()) {
1847 // Load the given url.
Patrick Scott9d53da02010-02-19 10:19:01 -05001848 loadUrlDataIn(currentTab, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001849 }
Leon Scroggins58d56c62010-01-28 15:12:40 -05001850 return currentTab;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001851 }
1852 }
1853
Grace Kloba22ac16e2009-10-07 18:00:23 -07001854 private Tab openTab(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001855 if (mSettings.openInBackground()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001856 Tab t = mTabControl.createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001857 if (t != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001858 WebView view = t.getWebView();
Leon Scroggins92472e82010-02-17 16:32:28 -05001859 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001860 }
Grace Klobac9181842009-04-14 08:53:22 -07001861 return t;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001862 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001863 return openTabAndShow(url, false, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001864 }
1865 }
1866
1867 private class Copy implements OnMenuItemClickListener {
1868 private CharSequence mText;
1869
1870 public boolean onMenuItemClick(MenuItem item) {
1871 copy(mText);
1872 return true;
1873 }
1874
1875 public Copy(CharSequence toCopy) {
1876 mText = toCopy;
1877 }
1878 }
1879
1880 private class Download implements OnMenuItemClickListener {
1881 private String mText;
1882
1883 public boolean onMenuItemClick(MenuItem item) {
1884 onDownloadStartNoStream(mText, null, null, null, -1);
1885 return true;
1886 }
1887
1888 public Download(String toDownload) {
1889 mText = toDownload;
1890 }
1891 }
1892
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001893 private class SetAsWallpaper extends Thread implements
1894 OnMenuItemClickListener, DialogInterface.OnCancelListener {
1895 private URL mUrl;
1896 private ProgressDialog mWallpaperProgress;
1897 private boolean mCanceled = false;
1898
1899 public SetAsWallpaper(String url) {
1900 try {
1901 mUrl = new URL(url);
1902 } catch (MalformedURLException e) {
1903 mUrl = null;
1904 }
1905 }
1906
1907 public void onCancel(DialogInterface dialog) {
1908 mCanceled = true;
1909 }
1910
1911 public boolean onMenuItemClick(MenuItem item) {
1912 if (mUrl != null) {
1913 // The user may have tried to set a image with a large file size as their
1914 // background so it may take a few moments to perform the operation. Display
1915 // a progress spinner while it is working.
1916 mWallpaperProgress = new ProgressDialog(BrowserActivity.this);
1917 mWallpaperProgress.setIndeterminate(true);
1918 mWallpaperProgress.setMessage(getText(R.string.progress_dialog_setting_wallpaper));
1919 mWallpaperProgress.setCancelable(true);
1920 mWallpaperProgress.setOnCancelListener(this);
1921 mWallpaperProgress.show();
1922 start();
1923 }
1924 return true;
1925 }
1926
Michael Kolbe0a36662010-06-29 10:37:12 -07001927 @Override
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001928 public void run() {
1929 Drawable oldWallpaper = BrowserActivity.this.getWallpaper();
1930 try {
1931 // TODO: This will cause the resource to be downloaded again, when we
1932 // should in most cases be able to grab it from the cache. To fix this
1933 // we should query WebCore to see if we can access a cached version and
1934 // instead open an input stream on that. This pattern could also be used
1935 // in the download manager where the same problem exists.
1936 InputStream inputstream = mUrl.openStream();
1937 if (inputstream != null) {
1938 setWallpaper(inputstream);
1939 }
1940 } catch (IOException e) {
1941 Log.e(LOGTAG, "Unable to set new wallpaper");
1942 // Act as though the user canceled the operation so we try to
1943 // restore the old wallpaper.
1944 mCanceled = true;
1945 }
1946
1947 if (mCanceled) {
1948 // Restore the old wallpaper if the user cancelled whilst we were setting
1949 // the new wallpaper.
1950 int width = oldWallpaper.getIntrinsicWidth();
1951 int height = oldWallpaper.getIntrinsicHeight();
1952 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1953 Canvas canvas = new Canvas(bm);
1954 oldWallpaper.setBounds(0, 0, width, height);
1955 oldWallpaper.draw(canvas);
1956 try {
1957 setWallpaper(bm);
1958 } catch (IOException e) {
1959 Log.e(LOGTAG, "Unable to restore old wallpaper.");
1960 }
1961 mCanceled = false;
1962 }
1963
1964 if (mWallpaperProgress.isShowing()) {
1965 mWallpaperProgress.dismiss();
1966 }
1967 }
1968 }
1969
The Android Open Source Project0c908882009-03-03 19:32:16 -08001970 private void copy(CharSequence text) {
1971 try {
1972 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
1973 if (clip != null) {
1974 clip.setClipboardText(text);
1975 }
1976 } catch (android.os.RemoteException e) {
1977 Log.e(LOGTAG, "Copy failed", e);
1978 }
1979 }
1980
1981 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001982 * Resets the browser title-view to whatever it must be
1983 * (for example, if we had a loading error)
1984 * When we have a new page, we call resetTitle, when we
1985 * have to reset the titlebar to whatever it used to be
1986 * (for example, if the user chose to stop loading), we
1987 * call resetTitleAndRevertLockIcon.
1988 */
1989 /* package */ void resetTitleAndRevertLockIcon() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001990 mTabControl.getCurrentTab().revertLockIcon();
1991 updateLockIconToLatest();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001992 resetTitleIconAndProgress();
1993 }
1994
1995 /**
1996 * Reset the title, favicon, and progress.
1997 */
1998 private void resetTitleIconAndProgress() {
1999 WebView current = mTabControl.getCurrentWebView();
2000 if (current == null) {
2001 return;
2002 }
2003 resetTitleAndIcon(current);
2004 int progress = current.getProgress();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002005 current.getWebChromeClient().onProgressChanged(current, progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002006 }
2007
2008 // Reset the title and the icon based on the given item.
2009 private void resetTitleAndIcon(WebView view) {
2010 WebHistoryItem item = view.copyBackForwardList().getCurrentItem();
2011 if (item != null) {
Leon Scroggins68579392009-09-15 15:31:54 -04002012 setUrlTitle(item.getUrl(), item.getTitle());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002013 setFavicon(item.getFavicon());
2014 } else {
Leon Scroggins68579392009-09-15 15:31:54 -04002015 setUrlTitle(null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002016 setFavicon(null);
2017 }
2018 }
2019
2020 /**
2021 * Sets a title composed of the URL and the title string.
2022 * @param url The URL of the site being loaded.
2023 * @param title The title of the site being loaded.
2024 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002025 void setUrlTitle(String url, String title) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002026 mUrl = url;
2027 mTitle = title;
2028
Leon Scroggins58d56c62010-01-28 15:12:40 -05002029 // If we are in voice search mode, the title has already been set.
2030 if (mTabControl.getCurrentTab().isInVoiceSearchMode()) return;
2031 mTitleBar.setDisplayTitle(url);
Leon Scrogginsd746a942010-05-19 13:21:44 -04002032 if (!mXLargeScreenSize) {
2033 mFakeTitleBar.setDisplayTitle(url);
2034 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002035 }
2036
2037 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08002038 * @param url The URL to build a title version of the URL from.
2039 * @return The title version of the URL or null if fails.
2040 * The title version of the URL can be either the URL hostname,
2041 * or the hostname with an "https://" prefix (for secure URLs),
2042 * or an empty string if, for example, the URL in question is a
2043 * file:// URL with no hostname.
2044 */
Leon Scroggins32e14a62009-06-11 10:26:34 -04002045 /* package */ static String buildTitleUrl(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002046 String titleUrl = null;
2047
2048 if (url != null) {
2049 try {
2050 // parse the url string
2051 URL urlObj = new URL(url);
2052 if (urlObj != null) {
2053 titleUrl = "";
2054
2055 String protocol = urlObj.getProtocol();
2056 String host = urlObj.getHost();
2057
2058 if (host != null && 0 < host.length()) {
2059 titleUrl = host;
2060 if (protocol != null) {
2061 // if a secure site, add an "https://" prefix!
2062 if (protocol.equalsIgnoreCase("https")) {
2063 titleUrl = protocol + "://" + host;
2064 }
2065 }
2066 }
2067 }
2068 } catch (MalformedURLException e) {}
2069 }
2070
2071 return titleUrl;
2072 }
2073
2074 // Set the favicon in the title bar.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002075 void setFavicon(Bitmap icon) {
Leon Scroggins68579392009-09-15 15:31:54 -04002076 mTitleBar.setFavicon(icon);
Leon Scrogginsd746a942010-05-19 13:21:44 -04002077 if (!mXLargeScreenSize) {
2078 mFakeTitleBar.setFavicon(icon);
2079 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002080 }
2081
2082 /**
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002083 * Close the tab, remove its associated title bar, and adjust mTabControl's
2084 * current tab to a valid value.
Leon Scroggins1f005d32009-08-10 17:36:42 -04002085 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002086 /* package */ void closeTab(Tab t) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002087 int currentIndex = mTabControl.getCurrentIndex();
2088 int removeIndex = mTabControl.getTabIndex(t);
Leon Scroggins1f005d32009-08-10 17:36:42 -04002089 mTabControl.removeTab(t);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002090 if (currentIndex >= removeIndex && currentIndex != 0) {
2091 currentIndex--;
2092 }
2093 mTabControl.setCurrentTab(mTabControl.getTab(currentIndex));
Andrei Popescua5bf1de2009-09-23 16:39:23 +01002094 resetTitleIconAndProgress();
Leon Scroggins654899b2010-06-25 16:25:23 -04002095 updateLockIconToLatest();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002096 }
2097
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05002098 /* package */ void goBackOnePageOrQuit() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002099 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002100 if (current == null) {
2101 /*
2102 * Instead of finishing the activity, simply push this to the back
2103 * of the stack and let ActivityManager to choose the foreground
2104 * activity. As BrowserActivity is singleTask, it will be always the
2105 * root of the task. So we can use either true or false for
2106 * moveTaskToBack().
2107 */
2108 moveTaskToBack(true);
Grace Kloba00d85e72009-09-23 18:50:05 -07002109 return;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002110 }
2111 WebView w = current.getWebView();
2112 if (w.canGoBack()) {
2113 w.goBack();
2114 } else {
2115 // Check to see if we are closing a window that was created by
2116 // another window. If so, we switch back to that window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002117 Tab parent = current.getParentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002118 if (parent != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04002119 switchToTab(mTabControl.getTabIndex(parent));
2120 // Now we close the other tab
2121 closeTab(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002122 } else {
2123 if (current.closeOnExit()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002124 // force the tab's inLoad() to be false as we are going to
2125 // either finish the activity or remove the tab. This will
2126 // ensure pauseWebViewTimers() taking action.
2127 mTabControl.getCurrentTab().clearInLoad();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002128 if (mTabControl.getTabCount() == 1) {
2129 finish();
2130 return;
2131 }
Mike Reed7bfa63b2009-05-28 11:08:32 -04002132 // call pauseWebViewTimers() now, we won't be able to call
2133 // it in onPause() as the WebView won't be valid.
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002134 // Temporarily change mActivityInPause to be true as
2135 // pauseWebViewTimers() will do nothing if mActivityInPause
2136 // is false.
Grace Kloba918e1d72009-08-13 14:55:06 -07002137 boolean savedState = mActivityInPause;
2138 if (savedState) {
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002139 Log.e(LOGTAG, "BrowserActivity is already paused "
2140 + "while handing goBackOnePageOrQuit.");
Grace Kloba918e1d72009-08-13 14:55:06 -07002141 }
2142 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -04002143 pauseWebViewTimers();
Grace Kloba918e1d72009-08-13 14:55:06 -07002144 mActivityInPause = savedState;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002145 removeTabFromContentView(current);
2146 mTabControl.removeTab(current);
2147 }
2148 /*
2149 * Instead of finishing the activity, simply push this to the back
2150 * of the stack and let ActivityManager to choose the foreground
2151 * activity. As BrowserActivity is singleTask, it will be always the
2152 * root of the task. So we can use either true or false for
2153 * moveTaskToBack().
2154 */
2155 moveTaskToBack(true);
2156 }
2157 }
2158 }
2159
Grace Kloba22ac16e2009-10-07 18:00:23 -07002160 boolean isMenuDown() {
2161 return mMenuIsDown;
2162 }
2163
Grace Kloba5942df02009-09-18 11:48:29 -07002164 @Override
2165 public boolean onKeyDown(int keyCode, KeyEvent event) {
Leon Scrogginsf65b50d2009-12-08 10:44:28 -05002166 // Even if MENU is already held down, we need to call to super to open
2167 // the IME on long press.
2168 if (KeyEvent.KEYCODE_MENU == keyCode) {
2169 mMenuIsDown = true;
2170 return super.onKeyDown(keyCode, event);
2171 }
Grace Kloba5942df02009-09-18 11:48:29 -07002172 // The default key mode is DEFAULT_KEYS_SEARCH_LOCAL. As the MENU is
2173 // still down, we don't want to trigger the search. Pretend to consume
2174 // the key and do nothing.
2175 if (mMenuIsDown) return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002176
Grace Kloba5942df02009-09-18 11:48:29 -07002177 switch(keyCode) {
Grace Kloba5942df02009-09-18 11:48:29 -07002178 case KeyEvent.KEYCODE_SPACE:
Grace Klobada0fe552009-09-22 18:17:24 -07002179 // WebView/WebTextView handle the keys in the KeyDown. As
2180 // the Activity's shortcut keys are only handled when WebView
2181 // doesn't, have to do it in onKeyDown instead of onKeyUp.
2182 if (event.isShiftPressed()) {
2183 getTopWindow().pageUp(false);
2184 } else {
2185 getTopWindow().pageDown(false);
2186 }
Grace Kloba5942df02009-09-18 11:48:29 -07002187 return true;
2188 case KeyEvent.KEYCODE_BACK:
2189 if (event.getRepeatCount() == 0) {
2190 event.startTracking();
2191 return true;
2192 } else if (mCustomView == null && mActiveTabsPage == null
2193 && event.isLongPress()) {
2194 bookmarksOrHistoryPicker(true);
2195 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002196 }
Grace Kloba5942df02009-09-18 11:48:29 -07002197 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002198 }
Grace Kloba5942df02009-09-18 11:48:29 -07002199 return super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002200 }
2201
Grace Kloba5942df02009-09-18 11:48:29 -07002202 @Override
2203 public boolean onKeyUp(int keyCode, KeyEvent event) {
2204 switch(keyCode) {
2205 case KeyEvent.KEYCODE_MENU:
2206 mMenuIsDown = false;
2207 break;
Grace Kloba5942df02009-09-18 11:48:29 -07002208 case KeyEvent.KEYCODE_BACK:
2209 if (event.isTracking() && !event.isCanceled()) {
2210 if (mCustomView != null) {
2211 // if a custom view is showing, hide it
Grace Kloba22ac16e2009-10-07 18:00:23 -07002212 mTabControl.getCurrentWebView().getWebChromeClient()
2213 .onHideCustomView();
Grace Kloba5942df02009-09-18 11:48:29 -07002214 } else if (mActiveTabsPage != null) {
2215 // if tab page is showing, hide it
2216 removeActiveTabPage(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002217 } else {
Grace Kloba5942df02009-09-18 11:48:29 -07002218 WebView subwindow = mTabControl.getCurrentSubWindow();
2219 if (subwindow != null) {
2220 if (subwindow.canGoBack()) {
2221 subwindow.goBack();
2222 } else {
2223 dismissSubWindow(mTabControl.getCurrentTab());
2224 }
2225 } else {
2226 goBackOnePageOrQuit();
2227 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002228 }
Grace Kloba5942df02009-09-18 11:48:29 -07002229 return true;
2230 }
2231 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002232 }
Grace Kloba5942df02009-09-18 11:48:29 -07002233 return super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002234 }
2235
Leon Scroggins68579392009-09-15 15:31:54 -04002236 /* package */ void stopLoading() {
Ben Murdochb7cc8b42009-09-28 10:59:09 +01002237 mDidStopLoad = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002238 resetTitleAndRevertLockIcon();
2239 WebView w = getTopWindow();
2240 w.stopLoading();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002241 // FIXME: before refactor, it is using mWebViewClient. So I keep the
2242 // same logic here. But for subwindow case, should we call into the main
2243 // WebView's onPageFinished as we never call its onPageStarted and if
2244 // the page finishes itself, we don't call onPageFinished.
2245 mTabControl.getCurrentWebView().getWebViewClient().onPageFinished(w,
2246 w.getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002247
2248 cancelStopToast();
2249 mStopToast = Toast
2250 .makeText(this, R.string.stopping, Toast.LENGTH_SHORT);
2251 mStopToast.show();
2252 }
2253
Grace Kloba22ac16e2009-10-07 18:00:23 -07002254 boolean didUserStopLoading() {
2255 return mDidStopLoad;
2256 }
2257
The Android Open Source Project0c908882009-03-03 19:32:16 -08002258 private void cancelStopToast() {
2259 if (mStopToast != null) {
2260 mStopToast.cancel();
2261 mStopToast = null;
2262 }
2263 }
2264
Grace Kloba22ac16e2009-10-07 18:00:23 -07002265 // called by a UI or non-UI thread to post the message
2266 public void postMessage(int what, int arg1, int arg2, Object obj,
2267 long delayMillis) {
2268 mHandler.sendMessageDelayed(mHandler.obtainMessage(what, arg1, arg2,
2269 obj), delayMillis);
2270 }
2271
2272 // called by a UI or non-UI thread to remove the message
2273 void removeMessages(int what, Object object) {
2274 mHandler.removeMessages(what, object);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002275 }
2276
2277 // public message ids
2278 public final static int LOAD_URL = 1001;
2279 public final static int STOP_LOAD = 1002;
2280
2281 // Message Ids
2282 private static final int FOCUS_NODE_HREF = 102;
Grace Kloba92c18a52009-07-31 23:48:32 -07002283 private static final int RELEASE_WAKELOCK = 107;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002284
Grace Kloba22ac16e2009-10-07 18:00:23 -07002285 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
Ben Murdoch2694e232009-09-29 09:41:11 +01002286
The Android Open Source Project0c908882009-03-03 19:32:16 -08002287 // Private handler for handling javascript and saving passwords
2288 private Handler mHandler = new Handler() {
2289
Michael Kolbe0a36662010-06-29 10:37:12 -07002290 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -08002291 public void handleMessage(Message msg) {
2292 switch (msg.what) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002293 case FOCUS_NODE_HREF:
Ben Murdoch2694e232009-09-29 09:41:11 +01002294 {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002295 String url = (String) msg.getData().get("url");
Ben Murdoch90d088c2009-11-17 18:14:04 +00002296 String title = (String) msg.getData().get("title");
The Android Open Source Project0c908882009-03-03 19:32:16 -08002297 if (url == null || url.length() == 0) {
2298 break;
2299 }
2300 HashMap focusNodeMap = (HashMap) msg.obj;
2301 WebView view = (WebView) focusNodeMap.get("webview");
2302 // Only apply the action if the top window did not change.
2303 if (getTopWindow() != view) {
2304 break;
2305 }
2306 switch (msg.arg1) {
2307 case R.id.open_context_menu_id:
2308 case R.id.view_image_context_menu_id:
Leon Scroggins92472e82010-02-17 16:32:28 -05002309 loadUrlFromContext(getTopWindow(), url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002310 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002311 case R.id.bookmark_context_menu_id:
2312 Intent intent = new Intent(BrowserActivity.this,
2313 AddBookmarkPage.class);
2314 intent.putExtra("url", url);
Ben Murdoch90d088c2009-11-17 18:14:04 +00002315 intent.putExtra("title", title);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002316 startActivity(intent);
2317 break;
2318 case R.id.share_link_context_menu_id:
Leon Scroggins3e204452010-05-10 11:06:03 -04002319 sharePage(BrowserActivity.this, title, url, null,
Leon Scroggins96afcb12009-12-10 12:35:56 -05002320 null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002321 break;
2322 case R.id.copy_link_context_menu_id:
2323 copy(url);
2324 break;
2325 case R.id.save_link_context_menu_id:
2326 case R.id.download_context_menu_id:
2327 onDownloadStartNoStream(url, null, null, null, -1);
2328 break;
2329 }
2330 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002331 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002332
2333 case LOAD_URL:
Leon Scroggins92472e82010-02-17 16:32:28 -05002334 loadUrlFromContext(getTopWindow(), (String) msg.obj);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002335 break;
2336
2337 case STOP_LOAD:
2338 stopLoading();
2339 break;
2340
The Android Open Source Project0c908882009-03-03 19:32:16 -08002341 case RELEASE_WAKELOCK:
2342 if (mWakeLock.isHeld()) {
2343 mWakeLock.release();
Grace Kloba5d0e02e2009-10-05 15:15:36 -07002344 // if we reach here, Browser should be still in the
2345 // background loading after WAKELOCK_TIMEOUT (5-min).
2346 // To avoid burning the battery, stop loading.
2347 mTabControl.stopAllLoading();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002348 }
2349 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002350
2351 case UPDATE_BOOKMARK_THUMBNAIL:
2352 WebView view = (WebView) msg.obj;
2353 if (view != null) {
2354 updateScreenshot(view);
2355 }
2356 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002357 }
2358 }
2359 };
2360
Leon Scroggins96afcb12009-12-10 12:35:56 -05002361 /**
2362 * Share a page, providing the title, url, favicon, and a screenshot. Uses
2363 * an {@link Intent} to launch the Activity chooser.
2364 * @param c Context used to launch a new Activity.
2365 * @param title Title of the page. Stored in the Intent with
Paul Westbrook03e6d392010-02-12 10:33:29 -08002366 * {@link Intent#EXTRA_SUBJECT}
Leon Scroggins96afcb12009-12-10 12:35:56 -05002367 * @param url URL of the page. Stored in the Intent with
2368 * {@link Intent#EXTRA_TEXT}
2369 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
2370 * with {@link Browser#EXTRA_SHARE_FAVICON}
2371 * @param screenshot Bitmap of a screenshot of the page. Stored in the
2372 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
2373 */
2374 public static final void sharePage(Context c, String title, String url,
2375 Bitmap favicon, Bitmap screenshot) {
2376 Intent send = new Intent(Intent.ACTION_SEND);
2377 send.setType("text/plain");
2378 send.putExtra(Intent.EXTRA_TEXT, url);
Paul Westbrook03e6d392010-02-12 10:33:29 -08002379 send.putExtra(Intent.EXTRA_SUBJECT, title);
Leon Scroggins96afcb12009-12-10 12:35:56 -05002380 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
2381 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
2382 try {
2383 c.startActivity(Intent.createChooser(send, c.getString(
2384 R.string.choosertitle_sharevia)));
2385 } catch(android.content.ActivityNotFoundException ex) {
2386 // if no app handles it, do nothing
2387 }
2388 }
2389
Leon Scroggins89c6d362009-07-15 16:54:37 -04002390 private void updateScreenshot(WebView view) {
2391 // If this is a bookmarked site, add a screenshot to the database.
2392 // FIXME: When should we update? Every time?
2393 // FIXME: Would like to make sure there is actually something to
2394 // draw, but the API for that (WebViewCore.pictureReady()) is not
2395 // currently accessible here.
Ben Murdochaac7aa62009-09-17 16:57:40 +01002396
Patrick Scottcb192b52010-04-14 14:38:55 -04002397 final Bitmap bm = createScreenshot(view);
2398 if (bm == null) {
2399 return;
Leon Scroggins89c6d362009-07-15 16:54:37 -04002400 }
Patrick Scottcb192b52010-04-14 14:38:55 -04002401
2402 final ContentResolver cr = getContentResolver();
2403 final String url = view.getUrl();
2404 final String originalUrl = view.getOriginalUrl();
2405
2406 new AsyncTask<Void, Void, Void>() {
2407 @Override
2408 protected Void doInBackground(Void... unused) {
2409 Cursor c = null;
2410 try {
2411 c = BrowserBookmarksAdapter.queryBookmarksForUrl(
2412 cr, originalUrl, url, true);
2413 if (c != null) {
2414 if (c.moveToFirst()) {
2415 ContentValues values = new ContentValues();
2416 final ByteArrayOutputStream os
2417 = new ByteArrayOutputStream();
2418 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
2419 values.put(Browser.BookmarkColumns.THUMBNAIL,
2420 os.toByteArray());
2421 do {
2422 cr.update(ContentUris.withAppendedId(
2423 Browser.BOOKMARKS_URI, c.getInt(0)),
2424 values, null, null);
2425 } while (c.moveToNext());
2426 }
2427 }
2428 } catch (IllegalStateException e) {
2429 // Ignore
2430 } finally {
2431 if (c != null) c.close();
2432 }
2433 return null;
2434 }
2435 }.execute();
Leon Scroggins89c6d362009-07-15 16:54:37 -04002436 }
2437
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002438 /**
Leon Scrogginsf8551612009-09-24 16:06:02 -04002439 * Values for the size of the thumbnail created when taking a screenshot.
2440 * Lazily initialized. Instead of using these directly, use
2441 * getDesiredThumbnailWidth() or getDesiredThumbnailHeight().
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002442 */
Leon Scrogginsf8551612009-09-24 16:06:02 -04002443 private static int THUMBNAIL_WIDTH = 0;
2444 private static int THUMBNAIL_HEIGHT = 0;
2445
2446 /**
2447 * Return the desired width for thumbnail screenshots, which are stored in
2448 * the database, and used on the bookmarks screen.
2449 * @param context Context for finding out the density of the screen.
2450 * @return int desired width for thumbnail screenshot.
2451 */
2452 /* package */ static int getDesiredThumbnailWidth(Context context) {
2453 if (THUMBNAIL_WIDTH == 0) {
2454 float density = context.getResources().getDisplayMetrics().density;
2455 THUMBNAIL_WIDTH = (int) (90 * density);
2456 THUMBNAIL_HEIGHT = (int) (80 * density);
2457 }
2458 return THUMBNAIL_WIDTH;
2459 }
2460
2461 /**
2462 * Return the desired height for thumbnail screenshots, which are stored in
2463 * the database, and used on the bookmarks screen.
2464 * @param context Context for finding out the density of the screen.
2465 * @return int desired height for thumbnail screenshot.
2466 */
2467 /* package */ static int getDesiredThumbnailHeight(Context context) {
2468 // To ensure that they are both initialized.
2469 getDesiredThumbnailWidth(context);
2470 return THUMBNAIL_HEIGHT;
2471 }
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002472
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002473 private Bitmap createScreenshot(WebView view) {
2474 Picture thumbnail = view.capturePicture();
Leon Scroggins45800572009-09-29 16:38:47 -04002475 if (thumbnail == null) {
2476 return null;
2477 }
Leon Scrogginsf8551612009-09-24 16:06:02 -04002478 Bitmap bm = Bitmap.createBitmap(getDesiredThumbnailWidth(this),
Cary Clarkab168ba2010-04-08 09:11:28 -04002479 getDesiredThumbnailHeight(this), Bitmap.Config.RGB_565);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002480 Canvas canvas = new Canvas(bm);
2481 // May need to tweak these values to determine what is the
2482 // best scale factor
Ben Murdoch2694e232009-09-29 09:41:11 +01002483 int thumbnailWidth = thumbnail.getWidth();
Ben Murdochae59c3f2009-10-20 18:30:28 +01002484 int thumbnailHeight = thumbnail.getHeight();
2485 float scaleFactorX = 1.0f;
2486 float scaleFactorY = 1.0f;
Ben Murdoch2694e232009-09-29 09:41:11 +01002487 if (thumbnailWidth > 0) {
Ben Murdochae59c3f2009-10-20 18:30:28 +01002488 scaleFactorX = (float) getDesiredThumbnailWidth(this) /
Ben Murdoch2694e232009-09-29 09:41:11 +01002489 (float)thumbnailWidth;
Ben Murdochae59c3f2009-10-20 18:30:28 +01002490 } else {
2491 return null;
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002492 }
Ben Murdochae59c3f2009-10-20 18:30:28 +01002493
2494 if (view.getWidth() > view.getHeight() &&
2495 thumbnailHeight < view.getHeight() && thumbnailHeight > 0) {
2496 // If the device is in landscape and the page is shorter
2497 // than the height of the view, stretch the thumbnail to fill the
2498 // space.
2499 scaleFactorY = (float) getDesiredThumbnailHeight(this) /
2500 (float)thumbnailHeight;
2501 } else {
2502 // In the portrait case, this looks nice.
2503 scaleFactorY = scaleFactorX;
2504 }
2505
2506 canvas.scale(scaleFactorX, scaleFactorY);
2507
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002508 thumbnail.draw(canvas);
2509 return bm;
2510 }
2511
The Android Open Source Project0c908882009-03-03 19:32:16 -08002512 // -------------------------------------------------------------------------
Grace Kloba22ac16e2009-10-07 18:00:23 -07002513 // Helper function for WebViewClient.
The Android Open Source Project0c908882009-03-03 19:32:16 -08002514 //-------------------------------------------------------------------------
2515
2516 // Use in overrideUrlLoading
2517 /* package */ final static String SCHEME_WTAI = "wtai://wp/";
2518 /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
2519 /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;";
2520 /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;";
2521
Leon Scroggins92472e82010-02-17 16:32:28 -05002522 // Keep this initial progress in sync with initialProgressValue (* 100)
2523 // in ProgressTracker.cpp
2524 private final static int INITIAL_PROGRESS = 10;
2525
Grace Kloba22ac16e2009-10-07 18:00:23 -07002526 void onPageStarted(WebView view, String url, Bitmap favicon) {
2527 // when BrowserActivity just starts, onPageStarted may be called before
2528 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
2529 // to start the timer. As we won't switch tabs while an activity is in
2530 // pause state, we can ensure calling resume and pause in pair.
2531 if (mActivityInPause) resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002532
Grace Kloba22ac16e2009-10-07 18:00:23 -07002533 resetLockIcon(url);
2534 setUrlTitle(url, null);
2535 setFavicon(favicon);
Leon Scroggins8cf8f682009-11-04 11:13:50 -08002536 // Show some progress so that the user knows the page is beginning to
2537 // load
Leon Scroggins92472e82010-02-17 16:32:28 -05002538 onProgressChanged(view, INITIAL_PROGRESS);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002539 mDidStopLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07002540 if (!mIsNetworkUp) createAndShowNetworkDialog();
Cary Clark01cfcdd2010-06-04 16:36:45 -04002541 closeDialogs();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002542 if (mSettings.isTracing()) {
2543 String host;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002544 try {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002545 WebAddress uri = new WebAddress(url);
2546 host = uri.mHost;
2547 } catch (android.net.ParseException ex) {
2548 host = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -08002549 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002550 host = host.replace('.', '_');
2551 host += ".trace";
2552 mInTrace = true;
2553 Debug.startMethodTracing(host, 20 * 1024 * 1024);
2554 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002555
Grace Kloba22ac16e2009-10-07 18:00:23 -07002556 // Performance probe
2557 if (false) {
2558 mStart = SystemClock.uptimeMillis();
2559 mProcessStart = Process.getElapsedCpuTime();
2560 long[] sysCpu = new long[7];
2561 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2562 sysCpu, null)) {
2563 mUserStart = sysCpu[0] + sysCpu[1];
2564 mSystemStart = sysCpu[2];
2565 mIdleStart = sysCpu[3];
2566 mIrqStart = sysCpu[4] + sysCpu[5] + sysCpu[6];
2567 }
2568 mUiStart = SystemClock.currentThreadTimeMillis();
2569 }
2570 }
2571
2572 void onPageFinished(WebView view, String url) {
2573 // Reset the title and icon in case we stopped a provisional load.
2574 resetTitleAndIcon(view);
2575 // Update the lock icon image only once we are done loading
2576 updateLockIconToLatest();
2577 // pause the WebView timer and release the wake lock if it is finished
2578 // while BrowserActivity is in pause state.
2579 if (mActivityInPause && pauseWebViewTimers()) {
2580 if (mWakeLock.isHeld()) {
2581 mHandler.removeMessages(RELEASE_WAKELOCK);
2582 mWakeLock.release();
2583 }
2584 }
2585
2586 // Performance probe
2587 if (false) {
2588 long[] sysCpu = new long[7];
2589 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2590 sysCpu, null)) {
2591 String uiInfo = "UI thread used "
2592 + (SystemClock.currentThreadTimeMillis() - mUiStart)
2593 + " ms";
2594 if (LOGD_ENABLED) {
2595 Log.d(LOGTAG, uiInfo);
2596 }
2597 //The string that gets written to the log
2598 String performanceString = "It took total "
2599 + (SystemClock.uptimeMillis() - mStart)
2600 + " ms clock time to load the page."
2601 + "\nbrowser process used "
2602 + (Process.getElapsedCpuTime() - mProcessStart)
2603 + " ms, user processes used "
2604 + (sysCpu[0] + sysCpu[1] - mUserStart) * 10
2605 + " ms, kernel used "
2606 + (sysCpu[2] - mSystemStart) * 10
2607 + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10
2608 + " ms and irq took "
2609 + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart)
2610 * 10 + " ms, " + uiInfo;
2611 if (LOGD_ENABLED) {
2612 Log.d(LOGTAG, performanceString + "\nWebpage: " + url);
2613 }
2614 if (url != null) {
2615 // strip the url to maintain consistency
2616 String newUrl = new String(url);
2617 if (newUrl.startsWith("http://www.")) {
2618 newUrl = newUrl.substring(11);
2619 } else if (newUrl.startsWith("http://")) {
2620 newUrl = newUrl.substring(7);
2621 } else if (newUrl.startsWith("https://www.")) {
2622 newUrl = newUrl.substring(12);
2623 } else if (newUrl.startsWith("https://")) {
2624 newUrl = newUrl.substring(8);
2625 }
2626 if (LOGD_ENABLED) {
2627 Log.d(LOGTAG, newUrl + " loaded");
2628 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002629 }
2630 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002631 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002632
Grace Kloba22ac16e2009-10-07 18:00:23 -07002633 if (mInTrace) {
2634 mInTrace = false;
2635 Debug.stopMethodTracing();
2636 }
2637 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002638
Grace Klobae7fe26b2010-06-28 16:23:33 -07002639 private void closeEmptyChildTab() {
2640 Tab current = mTabControl.getCurrentTab();
2641 if (current != null
2642 && current.getWebView().copyBackForwardList().getSize() == 0) {
2643 Tab parent = current.getParentTab();
2644 if (parent != null) {
2645 switchToTab(mTabControl.getTabIndex(parent));
2646 closeTab(current);
2647 }
2648 }
2649 }
2650
Grace Kloba22ac16e2009-10-07 18:00:23 -07002651 boolean shouldOverrideUrlLoading(WebView view, String url) {
2652 if (url.startsWith(SCHEME_WTAI)) {
2653 // wtai://wp/mc;number
2654 // number=string(phone-number)
2655 if (url.startsWith(SCHEME_WTAI_MC)) {
2656 Intent intent = new Intent(Intent.ACTION_VIEW,
2657 Uri.parse(WebView.SCHEME_TEL +
2658 url.substring(SCHEME_WTAI_MC.length())));
2659 startActivity(intent);
Grace Klobae7fe26b2010-06-28 16:23:33 -07002660 // before leaving BrowserActivity, close the empty child tab.
2661 // If a new tab is created through JavaScript open to load this
2662 // url, we would like to close it as we will load this url in a
2663 // different Activity.
2664 closeEmptyChildTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002665 return true;
2666 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002667 // wtai://wp/sd;dtmf
2668 // dtmf=string(dialstring)
2669 if (url.startsWith(SCHEME_WTAI_SD)) {
2670 // TODO: only send when there is active voice connection
2671 return false;
2672 }
2673 // wtai://wp/ap;number;name
2674 // number=string(phone-number)
2675 // name=string
2676 if (url.startsWith(SCHEME_WTAI_AP)) {
2677 // TODO
2678 return false;
2679 }
2680 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002681
Grace Kloba22ac16e2009-10-07 18:00:23 -07002682 // The "about:" schemes are internal to the browser; don't want these to
2683 // be dispatched to other apps.
2684 if (url.startsWith("about:")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002685 return false;
2686 }
2687
Grace Kloba22ac16e2009-10-07 18:00:23 -07002688 Intent intent;
2689 // perform generic parsing of the URI to turn it into an Intent.
2690 try {
2691 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
2692 } catch (URISyntaxException ex) {
2693 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
2694 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002695 }
2696
Grace Kloba22ac16e2009-10-07 18:00:23 -07002697 // check whether the intent can be resolved. If not, we will see
2698 // whether we can download it from the Market.
2699 if (getPackageManager().resolveActivity(intent, 0) == null) {
2700 String packagename = intent.getPackage();
2701 if (packagename != null) {
2702 intent = new Intent(Intent.ACTION_VIEW, Uri
2703 .parse("market://search?q=pname:" + packagename));
2704 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2705 startActivity(intent);
Grace Klobae7fe26b2010-06-28 16:23:33 -07002706 // before leaving BrowserActivity, close the empty child tab.
2707 // If a new tab is created through JavaScript open to load this
2708 // url, we would like to close it as we will load this url in a
2709 // different Activity.
2710 closeEmptyChildTab();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002711 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002712 } else {
2713 return false;
2714 }
2715 }
2716
Grace Kloba22ac16e2009-10-07 18:00:23 -07002717 // sanitize the Intent, ensuring web pages can not bypass browser
2718 // security (only access to BROWSABLE activities).
2719 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2720 intent.setComponent(null);
2721 try {
2722 if (startActivityIfNeeded(intent, -1)) {
Grace Klobae7fe26b2010-06-28 16:23:33 -07002723 // before leaving BrowserActivity, close the empty child tab.
2724 // If a new tab is created through JavaScript open to load this
2725 // url, we would like to close it as we will load this url in a
2726 // different Activity.
2727 closeEmptyChildTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002728 return true;
2729 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002730 } catch (ActivityNotFoundException ex) {
2731 // ignore the error. If no application can handle the URL,
2732 // eg about:blank, assume the browser can handle it.
2733 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002734
Grace Kloba22ac16e2009-10-07 18:00:23 -07002735 if (mMenuIsDown) {
2736 openTab(url);
2737 closeOptionsMenu();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002738 return true;
2739 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002740 return false;
2741 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002742
Grace Kloba22ac16e2009-10-07 18:00:23 -07002743 // -------------------------------------------------------------------------
2744 // Helper function for WebChromeClient
2745 // -------------------------------------------------------------------------
The Android Open Source Project0c908882009-03-03 19:32:16 -08002746
Grace Kloba22ac16e2009-10-07 18:00:23 -07002747 void onProgressChanged(WebView view, int newProgress) {
Leon Scrogginsd746a942010-05-19 13:21:44 -04002748 if (mXLargeScreenSize) {
2749 mTitleBar.setProgress(newProgress);
2750 } else {
2751 // On the phone, the fake title bar will always cover up the
2752 // regular title bar (or the regular one is offscreen), so only the
2753 // fake title bar needs to change its progress
2754 mFakeTitleBar.setProgress(newProgress);
2755 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002756
Grace Kloba22ac16e2009-10-07 18:00:23 -07002757 if (newProgress == 100) {
2758 // onProgressChanged() may continue to be called after the main
2759 // frame has finished loading, as any remaining sub frames continue
2760 // to load. We'll only get called once though with newProgress as
2761 // 100 when everything is loaded. (onPageFinished is called once
2762 // when the main frame completes loading regardless of the state of
2763 // any sub frames so calls to onProgressChanges may continue after
2764 // onPageFinished has executed)
2765 if (mInLoad) {
2766 mInLoad = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -04002767 updateInLoadMenuItems();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002768 // If the options menu is open, leave the title bar
2769 if (!mOptionsMenuOpen || !mIconView) {
2770 hideFakeTitleBar();
The Android Open Source Projectcb9a0bb2009-03-11 12:11:58 -07002771 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002772 }
Leon Scrogginsaa37e7f2010-03-05 11:51:20 -05002773 } else {
2774 if (!mInLoad) {
2775 // onPageFinished may have already been called but a subframe is
2776 // still loading and updating the progress. Reset mInLoad and
2777 // update the menu items.
2778 mInLoad = true;
2779 updateInLoadMenuItems();
2780 }
2781 // When the page first begins to load, the Activity may still be
2782 // paused, in which case showFakeTitleBar will do nothing. Call
2783 // again as the page continues to load so that it will be shown.
2784 // (Calling it will the fake title bar is already showing will also
2785 // do nothing.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002786 if (!mOptionsMenuOpen || mIconView) {
2787 // This page has begun to load, so show the title bar
2788 showFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002789 }
2790 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002791 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002792
Grace Kloba22ac16e2009-10-07 18:00:23 -07002793 void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002794 // if a view already exists then immediately terminate the new one
2795 if (mCustomView != null) {
2796 callback.onCustomViewHidden();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002797 return;
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002798 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002799
2800 // Add the custom view to its container.
2801 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
2802 mCustomView = view;
2803 mCustomViewCallback = callback;
2804 // Save the menu state and set it to empty while the custom
2805 // view is showing.
2806 mOldMenuState = mMenuState;
2807 mMenuState = EMPTY_MENU;
2808 // Hide the content view.
2809 mContentView.setVisibility(View.GONE);
2810 // Finally show the custom view container.
Andrei Popescu163ab742009-10-20 17:58:23 +01002811 setStatusBarVisibility(false);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002812 mCustomViewContainer.setVisibility(View.VISIBLE);
2813 mCustomViewContainer.bringToFront();
2814 }
2815
2816 void onHideCustomView() {
2817 if (mCustomView == null)
2818 return;
2819
2820 // Hide the custom view.
2821 mCustomView.setVisibility(View.GONE);
2822 // Remove the custom view from its container.
2823 mCustomViewContainer.removeView(mCustomView);
2824 mCustomView = null;
2825 // Reset the old menu state.
2826 mMenuState = mOldMenuState;
2827 mOldMenuState = EMPTY_MENU;
2828 mCustomViewContainer.setVisibility(View.GONE);
2829 mCustomViewCallback.onCustomViewHidden();
2830 // Show the content view.
Andrei Popescu163ab742009-10-20 17:58:23 +01002831 setStatusBarVisibility(true);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002832 mContentView.setVisibility(View.VISIBLE);
2833 }
2834
2835 Bitmap getDefaultVideoPoster() {
2836 if (mDefaultVideoPoster == null) {
2837 mDefaultVideoPoster = BitmapFactory.decodeResource(
2838 getResources(), R.drawable.default_video_poster);
Patrick Scott3918d442009-08-04 13:22:29 -04002839 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002840 return mDefaultVideoPoster;
2841 }
Patrick Scott3918d442009-08-04 13:22:29 -04002842
Grace Kloba22ac16e2009-10-07 18:00:23 -07002843 View getVideoLoadingProgressView() {
2844 if (mVideoProgressView == null) {
2845 LayoutInflater inflater = LayoutInflater.from(BrowserActivity.this);
2846 mVideoProgressView = inflater.inflate(
2847 R.layout.video_loading_progress, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002848 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002849 return mVideoProgressView;
2850 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002851
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002852 /*
2853 * The Object used to inform the WebView of the file to upload.
2854 */
2855 private ValueCallback<Uri> mUploadMessage;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002856 private String mCameraFilePath;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002857
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002858 void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
2859
2860 final String imageMimeType = "image/*";
2861 final String imageSourceKey = "source";
2862 final String imageSourceValueCamera = "camera";
2863 final String imageSourceValueGallery = "gallery";
2864
2865 // image source can be 'gallery' or 'camera'.
2866 String imageSource = "";
2867
2868 // We add the camera intent if there was no accept type (or '*/*') or 'image/*'.
2869 boolean addCameraIntent = true;
2870
Grace Kloba22ac16e2009-10-07 18:00:23 -07002871 if (mUploadMessage != null) return;
2872 mUploadMessage = uploadMsg;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002873
2874 // Parse the accept type.
2875 String params[] = acceptType.split(";");
2876 String mimeType = params[0];
2877
2878 for (String p : params) {
2879 String[] keyValue = p.split("=");
2880 if (keyValue.length == 2) {
2881 // Process key=value parameters.
2882 if (imageSourceKey.equals(keyValue[0])) {
2883 imageSource = keyValue[1];
2884 }
2885 }
2886 }
2887
2888 // This intent will display the standard OPENABLE file picker.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002889 Intent i = new Intent(Intent.ACTION_GET_CONTENT);
2890 i.addCategory(Intent.CATEGORY_OPENABLE);
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002891
2892 // Create an intent to add to the standard file picker that will
2893 // capture an image from the camera. We'll combine this intent with
2894 // the standard OPENABLE picker unless the web developer specifically
2895 // requested the camera or gallery be opened by passing a parameter
2896 // in the accept type.
2897 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
2898 File externalDataDir = Environment.getExternalStoragePublicDirectory(
2899 Environment.DIRECTORY_DCIM);
2900 File cameraDataDir = new File(externalDataDir.getAbsolutePath() +
2901 File.separator + "browser-photos");
2902 cameraDataDir.mkdirs();
2903 mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator +
2904 System.currentTimeMillis() + ".jpg";
2905 cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCameraFilePath)));
2906
2907 if (mimeType.equals(imageMimeType)) {
2908 i.setType(imageMimeType);
2909 if (imageSource.equals(imageSourceValueCamera)) {
2910 // Specified 'image/*' and requested the camera, so go ahead and launch the camera
2911 // directly.
2912 BrowserActivity.this.startActivityForResult(cameraIntent, FILE_SELECTED);
2913 return;
2914 } else if (imageSource.equals(imageSourceValueGallery)) {
2915 // Specified gallery as the source, so don't want to consider the camera.
2916 addCameraIntent = false;
2917 }
2918 } else {
2919 i.setType("*/*");
2920 }
2921
2922 // Combine the chooser and the extra choices (like camera)
2923 Intent chooser = new Intent(Intent.ACTION_CHOOSER);
2924 chooser.putExtra(Intent.EXTRA_INTENT, i);
2925
2926 if (addCameraIntent) {
2927 // Add the camera Intent
2928 Intent[] choices = new Intent[1];
2929 choices[0] = cameraIntent;
2930 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, choices);
2931 }
2932
2933 chooser.putExtra(Intent.EXTRA_TITLE, getString(R.string.choose_upload));
2934 BrowserActivity.this.startActivityForResult(chooser, FILE_SELECTED);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002935 }
2936
2937 // -------------------------------------------------------------------------
2938 // Implement functions for DownloadListener
2939 // -------------------------------------------------------------------------
2940
The Android Open Source Project0c908882009-03-03 19:32:16 -08002941 /**
2942 * Notify the host application a download should be done, or that
2943 * the data should be streamed if a streaming viewer is available.
2944 * @param url The full url to the content that should be downloaded
2945 * @param contentDisposition Content-disposition http header, if
2946 * present.
2947 * @param mimetype The mimetype of the content reported by the server
2948 * @param contentLength The file size reported by the server
2949 */
2950 public void onDownloadStart(String url, String userAgent,
2951 String contentDisposition, String mimetype, long contentLength) {
2952 // if we're dealing wih A/V content that's not explicitly marked
2953 // for download, check if it's streamable.
2954 if (contentDisposition == null
Patrick Scotte1fb9662009-08-31 14:31:52 -04002955 || !contentDisposition.regionMatches(
2956 true, 0, "attachment", 0, 10)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002957 // query the package manager to see if there's a registered handler
2958 // that matches.
2959 Intent intent = new Intent(Intent.ACTION_VIEW);
2960 intent.setDataAndType(Uri.parse(url), mimetype);
Patrick Scotte1fb9662009-08-31 14:31:52 -04002961 ResolveInfo info = getPackageManager().resolveActivity(intent,
2962 PackageManager.MATCH_DEFAULT_ONLY);
2963 if (info != null) {
2964 ComponentName myName = getComponentName();
2965 // If we resolved to ourselves, we don't want to attempt to
2966 // load the url only to try and download it again.
2967 if (!myName.getPackageName().equals(
2968 info.activityInfo.packageName)
2969 || !myName.getClassName().equals(
2970 info.activityInfo.name)) {
2971 // someone (other than us) knows how to handle this mime
2972 // type with this scheme, don't download.
2973 try {
2974 startActivity(intent);
2975 return;
2976 } catch (ActivityNotFoundException ex) {
2977 if (LOGD_ENABLED) {
2978 Log.d(LOGTAG, "activity not found for " + mimetype
2979 + " over " + Uri.parse(url).getScheme(),
2980 ex);
2981 }
2982 // Best behavior is to fall back to a download in this
2983 // case
The Android Open Source Project0c908882009-03-03 19:32:16 -08002984 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002985 }
2986 }
2987 }
2988 onDownloadStartNoStream(url, userAgent, contentDisposition, mimetype, contentLength);
2989 }
2990
Kristian Monsenfa52d172010-03-25 18:29:21 +00002991 // This is to work around the fact that java.net.URI throws Exceptions
2992 // instead of just encoding URL's properly
2993 // Helper method for onDownloadStartNoStream
2994 private static String encodePath(String path) {
2995 char[] chars = path.toCharArray();
2996
2997 boolean needed = false;
2998 for (char c : chars) {
2999 if (c == '[' || c == ']') {
3000 needed = true;
3001 break;
3002 }
3003 }
3004 if (needed == false) {
3005 return path;
3006 }
3007
3008 StringBuilder sb = new StringBuilder("");
3009 for (char c : chars) {
3010 if (c == '[' || c == ']') {
3011 sb.append('%');
3012 sb.append(Integer.toHexString(c));
3013 } else {
3014 sb.append(c);
3015 }
3016 }
3017
3018 return sb.toString();
3019 }
3020
The Android Open Source Project0c908882009-03-03 19:32:16 -08003021 /**
3022 * Notify the host application a download should be done, even if there
3023 * is a streaming viewer available for thise type.
3024 * @param url The full url to the content that should be downloaded
3025 * @param contentDisposition Content-disposition http header, if
3026 * present.
3027 * @param mimetype The mimetype of the content reported by the server
3028 * @param contentLength The file size reported by the server
3029 */
3030 /*package */ void onDownloadStartNoStream(String url, String userAgent,
3031 String contentDisposition, String mimetype, long contentLength) {
3032
3033 String filename = URLUtil.guessFileName(url,
3034 contentDisposition, mimetype);
3035
3036 // Check to see if we have an SDCard
3037 String status = Environment.getExternalStorageState();
3038 if (!status.equals(Environment.MEDIA_MOUNTED)) {
3039 int title;
3040 String msg;
3041
3042 // Check to see if the SDCard is busy, same as the music app
3043 if (status.equals(Environment.MEDIA_SHARED)) {
3044 msg = getString(R.string.download_sdcard_busy_dlg_msg);
3045 title = R.string.download_sdcard_busy_dlg_title;
3046 } else {
3047 msg = getString(R.string.download_no_sdcard_dlg_msg, filename);
3048 title = R.string.download_no_sdcard_dlg_title;
3049 }
3050
3051 new AlertDialog.Builder(this)
3052 .setTitle(title)
3053 .setIcon(android.R.drawable.ic_dialog_alert)
3054 .setMessage(msg)
3055 .setPositiveButton(R.string.ok, null)
3056 .show();
3057 return;
3058 }
3059
Kristian Monsenfa52d172010-03-25 18:29:21 +00003060 // java.net.URI is a lot stricter than KURL so we have to encode some
3061 // extra characters. Fix for b 2538060 and b 1634719
3062 WebAddress webAddress;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003063 try {
Kristian Monsenfa52d172010-03-25 18:29:21 +00003064 webAddress = new WebAddress(url);
3065 webAddress.mPath = encodePath(webAddress.mPath);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003066 } catch (Exception e) {
Kristian Monsenfa52d172010-03-25 18:29:21 +00003067 // This only happens for very bad urls, we want to chatch the
3068 // exception here
3069 Log.e(LOGTAG, "Exception trying to parse url:" + url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003070 return;
3071 }
3072
3073 // XXX: Have to use the old url since the cookies were stored using the
3074 // old percent-encoded url.
3075 String cookies = CookieManager.getInstance().getCookie(url);
3076
3077 ContentValues values = new ContentValues();
Kristian Monsenfa52d172010-03-25 18:29:21 +00003078 values.put(Downloads.Impl.COLUMN_URI, webAddress.toString());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003079 values.put(Downloads.Impl.COLUMN_COOKIE_DATA, cookies);
3080 values.put(Downloads.Impl.COLUMN_USER_AGENT, userAgent);
3081 values.put(Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003082 getPackageName());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003083 values.put(Downloads.Impl.COLUMN_NOTIFICATION_CLASS,
Leon Scrogginsa563d092010-04-19 16:53:49 -04003084 OpenDownloadReceiver.class.getCanonicalName());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003085 values.put(Downloads.Impl.COLUMN_VISIBILITY,
3086 Downloads.Impl.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
3087 values.put(Downloads.Impl.COLUMN_MIME_TYPE, mimetype);
3088 values.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, filename);
Kristian Monsenfa52d172010-03-25 18:29:21 +00003089 values.put(Downloads.Impl.COLUMN_DESCRIPTION, webAddress.mHost);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003090 if (contentLength > 0) {
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003091 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, contentLength);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003092 }
3093 if (mimetype == null) {
3094 // We must have long pressed on a link or image to download it. We
3095 // are not sure of the mimetype in this case, so do a head request
3096 new FetchUrlMimeType(this).execute(values);
3097 } else {
3098 final Uri contentUri =
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003099 getContentResolver().insert(Downloads.Impl.CONTENT_URI, values);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003100 }
Leon Scroggins9191a7f2010-03-18 14:44:44 -04003101 Toast.makeText(this, R.string.download_pending, Toast.LENGTH_SHORT)
3102 .show();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003103 }
3104
Grace Kloba22ac16e2009-10-07 18:00:23 -07003105 // -------------------------------------------------------------------------
3106
The Android Open Source Project0c908882009-03-03 19:32:16 -08003107 /**
3108 * Resets the lock icon. This method is called when we start a new load and
3109 * know the url to be loaded.
3110 */
3111 private void resetLockIcon(String url) {
3112 // Save the lock-icon state (we revert to it if the load gets cancelled)
Grace Kloba22ac16e2009-10-07 18:00:23 -07003113 mTabControl.getCurrentTab().resetLockIcon(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003114 updateLockIconImage(LOCK_ICON_UNSECURE);
3115 }
3116
The Android Open Source Project0c908882009-03-03 19:32:16 -08003117 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003118 * Update the lock icon to correspond to our latest state.
3119 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003120 private void updateLockIconToLatest() {
Leon Scroggins91f65fc2010-06-29 13:52:39 -04003121 Tab t = mTabControl.getCurrentTab();
3122 if (t != null) {
3123 updateLockIconImage(t.getLockIconType());
3124 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003125 }
3126
3127 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08003128 * Updates the lock-icon image in the title-bar.
3129 */
3130 private void updateLockIconImage(int lockIconType) {
3131 Drawable d = null;
3132 if (lockIconType == LOCK_ICON_SECURE) {
3133 d = mSecLockIcon;
3134 } else if (lockIconType == LOCK_ICON_MIXED) {
3135 d = mMixLockIcon;
3136 }
Leon Scroggins68579392009-09-15 15:31:54 -04003137 mTitleBar.setLock(d);
Leon Scrogginsd746a942010-05-19 13:21:44 -04003138 if (!mXLargeScreenSize) {
3139 mFakeTitleBar.setLock(d);
3140 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003141 }
3142
3143 /**
3144 * Displays a page-info dialog.
3145 * @param tab The tab to show info about
3146 * @param fromShowSSLCertificateOnError The flag that indicates whether
3147 * this dialog was opened from the SSL-certificate-on-error dialog or
3148 * not. This is important, since we need to know whether to return to
3149 * the parent dialog or simply dismiss.
3150 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003151 private void showPageInfo(final Tab tab,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003152 final boolean fromShowSSLCertificateOnError) {
3153 final LayoutInflater factory = LayoutInflater
3154 .from(this);
3155
3156 final View pageInfoView = factory.inflate(R.layout.page_info, null);
3157
3158 final WebView view = tab.getWebView();
3159
3160 String url = null;
3161 String title = null;
3162
3163 if (view == null) {
3164 url = tab.getUrl();
3165 title = tab.getTitle();
3166 } else if (view == mTabControl.getCurrentWebView()) {
3167 // Use the cached title and url if this is the current WebView
3168 url = mUrl;
3169 title = mTitle;
3170 } else {
3171 url = view.getUrl();
3172 title = view.getTitle();
3173 }
3174
3175 if (url == null) {
3176 url = "";
3177 }
3178 if (title == null) {
3179 title = "";
3180 }
3181
3182 ((TextView) pageInfoView.findViewById(R.id.address)).setText(url);
3183 ((TextView) pageInfoView.findViewById(R.id.title)).setText(title);
3184
3185 mPageInfoView = tab;
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05003186 mPageInfoFromShowSSLCertificateOnError = fromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003187
3188 AlertDialog.Builder alertDialogBuilder =
3189 new AlertDialog.Builder(this)
3190 .setTitle(R.string.page_info).setIcon(android.R.drawable.ic_dialog_info)
3191 .setView(pageInfoView)
3192 .setPositiveButton(
3193 R.string.ok,
3194 new DialogInterface.OnClickListener() {
3195 public void onClick(DialogInterface dialog,
3196 int whichButton) {
3197 mPageInfoDialog = null;
3198 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003199
3200 // if we came here from the SSL error dialog
3201 if (fromShowSSLCertificateOnError) {
3202 // go back to the SSL error dialog
3203 showSSLCertificateOnError(
3204 mSSLCertificateOnErrorView,
3205 mSSLCertificateOnErrorHandler,
3206 mSSLCertificateOnErrorError);
3207 }
3208 }
3209 })
3210 .setOnCancelListener(
3211 new DialogInterface.OnCancelListener() {
3212 public void onCancel(DialogInterface dialog) {
3213 mPageInfoDialog = null;
3214 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003215
3216 // if we came here from the SSL error dialog
3217 if (fromShowSSLCertificateOnError) {
3218 // go back to the SSL error dialog
3219 showSSLCertificateOnError(
3220 mSSLCertificateOnErrorView,
3221 mSSLCertificateOnErrorHandler,
3222 mSSLCertificateOnErrorError);
3223 }
3224 }
3225 });
3226
3227 // if we have a main top-level page SSL certificate set or a certificate
3228 // error
3229 if (fromShowSSLCertificateOnError ||
3230 (view != null && view.getCertificate() != null)) {
3231 // add a 'View Certificate' button
3232 alertDialogBuilder.setNeutralButton(
3233 R.string.view_certificate,
3234 new DialogInterface.OnClickListener() {
3235 public void onClick(DialogInterface dialog,
3236 int whichButton) {
3237 mPageInfoDialog = null;
3238 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003239
3240 // if we came here from the SSL error dialog
3241 if (fromShowSSLCertificateOnError) {
3242 // go back to the SSL error dialog
3243 showSSLCertificateOnError(
3244 mSSLCertificateOnErrorView,
3245 mSSLCertificateOnErrorHandler,
3246 mSSLCertificateOnErrorError);
3247 } else {
3248 // otherwise, display the top-most certificate from
3249 // the chain
3250 if (view.getCertificate() != null) {
3251 showSSLCertificate(tab);
3252 }
3253 }
3254 }
3255 });
3256 }
3257
3258 mPageInfoDialog = alertDialogBuilder.show();
3259 }
3260
3261 /**
3262 * Displays the main top-level page SSL certificate dialog
3263 * (accessible from the Page-Info dialog).
3264 * @param tab The tab to show certificate for.
3265 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003266 private void showSSLCertificate(final Tab tab) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003267 final View certificateView =
3268 inflateCertificateView(tab.getWebView().getCertificate());
3269 if (certificateView == null) {
3270 return;
3271 }
3272
3273 LayoutInflater factory = LayoutInflater.from(this);
3274
3275 final LinearLayout placeholder =
3276 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3277
3278 LinearLayout ll = (LinearLayout) factory.inflate(
3279 R.layout.ssl_success, placeholder);
3280 ((TextView)ll.findViewById(R.id.success))
3281 .setText(R.string.ssl_certificate_is_valid);
3282
3283 mSSLCertificateView = tab;
3284 mSSLCertificateDialog =
3285 new AlertDialog.Builder(this)
3286 .setTitle(R.string.ssl_certificate).setIcon(
3287 R.drawable.ic_dialog_browser_certificate_secure)
3288 .setView(certificateView)
3289 .setPositiveButton(R.string.ok,
3290 new DialogInterface.OnClickListener() {
3291 public void onClick(DialogInterface dialog,
3292 int whichButton) {
3293 mSSLCertificateDialog = null;
3294 mSSLCertificateView = null;
3295
3296 showPageInfo(tab, false);
3297 }
3298 })
3299 .setOnCancelListener(
3300 new DialogInterface.OnCancelListener() {
3301 public void onCancel(DialogInterface dialog) {
3302 mSSLCertificateDialog = null;
3303 mSSLCertificateView = null;
3304
3305 showPageInfo(tab, false);
3306 }
3307 })
3308 .show();
3309 }
3310
3311 /**
3312 * Displays the SSL error certificate dialog.
3313 * @param view The target web-view.
3314 * @param handler The SSL error handler responsible for cancelling the
3315 * connection that resulted in an SSL error or proceeding per user request.
3316 * @param error The SSL error object.
3317 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003318 void showSSLCertificateOnError(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003319 final WebView view, final SslErrorHandler handler, final SslError error) {
3320
3321 final View certificateView =
3322 inflateCertificateView(error.getCertificate());
3323 if (certificateView == null) {
3324 return;
3325 }
3326
3327 LayoutInflater factory = LayoutInflater.from(this);
3328
3329 final LinearLayout placeholder =
3330 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3331
3332 if (error.hasError(SslError.SSL_UNTRUSTED)) {
3333 LinearLayout ll = (LinearLayout)factory
3334 .inflate(R.layout.ssl_warning, placeholder);
3335 ((TextView)ll.findViewById(R.id.warning))
3336 .setText(R.string.ssl_untrusted);
3337 }
3338
3339 if (error.hasError(SslError.SSL_IDMISMATCH)) {
3340 LinearLayout ll = (LinearLayout)factory
3341 .inflate(R.layout.ssl_warning, placeholder);
3342 ((TextView)ll.findViewById(R.id.warning))
3343 .setText(R.string.ssl_mismatch);
3344 }
3345
3346 if (error.hasError(SslError.SSL_EXPIRED)) {
3347 LinearLayout ll = (LinearLayout)factory
3348 .inflate(R.layout.ssl_warning, placeholder);
3349 ((TextView)ll.findViewById(R.id.warning))
3350 .setText(R.string.ssl_expired);
3351 }
3352
3353 if (error.hasError(SslError.SSL_NOTYETVALID)) {
3354 LinearLayout ll = (LinearLayout)factory
3355 .inflate(R.layout.ssl_warning, placeholder);
3356 ((TextView)ll.findViewById(R.id.warning))
3357 .setText(R.string.ssl_not_yet_valid);
3358 }
3359
3360 mSSLCertificateOnErrorHandler = handler;
3361 mSSLCertificateOnErrorView = view;
3362 mSSLCertificateOnErrorError = error;
3363 mSSLCertificateOnErrorDialog =
3364 new AlertDialog.Builder(this)
3365 .setTitle(R.string.ssl_certificate).setIcon(
3366 R.drawable.ic_dialog_browser_certificate_partially_secure)
3367 .setView(certificateView)
3368 .setPositiveButton(R.string.ok,
3369 new DialogInterface.OnClickListener() {
3370 public void onClick(DialogInterface dialog,
3371 int whichButton) {
3372 mSSLCertificateOnErrorDialog = null;
3373 mSSLCertificateOnErrorView = null;
3374 mSSLCertificateOnErrorHandler = null;
3375 mSSLCertificateOnErrorError = null;
3376
Grace Kloba22ac16e2009-10-07 18:00:23 -07003377 view.getWebViewClient().onReceivedSslError(
3378 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003379 }
3380 })
3381 .setNeutralButton(R.string.page_info_view,
3382 new DialogInterface.OnClickListener() {
3383 public void onClick(DialogInterface dialog,
3384 int whichButton) {
3385 mSSLCertificateOnErrorDialog = null;
3386
3387 // do not clear the dialog state: we will
3388 // need to show the dialog again once the
3389 // user is done exploring the page-info details
3390
3391 showPageInfo(mTabControl.getTabFromView(view),
3392 true);
3393 }
3394 })
3395 .setOnCancelListener(
3396 new DialogInterface.OnCancelListener() {
3397 public void onCancel(DialogInterface dialog) {
3398 mSSLCertificateOnErrorDialog = null;
3399 mSSLCertificateOnErrorView = null;
3400 mSSLCertificateOnErrorHandler = null;
3401 mSSLCertificateOnErrorError = null;
3402
Grace Kloba22ac16e2009-10-07 18:00:23 -07003403 view.getWebViewClient().onReceivedSslError(
3404 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003405 }
3406 })
3407 .show();
3408 }
3409
3410 /**
3411 * Inflates the SSL certificate view (helper method).
3412 * @param certificate The SSL certificate.
3413 * @return The resultant certificate view with issued-to, issued-by,
3414 * issued-on, expires-on, and possibly other fields set.
3415 * If the input certificate is null, returns null.
3416 */
3417 private View inflateCertificateView(SslCertificate certificate) {
3418 if (certificate == null) {
3419 return null;
3420 }
3421
3422 LayoutInflater factory = LayoutInflater.from(this);
3423
3424 View certificateView = factory.inflate(
3425 R.layout.ssl_certificate, null);
3426
3427 // issued to:
3428 SslCertificate.DName issuedTo = certificate.getIssuedTo();
3429 if (issuedTo != null) {
3430 ((TextView) certificateView.findViewById(R.id.to_common))
3431 .setText(issuedTo.getCName());
3432 ((TextView) certificateView.findViewById(R.id.to_org))
3433 .setText(issuedTo.getOName());
3434 ((TextView) certificateView.findViewById(R.id.to_org_unit))
3435 .setText(issuedTo.getUName());
3436 }
3437
3438 // issued by:
3439 SslCertificate.DName issuedBy = certificate.getIssuedBy();
3440 if (issuedBy != null) {
3441 ((TextView) certificateView.findViewById(R.id.by_common))
3442 .setText(issuedBy.getCName());
3443 ((TextView) certificateView.findViewById(R.id.by_org))
3444 .setText(issuedBy.getOName());
3445 ((TextView) certificateView.findViewById(R.id.by_org_unit))
3446 .setText(issuedBy.getUName());
3447 }
3448
3449 // issued on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003450 String issuedOn = formatCertificateDate(
3451 certificate.getValidNotBeforeDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003452 ((TextView) certificateView.findViewById(R.id.issued_on))
3453 .setText(issuedOn);
3454
3455 // expires on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003456 String expiresOn = formatCertificateDate(
3457 certificate.getValidNotAfterDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003458 ((TextView) certificateView.findViewById(R.id.expires_on))
3459 .setText(expiresOn);
3460
3461 return certificateView;
3462 }
3463
3464 /**
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003465 * Formats the certificate date to a properly localized date string.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003466 * @return Properly localized version of the certificate date string and
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003467 * the "" if it fails to localize.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003468 */
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003469 private String formatCertificateDate(Date certificateDate) {
3470 if (certificateDate == null) {
3471 return "";
The Android Open Source Project0c908882009-03-03 19:32:16 -08003472 }
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003473 String formattedDate = DateFormat.getDateFormat(this).format(certificateDate);
3474 if (formattedDate == null) {
3475 return "";
3476 }
3477 return formattedDate;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003478 }
3479
3480 /**
3481 * Displays an http-authentication dialog.
3482 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003483 void showHttpAuthentication(final HttpAuthHandler handler,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003484 final String host, final String realm, final String title,
3485 final String name, final String password, int focusId) {
3486 LayoutInflater factory = LayoutInflater.from(this);
3487 final View v = factory
3488 .inflate(R.layout.http_authentication, null);
3489 if (name != null) {
3490 ((EditText) v.findViewById(R.id.username_edit)).setText(name);
3491 }
3492 if (password != null) {
3493 ((EditText) v.findViewById(R.id.password_edit)).setText(password);
3494 }
3495
3496 String titleText = title;
3497 if (titleText == null) {
3498 titleText = getText(R.string.sign_in_to).toString().replace(
3499 "%s1", host).replace("%s2", realm);
3500 }
3501
3502 mHttpAuthHandler = handler;
3503 AlertDialog dialog = new AlertDialog.Builder(this)
3504 .setTitle(titleText)
3505 .setIcon(android.R.drawable.ic_dialog_alert)
3506 .setView(v)
3507 .setPositiveButton(R.string.action,
3508 new DialogInterface.OnClickListener() {
3509 public void onClick(DialogInterface dialog,
3510 int whichButton) {
3511 String nm = ((EditText) v
3512 .findViewById(R.id.username_edit))
3513 .getText().toString();
3514 String pw = ((EditText) v
3515 .findViewById(R.id.password_edit))
3516 .getText().toString();
3517 BrowserActivity.this.setHttpAuthUsernamePassword
3518 (host, realm, nm, pw);
3519 handler.proceed(nm, pw);
3520 mHttpAuthenticationDialog = null;
3521 mHttpAuthHandler = null;
3522 }})
3523 .setNegativeButton(R.string.cancel,
3524 new DialogInterface.OnClickListener() {
3525 public void onClick(DialogInterface dialog,
3526 int whichButton) {
3527 handler.cancel();
3528 BrowserActivity.this.resetTitleAndRevertLockIcon();
3529 mHttpAuthenticationDialog = null;
3530 mHttpAuthHandler = null;
3531 }})
3532 .setOnCancelListener(new DialogInterface.OnCancelListener() {
3533 public void onCancel(DialogInterface dialog) {
3534 handler.cancel();
3535 BrowserActivity.this.resetTitleAndRevertLockIcon();
3536 mHttpAuthenticationDialog = null;
3537 mHttpAuthHandler = null;
3538 }})
3539 .create();
3540 // Make the IME appear when the dialog is displayed if applicable.
3541 dialog.getWindow().setSoftInputMode(
3542 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
3543 dialog.show();
3544 if (focusId != 0) {
3545 dialog.findViewById(focusId).requestFocus();
3546 } else {
3547 v.findViewById(R.id.username_edit).requestFocus();
3548 }
3549 mHttpAuthenticationDialog = dialog;
3550 }
3551
3552 public int getProgress() {
3553 WebView w = mTabControl.getCurrentWebView();
3554 if (w != null) {
3555 return w.getProgress();
3556 } else {
3557 return 100;
3558 }
3559 }
3560
3561 /**
3562 * Set HTTP authentication password.
3563 *
3564 * @param host The host for the password
3565 * @param realm The realm for the password
3566 * @param username The username for the password. If it is null, it means
3567 * password can't be saved.
3568 * @param password The password
3569 */
3570 public void setHttpAuthUsernamePassword(String host, String realm,
3571 String username,
3572 String password) {
Steve Block95a53b22010-03-25 17:24:58 +00003573 WebView w = getTopWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003574 if (w != null) {
3575 w.setHttpAuthUsernamePassword(host, realm, username, password);
3576 }
3577 }
3578
3579 /**
3580 * connectivity manager says net has come or gone... inform the user
3581 * @param up true if net has come up, false if net has gone down
3582 */
3583 public void onNetworkToggle(boolean up) {
3584 if (up == mIsNetworkUp) {
3585 return;
3586 } else if (up) {
3587 mIsNetworkUp = true;
3588 if (mAlertDialog != null) {
3589 mAlertDialog.cancel();
3590 mAlertDialog = null;
3591 }
3592 } else {
3593 mIsNetworkUp = false;
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003594 if (mInLoad) {
3595 createAndShowNetworkDialog();
3596 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003597 }
3598 WebView w = mTabControl.getCurrentWebView();
3599 if (w != null) {
3600 w.setNetworkAvailable(up);
3601 }
3602 }
3603
Grace Kloba22ac16e2009-10-07 18:00:23 -07003604 boolean isNetworkUp() {
3605 return mIsNetworkUp;
3606 }
3607
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003608 // This method shows the network dialog alerting the user that the net is
3609 // down. It will only show the dialog if mAlertDialog is null.
3610 private void createAndShowNetworkDialog() {
3611 if (mAlertDialog == null) {
3612 mAlertDialog = new AlertDialog.Builder(this)
3613 .setTitle(R.string.loadSuspendedTitle)
3614 .setMessage(R.string.loadSuspended)
3615 .setPositiveButton(R.string.ok, null)
3616 .show();
3617 }
3618 }
3619
The Android Open Source Project0c908882009-03-03 19:32:16 -08003620 @Override
3621 protected void onActivityResult(int requestCode, int resultCode,
3622 Intent intent) {
Grace Klobabb394f32009-11-19 10:26:37 -08003623 if (getTopWindow() == null) return;
3624
The Android Open Source Project0c908882009-03-03 19:32:16 -08003625 switch (requestCode) {
3626 case COMBO_PAGE:
3627 if (resultCode == RESULT_OK && intent != null) {
3628 String data = intent.getAction();
3629 Bundle extras = intent.getExtras();
3630 if (extras != null && extras.getBoolean("new_window", false)) {
Leon Scroggins25d35472009-09-15 11:37:27 -04003631 openTab(data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003632 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -07003633 final Tab currentTab =
The Android Open Source Project0c908882009-03-03 19:32:16 -08003634 mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04003635 dismissSubWindow(currentTab);
3636 if (data != null && data.length() != 0) {
Leon Scroggins92472e82010-02-17 16:32:28 -05003637 loadUrl(getTopWindow(), data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003638 }
3639 }
3640 }
Leon Scrogginsfde97462010-01-11 13:06:21 -05003641 // Deliberately fall through to PREFERENCES_PAGE, since the
3642 // same extra may be attached to the COMBO_PAGE
3643 case PREFERENCES_PAGE:
3644 if (resultCode == RESULT_OK && intent != null) {
3645 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
3646 if (BrowserSettings.PREF_CLEAR_HISTORY.equals(action)) {
3647 mTabControl.removeParentChildRelationShips();
3648 }
3649 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003650 break;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003651 // Choose a file from the file picker.
3652 case FILE_SELECTED:
3653 if (null == mUploadMessage) break;
3654 Uri result = intent == null || resultCode != RESULT_OK ? null
3655 : intent.getData();
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003656
3657 // As we ask the camera to save the result of the user taking
3658 // a picture, the camera application does not return anything other
3659 // than RESULT_OK. So we need to check whether the file we expected
3660 // was written to disk in the in the case that we
3661 // did not get an intent returned but did get a RESULT_OK. If it was,
3662 // we assume that this result has came back from the camera.
3663 if (result == null && intent == null && resultCode == RESULT_OK) {
3664 File cameraFile = new File(mCameraFilePath);
3665 if (cameraFile.exists()) {
3666 result = Uri.fromFile(cameraFile);
Ben Murdoch07d34732010-05-27 18:34:46 +01003667 // Broadcast to the media scanner that we have a new photo
3668 // so it will be added into the gallery for the user.
3669 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003670 }
3671 }
3672
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003673 mUploadMessage.onReceiveValue(result);
3674 mUploadMessage = null;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003675 mCameraFilePath = null;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003676 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003677 default:
3678 break;
3679 }
Leon Scroggins30444232009-09-04 18:36:20 -04003680 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003681 }
3682
3683 /*
3684 * This method is called as a result of the user selecting the options
Leon Scrogginsf08809b2010-01-21 09:27:46 -05003685 * menu to see the download window. It shows the download window on top of
3686 * the current window.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003687 */
Leon Scrogginsf08809b2010-01-21 09:27:46 -05003688 private void viewDownloads(Uri downloadRecord) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003689 Intent intent = new Intent(this,
3690 BrowserDownloadPage.class);
3691 intent.setData(downloadRecord);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003692 startActivityForResult(intent, BrowserActivity.DOWNLOAD_PAGE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003693
3694 }
3695
Leon Scroggins160a7e72009-08-14 18:28:01 -04003696 /**
3697 * Open the Go page.
3698 * @param startWithHistory If true, open starting on the history tab.
3699 * Otherwise, start with the bookmarks tab.
Leon Scroggins160a7e72009-08-14 18:28:01 -04003700 */
Leon Scroggins30444232009-09-04 18:36:20 -04003701 /* package */ void bookmarksOrHistoryPicker(boolean startWithHistory) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003702 WebView current = mTabControl.getCurrentWebView();
3703 if (current == null) {
3704 return;
3705 }
3706 Intent intent = new Intent(this,
3707 CombinedBookmarkHistoryActivity.class);
3708 String title = current.getTitle();
3709 String url = current.getUrl();
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003710 Bitmap thumbnail = createScreenshot(current);
3711
The Android Open Source Project0c908882009-03-03 19:32:16 -08003712 // Just in case the user opens bookmarks before a page finishes loading
3713 // so the current history item, and therefore the page, is null.
3714 if (null == url) {
3715 url = mLastEnteredUrl;
3716 // This can happen.
3717 if (null == url) {
3718 url = mSettings.getHomePage();
3719 }
3720 }
3721 // In case the web page has not yet received its associated title.
3722 if (title == null) {
3723 title = url;
3724 }
3725 intent.putExtra("title", title);
3726 intent.putExtra("url", url);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003727 intent.putExtra("thumbnail", thumbnail);
Leon Scroggins30444232009-09-04 18:36:20 -04003728 // Disable opening in a new window if we have maxed out the windows
Grace Kloba22ac16e2009-10-07 18:00:23 -07003729 intent.putExtra("disable_new_window", !mTabControl.canCreateNewTab());
Patrick Scott3918d442009-08-04 13:22:29 -04003730 intent.putExtra("touch_icon_url", current.getTouchIconUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003731 if (startWithHistory) {
3732 intent.putExtra(CombinedBookmarkHistoryActivity.STARTING_TAB,
3733 CombinedBookmarkHistoryActivity.HISTORY_TAB);
3734 }
3735 startActivityForResult(intent, COMBO_PAGE);
3736 }
3737
3738 // Called when loading from context menu or LOAD_URL message
Leon Scroggins92472e82010-02-17 16:32:28 -05003739 private void loadUrlFromContext(WebView view, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003740 // In case the user enters nothing.
3741 if (url != null && url.length() != 0 && view != null) {
3742 url = smartUrlFilter(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003743 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
Leon Scroggins92472e82010-02-17 16:32:28 -05003744 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003745 }
3746 }
3747 }
3748
Leon Scroggins92472e82010-02-17 16:32:28 -05003749 /**
3750 * Load the URL into the given WebView and update the title bar
3751 * to reflect the new load. Call this instead of WebView.loadUrl
3752 * directly.
3753 * @param view The WebView used to load url.
3754 * @param url The URL to load.
3755 */
3756 private void loadUrl(WebView view, String url) {
3757 updateTitleBarForNewLoad(view, url);
3758 view.loadUrl(url);
3759 }
3760
3761 /**
3762 * Load UrlData into a Tab and update the title bar to reflect the new
3763 * load. Call this instead of UrlData.loadIn directly.
3764 * @param t The Tab used to load.
3765 * @param data The UrlData being loaded.
3766 */
3767 private void loadUrlDataIn(Tab t, UrlData data) {
3768 updateTitleBarForNewLoad(t.getWebView(), data.mUrl);
3769 data.loadIn(t);
3770 }
3771
3772 /**
3773 * If the WebView is the top window, update the title bar to reflect
3774 * loading the new URL. i.e. set its text, clear the favicon (which
3775 * will be set once the page begins loading), and set the progress to
3776 * INITIAL_PROGRESS to show that the page has begun to load. Called
3777 * by loadUrl and loadUrlDataIn.
3778 * @param view The WebView that is starting a load.
3779 * @param url The URL that is being loaded.
3780 */
3781 private void updateTitleBarForNewLoad(WebView view, String url) {
3782 if (view == getTopWindow()) {
3783 setUrlTitle(url, null);
3784 setFavicon(null);
3785 onProgressChanged(view, INITIAL_PROGRESS);
3786 }
3787 }
3788
The Android Open Source Project0c908882009-03-03 19:32:16 -08003789 private String smartUrlFilter(Uri inUri) {
3790 if (inUri != null) {
3791 return smartUrlFilter(inUri.toString());
3792 }
3793 return null;
3794 }
3795
Feng Qianb34f87a2009-03-24 21:27:26 -07003796 protected static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003797 "(?i)" + // switch on case insensitive matching
3798 "(" + // begin group for schema
3799 "(?:http|https|file):\\/\\/" +
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003800 "|(?:inline|data|about|content|javascript):" +
The Android Open Source Project0c908882009-03-03 19:32:16 -08003801 ")" +
3802 "(.*)" );
3803
3804 /**
3805 * Attempts to determine whether user input is a URL or search
3806 * terms. Anything with a space is passed to search.
3807 *
3808 * Converts to lowercase any mistakenly uppercased schema (i.e.,
3809 * "Http://" converts to "http://"
3810 *
3811 * @return Original or modified URL
3812 *
3813 */
3814 String smartUrlFilter(String url) {
3815
3816 String inUrl = url.trim();
3817 boolean hasSpace = inUrl.indexOf(' ') != -1;
3818
3819 Matcher matcher = ACCEPTED_URI_SCHEMA.matcher(inUrl);
3820 if (matcher.matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003821 // force scheme to lowercase
3822 String scheme = matcher.group(1);
3823 String lcScheme = scheme.toLowerCase();
3824 if (!lcScheme.equals(scheme)) {
Mitsuru Oshima123ecfb2009-05-18 19:11:14 -07003825 inUrl = lcScheme + matcher.group(2);
3826 }
3827 if (hasSpace) {
3828 inUrl = inUrl.replace(" ", "%20");
The Android Open Source Project0c908882009-03-03 19:32:16 -08003829 }
3830 return inUrl;
3831 }
3832 if (hasSpace) {
Satish Sampath565505b2009-05-29 15:37:27 +01003833 // FIXME: Is this the correct place to add to searches?
3834 // what if someone else calls this function?
3835 int shortcut = parseUrlShortcut(inUrl);
3836 if (shortcut != SHORTCUT_INVALID) {
3837 Browser.addSearchUrl(mResolver, inUrl);
3838 String query = inUrl.substring(2);
3839 switch (shortcut) {
3840 case SHORTCUT_GOOGLE_SEARCH:
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003841 return URLUtil.composeSearchUrl(query, QuickSearch_G, QUERY_PLACE_HOLDER);
Satish Sampath565505b2009-05-29 15:37:27 +01003842 case SHORTCUT_WIKIPEDIA_SEARCH:
3843 return URLUtil.composeSearchUrl(query, QuickSearch_W, QUERY_PLACE_HOLDER);
3844 case SHORTCUT_DICTIONARY_SEARCH:
3845 return URLUtil.composeSearchUrl(query, QuickSearch_D, QUERY_PLACE_HOLDER);
3846 case SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH:
The Android Open Source Project0c908882009-03-03 19:32:16 -08003847 // FIXME: we need location in this case
Satish Sampath565505b2009-05-29 15:37:27 +01003848 return URLUtil.composeSearchUrl(query, QuickSearch_L, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003849 }
3850 }
3851 } else {
Dan Egnor5ee906c2009-11-18 12:11:49 -08003852 if (Patterns.WEB_URL.matcher(inUrl).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003853 return URLUtil.guessUrl(inUrl);
3854 }
3855 }
3856
3857 Browser.addSearchUrl(mResolver, inUrl);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003858 return URLUtil.composeSearchUrl(inUrl, QuickSearch_G, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003859 }
3860
Ben Murdochbff2d602009-07-01 20:19:05 +01003861 /* package */ void setShouldShowErrorConsole(boolean flag) {
3862 if (flag == mShouldShowErrorConsole) {
3863 // Nothing to do.
3864 return;
3865 }
3866
3867 mShouldShowErrorConsole = flag;
3868
Grace Kloba22ac16e2009-10-07 18:00:23 -07003869 ErrorConsoleView errorConsole = mTabControl.getCurrentTab()
3870 .getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01003871
3872 if (flag) {
3873 // Setting the show state of the console will cause it's the layout to be inflated.
3874 if (errorConsole.numberOfErrors() > 0) {
3875 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
3876 } else {
3877 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
3878 }
3879
3880 // Now we can add it to the main view.
3881 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08003882 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01003883 ViewGroup.LayoutParams.WRAP_CONTENT));
3884 } else {
3885 mErrorConsoleContainer.removeView(errorConsole);
3886 }
3887
3888 }
3889
Grace Kloba22ac16e2009-10-07 18:00:23 -07003890 boolean shouldShowErrorConsole() {
3891 return mShouldShowErrorConsole;
3892 }
3893
Andrei Popescu163ab742009-10-20 17:58:23 +01003894 private void setStatusBarVisibility(boolean visible) {
3895 int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
3896 getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
3897 }
3898
Andrei Popescu56199cc2010-01-12 22:39:16 +00003899
3900 private void sendNetworkType(String type, String subtype) {
3901 WebView w = mTabControl.getCurrentWebView();
3902 if (w != null) {
3903 w.setNetworkType(type, subtype);
3904 }
3905 }
3906
Andrei Popescu30995e72010-02-09 16:59:58 +00003907 private void packageChanged(String packageName, boolean wasAdded) {
3908 WebView w = mTabControl.getCurrentWebView();
3909 if (w == null) {
3910 return;
3911 }
3912
3913 if (wasAdded) {
3914 w.addPackageName(packageName);
3915 } else {
3916 w.removePackageName(packageName);
3917 }
3918 }
3919
3920 private void addPackageNames(Set<String> packageNames) {
3921 WebView w = mTabControl.getCurrentWebView();
3922 if (w == null) {
3923 return;
3924 }
3925
3926 w.addPackageNames(packageNames);
3927 }
3928
3929 private void getInstalledPackages() {
3930 AsyncTask<Void, Void, Set<String> > task =
3931 new AsyncTask<Void, Void, Set<String> >() {
Michael Kolbe0a36662010-06-29 10:37:12 -07003932 @Override
Andrei Popescu30995e72010-02-09 16:59:58 +00003933 protected Set<String> doInBackground(Void... unused) {
3934 Set<String> installedPackages = new HashSet<String>();
3935 PackageManager pm = BrowserActivity.this.getPackageManager();
3936 if (pm != null) {
3937 List<PackageInfo> packages = pm.getInstalledPackages(0);
3938 for (PackageInfo p : packages) {
3939 if (BrowserActivity.this.sGoogleApps.contains(p.packageName)) {
3940 installedPackages.add(p.packageName);
3941 }
3942 }
3943 }
3944
3945 return installedPackages;
3946 }
3947
3948 // Executes on the UI thread
Michael Kolbe0a36662010-06-29 10:37:12 -07003949 @Override
Andrei Popescu30995e72010-02-09 16:59:58 +00003950 protected void onPostExecute(Set<String> installedPackages) {
3951 addPackageNames(installedPackages);
3952 }
3953 };
3954 task.execute();
3955 }
3956
Grace Klobaeb6eef42009-09-15 17:56:32 -07003957 final static int LOCK_ICON_UNSECURE = 0;
3958 final static int LOCK_ICON_SECURE = 1;
3959 final static int LOCK_ICON_MIXED = 2;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003960
The Android Open Source Project0c908882009-03-03 19:32:16 -08003961 private BrowserSettings mSettings;
3962 private TabControl mTabControl;
3963 private ContentResolver mResolver;
3964 private FrameLayout mContentView;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003965 private View mCustomView;
3966 private FrameLayout mCustomViewContainer;
Andrei Popescuc9b55562009-07-07 10:51:15 +01003967 private WebChromeClient.CustomViewCallback mCustomViewCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003968
3969 // FIXME, temp address onPrepareMenu performance problem. When we move everything out of
3970 // view, we should rewrite this.
3971 private int mCurrentMenuState = 0;
3972 private int mMenuState = R.id.MAIN_MENU;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003973 private int mOldMenuState = EMPTY_MENU;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003974 private static final int EMPTY_MENU = -1;
3975 private Menu mMenu;
3976
3977 private FindDialog mFindDialog;
Cary Clark01cfcdd2010-06-04 16:36:45 -04003978 private SelectDialog mSelectDialog;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003979 // Used to prevent chording to result in firing two shortcuts immediately
3980 // one after another. Fixes bug 1211714.
3981 boolean mCanChord;
3982
3983 private boolean mInLoad;
3984 private boolean mIsNetworkUp;
Ben Murdochb7cc8b42009-09-28 10:59:09 +01003985 private boolean mDidStopLoad;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003986
Cary Clark1f10cbf2010-03-22 11:45:23 -04003987 /* package */ boolean mActivityInPause = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003988
3989 private boolean mMenuIsDown;
3990
The Android Open Source Project0c908882009-03-03 19:32:16 -08003991 private static boolean mInTrace;
3992
3993 // Performance probe
3994 private static final int[] SYSTEM_CPU_FORMAT = new int[] {
3995 Process.PROC_SPACE_TERM | Process.PROC_COMBINE,
3996 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time
3997 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time
3998 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time
3999 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time
4000 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time
4001 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time
4002 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG // 7: softirq time
4003 };
4004
4005 private long mStart;
4006 private long mProcessStart;
4007 private long mUserStart;
4008 private long mSystemStart;
4009 private long mIdleStart;
4010 private long mIrqStart;
4011
4012 private long mUiStart;
4013
4014 private Drawable mMixLockIcon;
4015 private Drawable mSecLockIcon;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004016
4017 /* hold a ref so we can auto-cancel if necessary */
4018 private AlertDialog mAlertDialog;
4019
The Android Open Source Project0c908882009-03-03 19:32:16 -08004020 // The up-to-date URL and title (these can be different from those stored
4021 // in WebView, since it takes some time for the information in WebView to
4022 // get updated)
4023 private String mUrl;
4024 private String mTitle;
4025
4026 // As PageInfo has different style for landscape / portrait, we have
4027 // to re-open it when configuration changed
4028 private AlertDialog mPageInfoDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07004029 private Tab mPageInfoView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004030 // If the Page-Info dialog is launched from the SSL-certificate-on-error
4031 // dialog, we should not just dismiss it, but should get back to the
4032 // SSL-certificate-on-error dialog. This flag is used to store this state
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05004033 private boolean mPageInfoFromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004034
4035 // as SSLCertificateOnError has different style for landscape / portrait,
4036 // we have to re-open it when configuration changed
4037 private AlertDialog mSSLCertificateOnErrorDialog;
4038 private WebView mSSLCertificateOnErrorView;
4039 private SslErrorHandler mSSLCertificateOnErrorHandler;
4040 private SslError mSSLCertificateOnErrorError;
4041
4042 // as SSLCertificate has different style for landscape / portrait, we
4043 // have to re-open it when configuration changed
4044 private AlertDialog mSSLCertificateDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07004045 private Tab mSSLCertificateView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004046
4047 // as HttpAuthentication has different style for landscape / portrait, we
4048 // have to re-open it when configuration changed
4049 private AlertDialog mHttpAuthenticationDialog;
4050 private HttpAuthHandler mHttpAuthHandler;
4051
4052 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
4053 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08004054 ViewGroup.LayoutParams.MATCH_PARENT,
4055 ViewGroup.LayoutParams.MATCH_PARENT);
Andrei Popescuadc008d2009-06-26 14:11:30 +01004056 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
4057 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08004058 ViewGroup.LayoutParams.MATCH_PARENT,
4059 ViewGroup.LayoutParams.MATCH_PARENT,
Andrei Popescuadc008d2009-06-26 14:11:30 +01004060 Gravity.CENTER);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07004061 // Google search
4062 final static String QuickSearch_G = "http://www.google.com/m?q=%s";
The Android Open Source Project0c908882009-03-03 19:32:16 -08004063 // Wikipedia search
4064 final static String QuickSearch_W = "http://en.wikipedia.org/w/index.php?search=%s&go=Go";
4065 // Dictionary search
4066 final static String QuickSearch_D = "http://dictionary.reference.com/search?q=%s";
4067 // Google Mobile Local search
4068 final static String QuickSearch_L = "http://www.google.com/m/search?site=local&q=%s&near=mountain+view";
4069
4070 final static String QUERY_PLACE_HOLDER = "%s";
4071
4072 // "source" parameter for Google search through search key
4073 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
4074 // "source" parameter for Google search through goto menu
4075 final static String GOOGLE_SEARCH_SOURCE_GOTO = "browser-goto";
4076 // "source" parameter for Google search through simplily type
4077 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
4078 // "source" parameter for Google search suggested by the browser
4079 final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
4080 // "source" parameter for Google search from unknown source
4081 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown";
4082
4083 private final static String LOGTAG = "browser";
4084
The Android Open Source Project0c908882009-03-03 19:32:16 -08004085 private String mLastEnteredUrl;
4086
4087 private PowerManager.WakeLock mWakeLock;
4088 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
4089
4090 private Toast mStopToast;
4091
Leon Scroggins571b3762010-05-26 10:25:01 -04004092 private TitleBarBase mTitleBar;
Leon Scroggins81db3662009-06-04 17:45:11 -04004093
Ben Murdochbff2d602009-07-01 20:19:05 +01004094 private LinearLayout mErrorConsoleContainer = null;
4095 private boolean mShouldShowErrorConsole = false;
4096
The Android Open Source Project0c908882009-03-03 19:32:16 -08004097 // As the ids are dynamically created, we can't guarantee that they will
4098 // be in sequence, so this static array maps ids to a window number.
4099 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
4100 { R.id.window_one_menu_id, R.id.window_two_menu_id, R.id.window_three_menu_id,
4101 R.id.window_four_menu_id, R.id.window_five_menu_id, R.id.window_six_menu_id,
4102 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
4103
4104 // monitor platform changes
4105 private IntentFilter mNetworkStateChangedFilter;
4106 private BroadcastReceiver mNetworkStateIntentReceiver;
4107
Grace Klobab4da0ad2009-05-14 14:45:40 -07004108 private BroadcastReceiver mPackageInstallationReceiver;
4109
Bjorn Bringerta7611812010-03-24 11:12:02 +00004110 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
4111
The Android Open Source Project0c908882009-03-03 19:32:16 -08004112 // activity requestCode
Nicolas Roard78a98e42009-05-11 13:34:17 +01004113 final static int COMBO_PAGE = 1;
4114 final static int DOWNLOAD_PAGE = 2;
4115 final static int PREFERENCES_PAGE = 3;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04004116 final static int FILE_SELECTED = 4;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004117
Andrei Popescu540035d2009-09-18 18:59:20 +01004118 // the default <video> poster
4119 private Bitmap mDefaultVideoPoster;
4120 // the video progress view
4121 private View mVideoProgressView;
4122
Andrei Popescu30995e72010-02-09 16:59:58 +00004123 // The Google packages we monitor for the navigator.isApplicationInstalled()
4124 // API. Add as needed.
4125 private static Set<String> sGoogleApps;
4126 static {
4127 sGoogleApps = new HashSet<String>();
4128 sGoogleApps.add("com.google.android.youtube");
4129 }
4130
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004131 /**
4132 * A UrlData class to abstract how the content will be set to WebView.
4133 * This base class uses loadUrl to show the content.
4134 */
Leon Scroggins6eac63e2010-03-15 18:19:14 -04004135 /* package */ static class UrlData {
Grace Kloba068e48b2010-01-26 18:11:27 -08004136 final String mUrl;
4137 final Map<String, String> mHeaders;
Leon Scroggins58d56c62010-01-28 15:12:40 -05004138 final Intent mVoiceIntent;
Grace Kloba60e095c2009-06-16 11:50:55 -07004139
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004140 UrlData(String url) {
4141 this.mUrl = url;
Grace Kloba068e48b2010-01-26 18:11:27 -08004142 this.mHeaders = null;
Leon Scroggins58d56c62010-01-28 15:12:40 -05004143 this.mVoiceIntent = null;
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004144 }
Grace Kloba60e095c2009-06-16 11:50:55 -07004145
Leon Scroggins58d56c62010-01-28 15:12:40 -05004146 UrlData(String url, Map<String, String> headers, Intent intent) {
Grace Kloba068e48b2010-01-26 18:11:27 -08004147 this.mUrl = url;
4148 this.mHeaders = headers;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -05004149 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
4150 .equals(intent.getAction())) {
Leon Scroggins58d56c62010-01-28 15:12:40 -05004151 this.mVoiceIntent = intent;
4152 } else {
4153 this.mVoiceIntent = null;
4154 }
Grace Kloba60e095c2009-06-16 11:50:55 -07004155 }
4156
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004157 boolean isEmpty() {
Leon Scroggins58d56c62010-01-28 15:12:40 -05004158 return mVoiceIntent == null && (mUrl == null || mUrl.length() == 0);
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004159 }
4160
Leon Scroggins92472e82010-02-17 16:32:28 -05004161 /**
4162 * Load this UrlData into the given Tab. Use loadUrlDataIn to update
4163 * the title bar as well.
4164 */
Leon Scroggins58d56c62010-01-28 15:12:40 -05004165 public void loadIn(Tab t) {
4166 if (mVoiceIntent != null) {
4167 t.activateVoiceSearchMode(mVoiceIntent);
4168 } else {
4169 t.getWebView().loadUrl(mUrl, mHeaders);
4170 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004171 }
4172 };
4173
Leon Scroggins1f005d32009-08-10 17:36:42 -04004174 /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08004175}