blob: c7abad162e7f377a184c3916fa72211e98f771c8 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.app.Activity;
The Android Open Source Project0c908882009-03-03 19:32:16 -080020import android.app.AlertDialog;
21import android.app.ProgressDialog;
22import android.app.SearchManager;
23import android.content.ActivityNotFoundException;
24import android.content.BroadcastReceiver;
25import android.content.ComponentName;
Leon Scroggins58d56c62010-01-28 15:12:40 -050026import android.content.ContentProvider;
27import android.content.ContentProviderClient;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.content.ContentResolver;
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -040029import android.content.ContentUris;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.content.ContentValues;
31import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
34import android.content.IntentFilter;
Grace Klobab4da0ad2009-05-14 14:45:40 -070035import android.content.pm.PackageInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.content.pm.PackageManager;
37import android.content.pm.ResolveInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.content.res.Configuration;
39import android.content.res.Resources;
40import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041import android.graphics.Bitmap;
Andrei Popescu540035d2009-09-18 18:59:20 +010042import android.graphics.BitmapFactory;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import android.graphics.Canvas;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import android.graphics.Picture;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040045import android.graphics.PixelFormat;
The Android Open Source Project0c908882009-03-03 19:32:16 -080046import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047import android.net.ConnectivityManager;
Andrei Popescu56199cc2010-01-12 22:39:16 +000048import android.net.NetworkInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049import android.net.Uri;
50import android.net.WebAddress;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051import android.net.http.SslCertificate;
52import android.net.http.SslError;
53import android.os.AsyncTask;
54import android.os.Bundle;
55import android.os.Debug;
56import android.os.Environment;
57import android.os.Handler;
The Android Open Source Project0c908882009-03-03 19:32:16 -080058import android.os.Message;
59import android.os.PowerManager;
60import android.os.Process;
The Android Open Source Project0c908882009-03-03 19:32:16 -080061import android.os.ServiceManager;
62import android.os.SystemClock;
The Android Open Source Project0c908882009-03-03 19:32:16 -080063import android.provider.Browser;
Cary Clark5e335a32009-09-22 14:53:11 -040064import android.provider.ContactsContract;
The Android Open Source Project0c908882009-03-03 19:32:16 -080065import android.provider.Downloads;
66import android.provider.MediaStore;
Michael Kolbe0a36662010-06-29 10:37:12 -070067import android.provider.ContactsContract.Intents.Insert;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050068import android.speech.RecognizerResultsIntent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080069import android.text.IClipboard;
70import android.text.TextUtils;
71import android.text.format.DateFormat;
The Android Open Source Project0c908882009-03-03 19:32:16 -080072import android.util.Log;
Dianne Hackborn385effd2010-02-24 20:03:04 -080073import android.util.Patterns;
The Android Open Source Project0c908882009-03-03 19:32:16 -080074import android.view.ContextMenu;
75import android.view.Gravity;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
79import android.view.MenuInflater;
80import android.view.MenuItem;
81import android.view.View;
82import android.view.ViewGroup;
83import android.view.Window;
84import android.view.WindowManager;
85import android.view.ContextMenu.ContextMenuInfo;
86import android.view.MenuItem.OnMenuItemClickListener;
Svetoslav Ganov2b345992010-05-06 06:13:54 -070087import android.view.accessibility.AccessibilityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080088import android.webkit.CookieManager;
89import android.webkit.CookieSyncManager;
90import android.webkit.DownloadListener;
91import android.webkit.HttpAuthHandler;
Grace Klobab4da0ad2009-05-14 14:45:40 -070092import android.webkit.PluginManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080093import android.webkit.SslErrorHandler;
94import android.webkit.URLUtil;
Leon Clarkecb6cc862009-09-29 18:35:13 +010095import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -080096import android.webkit.WebChromeClient;
97import android.webkit.WebHistoryItem;
98import android.webkit.WebIconDatabase;
99import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800100import android.widget.EditText;
101import android.widget.FrameLayout;
102import android.widget.LinearLayout;
103import android.widget.TextView;
104import android.widget.Toast;
105
Bjorn Bringert10d1cca2010-02-10 14:22:12 +0000106import com.android.common.Search;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500107import com.android.common.speech.LoggingEvents;
Dan Egnor5ee906c2009-11-18 12:11:49 -0800108
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400109import java.io.ByteArrayOutputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110import java.io.File;
Ben Murdoch4f75ba22009-10-27 11:48:28 +0000111import java.io.IOException;
112import java.io.InputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800113import java.net.MalformedURLException;
Dianne Hackborn99189432009-06-17 18:06:18 -0700114import java.net.URISyntaxException;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800115import java.net.URL;
116import java.net.URLEncoder;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800117import java.util.Date;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118import java.util.HashMap;
Andrei Popescu30995e72010-02-09 16:59:58 +0000119import java.util.HashSet;
Grace Kloba00f54c52010-01-27 14:53:51 -0800120import java.util.Iterator;
Andrei Popescu30995e72010-02-09 16:59:58 +0000121import java.util.List;
Grace Kloba068e48b2010-01-26 18:11:27 -0800122import java.util.Map;
Andrei Popescu30995e72010-02-09 16:59:58 +0000123import java.util.Set;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124import java.util.regex.Matcher;
125import java.util.regex.Pattern;
Ben Murdoch6af492a2010-06-15 12:38:17 +0100126import java.util.Vector;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800127
128public class BrowserActivity extends Activity
Shimeng (Simon) Wang98d5fce2010-03-16 13:23:39 -0700129 implements View.OnCreateContextMenuListener, DownloadListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800130
Dave Bort31a6d1c2009-04-13 15:56:49 -0700131 /* Define some aliases to make these debugging flags easier to refer to.
132 * This file imports android.provider.Browser, so we can't just refer to "Browser.DEBUG".
133 */
134 private final static boolean DEBUG = com.android.browser.Browser.DEBUG;
135 private final static boolean LOGV_ENABLED = com.android.browser.Browser.LOGV_ENABLED;
136 private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
137
Satish Sampath565505b2009-05-29 15:37:27 +0100138 // These are single-character shortcuts for searching popular sources.
139 private static final int SHORTCUT_INVALID = 0;
140 private static final int SHORTCUT_GOOGLE_SEARCH = 1;
141 private static final int SHORTCUT_WIKIPEDIA_SEARCH = 2;
142 private static final int SHORTCUT_DICTIONARY_SEARCH = 3;
143 private static final int SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH = 4;
144
Cary Clarka9771242009-08-11 16:42:26 -0400145 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 @Override
147 public Void doInBackground(File... files) {
148 if (files != null) {
149 for (File f : files) {
Cary Clarkd6be1752009-08-12 12:56:42 -0400150 if (!f.delete()) {
151 Log.e(LOGTAG, f.getPath() + " was not deleted");
152 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800153 }
154 }
155 return null;
156 }
157 }
158
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400159 /**
160 * This layout holds everything you see below the status bar, including the
161 * error console, the custom view container, and the webviews.
162 */
163 private FrameLayout mBrowserFrameLayout;
Leon Scroggins81db3662009-06-04 17:45:11 -0400164
Leon Scrogginsd746a942010-05-19 13:21:44 -0400165 private boolean mXLargeScreenSize;
166
Grace Kloba22ac16e2009-10-07 18:00:23 -0700167 @Override
168 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700169 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800170 Log.v(LOGTAG, this + " onStart");
171 }
172 super.onCreate(icicle);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800173 // test the browser in OpenGL
174 // requestWindowFeature(Window.FEATURE_OPENGL);
175
Mike Reedd334bf52010-01-26 15:21:44 -0500176 // enable this to test the browser in 32bit
177 if (false) {
178 getWindow().setFormat(PixelFormat.RGBX_8888);
179 BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
180 }
181
Svetoslav Ganov2b345992010-05-06 06:13:54 -0700182 if (AccessibilityManager.getInstance(this).isEnabled()) {
183 setDefaultKeyMode(DEFAULT_KEYS_DISABLE);
184 } else {
185 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
186 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800187
188 mResolver = getContentResolver();
189
Grace Kloba0923d692009-09-23 21:37:25 -0700190 // If this was a web search request, pass it on to the default web
191 // search provider and finish this activity.
192 if (handleWebSearchIntent(getIntent())) {
193 finish();
194 return;
195 }
196
The Android Open Source Project0c908882009-03-03 19:32:16 -0800197 mSecLockIcon = Resources.getSystem().getDrawable(
198 android.R.drawable.ic_secure);
199 mMixLockIcon = Resources.getSystem().getDrawable(
200 android.R.drawable.ic_partial_secure);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800201
Leon Scroggins81db3662009-06-04 17:45:11 -0400202 FrameLayout frameLayout = (FrameLayout) getWindow().getDecorView()
203 .findViewById(com.android.internal.R.id.content);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400204 mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(this)
205 .inflate(R.layout.custom_screen, null);
206 mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(
207 R.id.main_content);
208 mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout
209 .findViewById(R.id.error_console);
210 mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
211 .findViewById(R.id.fullscreen_custom_content);
212 frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400213 mXLargeScreenSize = (getResources().getConfiguration().screenLayout
214 & Configuration.SCREENLAYOUT_SIZE_MASK)
215 == Configuration.SCREENLAYOUT_SIZE_XLARGE;
Michael Kolbe0a36662010-06-29 10:37:12 -0700216
Leon Scrogginsd746a942010-05-19 13:21:44 -0400217 if (mXLargeScreenSize) {
Leon Scroggins571b3762010-05-26 10:25:01 -0400218 mTitleBar = new TitleBarXLarge(this);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400219 LinearLayout layout = (LinearLayout) mBrowserFrameLayout.
220 findViewById(R.id.vertical_layout);
221 layout.addView(mTitleBar, 0, new LinearLayout.LayoutParams(
222 ViewGroup.LayoutParams.MATCH_PARENT,
223 ViewGroup.LayoutParams.WRAP_CONTENT));
224 } else {
Leon Scroggins571b3762010-05-26 10:25:01 -0400225 mTitleBar = new TitleBar(this);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400226 // mTitleBar will be always be shown in the fully loaded mode on
227 // phone
228 mTitleBar.setProgress(100);
229 // Fake title bar is not needed in xlarge layout
230 mFakeTitleBar = new TitleBar(this);
231 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800232
233 // Create the tab control and our initial tab
234 mTabControl = new TabControl(this);
235
236 // Open the icon database and retain all the bookmark urls for favicons
237 retainIconsOnStartup();
238
239 // Keep a settings instance handy.
240 mSettings = BrowserSettings.getInstance();
241 mSettings.setTabControl(mTabControl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800242
243 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
244 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
245
Patrick Scott6adacc92010-03-05 08:24:51 -0500246 // Find out if the network is currently up.
247 ConnectivityManager cm = (ConnectivityManager) getSystemService(
248 Context.CONNECTIVITY_SERVICE);
249 NetworkInfo info = cm.getActiveNetworkInfo();
250 if (info != null) {
251 mIsNetworkUp = info.isAvailable();
252 }
253
Grace Klobaa34f6862009-07-31 16:28:17 -0700254 /* enables registration for changes in network status from
255 http stack */
256 mNetworkStateChangedFilter = new IntentFilter();
257 mNetworkStateChangedFilter.addAction(
258 ConnectivityManager.CONNECTIVITY_ACTION);
259 mNetworkStateIntentReceiver = new BroadcastReceiver() {
260 @Override
261 public void onReceive(Context context, Intent intent) {
262 if (intent.getAction().equals(
263 ConnectivityManager.CONNECTIVITY_ACTION)) {
Andrei Popescue4c98462010-02-19 15:44:13 +0000264
265 NetworkInfo info = intent.getParcelableExtra(
266 ConnectivityManager.EXTRA_NETWORK_INFO);
267 String typeName = info.getTypeName();
268 String subtypeName = info.getSubtypeName();
269 sendNetworkType(typeName.toLowerCase(),
270 (subtypeName != null ? subtypeName.toLowerCase() : ""));
271
272 onNetworkToggle(info.isAvailable());
Grace Klobaa34f6862009-07-31 16:28:17 -0700273 }
274 }
275 };
276
Grace Kloba615c6c92009-08-03 10:22:44 -0700277 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
278 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
279 filter.addDataScheme("package");
280 mPackageInstallationReceiver = new BroadcastReceiver() {
281 @Override
282 public void onReceive(Context context, Intent intent) {
283 final String action = intent.getAction();
284 final String packageName = intent.getData()
285 .getSchemeSpecificPart();
286 final boolean replacing = intent.getBooleanExtra(
287 Intent.EXTRA_REPLACING, false);
288 if (Intent.ACTION_PACKAGE_REMOVED.equals(action) && replacing) {
289 // if it is replacing, refreshPlugins() when adding
290 return;
291 }
Andrei Popescu30995e72010-02-09 16:59:58 +0000292
293 if (sGoogleApps.contains(packageName)) {
294 BrowserActivity.this.packageChanged(packageName,
295 Intent.ACTION_PACKAGE_ADDED.equals(action));
296 }
297
Grace Kloba615c6c92009-08-03 10:22:44 -0700298 PackageManager pm = BrowserActivity.this.getPackageManager();
299 PackageInfo pkgInfo = null;
300 try {
301 pkgInfo = pm.getPackageInfo(packageName,
302 PackageManager.GET_PERMISSIONS);
303 } catch (PackageManager.NameNotFoundException e) {
304 return;
305 }
306 if (pkgInfo != null) {
307 String permissions[] = pkgInfo.requestedPermissions;
308 if (permissions == null) {
309 return;
310 }
311 boolean permissionOk = false;
312 for (String permit : permissions) {
313 if (PluginManager.PLUGIN_PERMISSION.equals(permit)) {
314 permissionOk = true;
315 break;
316 }
317 }
318 if (permissionOk) {
319 PluginManager.getInstance(BrowserActivity.this)
Grace Klobae56a0f22010-05-26 17:31:02 -0700320 .refreshPlugins(true);
Grace Kloba615c6c92009-08-03 10:22:44 -0700321 }
322 }
323 }
324 };
325 registerReceiver(mPackageInstallationReceiver, filter);
326
The Android Open Source Project0c908882009-03-03 19:32:16 -0800327 if (!mTabControl.restoreState(icicle)) {
328 // clear up the thumbnail directory if we can't restore the state as
329 // none of the files in the directory are referenced any more.
330 new ClearThumbnails().execute(
331 mTabControl.getThumbnailDir().listFiles());
Grace Klobaaab3f092009-07-30 12:29:51 -0700332 // there is no quit on Android. But if we can't restore the state,
333 // we can treat it as a new Browser, remove the old session cookies.
334 CookieManager.getInstance().removeSessionCookie();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800335 final Intent intent = getIntent();
336 final Bundle extra = intent.getExtras();
337 // Create an initial tab.
338 // If the intent is ACTION_VIEW and data is not null, the Browser is
339 // invoked to view the content by another application. In this case,
340 // the tab will be close when exit.
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700341 UrlData urlData = getUrlDataFromIntent(intent);
342
Leon Scroggins58d56c62010-01-28 15:12:40 -0500343 String action = intent.getAction();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700344 final Tab t = mTabControl.createNewTab(
Leon Scroggins58d56c62010-01-28 15:12:40 -0500345 (Intent.ACTION_VIEW.equals(action) &&
346 intent.getData() != null)
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500347 || RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
348 .equals(action),
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700349 intent.getStringExtra(Browser.EXTRA_APPLICATION_ID), urlData.mUrl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800350 mTabControl.setCurrentTab(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800351 attachTabToContentView(t);
352 WebView webView = t.getWebView();
353 if (extra != null) {
354 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
355 if (scale > 0 && scale <= 1000) {
356 webView.setInitialScale(scale);
357 }
358 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800359
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700360 if (urlData.isEmpty()) {
Shimeng (Simon) Wang98d5fce2010-03-16 13:23:39 -0700361 loadUrl(webView, mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800362 } else {
Patrick Scott9d53da02010-02-19 10:19:01 -0500363 loadUrlDataIn(t, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800364 }
365 } else {
366 // TabControl.restoreState() will create a new tab even if
Leon Scroggins1f005d32009-08-10 17:36:42 -0400367 // restoring the state fails.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800368 attachTabToContentView(mTabControl.getCurrentTab());
369 }
Grace Kloba615c6c92009-08-03 10:22:44 -0700370
Feng Qianb3c02da2009-06-29 15:58:08 -0700371 // Read JavaScript flags if it exists.
372 String jsFlags = mSettings.getJsFlags();
373 if (jsFlags.trim().length() != 0) {
374 mTabControl.getCurrentWebView().setJsFlags(jsFlags);
375 }
Andrei Popescu30995e72010-02-09 16:59:58 +0000376 // Work out which packages are installed on the system.
377 getInstalledPackages();
Bjorn Bringerta7611812010-03-24 11:12:02 +0000378
379 // Start watching the default geolocation permissions
380 mSystemAllowGeolocationOrigins
381 = new SystemAllowGeolocationOrigins(getApplicationContext());
382 mSystemAllowGeolocationOrigins.start();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800383 }
384
Leon Scroggins58d56c62010-01-28 15:12:40 -0500385 /**
386 * Feed the previously stored results strings to the BrowserProvider so that
387 * the SearchDialog will show them instead of the standard searches.
388 * @param result String to show on the editable line of the SearchDialog.
389 */
390 /* package */ void showVoiceSearchResults(String result) {
391 ContentProviderClient client = mResolver.acquireContentProviderClient(
392 Browser.BOOKMARKS_URI);
393 ContentProvider prov = client.getLocalContentProvider();
394 BrowserProvider bp = (BrowserProvider) prov;
395 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
396 client.release();
397
Leon Scrogginsfbb3f152010-03-09 17:26:56 -0500398 Bundle bundle = createGoogleSearchSourceBundle(
399 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
400 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
401 startSearch(result, false, bundle, false);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500402 }
403
The Android Open Source Project0c908882009-03-03 19:32:16 -0800404 @Override
405 protected void onNewIntent(Intent intent) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700406 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800407 // When a tab is closed on exit, the current tab index is set to -1.
408 // Reset before proceed as Browser requires the current tab to be set.
409 if (current == null) {
410 // Try to reset the tab in case the index was incorrect.
411 current = mTabControl.getTab(0);
412 if (current == null) {
413 // No tabs at all so just ignore this intent.
414 return;
415 }
416 mTabControl.setCurrentTab(current);
417 attachTabToContentView(current);
418 resetTitleAndIcon(current.getWebView());
419 }
420 final String action = intent.getAction();
421 final int flags = intent.getFlags();
422 if (Intent.ACTION_MAIN.equals(action) ||
423 (flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
424 // just resume the browser
425 return;
426 }
Leon Scrogginsb8a844d2010-03-18 15:06:15 -0400427 // In case the SearchDialog is open.
428 ((SearchManager) getSystemService(Context.SEARCH_SERVICE))
429 .stopSearch();
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500430 boolean activateVoiceSearch = RecognizerResultsIntent
431 .ACTION_VOICE_SEARCH_RESULTS.equals(action);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800432 if (Intent.ACTION_VIEW.equals(action)
433 || Intent.ACTION_SEARCH.equals(action)
434 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
Leon Scroggins58d56c62010-01-28 15:12:40 -0500435 || Intent.ACTION_WEB_SEARCH.equals(action)
436 || activateVoiceSearch) {
Leon Scroggins3b18ce32010-02-08 17:35:59 -0500437 if (current.isInVoiceSearchMode()) {
438 String title = current.getVoiceDisplayTitle();
439 if (title != null && title.equals(intent.getStringExtra(
440 SearchManager.QUERY))) {
441 // The user submitted the same search as the last voice
442 // search, so do nothing.
443 return;
444 }
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500445 if (Intent.ACTION_SEARCH.equals(action)
446 && current.voiceSearchSourceIsGoogle()) {
447 Intent logIntent = new Intent(
448 LoggingEvents.ACTION_LOG_EVENT);
449 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
450 LoggingEvents.VoiceSearch.QUERY_UPDATED);
451 logIntent.putExtra(
452 LoggingEvents.VoiceSearch.EXTRA_QUERY_UPDATED_VALUE,
453 intent.getDataString());
454 sendBroadcast(logIntent);
455 // Note, onPageStarted will revert the voice title bar
456 // When http://b/issue?id=2379215 is fixed, we should update
457 // the title bar here.
458 }
Leon Scroggins3b18ce32010-02-08 17:35:59 -0500459 }
Satish Sampath565505b2009-05-29 15:37:27 +0100460 // If this was a search request (e.g. search query directly typed into the address bar),
461 // pass it on to the default web search provider.
462 if (handleWebSearchIntent(intent)) {
463 return;
464 }
465
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700466 UrlData urlData = getUrlDataFromIntent(intent);
467 if (urlData.isEmpty()) {
468 urlData = new UrlData(mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800469 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700470
Grace Klobacc634032009-07-28 15:58:19 -0700471 final String appId = intent
472 .getStringExtra(Browser.EXTRA_APPLICATION_ID);
Leon Scroggins47208682010-04-07 17:59:48 -0400473 if ((Intent.ACTION_VIEW.equals(action)
474 // If a voice search has no appId, it means that it came
475 // from the browser. In that case, reuse the current tab.
476 || (activateVoiceSearch && appId != null))
Grace Klobacc634032009-07-28 15:58:19 -0700477 && !getPackageName().equals(appId)
478 && (flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700479 Tab appTab = mTabControl.getTabFromId(appId);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700480 if (appTab != null) {
481 Log.i(LOGTAG, "Reusing tab for " + appId);
482 // Dismiss the subwindow if applicable.
483 dismissSubWindow(appTab);
484 // Since we might kill the WebView, remove it from the
485 // content view first.
486 removeTabFromContentView(appTab);
487 // Recreate the main WebView after destroying the old one.
488 // If the WebView has the same original url and is on that
489 // page, it can be reused.
490 boolean needsLoad =
Leon Scroggins6eac63e2010-03-15 18:19:14 -0400491 mTabControl.recreateWebView(appTab, urlData);
Ben Murdochbff2d602009-07-01 20:19:05 +0100492
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700493 if (current != appTab) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400494 switchToTab(mTabControl.getTabIndex(appTab));
495 if (needsLoad) {
Patrick Scott9d53da02010-02-19 10:19:01 -0500496 loadUrlDataIn(appTab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400497 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700498 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400499 // If the tab was the current tab, we have to attach
500 // it to the view system again.
501 attachTabToContentView(appTab);
502 if (needsLoad) {
Patrick Scott9d53da02010-02-19 10:19:01 -0500503 loadUrlDataIn(appTab, urlData);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700504 }
505 }
506 return;
Patrick Scottcd115892009-07-16 09:42:58 -0400507 } else {
508 // No matching application tab, try to find a regular tab
509 // with a matching url.
510 appTab = mTabControl.findUnusedTabWithUrl(urlData.mUrl);
Leon Scroggins25515f82009-08-19 15:31:58 -0400511 if (appTab != null) {
512 if (current != appTab) {
513 switchToTab(mTabControl.getTabIndex(appTab));
514 }
515 // Otherwise, we are already viewing the correct tab.
Patrick Scottcd115892009-07-16 09:42:58 -0400516 } else {
517 // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
518 // will be opened in a new tab unless we have reached
519 // MAX_TABS. Then the url will be opened in the current
520 // tab. If a new tab is created, it will have "true" for
521 // exit on close.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400522 openTabAndShow(urlData, true, appId);
Patrick Scottcd115892009-07-16 09:42:58 -0400523 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700524 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800525 } else {
Grace Kloba638d3f42009-11-23 10:35:04 -0800526 if (!urlData.isEmpty()
527 && urlData.mUrl.startsWith("about:debug")) {
528 if ("about:debug.dom".equals(urlData.mUrl)) {
529 current.getWebView().dumpDomTree(false);
530 } else if ("about:debug.dom.file".equals(urlData.mUrl)) {
531 current.getWebView().dumpDomTree(true);
532 } else if ("about:debug.render".equals(urlData.mUrl)) {
533 current.getWebView().dumpRenderTree(false);
534 } else if ("about:debug.render.file".equals(urlData.mUrl)) {
535 current.getWebView().dumpRenderTree(true);
536 } else if ("about:debug.display".equals(urlData.mUrl)) {
537 current.getWebView().dumpDisplayTree();
Mike Reed9b78e1d2010-01-13 14:40:52 -0800538 } else if (urlData.mUrl.startsWith("about:debug.drag")) {
539 int index = urlData.mUrl.codePointAt(16) - '0';
540 if (index <= 0 || index > 9) {
541 current.getWebView().setDragTracker(null);
542 } else {
543 current.getWebView().setDragTracker(new MeshTracker(index));
544 }
Grace Kloba638d3f42009-11-23 10:35:04 -0800545 } else {
546 mSettings.toggleDebugSettings();
547 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800548 return;
549 }
Leon Scroggins1f005d32009-08-10 17:36:42 -0400550 // Get rid of the subwindow if it exists
551 dismissSubWindow(current);
Leon Scroggins8588d152010-04-15 11:01:53 -0400552 // If the current Tab is being used as an application tab,
553 // remove the association, since the new Intent means that it is
554 // no longer associated with that application.
555 current.setAppId(null);
Patrick Scott9d53da02010-02-19 10:19:01 -0500556 loadUrlDataIn(current, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800557 }
558 }
559 }
560
Satish Sampath565505b2009-05-29 15:37:27 +0100561 private int parseUrlShortcut(String url) {
562 if (url == null) return SHORTCUT_INVALID;
563
564 // FIXME: quick search, need to be customized by setting
565 if (url.length() > 2 && url.charAt(1) == ' ') {
566 switch (url.charAt(0)) {
567 case 'g': return SHORTCUT_GOOGLE_SEARCH;
568 case 'w': return SHORTCUT_WIKIPEDIA_SEARCH;
569 case 'd': return SHORTCUT_DICTIONARY_SEARCH;
570 case 'l': return SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH;
571 }
572 }
573 return SHORTCUT_INVALID;
574 }
575
576 /**
577 * Launches the default web search activity with the query parameters if the given intent's data
578 * are identified as plain search terms and not URLs/shortcuts.
579 * @return true if the intent was handled and web search activity was launched, false if not.
580 */
581 private boolean handleWebSearchIntent(Intent intent) {
582 if (intent == null) return false;
583
584 String url = null;
585 final String action = intent.getAction();
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500586 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS.equals(
587 action)) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500588 return false;
589 }
Satish Sampath565505b2009-05-29 15:37:27 +0100590 if (Intent.ACTION_VIEW.equals(action)) {
Grace Kloba1e705052009-09-29 13:13:36 -0700591 Uri data = intent.getData();
592 if (data != null) url = data.toString();
Satish Sampath565505b2009-05-29 15:37:27 +0100593 } else if (Intent.ACTION_SEARCH.equals(action)
594 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
595 || Intent.ACTION_WEB_SEARCH.equals(action)) {
596 url = intent.getStringExtra(SearchManager.QUERY);
597 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100598 return handleWebSearchRequest(url, intent.getBundleExtra(SearchManager.APP_DATA),
599 intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
Satish Sampath565505b2009-05-29 15:37:27 +0100600 }
601
602 /**
603 * Launches the default web search activity with the query parameters if the given url string
604 * was identified as plain search terms and not URL/shortcut.
605 * @return true if the request was handled and web search activity was launched, false if not.
606 */
Bjorn Bringert04851702009-09-22 10:36:01 +0100607 private boolean handleWebSearchRequest(String inUrl, Bundle appData, String extraData) {
Satish Sampath565505b2009-05-29 15:37:27 +0100608 if (inUrl == null) return false;
609
610 // In general, we shouldn't modify URL from Intent.
611 // But currently, we get the user-typed URL from search box as well.
612 String url = fixUrl(inUrl).trim();
613
614 // URLs and site specific search shortcuts are handled by the regular flow of control, so
615 // return early.
Dan Egnor5ee906c2009-11-18 12:11:49 -0800616 if (Patterns.WEB_URL.matcher(url).matches()
Satish Sampathbc5b9f32009-06-04 18:21:40 +0100617 || ACCEPTED_URI_SCHEMA.matcher(url).matches()
Satish Sampath565505b2009-05-29 15:37:27 +0100618 || parseUrlShortcut(url) != SHORTCUT_INVALID) {
619 return false;
620 }
621
Leon Scroggins8d06e362010-03-24 14:45:57 -0400622 final ContentResolver cr = mResolver;
623 final String newUrl = url;
624 new AsyncTask<Void, Void, Void>() {
Michael Kolbe0a36662010-06-29 10:37:12 -0700625 @Override
Leon Scroggins8d06e362010-03-24 14:45:57 -0400626 protected Void doInBackground(Void... unused) {
627 Browser.updateVisitedHistory(cr, newUrl, false);
628 Browser.addSearchUrl(cr, newUrl);
629 return null;
630 }
631 }.execute();
Satish Sampath565505b2009-05-29 15:37:27 +0100632
633 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
634 intent.addCategory(Intent.CATEGORY_DEFAULT);
635 intent.putExtra(SearchManager.QUERY, url);
Satish Sampath15e9f2d2009-06-23 22:29:49 +0100636 if (appData != null) {
637 intent.putExtra(SearchManager.APP_DATA, appData);
638 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100639 if (extraData != null) {
640 intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
641 }
Grace Klobacc634032009-07-28 15:58:19 -0700642 intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName());
Satish Sampath565505b2009-05-29 15:37:27 +0100643 startActivity(intent);
644
645 return true;
646 }
647
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700648 private UrlData getUrlDataFromIntent(Intent intent) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500649 String url = "";
Grace Kloba068e48b2010-01-26 18:11:27 -0800650 Map<String, String> headers = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800651 if (intent != null) {
652 final String action = intent.getAction();
653 if (Intent.ACTION_VIEW.equals(action)) {
654 url = smartUrlFilter(intent.getData());
655 if (url != null && url.startsWith("content:")) {
656 /* Append mimetype so webview knows how to display */
657 String mimeType = intent.resolveType(getContentResolver());
658 if (mimeType != null) {
659 url += "?" + mimeType;
660 }
661 }
Grace Kloba068e48b2010-01-26 18:11:27 -0800662 if (url != null && url.startsWith("http")) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800663 final Bundle pairs = intent
664 .getBundleExtra(Browser.EXTRA_HEADERS);
Grace Kloba2d508ed2010-01-28 11:39:15 -0800665 if (pairs != null && !pairs.isEmpty()) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800666 Iterator<String> iter = pairs.keySet().iterator();
Grace Kloba068e48b2010-01-26 18:11:27 -0800667 headers = new HashMap<String, String>();
Grace Kloba00f54c52010-01-27 14:53:51 -0800668 while (iter.hasNext()) {
669 String key = iter.next();
670 headers.put(key, pairs.getString(key));
Grace Kloba068e48b2010-01-26 18:11:27 -0800671 }
672 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700673 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674 } else if (Intent.ACTION_SEARCH.equals(action)
675 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
676 || Intent.ACTION_WEB_SEARCH.equals(action)) {
677 url = intent.getStringExtra(SearchManager.QUERY);
678 if (url != null) {
679 mLastEnteredUrl = url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800680 // In general, we shouldn't modify URL from Intent.
681 // But currently, we get the user-typed URL from search box as well.
682 url = fixUrl(url);
683 url = smartUrlFilter(url);
Leon Scroggins8d06e362010-03-24 14:45:57 -0400684 final ContentResolver cr = mResolver;
685 final String newUrl = url;
686 new AsyncTask<Void, Void, Void>() {
Michael Kolbe0a36662010-06-29 10:37:12 -0700687 @Override
Leon Scroggins8d06e362010-03-24 14:45:57 -0400688 protected Void doInBackground(Void... unused) {
689 Browser.updateVisitedHistory(cr, newUrl, false);
690 return null;
691 }
692 }.execute();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800693 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
694 if (url.contains(searchSource)) {
695 String source = null;
696 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
697 if (appData != null) {
Bjorn Bringert10d1cca2010-02-10 14:22:12 +0000698 source = appData.getString(Search.SOURCE);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800699 }
700 if (TextUtils.isEmpty(source)) {
701 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
702 }
703 url = url.replace(searchSource, "&source=android-"+source+"&");
704 }
705 }
706 }
707 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500708 return new UrlData(url, headers, intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800709 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500710 /* package */ void showVoiceTitleBar(String title) {
711 mTitleBar.setInVoiceMode(true);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500712 mTitleBar.setDisplayTitle(title);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400713
714 if (!mXLargeScreenSize) {
715 mFakeTitleBar.setInVoiceMode(true);
716 mFakeTitleBar.setDisplayTitle(title);
717 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500718 }
719 /* package */ void revertVoiceTitleBar() {
720 mTitleBar.setInVoiceMode(false);
Leon Scroggins003a5dd2010-03-10 12:13:14 -0500721 mTitleBar.setDisplayTitle(mUrl);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400722
723 if (!mXLargeScreenSize) {
724 mFakeTitleBar.setInVoiceMode(false);
725 mFakeTitleBar.setDisplayTitle(mUrl);
726 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500727 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800728 /* package */ static String fixUrl(String inUrl) {
Cary Clark652ff872009-09-10 13:34:44 -0400729 // FIXME: Converting the url to lower case
730 // duplicates functionality in smartUrlFilter().
731 // However, changing all current callers of fixUrl to
732 // call smartUrlFilter in addition may have unwanted
733 // consequences, and is deferred for now.
734 int colon = inUrl.indexOf(':');
735 boolean allLower = true;
736 for (int index = 0; index < colon; index++) {
737 char ch = inUrl.charAt(index);
738 if (!Character.isLetter(ch)) {
739 break;
740 }
741 allLower &= Character.isLowerCase(ch);
742 if (index == colon - 1 && !allLower) {
743 inUrl = inUrl.substring(0, colon).toLowerCase()
744 + inUrl.substring(colon);
745 }
746 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800747 if (inUrl.startsWith("http://") || inUrl.startsWith("https://"))
748 return inUrl;
749 if (inUrl.startsWith("http:") ||
750 inUrl.startsWith("https:")) {
751 if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) {
752 inUrl = inUrl.replaceFirst("/", "//");
753 } else inUrl = inUrl.replaceFirst(":", "://");
754 }
755 return inUrl;
756 }
757
Grace Kloba22ac16e2009-10-07 18:00:23 -0700758 @Override
759 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800760 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700761 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800762 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
763 }
764
765 if (!mActivityInPause) {
766 Log.e(LOGTAG, "BrowserActivity is already resumed.");
767 return;
768 }
769
Mike Reed7bfa63b2009-05-28 11:08:32 -0400770 mTabControl.resumeCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800771 mActivityInPause = false;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400772 resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800773
774 if (mWakeLock.isHeld()) {
775 mHandler.removeMessages(RELEASE_WAKELOCK);
776 mWakeLock.release();
777 }
778
The Android Open Source Project0c908882009-03-03 19:32:16 -0800779 registerReceiver(mNetworkStateIntentReceiver,
780 mNetworkStateChangedFilter);
781 WebView.enablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800782 }
783
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400784 /**
785 * Since the actual title bar is embedded in the WebView, and removing it
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400786 * would change its appearance, use a different TitleBar to show overlayed
787 * at the top of the screen, when the menu is open or the page is loading.
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400788 */
789 private TitleBar mFakeTitleBar;
790
791 /**
792 * Keeps track of whether the options menu is open. This is important in
793 * determining whether to show or hide the title bar overlay.
794 */
795 private boolean mOptionsMenuOpen;
796
797 /**
798 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
799 * of whether the configuration has changed. The first onMenuOpened call
800 * after a configuration change is simply a reopening of the same menu
801 * (i.e. mIconView did not change).
802 */
803 private boolean mConfigChanged;
804
805 /**
806 * Whether or not the options menu is in its smaller, icon menu form. When
807 * true, we want the title bar overlay to be up. When false, we do not.
808 * Only meaningful if mOptionsMenuOpen is true.
809 */
810 private boolean mIconView;
811
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400812 @Override
813 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400814 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
815 if (mOptionsMenuOpen) {
816 if (mConfigChanged) {
817 // We do not need to make any changes to the state of the
818 // title bar, since the only thing that happened was a
819 // change in orientation
820 mConfigChanged = false;
821 } else {
822 if (mIconView) {
823 // Switching the menu to expanded view, so hide the
824 // title bar.
825 hideFakeTitleBar();
826 mIconView = false;
827 } else {
828 // Switching the menu back to icon view, so show the
829 // title bar once again.
830 showFakeTitleBar();
831 mIconView = true;
832 }
833 }
834 } else {
835 // The options menu is closed, so open it, and show the title
836 showFakeTitleBar();
837 mOptionsMenuOpen = true;
838 mConfigChanged = false;
839 mIconView = true;
840 }
841 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400842 return true;
843 }
844
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400845 private void showFakeTitleBar() {
Leon Scrogginsd746a942010-05-19 13:21:44 -0400846 if (mXLargeScreenSize) return;
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400847 if (mFakeTitleBar.getParent() == null && mActiveTabsPage == null
Grace Kloba847c25b2010-03-30 16:00:26 -0700848 && !mActivityInPause) {
849 WebView mainView = mTabControl.getCurrentWebView();
850 // if there is no current WebView, don't show the faked title bar;
Grace Kloba65190702010-04-02 23:37:26 -0700851 if (mainView == null) {
Cary Clarka0464552009-09-29 13:00:45 -0400852 return;
853 }
Leon Scroggins79e36d92010-04-29 16:01:46 +0100854 // Do not need to check for null, since the current tab will have
855 // at least a main WebView, or we would have returned above.
Cary Clark01cfcdd2010-06-04 16:36:45 -0400856 if (dialogIsUp()) {
Leon Scroggins79e36d92010-04-29 16:01:46 +0100857 // Do not show the fake title bar, which would cover up the
Cary Clark01cfcdd2010-06-04 16:36:45 -0400858 // find or select dialog.
Leon Scroggins79e36d92010-04-29 16:01:46 +0100859 return;
860 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400861
862 WindowManager manager
863 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
864
865 // Add the title bar to the window manager so it can receive touches
866 // while the menu is up
867 WindowManager.LayoutParams params
868 = new WindowManager.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -0800869 ViewGroup.LayoutParams.MATCH_PARENT,
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400870 ViewGroup.LayoutParams.WRAP_CONTENT,
Grace Kloba847c25b2010-03-30 16:00:26 -0700871 WindowManager.LayoutParams.TYPE_APPLICATION,
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400872 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
Leon Scroggins68549862009-09-21 16:02:01 -0400873 PixelFormat.TRANSLUCENT);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400874 params.gravity = Gravity.TOP;
Grace Kloba847c25b2010-03-30 16:00:26 -0700875 boolean atTop = mainView.getScrollY() == 0;
Leon Scroggins83932c72009-09-30 11:55:54 -0400876 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
Grace Kloba13f9ace2010-03-18 21:44:14 -0700877 manager.addView(mFakeTitleBar, params);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400878 }
879 }
880
881 @Override
882 public void onOptionsMenuClosed(Menu menu) {
883 mOptionsMenuOpen = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -0400884 if (!mInLoad) {
885 hideFakeTitleBar();
886 } else if (!mIconView) {
887 // The page is currently loading, and we are in expanded mode, so
888 // we were not showing the menu. Show it once again. It will be
889 // removed when the page finishes.
890 showFakeTitleBar();
891 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400892 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700893
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400894 private void hideFakeTitleBar() {
Leon Scrogginsd746a942010-05-19 13:21:44 -0400895 if (mXLargeScreenSize || mFakeTitleBar.getParent() == null) return;
Leon Scroggins20329572009-09-23 17:42:41 -0400896 WindowManager.LayoutParams params = (WindowManager.LayoutParams)
Grace Kloba13f9ace2010-03-18 21:44:14 -0700897 mFakeTitleBar.getLayoutParams();
Leon Scroggins20329572009-09-23 17:42:41 -0400898 WebView mainView = mTabControl.getCurrentWebView();
899 // Although we decided whether or not to animate based on the current
900 // scroll position, the scroll position may have changed since the
901 // fake title bar was displayed. Make sure it has the appropriate
902 // animation/lack thereof before removing.
903 params.windowAnimations = mainView != null && mainView.getScrollY() == 0
Leon Scroggins83932c72009-09-30 11:55:54 -0400904 ? 0 : R.style.TitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400905 WindowManager manager
906 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Grace Kloba13f9ace2010-03-18 21:44:14 -0700907 manager.updateViewLayout(mFakeTitleBar, params);
908 manager.removeView(mFakeTitleBar);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400909 }
910
The Android Open Source Project0c908882009-03-03 19:32:16 -0800911 /**
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400912 * Special method for the fake title bar to call when displaying its context
913 * menu, since it is in its own Window, and its parent does not show a
914 * context menu.
915 */
916 /* package */ void showTitleBarContextMenu() {
Cary Clark65f4a3c2009-09-28 17:05:06 -0400917 if (null == mTitleBar.getParent()) {
918 return;
919 }
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400920 openContextMenu(mTitleBar);
921 }
922
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400923 @Override
924 public void onContextMenuClosed(Menu menu) {
925 super.onContextMenuClosed(menu);
926 if (mInLoad) {
927 showFakeTitleBar();
928 }
929 }
930
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400931 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800932 * onSaveInstanceState(Bundle map)
933 * onSaveInstanceState is called right before onStop(). The map contains
934 * the saved state.
935 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700936 @Override
937 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700938 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800939 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
940 }
941 // the default implementation requires each view to have an id. As the
942 // browser handles the state itself and it doesn't use id for the views,
943 // don't call the default implementation. Otherwise it will trigger the
944 // warning like this, "couldn't save which view has focus because the
945 // focused view XXX has no id".
946
947 // Save all the tabs
948 mTabControl.saveState(outState);
949 }
950
Grace Kloba22ac16e2009-10-07 18:00:23 -0700951 @Override
952 protected void onPause() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800953 super.onPause();
954
955 if (mActivityInPause) {
956 Log.e(LOGTAG, "BrowserActivity is already paused.");
957 return;
958 }
959
Mike Reed7bfa63b2009-05-28 11:08:32 -0400960 mTabControl.pauseCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800961 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400962 if (mTabControl.getCurrentIndex() >= 0 && !pauseWebViewTimers()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800963 mWakeLock.acquire();
964 mHandler.sendMessageDelayed(mHandler
965 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
966 }
967
Leon Scrogginsa2ab6a72009-09-11 11:49:52 -0400968 // FIXME: This removes the active tabs page and resets the menu to
969 // MAIN_MENU. A better solution might be to do this work in onNewIntent
970 // but then we would need to save it in onSaveInstanceState and restore
971 // it in onCreate/onRestoreInstanceState
972 if (mActiveTabsPage != null) {
973 removeActiveTabPage(true);
974 }
975
The Android Open Source Project0c908882009-03-03 19:32:16 -0800976 cancelStopToast();
977
978 // unregister network state listener
979 unregisterReceiver(mNetworkStateIntentReceiver);
980 WebView.disablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800981 }
982
Grace Kloba22ac16e2009-10-07 18:00:23 -0700983 @Override
984 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700985 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800986 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
987 }
988 super.onDestroy();
Grace Kloba0923d692009-09-23 21:37:25 -0700989
Leon Scroggins8d5fa432009-10-02 15:55:59 -0400990 if (mUploadMessage != null) {
991 mUploadMessage.onReceiveValue(null);
992 mUploadMessage = null;
993 }
994
Grace Kloba0923d692009-09-23 21:37:25 -0700995 if (mTabControl == null) return;
996
Grace Kloba1fc98a32009-10-21 13:23:08 -0700997 // Remove the fake title bar if it is there
998 hideFakeTitleBar();
999
The Android Open Source Project0c908882009-03-03 19:32:16 -08001000 // Remove the current tab and sub window
Grace Kloba22ac16e2009-10-07 18:00:23 -07001001 Tab t = mTabControl.getCurrentTab();
Patrick Scottfb5e77f2009-04-08 19:17:37 -07001002 if (t != null) {
1003 dismissSubWindow(t);
1004 removeTabFromContentView(t);
1005 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001006 // Destroy all the tabs
1007 mTabControl.destroy();
1008 WebIconDatabase.getInstance().close();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001009
Grace Klobab4da0ad2009-05-14 14:45:40 -07001010 unregisterReceiver(mPackageInstallationReceiver);
Bjorn Bringerta7611812010-03-24 11:12:02 +00001011
1012 // Stop watching the default geolocation permissions
1013 mSystemAllowGeolocationOrigins.stop();
1014 mSystemAllowGeolocationOrigins = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001015 }
1016
1017 @Override
1018 public void onConfigurationChanged(Configuration newConfig) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04001019 mConfigChanged = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001020 super.onConfigurationChanged(newConfig);
1021
1022 if (mPageInfoDialog != null) {
1023 mPageInfoDialog.dismiss();
1024 showPageInfo(
1025 mPageInfoView,
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05001026 mPageInfoFromShowSSLCertificateOnError);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001027 }
1028 if (mSSLCertificateDialog != null) {
1029 mSSLCertificateDialog.dismiss();
1030 showSSLCertificate(
1031 mSSLCertificateView);
1032 }
1033 if (mSSLCertificateOnErrorDialog != null) {
1034 mSSLCertificateOnErrorDialog.dismiss();
1035 showSSLCertificateOnError(
1036 mSSLCertificateOnErrorView,
1037 mSSLCertificateOnErrorHandler,
1038 mSSLCertificateOnErrorError);
1039 }
1040 if (mHttpAuthenticationDialog != null) {
1041 String title = ((TextView) mHttpAuthenticationDialog
1042 .findViewById(com.android.internal.R.id.alertTitle)).getText()
1043 .toString();
1044 String name = ((TextView) mHttpAuthenticationDialog
1045 .findViewById(R.id.username_edit)).getText().toString();
1046 String password = ((TextView) mHttpAuthenticationDialog
1047 .findViewById(R.id.password_edit)).getText().toString();
1048 int focusId = mHttpAuthenticationDialog.getCurrentFocus()
1049 .getId();
1050 mHttpAuthenticationDialog.dismiss();
1051 showHttpAuthentication(mHttpAuthHandler, null, null, title,
1052 name, password, focusId);
1053 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001054 }
1055
Grace Kloba22ac16e2009-10-07 18:00:23 -07001056 @Override
1057 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001058 super.onLowMemory();
1059 mTabControl.freeMemory();
1060 }
1061
Cary Clarkff4d92c2010-03-25 11:17:03 -04001062 private void resumeWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001063 Tab tab = mTabControl.getCurrentTab();
Cary Clarkff4d92c2010-03-25 11:17:03 -04001064 if (tab == null) return; // monkey can trigger this
Grace Kloba22ac16e2009-10-07 18:00:23 -07001065 boolean inLoad = tab.inLoad();
1066 if ((!mActivityInPause && !inLoad) || (mActivityInPause && inLoad)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001067 CookieSyncManager.getInstance().startSync();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001068 WebView w = tab.getWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001069 if (w != null) {
1070 w.resumeTimers();
1071 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001072 }
1073 }
1074
Mike Reed7bfa63b2009-05-28 11:08:32 -04001075 private boolean pauseWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001076 Tab tab = mTabControl.getCurrentTab();
1077 boolean inLoad = tab.inLoad();
1078 if (mActivityInPause && !inLoad) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001079 CookieSyncManager.getInstance().stopSync();
1080 WebView w = mTabControl.getCurrentWebView();
1081 if (w != null) {
1082 w.pauseTimers();
1083 }
1084 return true;
1085 } else {
1086 return false;
1087 }
1088 }
1089
The Android Open Source Project0c908882009-03-03 19:32:16 -08001090 // Open the icon database and retain all the icons for visited sites.
1091 private void retainIconsOnStartup() {
1092 final WebIconDatabase db = WebIconDatabase.getInstance();
1093 db.open(getDir("icons", 0).getPath());
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001094 Cursor c = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001095 try {
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001096 c = Browser.getAllBookmarks(mResolver);
1097 if (c.moveToFirst()) {
1098 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
1099 do {
1100 String url = c.getString(urlIndex);
1101 db.retainIconForPageUrl(url);
1102 } while (c.moveToNext());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001103 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001104 } catch (IllegalStateException e) {
1105 Log.e(LOGTAG, "retainIconsOnStartup", e);
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001106 } finally {
1107 if (c!= null) c.close();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001108 }
1109 }
1110
1111 // Helper method for getting the top window.
1112 WebView getTopWindow() {
1113 return mTabControl.getCurrentTopWebView();
1114 }
1115
Grace Kloba22ac16e2009-10-07 18:00:23 -07001116 TabControl getTabControl() {
1117 return mTabControl;
1118 }
1119
The Android Open Source Project0c908882009-03-03 19:32:16 -08001120 @Override
1121 public boolean onCreateOptionsMenu(Menu menu) {
1122 super.onCreateOptionsMenu(menu);
1123
1124 MenuInflater inflater = getMenuInflater();
1125 inflater.inflate(R.menu.browser, menu);
1126 mMenu = menu;
1127 updateInLoadMenuItems();
1128 return true;
1129 }
1130
1131 /**
1132 * As the menu can be open when loading state changes
1133 * we must manually update the state of the stop/reload menu
1134 * item
1135 */
1136 private void updateInLoadMenuItems() {
1137 if (mMenu == null) {
1138 return;
1139 }
Michael Kolbe0a36662010-06-29 10:37:12 -07001140 MenuItem dest = mMenu.findItem(R.id.stop_reload_menu_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001141 MenuItem src = mInLoad ?
1142 mMenu.findItem(R.id.stop_menu_id):
Michael Kolbe0a36662010-06-29 10:37:12 -07001143 mMenu.findItem(R.id.reload_menu_id);
1144 if (src != null) {
1145 dest.setIcon(src.getIcon());
1146 dest.setTitle(src.getTitle());
1147 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001148 }
1149
1150 @Override
1151 public boolean onContextItemSelected(MenuItem item) {
1152 // chording is not an issue with context menus, but we use the same
1153 // options selector, so set mCanChord to true so we can access them.
1154 mCanChord = true;
1155 int id = item.getItemId();
Leon Scroggins96afcb12009-12-10 12:35:56 -05001156 boolean result = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001157 switch (id) {
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001158 // For the context menu from the title bar
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001159 case R.id.title_bar_copy_page_url:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001160 Tab currentTab = mTabControl.getCurrentTab();
1161 if (null == currentTab) {
1162 result = false;
1163 break;
1164 }
1165 WebView mainView = currentTab.getWebView();
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001166 if (null == mainView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001167 result = false;
1168 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001169 }
Leon Scroggins96afcb12009-12-10 12:35:56 -05001170 copy(mainView.getUrl());
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001171 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001172 // -- Browser context menu
1173 case R.id.open_context_menu_id:
The Android Open Source Project0c908882009-03-03 19:32:16 -08001174 case R.id.bookmark_context_menu_id:
1175 case R.id.save_link_context_menu_id:
1176 case R.id.share_link_context_menu_id:
1177 case R.id.copy_link_context_menu_id:
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001178 final WebView webView = getTopWindow();
1179 if (null == webView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001180 result = false;
1181 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001182 }
1183 final HashMap hrefMap = new HashMap();
1184 hrefMap.put("webview", webView);
1185 final Message msg = mHandler.obtainMessage(
1186 FOCUS_NODE_HREF, id, 0, hrefMap);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001187 webView.requestFocusNodeHref(msg);
1188 break;
1189
1190 default:
1191 // For other context menus
Leon Scroggins96afcb12009-12-10 12:35:56 -05001192 result = onOptionsItemSelected(item);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001193 }
1194 mCanChord = false;
Leon Scroggins96afcb12009-12-10 12:35:56 -05001195 return result;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001196 }
1197
1198 private Bundle createGoogleSearchSourceBundle(String source) {
1199 Bundle bundle = new Bundle();
Bjorn Bringert10d1cca2010-02-10 14:22:12 +00001200 bundle.putString(Search.SOURCE, source);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001201 return bundle;
1202 }
1203
Leon Scroggins8ad29922010-02-16 12:33:55 -05001204 /* package */ void editUrl() {
Leon Scroggins68579392009-09-15 15:31:54 -04001205 if (mOptionsMenuOpen) closeOptionsMenu();
Leon Scroggins5bbe9802009-07-31 13:10:55 -04001206 String url = (getTopWindow() == null) ? null : getTopWindow().getUrl();
Grace Kloba83f47342009-07-20 10:44:31 -07001207 startSearch(mSettings.getHomePage().equals(url) ? null : url, true,
Leon Scroggins8ad29922010-02-16 12:33:55 -05001208 null, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001209 }
1210
Leon Scroggins8ad29922010-02-16 12:33:55 -05001211 /**
1212 * Overriding this to insert a local information bundle
1213 */
The Android Open Source Project0c908882009-03-03 19:32:16 -08001214 @Override
1215 public void startSearch(String initialQuery, boolean selectInitialQuery,
1216 Bundle appSearchData, boolean globalSearch) {
1217 if (appSearchData == null) {
1218 appSearchData = createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_TYPE);
1219 }
1220 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1221 }
1222
Leon Scroggins1f005d32009-08-10 17:36:42 -04001223 /**
1224 * Switch tabs. Called by the TitleBarSet when sliding the title bar
1225 * results in changing tabs.
Leon Scroggins160a7e72009-08-14 18:28:01 -04001226 * @param index Index of the tab to change to, as defined by
1227 * mTabControl.getTabIndex(Tab t).
1228 * @return boolean True if we successfully switched to a different tab. If
1229 * the indexth tab is null, or if that tab is the same as
1230 * the current one, return false.
Leon Scroggins1f005d32009-08-10 17:36:42 -04001231 */
Leon Scroggins160a7e72009-08-14 18:28:01 -04001232 /* package */ boolean switchToTab(int index) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001233 Tab tab = mTabControl.getTab(index);
1234 Tab currentTab = mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001235 if (tab == null || tab == currentTab) {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001236 return false;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001237 }
1238 if (currentTab != null) {
1239 // currentTab may be null if it was just removed. In that case,
1240 // we do not need to remove it
1241 removeTabFromContentView(currentTab);
1242 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001243 mTabControl.setCurrentTab(tab);
1244 attachTabToContentView(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001245 resetTitleIconAndProgress();
1246 updateLockIconToLatest();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001247 return true;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001248 }
1249
Grace Kloba22ac16e2009-10-07 18:00:23 -07001250 /* package */ Tab openTabToHomePage() {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001251 return openTabAndShow(mSettings.getHomePage(), false, null);
1252 }
1253
Leon Scroggins1f005d32009-08-10 17:36:42 -04001254 /* package */ void closeCurrentWindow() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001255 final Tab current = mTabControl.getCurrentTab();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001256 if (mTabControl.getTabCount() == 1) {
Leon Scroggins30444232009-09-04 18:36:20 -04001257 // This is the last tab. Open a new one, with the home
1258 // page and close the current one.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001259 openTabToHomePage();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001260 closeTab(current);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001261 return;
1262 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001263 final Tab parent = current.getParentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001264 int indexToShow = -1;
1265 if (parent != null) {
1266 indexToShow = mTabControl.getTabIndex(parent);
1267 } else {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001268 final int currentIndex = mTabControl.getCurrentIndex();
1269 // Try to move to the tab to the right
1270 indexToShow = currentIndex + 1;
1271 if (indexToShow > mTabControl.getTabCount() - 1) {
1272 // Try to move to the tab to the left
1273 indexToShow = currentIndex - 1;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001274 }
1275 }
Leon Scroggins160a7e72009-08-14 18:28:01 -04001276 if (switchToTab(indexToShow)) {
1277 // Close window
1278 closeTab(current);
1279 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001280 }
1281
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001282 private ActiveTabsPage mActiveTabsPage;
1283
1284 /**
1285 * Remove the active tabs page.
1286 * @param needToAttach If true, the active tabs page did not attach a tab
1287 * to the content view, so we need to do that here.
1288 */
1289 /* package */ void removeActiveTabPage(boolean needToAttach) {
1290 mContentView.removeView(mActiveTabsPage);
Leon Scrogginsd746a942010-05-19 13:21:44 -04001291 mTitleBar.setVisibility(View.VISIBLE);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001292 mActiveTabsPage = null;
1293 mMenuState = R.id.MAIN_MENU;
1294 if (needToAttach) {
1295 attachTabToContentView(mTabControl.getCurrentTab());
1296 }
1297 getTopWindow().requestFocus();
1298 }
1299
Cary Clark01cfcdd2010-06-04 16:36:45 -04001300 private WebView showDialog(WebDialog dialog) {
1301 // Need to do something special for Tablet
1302 Tab tab = mTabControl.getCurrentTab();
1303 if (tab.getSubWebView() == null) {
1304 // If the find or select is being performed on the main webview,
1305 // remove the embedded title bar.
1306 WebView mainView = tab.getWebView();
1307 if (mainView != null) {
1308 mainView.setEmbeddedTitleBar(null);
1309 }
1310 }
1311 hideFakeTitleBar();
1312 mMenuState = EMPTY_MENU;
1313 return tab.showDialog(dialog);
1314 }
1315
The Android Open Source Project0c908882009-03-03 19:32:16 -08001316 @Override
1317 public boolean onOptionsItemSelected(MenuItem item) {
1318 if (!mCanChord) {
1319 // The user has already fired a shortcut with this hold down of the
1320 // menu key.
1321 return false;
1322 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001323 if (null == getTopWindow()) {
Leon Scroggins0d7ae0e2009-06-05 11:04:45 -04001324 return false;
1325 }
Grace Kloba6ee9c492009-07-13 10:04:34 -07001326 if (mMenuIsDown) {
1327 // The shortcut action consumes the MENU. Even if it is still down,
1328 // it won't trigger the next shortcut action. In the case of the
1329 // shortcut action triggering a new activity, like Bookmarks, we
1330 // won't get onKeyUp for MENU. So it is important to reset it here.
1331 mMenuIsDown = false;
1332 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001333 switch (item.getItemId()) {
1334 // -- Main menu
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001335 case R.id.new_tab_menu_id:
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001336 openTabToHomePage();
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001337 break;
1338
Leon Scroggins64b80f32009-08-07 12:03:34 -04001339 case R.id.goto_menu_id:
Leon Scroggins8ad29922010-02-16 12:33:55 -05001340 editUrl();
Leon Scrogginsb3a5bed2009-09-28 11:21:56 -04001341 break;
1342
1343 case R.id.bookmarks_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001344 bookmarksOrHistoryPicker(false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001345 break;
1346
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001347 case R.id.active_tabs_menu_id:
1348 mActiveTabsPage = new ActiveTabsPage(this, mTabControl);
1349 removeTabFromContentView(mTabControl.getCurrentTab());
Leon Scrogginsd746a942010-05-19 13:21:44 -04001350 mTitleBar.setVisibility(View.GONE);
Leon Scroggins43de6162009-09-14 19:59:58 -04001351 hideFakeTitleBar();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001352 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
1353 mActiveTabsPage.requestFocus();
1354 mMenuState = EMPTY_MENU;
1355 break;
1356
Leon Scroggins1f005d32009-08-10 17:36:42 -04001357 case R.id.add_bookmark_menu_id:
Leon Scroggins571b3762010-05-26 10:25:01 -04001358 bookmarkCurrentPage();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001359 break;
1360
1361 case R.id.stop_reload_menu_id:
1362 if (mInLoad) {
1363 stopLoading();
1364 } else {
1365 getTopWindow().reload();
1366 }
1367 break;
1368
1369 case R.id.back_menu_id:
1370 getTopWindow().goBack();
1371 break;
1372
1373 case R.id.forward_menu_id:
1374 getTopWindow().goForward();
1375 break;
1376
1377 case R.id.close_menu_id:
1378 // Close the subwindow if it exists.
1379 if (mTabControl.getCurrentSubWindow() != null) {
1380 dismissSubWindow(mTabControl.getCurrentTab());
1381 break;
1382 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001383 closeCurrentWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001384 break;
1385
1386 case R.id.homepage_menu_id:
Grace Kloba22ac16e2009-10-07 18:00:23 -07001387 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001388 if (current != null) {
1389 dismissSubWindow(current);
Leon Scroggins92472e82010-02-17 16:32:28 -05001390 loadUrl(current.getWebView(), mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001391 }
1392 break;
1393
1394 case R.id.preferences_menu_id:
1395 Intent intent = new Intent(this,
1396 BrowserPreferencesPage.class);
Leon Scrogginsd5304942009-12-10 16:11:39 -05001397 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1398 getTopWindow().getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001399 startActivityForResult(intent, PREFERENCES_PAGE);
1400 break;
1401
1402 case R.id.find_menu_id:
Cary Clark01cfcdd2010-06-04 16:36:45 -04001403 showFindDialog();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001404 break;
1405
The Android Open Source Project0c908882009-03-03 19:32:16 -08001406 case R.id.page_info_menu_id:
1407 showPageInfo(mTabControl.getCurrentTab(), false);
1408 break;
1409
1410 case R.id.classic_history_menu_id:
Leon Scroggins30444232009-09-04 18:36:20 -04001411 bookmarksOrHistoryPicker(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001412 break;
1413
Leon Scroggins96afcb12009-12-10 12:35:56 -05001414 case R.id.title_bar_share_page_url:
The Android Open Source Project0c908882009-03-03 19:32:16 -08001415 case R.id.share_page_menu_id:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001416 Tab currentTab = mTabControl.getCurrentTab();
1417 if (null == currentTab) {
1418 mCanChord = false;
1419 return false;
1420 }
1421 currentTab.populatePickerData();
1422 sharePage(this, currentTab.getTitle(),
1423 currentTab.getUrl(), currentTab.getFavicon(),
Ben Murdoch87cc65d2010-06-29 20:34:10 +01001424 createScreenshot(currentTab.getWebView(), getDesiredThumbnailWidth(this),
1425 getDesiredThumbnailHeight(this)));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001426 break;
1427
1428 case R.id.dump_nav_menu_id:
1429 getTopWindow().debugDump();
1430 break;
1431
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001432 case R.id.dump_counters_menu_id:
1433 getTopWindow().dumpV8Counters();
1434 break;
1435
The Android Open Source Project0c908882009-03-03 19:32:16 -08001436 case R.id.zoom_in_menu_id:
1437 getTopWindow().zoomIn();
1438 break;
1439
1440 case R.id.zoom_out_menu_id:
1441 getTopWindow().zoomOut();
1442 break;
1443
1444 case R.id.view_downloads_menu_id:
1445 viewDownloads(null);
1446 break;
1447
The Android Open Source Project0c908882009-03-03 19:32:16 -08001448 case R.id.window_one_menu_id:
1449 case R.id.window_two_menu_id:
1450 case R.id.window_three_menu_id:
1451 case R.id.window_four_menu_id:
1452 case R.id.window_five_menu_id:
1453 case R.id.window_six_menu_id:
1454 case R.id.window_seven_menu_id:
1455 case R.id.window_eight_menu_id:
1456 {
1457 int menuid = item.getItemId();
1458 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1459 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001460 Tab desiredTab = mTabControl.getTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001461 if (desiredTab != null &&
1462 desiredTab != mTabControl.getCurrentTab()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001463 switchToTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001464 }
1465 break;
1466 }
1467 }
1468 }
1469 break;
1470
1471 default:
1472 if (!super.onOptionsItemSelected(item)) {
1473 return false;
1474 }
1475 // Otherwise fall through.
1476 }
1477 mCanChord = false;
1478 return true;
1479 }
1480
Leon Scroggins571b3762010-05-26 10:25:01 -04001481 /* package */ void bookmarkCurrentPage() {
1482 Intent i = new Intent(BrowserActivity.this,
1483 AddBookmarkPage.class);
1484 WebView w = getTopWindow();
1485 i.putExtra("url", w.getUrl());
1486 i.putExtra("title", w.getTitle());
1487 i.putExtra("touch_icon_url", w.getTouchIconUrl());
Ben Murdoch87cc65d2010-06-29 20:34:10 +01001488 i.putExtra("thumbnail", createScreenshot(w, getDesiredThumbnailWidth(this),
1489 getDesiredThumbnailHeight(this)));
Leon Scroggins571b3762010-05-26 10:25:01 -04001490 startActivity(i);
1491 }
1492
Cary Clark01cfcdd2010-06-04 16:36:45 -04001493 private boolean dialogIsUp() {
1494 return null != mFindDialog && mFindDialog.isVisible() ||
1495 null != mSelectDialog && mSelectDialog.isVisible();
1496 }
1497
1498 private boolean closeDialog(WebDialog dialog) {
1499 if (null == dialog || !dialog.isVisible()) return false;
Leon Scroggins79e36d92010-04-29 16:01:46 +01001500 Tab currentTab = mTabControl.getCurrentTab();
Cary Clark01cfcdd2010-06-04 16:36:45 -04001501 currentTab.closeDialog(dialog);
1502 dialog.dismiss();
1503 return true;
1504 }
1505
1506 /*
1507 * Remove the find dialog or select dialog.
1508 */
1509 public void closeDialogs() {
1510 if (!(closeDialog(mFindDialog) || closeDialog(mSelectDialog))) return;
Leon Scrogginsd746a942010-05-19 13:21:44 -04001511 if (!mXLargeScreenSize) {
1512 // If the Find was being performed in the main WebView, replace the
1513 // embedded title bar.
Cary Clark01cfcdd2010-06-04 16:36:45 -04001514 Tab currentTab = mTabControl.getCurrentTab();
Leon Scrogginsd746a942010-05-19 13:21:44 -04001515 if (currentTab.getSubWebView() == null) {
1516 WebView mainView = currentTab.getWebView();
1517 if (mainView != null) {
1518 mainView.setEmbeddedTitleBar(mTitleBar);
1519 }
Leon Scroggins79e36d92010-04-29 16:01:46 +01001520 }
1521 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001522 mMenuState = R.id.MAIN_MENU;
Leon Scroggins79e36d92010-04-29 16:01:46 +01001523 if (mInLoad) {
1524 // The title bar was hidden, because otherwise it would cover up the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001525 // find or select dialog. Now that the dialog has been removed,
1526 // show the fake title bar once again.
Leon Scroggins79e36d92010-04-29 16:01:46 +01001527 showFakeTitleBar();
1528 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001529 }
1530
Cary Clark01cfcdd2010-06-04 16:36:45 -04001531 public void showFindDialog() {
1532 if (null == mFindDialog) {
1533 mFindDialog = new FindDialog(this);
1534 }
1535 showDialog(mFindDialog).setFindIsUp(true);
1536 }
1537
1538 public void setFindDialogText(String text) {
1539 mFindDialog.setText(text);
1540 }
1541
1542 public void showSelectDialog() {
1543 if (null == mSelectDialog) {
1544 mSelectDialog = new SelectDialog(this);
1545 }
1546 showDialog(mSelectDialog).setUpSelect();
1547 mSelectDialog.hideSoftInput();
1548 }
1549
Grace Kloba22ac16e2009-10-07 18:00:23 -07001550 @Override
1551 public boolean onPrepareOptionsMenu(Menu menu) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001552 // This happens when the user begins to hold down the menu key, so
1553 // allow them to chord to get a shortcut.
1554 mCanChord = true;
1555 // Note: setVisible will decide whether an item is visible; while
1556 // setEnabled() will decide whether an item is enabled, which also means
1557 // whether the matching shortcut key will function.
1558 super.onPrepareOptionsMenu(menu);
1559 switch (mMenuState) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001560 case EMPTY_MENU:
1561 if (mCurrentMenuState != mMenuState) {
1562 menu.setGroupVisible(R.id.MAIN_MENU, false);
1563 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1564 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001565 }
1566 break;
1567 default:
1568 if (mCurrentMenuState != mMenuState) {
1569 menu.setGroupVisible(R.id.MAIN_MENU, true);
1570 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1571 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001572 }
1573 final WebView w = getTopWindow();
1574 boolean canGoBack = false;
1575 boolean canGoForward = false;
1576 boolean isHome = false;
1577 if (w != null) {
1578 canGoBack = w.canGoBack();
1579 canGoForward = w.canGoForward();
1580 isHome = mSettings.getHomePage().equals(w.getUrl());
1581 }
1582 final MenuItem back = menu.findItem(R.id.back_menu_id);
1583 back.setEnabled(canGoBack);
1584
1585 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1586 home.setEnabled(!isHome);
1587
Michael Kolbe0a36662010-06-29 10:37:12 -07001588 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1589 forward.setEnabled(canGoForward);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001590
Michael Kolbe0a36662010-06-29 10:37:12 -07001591 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
1592 newtab.setEnabled(mTabControl.canCreateNewTab());
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001593
The Android Open Source Project0c908882009-03-03 19:32:16 -08001594 // decide whether to show the share link option
1595 PackageManager pm = getPackageManager();
1596 Intent send = new Intent(Intent.ACTION_SEND);
1597 send.setType("text/plain");
1598 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1599 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1600
The Android Open Source Project0c908882009-03-03 19:32:16 -08001601 boolean isNavDump = mSettings.isNavDump();
1602 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1603 nav.setVisible(isNavDump);
1604 nav.setEnabled(isNavDump);
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001605
1606 boolean showDebugSettings = mSettings.showDebugSettings();
1607 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1608 counter.setVisible(showDebugSettings);
1609 counter.setEnabled(showDebugSettings);
1610
The Android Open Source Project0c908882009-03-03 19:32:16 -08001611 break;
1612 }
1613 mCurrentMenuState = mMenuState;
1614 return true;
1615 }
1616
1617 @Override
1618 public void onCreateContextMenu(ContextMenu menu, View v,
1619 ContextMenuInfo menuInfo) {
Leon Scroggins571b3762010-05-26 10:25:01 -04001620 if (v instanceof TitleBarBase) {
Leon Scroggins4e9f89b2010-02-22 16:54:14 -05001621 return;
1622 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001623 WebView webview = (WebView) v;
1624 WebView.HitTestResult result = webview.getHitTestResult();
1625 if (result == null) {
1626 return;
1627 }
1628
1629 int type = result.getType();
1630 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1631 Log.w(LOGTAG,
1632 "We should not show context menu when nothing is touched");
1633 return;
1634 }
1635 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1636 // let TextView handles context menu
1637 return;
1638 }
1639
1640 // Note, http://b/issue?id=1106666 is requesting that
1641 // an inflated menu can be used again. This is not available
1642 // yet, so inflate each time (yuk!)
1643 MenuInflater inflater = getMenuInflater();
1644 inflater.inflate(R.menu.browsercontext, menu);
1645
1646 // Show the correct menu group
Leon Scroggins III9e997c72010-05-26 13:25:16 -04001647 final String extra = result.getExtra();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001648 menu.setGroupVisible(R.id.PHONE_MENU,
1649 type == WebView.HitTestResult.PHONE_TYPE);
1650 menu.setGroupVisible(R.id.EMAIL_MENU,
1651 type == WebView.HitTestResult.EMAIL_TYPE);
1652 menu.setGroupVisible(R.id.GEO_MENU,
1653 type == WebView.HitTestResult.GEO_TYPE);
1654 menu.setGroupVisible(R.id.IMAGE_MENU,
1655 type == WebView.HitTestResult.IMAGE_TYPE
1656 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1657 menu.setGroupVisible(R.id.ANCHOR_MENU,
1658 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1659 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1660
1661 // Setup custom handling depending on the type
1662 switch (type) {
1663 case WebView.HitTestResult.PHONE_TYPE:
1664 menu.setHeaderTitle(Uri.decode(extra));
1665 menu.findItem(R.id.dial_context_menu_id).setIntent(
1666 new Intent(Intent.ACTION_VIEW, Uri
1667 .parse(WebView.SCHEME_TEL + extra)));
1668 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1669 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
Cary Clark5e335a32009-09-22 14:53:11 -04001670 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001671 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1672 addIntent);
1673 menu.findItem(R.id.copy_phone_context_menu_id).setOnMenuItemClickListener(
1674 new Copy(extra));
1675 break;
1676
1677 case WebView.HitTestResult.EMAIL_TYPE:
1678 menu.setHeaderTitle(extra);
1679 menu.findItem(R.id.email_context_menu_id).setIntent(
1680 new Intent(Intent.ACTION_VIEW, Uri
1681 .parse(WebView.SCHEME_MAILTO + extra)));
1682 menu.findItem(R.id.copy_mail_context_menu_id).setOnMenuItemClickListener(
1683 new Copy(extra));
1684 break;
1685
1686 case WebView.HitTestResult.GEO_TYPE:
1687 menu.setHeaderTitle(extra);
1688 menu.findItem(R.id.map_context_menu_id).setIntent(
1689 new Intent(Intent.ACTION_VIEW, Uri
1690 .parse(WebView.SCHEME_GEO
1691 + URLEncoder.encode(extra))));
1692 menu.findItem(R.id.copy_geo_context_menu_id).setOnMenuItemClickListener(
1693 new Copy(extra));
1694 break;
1695
1696 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1697 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
1698 TextView titleView = (TextView) LayoutInflater.from(this)
1699 .inflate(android.R.layout.browser_link_context_header,
1700 null);
1701 titleView.setText(extra);
1702 menu.setHeaderView(titleView);
1703 // decide whether to show the open link in new tab option
Leon Scroggins III9e997c72010-05-26 13:25:16 -04001704 boolean showNewTab = mTabControl.canCreateNewTab();
1705 MenuItem newTabItem
1706 = menu.findItem(R.id.open_newtab_context_menu_id);
1707 newTabItem.setVisible(showNewTab);
1708 if (showNewTab) {
1709 newTabItem.setOnMenuItemClickListener(
1710 new MenuItem.OnMenuItemClickListener() {
1711 public boolean onMenuItemClick(MenuItem item) {
1712 final Tab parent = mTabControl.getCurrentTab();
1713 final Tab newTab = openTab(extra);
1714 if (newTab != parent) {
1715 parent.addChildTab(newTab);
1716 }
1717 return true;
1718 }
1719 });
1720 }
Ben Murdochde353622009-10-12 10:29:00 +01001721 menu.findItem(R.id.bookmark_context_menu_id).setVisible(
1722 Bookmarks.urlHasAcceptableScheme(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001723 PackageManager pm = getPackageManager();
1724 Intent send = new Intent(Intent.ACTION_SEND);
1725 send.setType("text/plain");
1726 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1727 menu.findItem(R.id.share_link_context_menu_id).setVisible(ri != null);
1728 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1729 break;
1730 }
1731 // otherwise fall through to handle image part
1732 case WebView.HitTestResult.IMAGE_TYPE:
1733 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1734 menu.setHeaderTitle(extra);
1735 }
1736 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1737 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1738 menu.findItem(R.id.download_context_menu_id).
1739 setOnMenuItemClickListener(new Download(extra));
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001740 menu.findItem(R.id.set_wallpaper_context_menu_id).
1741 setOnMenuItemClickListener(new SetAsWallpaper(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001742 break;
1743
1744 default:
1745 Log.w(LOGTAG, "We should not get here.");
1746 break;
1747 }
Leon Scrogginsb2b19f52009-10-09 16:10:00 -04001748 hideFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001749 }
1750
The Android Open Source Project0c908882009-03-03 19:32:16 -08001751 // Attach the given tab to the content view.
Grace Klobac928c302009-09-17 11:51:21 -07001752 // this should only be called for the current tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001753 private void attachTabToContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001754 // Attach the container that contains the main WebView and any other UI
1755 // associated with the tab.
Patrick Scottd0119532009-09-17 08:00:31 -04001756 t.attachTabToContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001757
1758 if (mShouldShowErrorConsole) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001759 ErrorConsoleView errorConsole = t.getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01001760 if (errorConsole.numberOfErrors() == 0) {
1761 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
1762 } else {
1763 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1764 }
1765
1766 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08001767 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01001768 ViewGroup.LayoutParams.WRAP_CONTENT));
1769 }
1770
Leon Scrogginsd746a942010-05-19 13:21:44 -04001771 if (!mXLargeScreenSize){
1772 WebView view = t.getWebView();
1773 view.setEmbeddedTitleBar(mTitleBar);
1774 }
Leon Scroggins58d56c62010-01-28 15:12:40 -05001775 if (t.isInVoiceSearchMode()) {
1776 showVoiceTitleBar(t.getVoiceDisplayTitle());
1777 } else {
1778 revertVoiceTitleBar();
1779 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001780 // Request focus on the top window.
1781 t.getTopWindow().requestFocus();
1782 }
1783
1784 // Attach a sub window to the main WebView of the given tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001785 void attachSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001786 t.attachSubWindow(mContentView);
1787 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001788 }
1789
1790 // Remove the given tab from the content view.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001791 private void removeTabFromContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001792 // Remove the container that contains the main WebView.
Patrick Scottd0119532009-09-17 08:00:31 -04001793 t.removeTabFromContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001794
Grace Kloba22ac16e2009-10-07 18:00:23 -07001795 ErrorConsoleView errorConsole = t.getErrorConsole(false);
1796 if (errorConsole != null) {
1797 mErrorConsoleContainer.removeView(errorConsole);
Ben Murdochbff2d602009-07-01 20:19:05 +01001798 }
1799
Leon Scrogginsd746a942010-05-19 13:21:44 -04001800 if (!mXLargeScreenSize) {
1801 WebView view = t.getWebView();
1802 if (view != null) {
1803 view.setEmbeddedTitleBar(null);
1804 }
Leon Scrogginsbb85b902009-09-14 19:27:20 -04001805 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001806 }
1807
1808 // Remove the sub window if it exists. Also called by TabControl when the
1809 // user clicks the 'X' to dismiss a sub window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001810 /* package */ void dismissSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001811 t.removeSubWindow(mContentView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001812 // dismiss the subwindow. This will destroy the WebView.
1813 t.dismissSubWindow();
Patrick Scottd0119532009-09-17 08:00:31 -04001814 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001815 }
1816
Leon Scroggins1f005d32009-08-10 17:36:42 -04001817 // A wrapper function of {@link #openTabAndShow(UrlData, boolean, String)}
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07001818 // that accepts url as string.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001819 private Tab openTabAndShow(String url, boolean closeOnExit, String appId) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001820 return openTabAndShow(new UrlData(url), closeOnExit, appId);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001821 }
1822
1823 // This method does a ton of stuff. It will attempt to create a new tab
1824 // if we haven't reached MAX_TABS. Otherwise it uses the current tab. If
Leon Scroggins1f005d32009-08-10 17:36:42 -04001825 // url isn't null, it will load the given url.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001826 /* package */Tab openTabAndShow(UrlData urlData, boolean closeOnExit,
1827 String appId) {
1828 final Tab currentTab = mTabControl.getCurrentTab();
1829 if (mTabControl.canCreateNewTab()) {
1830 final Tab tab = mTabControl.createNewTab(closeOnExit, appId,
1831 urlData.mUrl);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001832 WebView webview = tab.getWebView();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001833 // If the last tab was removed from the active tabs page, currentTab
1834 // will be null.
1835 if (currentTab != null) {
1836 removeTabFromContentView(currentTab);
1837 }
Patrick Scott8bbd69f2009-08-14 13:35:53 -04001838 // We must set the new tab as the current tab to reflect the old
1839 // animation behavior.
1840 mTabControl.setCurrentTab(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001841 attachTabToContentView(tab);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001842 if (!urlData.isEmpty()) {
Patrick Scott9d53da02010-02-19 10:19:01 -05001843 loadUrlDataIn(tab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001844 }
1845 return tab;
1846 } else {
1847 // Get rid of the subwindow if it exists
1848 dismissSubWindow(currentTab);
1849 if (!urlData.isEmpty()) {
1850 // Load the given url.
Patrick Scott9d53da02010-02-19 10:19:01 -05001851 loadUrlDataIn(currentTab, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001852 }
Leon Scroggins58d56c62010-01-28 15:12:40 -05001853 return currentTab;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001854 }
1855 }
1856
Grace Kloba22ac16e2009-10-07 18:00:23 -07001857 private Tab openTab(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001858 if (mSettings.openInBackground()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001859 Tab t = mTabControl.createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001860 if (t != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001861 WebView view = t.getWebView();
Leon Scroggins92472e82010-02-17 16:32:28 -05001862 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001863 }
Grace Klobac9181842009-04-14 08:53:22 -07001864 return t;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001865 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001866 return openTabAndShow(url, false, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001867 }
1868 }
1869
1870 private class Copy implements OnMenuItemClickListener {
1871 private CharSequence mText;
1872
1873 public boolean onMenuItemClick(MenuItem item) {
1874 copy(mText);
1875 return true;
1876 }
1877
1878 public Copy(CharSequence toCopy) {
1879 mText = toCopy;
1880 }
1881 }
1882
1883 private class Download implements OnMenuItemClickListener {
1884 private String mText;
1885
1886 public boolean onMenuItemClick(MenuItem item) {
1887 onDownloadStartNoStream(mText, null, null, null, -1);
1888 return true;
1889 }
1890
1891 public Download(String toDownload) {
1892 mText = toDownload;
1893 }
1894 }
1895
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001896 private class SetAsWallpaper extends Thread implements
1897 OnMenuItemClickListener, DialogInterface.OnCancelListener {
1898 private URL mUrl;
1899 private ProgressDialog mWallpaperProgress;
1900 private boolean mCanceled = false;
1901
1902 public SetAsWallpaper(String url) {
1903 try {
1904 mUrl = new URL(url);
1905 } catch (MalformedURLException e) {
1906 mUrl = null;
1907 }
1908 }
1909
1910 public void onCancel(DialogInterface dialog) {
1911 mCanceled = true;
1912 }
1913
1914 public boolean onMenuItemClick(MenuItem item) {
1915 if (mUrl != null) {
1916 // The user may have tried to set a image with a large file size as their
1917 // background so it may take a few moments to perform the operation. Display
1918 // a progress spinner while it is working.
1919 mWallpaperProgress = new ProgressDialog(BrowserActivity.this);
1920 mWallpaperProgress.setIndeterminate(true);
1921 mWallpaperProgress.setMessage(getText(R.string.progress_dialog_setting_wallpaper));
1922 mWallpaperProgress.setCancelable(true);
1923 mWallpaperProgress.setOnCancelListener(this);
1924 mWallpaperProgress.show();
1925 start();
1926 }
1927 return true;
1928 }
1929
Michael Kolbe0a36662010-06-29 10:37:12 -07001930 @Override
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001931 public void run() {
1932 Drawable oldWallpaper = BrowserActivity.this.getWallpaper();
1933 try {
1934 // TODO: This will cause the resource to be downloaded again, when we
1935 // should in most cases be able to grab it from the cache. To fix this
1936 // we should query WebCore to see if we can access a cached version and
1937 // instead open an input stream on that. This pattern could also be used
1938 // in the download manager where the same problem exists.
1939 InputStream inputstream = mUrl.openStream();
1940 if (inputstream != null) {
1941 setWallpaper(inputstream);
1942 }
1943 } catch (IOException e) {
1944 Log.e(LOGTAG, "Unable to set new wallpaper");
1945 // Act as though the user canceled the operation so we try to
1946 // restore the old wallpaper.
1947 mCanceled = true;
1948 }
1949
1950 if (mCanceled) {
1951 // Restore the old wallpaper if the user cancelled whilst we were setting
1952 // the new wallpaper.
1953 int width = oldWallpaper.getIntrinsicWidth();
1954 int height = oldWallpaper.getIntrinsicHeight();
1955 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1956 Canvas canvas = new Canvas(bm);
1957 oldWallpaper.setBounds(0, 0, width, height);
1958 oldWallpaper.draw(canvas);
1959 try {
1960 setWallpaper(bm);
1961 } catch (IOException e) {
1962 Log.e(LOGTAG, "Unable to restore old wallpaper.");
1963 }
1964 mCanceled = false;
1965 }
1966
1967 if (mWallpaperProgress.isShowing()) {
1968 mWallpaperProgress.dismiss();
1969 }
1970 }
1971 }
1972
The Android Open Source Project0c908882009-03-03 19:32:16 -08001973 private void copy(CharSequence text) {
1974 try {
1975 IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
1976 if (clip != null) {
1977 clip.setClipboardText(text);
1978 }
1979 } catch (android.os.RemoteException e) {
1980 Log.e(LOGTAG, "Copy failed", e);
1981 }
1982 }
1983
1984 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001985 * Resets the browser title-view to whatever it must be
1986 * (for example, if we had a loading error)
1987 * When we have a new page, we call resetTitle, when we
1988 * have to reset the titlebar to whatever it used to be
1989 * (for example, if the user chose to stop loading), we
1990 * call resetTitleAndRevertLockIcon.
1991 */
1992 /* package */ void resetTitleAndRevertLockIcon() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001993 mTabControl.getCurrentTab().revertLockIcon();
1994 updateLockIconToLatest();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001995 resetTitleIconAndProgress();
1996 }
1997
1998 /**
1999 * Reset the title, favicon, and progress.
2000 */
2001 private void resetTitleIconAndProgress() {
2002 WebView current = mTabControl.getCurrentWebView();
2003 if (current == null) {
2004 return;
2005 }
2006 resetTitleAndIcon(current);
2007 int progress = current.getProgress();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002008 current.getWebChromeClient().onProgressChanged(current, progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002009 }
2010
2011 // Reset the title and the icon based on the given item.
2012 private void resetTitleAndIcon(WebView view) {
2013 WebHistoryItem item = view.copyBackForwardList().getCurrentItem();
2014 if (item != null) {
Leon Scroggins68579392009-09-15 15:31:54 -04002015 setUrlTitle(item.getUrl(), item.getTitle());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002016 setFavicon(item.getFavicon());
2017 } else {
Leon Scroggins68579392009-09-15 15:31:54 -04002018 setUrlTitle(null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002019 setFavicon(null);
2020 }
2021 }
2022
2023 /**
2024 * Sets a title composed of the URL and the title string.
2025 * @param url The URL of the site being loaded.
2026 * @param title The title of the site being loaded.
2027 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002028 void setUrlTitle(String url, String title) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002029 mUrl = url;
2030 mTitle = title;
2031
Leon Scroggins58d56c62010-01-28 15:12:40 -05002032 // If we are in voice search mode, the title has already been set.
2033 if (mTabControl.getCurrentTab().isInVoiceSearchMode()) return;
2034 mTitleBar.setDisplayTitle(url);
Leon Scrogginsd746a942010-05-19 13:21:44 -04002035 if (!mXLargeScreenSize) {
2036 mFakeTitleBar.setDisplayTitle(url);
2037 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002038 }
2039
2040 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08002041 * @param url The URL to build a title version of the URL from.
2042 * @return The title version of the URL or null if fails.
2043 * The title version of the URL can be either the URL hostname,
2044 * or the hostname with an "https://" prefix (for secure URLs),
2045 * or an empty string if, for example, the URL in question is a
2046 * file:// URL with no hostname.
2047 */
Leon Scroggins32e14a62009-06-11 10:26:34 -04002048 /* package */ static String buildTitleUrl(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002049 String titleUrl = null;
2050
2051 if (url != null) {
2052 try {
2053 // parse the url string
2054 URL urlObj = new URL(url);
2055 if (urlObj != null) {
2056 titleUrl = "";
2057
2058 String protocol = urlObj.getProtocol();
2059 String host = urlObj.getHost();
2060
2061 if (host != null && 0 < host.length()) {
2062 titleUrl = host;
2063 if (protocol != null) {
2064 // if a secure site, add an "https://" prefix!
2065 if (protocol.equalsIgnoreCase("https")) {
2066 titleUrl = protocol + "://" + host;
2067 }
2068 }
2069 }
2070 }
2071 } catch (MalformedURLException e) {}
2072 }
2073
2074 return titleUrl;
2075 }
2076
2077 // Set the favicon in the title bar.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002078 void setFavicon(Bitmap icon) {
Leon Scroggins68579392009-09-15 15:31:54 -04002079 mTitleBar.setFavicon(icon);
Leon Scrogginsd746a942010-05-19 13:21:44 -04002080 if (!mXLargeScreenSize) {
2081 mFakeTitleBar.setFavicon(icon);
2082 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002083 }
2084
2085 /**
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002086 * Close the tab, remove its associated title bar, and adjust mTabControl's
2087 * current tab to a valid value.
Leon Scroggins1f005d32009-08-10 17:36:42 -04002088 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002089 /* package */ void closeTab(Tab t) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002090 int currentIndex = mTabControl.getCurrentIndex();
2091 int removeIndex = mTabControl.getTabIndex(t);
Leon Scroggins1f005d32009-08-10 17:36:42 -04002092 mTabControl.removeTab(t);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002093 if (currentIndex >= removeIndex && currentIndex != 0) {
2094 currentIndex--;
2095 }
2096 mTabControl.setCurrentTab(mTabControl.getTab(currentIndex));
Andrei Popescua5bf1de2009-09-23 16:39:23 +01002097 resetTitleIconAndProgress();
Leon Scroggins654899b2010-06-25 16:25:23 -04002098 updateLockIconToLatest();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002099 }
2100
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05002101 /* package */ void goBackOnePageOrQuit() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002102 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002103 if (current == null) {
2104 /*
2105 * Instead of finishing the activity, simply push this to the back
2106 * of the stack and let ActivityManager to choose the foreground
2107 * activity. As BrowserActivity is singleTask, it will be always the
2108 * root of the task. So we can use either true or false for
2109 * moveTaskToBack().
2110 */
2111 moveTaskToBack(true);
Grace Kloba00d85e72009-09-23 18:50:05 -07002112 return;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002113 }
2114 WebView w = current.getWebView();
2115 if (w.canGoBack()) {
2116 w.goBack();
2117 } else {
2118 // Check to see if we are closing a window that was created by
2119 // another window. If so, we switch back to that window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002120 Tab parent = current.getParentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002121 if (parent != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04002122 switchToTab(mTabControl.getTabIndex(parent));
2123 // Now we close the other tab
2124 closeTab(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002125 } else {
2126 if (current.closeOnExit()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002127 // force the tab's inLoad() to be false as we are going to
2128 // either finish the activity or remove the tab. This will
2129 // ensure pauseWebViewTimers() taking action.
2130 mTabControl.getCurrentTab().clearInLoad();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002131 if (mTabControl.getTabCount() == 1) {
2132 finish();
2133 return;
2134 }
Mike Reed7bfa63b2009-05-28 11:08:32 -04002135 // call pauseWebViewTimers() now, we won't be able to call
2136 // it in onPause() as the WebView won't be valid.
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002137 // Temporarily change mActivityInPause to be true as
2138 // pauseWebViewTimers() will do nothing if mActivityInPause
2139 // is false.
Grace Kloba918e1d72009-08-13 14:55:06 -07002140 boolean savedState = mActivityInPause;
2141 if (savedState) {
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002142 Log.e(LOGTAG, "BrowserActivity is already paused "
2143 + "while handing goBackOnePageOrQuit.");
Grace Kloba918e1d72009-08-13 14:55:06 -07002144 }
2145 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -04002146 pauseWebViewTimers();
Grace Kloba918e1d72009-08-13 14:55:06 -07002147 mActivityInPause = savedState;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002148 removeTabFromContentView(current);
2149 mTabControl.removeTab(current);
2150 }
2151 /*
2152 * Instead of finishing the activity, simply push this to the back
2153 * of the stack and let ActivityManager to choose the foreground
2154 * activity. As BrowserActivity is singleTask, it will be always the
2155 * root of the task. So we can use either true or false for
2156 * moveTaskToBack().
2157 */
2158 moveTaskToBack(true);
2159 }
2160 }
2161 }
2162
Grace Kloba22ac16e2009-10-07 18:00:23 -07002163 boolean isMenuDown() {
2164 return mMenuIsDown;
2165 }
2166
Grace Kloba5942df02009-09-18 11:48:29 -07002167 @Override
2168 public boolean onKeyDown(int keyCode, KeyEvent event) {
Leon Scrogginsf65b50d2009-12-08 10:44:28 -05002169 // Even if MENU is already held down, we need to call to super to open
2170 // the IME on long press.
2171 if (KeyEvent.KEYCODE_MENU == keyCode) {
2172 mMenuIsDown = true;
2173 return super.onKeyDown(keyCode, event);
2174 }
Grace Kloba5942df02009-09-18 11:48:29 -07002175 // The default key mode is DEFAULT_KEYS_SEARCH_LOCAL. As the MENU is
2176 // still down, we don't want to trigger the search. Pretend to consume
2177 // the key and do nothing.
2178 if (mMenuIsDown) return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002179
Grace Kloba5942df02009-09-18 11:48:29 -07002180 switch(keyCode) {
Grace Kloba5942df02009-09-18 11:48:29 -07002181 case KeyEvent.KEYCODE_SPACE:
Grace Klobada0fe552009-09-22 18:17:24 -07002182 // WebView/WebTextView handle the keys in the KeyDown. As
2183 // the Activity's shortcut keys are only handled when WebView
2184 // doesn't, have to do it in onKeyDown instead of onKeyUp.
2185 if (event.isShiftPressed()) {
2186 getTopWindow().pageUp(false);
2187 } else {
2188 getTopWindow().pageDown(false);
2189 }
Grace Kloba5942df02009-09-18 11:48:29 -07002190 return true;
2191 case KeyEvent.KEYCODE_BACK:
2192 if (event.getRepeatCount() == 0) {
2193 event.startTracking();
2194 return true;
2195 } else if (mCustomView == null && mActiveTabsPage == null
2196 && event.isLongPress()) {
2197 bookmarksOrHistoryPicker(true);
2198 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002199 }
Grace Kloba5942df02009-09-18 11:48:29 -07002200 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002201 }
Grace Kloba5942df02009-09-18 11:48:29 -07002202 return super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002203 }
2204
Grace Kloba5942df02009-09-18 11:48:29 -07002205 @Override
2206 public boolean onKeyUp(int keyCode, KeyEvent event) {
2207 switch(keyCode) {
2208 case KeyEvent.KEYCODE_MENU:
2209 mMenuIsDown = false;
2210 break;
Grace Kloba5942df02009-09-18 11:48:29 -07002211 case KeyEvent.KEYCODE_BACK:
2212 if (event.isTracking() && !event.isCanceled()) {
2213 if (mCustomView != null) {
2214 // if a custom view is showing, hide it
Grace Kloba22ac16e2009-10-07 18:00:23 -07002215 mTabControl.getCurrentWebView().getWebChromeClient()
2216 .onHideCustomView();
Grace Kloba5942df02009-09-18 11:48:29 -07002217 } else if (mActiveTabsPage != null) {
2218 // if tab page is showing, hide it
2219 removeActiveTabPage(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002220 } else {
Grace Kloba5942df02009-09-18 11:48:29 -07002221 WebView subwindow = mTabControl.getCurrentSubWindow();
2222 if (subwindow != null) {
2223 if (subwindow.canGoBack()) {
2224 subwindow.goBack();
2225 } else {
2226 dismissSubWindow(mTabControl.getCurrentTab());
2227 }
2228 } else {
2229 goBackOnePageOrQuit();
2230 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002231 }
Grace Kloba5942df02009-09-18 11:48:29 -07002232 return true;
2233 }
2234 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002235 }
Grace Kloba5942df02009-09-18 11:48:29 -07002236 return super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002237 }
2238
Leon Scroggins68579392009-09-15 15:31:54 -04002239 /* package */ void stopLoading() {
Ben Murdochb7cc8b42009-09-28 10:59:09 +01002240 mDidStopLoad = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002241 resetTitleAndRevertLockIcon();
2242 WebView w = getTopWindow();
2243 w.stopLoading();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002244 // FIXME: before refactor, it is using mWebViewClient. So I keep the
2245 // same logic here. But for subwindow case, should we call into the main
2246 // WebView's onPageFinished as we never call its onPageStarted and if
2247 // the page finishes itself, we don't call onPageFinished.
2248 mTabControl.getCurrentWebView().getWebViewClient().onPageFinished(w,
2249 w.getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002250
2251 cancelStopToast();
2252 mStopToast = Toast
2253 .makeText(this, R.string.stopping, Toast.LENGTH_SHORT);
2254 mStopToast.show();
2255 }
2256
Grace Kloba22ac16e2009-10-07 18:00:23 -07002257 boolean didUserStopLoading() {
2258 return mDidStopLoad;
2259 }
2260
The Android Open Source Project0c908882009-03-03 19:32:16 -08002261 private void cancelStopToast() {
2262 if (mStopToast != null) {
2263 mStopToast.cancel();
2264 mStopToast = null;
2265 }
2266 }
2267
Grace Kloba22ac16e2009-10-07 18:00:23 -07002268 // called by a UI or non-UI thread to post the message
2269 public void postMessage(int what, int arg1, int arg2, Object obj,
2270 long delayMillis) {
2271 mHandler.sendMessageDelayed(mHandler.obtainMessage(what, arg1, arg2,
2272 obj), delayMillis);
2273 }
2274
2275 // called by a UI or non-UI thread to remove the message
2276 void removeMessages(int what, Object object) {
2277 mHandler.removeMessages(what, object);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002278 }
2279
2280 // public message ids
2281 public final static int LOAD_URL = 1001;
2282 public final static int STOP_LOAD = 1002;
2283
2284 // Message Ids
2285 private static final int FOCUS_NODE_HREF = 102;
Grace Kloba92c18a52009-07-31 23:48:32 -07002286 private static final int RELEASE_WAKELOCK = 107;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002287
Grace Kloba22ac16e2009-10-07 18:00:23 -07002288 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
Ben Murdoch2694e232009-09-29 09:41:11 +01002289
The Android Open Source Project0c908882009-03-03 19:32:16 -08002290 // Private handler for handling javascript and saving passwords
2291 private Handler mHandler = new Handler() {
2292
Michael Kolbe0a36662010-06-29 10:37:12 -07002293 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -08002294 public void handleMessage(Message msg) {
2295 switch (msg.what) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002296 case FOCUS_NODE_HREF:
Ben Murdoch2694e232009-09-29 09:41:11 +01002297 {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002298 String url = (String) msg.getData().get("url");
Ben Murdoch90d088c2009-11-17 18:14:04 +00002299 String title = (String) msg.getData().get("title");
The Android Open Source Project0c908882009-03-03 19:32:16 -08002300 if (url == null || url.length() == 0) {
2301 break;
2302 }
2303 HashMap focusNodeMap = (HashMap) msg.obj;
2304 WebView view = (WebView) focusNodeMap.get("webview");
2305 // Only apply the action if the top window did not change.
2306 if (getTopWindow() != view) {
2307 break;
2308 }
2309 switch (msg.arg1) {
2310 case R.id.open_context_menu_id:
2311 case R.id.view_image_context_menu_id:
Leon Scroggins92472e82010-02-17 16:32:28 -05002312 loadUrlFromContext(getTopWindow(), url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002313 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002314 case R.id.bookmark_context_menu_id:
2315 Intent intent = new Intent(BrowserActivity.this,
2316 AddBookmarkPage.class);
2317 intent.putExtra("url", url);
Ben Murdoch90d088c2009-11-17 18:14:04 +00002318 intent.putExtra("title", title);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002319 startActivity(intent);
2320 break;
2321 case R.id.share_link_context_menu_id:
Leon Scroggins3e204452010-05-10 11:06:03 -04002322 sharePage(BrowserActivity.this, title, url, null,
Leon Scroggins96afcb12009-12-10 12:35:56 -05002323 null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002324 break;
2325 case R.id.copy_link_context_menu_id:
2326 copy(url);
2327 break;
2328 case R.id.save_link_context_menu_id:
2329 case R.id.download_context_menu_id:
2330 onDownloadStartNoStream(url, null, null, null, -1);
2331 break;
2332 }
2333 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002334 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002335
2336 case LOAD_URL:
Leon Scroggins92472e82010-02-17 16:32:28 -05002337 loadUrlFromContext(getTopWindow(), (String) msg.obj);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002338 break;
2339
2340 case STOP_LOAD:
2341 stopLoading();
2342 break;
2343
The Android Open Source Project0c908882009-03-03 19:32:16 -08002344 case RELEASE_WAKELOCK:
2345 if (mWakeLock.isHeld()) {
2346 mWakeLock.release();
Grace Kloba5d0e02e2009-10-05 15:15:36 -07002347 // if we reach here, Browser should be still in the
2348 // background loading after WAKELOCK_TIMEOUT (5-min).
2349 // To avoid burning the battery, stop loading.
2350 mTabControl.stopAllLoading();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002351 }
2352 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002353
2354 case UPDATE_BOOKMARK_THUMBNAIL:
2355 WebView view = (WebView) msg.obj;
2356 if (view != null) {
2357 updateScreenshot(view);
2358 }
2359 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002360 }
2361 }
2362 };
2363
Leon Scroggins96afcb12009-12-10 12:35:56 -05002364 /**
2365 * Share a page, providing the title, url, favicon, and a screenshot. Uses
2366 * an {@link Intent} to launch the Activity chooser.
2367 * @param c Context used to launch a new Activity.
2368 * @param title Title of the page. Stored in the Intent with
Paul Westbrook03e6d392010-02-12 10:33:29 -08002369 * {@link Intent#EXTRA_SUBJECT}
Leon Scroggins96afcb12009-12-10 12:35:56 -05002370 * @param url URL of the page. Stored in the Intent with
2371 * {@link Intent#EXTRA_TEXT}
2372 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
2373 * with {@link Browser#EXTRA_SHARE_FAVICON}
2374 * @param screenshot Bitmap of a screenshot of the page. Stored in the
2375 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
2376 */
2377 public static final void sharePage(Context c, String title, String url,
2378 Bitmap favicon, Bitmap screenshot) {
2379 Intent send = new Intent(Intent.ACTION_SEND);
2380 send.setType("text/plain");
2381 send.putExtra(Intent.EXTRA_TEXT, url);
Paul Westbrook03e6d392010-02-12 10:33:29 -08002382 send.putExtra(Intent.EXTRA_SUBJECT, title);
Leon Scroggins96afcb12009-12-10 12:35:56 -05002383 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
2384 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
2385 try {
2386 c.startActivity(Intent.createChooser(send, c.getString(
2387 R.string.choosertitle_sharevia)));
2388 } catch(android.content.ActivityNotFoundException ex) {
2389 // if no app handles it, do nothing
2390 }
2391 }
2392
Leon Scroggins89c6d362009-07-15 16:54:37 -04002393 private void updateScreenshot(WebView view) {
2394 // If this is a bookmarked site, add a screenshot to the database.
2395 // FIXME: When should we update? Every time?
2396 // FIXME: Would like to make sure there is actually something to
2397 // draw, but the API for that (WebViewCore.pictureReady()) is not
2398 // currently accessible here.
Ben Murdochaac7aa62009-09-17 16:57:40 +01002399
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002400 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(this),
2401 getDesiredThumbnailHeight(this));
Patrick Scottcb192b52010-04-14 14:38:55 -04002402 if (bm == null) {
2403 return;
Leon Scroggins89c6d362009-07-15 16:54:37 -04002404 }
Patrick Scottcb192b52010-04-14 14:38:55 -04002405
2406 final ContentResolver cr = getContentResolver();
2407 final String url = view.getUrl();
2408 final String originalUrl = view.getOriginalUrl();
2409
2410 new AsyncTask<Void, Void, Void>() {
2411 @Override
2412 protected Void doInBackground(Void... unused) {
2413 Cursor c = null;
2414 try {
2415 c = BrowserBookmarksAdapter.queryBookmarksForUrl(
2416 cr, originalUrl, url, true);
2417 if (c != null) {
2418 if (c.moveToFirst()) {
2419 ContentValues values = new ContentValues();
2420 final ByteArrayOutputStream os
2421 = new ByteArrayOutputStream();
2422 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
2423 values.put(Browser.BookmarkColumns.THUMBNAIL,
2424 os.toByteArray());
2425 do {
2426 cr.update(ContentUris.withAppendedId(
2427 Browser.BOOKMARKS_URI, c.getInt(0)),
2428 values, null, null);
2429 } while (c.moveToNext());
2430 }
2431 }
2432 } catch (IllegalStateException e) {
2433 // Ignore
2434 } finally {
2435 if (c != null) c.close();
2436 }
2437 return null;
2438 }
2439 }.execute();
Leon Scroggins89c6d362009-07-15 16:54:37 -04002440 }
2441
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002442 /**
Leon Scrogginsf8551612009-09-24 16:06:02 -04002443 * Values for the size of the thumbnail created when taking a screenshot.
2444 * Lazily initialized. Instead of using these directly, use
2445 * getDesiredThumbnailWidth() or getDesiredThumbnailHeight().
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002446 */
Leon Scrogginsf8551612009-09-24 16:06:02 -04002447 private static int THUMBNAIL_WIDTH = 0;
2448 private static int THUMBNAIL_HEIGHT = 0;
2449
2450 /**
2451 * Return the desired width for thumbnail screenshots, which are stored in
2452 * the database, and used on the bookmarks screen.
2453 * @param context Context for finding out the density of the screen.
2454 * @return int desired width for thumbnail screenshot.
2455 */
2456 /* package */ static int getDesiredThumbnailWidth(Context context) {
2457 if (THUMBNAIL_WIDTH == 0) {
2458 float density = context.getResources().getDisplayMetrics().density;
2459 THUMBNAIL_WIDTH = (int) (90 * density);
2460 THUMBNAIL_HEIGHT = (int) (80 * density);
2461 }
2462 return THUMBNAIL_WIDTH;
2463 }
2464
2465 /**
2466 * Return the desired height for thumbnail screenshots, which are stored in
2467 * the database, and used on the bookmarks screen.
2468 * @param context Context for finding out the density of the screen.
2469 * @return int desired height for thumbnail screenshot.
2470 */
2471 /* package */ static int getDesiredThumbnailHeight(Context context) {
2472 // To ensure that they are both initialized.
2473 getDesiredThumbnailWidth(context);
2474 return THUMBNAIL_HEIGHT;
2475 }
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002476
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002477 private Bitmap createScreenshot(WebView view, int width, int height) {
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002478 Picture thumbnail = view.capturePicture();
Leon Scroggins45800572009-09-29 16:38:47 -04002479 if (thumbnail == null) {
2480 return null;
2481 }
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002482 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002483 Canvas canvas = new Canvas(bm);
2484 // May need to tweak these values to determine what is the
2485 // best scale factor
Ben Murdoch2694e232009-09-29 09:41:11 +01002486 int thumbnailWidth = thumbnail.getWidth();
Ben Murdochae59c3f2009-10-20 18:30:28 +01002487 int thumbnailHeight = thumbnail.getHeight();
2488 float scaleFactorX = 1.0f;
2489 float scaleFactorY = 1.0f;
Ben Murdoch2694e232009-09-29 09:41:11 +01002490 if (thumbnailWidth > 0) {
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002491 scaleFactorX = (float) width / (float)thumbnailWidth;
Ben Murdochae59c3f2009-10-20 18:30:28 +01002492 } else {
2493 return null;
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002494 }
Ben Murdochae59c3f2009-10-20 18:30:28 +01002495
2496 if (view.getWidth() > view.getHeight() &&
2497 thumbnailHeight < view.getHeight() && thumbnailHeight > 0) {
2498 // If the device is in landscape and the page is shorter
2499 // than the height of the view, stretch the thumbnail to fill the
2500 // space.
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002501 scaleFactorY = (float) height / (float)thumbnailHeight;
Ben Murdochae59c3f2009-10-20 18:30:28 +01002502 } else {
2503 // In the portrait case, this looks nice.
2504 scaleFactorY = scaleFactorX;
2505 }
2506
2507 canvas.scale(scaleFactorX, scaleFactorY);
2508
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002509 thumbnail.draw(canvas);
2510 return bm;
2511 }
2512
The Android Open Source Project0c908882009-03-03 19:32:16 -08002513 // -------------------------------------------------------------------------
Grace Kloba22ac16e2009-10-07 18:00:23 -07002514 // Helper function for WebViewClient.
The Android Open Source Project0c908882009-03-03 19:32:16 -08002515 //-------------------------------------------------------------------------
2516
2517 // Use in overrideUrlLoading
2518 /* package */ final static String SCHEME_WTAI = "wtai://wp/";
2519 /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
2520 /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;";
2521 /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;";
2522
Leon Scroggins92472e82010-02-17 16:32:28 -05002523 // Keep this initial progress in sync with initialProgressValue (* 100)
2524 // in ProgressTracker.cpp
2525 private final static int INITIAL_PROGRESS = 10;
2526
Grace Kloba22ac16e2009-10-07 18:00:23 -07002527 void onPageStarted(WebView view, String url, Bitmap favicon) {
2528 // when BrowserActivity just starts, onPageStarted may be called before
2529 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
2530 // to start the timer. As we won't switch tabs while an activity is in
2531 // pause state, we can ensure calling resume and pause in pair.
2532 if (mActivityInPause) resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002533
Grace Kloba22ac16e2009-10-07 18:00:23 -07002534 resetLockIcon(url);
2535 setUrlTitle(url, null);
2536 setFavicon(favicon);
Leon Scroggins8cf8f682009-11-04 11:13:50 -08002537 // Show some progress so that the user knows the page is beginning to
2538 // load
Leon Scroggins92472e82010-02-17 16:32:28 -05002539 onProgressChanged(view, INITIAL_PROGRESS);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002540 mDidStopLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07002541 if (!mIsNetworkUp) createAndShowNetworkDialog();
Cary Clark01cfcdd2010-06-04 16:36:45 -04002542 closeDialogs();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002543 if (mSettings.isTracing()) {
2544 String host;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002545 try {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002546 WebAddress uri = new WebAddress(url);
2547 host = uri.mHost;
2548 } catch (android.net.ParseException ex) {
2549 host = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -08002550 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002551 host = host.replace('.', '_');
2552 host += ".trace";
2553 mInTrace = true;
2554 Debug.startMethodTracing(host, 20 * 1024 * 1024);
2555 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002556
Grace Kloba22ac16e2009-10-07 18:00:23 -07002557 // Performance probe
2558 if (false) {
2559 mStart = SystemClock.uptimeMillis();
2560 mProcessStart = Process.getElapsedCpuTime();
2561 long[] sysCpu = new long[7];
2562 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2563 sysCpu, null)) {
2564 mUserStart = sysCpu[0] + sysCpu[1];
2565 mSystemStart = sysCpu[2];
2566 mIdleStart = sysCpu[3];
2567 mIrqStart = sysCpu[4] + sysCpu[5] + sysCpu[6];
2568 }
2569 mUiStart = SystemClock.currentThreadTimeMillis();
2570 }
2571 }
2572
2573 void onPageFinished(WebView view, String url) {
2574 // Reset the title and icon in case we stopped a provisional load.
2575 resetTitleAndIcon(view);
2576 // Update the lock icon image only once we are done loading
2577 updateLockIconToLatest();
2578 // pause the WebView timer and release the wake lock if it is finished
2579 // while BrowserActivity is in pause state.
2580 if (mActivityInPause && pauseWebViewTimers()) {
2581 if (mWakeLock.isHeld()) {
2582 mHandler.removeMessages(RELEASE_WAKELOCK);
2583 mWakeLock.release();
2584 }
2585 }
2586
2587 // Performance probe
2588 if (false) {
2589 long[] sysCpu = new long[7];
2590 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2591 sysCpu, null)) {
2592 String uiInfo = "UI thread used "
2593 + (SystemClock.currentThreadTimeMillis() - mUiStart)
2594 + " ms";
2595 if (LOGD_ENABLED) {
2596 Log.d(LOGTAG, uiInfo);
2597 }
2598 //The string that gets written to the log
2599 String performanceString = "It took total "
2600 + (SystemClock.uptimeMillis() - mStart)
2601 + " ms clock time to load the page."
2602 + "\nbrowser process used "
2603 + (Process.getElapsedCpuTime() - mProcessStart)
2604 + " ms, user processes used "
2605 + (sysCpu[0] + sysCpu[1] - mUserStart) * 10
2606 + " ms, kernel used "
2607 + (sysCpu[2] - mSystemStart) * 10
2608 + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10
2609 + " ms and irq took "
2610 + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart)
2611 * 10 + " ms, " + uiInfo;
2612 if (LOGD_ENABLED) {
2613 Log.d(LOGTAG, performanceString + "\nWebpage: " + url);
2614 }
2615 if (url != null) {
2616 // strip the url to maintain consistency
2617 String newUrl = new String(url);
2618 if (newUrl.startsWith("http://www.")) {
2619 newUrl = newUrl.substring(11);
2620 } else if (newUrl.startsWith("http://")) {
2621 newUrl = newUrl.substring(7);
2622 } else if (newUrl.startsWith("https://www.")) {
2623 newUrl = newUrl.substring(12);
2624 } else if (newUrl.startsWith("https://")) {
2625 newUrl = newUrl.substring(8);
2626 }
2627 if (LOGD_ENABLED) {
2628 Log.d(LOGTAG, newUrl + " loaded");
2629 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002630 }
2631 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002632 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002633
Grace Kloba22ac16e2009-10-07 18:00:23 -07002634 if (mInTrace) {
2635 mInTrace = false;
2636 Debug.stopMethodTracing();
2637 }
2638 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002639
Grace Klobae7fe26b2010-06-28 16:23:33 -07002640 private void closeEmptyChildTab() {
2641 Tab current = mTabControl.getCurrentTab();
2642 if (current != null
2643 && current.getWebView().copyBackForwardList().getSize() == 0) {
2644 Tab parent = current.getParentTab();
2645 if (parent != null) {
2646 switchToTab(mTabControl.getTabIndex(parent));
2647 closeTab(current);
2648 }
2649 }
2650 }
2651
Grace Kloba22ac16e2009-10-07 18:00:23 -07002652 boolean shouldOverrideUrlLoading(WebView view, String url) {
2653 if (url.startsWith(SCHEME_WTAI)) {
2654 // wtai://wp/mc;number
2655 // number=string(phone-number)
2656 if (url.startsWith(SCHEME_WTAI_MC)) {
2657 Intent intent = new Intent(Intent.ACTION_VIEW,
2658 Uri.parse(WebView.SCHEME_TEL +
2659 url.substring(SCHEME_WTAI_MC.length())));
2660 startActivity(intent);
Grace Klobae7fe26b2010-06-28 16:23:33 -07002661 // before leaving BrowserActivity, close the empty child tab.
2662 // If a new tab is created through JavaScript open to load this
2663 // url, we would like to close it as we will load this url in a
2664 // different Activity.
2665 closeEmptyChildTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002666 return true;
2667 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002668 // wtai://wp/sd;dtmf
2669 // dtmf=string(dialstring)
2670 if (url.startsWith(SCHEME_WTAI_SD)) {
2671 // TODO: only send when there is active voice connection
2672 return false;
2673 }
2674 // wtai://wp/ap;number;name
2675 // number=string(phone-number)
2676 // name=string
2677 if (url.startsWith(SCHEME_WTAI_AP)) {
2678 // TODO
2679 return false;
2680 }
2681 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002682
Grace Kloba22ac16e2009-10-07 18:00:23 -07002683 // The "about:" schemes are internal to the browser; don't want these to
2684 // be dispatched to other apps.
2685 if (url.startsWith("about:")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002686 return false;
2687 }
2688
Grace Kloba22ac16e2009-10-07 18:00:23 -07002689 Intent intent;
2690 // perform generic parsing of the URI to turn it into an Intent.
2691 try {
2692 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
2693 } catch (URISyntaxException ex) {
2694 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
2695 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002696 }
2697
Grace Kloba22ac16e2009-10-07 18:00:23 -07002698 // check whether the intent can be resolved. If not, we will see
2699 // whether we can download it from the Market.
2700 if (getPackageManager().resolveActivity(intent, 0) == null) {
2701 String packagename = intent.getPackage();
2702 if (packagename != null) {
2703 intent = new Intent(Intent.ACTION_VIEW, Uri
2704 .parse("market://search?q=pname:" + packagename));
2705 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2706 startActivity(intent);
Grace Klobae7fe26b2010-06-28 16:23:33 -07002707 // before leaving BrowserActivity, close the empty child tab.
2708 // If a new tab is created through JavaScript open to load this
2709 // url, we would like to close it as we will load this url in a
2710 // different Activity.
2711 closeEmptyChildTab();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002712 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002713 } else {
2714 return false;
2715 }
2716 }
2717
Grace Kloba22ac16e2009-10-07 18:00:23 -07002718 // sanitize the Intent, ensuring web pages can not bypass browser
2719 // security (only access to BROWSABLE activities).
2720 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2721 intent.setComponent(null);
2722 try {
2723 if (startActivityIfNeeded(intent, -1)) {
Grace Klobae7fe26b2010-06-28 16:23:33 -07002724 // before leaving BrowserActivity, close the empty child tab.
2725 // If a new tab is created through JavaScript open to load this
2726 // url, we would like to close it as we will load this url in a
2727 // different Activity.
2728 closeEmptyChildTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002729 return true;
2730 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002731 } catch (ActivityNotFoundException ex) {
2732 // ignore the error. If no application can handle the URL,
2733 // eg about:blank, assume the browser can handle it.
2734 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002735
Grace Kloba22ac16e2009-10-07 18:00:23 -07002736 if (mMenuIsDown) {
2737 openTab(url);
2738 closeOptionsMenu();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002739 return true;
2740 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002741 return false;
2742 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002743
Grace Kloba22ac16e2009-10-07 18:00:23 -07002744 // -------------------------------------------------------------------------
2745 // Helper function for WebChromeClient
2746 // -------------------------------------------------------------------------
The Android Open Source Project0c908882009-03-03 19:32:16 -08002747
Grace Kloba22ac16e2009-10-07 18:00:23 -07002748 void onProgressChanged(WebView view, int newProgress) {
Leon Scrogginsd746a942010-05-19 13:21:44 -04002749 if (mXLargeScreenSize) {
2750 mTitleBar.setProgress(newProgress);
2751 } else {
2752 // On the phone, the fake title bar will always cover up the
2753 // regular title bar (or the regular one is offscreen), so only the
2754 // fake title bar needs to change its progress
2755 mFakeTitleBar.setProgress(newProgress);
2756 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002757
Grace Kloba22ac16e2009-10-07 18:00:23 -07002758 if (newProgress == 100) {
2759 // onProgressChanged() may continue to be called after the main
2760 // frame has finished loading, as any remaining sub frames continue
2761 // to load. We'll only get called once though with newProgress as
2762 // 100 when everything is loaded. (onPageFinished is called once
2763 // when the main frame completes loading regardless of the state of
2764 // any sub frames so calls to onProgressChanges may continue after
2765 // onPageFinished has executed)
2766 if (mInLoad) {
2767 mInLoad = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -04002768 updateInLoadMenuItems();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002769 // If the options menu is open, leave the title bar
2770 if (!mOptionsMenuOpen || !mIconView) {
2771 hideFakeTitleBar();
The Android Open Source Projectcb9a0bb2009-03-11 12:11:58 -07002772 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002773 }
Leon Scrogginsaa37e7f2010-03-05 11:51:20 -05002774 } else {
2775 if (!mInLoad) {
2776 // onPageFinished may have already been called but a subframe is
2777 // still loading and updating the progress. Reset mInLoad and
2778 // update the menu items.
2779 mInLoad = true;
2780 updateInLoadMenuItems();
2781 }
2782 // When the page first begins to load, the Activity may still be
2783 // paused, in which case showFakeTitleBar will do nothing. Call
2784 // again as the page continues to load so that it will be shown.
2785 // (Calling it will the fake title bar is already showing will also
2786 // do nothing.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002787 if (!mOptionsMenuOpen || mIconView) {
2788 // This page has begun to load, so show the title bar
2789 showFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002790 }
2791 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002792 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002793
Grace Kloba22ac16e2009-10-07 18:00:23 -07002794 void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002795 // if a view already exists then immediately terminate the new one
2796 if (mCustomView != null) {
2797 callback.onCustomViewHidden();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002798 return;
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002799 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002800
2801 // Add the custom view to its container.
2802 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
2803 mCustomView = view;
2804 mCustomViewCallback = callback;
2805 // Save the menu state and set it to empty while the custom
2806 // view is showing.
2807 mOldMenuState = mMenuState;
2808 mMenuState = EMPTY_MENU;
2809 // Hide the content view.
2810 mContentView.setVisibility(View.GONE);
2811 // Finally show the custom view container.
Andrei Popescu163ab742009-10-20 17:58:23 +01002812 setStatusBarVisibility(false);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002813 mCustomViewContainer.setVisibility(View.VISIBLE);
2814 mCustomViewContainer.bringToFront();
2815 }
2816
2817 void onHideCustomView() {
2818 if (mCustomView == null)
2819 return;
2820
2821 // Hide the custom view.
2822 mCustomView.setVisibility(View.GONE);
2823 // Remove the custom view from its container.
2824 mCustomViewContainer.removeView(mCustomView);
2825 mCustomView = null;
2826 // Reset the old menu state.
2827 mMenuState = mOldMenuState;
2828 mOldMenuState = EMPTY_MENU;
2829 mCustomViewContainer.setVisibility(View.GONE);
2830 mCustomViewCallback.onCustomViewHidden();
2831 // Show the content view.
Andrei Popescu163ab742009-10-20 17:58:23 +01002832 setStatusBarVisibility(true);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002833 mContentView.setVisibility(View.VISIBLE);
2834 }
2835
2836 Bitmap getDefaultVideoPoster() {
2837 if (mDefaultVideoPoster == null) {
2838 mDefaultVideoPoster = BitmapFactory.decodeResource(
2839 getResources(), R.drawable.default_video_poster);
Patrick Scott3918d442009-08-04 13:22:29 -04002840 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002841 return mDefaultVideoPoster;
2842 }
Patrick Scott3918d442009-08-04 13:22:29 -04002843
Grace Kloba22ac16e2009-10-07 18:00:23 -07002844 View getVideoLoadingProgressView() {
2845 if (mVideoProgressView == null) {
2846 LayoutInflater inflater = LayoutInflater.from(BrowserActivity.this);
2847 mVideoProgressView = inflater.inflate(
2848 R.layout.video_loading_progress, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002849 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002850 return mVideoProgressView;
2851 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002852
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002853 /*
2854 * The Object used to inform the WebView of the file to upload.
2855 */
2856 private ValueCallback<Uri> mUploadMessage;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002857 private String mCameraFilePath;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002858
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002859 void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
2860
2861 final String imageMimeType = "image/*";
Ben Murdoch6af492a2010-06-15 12:38:17 +01002862 final String videoMimeType = "video/*";
2863 final String mediaSourceKey = "source";
2864 final String mediaSourceValueCamera = "camera";
2865 final String mediaSourceValueGallery = "gallery";
2866 final String mediaSourceValueCamcorder = "camcorder";
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002867
Ben Murdoch6af492a2010-06-15 12:38:17 +01002868 // media source can be 'gallery' or 'camera' or 'camcorder'
2869 String mediaSource = "";
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002870
Ben Murdoch6af492a2010-06-15 12:38:17 +01002871 // We add the camera intent if there was no accept type (or '*/*' or 'image/*').
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002872 boolean addCameraIntent = true;
Ben Murdoch6af492a2010-06-15 12:38:17 +01002873 // We add the camcorder intent if there was no accept type (or '*/*' or 'video/*').
2874 boolean addCamcorderIntent = true;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002875
Ben Murdoch6af492a2010-06-15 12:38:17 +01002876 if (mUploadMessage != null) {
2877 // Already a file picker operation in progress.
2878 return;
2879 }
2880
Grace Kloba22ac16e2009-10-07 18:00:23 -07002881 mUploadMessage = uploadMsg;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002882
2883 // Parse the accept type.
2884 String params[] = acceptType.split(";");
2885 String mimeType = params[0];
2886
2887 for (String p : params) {
2888 String[] keyValue = p.split("=");
2889 if (keyValue.length == 2) {
2890 // Process key=value parameters.
Ben Murdoch6af492a2010-06-15 12:38:17 +01002891 if (mediaSourceKey.equals(keyValue[0])) {
2892 mediaSource = keyValue[1];
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002893 }
2894 }
2895 }
2896
2897 // This intent will display the standard OPENABLE file picker.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002898 Intent i = new Intent(Intent.ACTION_GET_CONTENT);
2899 i.addCategory(Intent.CATEGORY_OPENABLE);
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002900
2901 // Create an intent to add to the standard file picker that will
2902 // capture an image from the camera. We'll combine this intent with
2903 // the standard OPENABLE picker unless the web developer specifically
2904 // requested the camera or gallery be opened by passing a parameter
2905 // in the accept type.
2906 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
2907 File externalDataDir = Environment.getExternalStoragePublicDirectory(
2908 Environment.DIRECTORY_DCIM);
2909 File cameraDataDir = new File(externalDataDir.getAbsolutePath() +
2910 File.separator + "browser-photos");
2911 cameraDataDir.mkdirs();
2912 mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator +
2913 System.currentTimeMillis() + ".jpg";
2914 cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCameraFilePath)));
2915
Ben Murdoch6af492a2010-06-15 12:38:17 +01002916 Intent camcorderIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
2917
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002918 if (mimeType.equals(imageMimeType)) {
2919 i.setType(imageMimeType);
Ben Murdoch6af492a2010-06-15 12:38:17 +01002920 addCamcorderIntent = false;
2921 if (mediaSource.equals(mediaSourceValueCamera)) {
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002922 // Specified 'image/*' and requested the camera, so go ahead and launch the camera
2923 // directly.
2924 BrowserActivity.this.startActivityForResult(cameraIntent, FILE_SELECTED);
2925 return;
Ben Murdoch6af492a2010-06-15 12:38:17 +01002926 } else if (mediaSource.equals(mediaSourceValueGallery)) {
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002927 // Specified gallery as the source, so don't want to consider the camera.
2928 addCameraIntent = false;
2929 }
Ben Murdoch6af492a2010-06-15 12:38:17 +01002930 } else if (mimeType.equals(videoMimeType)) {
2931 i.setType(videoMimeType);
2932 addCameraIntent = false;
2933 // The camcorder saves it's own file and returns it to us in the intent, so
2934 // we don't need to generate one here.
2935 mCameraFilePath = null;
2936
2937 if (mediaSource.equals(mediaSourceValueCamcorder)) {
2938 // Specified 'video/*' and requested the camcorder, so go ahead and launch the camcorder
2939 // directly.
2940 BrowserActivity.this.startActivityForResult(camcorderIntent, FILE_SELECTED);
2941 return;
2942 } else if (mediaSource.equals(mediaSourceValueGallery)) {
2943 // Specified gallery as the source, so don't want to consider the camcorder.
2944 addCamcorderIntent = false;
2945 }
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002946 } else {
2947 i.setType("*/*");
2948 }
2949
Ben Murdoch6af492a2010-06-15 12:38:17 +01002950 // Combine the chooser and the extra choices (like camera or camcorder)
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002951 Intent chooser = new Intent(Intent.ACTION_CHOOSER);
2952 chooser.putExtra(Intent.EXTRA_INTENT, i);
2953
Ben Murdoch6af492a2010-06-15 12:38:17 +01002954 Vector<Intent> extraInitialIntents = new Vector<Intent>(0);
2955
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002956 if (addCameraIntent) {
Ben Murdoch6af492a2010-06-15 12:38:17 +01002957 extraInitialIntents.add(cameraIntent);
2958 }
2959
2960 if (addCamcorderIntent) {
2961 extraInitialIntents.add(camcorderIntent);
2962 }
2963
2964 if (extraInitialIntents.size() > 0) {
2965 Intent[] extraIntents = new Intent[extraInitialIntents.size()];
2966 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraInitialIntents.toArray(extraIntents));
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002967 }
2968
2969 chooser.putExtra(Intent.EXTRA_TITLE, getString(R.string.choose_upload));
2970 BrowserActivity.this.startActivityForResult(chooser, FILE_SELECTED);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002971 }
2972
2973 // -------------------------------------------------------------------------
2974 // Implement functions for DownloadListener
2975 // -------------------------------------------------------------------------
2976
The Android Open Source Project0c908882009-03-03 19:32:16 -08002977 /**
2978 * Notify the host application a download should be done, or that
2979 * the data should be streamed if a streaming viewer is available.
2980 * @param url The full url to the content that should be downloaded
2981 * @param contentDisposition Content-disposition http header, if
2982 * present.
2983 * @param mimetype The mimetype of the content reported by the server
2984 * @param contentLength The file size reported by the server
2985 */
2986 public void onDownloadStart(String url, String userAgent,
2987 String contentDisposition, String mimetype, long contentLength) {
2988 // if we're dealing wih A/V content that's not explicitly marked
2989 // for download, check if it's streamable.
2990 if (contentDisposition == null
Patrick Scotte1fb9662009-08-31 14:31:52 -04002991 || !contentDisposition.regionMatches(
2992 true, 0, "attachment", 0, 10)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002993 // query the package manager to see if there's a registered handler
2994 // that matches.
2995 Intent intent = new Intent(Intent.ACTION_VIEW);
2996 intent.setDataAndType(Uri.parse(url), mimetype);
Patrick Scotte1fb9662009-08-31 14:31:52 -04002997 ResolveInfo info = getPackageManager().resolveActivity(intent,
2998 PackageManager.MATCH_DEFAULT_ONLY);
2999 if (info != null) {
3000 ComponentName myName = getComponentName();
3001 // If we resolved to ourselves, we don't want to attempt to
3002 // load the url only to try and download it again.
3003 if (!myName.getPackageName().equals(
3004 info.activityInfo.packageName)
3005 || !myName.getClassName().equals(
3006 info.activityInfo.name)) {
3007 // someone (other than us) knows how to handle this mime
3008 // type with this scheme, don't download.
3009 try {
3010 startActivity(intent);
3011 return;
3012 } catch (ActivityNotFoundException ex) {
3013 if (LOGD_ENABLED) {
3014 Log.d(LOGTAG, "activity not found for " + mimetype
3015 + " over " + Uri.parse(url).getScheme(),
3016 ex);
3017 }
3018 // Best behavior is to fall back to a download in this
3019 // case
The Android Open Source Project0c908882009-03-03 19:32:16 -08003020 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003021 }
3022 }
3023 }
3024 onDownloadStartNoStream(url, userAgent, contentDisposition, mimetype, contentLength);
3025 }
3026
Kristian Monsenfa52d172010-03-25 18:29:21 +00003027 // This is to work around the fact that java.net.URI throws Exceptions
3028 // instead of just encoding URL's properly
3029 // Helper method for onDownloadStartNoStream
3030 private static String encodePath(String path) {
3031 char[] chars = path.toCharArray();
3032
3033 boolean needed = false;
3034 for (char c : chars) {
3035 if (c == '[' || c == ']') {
3036 needed = true;
3037 break;
3038 }
3039 }
3040 if (needed == false) {
3041 return path;
3042 }
3043
3044 StringBuilder sb = new StringBuilder("");
3045 for (char c : chars) {
3046 if (c == '[' || c == ']') {
3047 sb.append('%');
3048 sb.append(Integer.toHexString(c));
3049 } else {
3050 sb.append(c);
3051 }
3052 }
3053
3054 return sb.toString();
3055 }
3056
The Android Open Source Project0c908882009-03-03 19:32:16 -08003057 /**
3058 * Notify the host application a download should be done, even if there
3059 * is a streaming viewer available for thise type.
3060 * @param url The full url to the content that should be downloaded
3061 * @param contentDisposition Content-disposition http header, if
3062 * present.
3063 * @param mimetype The mimetype of the content reported by the server
3064 * @param contentLength The file size reported by the server
3065 */
3066 /*package */ void onDownloadStartNoStream(String url, String userAgent,
3067 String contentDisposition, String mimetype, long contentLength) {
3068
3069 String filename = URLUtil.guessFileName(url,
3070 contentDisposition, mimetype);
3071
3072 // Check to see if we have an SDCard
3073 String status = Environment.getExternalStorageState();
3074 if (!status.equals(Environment.MEDIA_MOUNTED)) {
3075 int title;
3076 String msg;
3077
3078 // Check to see if the SDCard is busy, same as the music app
3079 if (status.equals(Environment.MEDIA_SHARED)) {
3080 msg = getString(R.string.download_sdcard_busy_dlg_msg);
3081 title = R.string.download_sdcard_busy_dlg_title;
3082 } else {
3083 msg = getString(R.string.download_no_sdcard_dlg_msg, filename);
3084 title = R.string.download_no_sdcard_dlg_title;
3085 }
3086
3087 new AlertDialog.Builder(this)
3088 .setTitle(title)
3089 .setIcon(android.R.drawable.ic_dialog_alert)
3090 .setMessage(msg)
3091 .setPositiveButton(R.string.ok, null)
3092 .show();
3093 return;
3094 }
3095
Kristian Monsenfa52d172010-03-25 18:29:21 +00003096 // java.net.URI is a lot stricter than KURL so we have to encode some
3097 // extra characters. Fix for b 2538060 and b 1634719
3098 WebAddress webAddress;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003099 try {
Kristian Monsenfa52d172010-03-25 18:29:21 +00003100 webAddress = new WebAddress(url);
3101 webAddress.mPath = encodePath(webAddress.mPath);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003102 } catch (Exception e) {
Kristian Monsenfa52d172010-03-25 18:29:21 +00003103 // This only happens for very bad urls, we want to chatch the
3104 // exception here
3105 Log.e(LOGTAG, "Exception trying to parse url:" + url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003106 return;
3107 }
3108
3109 // XXX: Have to use the old url since the cookies were stored using the
3110 // old percent-encoded url.
3111 String cookies = CookieManager.getInstance().getCookie(url);
3112
3113 ContentValues values = new ContentValues();
Kristian Monsenfa52d172010-03-25 18:29:21 +00003114 values.put(Downloads.Impl.COLUMN_URI, webAddress.toString());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003115 values.put(Downloads.Impl.COLUMN_COOKIE_DATA, cookies);
3116 values.put(Downloads.Impl.COLUMN_USER_AGENT, userAgent);
3117 values.put(Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003118 getPackageName());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003119 values.put(Downloads.Impl.COLUMN_NOTIFICATION_CLASS,
Leon Scrogginsa563d092010-04-19 16:53:49 -04003120 OpenDownloadReceiver.class.getCanonicalName());
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003121 values.put(Downloads.Impl.COLUMN_VISIBILITY,
3122 Downloads.Impl.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
3123 values.put(Downloads.Impl.COLUMN_MIME_TYPE, mimetype);
3124 values.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, filename);
Kristian Monsenfa52d172010-03-25 18:29:21 +00003125 values.put(Downloads.Impl.COLUMN_DESCRIPTION, webAddress.mHost);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003126 if (contentLength > 0) {
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003127 values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, contentLength);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003128 }
3129 if (mimetype == null) {
3130 // We must have long pressed on a link or image to download it. We
3131 // are not sure of the mimetype in this case, so do a head request
3132 new FetchUrlMimeType(this).execute(values);
3133 } else {
3134 final Uri contentUri =
Jean-Baptiste Queru1e5bad92010-01-14 16:09:03 -08003135 getContentResolver().insert(Downloads.Impl.CONTENT_URI, values);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003136 }
Leon Scroggins9191a7f2010-03-18 14:44:44 -04003137 Toast.makeText(this, R.string.download_pending, Toast.LENGTH_SHORT)
3138 .show();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003139 }
3140
Grace Kloba22ac16e2009-10-07 18:00:23 -07003141 // -------------------------------------------------------------------------
3142
The Android Open Source Project0c908882009-03-03 19:32:16 -08003143 /**
3144 * Resets the lock icon. This method is called when we start a new load and
3145 * know the url to be loaded.
3146 */
3147 private void resetLockIcon(String url) {
3148 // Save the lock-icon state (we revert to it if the load gets cancelled)
Grace Kloba22ac16e2009-10-07 18:00:23 -07003149 mTabControl.getCurrentTab().resetLockIcon(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003150 updateLockIconImage(LOCK_ICON_UNSECURE);
3151 }
3152
The Android Open Source Project0c908882009-03-03 19:32:16 -08003153 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003154 * Update the lock icon to correspond to our latest state.
3155 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003156 private void updateLockIconToLatest() {
Leon Scroggins91f65fc2010-06-29 13:52:39 -04003157 Tab t = mTabControl.getCurrentTab();
3158 if (t != null) {
3159 updateLockIconImage(t.getLockIconType());
3160 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003161 }
3162
3163 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08003164 * Updates the lock-icon image in the title-bar.
3165 */
3166 private void updateLockIconImage(int lockIconType) {
3167 Drawable d = null;
3168 if (lockIconType == LOCK_ICON_SECURE) {
3169 d = mSecLockIcon;
3170 } else if (lockIconType == LOCK_ICON_MIXED) {
3171 d = mMixLockIcon;
3172 }
Leon Scroggins68579392009-09-15 15:31:54 -04003173 mTitleBar.setLock(d);
Leon Scrogginsd746a942010-05-19 13:21:44 -04003174 if (!mXLargeScreenSize) {
3175 mFakeTitleBar.setLock(d);
3176 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003177 }
3178
3179 /**
3180 * Displays a page-info dialog.
3181 * @param tab The tab to show info about
3182 * @param fromShowSSLCertificateOnError The flag that indicates whether
3183 * this dialog was opened from the SSL-certificate-on-error dialog or
3184 * not. This is important, since we need to know whether to return to
3185 * the parent dialog or simply dismiss.
3186 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003187 private void showPageInfo(final Tab tab,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003188 final boolean fromShowSSLCertificateOnError) {
3189 final LayoutInflater factory = LayoutInflater
3190 .from(this);
3191
3192 final View pageInfoView = factory.inflate(R.layout.page_info, null);
3193
3194 final WebView view = tab.getWebView();
3195
3196 String url = null;
3197 String title = null;
3198
3199 if (view == null) {
3200 url = tab.getUrl();
3201 title = tab.getTitle();
3202 } else if (view == mTabControl.getCurrentWebView()) {
3203 // Use the cached title and url if this is the current WebView
3204 url = mUrl;
3205 title = mTitle;
3206 } else {
3207 url = view.getUrl();
3208 title = view.getTitle();
3209 }
3210
3211 if (url == null) {
3212 url = "";
3213 }
3214 if (title == null) {
3215 title = "";
3216 }
3217
3218 ((TextView) pageInfoView.findViewById(R.id.address)).setText(url);
3219 ((TextView) pageInfoView.findViewById(R.id.title)).setText(title);
3220
3221 mPageInfoView = tab;
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05003222 mPageInfoFromShowSSLCertificateOnError = fromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003223
3224 AlertDialog.Builder alertDialogBuilder =
3225 new AlertDialog.Builder(this)
3226 .setTitle(R.string.page_info).setIcon(android.R.drawable.ic_dialog_info)
3227 .setView(pageInfoView)
3228 .setPositiveButton(
3229 R.string.ok,
3230 new DialogInterface.OnClickListener() {
3231 public void onClick(DialogInterface dialog,
3232 int whichButton) {
3233 mPageInfoDialog = null;
3234 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003235
3236 // if we came here from the SSL error dialog
3237 if (fromShowSSLCertificateOnError) {
3238 // go back to the SSL error dialog
3239 showSSLCertificateOnError(
3240 mSSLCertificateOnErrorView,
3241 mSSLCertificateOnErrorHandler,
3242 mSSLCertificateOnErrorError);
3243 }
3244 }
3245 })
3246 .setOnCancelListener(
3247 new DialogInterface.OnCancelListener() {
3248 public void onCancel(DialogInterface dialog) {
3249 mPageInfoDialog = null;
3250 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003251
3252 // if we came here from the SSL error dialog
3253 if (fromShowSSLCertificateOnError) {
3254 // go back to the SSL error dialog
3255 showSSLCertificateOnError(
3256 mSSLCertificateOnErrorView,
3257 mSSLCertificateOnErrorHandler,
3258 mSSLCertificateOnErrorError);
3259 }
3260 }
3261 });
3262
3263 // if we have a main top-level page SSL certificate set or a certificate
3264 // error
3265 if (fromShowSSLCertificateOnError ||
3266 (view != null && view.getCertificate() != null)) {
3267 // add a 'View Certificate' button
3268 alertDialogBuilder.setNeutralButton(
3269 R.string.view_certificate,
3270 new DialogInterface.OnClickListener() {
3271 public void onClick(DialogInterface dialog,
3272 int whichButton) {
3273 mPageInfoDialog = null;
3274 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003275
3276 // if we came here from the SSL error dialog
3277 if (fromShowSSLCertificateOnError) {
3278 // go back to the SSL error dialog
3279 showSSLCertificateOnError(
3280 mSSLCertificateOnErrorView,
3281 mSSLCertificateOnErrorHandler,
3282 mSSLCertificateOnErrorError);
3283 } else {
3284 // otherwise, display the top-most certificate from
3285 // the chain
3286 if (view.getCertificate() != null) {
3287 showSSLCertificate(tab);
3288 }
3289 }
3290 }
3291 });
3292 }
3293
3294 mPageInfoDialog = alertDialogBuilder.show();
3295 }
3296
3297 /**
3298 * Displays the main top-level page SSL certificate dialog
3299 * (accessible from the Page-Info dialog).
3300 * @param tab The tab to show certificate for.
3301 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003302 private void showSSLCertificate(final Tab tab) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003303 final View certificateView =
3304 inflateCertificateView(tab.getWebView().getCertificate());
3305 if (certificateView == null) {
3306 return;
3307 }
3308
3309 LayoutInflater factory = LayoutInflater.from(this);
3310
3311 final LinearLayout placeholder =
3312 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3313
3314 LinearLayout ll = (LinearLayout) factory.inflate(
3315 R.layout.ssl_success, placeholder);
3316 ((TextView)ll.findViewById(R.id.success))
3317 .setText(R.string.ssl_certificate_is_valid);
3318
3319 mSSLCertificateView = tab;
3320 mSSLCertificateDialog =
3321 new AlertDialog.Builder(this)
3322 .setTitle(R.string.ssl_certificate).setIcon(
3323 R.drawable.ic_dialog_browser_certificate_secure)
3324 .setView(certificateView)
3325 .setPositiveButton(R.string.ok,
3326 new DialogInterface.OnClickListener() {
3327 public void onClick(DialogInterface dialog,
3328 int whichButton) {
3329 mSSLCertificateDialog = null;
3330 mSSLCertificateView = null;
3331
3332 showPageInfo(tab, false);
3333 }
3334 })
3335 .setOnCancelListener(
3336 new DialogInterface.OnCancelListener() {
3337 public void onCancel(DialogInterface dialog) {
3338 mSSLCertificateDialog = null;
3339 mSSLCertificateView = null;
3340
3341 showPageInfo(tab, false);
3342 }
3343 })
3344 .show();
3345 }
3346
3347 /**
3348 * Displays the SSL error certificate dialog.
3349 * @param view The target web-view.
3350 * @param handler The SSL error handler responsible for cancelling the
3351 * connection that resulted in an SSL error or proceeding per user request.
3352 * @param error The SSL error object.
3353 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003354 void showSSLCertificateOnError(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003355 final WebView view, final SslErrorHandler handler, final SslError error) {
3356
3357 final View certificateView =
3358 inflateCertificateView(error.getCertificate());
3359 if (certificateView == null) {
3360 return;
3361 }
3362
3363 LayoutInflater factory = LayoutInflater.from(this);
3364
3365 final LinearLayout placeholder =
3366 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3367
3368 if (error.hasError(SslError.SSL_UNTRUSTED)) {
3369 LinearLayout ll = (LinearLayout)factory
3370 .inflate(R.layout.ssl_warning, placeholder);
3371 ((TextView)ll.findViewById(R.id.warning))
3372 .setText(R.string.ssl_untrusted);
3373 }
3374
3375 if (error.hasError(SslError.SSL_IDMISMATCH)) {
3376 LinearLayout ll = (LinearLayout)factory
3377 .inflate(R.layout.ssl_warning, placeholder);
3378 ((TextView)ll.findViewById(R.id.warning))
3379 .setText(R.string.ssl_mismatch);
3380 }
3381
3382 if (error.hasError(SslError.SSL_EXPIRED)) {
3383 LinearLayout ll = (LinearLayout)factory
3384 .inflate(R.layout.ssl_warning, placeholder);
3385 ((TextView)ll.findViewById(R.id.warning))
3386 .setText(R.string.ssl_expired);
3387 }
3388
3389 if (error.hasError(SslError.SSL_NOTYETVALID)) {
3390 LinearLayout ll = (LinearLayout)factory
3391 .inflate(R.layout.ssl_warning, placeholder);
3392 ((TextView)ll.findViewById(R.id.warning))
3393 .setText(R.string.ssl_not_yet_valid);
3394 }
3395
3396 mSSLCertificateOnErrorHandler = handler;
3397 mSSLCertificateOnErrorView = view;
3398 mSSLCertificateOnErrorError = error;
3399 mSSLCertificateOnErrorDialog =
3400 new AlertDialog.Builder(this)
3401 .setTitle(R.string.ssl_certificate).setIcon(
3402 R.drawable.ic_dialog_browser_certificate_partially_secure)
3403 .setView(certificateView)
3404 .setPositiveButton(R.string.ok,
3405 new DialogInterface.OnClickListener() {
3406 public void onClick(DialogInterface dialog,
3407 int whichButton) {
3408 mSSLCertificateOnErrorDialog = null;
3409 mSSLCertificateOnErrorView = null;
3410 mSSLCertificateOnErrorHandler = null;
3411 mSSLCertificateOnErrorError = null;
3412
Grace Kloba22ac16e2009-10-07 18:00:23 -07003413 view.getWebViewClient().onReceivedSslError(
3414 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003415 }
3416 })
3417 .setNeutralButton(R.string.page_info_view,
3418 new DialogInterface.OnClickListener() {
3419 public void onClick(DialogInterface dialog,
3420 int whichButton) {
3421 mSSLCertificateOnErrorDialog = null;
3422
3423 // do not clear the dialog state: we will
3424 // need to show the dialog again once the
3425 // user is done exploring the page-info details
3426
3427 showPageInfo(mTabControl.getTabFromView(view),
3428 true);
3429 }
3430 })
3431 .setOnCancelListener(
3432 new DialogInterface.OnCancelListener() {
3433 public void onCancel(DialogInterface dialog) {
3434 mSSLCertificateOnErrorDialog = null;
3435 mSSLCertificateOnErrorView = null;
3436 mSSLCertificateOnErrorHandler = null;
3437 mSSLCertificateOnErrorError = null;
3438
Grace Kloba22ac16e2009-10-07 18:00:23 -07003439 view.getWebViewClient().onReceivedSslError(
3440 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003441 }
3442 })
3443 .show();
3444 }
3445
3446 /**
3447 * Inflates the SSL certificate view (helper method).
3448 * @param certificate The SSL certificate.
3449 * @return The resultant certificate view with issued-to, issued-by,
3450 * issued-on, expires-on, and possibly other fields set.
3451 * If the input certificate is null, returns null.
3452 */
3453 private View inflateCertificateView(SslCertificate certificate) {
3454 if (certificate == null) {
3455 return null;
3456 }
3457
3458 LayoutInflater factory = LayoutInflater.from(this);
3459
3460 View certificateView = factory.inflate(
3461 R.layout.ssl_certificate, null);
3462
3463 // issued to:
3464 SslCertificate.DName issuedTo = certificate.getIssuedTo();
3465 if (issuedTo != null) {
3466 ((TextView) certificateView.findViewById(R.id.to_common))
3467 .setText(issuedTo.getCName());
3468 ((TextView) certificateView.findViewById(R.id.to_org))
3469 .setText(issuedTo.getOName());
3470 ((TextView) certificateView.findViewById(R.id.to_org_unit))
3471 .setText(issuedTo.getUName());
3472 }
3473
3474 // issued by:
3475 SslCertificate.DName issuedBy = certificate.getIssuedBy();
3476 if (issuedBy != null) {
3477 ((TextView) certificateView.findViewById(R.id.by_common))
3478 .setText(issuedBy.getCName());
3479 ((TextView) certificateView.findViewById(R.id.by_org))
3480 .setText(issuedBy.getOName());
3481 ((TextView) certificateView.findViewById(R.id.by_org_unit))
3482 .setText(issuedBy.getUName());
3483 }
3484
3485 // issued on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003486 String issuedOn = formatCertificateDate(
3487 certificate.getValidNotBeforeDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003488 ((TextView) certificateView.findViewById(R.id.issued_on))
3489 .setText(issuedOn);
3490
3491 // expires on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003492 String expiresOn = formatCertificateDate(
3493 certificate.getValidNotAfterDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003494 ((TextView) certificateView.findViewById(R.id.expires_on))
3495 .setText(expiresOn);
3496
3497 return certificateView;
3498 }
3499
3500 /**
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003501 * Formats the certificate date to a properly localized date string.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003502 * @return Properly localized version of the certificate date string and
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003503 * the "" if it fails to localize.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003504 */
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003505 private String formatCertificateDate(Date certificateDate) {
3506 if (certificateDate == null) {
3507 return "";
The Android Open Source Project0c908882009-03-03 19:32:16 -08003508 }
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003509 String formattedDate = DateFormat.getDateFormat(this).format(certificateDate);
3510 if (formattedDate == null) {
3511 return "";
3512 }
3513 return formattedDate;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003514 }
3515
3516 /**
3517 * Displays an http-authentication dialog.
3518 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003519 void showHttpAuthentication(final HttpAuthHandler handler,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003520 final String host, final String realm, final String title,
3521 final String name, final String password, int focusId) {
3522 LayoutInflater factory = LayoutInflater.from(this);
3523 final View v = factory
3524 .inflate(R.layout.http_authentication, null);
3525 if (name != null) {
3526 ((EditText) v.findViewById(R.id.username_edit)).setText(name);
3527 }
3528 if (password != null) {
3529 ((EditText) v.findViewById(R.id.password_edit)).setText(password);
3530 }
3531
3532 String titleText = title;
3533 if (titleText == null) {
3534 titleText = getText(R.string.sign_in_to).toString().replace(
3535 "%s1", host).replace("%s2", realm);
3536 }
3537
3538 mHttpAuthHandler = handler;
3539 AlertDialog dialog = new AlertDialog.Builder(this)
3540 .setTitle(titleText)
3541 .setIcon(android.R.drawable.ic_dialog_alert)
3542 .setView(v)
3543 .setPositiveButton(R.string.action,
3544 new DialogInterface.OnClickListener() {
3545 public void onClick(DialogInterface dialog,
3546 int whichButton) {
3547 String nm = ((EditText) v
3548 .findViewById(R.id.username_edit))
3549 .getText().toString();
3550 String pw = ((EditText) v
3551 .findViewById(R.id.password_edit))
3552 .getText().toString();
3553 BrowserActivity.this.setHttpAuthUsernamePassword
3554 (host, realm, nm, pw);
3555 handler.proceed(nm, pw);
3556 mHttpAuthenticationDialog = null;
3557 mHttpAuthHandler = null;
3558 }})
3559 .setNegativeButton(R.string.cancel,
3560 new DialogInterface.OnClickListener() {
3561 public void onClick(DialogInterface dialog,
3562 int whichButton) {
3563 handler.cancel();
3564 BrowserActivity.this.resetTitleAndRevertLockIcon();
3565 mHttpAuthenticationDialog = null;
3566 mHttpAuthHandler = null;
3567 }})
3568 .setOnCancelListener(new DialogInterface.OnCancelListener() {
3569 public void onCancel(DialogInterface dialog) {
3570 handler.cancel();
3571 BrowserActivity.this.resetTitleAndRevertLockIcon();
3572 mHttpAuthenticationDialog = null;
3573 mHttpAuthHandler = null;
3574 }})
3575 .create();
3576 // Make the IME appear when the dialog is displayed if applicable.
3577 dialog.getWindow().setSoftInputMode(
3578 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
3579 dialog.show();
3580 if (focusId != 0) {
3581 dialog.findViewById(focusId).requestFocus();
3582 } else {
3583 v.findViewById(R.id.username_edit).requestFocus();
3584 }
3585 mHttpAuthenticationDialog = dialog;
3586 }
3587
3588 public int getProgress() {
3589 WebView w = mTabControl.getCurrentWebView();
3590 if (w != null) {
3591 return w.getProgress();
3592 } else {
3593 return 100;
3594 }
3595 }
3596
3597 /**
3598 * Set HTTP authentication password.
3599 *
3600 * @param host The host for the password
3601 * @param realm The realm for the password
3602 * @param username The username for the password. If it is null, it means
3603 * password can't be saved.
3604 * @param password The password
3605 */
3606 public void setHttpAuthUsernamePassword(String host, String realm,
3607 String username,
3608 String password) {
Steve Block95a53b22010-03-25 17:24:58 +00003609 WebView w = getTopWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003610 if (w != null) {
3611 w.setHttpAuthUsernamePassword(host, realm, username, password);
3612 }
3613 }
3614
3615 /**
3616 * connectivity manager says net has come or gone... inform the user
3617 * @param up true if net has come up, false if net has gone down
3618 */
3619 public void onNetworkToggle(boolean up) {
3620 if (up == mIsNetworkUp) {
3621 return;
3622 } else if (up) {
3623 mIsNetworkUp = true;
3624 if (mAlertDialog != null) {
3625 mAlertDialog.cancel();
3626 mAlertDialog = null;
3627 }
3628 } else {
3629 mIsNetworkUp = false;
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003630 if (mInLoad) {
3631 createAndShowNetworkDialog();
3632 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003633 }
3634 WebView w = mTabControl.getCurrentWebView();
3635 if (w != null) {
3636 w.setNetworkAvailable(up);
3637 }
3638 }
3639
Grace Kloba22ac16e2009-10-07 18:00:23 -07003640 boolean isNetworkUp() {
3641 return mIsNetworkUp;
3642 }
3643
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003644 // This method shows the network dialog alerting the user that the net is
3645 // down. It will only show the dialog if mAlertDialog is null.
3646 private void createAndShowNetworkDialog() {
3647 if (mAlertDialog == null) {
3648 mAlertDialog = new AlertDialog.Builder(this)
3649 .setTitle(R.string.loadSuspendedTitle)
3650 .setMessage(R.string.loadSuspended)
3651 .setPositiveButton(R.string.ok, null)
3652 .show();
3653 }
3654 }
3655
The Android Open Source Project0c908882009-03-03 19:32:16 -08003656 @Override
3657 protected void onActivityResult(int requestCode, int resultCode,
3658 Intent intent) {
Grace Klobabb394f32009-11-19 10:26:37 -08003659 if (getTopWindow() == null) return;
3660
The Android Open Source Project0c908882009-03-03 19:32:16 -08003661 switch (requestCode) {
3662 case COMBO_PAGE:
3663 if (resultCode == RESULT_OK && intent != null) {
3664 String data = intent.getAction();
3665 Bundle extras = intent.getExtras();
3666 if (extras != null && extras.getBoolean("new_window", false)) {
Leon Scroggins25d35472009-09-15 11:37:27 -04003667 openTab(data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003668 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -07003669 final Tab currentTab =
The Android Open Source Project0c908882009-03-03 19:32:16 -08003670 mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04003671 dismissSubWindow(currentTab);
3672 if (data != null && data.length() != 0) {
Leon Scroggins92472e82010-02-17 16:32:28 -05003673 loadUrl(getTopWindow(), data);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003674 }
3675 }
3676 }
Leon Scrogginsfde97462010-01-11 13:06:21 -05003677 // Deliberately fall through to PREFERENCES_PAGE, since the
3678 // same extra may be attached to the COMBO_PAGE
3679 case PREFERENCES_PAGE:
3680 if (resultCode == RESULT_OK && intent != null) {
3681 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
3682 if (BrowserSettings.PREF_CLEAR_HISTORY.equals(action)) {
3683 mTabControl.removeParentChildRelationShips();
3684 }
3685 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003686 break;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003687 // Choose a file from the file picker.
3688 case FILE_SELECTED:
3689 if (null == mUploadMessage) break;
3690 Uri result = intent == null || resultCode != RESULT_OK ? null
3691 : intent.getData();
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003692
3693 // As we ask the camera to save the result of the user taking
3694 // a picture, the camera application does not return anything other
3695 // than RESULT_OK. So we need to check whether the file we expected
3696 // was written to disk in the in the case that we
3697 // did not get an intent returned but did get a RESULT_OK. If it was,
3698 // we assume that this result has came back from the camera.
3699 if (result == null && intent == null && resultCode == RESULT_OK) {
3700 File cameraFile = new File(mCameraFilePath);
3701 if (cameraFile.exists()) {
3702 result = Uri.fromFile(cameraFile);
Ben Murdoch07d34732010-05-27 18:34:46 +01003703 // Broadcast to the media scanner that we have a new photo
3704 // so it will be added into the gallery for the user.
3705 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003706 }
3707 }
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003708 mUploadMessage.onReceiveValue(result);
3709 mUploadMessage = null;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003710 mCameraFilePath = null;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003711 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003712 default:
3713 break;
3714 }
Leon Scroggins30444232009-09-04 18:36:20 -04003715 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003716 }
3717
3718 /*
3719 * This method is called as a result of the user selecting the options
Leon Scrogginsf08809b2010-01-21 09:27:46 -05003720 * menu to see the download window. It shows the download window on top of
3721 * the current window.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003722 */
Leon Scrogginsf08809b2010-01-21 09:27:46 -05003723 private void viewDownloads(Uri downloadRecord) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003724 Intent intent = new Intent(this,
3725 BrowserDownloadPage.class);
3726 intent.setData(downloadRecord);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003727 startActivityForResult(intent, BrowserActivity.DOWNLOAD_PAGE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003728
3729 }
3730
Leon Scroggins160a7e72009-08-14 18:28:01 -04003731 /**
3732 * Open the Go page.
3733 * @param startWithHistory If true, open starting on the history tab.
3734 * Otherwise, start with the bookmarks tab.
Leon Scroggins160a7e72009-08-14 18:28:01 -04003735 */
Leon Scroggins30444232009-09-04 18:36:20 -04003736 /* package */ void bookmarksOrHistoryPicker(boolean startWithHistory) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003737 WebView current = mTabControl.getCurrentWebView();
3738 if (current == null) {
3739 return;
3740 }
3741 Intent intent = new Intent(this,
3742 CombinedBookmarkHistoryActivity.class);
3743 String title = current.getTitle();
3744 String url = current.getUrl();
Ben Murdoch87cc65d2010-06-29 20:34:10 +01003745 Bitmap thumbnail = createScreenshot(current, getDesiredThumbnailWidth(this),
3746 getDesiredThumbnailHeight(this));
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003747
The Android Open Source Project0c908882009-03-03 19:32:16 -08003748 // Just in case the user opens bookmarks before a page finishes loading
3749 // so the current history item, and therefore the page, is null.
3750 if (null == url) {
3751 url = mLastEnteredUrl;
3752 // This can happen.
3753 if (null == url) {
3754 url = mSettings.getHomePage();
3755 }
3756 }
3757 // In case the web page has not yet received its associated title.
3758 if (title == null) {
3759 title = url;
3760 }
3761 intent.putExtra("title", title);
3762 intent.putExtra("url", url);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01003763 intent.putExtra("thumbnail", thumbnail);
Leon Scroggins30444232009-09-04 18:36:20 -04003764 // Disable opening in a new window if we have maxed out the windows
Grace Kloba22ac16e2009-10-07 18:00:23 -07003765 intent.putExtra("disable_new_window", !mTabControl.canCreateNewTab());
Patrick Scott3918d442009-08-04 13:22:29 -04003766 intent.putExtra("touch_icon_url", current.getTouchIconUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003767 if (startWithHistory) {
3768 intent.putExtra(CombinedBookmarkHistoryActivity.STARTING_TAB,
3769 CombinedBookmarkHistoryActivity.HISTORY_TAB);
3770 }
3771 startActivityForResult(intent, COMBO_PAGE);
3772 }
3773
3774 // Called when loading from context menu or LOAD_URL message
Leon Scroggins92472e82010-02-17 16:32:28 -05003775 private void loadUrlFromContext(WebView view, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003776 // In case the user enters nothing.
3777 if (url != null && url.length() != 0 && view != null) {
3778 url = smartUrlFilter(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003779 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
Leon Scroggins92472e82010-02-17 16:32:28 -05003780 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003781 }
3782 }
3783 }
3784
Leon Scroggins92472e82010-02-17 16:32:28 -05003785 /**
3786 * Load the URL into the given WebView and update the title bar
3787 * to reflect the new load. Call this instead of WebView.loadUrl
3788 * directly.
3789 * @param view The WebView used to load url.
3790 * @param url The URL to load.
3791 */
3792 private void loadUrl(WebView view, String url) {
3793 updateTitleBarForNewLoad(view, url);
3794 view.loadUrl(url);
3795 }
3796
3797 /**
3798 * Load UrlData into a Tab and update the title bar to reflect the new
3799 * load. Call this instead of UrlData.loadIn directly.
3800 * @param t The Tab used to load.
3801 * @param data The UrlData being loaded.
3802 */
3803 private void loadUrlDataIn(Tab t, UrlData data) {
3804 updateTitleBarForNewLoad(t.getWebView(), data.mUrl);
3805 data.loadIn(t);
3806 }
3807
3808 /**
3809 * If the WebView is the top window, update the title bar to reflect
3810 * loading the new URL. i.e. set its text, clear the favicon (which
3811 * will be set once the page begins loading), and set the progress to
3812 * INITIAL_PROGRESS to show that the page has begun to load. Called
3813 * by loadUrl and loadUrlDataIn.
3814 * @param view The WebView that is starting a load.
3815 * @param url The URL that is being loaded.
3816 */
3817 private void updateTitleBarForNewLoad(WebView view, String url) {
3818 if (view == getTopWindow()) {
3819 setUrlTitle(url, null);
3820 setFavicon(null);
3821 onProgressChanged(view, INITIAL_PROGRESS);
3822 }
3823 }
3824
The Android Open Source Project0c908882009-03-03 19:32:16 -08003825 private String smartUrlFilter(Uri inUri) {
3826 if (inUri != null) {
3827 return smartUrlFilter(inUri.toString());
3828 }
3829 return null;
3830 }
3831
Feng Qianb34f87a2009-03-24 21:27:26 -07003832 protected static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003833 "(?i)" + // switch on case insensitive matching
3834 "(" + // begin group for schema
3835 "(?:http|https|file):\\/\\/" +
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003836 "|(?:inline|data|about|content|javascript):" +
The Android Open Source Project0c908882009-03-03 19:32:16 -08003837 ")" +
3838 "(.*)" );
3839
3840 /**
3841 * Attempts to determine whether user input is a URL or search
3842 * terms. Anything with a space is passed to search.
3843 *
3844 * Converts to lowercase any mistakenly uppercased schema (i.e.,
3845 * "Http://" converts to "http://"
3846 *
3847 * @return Original or modified URL
3848 *
3849 */
3850 String smartUrlFilter(String url) {
3851
3852 String inUrl = url.trim();
3853 boolean hasSpace = inUrl.indexOf(' ') != -1;
3854
3855 Matcher matcher = ACCEPTED_URI_SCHEMA.matcher(inUrl);
3856 if (matcher.matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003857 // force scheme to lowercase
3858 String scheme = matcher.group(1);
3859 String lcScheme = scheme.toLowerCase();
3860 if (!lcScheme.equals(scheme)) {
Mitsuru Oshima123ecfb2009-05-18 19:11:14 -07003861 inUrl = lcScheme + matcher.group(2);
3862 }
3863 if (hasSpace) {
3864 inUrl = inUrl.replace(" ", "%20");
The Android Open Source Project0c908882009-03-03 19:32:16 -08003865 }
3866 return inUrl;
3867 }
3868 if (hasSpace) {
Satish Sampath565505b2009-05-29 15:37:27 +01003869 // FIXME: Is this the correct place to add to searches?
3870 // what if someone else calls this function?
3871 int shortcut = parseUrlShortcut(inUrl);
3872 if (shortcut != SHORTCUT_INVALID) {
3873 Browser.addSearchUrl(mResolver, inUrl);
3874 String query = inUrl.substring(2);
3875 switch (shortcut) {
3876 case SHORTCUT_GOOGLE_SEARCH:
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003877 return URLUtil.composeSearchUrl(query, QuickSearch_G, QUERY_PLACE_HOLDER);
Satish Sampath565505b2009-05-29 15:37:27 +01003878 case SHORTCUT_WIKIPEDIA_SEARCH:
3879 return URLUtil.composeSearchUrl(query, QuickSearch_W, QUERY_PLACE_HOLDER);
3880 case SHORTCUT_DICTIONARY_SEARCH:
3881 return URLUtil.composeSearchUrl(query, QuickSearch_D, QUERY_PLACE_HOLDER);
3882 case SHORTCUT_GOOGLE_MOBILE_LOCAL_SEARCH:
The Android Open Source Project0c908882009-03-03 19:32:16 -08003883 // FIXME: we need location in this case
Satish Sampath565505b2009-05-29 15:37:27 +01003884 return URLUtil.composeSearchUrl(query, QuickSearch_L, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003885 }
3886 }
3887 } else {
Dan Egnor5ee906c2009-11-18 12:11:49 -08003888 if (Patterns.WEB_URL.matcher(inUrl).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003889 return URLUtil.guessUrl(inUrl);
3890 }
3891 }
3892
3893 Browser.addSearchUrl(mResolver, inUrl);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003894 return URLUtil.composeSearchUrl(inUrl, QuickSearch_G, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003895 }
3896
Ben Murdochbff2d602009-07-01 20:19:05 +01003897 /* package */ void setShouldShowErrorConsole(boolean flag) {
3898 if (flag == mShouldShowErrorConsole) {
3899 // Nothing to do.
3900 return;
3901 }
3902
3903 mShouldShowErrorConsole = flag;
3904
Grace Kloba22ac16e2009-10-07 18:00:23 -07003905 ErrorConsoleView errorConsole = mTabControl.getCurrentTab()
3906 .getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01003907
3908 if (flag) {
3909 // Setting the show state of the console will cause it's the layout to be inflated.
3910 if (errorConsole.numberOfErrors() > 0) {
3911 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
3912 } else {
3913 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
3914 }
3915
3916 // Now we can add it to the main view.
3917 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08003918 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01003919 ViewGroup.LayoutParams.WRAP_CONTENT));
3920 } else {
3921 mErrorConsoleContainer.removeView(errorConsole);
3922 }
3923
3924 }
3925
Grace Kloba22ac16e2009-10-07 18:00:23 -07003926 boolean shouldShowErrorConsole() {
3927 return mShouldShowErrorConsole;
3928 }
3929
Andrei Popescu163ab742009-10-20 17:58:23 +01003930 private void setStatusBarVisibility(boolean visible) {
3931 int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
3932 getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
3933 }
3934
Andrei Popescu56199cc2010-01-12 22:39:16 +00003935
3936 private void sendNetworkType(String type, String subtype) {
3937 WebView w = mTabControl.getCurrentWebView();
3938 if (w != null) {
3939 w.setNetworkType(type, subtype);
3940 }
3941 }
3942
Andrei Popescu30995e72010-02-09 16:59:58 +00003943 private void packageChanged(String packageName, boolean wasAdded) {
3944 WebView w = mTabControl.getCurrentWebView();
3945 if (w == null) {
3946 return;
3947 }
3948
3949 if (wasAdded) {
3950 w.addPackageName(packageName);
3951 } else {
3952 w.removePackageName(packageName);
3953 }
3954 }
3955
3956 private void addPackageNames(Set<String> packageNames) {
3957 WebView w = mTabControl.getCurrentWebView();
3958 if (w == null) {
3959 return;
3960 }
3961
3962 w.addPackageNames(packageNames);
3963 }
3964
3965 private void getInstalledPackages() {
3966 AsyncTask<Void, Void, Set<String> > task =
3967 new AsyncTask<Void, Void, Set<String> >() {
Michael Kolbe0a36662010-06-29 10:37:12 -07003968 @Override
Andrei Popescu30995e72010-02-09 16:59:58 +00003969 protected Set<String> doInBackground(Void... unused) {
3970 Set<String> installedPackages = new HashSet<String>();
3971 PackageManager pm = BrowserActivity.this.getPackageManager();
3972 if (pm != null) {
3973 List<PackageInfo> packages = pm.getInstalledPackages(0);
3974 for (PackageInfo p : packages) {
3975 if (BrowserActivity.this.sGoogleApps.contains(p.packageName)) {
3976 installedPackages.add(p.packageName);
3977 }
3978 }
3979 }
3980
3981 return installedPackages;
3982 }
3983
3984 // Executes on the UI thread
Michael Kolbe0a36662010-06-29 10:37:12 -07003985 @Override
Andrei Popescu30995e72010-02-09 16:59:58 +00003986 protected void onPostExecute(Set<String> installedPackages) {
3987 addPackageNames(installedPackages);
3988 }
3989 };
3990 task.execute();
3991 }
3992
Grace Klobaeb6eef42009-09-15 17:56:32 -07003993 final static int LOCK_ICON_UNSECURE = 0;
3994 final static int LOCK_ICON_SECURE = 1;
3995 final static int LOCK_ICON_MIXED = 2;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003996
The Android Open Source Project0c908882009-03-03 19:32:16 -08003997 private BrowserSettings mSettings;
3998 private TabControl mTabControl;
3999 private ContentResolver mResolver;
4000 private FrameLayout mContentView;
Andrei Popescuadc008d2009-06-26 14:11:30 +01004001 private View mCustomView;
4002 private FrameLayout mCustomViewContainer;
Andrei Popescuc9b55562009-07-07 10:51:15 +01004003 private WebChromeClient.CustomViewCallback mCustomViewCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004004
4005 // FIXME, temp address onPrepareMenu performance problem. When we move everything out of
4006 // view, we should rewrite this.
4007 private int mCurrentMenuState = 0;
4008 private int mMenuState = R.id.MAIN_MENU;
Andrei Popescuadc008d2009-06-26 14:11:30 +01004009 private int mOldMenuState = EMPTY_MENU;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004010 private static final int EMPTY_MENU = -1;
4011 private Menu mMenu;
4012
4013 private FindDialog mFindDialog;
Cary Clark01cfcdd2010-06-04 16:36:45 -04004014 private SelectDialog mSelectDialog;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004015 // Used to prevent chording to result in firing two shortcuts immediately
4016 // one after another. Fixes bug 1211714.
4017 boolean mCanChord;
4018
4019 private boolean mInLoad;
4020 private boolean mIsNetworkUp;
Ben Murdochb7cc8b42009-09-28 10:59:09 +01004021 private boolean mDidStopLoad;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004022
Cary Clark1f10cbf2010-03-22 11:45:23 -04004023 /* package */ boolean mActivityInPause = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004024
4025 private boolean mMenuIsDown;
4026
The Android Open Source Project0c908882009-03-03 19:32:16 -08004027 private static boolean mInTrace;
4028
4029 // Performance probe
4030 private static final int[] SYSTEM_CPU_FORMAT = new int[] {
4031 Process.PROC_SPACE_TERM | Process.PROC_COMBINE,
4032 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time
4033 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time
4034 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time
4035 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time
4036 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time
4037 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time
4038 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG // 7: softirq time
4039 };
4040
4041 private long mStart;
4042 private long mProcessStart;
4043 private long mUserStart;
4044 private long mSystemStart;
4045 private long mIdleStart;
4046 private long mIrqStart;
4047
4048 private long mUiStart;
4049
4050 private Drawable mMixLockIcon;
4051 private Drawable mSecLockIcon;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004052
4053 /* hold a ref so we can auto-cancel if necessary */
4054 private AlertDialog mAlertDialog;
4055
The Android Open Source Project0c908882009-03-03 19:32:16 -08004056 // The up-to-date URL and title (these can be different from those stored
4057 // in WebView, since it takes some time for the information in WebView to
4058 // get updated)
4059 private String mUrl;
4060 private String mTitle;
4061
4062 // As PageInfo has different style for landscape / portrait, we have
4063 // to re-open it when configuration changed
4064 private AlertDialog mPageInfoDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07004065 private Tab mPageInfoView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004066 // If the Page-Info dialog is launched from the SSL-certificate-on-error
4067 // dialog, we should not just dismiss it, but should get back to the
4068 // SSL-certificate-on-error dialog. This flag is used to store this state
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05004069 private boolean mPageInfoFromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004070
4071 // as SSLCertificateOnError has different style for landscape / portrait,
4072 // we have to re-open it when configuration changed
4073 private AlertDialog mSSLCertificateOnErrorDialog;
4074 private WebView mSSLCertificateOnErrorView;
4075 private SslErrorHandler mSSLCertificateOnErrorHandler;
4076 private SslError mSSLCertificateOnErrorError;
4077
4078 // as SSLCertificate has different style for landscape / portrait, we
4079 // have to re-open it when configuration changed
4080 private AlertDialog mSSLCertificateDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07004081 private Tab mSSLCertificateView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004082
4083 // as HttpAuthentication has different style for landscape / portrait, we
4084 // have to re-open it when configuration changed
4085 private AlertDialog mHttpAuthenticationDialog;
4086 private HttpAuthHandler mHttpAuthHandler;
4087
4088 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
4089 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08004090 ViewGroup.LayoutParams.MATCH_PARENT,
4091 ViewGroup.LayoutParams.MATCH_PARENT);
Andrei Popescuadc008d2009-06-26 14:11:30 +01004092 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
4093 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08004094 ViewGroup.LayoutParams.MATCH_PARENT,
4095 ViewGroup.LayoutParams.MATCH_PARENT,
Andrei Popescuadc008d2009-06-26 14:11:30 +01004096 Gravity.CENTER);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07004097 // Google search
4098 final static String QuickSearch_G = "http://www.google.com/m?q=%s";
The Android Open Source Project0c908882009-03-03 19:32:16 -08004099 // Wikipedia search
4100 final static String QuickSearch_W = "http://en.wikipedia.org/w/index.php?search=%s&go=Go";
4101 // Dictionary search
4102 final static String QuickSearch_D = "http://dictionary.reference.com/search?q=%s";
4103 // Google Mobile Local search
4104 final static String QuickSearch_L = "http://www.google.com/m/search?site=local&q=%s&near=mountain+view";
4105
4106 final static String QUERY_PLACE_HOLDER = "%s";
4107
4108 // "source" parameter for Google search through search key
4109 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
4110 // "source" parameter for Google search through goto menu
4111 final static String GOOGLE_SEARCH_SOURCE_GOTO = "browser-goto";
4112 // "source" parameter for Google search through simplily type
4113 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
4114 // "source" parameter for Google search suggested by the browser
4115 final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
4116 // "source" parameter for Google search from unknown source
4117 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown";
4118
4119 private final static String LOGTAG = "browser";
4120
The Android Open Source Project0c908882009-03-03 19:32:16 -08004121 private String mLastEnteredUrl;
4122
4123 private PowerManager.WakeLock mWakeLock;
4124 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
4125
4126 private Toast mStopToast;
4127
Leon Scroggins571b3762010-05-26 10:25:01 -04004128 private TitleBarBase mTitleBar;
Leon Scroggins81db3662009-06-04 17:45:11 -04004129
Ben Murdochbff2d602009-07-01 20:19:05 +01004130 private LinearLayout mErrorConsoleContainer = null;
4131 private boolean mShouldShowErrorConsole = false;
4132
The Android Open Source Project0c908882009-03-03 19:32:16 -08004133 // As the ids are dynamically created, we can't guarantee that they will
4134 // be in sequence, so this static array maps ids to a window number.
4135 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
4136 { R.id.window_one_menu_id, R.id.window_two_menu_id, R.id.window_three_menu_id,
4137 R.id.window_four_menu_id, R.id.window_five_menu_id, R.id.window_six_menu_id,
4138 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
4139
4140 // monitor platform changes
4141 private IntentFilter mNetworkStateChangedFilter;
4142 private BroadcastReceiver mNetworkStateIntentReceiver;
4143
Grace Klobab4da0ad2009-05-14 14:45:40 -07004144 private BroadcastReceiver mPackageInstallationReceiver;
4145
Bjorn Bringerta7611812010-03-24 11:12:02 +00004146 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
4147
The Android Open Source Project0c908882009-03-03 19:32:16 -08004148 // activity requestCode
Nicolas Roard78a98e42009-05-11 13:34:17 +01004149 final static int COMBO_PAGE = 1;
4150 final static int DOWNLOAD_PAGE = 2;
4151 final static int PREFERENCES_PAGE = 3;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04004152 final static int FILE_SELECTED = 4;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004153
Andrei Popescu540035d2009-09-18 18:59:20 +01004154 // the default <video> poster
4155 private Bitmap mDefaultVideoPoster;
4156 // the video progress view
4157 private View mVideoProgressView;
4158
Andrei Popescu30995e72010-02-09 16:59:58 +00004159 // The Google packages we monitor for the navigator.isApplicationInstalled()
4160 // API. Add as needed.
4161 private static Set<String> sGoogleApps;
4162 static {
4163 sGoogleApps = new HashSet<String>();
4164 sGoogleApps.add("com.google.android.youtube");
4165 }
4166
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004167 /**
4168 * A UrlData class to abstract how the content will be set to WebView.
4169 * This base class uses loadUrl to show the content.
4170 */
Leon Scroggins6eac63e2010-03-15 18:19:14 -04004171 /* package */ static class UrlData {
Grace Kloba068e48b2010-01-26 18:11:27 -08004172 final String mUrl;
4173 final Map<String, String> mHeaders;
Leon Scroggins58d56c62010-01-28 15:12:40 -05004174 final Intent mVoiceIntent;
Grace Kloba60e095c2009-06-16 11:50:55 -07004175
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004176 UrlData(String url) {
4177 this.mUrl = url;
Grace Kloba068e48b2010-01-26 18:11:27 -08004178 this.mHeaders = null;
Leon Scroggins58d56c62010-01-28 15:12:40 -05004179 this.mVoiceIntent = null;
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004180 }
Grace Kloba60e095c2009-06-16 11:50:55 -07004181
Leon Scroggins58d56c62010-01-28 15:12:40 -05004182 UrlData(String url, Map<String, String> headers, Intent intent) {
Grace Kloba068e48b2010-01-26 18:11:27 -08004183 this.mUrl = url;
4184 this.mHeaders = headers;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -05004185 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
4186 .equals(intent.getAction())) {
Leon Scroggins58d56c62010-01-28 15:12:40 -05004187 this.mVoiceIntent = intent;
4188 } else {
4189 this.mVoiceIntent = null;
4190 }
Grace Kloba60e095c2009-06-16 11:50:55 -07004191 }
4192
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004193 boolean isEmpty() {
Leon Scroggins58d56c62010-01-28 15:12:40 -05004194 return mVoiceIntent == null && (mUrl == null || mUrl.length() == 0);
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004195 }
4196
Leon Scroggins92472e82010-02-17 16:32:28 -05004197 /**
4198 * Load this UrlData into the given Tab. Use loadUrlDataIn to update
4199 * the title bar as well.
4200 */
Leon Scroggins58d56c62010-01-28 15:12:40 -05004201 public void loadIn(Tab t) {
4202 if (mVoiceIntent != null) {
4203 t.activateVoiceSearchMode(mVoiceIntent);
4204 } else {
4205 t.getWebView().loadUrl(mUrl, mHeaders);
4206 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004207 }
4208 };
4209
Leon Scroggins1f005d32009-08-10 17:36:42 -04004210 /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08004211}