blob: ef26145529fccaa5abf5f18a912d2a8774c73977 [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
Michael Kolb3f65c382010-08-20 15:31:16 -070019import com.android.browser.ScrollWebView.ScrollListener;
Bjorn Bringertd69f51d2010-09-13 14:06:41 +010020import com.android.browser.search.SearchEngine;
Michael Kolb3f65c382010-08-20 15:31:16 -070021import com.android.common.Search;
22import com.android.common.speech.LoggingEvents;
23
Michael Kolbed217742010-08-10 17:52:34 -070024import android.app.ActionBar;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.app.Activity;
The Android Open Source Project0c908882009-03-03 19:32:16 -080026import android.app.AlertDialog;
Steve Howard5a862fc2010-09-28 12:52:06 -070027import android.app.DownloadManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.app.ProgressDialog;
29import android.app.SearchManager;
30import android.content.ActivityNotFoundException;
31import android.content.BroadcastReceiver;
Dianne Hackborn80f32622010-08-05 14:17:53 -070032import android.content.ClipboardManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.content.ComponentName;
Leon Scroggins58d56c62010-01-28 15:12:40 -050034import android.content.ContentProvider;
35import android.content.ContentProviderClient;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.content.ContentResolver;
37import android.content.ContentValues;
38import android.content.Context;
39import android.content.DialogInterface;
40import android.content.Intent;
41import android.content.IntentFilter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042import android.content.pm.PackageManager;
43import android.content.pm.ResolveInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import android.content.res.Configuration;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080046import android.graphics.Bitmap;
Andrei Popescu540035d2009-09-18 18:59:20 +010047import android.graphics.BitmapFactory;
The Android Open Source Project0c908882009-03-03 19:32:16 -080048import android.graphics.Canvas;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049import android.graphics.Picture;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040050import android.graphics.PixelFormat;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080052import android.net.ConnectivityManager;
Andrei Popescu56199cc2010-01-12 22:39:16 +000053import android.net.NetworkInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080054import android.net.Uri;
55import android.net.WebAddress;
The Android Open Source Project0c908882009-03-03 19:32:16 -080056import android.net.http.SslCertificate;
57import android.net.http.SslError;
58import android.os.AsyncTask;
59import android.os.Bundle;
60import android.os.Debug;
61import android.os.Environment;
62import android.os.Handler;
The Android Open Source Project0c908882009-03-03 19:32:16 -080063import android.os.Message;
64import android.os.PowerManager;
65import android.os.Process;
The Android Open Source Project0c908882009-03-03 19:32:16 -080066import android.os.SystemClock;
The Android Open Source Project0c908882009-03-03 19:32:16 -080067import android.provider.Browser;
Michael Kolb370a4f32010-10-06 10:45:32 -070068import android.provider.BrowserContract;
Jeff Hamilton1a805652010-09-07 12:36:30 -070069import android.provider.BrowserContract.Images;
Michael Kolbe421c242010-10-04 19:29:01 -070070import android.provider.ContactsContract;
Michael Kolba2b2ba82010-08-04 17:54:03 -070071import android.provider.ContactsContract.Intents.Insert;
The Android Open Source Project0c908882009-03-03 19:32:16 -080072import android.provider.MediaStore;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050073import android.speech.RecognizerResultsIntent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080074import android.text.TextUtils;
75import android.text.format.DateFormat;
The Android Open Source Project0c908882009-03-03 19:32:16 -080076import android.util.Log;
Dianne Hackborn385effd2010-02-24 20:03:04 -080077import android.util.Patterns;
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -040078import android.view.ActionMode;
The Android Open Source Project0c908882009-03-03 19:32:16 -080079import android.view.ContextMenu;
Michael Kolba2b2ba82010-08-04 17:54:03 -070080import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080081import android.view.Gravity;
82import android.view.KeyEvent;
83import android.view.LayoutInflater;
84import android.view.Menu;
85import android.view.MenuInflater;
86import android.view.MenuItem;
Michael Kolba2b2ba82010-08-04 17:54:03 -070087import android.view.MenuItem.OnMenuItemClickListener;
The Android Open Source Project0c908882009-03-03 19:32:16 -080088import android.view.View;
89import android.view.ViewGroup;
90import android.view.Window;
91import android.view.WindowManager;
Svetoslav Ganov2b345992010-05-06 06:13:54 -070092import android.view.accessibility.AccessibilityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080093import android.webkit.CookieManager;
94import android.webkit.CookieSyncManager;
95import android.webkit.DownloadListener;
96import android.webkit.HttpAuthHandler;
97import android.webkit.SslErrorHandler;
98import android.webkit.URLUtil;
Leon Clarkecb6cc862009-09-29 18:35:13 +010099import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800100import android.webkit.WebChromeClient;
101import android.webkit.WebHistoryItem;
102import android.webkit.WebIconDatabase;
Leon Scroggins88d08032010-10-21 15:17:10 -0400103import android.webkit.WebSettings;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800104import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800105import android.widget.FrameLayout;
106import android.widget.LinearLayout;
107import android.widget.TextView;
108import android.widget.Toast;
109
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400110import java.io.ByteArrayOutputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800111import java.io.File;
Ben Murdoch4f75ba22009-10-27 11:48:28 +0000112import java.io.IOException;
113import java.io.InputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114import java.net.MalformedURLException;
Dianne Hackborn99189432009-06-17 18:06:18 -0700115import java.net.URISyntaxException;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800116import java.net.URL;
117import java.net.URLEncoder;
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700118import java.util.Calendar;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119import java.util.Date;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800120import java.util.HashMap;
Grace Kloba00f54c52010-01-27 14:53:51 -0800121import java.util.Iterator;
Grace Kloba068e48b2010-01-26 18:11:27 -0800122import java.util.Map;
Michael Kolbfe251992010-07-08 15:41:55 -0700123import java.util.Vector;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124import java.util.regex.Matcher;
125import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800126
127public class BrowserActivity extends Activity
Michael Kolbe421c242010-10-04 19:29:01 -0700128 implements View.OnCreateContextMenuListener, DownloadListener,
Leon Scroggins88d08032010-10-21 15:17:10 -0400129 BookmarksHistoryCallbacks {
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
Cary Clarka9771242009-08-11 16:42:26 -0400138 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800139 @Override
140 public Void doInBackground(File... files) {
141 if (files != null) {
142 for (File f : files) {
Cary Clarkd6be1752009-08-12 12:56:42 -0400143 if (!f.delete()) {
144 Log.e(LOGTAG, f.getPath() + " was not deleted");
145 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 }
147 }
148 return null;
149 }
150 }
151
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400152 /**
153 * This layout holds everything you see below the status bar, including the
154 * error console, the custom view container, and the webviews.
155 */
156 private FrameLayout mBrowserFrameLayout;
Leon Scroggins81db3662009-06-04 17:45:11 -0400157
Michael Kolbe421c242010-10-04 19:29:01 -0700158 private CombinedBookmarkHistoryView mComboView;
159
Leon Scrogginsd746a942010-05-19 13:21:44 -0400160 private boolean mXLargeScreenSize;
161
Jeff Davidson43610292010-07-16 16:03:58 -0700162 private Boolean mIsProviderPresent = null;
163 private Uri mRlzUri = null;
164
Grace Kloba22ac16e2009-10-07 18:00:23 -0700165 @Override
166 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700167 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800168 Log.v(LOGTAG, this + " onStart");
169 }
170 super.onCreate(icicle);
Derek Sollenbergerffa561e2010-11-16 14:19:01 -0500171
172 // Keep a settings instance handy.
173 mSettings = BrowserSettings.getInstance();
174
175 // render the browser in OpenGL
176 if (mSettings.isHardwareAccelerated()) {
177 // Set the flag in the activity's window
178 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
179 WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
180 } else {
181 // Clear the flag in the activity's window
182 this.getWindow().setFlags(0, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
183 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800184
Mike Reedd334bf52010-01-26 15:21:44 -0500185 // enable this to test the browser in 32bit
186 if (false) {
187 getWindow().setFormat(PixelFormat.RGBX_8888);
188 BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
189 }
190
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100191 if (((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE)).isEnabled()) {
Svetoslav Ganov2b345992010-05-06 06:13:54 -0700192 setDefaultKeyMode(DEFAULT_KEYS_DISABLE);
193 } else {
194 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
195 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800196
197 mResolver = getContentResolver();
198
Grace Kloba0923d692009-09-23 21:37:25 -0700199 // If this was a web search request, pass it on to the default web
200 // search provider and finish this activity.
201 if (handleWebSearchIntent(getIntent())) {
202 finish();
203 return;
204 }
205
Michael Kolb0506f2d2010-10-14 16:20:16 -0700206 mSecLockIcon = getResources().getDrawable(R.drawable.ic_secure);
207 mMixLockIcon = getResources().getDrawable(R.drawable.ic_partial_secure);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800208
Michael Kolbed217742010-08-10 17:52:34 -0700209 // Create the tab control and our initial tab
210 mTabControl = new TabControl(this);
211
212 mXLargeScreenSize = (getResources().getConfiguration().screenLayout
213 & Configuration.SCREENLAYOUT_SIZE_MASK)
214 == Configuration.SCREENLAYOUT_SIZE_XLARGE;
215
Leon Scroggins81db3662009-06-04 17:45:11 -0400216 FrameLayout frameLayout = (FrameLayout) getWindow().getDecorView()
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100217 .findViewById(android.R.id.content);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400218 mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(this)
219 .inflate(R.layout.custom_screen, null);
220 mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(
221 R.id.main_content);
222 mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout
223 .findViewById(R.id.error_console);
224 mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
225 .findViewById(R.id.fullscreen_custom_content);
226 frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
Michael Kolbe0a36662010-06-29 10:37:12 -0700227
Leon Scrogginsd746a942010-05-19 13:21:44 -0400228 if (mXLargeScreenSize) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700229 mTitleBar = new TitleBarXLarge(this);
230 mTitleBar.setProgress(100);
231 mFakeTitleBar = new TitleBarXLarge(this);
Michael Kolbed217742010-08-10 17:52:34 -0700232 ActionBar actionBar = getActionBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700233 mTabBar = new TabBar(this, mTabControl, (TitleBarXLarge) mFakeTitleBar);
Michael Kolbed217742010-08-10 17:52:34 -0700234 actionBar.setCustomNavigationMode(mTabBar);
Michael Kolb68775752010-08-19 12:42:01 -0700235 // disable built in zoom controls
236 mTabControl.setDisplayZoomControls(false);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400237 } else {
Leon Scroggins571b3762010-05-26 10:25:01 -0400238 mTitleBar = new TitleBar(this);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400239 // mTitleBar will be always be shown in the fully loaded mode on
240 // phone
241 mTitleBar.setProgress(100);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400242 mFakeTitleBar = new TitleBar(this);
243 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800244
The Android Open Source Project0c908882009-03-03 19:32:16 -0800245 // Open the icon database and retain all the bookmark urls for favicons
246 retainIconsOnStartup();
247
The Android Open Source Project0c908882009-03-03 19:32:16 -0800248 mSettings.setTabControl(mTabControl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800249
250 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
251 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
252
Patrick Scott6adacc92010-03-05 08:24:51 -0500253 // Find out if the network is currently up.
254 ConnectivityManager cm = (ConnectivityManager) getSystemService(
255 Context.CONNECTIVITY_SERVICE);
256 NetworkInfo info = cm.getActiveNetworkInfo();
257 if (info != null) {
258 mIsNetworkUp = info.isAvailable();
259 }
260
Grace Klobaa34f6862009-07-31 16:28:17 -0700261 /* enables registration for changes in network status from
262 http stack */
263 mNetworkStateChangedFilter = new IntentFilter();
264 mNetworkStateChangedFilter.addAction(
265 ConnectivityManager.CONNECTIVITY_ACTION);
266 mNetworkStateIntentReceiver = new BroadcastReceiver() {
267 @Override
268 public void onReceive(Context context, Intent intent) {
269 if (intent.getAction().equals(
270 ConnectivityManager.CONNECTIVITY_ACTION)) {
Andrei Popescue4c98462010-02-19 15:44:13 +0000271
272 NetworkInfo info = intent.getParcelableExtra(
273 ConnectivityManager.EXTRA_NETWORK_INFO);
274 String typeName = info.getTypeName();
275 String subtypeName = info.getSubtypeName();
276 sendNetworkType(typeName.toLowerCase(),
277 (subtypeName != null ? subtypeName.toLowerCase() : ""));
278
279 onNetworkToggle(info.isAvailable());
Grace Klobaa34f6862009-07-31 16:28:17 -0700280 }
281 }
282 };
283
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700284 // Unless the last browser usage was within 24 hours, destroy any
285 // remaining incognito tabs.
286
287 Calendar lastActiveDate = icicle != null ? (Calendar) icicle.getSerializable("lastActiveDate") : null;
288 Calendar today = Calendar.getInstance();
289 Calendar yesterday = Calendar.getInstance();
290 yesterday.add(Calendar.DATE, -1);
291
292 boolean dontRestoreIncognitoTabs = lastActiveDate == null
293 || lastActiveDate.before(yesterday)
294 || lastActiveDate.after(today);
295
296 if (!mTabControl.restoreState(icicle, dontRestoreIncognitoTabs)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800297 // clear up the thumbnail directory if we can't restore the state as
298 // none of the files in the directory are referenced any more.
299 new ClearThumbnails().execute(
300 mTabControl.getThumbnailDir().listFiles());
Grace Klobaaab3f092009-07-30 12:29:51 -0700301 // there is no quit on Android. But if we can't restore the state,
302 // we can treat it as a new Browser, remove the old session cookies.
303 CookieManager.getInstance().removeSessionCookie();
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700304 // remove any incognito files
305 WebView.cleanupPrivateBrowsingFiles(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800306 final Intent intent = getIntent();
307 final Bundle extra = intent.getExtras();
308 // Create an initial tab.
309 // If the intent is ACTION_VIEW and data is not null, the Browser is
310 // invoked to view the content by another application. In this case,
311 // the tab will be close when exit.
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700312 UrlData urlData = getUrlDataFromIntent(intent);
313
Leon Scroggins58d56c62010-01-28 15:12:40 -0500314 String action = intent.getAction();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700315 final Tab t = mTabControl.createNewTab(
Leon Scroggins58d56c62010-01-28 15:12:40 -0500316 (Intent.ACTION_VIEW.equals(action) &&
317 intent.getData() != null)
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500318 || RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
319 .equals(action),
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700320 intent.getStringExtra(Browser.EXTRA_APPLICATION_ID),
321 urlData.mUrl, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800322 mTabControl.setCurrentTab(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800323 attachTabToContentView(t);
324 WebView webView = t.getWebView();
325 if (extra != null) {
326 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
327 if (scale > 0 && scale <= 1000) {
328 webView.setInitialScale(scale);
329 }
330 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800331
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700332 if (urlData.isEmpty()) {
Shimeng (Simon) Wang98d5fce2010-03-16 13:23:39 -0700333 loadUrl(webView, mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800334 } else {
Patrick Scott9d53da02010-02-19 10:19:01 -0500335 loadUrlDataIn(t, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800336 }
337 } else {
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700338 if (dontRestoreIncognitoTabs) {
339 WebView.cleanupPrivateBrowsingFiles(this);
340 }
341
The Android Open Source Project0c908882009-03-03 19:32:16 -0800342 // TabControl.restoreState() will create a new tab even if
Leon Scroggins1f005d32009-08-10 17:36:42 -0400343 // restoring the state fails.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800344 attachTabToContentView(mTabControl.getCurrentTab());
345 }
Grace Kloba615c6c92009-08-03 10:22:44 -0700346
Cary Clarkb4b83182010-07-01 12:36:56 -0400347 // Delete old thumbnails to save space
348 File dir = mTabControl.getThumbnailDir();
349 if (dir.exists()) {
350 for (String child : dir.list()) {
351 File f = new File(dir, child);
352 f.delete();
353 }
354 }
355
Feng Qianb3c02da2009-06-29 15:58:08 -0700356 // Read JavaScript flags if it exists.
357 String jsFlags = mSettings.getJsFlags();
358 if (jsFlags.trim().length() != 0) {
359 mTabControl.getCurrentWebView().setJsFlags(jsFlags);
360 }
Bjorn Bringerta7611812010-03-24 11:12:02 +0000361
362 // Start watching the default geolocation permissions
363 mSystemAllowGeolocationOrigins
364 = new SystemAllowGeolocationOrigins(getApplicationContext());
365 mSystemAllowGeolocationOrigins.start();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800366 }
367
Michael Kolba2b2ba82010-08-04 17:54:03 -0700368 ScrollListener getScrollListener() {
369 return mTabBar;
370 }
371
Leon Scroggins58d56c62010-01-28 15:12:40 -0500372 /**
373 * Feed the previously stored results strings to the BrowserProvider so that
374 * the SearchDialog will show them instead of the standard searches.
375 * @param result String to show on the editable line of the SearchDialog.
376 */
377 /* package */ void showVoiceSearchResults(String result) {
378 ContentProviderClient client = mResolver.acquireContentProviderClient(
379 Browser.BOOKMARKS_URI);
380 ContentProvider prov = client.getLocalContentProvider();
381 BrowserProvider bp = (BrowserProvider) prov;
382 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
383 client.release();
384
Leon Scrogginsfbb3f152010-03-09 17:26:56 -0500385 Bundle bundle = createGoogleSearchSourceBundle(
386 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
387 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
388 startSearch(result, false, bundle, false);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500389 }
390
The Android Open Source Project0c908882009-03-03 19:32:16 -0800391 @Override
392 protected void onNewIntent(Intent intent) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700393 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800394 // When a tab is closed on exit, the current tab index is set to -1.
395 // Reset before proceed as Browser requires the current tab to be set.
396 if (current == null) {
397 // Try to reset the tab in case the index was incorrect.
398 current = mTabControl.getTab(0);
399 if (current == null) {
400 // No tabs at all so just ignore this intent.
401 return;
402 }
403 mTabControl.setCurrentTab(current);
404 attachTabToContentView(current);
405 resetTitleAndIcon(current.getWebView());
406 }
407 final String action = intent.getAction();
408 final int flags = intent.getFlags();
409 if (Intent.ACTION_MAIN.equals(action) ||
410 (flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
411 // just resume the browser
412 return;
413 }
Leon Scrogginsb8a844d2010-03-18 15:06:15 -0400414 // In case the SearchDialog is open.
415 ((SearchManager) getSystemService(Context.SEARCH_SERVICE))
416 .stopSearch();
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500417 boolean activateVoiceSearch = RecognizerResultsIntent
418 .ACTION_VOICE_SEARCH_RESULTS.equals(action);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800419 if (Intent.ACTION_VIEW.equals(action)
420 || Intent.ACTION_SEARCH.equals(action)
421 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
Leon Scroggins58d56c62010-01-28 15:12:40 -0500422 || Intent.ACTION_WEB_SEARCH.equals(action)
423 || activateVoiceSearch) {
Leon Scroggins3b18ce32010-02-08 17:35:59 -0500424 if (current.isInVoiceSearchMode()) {
425 String title = current.getVoiceDisplayTitle();
426 if (title != null && title.equals(intent.getStringExtra(
427 SearchManager.QUERY))) {
428 // The user submitted the same search as the last voice
429 // search, so do nothing.
430 return;
431 }
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500432 if (Intent.ACTION_SEARCH.equals(action)
433 && current.voiceSearchSourceIsGoogle()) {
434 Intent logIntent = new Intent(
435 LoggingEvents.ACTION_LOG_EVENT);
436 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
437 LoggingEvents.VoiceSearch.QUERY_UPDATED);
438 logIntent.putExtra(
439 LoggingEvents.VoiceSearch.EXTRA_QUERY_UPDATED_VALUE,
440 intent.getDataString());
441 sendBroadcast(logIntent);
442 // Note, onPageStarted will revert the voice title bar
443 // When http://b/issue?id=2379215 is fixed, we should update
444 // the title bar here.
445 }
Leon Scroggins3b18ce32010-02-08 17:35:59 -0500446 }
Satish Sampath565505b2009-05-29 15:37:27 +0100447 // If this was a search request (e.g. search query directly typed into the address bar),
448 // pass it on to the default web search provider.
449 if (handleWebSearchIntent(intent)) {
450 return;
451 }
452
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700453 UrlData urlData = getUrlDataFromIntent(intent);
454 if (urlData.isEmpty()) {
455 urlData = new UrlData(mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800456 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700457
Grace Klobacc634032009-07-28 15:58:19 -0700458 final String appId = intent
459 .getStringExtra(Browser.EXTRA_APPLICATION_ID);
Leon Scroggins47208682010-04-07 17:59:48 -0400460 if ((Intent.ACTION_VIEW.equals(action)
461 // If a voice search has no appId, it means that it came
462 // from the browser. In that case, reuse the current tab.
463 || (activateVoiceSearch && appId != null))
Grace Klobacc634032009-07-28 15:58:19 -0700464 && !getPackageName().equals(appId)
465 && (flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700466 Tab appTab = mTabControl.getTabFromId(appId);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700467 if (appTab != null) {
468 Log.i(LOGTAG, "Reusing tab for " + appId);
469 // Dismiss the subwindow if applicable.
470 dismissSubWindow(appTab);
471 // Since we might kill the WebView, remove it from the
472 // content view first.
473 removeTabFromContentView(appTab);
474 // Recreate the main WebView after destroying the old one.
475 // If the WebView has the same original url and is on that
476 // page, it can be reused.
477 boolean needsLoad =
Leon Scroggins6eac63e2010-03-15 18:19:14 -0400478 mTabControl.recreateWebView(appTab, urlData);
Ben Murdochbff2d602009-07-01 20:19:05 +0100479
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700480 if (current != appTab) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400481 switchToTab(mTabControl.getTabIndex(appTab));
482 if (needsLoad) {
Patrick Scott9d53da02010-02-19 10:19:01 -0500483 loadUrlDataIn(appTab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400484 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700485 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400486 // If the tab was the current tab, we have to attach
487 // it to the view system again.
488 attachTabToContentView(appTab);
489 if (needsLoad) {
Patrick Scott9d53da02010-02-19 10:19:01 -0500490 loadUrlDataIn(appTab, urlData);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700491 }
492 }
493 return;
Patrick Scottcd115892009-07-16 09:42:58 -0400494 } else {
495 // No matching application tab, try to find a regular tab
496 // with a matching url.
497 appTab = mTabControl.findUnusedTabWithUrl(urlData.mUrl);
Leon Scroggins25515f82009-08-19 15:31:58 -0400498 if (appTab != null) {
499 if (current != appTab) {
500 switchToTab(mTabControl.getTabIndex(appTab));
501 }
502 // Otherwise, we are already viewing the correct tab.
Patrick Scottcd115892009-07-16 09:42:58 -0400503 } else {
504 // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
505 // will be opened in a new tab unless we have reached
506 // MAX_TABS. Then the url will be opened in the current
507 // tab. If a new tab is created, it will have "true" for
508 // exit on close.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400509 openTabAndShow(urlData, true, appId);
Patrick Scottcd115892009-07-16 09:42:58 -0400510 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700511 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800512 } else {
Grace Kloba638d3f42009-11-23 10:35:04 -0800513 if (!urlData.isEmpty()
514 && urlData.mUrl.startsWith("about:debug")) {
515 if ("about:debug.dom".equals(urlData.mUrl)) {
516 current.getWebView().dumpDomTree(false);
517 } else if ("about:debug.dom.file".equals(urlData.mUrl)) {
518 current.getWebView().dumpDomTree(true);
519 } else if ("about:debug.render".equals(urlData.mUrl)) {
520 current.getWebView().dumpRenderTree(false);
521 } else if ("about:debug.render.file".equals(urlData.mUrl)) {
522 current.getWebView().dumpRenderTree(true);
523 } else if ("about:debug.display".equals(urlData.mUrl)) {
524 current.getWebView().dumpDisplayTree();
525 } else {
526 mSettings.toggleDebugSettings();
527 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800528 return;
529 }
Leon Scroggins1f005d32009-08-10 17:36:42 -0400530 // Get rid of the subwindow if it exists
531 dismissSubWindow(current);
Leon Scroggins8588d152010-04-15 11:01:53 -0400532 // If the current Tab is being used as an application tab,
533 // remove the association, since the new Intent means that it is
534 // no longer associated with that application.
535 current.setAppId(null);
Patrick Scott9d53da02010-02-19 10:19:01 -0500536 loadUrlDataIn(current, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800537 }
538 }
539 }
540
Satish Sampath565505b2009-05-29 15:37:27 +0100541 /**
542 * Launches the default web search activity with the query parameters if the given intent's data
543 * are identified as plain search terms and not URLs/shortcuts.
544 * @return true if the intent was handled and web search activity was launched, false if not.
545 */
546 private boolean handleWebSearchIntent(Intent intent) {
547 if (intent == null) return false;
548
549 String url = null;
550 final String action = intent.getAction();
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500551 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS.equals(
552 action)) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500553 return false;
554 }
Satish Sampath565505b2009-05-29 15:37:27 +0100555 if (Intent.ACTION_VIEW.equals(action)) {
Grace Kloba1e705052009-09-29 13:13:36 -0700556 Uri data = intent.getData();
557 if (data != null) url = data.toString();
Satish Sampath565505b2009-05-29 15:37:27 +0100558 } else if (Intent.ACTION_SEARCH.equals(action)
559 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
560 || Intent.ACTION_WEB_SEARCH.equals(action)) {
561 url = intent.getStringExtra(SearchManager.QUERY);
562 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100563 return handleWebSearchRequest(url, intent.getBundleExtra(SearchManager.APP_DATA),
564 intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
Satish Sampath565505b2009-05-29 15:37:27 +0100565 }
566
567 /**
568 * Launches the default web search activity with the query parameters if the given url string
569 * was identified as plain search terms and not URL/shortcut.
570 * @return true if the request was handled and web search activity was launched, false if not.
571 */
Bjorn Bringert04851702009-09-22 10:36:01 +0100572 private boolean handleWebSearchRequest(String inUrl, Bundle appData, String extraData) {
Satish Sampath565505b2009-05-29 15:37:27 +0100573 if (inUrl == null) return false;
574
575 // In general, we shouldn't modify URL from Intent.
576 // But currently, we get the user-typed URL from search box as well.
577 String url = fixUrl(inUrl).trim();
578
Leon Scroggins956cf012010-10-07 13:51:57 -0400579 // URLs are handled by the regular flow of control, so
Satish Sampath565505b2009-05-29 15:37:27 +0100580 // return early.
Dan Egnor5ee906c2009-11-18 12:11:49 -0800581 if (Patterns.WEB_URL.matcher(url).matches()
Leon Scroggins956cf012010-10-07 13:51:57 -0400582 || ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
Satish Sampath565505b2009-05-29 15:37:27 +0100583 return false;
584 }
585
Leon Scroggins8d06e362010-03-24 14:45:57 -0400586 final ContentResolver cr = mResolver;
587 final String newUrl = url;
Elliott Slaughter627d96f2010-08-18 16:35:30 -0700588 if (mTabControl == null || !mTabControl.getCurrentWebView().isPrivateBrowsingEnabled()) {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700589 new AsyncTask<Void, Void, Void>() {
590 @Override
591 protected Void doInBackground(Void... unused) {
592 Browser.updateVisitedHistory(cr, newUrl, false);
593 Browser.addSearchUrl(cr, newUrl);
594 return null;
595 }
596 }.execute();
597 }
Satish Sampath565505b2009-05-29 15:37:27 +0100598
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100599 SearchEngine searchEngine = mSettings.getSearchEngine();
600 if (searchEngine == null) return false;
601 searchEngine.startSearch(this, url, appData, extraData);
Satish Sampath565505b2009-05-29 15:37:27 +0100602
603 return true;
604 }
605
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700606 private UrlData getUrlDataFromIntent(Intent intent) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500607 String url = "";
Grace Kloba068e48b2010-01-26 18:11:27 -0800608 Map<String, String> headers = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800609 if (intent != null) {
610 final String action = intent.getAction();
611 if (Intent.ACTION_VIEW.equals(action)) {
612 url = smartUrlFilter(intent.getData());
613 if (url != null && url.startsWith("content:")) {
614 /* Append mimetype so webview knows how to display */
615 String mimeType = intent.resolveType(getContentResolver());
616 if (mimeType != null) {
617 url += "?" + mimeType;
618 }
619 }
Grace Kloba068e48b2010-01-26 18:11:27 -0800620 if (url != null && url.startsWith("http")) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800621 final Bundle pairs = intent
622 .getBundleExtra(Browser.EXTRA_HEADERS);
Grace Kloba2d508ed2010-01-28 11:39:15 -0800623 if (pairs != null && !pairs.isEmpty()) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800624 Iterator<String> iter = pairs.keySet().iterator();
Grace Kloba068e48b2010-01-26 18:11:27 -0800625 headers = new HashMap<String, String>();
Grace Kloba00f54c52010-01-27 14:53:51 -0800626 while (iter.hasNext()) {
627 String key = iter.next();
628 headers.put(key, pairs.getString(key));
Grace Kloba068e48b2010-01-26 18:11:27 -0800629 }
630 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700631 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800632 } else if (Intent.ACTION_SEARCH.equals(action)
633 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
634 || Intent.ACTION_WEB_SEARCH.equals(action)) {
635 url = intent.getStringExtra(SearchManager.QUERY);
636 if (url != null) {
637 mLastEnteredUrl = url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800638 // In general, we shouldn't modify URL from Intent.
639 // But currently, we get the user-typed URL from search box as well.
640 url = fixUrl(url);
641 url = smartUrlFilter(url);
Leon Scroggins8d06e362010-03-24 14:45:57 -0400642 final ContentResolver cr = mResolver;
643 final String newUrl = url;
Elliott Slaughter8389e992010-08-20 15:44:08 -0700644 if (mTabControl == null
645 || mTabControl.getCurrentWebView() == null
646 || !mTabControl.getCurrentWebView().isPrivateBrowsingEnabled()) {
647 new AsyncTask<Void, Void, Void>() {
648 @Override
649 protected Void doInBackground(Void... unused) {
650 Browser.updateVisitedHistory(cr, newUrl, false);
651 return null;
652 }
653 }.execute();
654 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800655 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
656 if (url.contains(searchSource)) {
657 String source = null;
658 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
659 if (appData != null) {
Bjorn Bringert10d1cca2010-02-10 14:22:12 +0000660 source = appData.getString(Search.SOURCE);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800661 }
662 if (TextUtils.isEmpty(source)) {
663 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
664 }
665 url = url.replace(searchSource, "&source=android-"+source+"&");
666 }
667 }
668 }
669 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500670 return new UrlData(url, headers, intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800671 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500672 /* package */ void showVoiceTitleBar(String title) {
673 mTitleBar.setInVoiceMode(true);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500674 mTitleBar.setDisplayTitle(title);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700675 mFakeTitleBar.setInVoiceMode(true);
676 mFakeTitleBar.setDisplayTitle(title);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500677 }
678 /* package */ void revertVoiceTitleBar() {
679 mTitleBar.setInVoiceMode(false);
Leon Scroggins003a5dd2010-03-10 12:13:14 -0500680 mTitleBar.setDisplayTitle(mUrl);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700681 mFakeTitleBar.setInVoiceMode(false);
682 mFakeTitleBar.setDisplayTitle(mUrl);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500683 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800684 /* package */ static String fixUrl(String inUrl) {
Cary Clark652ff872009-09-10 13:34:44 -0400685 // FIXME: Converting the url to lower case
686 // duplicates functionality in smartUrlFilter().
687 // However, changing all current callers of fixUrl to
688 // call smartUrlFilter in addition may have unwanted
689 // consequences, and is deferred for now.
690 int colon = inUrl.indexOf(':');
691 boolean allLower = true;
692 for (int index = 0; index < colon; index++) {
693 char ch = inUrl.charAt(index);
694 if (!Character.isLetter(ch)) {
695 break;
696 }
697 allLower &= Character.isLowerCase(ch);
698 if (index == colon - 1 && !allLower) {
699 inUrl = inUrl.substring(0, colon).toLowerCase()
700 + inUrl.substring(colon);
701 }
702 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800703 if (inUrl.startsWith("http://") || inUrl.startsWith("https://"))
704 return inUrl;
705 if (inUrl.startsWith("http:") ||
706 inUrl.startsWith("https:")) {
707 if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) {
708 inUrl = inUrl.replaceFirst("/", "//");
709 } else inUrl = inUrl.replaceFirst(":", "://");
710 }
711 return inUrl;
712 }
713
Grace Kloba22ac16e2009-10-07 18:00:23 -0700714 @Override
715 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800716 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700717 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800718 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
719 }
720
721 if (!mActivityInPause) {
722 Log.e(LOGTAG, "BrowserActivity is already resumed.");
723 return;
724 }
725
Mike Reed7bfa63b2009-05-28 11:08:32 -0400726 mTabControl.resumeCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800727 mActivityInPause = false;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400728 resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800729
730 if (mWakeLock.isHeld()) {
731 mHandler.removeMessages(RELEASE_WAKELOCK);
732 mWakeLock.release();
733 }
734
The Android Open Source Project0c908882009-03-03 19:32:16 -0800735 registerReceiver(mNetworkStateIntentReceiver,
736 mNetworkStateChangedFilter);
737 WebView.enablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800738 }
739
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400740 /**
741 * Since the actual title bar is embedded in the WebView, and removing it
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400742 * would change its appearance, use a different TitleBar to show overlayed
743 * at the top of the screen, when the menu is open or the page is loading.
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400744 */
Michael Kolba2b2ba82010-08-04 17:54:03 -0700745 private TitleBarBase mFakeTitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400746
747 /**
748 * Keeps track of whether the options menu is open. This is important in
749 * determining whether to show or hide the title bar overlay.
750 */
751 private boolean mOptionsMenuOpen;
752
753 /**
754 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
755 * of whether the configuration has changed. The first onMenuOpened call
756 * after a configuration change is simply a reopening of the same menu
757 * (i.e. mIconView did not change).
758 */
759 private boolean mConfigChanged;
760
761 /**
762 * Whether or not the options menu is in its smaller, icon menu form. When
763 * true, we want the title bar overlay to be up. When false, we do not.
764 * Only meaningful if mOptionsMenuOpen is true.
765 */
766 private boolean mIconView;
767
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400768 @Override
769 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400770 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
771 if (mOptionsMenuOpen) {
772 if (mConfigChanged) {
773 // We do not need to make any changes to the state of the
774 // title bar, since the only thing that happened was a
775 // change in orientation
776 mConfigChanged = false;
777 } else {
778 if (mIconView) {
779 // Switching the menu to expanded view, so hide the
780 // title bar.
781 hideFakeTitleBar();
782 mIconView = false;
783 } else {
784 // Switching the menu back to icon view, so show the
785 // title bar once again.
786 showFakeTitleBar();
787 mIconView = true;
788 }
789 }
790 } else {
791 // The options menu is closed, so open it, and show the title
792 showFakeTitleBar();
793 mOptionsMenuOpen = true;
794 mConfigChanged = false;
795 mIconView = true;
796 }
797 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400798 return true;
799 }
800
Michael Kolba2b2ba82010-08-04 17:54:03 -0700801 void showFakeTitleBar() {
802 if (!isFakeTitleBarShowing() && mActiveTabsPage == null && !mActivityInPause) {
Grace Kloba847c25b2010-03-30 16:00:26 -0700803 WebView mainView = mTabControl.getCurrentWebView();
804 // if there is no current WebView, don't show the faked title bar;
Grace Kloba65190702010-04-02 23:37:26 -0700805 if (mainView == null) {
Cary Clarka0464552009-09-29 13:00:45 -0400806 return;
807 }
Leon Scroggins79e36d92010-04-29 16:01:46 +0100808 // Do not need to check for null, since the current tab will have
809 // at least a main WebView, or we would have returned above.
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -0400810 if (isInCustomActionMode()) {
811 // Do not show the fake title bar, while a custom ActionMode
812 // (i.e. find or select) is showing.
Leon Scroggins79e36d92010-04-29 16:01:46 +0100813 return;
814 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700815 if (mXLargeScreenSize) {
816 mContentView.addView(mFakeTitleBar);
817 mTabBar.onShowTitleBar();
818 } else {
819 WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400820
Michael Kolba2b2ba82010-08-04 17:54:03 -0700821 // Add the title bar to the window manager so it can receive
822 // touches
823 // while the menu is up
824 WindowManager.LayoutParams params =
825 new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
826 ViewGroup.LayoutParams.WRAP_CONTENT,
827 WindowManager.LayoutParams.TYPE_APPLICATION,
828 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
829 PixelFormat.TRANSLUCENT);
830 params.gravity = Gravity.TOP;
831 boolean atTop = mainView.getScrollY() == 0;
832 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
833 manager.addView(mFakeTitleBar, params);
834 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400835 }
836 }
837
838 @Override
839 public void onOptionsMenuClosed(Menu menu) {
840 mOptionsMenuOpen = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -0400841 if (!mInLoad) {
842 hideFakeTitleBar();
843 } else if (!mIconView) {
844 // The page is currently loading, and we are in expanded mode, so
845 // we were not showing the menu. Show it once again. It will be
846 // removed when the page finishes.
847 showFakeTitleBar();
848 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400849 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700850
Michael Kolba2b2ba82010-08-04 17:54:03 -0700851 void stopScrolling() {
852 ((ScrollWebView) mTabControl.getCurrentWebView()).stopScroll();
853 }
854
855 void hideFakeTitleBar() {
856 if (!isFakeTitleBarShowing()) return;
857 if (mXLargeScreenSize) {
858 mContentView.removeView(mFakeTitleBar);
859 mTabBar.onHideTitleBar();
860 } else {
861 WindowManager.LayoutParams params =
862 (WindowManager.LayoutParams) mFakeTitleBar.getLayoutParams();
863 WebView mainView = mTabControl.getCurrentWebView();
864 // Although we decided whether or not to animate based on the
865 // current
866 // scroll position, the scroll position may have changed since the
867 // fake title bar was displayed. Make sure it has the appropriate
868 // animation/lack thereof before removing.
869 params.windowAnimations =
870 mainView != null && mainView.getScrollY() == 0 ? 0 : R.style.TitleBar;
871 WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
872 manager.updateViewLayout(mFakeTitleBar, params);
873 manager.removeView(mFakeTitleBar);
874 }
875 }
876
877 boolean isFakeTitleBarShowing() {
878 return (mFakeTitleBar.getParent() != null);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400879 }
880
The Android Open Source Project0c908882009-03-03 19:32:16 -0800881 /**
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400882 * Special method for the fake title bar to call when displaying its context
883 * menu, since it is in its own Window, and its parent does not show a
884 * context menu.
885 */
886 /* package */ void showTitleBarContextMenu() {
Cary Clark65f4a3c2009-09-28 17:05:06 -0400887 if (null == mTitleBar.getParent()) {
888 return;
889 }
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400890 openContextMenu(mTitleBar);
891 }
892
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400893 @Override
894 public void onContextMenuClosed(Menu menu) {
895 super.onContextMenuClosed(menu);
896 if (mInLoad) {
897 showFakeTitleBar();
898 }
899 }
900
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400901 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800902 * onSaveInstanceState(Bundle map)
903 * onSaveInstanceState is called right before onStop(). The map contains
904 * the saved state.
905 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700906 @Override
907 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700908 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800909 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
910 }
911 // the default implementation requires each view to have an id. As the
912 // browser handles the state itself and it doesn't use id for the views,
913 // don't call the default implementation. Otherwise it will trigger the
914 // warning like this, "couldn't save which view has focus because the
915 // focused view XXX has no id".
916
917 // Save all the tabs
918 mTabControl.saveState(outState);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700919
920 // Save time so that we know how old incognito tabs (if any) are.
921 outState.putSerializable("lastActiveDate", Calendar.getInstance());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800922 }
923
Grace Kloba22ac16e2009-10-07 18:00:23 -0700924 @Override
925 protected void onPause() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800926 super.onPause();
927
928 if (mActivityInPause) {
929 Log.e(LOGTAG, "BrowserActivity is already paused.");
930 return;
931 }
932
Mike Reed7bfa63b2009-05-28 11:08:32 -0400933 mTabControl.pauseCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800934 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400935 if (mTabControl.getCurrentIndex() >= 0 && !pauseWebViewTimers()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800936 mWakeLock.acquire();
937 mHandler.sendMessageDelayed(mHandler
938 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
939 }
940
Leon Scrogginsa2ab6a72009-09-11 11:49:52 -0400941 // FIXME: This removes the active tabs page and resets the menu to
942 // MAIN_MENU. A better solution might be to do this work in onNewIntent
943 // but then we would need to save it in onSaveInstanceState and restore
944 // it in onCreate/onRestoreInstanceState
945 if (mActiveTabsPage != null) {
946 removeActiveTabPage(true);
947 }
948
The Android Open Source Project0c908882009-03-03 19:32:16 -0800949 cancelStopToast();
950
951 // unregister network state listener
952 unregisterReceiver(mNetworkStateIntentReceiver);
953 WebView.disablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800954 }
955
Grace Kloba22ac16e2009-10-07 18:00:23 -0700956 @Override
957 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700958 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800959 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
960 }
961 super.onDestroy();
Grace Kloba0923d692009-09-23 21:37:25 -0700962
Leon Scroggins8d5fa432009-10-02 15:55:59 -0400963 if (mUploadMessage != null) {
964 mUploadMessage.onReceiveValue(null);
965 mUploadMessage = null;
966 }
967
Grace Kloba0923d692009-09-23 21:37:25 -0700968 if (mTabControl == null) return;
969
Grace Kloba1fc98a32009-10-21 13:23:08 -0700970 // Remove the fake title bar if it is there
971 hideFakeTitleBar();
972
The Android Open Source Project0c908882009-03-03 19:32:16 -0800973 // Remove the current tab and sub window
Grace Kloba22ac16e2009-10-07 18:00:23 -0700974 Tab t = mTabControl.getCurrentTab();
Patrick Scottfb5e77f2009-04-08 19:17:37 -0700975 if (t != null) {
976 dismissSubWindow(t);
977 removeTabFromContentView(t);
978 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800979 // Destroy all the tabs
980 mTabControl.destroy();
981 WebIconDatabase.getInstance().close();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800982
Bjorn Bringerta7611812010-03-24 11:12:02 +0000983 // Stop watching the default geolocation permissions
984 mSystemAllowGeolocationOrigins.stop();
985 mSystemAllowGeolocationOrigins = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800986 }
987
988 @Override
989 public void onConfigurationChanged(Configuration newConfig) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400990 mConfigChanged = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800991 super.onConfigurationChanged(newConfig);
992
993 if (mPageInfoDialog != null) {
994 mPageInfoDialog.dismiss();
995 showPageInfo(
996 mPageInfoView,
Leon Scrogginsc7b92f82010-01-11 18:17:31 -0500997 mPageInfoFromShowSSLCertificateOnError);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800998 }
999 if (mSSLCertificateDialog != null) {
1000 mSSLCertificateDialog.dismiss();
1001 showSSLCertificate(
1002 mSSLCertificateView);
1003 }
1004 if (mSSLCertificateOnErrorDialog != null) {
1005 mSSLCertificateOnErrorDialog.dismiss();
1006 showSSLCertificateOnError(
1007 mSSLCertificateOnErrorView,
1008 mSSLCertificateOnErrorHandler,
1009 mSSLCertificateOnErrorError);
1010 }
1011 if (mHttpAuthenticationDialog != null) {
Bjorn Bringert25738922010-10-12 10:56:20 +01001012 mHttpAuthenticationDialog.reshow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001013 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001014 }
1015
Grace Kloba22ac16e2009-10-07 18:00:23 -07001016 @Override
1017 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001018 super.onLowMemory();
1019 mTabControl.freeMemory();
1020 }
1021
Cary Clarkff4d92c2010-03-25 11:17:03 -04001022 private void resumeWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001023 Tab tab = mTabControl.getCurrentTab();
Cary Clarkff4d92c2010-03-25 11:17:03 -04001024 if (tab == null) return; // monkey can trigger this
Grace Kloba22ac16e2009-10-07 18:00:23 -07001025 boolean inLoad = tab.inLoad();
1026 if ((!mActivityInPause && !inLoad) || (mActivityInPause && inLoad)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001027 CookieSyncManager.getInstance().startSync();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001028 WebView w = tab.getWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001029 if (w != null) {
1030 w.resumeTimers();
1031 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001032 }
1033 }
1034
Mike Reed7bfa63b2009-05-28 11:08:32 -04001035 private boolean pauseWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001036 Tab tab = mTabControl.getCurrentTab();
1037 boolean inLoad = tab.inLoad();
1038 if (mActivityInPause && !inLoad) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001039 CookieSyncManager.getInstance().stopSync();
1040 WebView w = mTabControl.getCurrentWebView();
1041 if (w != null) {
1042 w.pauseTimers();
1043 }
1044 return true;
1045 } else {
1046 return false;
1047 }
1048 }
1049
The Android Open Source Project0c908882009-03-03 19:32:16 -08001050 // Open the icon database and retain all the icons for visited sites.
1051 private void retainIconsOnStartup() {
1052 final WebIconDatabase db = WebIconDatabase.getInstance();
1053 db.open(getDir("icons", 0).getPath());
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001054 Cursor c = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001055 try {
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001056 c = Browser.getAllBookmarks(mResolver);
1057 if (c.moveToFirst()) {
1058 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
1059 do {
1060 String url = c.getString(urlIndex);
1061 db.retainIconForPageUrl(url);
1062 } while (c.moveToNext());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001063 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001064 } catch (IllegalStateException e) {
1065 Log.e(LOGTAG, "retainIconsOnStartup", e);
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001066 } finally {
1067 if (c!= null) c.close();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001068 }
1069 }
1070
1071 // Helper method for getting the top window.
1072 WebView getTopWindow() {
1073 return mTabControl.getCurrentTopWebView();
1074 }
1075
Grace Kloba22ac16e2009-10-07 18:00:23 -07001076 TabControl getTabControl() {
1077 return mTabControl;
1078 }
1079
The Android Open Source Project0c908882009-03-03 19:32:16 -08001080 @Override
1081 public boolean onCreateOptionsMenu(Menu menu) {
1082 super.onCreateOptionsMenu(menu);
1083
1084 MenuInflater inflater = getMenuInflater();
1085 inflater.inflate(R.menu.browser, menu);
1086 mMenu = menu;
1087 updateInLoadMenuItems();
1088 return true;
1089 }
1090
1091 /**
1092 * As the menu can be open when loading state changes
1093 * we must manually update the state of the stop/reload menu
1094 * item
1095 */
1096 private void updateInLoadMenuItems() {
1097 if (mMenu == null) {
1098 return;
1099 }
Michael Kolbe0a36662010-06-29 10:37:12 -07001100 MenuItem dest = mMenu.findItem(R.id.stop_reload_menu_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001101 MenuItem src = mInLoad ?
1102 mMenu.findItem(R.id.stop_menu_id):
Michael Kolbe0a36662010-06-29 10:37:12 -07001103 mMenu.findItem(R.id.reload_menu_id);
1104 if (src != null) {
1105 dest.setIcon(src.getIcon());
1106 dest.setTitle(src.getTitle());
1107 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001108 }
1109
1110 @Override
1111 public boolean onContextItemSelected(MenuItem item) {
1112 // chording is not an issue with context menus, but we use the same
1113 // options selector, so set mCanChord to true so we can access them.
1114 mCanChord = true;
1115 int id = item.getItemId();
Leon Scroggins96afcb12009-12-10 12:35:56 -05001116 boolean result = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001117 switch (id) {
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001118 // For the context menu from the title bar
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001119 case R.id.title_bar_copy_page_url:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001120 Tab currentTab = mTabControl.getCurrentTab();
1121 if (null == currentTab) {
1122 result = false;
1123 break;
1124 }
1125 WebView mainView = currentTab.getWebView();
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001126 if (null == mainView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001127 result = false;
1128 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001129 }
Leon Scroggins96afcb12009-12-10 12:35:56 -05001130 copy(mainView.getUrl());
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001131 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001132 // -- Browser context menu
1133 case R.id.open_context_menu_id:
The Android Open Source Project0c908882009-03-03 19:32:16 -08001134 case R.id.bookmark_context_menu_id:
1135 case R.id.save_link_context_menu_id:
1136 case R.id.share_link_context_menu_id:
1137 case R.id.copy_link_context_menu_id:
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001138 final WebView webView = getTopWindow();
1139 if (null == webView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001140 result = false;
1141 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001142 }
1143 final HashMap hrefMap = new HashMap();
1144 hrefMap.put("webview", webView);
1145 final Message msg = mHandler.obtainMessage(
1146 FOCUS_NODE_HREF, id, 0, hrefMap);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001147 webView.requestFocusNodeHref(msg);
1148 break;
1149
1150 default:
1151 // For other context menus
Leon Scroggins96afcb12009-12-10 12:35:56 -05001152 result = onOptionsItemSelected(item);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001153 }
1154 mCanChord = false;
Leon Scroggins96afcb12009-12-10 12:35:56 -05001155 return result;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001156 }
1157
1158 private Bundle createGoogleSearchSourceBundle(String source) {
1159 Bundle bundle = new Bundle();
Bjorn Bringert10d1cca2010-02-10 14:22:12 +00001160 bundle.putString(Search.SOURCE, source);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001161 return bundle;
1162 }
1163
Leon Scroggins8ad29922010-02-16 12:33:55 -05001164 /* package */ void editUrl() {
Leon Scroggins68579392009-09-15 15:31:54 -04001165 if (mOptionsMenuOpen) closeOptionsMenu();
Leon Scroggins5bbe9802009-07-31 13:10:55 -04001166 String url = (getTopWindow() == null) ? null : getTopWindow().getUrl();
Grace Kloba83f47342009-07-20 10:44:31 -07001167 startSearch(mSettings.getHomePage().equals(url) ? null : url, true,
Leon Scroggins8ad29922010-02-16 12:33:55 -05001168 null, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001169 }
1170
Leon Scroggins8ad29922010-02-16 12:33:55 -05001171 /**
1172 * Overriding this to insert a local information bundle
1173 */
The Android Open Source Project0c908882009-03-03 19:32:16 -08001174 @Override
1175 public void startSearch(String initialQuery, boolean selectInitialQuery,
1176 Bundle appSearchData, boolean globalSearch) {
1177 if (appSearchData == null) {
1178 appSearchData = createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_TYPE);
1179 }
Leon Scroggins III430057d2010-09-14 10:57:37 -04001180
1181 SearchEngine searchEngine = mSettings.getSearchEngine();
1182 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
1183 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
1184 }
1185
The Android Open Source Project0c908882009-03-03 19:32:16 -08001186 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1187 }
1188
Leon Scroggins1f005d32009-08-10 17:36:42 -04001189 /**
1190 * Switch tabs. Called by the TitleBarSet when sliding the title bar
1191 * results in changing tabs.
Leon Scroggins160a7e72009-08-14 18:28:01 -04001192 * @param index Index of the tab to change to, as defined by
1193 * mTabControl.getTabIndex(Tab t).
1194 * @return boolean True if we successfully switched to a different tab. If
1195 * the indexth tab is null, or if that tab is the same as
1196 * the current one, return false.
Leon Scroggins1f005d32009-08-10 17:36:42 -04001197 */
Leon Scroggins160a7e72009-08-14 18:28:01 -04001198 /* package */ boolean switchToTab(int index) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001199 Tab tab = mTabControl.getTab(index);
1200 Tab currentTab = mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001201 if (tab == null || tab == currentTab) {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001202 return false;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001203 }
1204 if (currentTab != null) {
1205 // currentTab may be null if it was just removed. In that case,
1206 // we do not need to remove it
1207 removeTabFromContentView(currentTab);
1208 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001209 mTabControl.setCurrentTab(tab);
1210 attachTabToContentView(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001211 resetTitleIconAndProgress();
1212 updateLockIconToLatest();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001213 return true;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001214 }
1215
Grace Kloba22ac16e2009-10-07 18:00:23 -07001216 /* package */ Tab openTabToHomePage() {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001217 return openTabAndShow(mSettings.getHomePage(), false, null);
1218 }
1219
Leon Scroggins1f005d32009-08-10 17:36:42 -04001220 /* package */ void closeCurrentWindow() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001221 final Tab current = mTabControl.getCurrentTab();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001222 if (mTabControl.getTabCount() == 1) {
Leon Scroggins30444232009-09-04 18:36:20 -04001223 // This is the last tab. Open a new one, with the home
1224 // page and close the current one.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001225 openTabToHomePage();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001226 closeTab(current);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001227 return;
1228 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001229 final Tab parent = current.getParentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001230 int indexToShow = -1;
1231 if (parent != null) {
1232 indexToShow = mTabControl.getTabIndex(parent);
1233 } else {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001234 final int currentIndex = mTabControl.getCurrentIndex();
1235 // Try to move to the tab to the right
1236 indexToShow = currentIndex + 1;
1237 if (indexToShow > mTabControl.getTabCount() - 1) {
1238 // Try to move to the tab to the left
1239 indexToShow = currentIndex - 1;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001240 }
1241 }
Leon Scroggins160a7e72009-08-14 18:28:01 -04001242 if (switchToTab(indexToShow)) {
1243 // Close window
1244 closeTab(current);
1245 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001246 }
1247
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001248 private ActiveTabsPage mActiveTabsPage;
1249
1250 /**
1251 * Remove the active tabs page.
1252 * @param needToAttach If true, the active tabs page did not attach a tab
1253 * to the content view, so we need to do that here.
1254 */
1255 /* package */ void removeActiveTabPage(boolean needToAttach) {
1256 mContentView.removeView(mActiveTabsPage);
Leon Scrogginsd746a942010-05-19 13:21:44 -04001257 mTitleBar.setVisibility(View.VISIBLE);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001258 mActiveTabsPage = null;
1259 mMenuState = R.id.MAIN_MENU;
1260 if (needToAttach) {
1261 attachTabToContentView(mTabControl.getCurrentTab());
1262 }
1263 getTopWindow().requestFocus();
1264 }
1265
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -04001266 @Override
Adam Powelld7973052010-11-15 21:10:48 -08001267 public void onActionModeStarted(ActionMode mode) {
1268 super.onActionModeStarted(mode);
Adam Powell6c6e86f2010-11-16 11:06:24 -08001269 mActionMode = mode;
Leon Scroggins4fc12cb2010-08-24 14:24:05 -04001270 hideFakeTitleBar();
1271 // Would like to change the MENU, but onEndActionMode may not be called
Adam Powelld7973052010-11-15 21:10:48 -08001272 // TODO onActionModeFinished will notify when an action mode ends
Cary Clark01cfcdd2010-06-04 16:36:45 -04001273 }
1274
The Android Open Source Project0c908882009-03-03 19:32:16 -08001275 @Override
1276 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb370a4f32010-10-06 10:45:32 -07001277 if (item.getGroupId() != R.id.CONTEXT_MENU) {
1278 // menu remains active, so ensure comboview is dismissed
1279 // if main menu option is selected
1280 removeComboView();
1281 }
Michael Kolbed217742010-08-10 17:52:34 -07001282 // check the action bar button before mCanChord check, as the prepare call
1283 // doesn't come for action bar buttons
1284 if (item.getItemId() == R.id.newtab) {
Michael Kolb300b7f02010-08-25 13:47:24 -07001285 openTabToHomePage();
Michael Kolbed217742010-08-10 17:52:34 -07001286 return true;
1287 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001288 if (!mCanChord) {
1289 // The user has already fired a shortcut with this hold down of the
1290 // menu key.
1291 return false;
1292 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001293 if (null == getTopWindow()) {
Leon Scroggins0d7ae0e2009-06-05 11:04:45 -04001294 return false;
1295 }
Grace Kloba6ee9c492009-07-13 10:04:34 -07001296 if (mMenuIsDown) {
1297 // The shortcut action consumes the MENU. Even if it is still down,
1298 // it won't trigger the next shortcut action. In the case of the
1299 // shortcut action triggering a new activity, like Bookmarks, we
1300 // won't get onKeyUp for MENU. So it is important to reset it here.
1301 mMenuIsDown = false;
1302 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001303 switch (item.getItemId()) {
1304 // -- Main menu
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001305 case R.id.new_tab_menu_id:
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001306 openTabToHomePage();
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001307 break;
1308
Michael Kolbae62fd42010-08-18 16:33:28 -07001309 case R.id.incognito_menu_id:
1310 openIncognitoTab();
1311 break;
1312
Leon Scroggins64b80f32009-08-07 12:03:34 -04001313 case R.id.goto_menu_id:
Leon Scroggins8ad29922010-02-16 12:33:55 -05001314 editUrl();
Leon Scrogginsb3a5bed2009-09-28 11:21:56 -04001315 break;
1316
1317 case R.id.bookmarks_menu_id:
Michael Kolbe421c242010-10-04 19:29:01 -07001318 bookmarksOrHistoryPicker(false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001319 break;
1320
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001321 case R.id.active_tabs_menu_id:
1322 mActiveTabsPage = new ActiveTabsPage(this, mTabControl);
1323 removeTabFromContentView(mTabControl.getCurrentTab());
Leon Scrogginsd746a942010-05-19 13:21:44 -04001324 mTitleBar.setVisibility(View.GONE);
Leon Scroggins43de6162009-09-14 19:59:58 -04001325 hideFakeTitleBar();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001326 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
1327 mActiveTabsPage.requestFocus();
1328 mMenuState = EMPTY_MENU;
1329 break;
1330
Leon Scroggins1f005d32009-08-10 17:36:42 -04001331 case R.id.add_bookmark_menu_id:
Michael Kolb370a4f32010-10-06 10:45:32 -07001332 bookmarkCurrentPage(AddBookmarkPage.DEFAULT_FOLDER_ID);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001333 break;
1334
1335 case R.id.stop_reload_menu_id:
1336 if (mInLoad) {
1337 stopLoading();
1338 } else {
1339 getTopWindow().reload();
1340 }
1341 break;
1342
1343 case R.id.back_menu_id:
1344 getTopWindow().goBack();
1345 break;
1346
1347 case R.id.forward_menu_id:
1348 getTopWindow().goForward();
1349 break;
1350
1351 case R.id.close_menu_id:
1352 // Close the subwindow if it exists.
1353 if (mTabControl.getCurrentSubWindow() != null) {
1354 dismissSubWindow(mTabControl.getCurrentTab());
1355 break;
1356 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001357 closeCurrentWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001358 break;
1359
1360 case R.id.homepage_menu_id:
Grace Kloba22ac16e2009-10-07 18:00:23 -07001361 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001362 if (current != null) {
1363 dismissSubWindow(current);
Leon Scroggins92472e82010-02-17 16:32:28 -05001364 loadUrl(current.getWebView(), mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001365 }
1366 break;
1367
1368 case R.id.preferences_menu_id:
1369 Intent intent = new Intent(this,
1370 BrowserPreferencesPage.class);
Leon Scrogginsd5304942009-12-10 16:11:39 -05001371 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1372 getTopWindow().getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001373 startActivityForResult(intent, PREFERENCES_PAGE);
1374 break;
1375
1376 case R.id.find_menu_id:
Leon Scroggins4fc12cb2010-08-24 14:24:05 -04001377 getTopWindow().showFindDialog(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001378 break;
1379
The Android Open Source Project0c908882009-03-03 19:32:16 -08001380 case R.id.page_info_menu_id:
1381 showPageInfo(mTabControl.getCurrentTab(), false);
1382 break;
1383
1384 case R.id.classic_history_menu_id:
Michael Kolbe421c242010-10-04 19:29:01 -07001385 bookmarksOrHistoryPicker(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001386 break;
1387
Leon Scroggins96afcb12009-12-10 12:35:56 -05001388 case R.id.title_bar_share_page_url:
The Android Open Source Project0c908882009-03-03 19:32:16 -08001389 case R.id.share_page_menu_id:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001390 Tab currentTab = mTabControl.getCurrentTab();
1391 if (null == currentTab) {
1392 mCanChord = false;
1393 return false;
1394 }
1395 currentTab.populatePickerData();
1396 sharePage(this, currentTab.getTitle(),
1397 currentTab.getUrl(), currentTab.getFavicon(),
Ben Murdoch87cc65d2010-06-29 20:34:10 +01001398 createScreenshot(currentTab.getWebView(), getDesiredThumbnailWidth(this),
1399 getDesiredThumbnailHeight(this)));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001400 break;
1401
1402 case R.id.dump_nav_menu_id:
1403 getTopWindow().debugDump();
1404 break;
1405
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001406 case R.id.dump_counters_menu_id:
1407 getTopWindow().dumpV8Counters();
1408 break;
1409
The Android Open Source Project0c908882009-03-03 19:32:16 -08001410 case R.id.zoom_in_menu_id:
1411 getTopWindow().zoomIn();
1412 break;
1413
1414 case R.id.zoom_out_menu_id:
1415 getTopWindow().zoomOut();
1416 break;
1417
1418 case R.id.view_downloads_menu_id:
Leon Scroggins IIIa682a3c2010-09-27 12:32:40 -04001419 viewDownloads();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001420 break;
1421
The Android Open Source Project0c908882009-03-03 19:32:16 -08001422 case R.id.window_one_menu_id:
1423 case R.id.window_two_menu_id:
1424 case R.id.window_three_menu_id:
1425 case R.id.window_four_menu_id:
1426 case R.id.window_five_menu_id:
1427 case R.id.window_six_menu_id:
1428 case R.id.window_seven_menu_id:
1429 case R.id.window_eight_menu_id:
1430 {
1431 int menuid = item.getItemId();
1432 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1433 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001434 Tab desiredTab = mTabControl.getTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001435 if (desiredTab != null &&
1436 desiredTab != mTabControl.getCurrentTab()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001437 switchToTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001438 }
1439 break;
1440 }
1441 }
1442 }
1443 break;
1444
1445 default:
1446 if (!super.onOptionsItemSelected(item)) {
1447 return false;
1448 }
1449 // Otherwise fall through.
1450 }
1451 mCanChord = false;
1452 return true;
1453 }
1454
Michael Kolb370a4f32010-10-06 10:45:32 -07001455 /**
1456 * add the current page as a bookmark to the given folder id
1457 * @param folderId use -1 for the default folder
1458 */
1459 /* package */ void bookmarkCurrentPage(long folderId) {
Leon Scroggins571b3762010-05-26 10:25:01 -04001460 Intent i = new Intent(BrowserActivity.this,
1461 AddBookmarkPage.class);
1462 WebView w = getTopWindow();
Leon Scrogginsbc922852010-10-22 12:15:27 -04001463 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1464 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Leon Scroggins88d08032010-10-21 15:17:10 -04001465 String touchIconUrl = w.getTouchIconUrl();
1466 if (touchIconUrl != null) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001467 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -04001468 WebSettings settings = w.getSettings();
1469 if (settings != null) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001470 i.putExtra(AddBookmarkPage.USER_AGENT,
1471 settings.getUserAgentString());
Leon Scroggins88d08032010-10-21 15:17:10 -04001472 }
1473 }
Leon Scrogginsbc922852010-10-22 12:15:27 -04001474 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1475 createScreenshot(w, getDesiredThumbnailWidth(this),
Ben Murdoch87cc65d2010-06-29 20:34:10 +01001476 getDesiredThumbnailHeight(this)));
Leon Scrogginsbc922852010-10-22 12:15:27 -04001477 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
Michael Kolb370a4f32010-10-06 10:45:32 -07001478 i.putExtra(BrowserContract.Bookmarks.PARENT,
1479 folderId);
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -04001480 // Put the dialog at the upper right of the screen, covering the
1481 // star on the title bar.
1482 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
Leon Scroggins571b3762010-05-26 10:25:01 -04001483 startActivity(i);
1484 }
1485
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -04001486 /*
1487 * True if a custom ActionMode (i.e. find or select) is in use.
1488 */
1489 private boolean isInCustomActionMode() {
1490 return mActionMode != null;
Cary Clark01cfcdd2010-06-04 16:36:45 -04001491 }
1492
1493 /*
Leon Scroggins4fc12cb2010-08-24 14:24:05 -04001494 * End the current ActionMode.
Cary Clark01cfcdd2010-06-04 16:36:45 -04001495 */
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -04001496 void endActionMode() {
1497 if (mActionMode != null) {
Leon Scroggins4fc12cb2010-08-24 14:24:05 -04001498 ActionMode mode = mActionMode;
1499 onEndActionMode();
1500 mode.finish();
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -04001501 }
1502 }
1503
1504 /*
1505 * Called by find and select when they are finished. Replace title bars
1506 * as necessary.
1507 */
1508 public void onEndActionMode() {
1509 if (!isInCustomActionMode()) return;
Leon Scroggins79e36d92010-04-29 16:01:46 +01001510 if (mInLoad) {
1511 // The title bar was hidden, because otherwise it would cover up the
Michael Kolba2b2ba82010-08-04 17:54:03 -07001512 // find or select dialog. Now that the dialog has been removed,
Cary Clark01cfcdd2010-06-04 16:36:45 -04001513 // show the fake title bar once again.
Leon Scroggins79e36d92010-04-29 16:01:46 +01001514 showFakeTitleBar();
1515 }
Leon Scroggins4fc12cb2010-08-24 14:24:05 -04001516 // Would like to return the menu state to normal, but this does not
1517 // necessarily get called.
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -04001518 mActionMode = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001519 }
1520
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -04001521 // For select and find, we keep track of the ActionMode so that
1522 // finish() can be called as desired.
1523 private ActionMode mActionMode;
1524
Grace Kloba22ac16e2009-10-07 18:00:23 -07001525 @Override
1526 public boolean onPrepareOptionsMenu(Menu menu) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001527 // This happens when the user begins to hold down the menu key, so
1528 // allow them to chord to get a shortcut.
1529 mCanChord = true;
1530 // Note: setVisible will decide whether an item is visible; while
1531 // setEnabled() will decide whether an item is enabled, which also means
1532 // whether the matching shortcut key will function.
1533 super.onPrepareOptionsMenu(menu);
1534 switch (mMenuState) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001535 case EMPTY_MENU:
1536 if (mCurrentMenuState != mMenuState) {
1537 menu.setGroupVisible(R.id.MAIN_MENU, false);
1538 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1539 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001540 }
1541 break;
1542 default:
1543 if (mCurrentMenuState != mMenuState) {
1544 menu.setGroupVisible(R.id.MAIN_MENU, true);
1545 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1546 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001547 }
1548 final WebView w = getTopWindow();
1549 boolean canGoBack = false;
1550 boolean canGoForward = false;
1551 boolean isHome = false;
1552 if (w != null) {
1553 canGoBack = w.canGoBack();
1554 canGoForward = w.canGoForward();
1555 isHome = mSettings.getHomePage().equals(w.getUrl());
1556 }
1557 final MenuItem back = menu.findItem(R.id.back_menu_id);
1558 back.setEnabled(canGoBack);
1559
1560 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1561 home.setEnabled(!isHome);
1562
Michael Kolbe0a36662010-06-29 10:37:12 -07001563 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1564 forward.setEnabled(canGoForward);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001565
Michael Kolbed217742010-08-10 17:52:34 -07001566 if (!mXLargeScreenSize) {
1567 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
1568 newtab.setEnabled(mTabControl.canCreateNewTab());
1569 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001570 // decide whether to show the share link option
1571 PackageManager pm = getPackageManager();
1572 Intent send = new Intent(Intent.ACTION_SEND);
1573 send.setType("text/plain");
1574 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1575 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1576
The Android Open Source Project0c908882009-03-03 19:32:16 -08001577 boolean isNavDump = mSettings.isNavDump();
1578 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1579 nav.setVisible(isNavDump);
1580 nav.setEnabled(isNavDump);
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001581
1582 boolean showDebugSettings = mSettings.showDebugSettings();
1583 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1584 counter.setVisible(showDebugSettings);
1585 counter.setEnabled(showDebugSettings);
1586
The Android Open Source Project0c908882009-03-03 19:32:16 -08001587 break;
1588 }
1589 mCurrentMenuState = mMenuState;
1590 return true;
1591 }
1592
1593 @Override
1594 public void onCreateContextMenu(ContextMenu menu, View v,
1595 ContextMenuInfo menuInfo) {
Leon Scroggins571b3762010-05-26 10:25:01 -04001596 if (v instanceof TitleBarBase) {
Leon Scroggins4e9f89b2010-02-22 16:54:14 -05001597 return;
1598 }
Michael Kolb772927a2010-10-13 19:23:42 -07001599 if (!(v instanceof WebView)) {
1600 return;
1601 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001602 WebView webview = (WebView) v;
1603 WebView.HitTestResult result = webview.getHitTestResult();
1604 if (result == null) {
1605 return;
1606 }
1607
1608 int type = result.getType();
1609 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1610 Log.w(LOGTAG,
1611 "We should not show context menu when nothing is touched");
1612 return;
1613 }
1614 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1615 // let TextView handles context menu
1616 return;
1617 }
1618
1619 // Note, http://b/issue?id=1106666 is requesting that
1620 // an inflated menu can be used again. This is not available
1621 // yet, so inflate each time (yuk!)
1622 MenuInflater inflater = getMenuInflater();
1623 inflater.inflate(R.menu.browsercontext, menu);
1624
1625 // Show the correct menu group
Leon Scroggins III9e997c72010-05-26 13:25:16 -04001626 final String extra = result.getExtra();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001627 menu.setGroupVisible(R.id.PHONE_MENU,
1628 type == WebView.HitTestResult.PHONE_TYPE);
1629 menu.setGroupVisible(R.id.EMAIL_MENU,
1630 type == WebView.HitTestResult.EMAIL_TYPE);
1631 menu.setGroupVisible(R.id.GEO_MENU,
1632 type == WebView.HitTestResult.GEO_TYPE);
1633 menu.setGroupVisible(R.id.IMAGE_MENU,
1634 type == WebView.HitTestResult.IMAGE_TYPE
1635 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1636 menu.setGroupVisible(R.id.ANCHOR_MENU,
1637 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1638 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1639
1640 // Setup custom handling depending on the type
1641 switch (type) {
1642 case WebView.HitTestResult.PHONE_TYPE:
1643 menu.setHeaderTitle(Uri.decode(extra));
1644 menu.findItem(R.id.dial_context_menu_id).setIntent(
1645 new Intent(Intent.ACTION_VIEW, Uri
1646 .parse(WebView.SCHEME_TEL + extra)));
1647 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1648 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
Cary Clark5e335a32009-09-22 14:53:11 -04001649 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001650 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1651 addIntent);
1652 menu.findItem(R.id.copy_phone_context_menu_id).setOnMenuItemClickListener(
1653 new Copy(extra));
1654 break;
1655
1656 case WebView.HitTestResult.EMAIL_TYPE:
1657 menu.setHeaderTitle(extra);
1658 menu.findItem(R.id.email_context_menu_id).setIntent(
1659 new Intent(Intent.ACTION_VIEW, Uri
1660 .parse(WebView.SCHEME_MAILTO + extra)));
1661 menu.findItem(R.id.copy_mail_context_menu_id).setOnMenuItemClickListener(
1662 new Copy(extra));
1663 break;
1664
1665 case WebView.HitTestResult.GEO_TYPE:
1666 menu.setHeaderTitle(extra);
1667 menu.findItem(R.id.map_context_menu_id).setIntent(
1668 new Intent(Intent.ACTION_VIEW, Uri
1669 .parse(WebView.SCHEME_GEO
1670 + URLEncoder.encode(extra))));
1671 menu.findItem(R.id.copy_geo_context_menu_id).setOnMenuItemClickListener(
1672 new Copy(extra));
1673 break;
1674
1675 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1676 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
1677 TextView titleView = (TextView) LayoutInflater.from(this)
1678 .inflate(android.R.layout.browser_link_context_header,
1679 null);
1680 titleView.setText(extra);
1681 menu.setHeaderView(titleView);
1682 // decide whether to show the open link in new tab option
Leon Scroggins III9e997c72010-05-26 13:25:16 -04001683 boolean showNewTab = mTabControl.canCreateNewTab();
1684 MenuItem newTabItem
1685 = menu.findItem(R.id.open_newtab_context_menu_id);
1686 newTabItem.setVisible(showNewTab);
1687 if (showNewTab) {
1688 newTabItem.setOnMenuItemClickListener(
1689 new MenuItem.OnMenuItemClickListener() {
1690 public boolean onMenuItemClick(MenuItem item) {
1691 final Tab parent = mTabControl.getCurrentTab();
Michael Kolb68792c82010-08-09 16:39:18 -07001692 final Tab newTab = openTab(extra, false);
Leon Scroggins III9e997c72010-05-26 13:25:16 -04001693 if (newTab != parent) {
1694 parent.addChildTab(newTab);
1695 }
1696 return true;
1697 }
1698 });
1699 }
Ben Murdochde353622009-10-12 10:29:00 +01001700 menu.findItem(R.id.bookmark_context_menu_id).setVisible(
1701 Bookmarks.urlHasAcceptableScheme(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001702 PackageManager pm = getPackageManager();
1703 Intent send = new Intent(Intent.ACTION_SEND);
1704 send.setType("text/plain");
1705 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1706 menu.findItem(R.id.share_link_context_menu_id).setVisible(ri != null);
1707 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1708 break;
1709 }
1710 // otherwise fall through to handle image part
1711 case WebView.HitTestResult.IMAGE_TYPE:
1712 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1713 menu.setHeaderTitle(extra);
1714 }
1715 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1716 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1717 menu.findItem(R.id.download_context_menu_id).
1718 setOnMenuItemClickListener(new Download(extra));
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001719 menu.findItem(R.id.set_wallpaper_context_menu_id).
1720 setOnMenuItemClickListener(new SetAsWallpaper(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001721 break;
1722
1723 default:
1724 Log.w(LOGTAG, "We should not get here.");
1725 break;
1726 }
Leon Scrogginsb2b19f52009-10-09 16:10:00 -04001727 hideFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001728 }
1729
The Android Open Source Project0c908882009-03-03 19:32:16 -08001730 // Attach the given tab to the content view.
Grace Klobac928c302009-09-17 11:51:21 -07001731 // this should only be called for the current tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001732 private void attachTabToContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001733 // Attach the container that contains the main WebView and any other UI
1734 // associated with the tab.
Patrick Scottd0119532009-09-17 08:00:31 -04001735 t.attachTabToContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001736
1737 if (mShouldShowErrorConsole) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 ErrorConsoleView errorConsole = t.getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01001739 if (errorConsole.numberOfErrors() == 0) {
1740 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
1741 } else {
1742 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1743 }
1744
1745 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08001746 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01001747 ViewGroup.LayoutParams.WRAP_CONTENT));
1748 }
1749
Michael Kolba2b2ba82010-08-04 17:54:03 -07001750 WebView view = t.getWebView();
1751 view.setEmbeddedTitleBar(mTitleBar);
Leon Scroggins58d56c62010-01-28 15:12:40 -05001752 if (t.isInVoiceSearchMode()) {
1753 showVoiceTitleBar(t.getVoiceDisplayTitle());
1754 } else {
1755 revertVoiceTitleBar();
1756 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001757 // Request focus on the top window.
1758 t.getTopWindow().requestFocus();
Michael Kolba2b2ba82010-08-04 17:54:03 -07001759 if (mTabControl.getTabChangeListener() != null) {
1760 mTabControl.getTabChangeListener().onCurrentTab(t);
1761 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001762 }
1763
1764 // Attach a sub window to the main WebView of the given tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001765 void attachSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001766 t.attachSubWindow(mContentView);
1767 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001768 }
1769
1770 // Remove the given tab from the content view.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001771 private void removeTabFromContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001772 // Remove the container that contains the main WebView.
Patrick Scottd0119532009-09-17 08:00:31 -04001773 t.removeTabFromContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001774
Grace Kloba22ac16e2009-10-07 18:00:23 -07001775 ErrorConsoleView errorConsole = t.getErrorConsole(false);
1776 if (errorConsole != null) {
1777 mErrorConsoleContainer.removeView(errorConsole);
Ben Murdochbff2d602009-07-01 20:19:05 +01001778 }
1779
Michael Kolba2b2ba82010-08-04 17:54:03 -07001780 WebView view = t.getWebView();
1781 if (view != null) {
1782 view.setEmbeddedTitleBar(null);
Leon Scrogginsbb85b902009-09-14 19:27:20 -04001783 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001784 }
1785
1786 // Remove the sub window if it exists. Also called by TabControl when the
1787 // user clicks the 'X' to dismiss a sub window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001788 /* package */ void dismissSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001789 t.removeSubWindow(mContentView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001790 // dismiss the subwindow. This will destroy the WebView.
1791 t.dismissSubWindow();
Patrick Scottd0119532009-09-17 08:00:31 -04001792 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001793 }
1794
Leon Scroggins1f005d32009-08-10 17:36:42 -04001795 // A wrapper function of {@link #openTabAndShow(UrlData, boolean, String)}
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07001796 // that accepts url as string.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001797 private Tab openTabAndShow(String url, boolean closeOnExit, String appId) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001798 return openTabAndShow(new UrlData(url), closeOnExit, appId);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001799 }
1800
1801 // This method does a ton of stuff. It will attempt to create a new tab
1802 // if we haven't reached MAX_TABS. Otherwise it uses the current tab. If
Leon Scroggins1f005d32009-08-10 17:36:42 -04001803 // url isn't null, it will load the given url.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001804 /* package */Tab openTabAndShow(UrlData urlData, boolean closeOnExit,
1805 String appId) {
1806 final Tab currentTab = mTabControl.getCurrentTab();
1807 if (mTabControl.canCreateNewTab()) {
1808 final Tab tab = mTabControl.createNewTab(closeOnExit, appId,
Elliott Slaughterf0f03952010-07-14 18:10:36 -07001809 urlData.mUrl, false);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001810 WebView webview = tab.getWebView();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001811 // If the last tab was removed from the active tabs page, currentTab
1812 // will be null.
1813 if (currentTab != null) {
1814 removeTabFromContentView(currentTab);
1815 }
Patrick Scott8bbd69f2009-08-14 13:35:53 -04001816 // We must set the new tab as the current tab to reflect the old
1817 // animation behavior.
1818 mTabControl.setCurrentTab(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001819 attachTabToContentView(tab);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001820 if (!urlData.isEmpty()) {
Patrick Scott9d53da02010-02-19 10:19:01 -05001821 loadUrlDataIn(tab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001822 }
1823 return tab;
1824 } else {
1825 // Get rid of the subwindow if it exists
1826 dismissSubWindow(currentTab);
1827 if (!urlData.isEmpty()) {
1828 // Load the given url.
Patrick Scott9d53da02010-02-19 10:19:01 -05001829 loadUrlDataIn(currentTab, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001830 }
Leon Scroggins58d56c62010-01-28 15:12:40 -05001831 return currentTab;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001832 }
1833 }
1834
Michael Kolb68792c82010-08-09 16:39:18 -07001835 private Tab openTab(String url, boolean forceForeground) {
1836 if (mSettings.openInBackground() && !forceForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001837 Tab t = mTabControl.createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001838 if (t != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001839 WebView view = t.getWebView();
Leon Scroggins92472e82010-02-17 16:32:28 -05001840 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001841 }
Grace Klobac9181842009-04-14 08:53:22 -07001842 return t;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001843 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001844 return openTabAndShow(url, false, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001845 }
1846 }
1847
Elliott Slaughterf0f03952010-07-14 18:10:36 -07001848 /* package */ Tab openIncognitoTab() {
1849 if (mTabControl.canCreateNewTab()) {
1850 Tab currentTab = mTabControl.getCurrentTab();
1851 Tab tab = mTabControl.createNewTab(false, null, null, true);
1852 if (currentTab != null) {
1853 removeTabFromContentView(currentTab);
1854 }
1855 mTabControl.setCurrentTab(tab);
1856 attachTabToContentView(tab);
1857 return tab;
1858 }
1859 return null;
1860 }
1861
The Android Open Source Project0c908882009-03-03 19:32:16 -08001862 private class Copy implements OnMenuItemClickListener {
1863 private CharSequence mText;
1864
1865 public boolean onMenuItemClick(MenuItem item) {
1866 copy(mText);
1867 return true;
1868 }
1869
1870 public Copy(CharSequence toCopy) {
1871 mText = toCopy;
1872 }
1873 }
1874
1875 private class Download implements OnMenuItemClickListener {
1876 private String mText;
1877
1878 public boolean onMenuItemClick(MenuItem item) {
1879 onDownloadStartNoStream(mText, null, null, null, -1);
1880 return true;
1881 }
1882
1883 public Download(String toDownload) {
1884 mText = toDownload;
1885 }
1886 }
1887
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001888 private class SetAsWallpaper extends Thread implements
1889 OnMenuItemClickListener, DialogInterface.OnCancelListener {
1890 private URL mUrl;
1891 private ProgressDialog mWallpaperProgress;
1892 private boolean mCanceled = false;
1893
1894 public SetAsWallpaper(String url) {
1895 try {
1896 mUrl = new URL(url);
1897 } catch (MalformedURLException e) {
1898 mUrl = null;
1899 }
1900 }
1901
1902 public void onCancel(DialogInterface dialog) {
1903 mCanceled = true;
1904 }
1905
1906 public boolean onMenuItemClick(MenuItem item) {
1907 if (mUrl != null) {
1908 // The user may have tried to set a image with a large file size as their
1909 // background so it may take a few moments to perform the operation. Display
1910 // a progress spinner while it is working.
1911 mWallpaperProgress = new ProgressDialog(BrowserActivity.this);
1912 mWallpaperProgress.setIndeterminate(true);
1913 mWallpaperProgress.setMessage(getText(R.string.progress_dialog_setting_wallpaper));
1914 mWallpaperProgress.setCancelable(true);
1915 mWallpaperProgress.setOnCancelListener(this);
1916 mWallpaperProgress.show();
1917 start();
1918 }
1919 return true;
1920 }
1921
Michael Kolbe0a36662010-06-29 10:37:12 -07001922 @Override
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001923 public void run() {
1924 Drawable oldWallpaper = BrowserActivity.this.getWallpaper();
1925 try {
1926 // TODO: This will cause the resource to be downloaded again, when we
1927 // should in most cases be able to grab it from the cache. To fix this
1928 // we should query WebCore to see if we can access a cached version and
1929 // instead open an input stream on that. This pattern could also be used
1930 // in the download manager where the same problem exists.
1931 InputStream inputstream = mUrl.openStream();
1932 if (inputstream != null) {
1933 setWallpaper(inputstream);
1934 }
1935 } catch (IOException e) {
1936 Log.e(LOGTAG, "Unable to set new wallpaper");
1937 // Act as though the user canceled the operation so we try to
1938 // restore the old wallpaper.
1939 mCanceled = true;
1940 }
1941
1942 if (mCanceled) {
1943 // Restore the old wallpaper if the user cancelled whilst we were setting
1944 // the new wallpaper.
1945 int width = oldWallpaper.getIntrinsicWidth();
1946 int height = oldWallpaper.getIntrinsicHeight();
1947 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1948 Canvas canvas = new Canvas(bm);
1949 oldWallpaper.setBounds(0, 0, width, height);
1950 oldWallpaper.draw(canvas);
1951 try {
1952 setWallpaper(bm);
1953 } catch (IOException e) {
1954 Log.e(LOGTAG, "Unable to restore old wallpaper.");
1955 }
1956 mCanceled = false;
1957 }
1958
1959 if (mWallpaperProgress.isShowing()) {
1960 mWallpaperProgress.dismiss();
1961 }
1962 }
1963 }
1964
The Android Open Source Project0c908882009-03-03 19:32:16 -08001965 private void copy(CharSequence text) {
Dianne Hackborn80f32622010-08-05 14:17:53 -07001966 ClipboardManager cm = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
1967 cm.setText(text);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001968 }
1969
1970 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001971 * Resets the browser title-view to whatever it must be
1972 * (for example, if we had a loading error)
1973 * When we have a new page, we call resetTitle, when we
1974 * have to reset the titlebar to whatever it used to be
1975 * (for example, if the user chose to stop loading), we
1976 * call resetTitleAndRevertLockIcon.
1977 */
1978 /* package */ void resetTitleAndRevertLockIcon() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001979 mTabControl.getCurrentTab().revertLockIcon();
1980 updateLockIconToLatest();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001981 resetTitleIconAndProgress();
1982 }
1983
1984 /**
1985 * Reset the title, favicon, and progress.
1986 */
1987 private void resetTitleIconAndProgress() {
1988 WebView current = mTabControl.getCurrentWebView();
1989 if (current == null) {
1990 return;
1991 }
1992 resetTitleAndIcon(current);
1993 int progress = current.getProgress();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001994 current.getWebChromeClient().onProgressChanged(current, progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001995 }
1996
1997 // Reset the title and the icon based on the given item.
1998 private void resetTitleAndIcon(WebView view) {
1999 WebHistoryItem item = view.copyBackForwardList().getCurrentItem();
2000 if (item != null) {
Leon Scroggins68579392009-09-15 15:31:54 -04002001 setUrlTitle(item.getUrl(), item.getTitle());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002002 setFavicon(item.getFavicon());
2003 } else {
Leon Scroggins68579392009-09-15 15:31:54 -04002004 setUrlTitle(null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002005 setFavicon(null);
2006 }
2007 }
2008
2009 /**
2010 * Sets a title composed of the URL and the title string.
2011 * @param url The URL of the site being loaded.
2012 * @param title The title of the site being loaded.
2013 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002014 void setUrlTitle(String url, String title) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002015 mUrl = url;
2016 mTitle = title;
2017
Leon Scroggins58d56c62010-01-28 15:12:40 -05002018 // If we are in voice search mode, the title has already been set.
2019 if (mTabControl.getCurrentTab().isInVoiceSearchMode()) return;
2020 mTitleBar.setDisplayTitle(url);
Michael Kolba2b2ba82010-08-04 17:54:03 -07002021 mFakeTitleBar.setDisplayTitle(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002022 }
2023
2024 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08002025 * @param url The URL to build a title version of the URL from.
2026 * @return The title version of the URL or null if fails.
2027 * The title version of the URL can be either the URL hostname,
2028 * or the hostname with an "https://" prefix (for secure URLs),
2029 * or an empty string if, for example, the URL in question is a
2030 * file:// URL with no hostname.
2031 */
Leon Scroggins32e14a62009-06-11 10:26:34 -04002032 /* package */ static String buildTitleUrl(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002033 String titleUrl = null;
2034
2035 if (url != null) {
2036 try {
2037 // parse the url string
2038 URL urlObj = new URL(url);
2039 if (urlObj != null) {
2040 titleUrl = "";
2041
2042 String protocol = urlObj.getProtocol();
2043 String host = urlObj.getHost();
2044
2045 if (host != null && 0 < host.length()) {
2046 titleUrl = host;
2047 if (protocol != null) {
2048 // if a secure site, add an "https://" prefix!
2049 if (protocol.equalsIgnoreCase("https")) {
2050 titleUrl = protocol + "://" + host;
2051 }
2052 }
2053 }
2054 }
2055 } catch (MalformedURLException e) {}
2056 }
2057
2058 return titleUrl;
2059 }
2060
2061 // Set the favicon in the title bar.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002062 void setFavicon(Bitmap icon) {
Leon Scroggins68579392009-09-15 15:31:54 -04002063 mTitleBar.setFavicon(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -07002064 mFakeTitleBar.setFavicon(icon);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002065 }
2066
2067 /**
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002068 * Close the tab, remove its associated title bar, and adjust mTabControl's
2069 * current tab to a valid value.
Leon Scroggins1f005d32009-08-10 17:36:42 -04002070 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002071 /* package */ void closeTab(Tab t) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002072 int currentIndex = mTabControl.getCurrentIndex();
2073 int removeIndex = mTabControl.getTabIndex(t);
Leon Scroggins1f005d32009-08-10 17:36:42 -04002074 mTabControl.removeTab(t);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002075 if (currentIndex >= removeIndex && currentIndex != 0) {
2076 currentIndex--;
2077 }
2078 mTabControl.setCurrentTab(mTabControl.getTab(currentIndex));
Andrei Popescua5bf1de2009-09-23 16:39:23 +01002079 resetTitleIconAndProgress();
Leon Scroggins654899b2010-06-25 16:25:23 -04002080 updateLockIconToLatest();
Elliott Slaughtere440a882010-08-20 13:54:45 -07002081
2082 if (!mTabControl.hasAnyOpenIncognitoTabs()) {
2083 WebView.cleanupPrivateBrowsingFiles(this);
2084 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002085 }
2086
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05002087 /* package */ void goBackOnePageOrQuit() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002088 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002089 if (current == null) {
2090 /*
2091 * Instead of finishing the activity, simply push this to the back
2092 * of the stack and let ActivityManager to choose the foreground
2093 * activity. As BrowserActivity is singleTask, it will be always the
2094 * root of the task. So we can use either true or false for
2095 * moveTaskToBack().
2096 */
2097 moveTaskToBack(true);
Grace Kloba00d85e72009-09-23 18:50:05 -07002098 return;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002099 }
2100 WebView w = current.getWebView();
2101 if (w.canGoBack()) {
2102 w.goBack();
2103 } else {
2104 // Check to see if we are closing a window that was created by
2105 // another window. If so, we switch back to that window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002106 Tab parent = current.getParentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002107 if (parent != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04002108 switchToTab(mTabControl.getTabIndex(parent));
2109 // Now we close the other tab
2110 closeTab(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002111 } else {
2112 if (current.closeOnExit()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002113 // force the tab's inLoad() to be false as we are going to
2114 // either finish the activity or remove the tab. This will
2115 // ensure pauseWebViewTimers() taking action.
2116 mTabControl.getCurrentTab().clearInLoad();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002117 if (mTabControl.getTabCount() == 1) {
2118 finish();
2119 return;
2120 }
Mike Reed7bfa63b2009-05-28 11:08:32 -04002121 // call pauseWebViewTimers() now, we won't be able to call
2122 // it in onPause() as the WebView won't be valid.
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002123 // Temporarily change mActivityInPause to be true as
2124 // pauseWebViewTimers() will do nothing if mActivityInPause
2125 // is false.
Grace Kloba918e1d72009-08-13 14:55:06 -07002126 boolean savedState = mActivityInPause;
2127 if (savedState) {
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002128 Log.e(LOGTAG, "BrowserActivity is already paused "
2129 + "while handing goBackOnePageOrQuit.");
Grace Kloba918e1d72009-08-13 14:55:06 -07002130 }
2131 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -04002132 pauseWebViewTimers();
Grace Kloba918e1d72009-08-13 14:55:06 -07002133 mActivityInPause = savedState;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002134 removeTabFromContentView(current);
2135 mTabControl.removeTab(current);
2136 }
2137 /*
2138 * Instead of finishing the activity, simply push this to the back
2139 * of the stack and let ActivityManager to choose the foreground
2140 * activity. As BrowserActivity is singleTask, it will be always the
2141 * root of the task. So we can use either true or false for
2142 * moveTaskToBack().
2143 */
2144 moveTaskToBack(true);
2145 }
2146 }
2147 }
2148
Grace Kloba22ac16e2009-10-07 18:00:23 -07002149 boolean isMenuDown() {
2150 return mMenuIsDown;
2151 }
2152
Grace Kloba5942df02009-09-18 11:48:29 -07002153 @Override
2154 public boolean onKeyDown(int keyCode, KeyEvent event) {
Leon Scrogginsf65b50d2009-12-08 10:44:28 -05002155 // Even if MENU is already held down, we need to call to super to open
2156 // the IME on long press.
2157 if (KeyEvent.KEYCODE_MENU == keyCode) {
2158 mMenuIsDown = true;
2159 return super.onKeyDown(keyCode, event);
2160 }
Grace Kloba5942df02009-09-18 11:48:29 -07002161 // The default key mode is DEFAULT_KEYS_SEARCH_LOCAL. As the MENU is
2162 // still down, we don't want to trigger the search. Pretend to consume
2163 // the key and do nothing.
2164 if (mMenuIsDown) return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002165
Grace Kloba5942df02009-09-18 11:48:29 -07002166 switch(keyCode) {
Grace Kloba5942df02009-09-18 11:48:29 -07002167 case KeyEvent.KEYCODE_SPACE:
Grace Klobada0fe552009-09-22 18:17:24 -07002168 // WebView/WebTextView handle the keys in the KeyDown. As
2169 // the Activity's shortcut keys are only handled when WebView
2170 // doesn't, have to do it in onKeyDown instead of onKeyUp.
2171 if (event.isShiftPressed()) {
2172 getTopWindow().pageUp(false);
2173 } else {
2174 getTopWindow().pageDown(false);
2175 }
Grace Kloba5942df02009-09-18 11:48:29 -07002176 return true;
2177 case KeyEvent.KEYCODE_BACK:
2178 if (event.getRepeatCount() == 0) {
2179 event.startTracking();
2180 return true;
2181 } else if (mCustomView == null && mActiveTabsPage == null
Michael Kolbe421c242010-10-04 19:29:01 -07002182 && mComboView == null
Grace Kloba5942df02009-09-18 11:48:29 -07002183 && event.isLongPress()) {
Michael Kolbe421c242010-10-04 19:29:01 -07002184 bookmarksOrHistoryPicker(true);
Grace Kloba5942df02009-09-18 11:48:29 -07002185 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002186 }
Grace Kloba5942df02009-09-18 11:48:29 -07002187 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002188 }
Grace Kloba5942df02009-09-18 11:48:29 -07002189 return super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002190 }
2191
Grace Kloba5942df02009-09-18 11:48:29 -07002192 @Override
2193 public boolean onKeyUp(int keyCode, KeyEvent event) {
2194 switch(keyCode) {
2195 case KeyEvent.KEYCODE_MENU:
2196 mMenuIsDown = false;
2197 break;
Grace Kloba5942df02009-09-18 11:48:29 -07002198 case KeyEvent.KEYCODE_BACK:
2199 if (event.isTracking() && !event.isCanceled()) {
2200 if (mCustomView != null) {
2201 // if a custom view is showing, hide it
Grace Kloba22ac16e2009-10-07 18:00:23 -07002202 mTabControl.getCurrentWebView().getWebChromeClient()
2203 .onHideCustomView();
Grace Kloba5942df02009-09-18 11:48:29 -07002204 } else if (mActiveTabsPage != null) {
2205 // if tab page is showing, hide it
2206 removeActiveTabPage(true);
Michael Kolbe421c242010-10-04 19:29:01 -07002207 } else if (mComboView != null) {
Michael Kolb370a4f32010-10-06 10:45:32 -07002208 if (!mComboView.onBackPressed()) {
2209 removeComboView();
2210 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002211 } else {
Grace Kloba5942df02009-09-18 11:48:29 -07002212 WebView subwindow = mTabControl.getCurrentSubWindow();
2213 if (subwindow != null) {
2214 if (subwindow.canGoBack()) {
2215 subwindow.goBack();
2216 } else {
2217 dismissSubWindow(mTabControl.getCurrentTab());
2218 }
2219 } else {
2220 goBackOnePageOrQuit();
2221 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002222 }
Grace Kloba5942df02009-09-18 11:48:29 -07002223 return true;
2224 }
2225 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002226 }
Grace Kloba5942df02009-09-18 11:48:29 -07002227 return super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002228 }
2229
Leon Scroggins68579392009-09-15 15:31:54 -04002230 /* package */ void stopLoading() {
Ben Murdochb7cc8b42009-09-28 10:59:09 +01002231 mDidStopLoad = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002232 resetTitleAndRevertLockIcon();
2233 WebView w = getTopWindow();
2234 w.stopLoading();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002235 // FIXME: before refactor, it is using mWebViewClient. So I keep the
2236 // same logic here. But for subwindow case, should we call into the main
2237 // WebView's onPageFinished as we never call its onPageStarted and if
2238 // the page finishes itself, we don't call onPageFinished.
2239 mTabControl.getCurrentWebView().getWebViewClient().onPageFinished(w,
2240 w.getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002241
2242 cancelStopToast();
2243 mStopToast = Toast
2244 .makeText(this, R.string.stopping, Toast.LENGTH_SHORT);
2245 mStopToast.show();
2246 }
2247
Grace Kloba22ac16e2009-10-07 18:00:23 -07002248 boolean didUserStopLoading() {
2249 return mDidStopLoad;
2250 }
2251
The Android Open Source Project0c908882009-03-03 19:32:16 -08002252 private void cancelStopToast() {
2253 if (mStopToast != null) {
2254 mStopToast.cancel();
2255 mStopToast = null;
2256 }
2257 }
2258
Grace Kloba22ac16e2009-10-07 18:00:23 -07002259 // called by a UI or non-UI thread to post the message
2260 public void postMessage(int what, int arg1, int arg2, Object obj,
2261 long delayMillis) {
2262 mHandler.sendMessageDelayed(mHandler.obtainMessage(what, arg1, arg2,
2263 obj), delayMillis);
2264 }
2265
2266 // called by a UI or non-UI thread to remove the message
2267 void removeMessages(int what, Object object) {
2268 mHandler.removeMessages(what, object);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002269 }
2270
2271 // public message ids
2272 public final static int LOAD_URL = 1001;
2273 public final static int STOP_LOAD = 1002;
2274
2275 // Message Ids
2276 private static final int FOCUS_NODE_HREF = 102;
Grace Kloba92c18a52009-07-31 23:48:32 -07002277 private static final int RELEASE_WAKELOCK = 107;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002278
Grace Kloba22ac16e2009-10-07 18:00:23 -07002279 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
Ben Murdoch2694e232009-09-29 09:41:11 +01002280
Michael Kolb300b7f02010-08-25 13:47:24 -07002281 private static final int OPEN_BOOKMARKS = 201;
2282
The Android Open Source Project0c908882009-03-03 19:32:16 -08002283 // Private handler for handling javascript and saving passwords
2284 private Handler mHandler = new Handler() {
2285
Michael Kolbe0a36662010-06-29 10:37:12 -07002286 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -08002287 public void handleMessage(Message msg) {
2288 switch (msg.what) {
Michael Kolb300b7f02010-08-25 13:47:24 -07002289 case OPEN_BOOKMARKS:
Michael Kolbe421c242010-10-04 19:29:01 -07002290 bookmarksOrHistoryPicker(false);
Michael Kolb300b7f02010-08-25 13:47:24 -07002291 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002292 case FOCUS_NODE_HREF:
Ben Murdoch2694e232009-09-29 09:41:11 +01002293 {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002294 String url = (String) msg.getData().get("url");
Ben Murdoch90d088c2009-11-17 18:14:04 +00002295 String title = (String) msg.getData().get("title");
The Android Open Source Project0c908882009-03-03 19:32:16 -08002296 if (url == null || url.length() == 0) {
2297 break;
2298 }
2299 HashMap focusNodeMap = (HashMap) msg.obj;
2300 WebView view = (WebView) focusNodeMap.get("webview");
2301 // Only apply the action if the top window did not change.
2302 if (getTopWindow() != view) {
2303 break;
2304 }
2305 switch (msg.arg1) {
2306 case R.id.open_context_menu_id:
2307 case R.id.view_image_context_menu_id:
Leon Scroggins92472e82010-02-17 16:32:28 -05002308 loadUrlFromContext(getTopWindow(), url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002309 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002310 case R.id.bookmark_context_menu_id:
2311 Intent intent = new Intent(BrowserActivity.this,
2312 AddBookmarkPage.class);
Leon Scrogginsbc922852010-10-22 12:15:27 -04002313 intent.putExtra(BrowserContract.Bookmarks.URL, url);
2314 intent.putExtra(BrowserContract.Bookmarks.TITLE,
2315 title);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002316 startActivity(intent);
2317 break;
2318 case R.id.share_link_context_menu_id:
Leon Scroggins3e204452010-05-10 11:06:03 -04002319 sharePage(BrowserActivity.this, title, url, null,
Leon Scroggins96afcb12009-12-10 12:35:56 -05002320 null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002321 break;
2322 case R.id.copy_link_context_menu_id:
2323 copy(url);
2324 break;
2325 case R.id.save_link_context_menu_id:
2326 case R.id.download_context_menu_id:
2327 onDownloadStartNoStream(url, null, null, null, -1);
2328 break;
2329 }
2330 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002331 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002332
2333 case LOAD_URL:
Leon Scroggins92472e82010-02-17 16:32:28 -05002334 loadUrlFromContext(getTopWindow(), (String) msg.obj);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002335 break;
2336
2337 case STOP_LOAD:
2338 stopLoading();
2339 break;
2340
The Android Open Source Project0c908882009-03-03 19:32:16 -08002341 case RELEASE_WAKELOCK:
2342 if (mWakeLock.isHeld()) {
2343 mWakeLock.release();
Grace Kloba5d0e02e2009-10-05 15:15:36 -07002344 // if we reach here, Browser should be still in the
2345 // background loading after WAKELOCK_TIMEOUT (5-min).
2346 // To avoid burning the battery, stop loading.
2347 mTabControl.stopAllLoading();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002348 }
2349 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002350
2351 case UPDATE_BOOKMARK_THUMBNAIL:
2352 WebView view = (WebView) msg.obj;
2353 if (view != null) {
2354 updateScreenshot(view);
2355 }
2356 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002357 }
2358 }
2359 };
2360
Leon Scroggins96afcb12009-12-10 12:35:56 -05002361 /**
2362 * Share a page, providing the title, url, favicon, and a screenshot. Uses
2363 * an {@link Intent} to launch the Activity chooser.
2364 * @param c Context used to launch a new Activity.
2365 * @param title Title of the page. Stored in the Intent with
Paul Westbrook03e6d392010-02-12 10:33:29 -08002366 * {@link Intent#EXTRA_SUBJECT}
Leon Scroggins96afcb12009-12-10 12:35:56 -05002367 * @param url URL of the page. Stored in the Intent with
2368 * {@link Intent#EXTRA_TEXT}
2369 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
2370 * with {@link Browser#EXTRA_SHARE_FAVICON}
2371 * @param screenshot Bitmap of a screenshot of the page. Stored in the
2372 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
2373 */
2374 public static final void sharePage(Context c, String title, String url,
2375 Bitmap favicon, Bitmap screenshot) {
2376 Intent send = new Intent(Intent.ACTION_SEND);
2377 send.setType("text/plain");
2378 send.putExtra(Intent.EXTRA_TEXT, url);
Paul Westbrook03e6d392010-02-12 10:33:29 -08002379 send.putExtra(Intent.EXTRA_SUBJECT, title);
Leon Scroggins96afcb12009-12-10 12:35:56 -05002380 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
2381 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
2382 try {
2383 c.startActivity(Intent.createChooser(send, c.getString(
2384 R.string.choosertitle_sharevia)));
2385 } catch(android.content.ActivityNotFoundException ex) {
2386 // if no app handles it, do nothing
2387 }
2388 }
2389
Leon Scroggins89c6d362009-07-15 16:54:37 -04002390 private void updateScreenshot(WebView view) {
2391 // If this is a bookmarked site, add a screenshot to the database.
2392 // FIXME: When should we update? Every time?
2393 // FIXME: Would like to make sure there is actually something to
2394 // draw, but the API for that (WebViewCore.pictureReady()) is not
2395 // currently accessible here.
Ben Murdochaac7aa62009-09-17 16:57:40 +01002396
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002397 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(this),
2398 getDesiredThumbnailHeight(this));
Patrick Scottcb192b52010-04-14 14:38:55 -04002399 if (bm == null) {
2400 return;
Leon Scroggins89c6d362009-07-15 16:54:37 -04002401 }
Patrick Scottcb192b52010-04-14 14:38:55 -04002402
2403 final ContentResolver cr = getContentResolver();
2404 final String url = view.getUrl();
2405 final String originalUrl = view.getOriginalUrl();
2406
2407 new AsyncTask<Void, Void, Void>() {
2408 @Override
2409 protected Void doInBackground(Void... unused) {
Jeff Hamilton1a805652010-09-07 12:36:30 -07002410 Cursor cursor = null;
Patrick Scottcb192b52010-04-14 14:38:55 -04002411 try {
Jeff Hamilton1a805652010-09-07 12:36:30 -07002412 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2413 if (cursor != null && cursor.moveToFirst()) {
2414 final ByteArrayOutputStream os = new ByteArrayOutputStream();
2415 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
2416
2417 ContentValues values = new ContentValues();
2418 values.put(Images.THUMBNAIL, os.toByteArray());
2419 values.put(Images.URL, cursor.getString(0));
2420
2421 do {
2422 cr.update(Images.CONTENT_URI, values, null, null);
2423 } while (cursor.moveToNext());
Patrick Scottcb192b52010-04-14 14:38:55 -04002424 }
2425 } catch (IllegalStateException e) {
2426 // Ignore
2427 } finally {
Jeff Hamilton1a805652010-09-07 12:36:30 -07002428 if (cursor != null) cursor.close();
Patrick Scottcb192b52010-04-14 14:38:55 -04002429 }
2430 return null;
2431 }
2432 }.execute();
Leon Scroggins89c6d362009-07-15 16:54:37 -04002433 }
2434
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002435 /**
Leon Scrogginsf8551612009-09-24 16:06:02 -04002436 * Return the desired width for thumbnail screenshots, which are stored in
2437 * the database, and used on the bookmarks screen.
2438 * @param context Context for finding out the density of the screen.
Jeff Hamiltonf3026372010-08-19 00:12:27 -05002439 * @return desired width for thumbnail screenshot.
Leon Scrogginsf8551612009-09-24 16:06:02 -04002440 */
2441 /* package */ static int getDesiredThumbnailWidth(Context context) {
Jeff Hamiltonf3026372010-08-19 00:12:27 -05002442 return context.getResources().getDimensionPixelOffset(R.dimen.bookmarkThumbnailWidth);
Leon Scrogginsf8551612009-09-24 16:06:02 -04002443 }
2444
2445 /**
2446 * Return the desired height for thumbnail screenshots, which are stored in
2447 * the database, and used on the bookmarks screen.
2448 * @param context Context for finding out the density of the screen.
Jeff Hamiltonf3026372010-08-19 00:12:27 -05002449 * @return desired height for thumbnail screenshot.
Leon Scrogginsf8551612009-09-24 16:06:02 -04002450 */
2451 /* package */ static int getDesiredThumbnailHeight(Context context) {
Jeff Hamiltonf3026372010-08-19 00:12:27 -05002452 return context.getResources().getDimensionPixelOffset(R.dimen.bookmarkThumbnailHeight);
Leon Scrogginsf8551612009-09-24 16:06:02 -04002453 }
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002454
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002455 private Bitmap createScreenshot(WebView view, int width, int height) {
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002456 Picture thumbnail = view.capturePicture();
Leon Scroggins45800572009-09-29 16:38:47 -04002457 if (thumbnail == null) {
2458 return null;
2459 }
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002460 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002461 Canvas canvas = new Canvas(bm);
2462 // May need to tweak these values to determine what is the
2463 // best scale factor
Ben Murdoch2694e232009-09-29 09:41:11 +01002464 int thumbnailWidth = thumbnail.getWidth();
Ben Murdochae59c3f2009-10-20 18:30:28 +01002465 int thumbnailHeight = thumbnail.getHeight();
2466 float scaleFactorX = 1.0f;
2467 float scaleFactorY = 1.0f;
Ben Murdoch2694e232009-09-29 09:41:11 +01002468 if (thumbnailWidth > 0) {
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002469 scaleFactorX = (float) width / (float)thumbnailWidth;
Ben Murdochae59c3f2009-10-20 18:30:28 +01002470 } else {
2471 return null;
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002472 }
Ben Murdochae59c3f2009-10-20 18:30:28 +01002473
2474 if (view.getWidth() > view.getHeight() &&
2475 thumbnailHeight < view.getHeight() && thumbnailHeight > 0) {
2476 // If the device is in landscape and the page is shorter
2477 // than the height of the view, stretch the thumbnail to fill the
2478 // space.
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002479 scaleFactorY = (float) height / (float)thumbnailHeight;
Ben Murdochae59c3f2009-10-20 18:30:28 +01002480 } else {
2481 // In the portrait case, this looks nice.
2482 scaleFactorY = scaleFactorX;
2483 }
2484
2485 canvas.scale(scaleFactorX, scaleFactorY);
2486
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002487 thumbnail.draw(canvas);
2488 return bm;
2489 }
2490
The Android Open Source Project0c908882009-03-03 19:32:16 -08002491 // -------------------------------------------------------------------------
Grace Kloba22ac16e2009-10-07 18:00:23 -07002492 // Helper function for WebViewClient.
The Android Open Source Project0c908882009-03-03 19:32:16 -08002493 //-------------------------------------------------------------------------
2494
2495 // Use in overrideUrlLoading
2496 /* package */ final static String SCHEME_WTAI = "wtai://wp/";
2497 /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
2498 /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;";
2499 /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;";
2500
Leon Scroggins92472e82010-02-17 16:32:28 -05002501 // Keep this initial progress in sync with initialProgressValue (* 100)
2502 // in ProgressTracker.cpp
2503 private final static int INITIAL_PROGRESS = 10;
2504
Grace Kloba22ac16e2009-10-07 18:00:23 -07002505 void onPageStarted(WebView view, String url, Bitmap favicon) {
2506 // when BrowserActivity just starts, onPageStarted may be called before
2507 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
2508 // to start the timer. As we won't switch tabs while an activity is in
2509 // pause state, we can ensure calling resume and pause in pair.
2510 if (mActivityInPause) resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002511
Grace Kloba22ac16e2009-10-07 18:00:23 -07002512 resetLockIcon(url);
2513 setUrlTitle(url, null);
2514 setFavicon(favicon);
Leon Scroggins8cf8f682009-11-04 11:13:50 -08002515 // Show some progress so that the user knows the page is beginning to
2516 // load
Leon Scroggins92472e82010-02-17 16:32:28 -05002517 onProgressChanged(view, INITIAL_PROGRESS);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002518 mDidStopLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07002519 if (!mIsNetworkUp) createAndShowNetworkDialog();
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -04002520 endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002521 if (mSettings.isTracing()) {
2522 String host;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002523 try {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002524 WebAddress uri = new WebAddress(url);
Bjorn Bringert131ab512010-10-12 16:25:47 +01002525 host = uri.getHost();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002526 } catch (android.net.ParseException ex) {
2527 host = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -08002528 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002529 host = host.replace('.', '_');
2530 host += ".trace";
2531 mInTrace = true;
2532 Debug.startMethodTracing(host, 20 * 1024 * 1024);
2533 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002534
Grace Kloba22ac16e2009-10-07 18:00:23 -07002535 // Performance probe
2536 if (false) {
2537 mStart = SystemClock.uptimeMillis();
2538 mProcessStart = Process.getElapsedCpuTime();
2539 long[] sysCpu = new long[7];
2540 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2541 sysCpu, null)) {
2542 mUserStart = sysCpu[0] + sysCpu[1];
2543 mSystemStart = sysCpu[2];
2544 mIdleStart = sysCpu[3];
2545 mIrqStart = sysCpu[4] + sysCpu[5] + sysCpu[6];
2546 }
2547 mUiStart = SystemClock.currentThreadTimeMillis();
2548 }
2549 }
2550
2551 void onPageFinished(WebView view, String url) {
2552 // Reset the title and icon in case we stopped a provisional load.
2553 resetTitleAndIcon(view);
2554 // Update the lock icon image only once we are done loading
2555 updateLockIconToLatest();
2556 // pause the WebView timer and release the wake lock if it is finished
2557 // while BrowserActivity is in pause state.
2558 if (mActivityInPause && pauseWebViewTimers()) {
2559 if (mWakeLock.isHeld()) {
2560 mHandler.removeMessages(RELEASE_WAKELOCK);
2561 mWakeLock.release();
2562 }
2563 }
2564
2565 // Performance probe
2566 if (false) {
2567 long[] sysCpu = new long[7];
2568 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2569 sysCpu, null)) {
2570 String uiInfo = "UI thread used "
2571 + (SystemClock.currentThreadTimeMillis() - mUiStart)
2572 + " ms";
2573 if (LOGD_ENABLED) {
2574 Log.d(LOGTAG, uiInfo);
2575 }
2576 //The string that gets written to the log
2577 String performanceString = "It took total "
2578 + (SystemClock.uptimeMillis() - mStart)
2579 + " ms clock time to load the page."
2580 + "\nbrowser process used "
2581 + (Process.getElapsedCpuTime() - mProcessStart)
2582 + " ms, user processes used "
2583 + (sysCpu[0] + sysCpu[1] - mUserStart) * 10
2584 + " ms, kernel used "
2585 + (sysCpu[2] - mSystemStart) * 10
2586 + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10
2587 + " ms and irq took "
2588 + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart)
2589 * 10 + " ms, " + uiInfo;
2590 if (LOGD_ENABLED) {
2591 Log.d(LOGTAG, performanceString + "\nWebpage: " + url);
2592 }
2593 if (url != null) {
2594 // strip the url to maintain consistency
2595 String newUrl = new String(url);
2596 if (newUrl.startsWith("http://www.")) {
2597 newUrl = newUrl.substring(11);
2598 } else if (newUrl.startsWith("http://")) {
2599 newUrl = newUrl.substring(7);
2600 } else if (newUrl.startsWith("https://www.")) {
2601 newUrl = newUrl.substring(12);
2602 } else if (newUrl.startsWith("https://")) {
2603 newUrl = newUrl.substring(8);
2604 }
2605 if (LOGD_ENABLED) {
2606 Log.d(LOGTAG, newUrl + " loaded");
2607 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002608 }
2609 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002610 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002611
Grace Kloba22ac16e2009-10-07 18:00:23 -07002612 if (mInTrace) {
2613 mInTrace = false;
2614 Debug.stopMethodTracing();
2615 }
2616 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002617
Grace Klobae7fe26b2010-06-28 16:23:33 -07002618 private void closeEmptyChildTab() {
2619 Tab current = mTabControl.getCurrentTab();
2620 if (current != null
2621 && current.getWebView().copyBackForwardList().getSize() == 0) {
2622 Tab parent = current.getParentTab();
2623 if (parent != null) {
2624 switchToTab(mTabControl.getTabIndex(parent));
2625 closeTab(current);
2626 }
2627 }
2628 }
2629
Grace Kloba22ac16e2009-10-07 18:00:23 -07002630 boolean shouldOverrideUrlLoading(WebView view, String url) {
Jeff Hamilton47654f42010-09-07 09:57:51 -05002631 if (view.isPrivateBrowsingEnabled()) {
2632 // Don't allow urls to leave the browser app when in private browsing mode
2633 loadUrl(view, url);
2634 return true;
2635 }
2636
Grace Kloba22ac16e2009-10-07 18:00:23 -07002637 if (url.startsWith(SCHEME_WTAI)) {
2638 // wtai://wp/mc;number
2639 // number=string(phone-number)
2640 if (url.startsWith(SCHEME_WTAI_MC)) {
2641 Intent intent = new Intent(Intent.ACTION_VIEW,
2642 Uri.parse(WebView.SCHEME_TEL +
2643 url.substring(SCHEME_WTAI_MC.length())));
2644 startActivity(intent);
Grace Klobae7fe26b2010-06-28 16:23:33 -07002645 // before leaving BrowserActivity, close the empty child tab.
2646 // If a new tab is created through JavaScript open to load this
2647 // url, we would like to close it as we will load this url in a
2648 // different Activity.
2649 closeEmptyChildTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002650 return true;
2651 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002652 // wtai://wp/sd;dtmf
2653 // dtmf=string(dialstring)
2654 if (url.startsWith(SCHEME_WTAI_SD)) {
2655 // TODO: only send when there is active voice connection
2656 return false;
2657 }
2658 // wtai://wp/ap;number;name
2659 // number=string(phone-number)
2660 // name=string
2661 if (url.startsWith(SCHEME_WTAI_AP)) {
2662 // TODO
2663 return false;
2664 }
2665 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002666
Grace Kloba22ac16e2009-10-07 18:00:23 -07002667 // The "about:" schemes are internal to the browser; don't want these to
2668 // be dispatched to other apps.
2669 if (url.startsWith("about:")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002670 return false;
2671 }
2672
Jeff Davidson43610292010-07-16 16:03:58 -07002673 // If this is a Google search, attempt to add an RLZ string (if one isn't already present).
2674 if (rlzProviderPresent()) {
2675 Uri siteUri = Uri.parse(url);
2676 if (needsRlzString(siteUri)) {
2677 String rlz = null;
2678 Cursor cur = null;
2679 try {
2680 cur = getContentResolver().query(getRlzUri(), null, null, null, null);
2681 if (cur != null && cur.moveToFirst() && !cur.isNull(0)) {
2682 url = siteUri.buildUpon()
2683 .appendQueryParameter("rlz", cur.getString(0))
2684 .build().toString();
2685 }
2686 } finally {
2687 if (cur != null) {
2688 cur.close();
2689 }
2690 }
2691 loadUrl(view, url);
2692 return true;
2693 }
2694 }
2695
Grace Kloba22ac16e2009-10-07 18:00:23 -07002696 Intent intent;
2697 // perform generic parsing of the URI to turn it into an Intent.
2698 try {
2699 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
2700 } catch (URISyntaxException ex) {
2701 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
2702 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002703 }
2704
Grace Kloba22ac16e2009-10-07 18:00:23 -07002705 // check whether the intent can be resolved. If not, we will see
2706 // whether we can download it from the Market.
2707 if (getPackageManager().resolveActivity(intent, 0) == null) {
2708 String packagename = intent.getPackage();
2709 if (packagename != null) {
2710 intent = new Intent(Intent.ACTION_VIEW, Uri
2711 .parse("market://search?q=pname:" + packagename));
2712 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2713 startActivity(intent);
Grace Klobae7fe26b2010-06-28 16:23:33 -07002714 // before leaving BrowserActivity, close the empty child tab.
2715 // If a new tab is created through JavaScript open to load this
2716 // url, we would like to close it as we will load this url in a
2717 // different Activity.
2718 closeEmptyChildTab();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002719 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002720 } else {
2721 return false;
2722 }
2723 }
2724
Grace Kloba22ac16e2009-10-07 18:00:23 -07002725 // sanitize the Intent, ensuring web pages can not bypass browser
2726 // security (only access to BROWSABLE activities).
2727 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2728 intent.setComponent(null);
2729 try {
2730 if (startActivityIfNeeded(intent, -1)) {
Grace Klobae7fe26b2010-06-28 16:23:33 -07002731 // before leaving BrowserActivity, close the empty child tab.
2732 // If a new tab is created through JavaScript open to load this
2733 // url, we would like to close it as we will load this url in a
2734 // different Activity.
2735 closeEmptyChildTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002736 return true;
2737 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002738 } catch (ActivityNotFoundException ex) {
2739 // ignore the error. If no application can handle the URL,
2740 // eg about:blank, assume the browser can handle it.
2741 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002742
Grace Kloba22ac16e2009-10-07 18:00:23 -07002743 if (mMenuIsDown) {
Michael Kolb68792c82010-08-09 16:39:18 -07002744 openTab(url, false);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002745 closeOptionsMenu();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002746 return true;
2747 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002748 return false;
2749 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002750
Jeff Davidson43610292010-07-16 16:03:58 -07002751 // Determine whether the RLZ provider is present on the system.
2752 private boolean rlzProviderPresent() {
2753 if (mIsProviderPresent == null) {
2754 PackageManager pm = getPackageManager();
2755 mIsProviderPresent = pm.resolveContentProvider(BrowserSettings.RLZ_PROVIDER, 0) != null;
2756 }
2757 return mIsProviderPresent;
2758 }
2759
2760 // Retrieve the RLZ access point string and cache the URI used to retrieve RLZ values.
2761 private Uri getRlzUri() {
2762 if (mRlzUri == null) {
2763 String ap = getResources().getString(R.string.rlz_access_point);
2764 mRlzUri = Uri.withAppendedPath(BrowserSettings.RLZ_PROVIDER_URI, ap);
2765 }
2766 return mRlzUri;
2767 }
2768
2769 // Determine if this URI appears to be for a Google search and does not have an RLZ parameter.
2770 // Taken largely from Chrome source, src/chrome/browser/google_url_tracker.cc
2771 private static boolean needsRlzString(Uri uri) {
Bill Napierc50e71a2010-08-27 14:03:03 -07002772 String scheme = uri.getScheme();
2773 if (("http".equals(scheme) || "https".equals(scheme)) &&
2774 (uri.getQueryParameter("q") != null) && (uri.getQueryParameter("rlz") == null)) {
Jeff Davidson43610292010-07-16 16:03:58 -07002775 String host = uri.getHost();
2776 if (host == null) {
2777 return false;
2778 }
2779 String[] hostComponents = host.split("\\.");
2780
2781 if (hostComponents.length < 2) {
2782 return false;
2783 }
2784 int googleComponent = hostComponents.length - 2;
2785 String component = hostComponents[googleComponent];
2786 if (!"google".equals(component)) {
2787 if (hostComponents.length < 3 ||
2788 (!"co".equals(component) && !"com".equals(component))) {
2789 return false;
2790 }
2791 googleComponent = hostComponents.length - 3;
2792 if (!"google".equals(hostComponents[googleComponent])) {
2793 return false;
2794 }
2795 }
2796
2797 // Google corp network handling.
2798 if (googleComponent > 0 && "corp".equals(hostComponents[googleComponent - 1])) {
2799 return false;
2800 }
2801
2802 return true;
2803 }
2804 return false;
2805 }
2806
Grace Kloba22ac16e2009-10-07 18:00:23 -07002807 // -------------------------------------------------------------------------
2808 // Helper function for WebChromeClient
2809 // -------------------------------------------------------------------------
The Android Open Source Project0c908882009-03-03 19:32:16 -08002810
Grace Kloba22ac16e2009-10-07 18:00:23 -07002811 void onProgressChanged(WebView view, int newProgress) {
Michael Kolba2b2ba82010-08-04 17:54:03 -07002812
2813 // On the phone, the fake title bar will always cover up the
2814 // regular title bar (or the regular one is offscreen), so only the
2815 // fake title bar needs to change its progress
2816 mFakeTitleBar.setProgress(newProgress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002817
Grace Kloba22ac16e2009-10-07 18:00:23 -07002818 if (newProgress == 100) {
2819 // onProgressChanged() may continue to be called after the main
2820 // frame has finished loading, as any remaining sub frames continue
2821 // to load. We'll only get called once though with newProgress as
2822 // 100 when everything is loaded. (onPageFinished is called once
2823 // when the main frame completes loading regardless of the state of
2824 // any sub frames so calls to onProgressChanges may continue after
2825 // onPageFinished has executed)
2826 if (mInLoad) {
2827 mInLoad = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -04002828 updateInLoadMenuItems();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002829 // If the options menu is open, leave the title bar
2830 if (!mOptionsMenuOpen || !mIconView) {
2831 hideFakeTitleBar();
The Android Open Source Projectcb9a0bb2009-03-11 12:11:58 -07002832 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002833 }
Leon Scrogginsaa37e7f2010-03-05 11:51:20 -05002834 } else {
2835 if (!mInLoad) {
2836 // onPageFinished may have already been called but a subframe is
2837 // still loading and updating the progress. Reset mInLoad and
2838 // update the menu items.
2839 mInLoad = true;
2840 updateInLoadMenuItems();
2841 }
2842 // When the page first begins to load, the Activity may still be
2843 // paused, in which case showFakeTitleBar will do nothing. Call
2844 // again as the page continues to load so that it will be shown.
2845 // (Calling it will the fake title bar is already showing will also
2846 // do nothing.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002847 if (!mOptionsMenuOpen || mIconView) {
2848 // This page has begun to load, so show the title bar
2849 showFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002850 }
2851 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002852 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002853
Grace Kloba22ac16e2009-10-07 18:00:23 -07002854 void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002855 // if a view already exists then immediately terminate the new one
2856 if (mCustomView != null) {
2857 callback.onCustomViewHidden();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002858 return;
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002859 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002860
2861 // Add the custom view to its container.
2862 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
2863 mCustomView = view;
2864 mCustomViewCallback = callback;
2865 // Save the menu state and set it to empty while the custom
2866 // view is showing.
2867 mOldMenuState = mMenuState;
2868 mMenuState = EMPTY_MENU;
2869 // Hide the content view.
2870 mContentView.setVisibility(View.GONE);
2871 // Finally show the custom view container.
Andrei Popescu163ab742009-10-20 17:58:23 +01002872 setStatusBarVisibility(false);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002873 mCustomViewContainer.setVisibility(View.VISIBLE);
2874 mCustomViewContainer.bringToFront();
2875 }
2876
2877 void onHideCustomView() {
2878 if (mCustomView == null)
2879 return;
2880
2881 // Hide the custom view.
2882 mCustomView.setVisibility(View.GONE);
2883 // Remove the custom view from its container.
2884 mCustomViewContainer.removeView(mCustomView);
2885 mCustomView = null;
2886 // Reset the old menu state.
2887 mMenuState = mOldMenuState;
2888 mOldMenuState = EMPTY_MENU;
2889 mCustomViewContainer.setVisibility(View.GONE);
2890 mCustomViewCallback.onCustomViewHidden();
2891 // Show the content view.
Andrei Popescu163ab742009-10-20 17:58:23 +01002892 setStatusBarVisibility(true);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002893 mContentView.setVisibility(View.VISIBLE);
2894 }
2895
2896 Bitmap getDefaultVideoPoster() {
2897 if (mDefaultVideoPoster == null) {
2898 mDefaultVideoPoster = BitmapFactory.decodeResource(
2899 getResources(), R.drawable.default_video_poster);
Patrick Scott3918d442009-08-04 13:22:29 -04002900 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002901 return mDefaultVideoPoster;
2902 }
Patrick Scott3918d442009-08-04 13:22:29 -04002903
Grace Kloba22ac16e2009-10-07 18:00:23 -07002904 View getVideoLoadingProgressView() {
2905 if (mVideoProgressView == null) {
2906 LayoutInflater inflater = LayoutInflater.from(BrowserActivity.this);
2907 mVideoProgressView = inflater.inflate(
2908 R.layout.video_loading_progress, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002909 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002910 return mVideoProgressView;
2911 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002912
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002913 /*
2914 * The Object used to inform the WebView of the file to upload.
2915 */
2916 private ValueCallback<Uri> mUploadMessage;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002917 private String mCameraFilePath;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002918
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002919 void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
2920
2921 final String imageMimeType = "image/*";
Ben Murdoch6af492a2010-06-15 12:38:17 +01002922 final String videoMimeType = "video/*";
Ben Murdoch039bd472010-07-21 21:26:57 +01002923 final String audioMimeType = "audio/*";
Ben Murdochb50d7402010-07-16 12:42:22 +01002924 final String mediaSourceKey = "capture";
Ben Murdoch6af492a2010-06-15 12:38:17 +01002925 final String mediaSourceValueCamera = "camera";
Ben Murdochb50d7402010-07-16 12:42:22 +01002926 final String mediaSourceValueFileSystem = "filesystem";
Ben Murdoch6af492a2010-06-15 12:38:17 +01002927 final String mediaSourceValueCamcorder = "camcorder";
Ben Murdoch039bd472010-07-21 21:26:57 +01002928 final String mediaSourceValueMicrophone = "microphone";
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002929
Ben Murdoch039bd472010-07-21 21:26:57 +01002930 // media source can be 'filesystem' or 'camera' or 'camcorder' or 'microphone'.
Ben Murdoch6af492a2010-06-15 12:38:17 +01002931 String mediaSource = "";
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002932
Ben Murdoch6af492a2010-06-15 12:38:17 +01002933 // We add the camera intent if there was no accept type (or '*/*' or 'image/*').
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002934 boolean addCameraIntent = true;
Ben Murdoch6af492a2010-06-15 12:38:17 +01002935 // We add the camcorder intent if there was no accept type (or '*/*' or 'video/*').
2936 boolean addCamcorderIntent = true;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002937
Ben Murdoch6af492a2010-06-15 12:38:17 +01002938 if (mUploadMessage != null) {
2939 // Already a file picker operation in progress.
2940 return;
2941 }
2942
Grace Kloba22ac16e2009-10-07 18:00:23 -07002943 mUploadMessage = uploadMsg;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002944
2945 // Parse the accept type.
2946 String params[] = acceptType.split(";");
2947 String mimeType = params[0];
2948
2949 for (String p : params) {
2950 String[] keyValue = p.split("=");
2951 if (keyValue.length == 2) {
2952 // Process key=value parameters.
Ben Murdoch6af492a2010-06-15 12:38:17 +01002953 if (mediaSourceKey.equals(keyValue[0])) {
2954 mediaSource = keyValue[1];
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002955 }
2956 }
2957 }
2958
2959 // This intent will display the standard OPENABLE file picker.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002960 Intent i = new Intent(Intent.ACTION_GET_CONTENT);
2961 i.addCategory(Intent.CATEGORY_OPENABLE);
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002962
2963 // Create an intent to add to the standard file picker that will
2964 // capture an image from the camera. We'll combine this intent with
2965 // the standard OPENABLE picker unless the web developer specifically
2966 // requested the camera or gallery be opened by passing a parameter
2967 // in the accept type.
2968 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
2969 File externalDataDir = Environment.getExternalStoragePublicDirectory(
2970 Environment.DIRECTORY_DCIM);
2971 File cameraDataDir = new File(externalDataDir.getAbsolutePath() +
2972 File.separator + "browser-photos");
2973 cameraDataDir.mkdirs();
2974 mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator +
2975 System.currentTimeMillis() + ".jpg";
2976 cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCameraFilePath)));
2977
Ben Murdoch6af492a2010-06-15 12:38:17 +01002978 Intent camcorderIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
2979
Ben Murdoch039bd472010-07-21 21:26:57 +01002980 Intent soundRecIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
2981
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002982 if (mimeType.equals(imageMimeType)) {
2983 i.setType(imageMimeType);
Ben Murdoch6af492a2010-06-15 12:38:17 +01002984 addCamcorderIntent = false;
2985 if (mediaSource.equals(mediaSourceValueCamera)) {
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002986 // Specified 'image/*' and requested the camera, so go ahead and launch the camera
2987 // directly.
2988 BrowserActivity.this.startActivityForResult(cameraIntent, FILE_SELECTED);
2989 return;
Ben Murdochb50d7402010-07-16 12:42:22 +01002990 } else if (mediaSource.equals(mediaSourceValueFileSystem)) {
2991 // Specified filesytem as the source, so don't want to consider the camera.
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002992 addCameraIntent = false;
2993 }
Ben Murdoch6af492a2010-06-15 12:38:17 +01002994 } else if (mimeType.equals(videoMimeType)) {
2995 i.setType(videoMimeType);
2996 addCameraIntent = false;
2997 // The camcorder saves it's own file and returns it to us in the intent, so
2998 // we don't need to generate one here.
2999 mCameraFilePath = null;
3000
3001 if (mediaSource.equals(mediaSourceValueCamcorder)) {
Ben Murdoch039bd472010-07-21 21:26:57 +01003002 // Specified 'video/*' and requested the camcorder, so go ahead and launch the
3003 // camcorder directly.
Ben Murdoch6af492a2010-06-15 12:38:17 +01003004 BrowserActivity.this.startActivityForResult(camcorderIntent, FILE_SELECTED);
3005 return;
Ben Murdochb50d7402010-07-16 12:42:22 +01003006 } else if (mediaSource.equals(mediaSourceValueFileSystem)) {
3007 // Specified filesystem as the source, so don't want to consider the camcorder.
Ben Murdoch6af492a2010-06-15 12:38:17 +01003008 addCamcorderIntent = false;
3009 }
Ben Murdoch039bd472010-07-21 21:26:57 +01003010 } else if (mimeType.equals(audioMimeType)) {
3011 i.setType(audioMimeType);
3012 addCameraIntent = false;
3013 addCamcorderIntent = false;
3014 if (mediaSource.equals(mediaSourceValueMicrophone)) {
3015 // Specified 'audio/*' and requested microphone, so go ahead and launch the sound
3016 // recorder.
3017 BrowserActivity.this.startActivityForResult(soundRecIntent, FILE_SELECTED);
3018 return;
3019 }
3020 // On a default system, there is no single option to open an audio "gallery". Both the
3021 // sound recorder and music browser respond to the OPENABLE/audio/* intent unlike the
3022 // image/* and video/* OPENABLE intents where the image / video gallery are the only
3023 // respondants (and so the user is not prompted by default).
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003024 } else {
3025 i.setType("*/*");
3026 }
3027
Ben Murdoch6af492a2010-06-15 12:38:17 +01003028 // Combine the chooser and the extra choices (like camera or camcorder)
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003029 Intent chooser = new Intent(Intent.ACTION_CHOOSER);
3030 chooser.putExtra(Intent.EXTRA_INTENT, i);
3031
Ben Murdoch6af492a2010-06-15 12:38:17 +01003032 Vector<Intent> extraInitialIntents = new Vector<Intent>(0);
3033
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003034 if (addCameraIntent) {
Ben Murdoch6af492a2010-06-15 12:38:17 +01003035 extraInitialIntents.add(cameraIntent);
3036 }
3037
3038 if (addCamcorderIntent) {
3039 extraInitialIntents.add(camcorderIntent);
3040 }
3041
3042 if (extraInitialIntents.size() > 0) {
3043 Intent[] extraIntents = new Intent[extraInitialIntents.size()];
3044 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraInitialIntents.toArray(extraIntents));
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003045 }
3046
3047 chooser.putExtra(Intent.EXTRA_TITLE, getString(R.string.choose_upload));
3048 BrowserActivity.this.startActivityForResult(chooser, FILE_SELECTED);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003049 }
3050
3051 // -------------------------------------------------------------------------
3052 // Implement functions for DownloadListener
3053 // -------------------------------------------------------------------------
3054
The Android Open Source Project0c908882009-03-03 19:32:16 -08003055 /**
3056 * Notify the host application a download should be done, or that
3057 * the data should be streamed if a streaming viewer is available.
3058 * @param url The full url to the content that should be downloaded
3059 * @param contentDisposition Content-disposition http header, if
3060 * present.
3061 * @param mimetype The mimetype of the content reported by the server
3062 * @param contentLength The file size reported by the server
3063 */
3064 public void onDownloadStart(String url, String userAgent,
3065 String contentDisposition, String mimetype, long contentLength) {
3066 // if we're dealing wih A/V content that's not explicitly marked
3067 // for download, check if it's streamable.
3068 if (contentDisposition == null
Patrick Scotte1fb9662009-08-31 14:31:52 -04003069 || !contentDisposition.regionMatches(
3070 true, 0, "attachment", 0, 10)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003071 // query the package manager to see if there's a registered handler
3072 // that matches.
3073 Intent intent = new Intent(Intent.ACTION_VIEW);
3074 intent.setDataAndType(Uri.parse(url), mimetype);
Patrick Scotte1fb9662009-08-31 14:31:52 -04003075 ResolveInfo info = getPackageManager().resolveActivity(intent,
3076 PackageManager.MATCH_DEFAULT_ONLY);
3077 if (info != null) {
3078 ComponentName myName = getComponentName();
3079 // If we resolved to ourselves, we don't want to attempt to
3080 // load the url only to try and download it again.
3081 if (!myName.getPackageName().equals(
3082 info.activityInfo.packageName)
3083 || !myName.getClassName().equals(
3084 info.activityInfo.name)) {
3085 // someone (other than us) knows how to handle this mime
3086 // type with this scheme, don't download.
3087 try {
3088 startActivity(intent);
3089 return;
3090 } catch (ActivityNotFoundException ex) {
3091 if (LOGD_ENABLED) {
3092 Log.d(LOGTAG, "activity not found for " + mimetype
3093 + " over " + Uri.parse(url).getScheme(),
3094 ex);
3095 }
3096 // Best behavior is to fall back to a download in this
3097 // case
The Android Open Source Project0c908882009-03-03 19:32:16 -08003098 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003099 }
3100 }
3101 }
3102 onDownloadStartNoStream(url, userAgent, contentDisposition, mimetype, contentLength);
3103 }
3104
Kristian Monsenfa52d172010-03-25 18:29:21 +00003105 // This is to work around the fact that java.net.URI throws Exceptions
3106 // instead of just encoding URL's properly
3107 // Helper method for onDownloadStartNoStream
3108 private static String encodePath(String path) {
3109 char[] chars = path.toCharArray();
3110
3111 boolean needed = false;
3112 for (char c : chars) {
3113 if (c == '[' || c == ']') {
3114 needed = true;
3115 break;
3116 }
3117 }
3118 if (needed == false) {
3119 return path;
3120 }
3121
3122 StringBuilder sb = new StringBuilder("");
3123 for (char c : chars) {
3124 if (c == '[' || c == ']') {
3125 sb.append('%');
3126 sb.append(Integer.toHexString(c));
3127 } else {
3128 sb.append(c);
3129 }
3130 }
3131
3132 return sb.toString();
3133 }
3134
The Android Open Source Project0c908882009-03-03 19:32:16 -08003135 /**
3136 * Notify the host application a download should be done, even if there
3137 * is a streaming viewer available for thise type.
3138 * @param url The full url to the content that should be downloaded
3139 * @param contentDisposition Content-disposition http header, if
3140 * present.
3141 * @param mimetype The mimetype of the content reported by the server
3142 * @param contentLength The file size reported by the server
3143 */
3144 /*package */ void onDownloadStartNoStream(String url, String userAgent,
3145 String contentDisposition, String mimetype, long contentLength) {
3146
3147 String filename = URLUtil.guessFileName(url,
3148 contentDisposition, mimetype);
3149
3150 // Check to see if we have an SDCard
3151 String status = Environment.getExternalStorageState();
3152 if (!status.equals(Environment.MEDIA_MOUNTED)) {
3153 int title;
3154 String msg;
3155
3156 // Check to see if the SDCard is busy, same as the music app
3157 if (status.equals(Environment.MEDIA_SHARED)) {
3158 msg = getString(R.string.download_sdcard_busy_dlg_msg);
3159 title = R.string.download_sdcard_busy_dlg_title;
3160 } else {
3161 msg = getString(R.string.download_no_sdcard_dlg_msg, filename);
3162 title = R.string.download_no_sdcard_dlg_title;
3163 }
3164
3165 new AlertDialog.Builder(this)
3166 .setTitle(title)
3167 .setIcon(android.R.drawable.ic_dialog_alert)
3168 .setMessage(msg)
3169 .setPositiveButton(R.string.ok, null)
3170 .show();
3171 return;
3172 }
3173
Kristian Monsenfa52d172010-03-25 18:29:21 +00003174 // java.net.URI is a lot stricter than KURL so we have to encode some
3175 // extra characters. Fix for b 2538060 and b 1634719
3176 WebAddress webAddress;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003177 try {
Kristian Monsenfa52d172010-03-25 18:29:21 +00003178 webAddress = new WebAddress(url);
Bjorn Bringert131ab512010-10-12 16:25:47 +01003179 webAddress.setPath(encodePath(webAddress.getPath()));
The Android Open Source Project0c908882009-03-03 19:32:16 -08003180 } catch (Exception e) {
Kristian Monsenfa52d172010-03-25 18:29:21 +00003181 // This only happens for very bad urls, we want to chatch the
3182 // exception here
3183 Log.e(LOGTAG, "Exception trying to parse url:" + url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003184 return;
3185 }
3186
Leon Scrogginsc6076532010-10-22 15:06:59 -04003187 String addressString = webAddress.toString();
3188 Uri uri = Uri.parse(addressString);
3189 DownloadManager.Request request = new DownloadManager.Request(uri);
3190 request.setMimeType(mimetype);
3191 request.setDestinationInExternalFilesDir(this, null, filename);
Vasu Nori1d88aa02010-11-04 18:16:10 -07003192 // let this downloaded file be scanned by MediaScanner - so that it can show up
3193 // in Gallery app, for example.
3194 request.allowScanningByMediaScanner();
Leon Scrogginsc6076532010-10-22 15:06:59 -04003195 request.setDescription(webAddress.getHost());
Vasu Noric54d3282010-10-29 13:31:09 -07003196 String cookies = CookieManager.getInstance().getCookie(url);
3197 request.addRequestHeader("cookie", cookies);
Leon Scroggins09ccfc72010-10-28 16:22:24 -04003198 request.setNotificationVisibility(
3199 DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003200 if (mimetype == null) {
Leon Scrogginsc6076532010-10-22 15:06:59 -04003201 ContentValues values = new ContentValues();
3202 values.put(FetchUrlMimeType.URI, addressString);
3203 // XXX: Have to use the old url since the cookies were stored using the
3204 // old percent-encoded url.
Leon Scrogginsc6076532010-10-22 15:06:59 -04003205 values.put(FetchUrlMimeType.COOKIE_DATA, cookies);
3206 values.put(FetchUrlMimeType.USER_AGENT, userAgent);
3207
The Android Open Source Project0c908882009-03-03 19:32:16 -08003208 // We must have long pressed on a link or image to download it. We
3209 // are not sure of the mimetype in this case, so do a head request
Leon Scrogginsc6076532010-10-22 15:06:59 -04003210 new FetchUrlMimeType(this, request).execute(values);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003211 } else {
Leon Scrogginsc6076532010-10-22 15:06:59 -04003212 DownloadManager manager
3213 = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
3214 manager.enqueue(request);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003215 }
Leon Scroggins9191a7f2010-03-18 14:44:44 -04003216 Toast.makeText(this, R.string.download_pending, Toast.LENGTH_SHORT)
3217 .show();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003218 }
3219
Grace Kloba22ac16e2009-10-07 18:00:23 -07003220 // -------------------------------------------------------------------------
3221
The Android Open Source Project0c908882009-03-03 19:32:16 -08003222 /**
3223 * Resets the lock icon. This method is called when we start a new load and
3224 * know the url to be loaded.
3225 */
3226 private void resetLockIcon(String url) {
3227 // Save the lock-icon state (we revert to it if the load gets cancelled)
Grace Kloba22ac16e2009-10-07 18:00:23 -07003228 mTabControl.getCurrentTab().resetLockIcon(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003229 updateLockIconImage(LOCK_ICON_UNSECURE);
3230 }
3231
The Android Open Source Project0c908882009-03-03 19:32:16 -08003232 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003233 * Update the lock icon to correspond to our latest state.
3234 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003235 private void updateLockIconToLatest() {
Leon Scroggins91f65fc2010-06-29 13:52:39 -04003236 Tab t = mTabControl.getCurrentTab();
3237 if (t != null) {
3238 updateLockIconImage(t.getLockIconType());
3239 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003240 }
3241
3242 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08003243 * Updates the lock-icon image in the title-bar.
3244 */
3245 private void updateLockIconImage(int lockIconType) {
3246 Drawable d = null;
3247 if (lockIconType == LOCK_ICON_SECURE) {
3248 d = mSecLockIcon;
3249 } else if (lockIconType == LOCK_ICON_MIXED) {
3250 d = mMixLockIcon;
3251 }
Leon Scroggins68579392009-09-15 15:31:54 -04003252 mTitleBar.setLock(d);
Michael Kolba2b2ba82010-08-04 17:54:03 -07003253 mFakeTitleBar.setLock(d);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003254 }
3255
3256 /**
3257 * Displays a page-info dialog.
3258 * @param tab The tab to show info about
3259 * @param fromShowSSLCertificateOnError The flag that indicates whether
3260 * this dialog was opened from the SSL-certificate-on-error dialog or
3261 * not. This is important, since we need to know whether to return to
3262 * the parent dialog or simply dismiss.
3263 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003264 private void showPageInfo(final Tab tab,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003265 final boolean fromShowSSLCertificateOnError) {
3266 final LayoutInflater factory = LayoutInflater
3267 .from(this);
3268
3269 final View pageInfoView = factory.inflate(R.layout.page_info, null);
3270
3271 final WebView view = tab.getWebView();
3272
3273 String url = null;
3274 String title = null;
3275
3276 if (view == null) {
3277 url = tab.getUrl();
3278 title = tab.getTitle();
3279 } else if (view == mTabControl.getCurrentWebView()) {
3280 // Use the cached title and url if this is the current WebView
3281 url = mUrl;
3282 title = mTitle;
3283 } else {
3284 url = view.getUrl();
3285 title = view.getTitle();
3286 }
3287
3288 if (url == null) {
3289 url = "";
3290 }
3291 if (title == null) {
3292 title = "";
3293 }
3294
3295 ((TextView) pageInfoView.findViewById(R.id.address)).setText(url);
3296 ((TextView) pageInfoView.findViewById(R.id.title)).setText(title);
3297
3298 mPageInfoView = tab;
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05003299 mPageInfoFromShowSSLCertificateOnError = fromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003300
3301 AlertDialog.Builder alertDialogBuilder =
3302 new AlertDialog.Builder(this)
3303 .setTitle(R.string.page_info).setIcon(android.R.drawable.ic_dialog_info)
3304 .setView(pageInfoView)
3305 .setPositiveButton(
3306 R.string.ok,
3307 new DialogInterface.OnClickListener() {
3308 public void onClick(DialogInterface dialog,
3309 int whichButton) {
3310 mPageInfoDialog = null;
3311 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003312
3313 // if we came here from the SSL error dialog
3314 if (fromShowSSLCertificateOnError) {
3315 // go back to the SSL error dialog
3316 showSSLCertificateOnError(
3317 mSSLCertificateOnErrorView,
3318 mSSLCertificateOnErrorHandler,
3319 mSSLCertificateOnErrorError);
3320 }
3321 }
3322 })
3323 .setOnCancelListener(
3324 new DialogInterface.OnCancelListener() {
3325 public void onCancel(DialogInterface dialog) {
3326 mPageInfoDialog = null;
3327 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003328
3329 // if we came here from the SSL error dialog
3330 if (fromShowSSLCertificateOnError) {
3331 // go back to the SSL error dialog
3332 showSSLCertificateOnError(
3333 mSSLCertificateOnErrorView,
3334 mSSLCertificateOnErrorHandler,
3335 mSSLCertificateOnErrorError);
3336 }
3337 }
3338 });
3339
3340 // if we have a main top-level page SSL certificate set or a certificate
3341 // error
3342 if (fromShowSSLCertificateOnError ||
3343 (view != null && view.getCertificate() != null)) {
3344 // add a 'View Certificate' button
3345 alertDialogBuilder.setNeutralButton(
3346 R.string.view_certificate,
3347 new DialogInterface.OnClickListener() {
3348 public void onClick(DialogInterface dialog,
3349 int whichButton) {
3350 mPageInfoDialog = null;
3351 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003352
3353 // if we came here from the SSL error dialog
3354 if (fromShowSSLCertificateOnError) {
3355 // go back to the SSL error dialog
3356 showSSLCertificateOnError(
3357 mSSLCertificateOnErrorView,
3358 mSSLCertificateOnErrorHandler,
3359 mSSLCertificateOnErrorError);
3360 } else {
3361 // otherwise, display the top-most certificate from
3362 // the chain
3363 if (view.getCertificate() != null) {
3364 showSSLCertificate(tab);
3365 }
3366 }
3367 }
3368 });
3369 }
3370
3371 mPageInfoDialog = alertDialogBuilder.show();
3372 }
3373
3374 /**
3375 * Displays the main top-level page SSL certificate dialog
3376 * (accessible from the Page-Info dialog).
3377 * @param tab The tab to show certificate for.
3378 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003379 private void showSSLCertificate(final Tab tab) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003380 final View certificateView =
3381 inflateCertificateView(tab.getWebView().getCertificate());
3382 if (certificateView == null) {
3383 return;
3384 }
3385
3386 LayoutInflater factory = LayoutInflater.from(this);
3387
3388 final LinearLayout placeholder =
3389 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3390
3391 LinearLayout ll = (LinearLayout) factory.inflate(
3392 R.layout.ssl_success, placeholder);
3393 ((TextView)ll.findViewById(R.id.success))
3394 .setText(R.string.ssl_certificate_is_valid);
3395
3396 mSSLCertificateView = tab;
3397 mSSLCertificateDialog =
3398 new AlertDialog.Builder(this)
3399 .setTitle(R.string.ssl_certificate).setIcon(
3400 R.drawable.ic_dialog_browser_certificate_secure)
3401 .setView(certificateView)
3402 .setPositiveButton(R.string.ok,
3403 new DialogInterface.OnClickListener() {
3404 public void onClick(DialogInterface dialog,
3405 int whichButton) {
3406 mSSLCertificateDialog = null;
3407 mSSLCertificateView = null;
3408
3409 showPageInfo(tab, false);
3410 }
3411 })
3412 .setOnCancelListener(
3413 new DialogInterface.OnCancelListener() {
3414 public void onCancel(DialogInterface dialog) {
3415 mSSLCertificateDialog = null;
3416 mSSLCertificateView = null;
3417
3418 showPageInfo(tab, false);
3419 }
3420 })
3421 .show();
3422 }
3423
3424 /**
3425 * Displays the SSL error certificate dialog.
3426 * @param view The target web-view.
3427 * @param handler The SSL error handler responsible for cancelling the
3428 * connection that resulted in an SSL error or proceeding per user request.
3429 * @param error The SSL error object.
3430 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003431 void showSSLCertificateOnError(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003432 final WebView view, final SslErrorHandler handler, final SslError error) {
3433
3434 final View certificateView =
3435 inflateCertificateView(error.getCertificate());
3436 if (certificateView == null) {
3437 return;
3438 }
3439
3440 LayoutInflater factory = LayoutInflater.from(this);
3441
3442 final LinearLayout placeholder =
3443 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3444
3445 if (error.hasError(SslError.SSL_UNTRUSTED)) {
3446 LinearLayout ll = (LinearLayout)factory
3447 .inflate(R.layout.ssl_warning, placeholder);
3448 ((TextView)ll.findViewById(R.id.warning))
3449 .setText(R.string.ssl_untrusted);
3450 }
3451
3452 if (error.hasError(SslError.SSL_IDMISMATCH)) {
3453 LinearLayout ll = (LinearLayout)factory
3454 .inflate(R.layout.ssl_warning, placeholder);
3455 ((TextView)ll.findViewById(R.id.warning))
3456 .setText(R.string.ssl_mismatch);
3457 }
3458
3459 if (error.hasError(SslError.SSL_EXPIRED)) {
3460 LinearLayout ll = (LinearLayout)factory
3461 .inflate(R.layout.ssl_warning, placeholder);
3462 ((TextView)ll.findViewById(R.id.warning))
3463 .setText(R.string.ssl_expired);
3464 }
3465
3466 if (error.hasError(SslError.SSL_NOTYETVALID)) {
3467 LinearLayout ll = (LinearLayout)factory
3468 .inflate(R.layout.ssl_warning, placeholder);
3469 ((TextView)ll.findViewById(R.id.warning))
3470 .setText(R.string.ssl_not_yet_valid);
3471 }
3472
3473 mSSLCertificateOnErrorHandler = handler;
3474 mSSLCertificateOnErrorView = view;
3475 mSSLCertificateOnErrorError = error;
3476 mSSLCertificateOnErrorDialog =
3477 new AlertDialog.Builder(this)
3478 .setTitle(R.string.ssl_certificate).setIcon(
3479 R.drawable.ic_dialog_browser_certificate_partially_secure)
3480 .setView(certificateView)
3481 .setPositiveButton(R.string.ok,
3482 new DialogInterface.OnClickListener() {
3483 public void onClick(DialogInterface dialog,
3484 int whichButton) {
3485 mSSLCertificateOnErrorDialog = null;
3486 mSSLCertificateOnErrorView = null;
3487 mSSLCertificateOnErrorHandler = null;
3488 mSSLCertificateOnErrorError = null;
3489
Grace Kloba22ac16e2009-10-07 18:00:23 -07003490 view.getWebViewClient().onReceivedSslError(
3491 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003492 }
3493 })
3494 .setNeutralButton(R.string.page_info_view,
3495 new DialogInterface.OnClickListener() {
3496 public void onClick(DialogInterface dialog,
3497 int whichButton) {
3498 mSSLCertificateOnErrorDialog = null;
3499
3500 // do not clear the dialog state: we will
3501 // need to show the dialog again once the
3502 // user is done exploring the page-info details
3503
3504 showPageInfo(mTabControl.getTabFromView(view),
3505 true);
3506 }
3507 })
3508 .setOnCancelListener(
3509 new DialogInterface.OnCancelListener() {
3510 public void onCancel(DialogInterface dialog) {
3511 mSSLCertificateOnErrorDialog = null;
3512 mSSLCertificateOnErrorView = null;
3513 mSSLCertificateOnErrorHandler = null;
3514 mSSLCertificateOnErrorError = null;
3515
Grace Kloba22ac16e2009-10-07 18:00:23 -07003516 view.getWebViewClient().onReceivedSslError(
3517 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003518 }
3519 })
3520 .show();
3521 }
3522
3523 /**
3524 * Inflates the SSL certificate view (helper method).
3525 * @param certificate The SSL certificate.
3526 * @return The resultant certificate view with issued-to, issued-by,
3527 * issued-on, expires-on, and possibly other fields set.
3528 * If the input certificate is null, returns null.
3529 */
3530 private View inflateCertificateView(SslCertificate certificate) {
3531 if (certificate == null) {
3532 return null;
3533 }
3534
3535 LayoutInflater factory = LayoutInflater.from(this);
3536
3537 View certificateView = factory.inflate(
3538 R.layout.ssl_certificate, null);
3539
3540 // issued to:
3541 SslCertificate.DName issuedTo = certificate.getIssuedTo();
3542 if (issuedTo != null) {
3543 ((TextView) certificateView.findViewById(R.id.to_common))
3544 .setText(issuedTo.getCName());
3545 ((TextView) certificateView.findViewById(R.id.to_org))
3546 .setText(issuedTo.getOName());
3547 ((TextView) certificateView.findViewById(R.id.to_org_unit))
3548 .setText(issuedTo.getUName());
3549 }
3550
3551 // issued by:
3552 SslCertificate.DName issuedBy = certificate.getIssuedBy();
3553 if (issuedBy != null) {
3554 ((TextView) certificateView.findViewById(R.id.by_common))
3555 .setText(issuedBy.getCName());
3556 ((TextView) certificateView.findViewById(R.id.by_org))
3557 .setText(issuedBy.getOName());
3558 ((TextView) certificateView.findViewById(R.id.by_org_unit))
3559 .setText(issuedBy.getUName());
3560 }
3561
3562 // issued on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003563 String issuedOn = formatCertificateDate(
3564 certificate.getValidNotBeforeDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003565 ((TextView) certificateView.findViewById(R.id.issued_on))
3566 .setText(issuedOn);
3567
3568 // expires on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003569 String expiresOn = formatCertificateDate(
3570 certificate.getValidNotAfterDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003571 ((TextView) certificateView.findViewById(R.id.expires_on))
3572 .setText(expiresOn);
3573
3574 return certificateView;
3575 }
3576
3577 /**
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003578 * Formats the certificate date to a properly localized date string.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003579 * @return Properly localized version of the certificate date string and
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003580 * the "" if it fails to localize.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003581 */
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003582 private String formatCertificateDate(Date certificateDate) {
3583 if (certificateDate == null) {
3584 return "";
The Android Open Source Project0c908882009-03-03 19:32:16 -08003585 }
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003586 String formattedDate = DateFormat.getDateFormat(this).format(certificateDate);
3587 if (formattedDate == null) {
3588 return "";
3589 }
3590 return formattedDate;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003591 }
3592
3593 /**
3594 * Displays an http-authentication dialog.
3595 */
Bjorn Bringert25738922010-10-12 10:56:20 +01003596 void showHttpAuthentication(final HttpAuthHandler handler, String host, String realm) {
3597 mHttpAuthenticationDialog = new HttpAuthenticationDialog(this, host, realm);
3598 mHttpAuthenticationDialog.setOkListener(new HttpAuthenticationDialog.OkListener() {
3599 public void onOk(String host, String realm, String username, String password) {
3600 BrowserActivity.this.setHttpAuthUsernamePassword(host, realm, username, password);
3601 handler.proceed(username, password);
3602 mHttpAuthenticationDialog = null;
3603 }
3604 });
3605 mHttpAuthenticationDialog.setCancelListener(new HttpAuthenticationDialog.CancelListener() {
3606 public void onCancel() {
3607 handler.cancel();
3608 BrowserActivity.this.resetTitleAndRevertLockIcon();
3609 mHttpAuthenticationDialog = null;
3610 }
3611 });
3612 mHttpAuthenticationDialog.show();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003613 }
3614
3615 public int getProgress() {
3616 WebView w = mTabControl.getCurrentWebView();
3617 if (w != null) {
3618 return w.getProgress();
3619 } else {
3620 return 100;
3621 }
3622 }
3623
3624 /**
3625 * Set HTTP authentication password.
3626 *
3627 * @param host The host for the password
3628 * @param realm The realm for the password
3629 * @param username The username for the password. If it is null, it means
3630 * password can't be saved.
3631 * @param password The password
3632 */
3633 public void setHttpAuthUsernamePassword(String host, String realm,
3634 String username,
3635 String password) {
Steve Block95a53b22010-03-25 17:24:58 +00003636 WebView w = getTopWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003637 if (w != null) {
3638 w.setHttpAuthUsernamePassword(host, realm, username, password);
3639 }
3640 }
3641
3642 /**
3643 * connectivity manager says net has come or gone... inform the user
3644 * @param up true if net has come up, false if net has gone down
3645 */
3646 public void onNetworkToggle(boolean up) {
3647 if (up == mIsNetworkUp) {
3648 return;
3649 } else if (up) {
3650 mIsNetworkUp = true;
3651 if (mAlertDialog != null) {
3652 mAlertDialog.cancel();
3653 mAlertDialog = null;
3654 }
3655 } else {
3656 mIsNetworkUp = false;
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003657 if (mInLoad) {
3658 createAndShowNetworkDialog();
3659 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003660 }
3661 WebView w = mTabControl.getCurrentWebView();
3662 if (w != null) {
3663 w.setNetworkAvailable(up);
3664 }
3665 }
3666
Grace Kloba22ac16e2009-10-07 18:00:23 -07003667 boolean isNetworkUp() {
3668 return mIsNetworkUp;
3669 }
3670
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003671 // This method shows the network dialog alerting the user that the net is
3672 // down. It will only show the dialog if mAlertDialog is null.
3673 private void createAndShowNetworkDialog() {
3674 if (mAlertDialog == null) {
3675 mAlertDialog = new AlertDialog.Builder(this)
3676 .setTitle(R.string.loadSuspendedTitle)
3677 .setMessage(R.string.loadSuspended)
3678 .setPositiveButton(R.string.ok, null)
3679 .show();
3680 }
3681 }
3682
Michael Kolbe421c242010-10-04 19:29:01 -07003683 /**
3684 * callback from ComboPage when bookmark/history selection
3685 */
3686 @Override
3687 public void onUrlSelected(String url, boolean newTab) {
3688 removeComboView();
3689 if (!TextUtils.isEmpty(url)) {
3690 if (newTab) {
3691 openTab(url, false);
3692 } else {
3693 final Tab currentTab = mTabControl.getCurrentTab();
3694 dismissSubWindow(currentTab);
3695 loadUrl(getTopWindow(), url);
3696 }
3697 }
3698 }
3699
3700 /**
3701 * callback from ComboPage when dismissed
3702 */
3703 @Override
3704 public void onComboCanceled() {
3705 removeComboView();
3706 }
3707
3708 /**
3709 * dismiss the ComboPage
3710 */
3711 /* package */ void removeComboView() {
3712 if (mComboView != null) {
3713 mContentView.removeView(mComboView);
3714 mTitleBar.setVisibility(View.VISIBLE);
3715 mMenuState = R.id.MAIN_MENU;
3716 attachTabToContentView(mTabControl.getCurrentTab());
3717 getTopWindow().requestFocus();
3718 mComboView = null;
3719 }
3720 }
3721
3722 /**
3723 * callback from ComboPage when clear history is requested
3724 */
3725 public void onRemoveParentChildRelationships() {
3726 mTabControl.removeParentChildRelationShips();
3727 }
3728
The Android Open Source Project0c908882009-03-03 19:32:16 -08003729 @Override
3730 protected void onActivityResult(int requestCode, int resultCode,
3731 Intent intent) {
Grace Klobabb394f32009-11-19 10:26:37 -08003732 if (getTopWindow() == null) return;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003733 switch (requestCode) {
Leon Scrogginsfde97462010-01-11 13:06:21 -05003734 case PREFERENCES_PAGE:
3735 if (resultCode == RESULT_OK && intent != null) {
3736 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
3737 if (BrowserSettings.PREF_CLEAR_HISTORY.equals(action)) {
3738 mTabControl.removeParentChildRelationShips();
3739 }
3740 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003741 break;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003742 // Choose a file from the file picker.
3743 case FILE_SELECTED:
3744 if (null == mUploadMessage) break;
3745 Uri result = intent == null || resultCode != RESULT_OK ? null
3746 : intent.getData();
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003747
3748 // As we ask the camera to save the result of the user taking
3749 // a picture, the camera application does not return anything other
3750 // than RESULT_OK. So we need to check whether the file we expected
3751 // was written to disk in the in the case that we
3752 // did not get an intent returned but did get a RESULT_OK. If it was,
3753 // we assume that this result has came back from the camera.
3754 if (result == null && intent == null && resultCode == RESULT_OK) {
3755 File cameraFile = new File(mCameraFilePath);
3756 if (cameraFile.exists()) {
3757 result = Uri.fromFile(cameraFile);
Ben Murdoch07d34732010-05-27 18:34:46 +01003758 // Broadcast to the media scanner that we have a new photo
3759 // so it will be added into the gallery for the user.
3760 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003761 }
3762 }
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003763 mUploadMessage.onReceiveValue(result);
3764 mUploadMessage = null;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003765 mCameraFilePath = null;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003766 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003767 default:
3768 break;
3769 }
Leon Scroggins30444232009-09-04 18:36:20 -04003770 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003771 }
3772
3773 /*
3774 * This method is called as a result of the user selecting the options
Leon Scrogginsf08809b2010-01-21 09:27:46 -05003775 * menu to see the download window. It shows the download window on top of
3776 * the current window.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003777 */
Leon Scroggins IIIa682a3c2010-09-27 12:32:40 -04003778 private void viewDownloads() {
3779 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
3780 startActivity(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003781 }
3782
Leon Scroggins160a7e72009-08-14 18:28:01 -04003783 /**
3784 * Open the Go page.
3785 * @param startWithHistory If true, open starting on the history tab.
3786 * Otherwise, start with the bookmarks tab.
Leon Scroggins160a7e72009-08-14 18:28:01 -04003787 */
Michael Kolbe421c242010-10-04 19:29:01 -07003788 /* package */ void bookmarksOrHistoryPicker(boolean startWithHistory) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04003789 if (mTabControl.getCurrentWebView() == null) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003790 return;
3791 }
Michael Kolbe421c242010-10-04 19:29:01 -07003792 Bundle extras = new Bundle();
Leon Scroggins30444232009-09-04 18:36:20 -04003793 // Disable opening in a new window if we have maxed out the windows
Leon Scrogginsbc922852010-10-22 12:15:27 -04003794 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
3795 !mTabControl.canCreateNewTab());
Michael Kolbe421c242010-10-04 19:29:01 -07003796
3797 mComboView = new CombinedBookmarkHistoryView(this,
3798 startWithHistory ? CombinedBookmarkHistoryView.FRAGMENT_ID_HISTORY
3799 : CombinedBookmarkHistoryView.FRAGMENT_ID_BOOKMARKS,
3800 extras);
3801 removeTabFromContentView(mTabControl.getCurrentTab());
3802 mTitleBar.setVisibility(View.GONE);
3803 hideFakeTitleBar();
3804 mContentView.addView(mComboView, COVER_SCREEN_PARAMS);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003805 }
3806
3807 // Called when loading from context menu or LOAD_URL message
Leon Scroggins92472e82010-02-17 16:32:28 -05003808 private void loadUrlFromContext(WebView view, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003809 // In case the user enters nothing.
3810 if (url != null && url.length() != 0 && view != null) {
3811 url = smartUrlFilter(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003812 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
Leon Scroggins92472e82010-02-17 16:32:28 -05003813 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003814 }
3815 }
3816 }
3817
Leon Scroggins92472e82010-02-17 16:32:28 -05003818 /**
3819 * Load the URL into the given WebView and update the title bar
3820 * to reflect the new load. Call this instead of WebView.loadUrl
3821 * directly.
3822 * @param view The WebView used to load url.
3823 * @param url The URL to load.
3824 */
3825 private void loadUrl(WebView view, String url) {
3826 updateTitleBarForNewLoad(view, url);
3827 view.loadUrl(url);
3828 }
3829
3830 /**
3831 * Load UrlData into a Tab and update the title bar to reflect the new
3832 * load. Call this instead of UrlData.loadIn directly.
3833 * @param t The Tab used to load.
3834 * @param data The UrlData being loaded.
3835 */
3836 private void loadUrlDataIn(Tab t, UrlData data) {
3837 updateTitleBarForNewLoad(t.getWebView(), data.mUrl);
3838 data.loadIn(t);
3839 }
3840
3841 /**
3842 * If the WebView is the top window, update the title bar to reflect
3843 * loading the new URL. i.e. set its text, clear the favicon (which
3844 * will be set once the page begins loading), and set the progress to
3845 * INITIAL_PROGRESS to show that the page has begun to load. Called
3846 * by loadUrl and loadUrlDataIn.
3847 * @param view The WebView that is starting a load.
3848 * @param url The URL that is being loaded.
3849 */
3850 private void updateTitleBarForNewLoad(WebView view, String url) {
3851 if (view == getTopWindow()) {
3852 setUrlTitle(url, null);
3853 setFavicon(null);
3854 onProgressChanged(view, INITIAL_PROGRESS);
3855 }
3856 }
3857
The Android Open Source Project0c908882009-03-03 19:32:16 -08003858 private String smartUrlFilter(Uri inUri) {
3859 if (inUri != null) {
3860 return smartUrlFilter(inUri.toString());
3861 }
3862 return null;
3863 }
3864
Feng Qianb34f87a2009-03-24 21:27:26 -07003865 protected static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003866 "(?i)" + // switch on case insensitive matching
3867 "(" + // begin group for schema
3868 "(?:http|https|file):\\/\\/" +
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003869 "|(?:inline|data|about|content|javascript):" +
The Android Open Source Project0c908882009-03-03 19:32:16 -08003870 ")" +
3871 "(.*)" );
3872
3873 /**
3874 * Attempts to determine whether user input is a URL or search
3875 * terms. Anything with a space is passed to search.
3876 *
3877 * Converts to lowercase any mistakenly uppercased schema (i.e.,
3878 * "Http://" converts to "http://"
3879 *
3880 * @return Original or modified URL
3881 *
3882 */
3883 String smartUrlFilter(String url) {
3884
3885 String inUrl = url.trim();
3886 boolean hasSpace = inUrl.indexOf(' ') != -1;
3887
3888 Matcher matcher = ACCEPTED_URI_SCHEMA.matcher(inUrl);
3889 if (matcher.matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003890 // force scheme to lowercase
3891 String scheme = matcher.group(1);
3892 String lcScheme = scheme.toLowerCase();
3893 if (!lcScheme.equals(scheme)) {
Mitsuru Oshima123ecfb2009-05-18 19:11:14 -07003894 inUrl = lcScheme + matcher.group(2);
3895 }
3896 if (hasSpace) {
3897 inUrl = inUrl.replace(" ", "%20");
The Android Open Source Project0c908882009-03-03 19:32:16 -08003898 }
3899 return inUrl;
3900 }
Leon Scroggins956cf012010-10-07 13:51:57 -04003901 if (!hasSpace) {
Dan Egnor5ee906c2009-11-18 12:11:49 -08003902 if (Patterns.WEB_URL.matcher(inUrl).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003903 return URLUtil.guessUrl(inUrl);
3904 }
3905 }
3906
Leon Scroggins956cf012010-10-07 13:51:57 -04003907 // FIXME: Is this the correct place to add to searches?
3908 // what if someone else calls this function?
3909
The Android Open Source Project0c908882009-03-03 19:32:16 -08003910 Browser.addSearchUrl(mResolver, inUrl);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003911 return URLUtil.composeSearchUrl(inUrl, QuickSearch_G, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003912 }
3913
Ben Murdochbff2d602009-07-01 20:19:05 +01003914 /* package */ void setShouldShowErrorConsole(boolean flag) {
3915 if (flag == mShouldShowErrorConsole) {
3916 // Nothing to do.
3917 return;
3918 }
Leon Scroggins IIIa9e35b62010-09-16 15:30:15 -04003919 Tab t = mTabControl.getCurrentTab();
3920 if (t == null) {
3921 // There is no current tab so we cannot toggle the error console
3922 return;
3923 }
Ben Murdochbff2d602009-07-01 20:19:05 +01003924
3925 mShouldShowErrorConsole = flag;
3926
Leon Scroggins IIIa9e35b62010-09-16 15:30:15 -04003927 ErrorConsoleView errorConsole = t.getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01003928
3929 if (flag) {
3930 // Setting the show state of the console will cause it's the layout to be inflated.
3931 if (errorConsole.numberOfErrors() > 0) {
3932 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
3933 } else {
3934 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
3935 }
3936
3937 // Now we can add it to the main view.
3938 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08003939 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01003940 ViewGroup.LayoutParams.WRAP_CONTENT));
3941 } else {
3942 mErrorConsoleContainer.removeView(errorConsole);
3943 }
3944
3945 }
3946
Grace Kloba22ac16e2009-10-07 18:00:23 -07003947 boolean shouldShowErrorConsole() {
3948 return mShouldShowErrorConsole;
3949 }
3950
Andrei Popescu163ab742009-10-20 17:58:23 +01003951 private void setStatusBarVisibility(boolean visible) {
3952 int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
3953 getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
3954 }
3955
Andrei Popescu56199cc2010-01-12 22:39:16 +00003956
3957 private void sendNetworkType(String type, String subtype) {
3958 WebView w = mTabControl.getCurrentWebView();
3959 if (w != null) {
3960 w.setNetworkType(type, subtype);
3961 }
3962 }
3963
Grace Klobaeb6eef42009-09-15 17:56:32 -07003964 final static int LOCK_ICON_UNSECURE = 0;
3965 final static int LOCK_ICON_SECURE = 1;
3966 final static int LOCK_ICON_MIXED = 2;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003967
The Android Open Source Project0c908882009-03-03 19:32:16 -08003968 private BrowserSettings mSettings;
3969 private TabControl mTabControl;
3970 private ContentResolver mResolver;
3971 private FrameLayout mContentView;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003972 private View mCustomView;
3973 private FrameLayout mCustomViewContainer;
Andrei Popescuc9b55562009-07-07 10:51:15 +01003974 private WebChromeClient.CustomViewCallback mCustomViewCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003975
3976 // FIXME, temp address onPrepareMenu performance problem. When we move everything out of
3977 // view, we should rewrite this.
3978 private int mCurrentMenuState = 0;
3979 private int mMenuState = R.id.MAIN_MENU;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003980 private int mOldMenuState = EMPTY_MENU;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003981 private static final int EMPTY_MENU = -1;
3982 private Menu mMenu;
3983
The Android Open Source Project0c908882009-03-03 19:32:16 -08003984 // Used to prevent chording to result in firing two shortcuts immediately
3985 // one after another. Fixes bug 1211714.
3986 boolean mCanChord;
3987
3988 private boolean mInLoad;
3989 private boolean mIsNetworkUp;
Ben Murdochb7cc8b42009-09-28 10:59:09 +01003990 private boolean mDidStopLoad;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003991
Cary Clark1f10cbf2010-03-22 11:45:23 -04003992 /* package */ boolean mActivityInPause = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003993
3994 private boolean mMenuIsDown;
3995
The Android Open Source Project0c908882009-03-03 19:32:16 -08003996 private static boolean mInTrace;
3997
3998 // Performance probe
3999 private static final int[] SYSTEM_CPU_FORMAT = new int[] {
4000 Process.PROC_SPACE_TERM | Process.PROC_COMBINE,
4001 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time
4002 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time
4003 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time
4004 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time
4005 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time
4006 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time
4007 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG // 7: softirq time
4008 };
4009
4010 private long mStart;
4011 private long mProcessStart;
4012 private long mUserStart;
4013 private long mSystemStart;
4014 private long mIdleStart;
4015 private long mIrqStart;
4016
4017 private long mUiStart;
4018
4019 private Drawable mMixLockIcon;
4020 private Drawable mSecLockIcon;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004021
4022 /* hold a ref so we can auto-cancel if necessary */
4023 private AlertDialog mAlertDialog;
4024
The Android Open Source Project0c908882009-03-03 19:32:16 -08004025 // The up-to-date URL and title (these can be different from those stored
4026 // in WebView, since it takes some time for the information in WebView to
4027 // get updated)
4028 private String mUrl;
4029 private String mTitle;
4030
4031 // As PageInfo has different style for landscape / portrait, we have
4032 // to re-open it when configuration changed
4033 private AlertDialog mPageInfoDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07004034 private Tab mPageInfoView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004035 // If the Page-Info dialog is launched from the SSL-certificate-on-error
4036 // dialog, we should not just dismiss it, but should get back to the
4037 // SSL-certificate-on-error dialog. This flag is used to store this state
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05004038 private boolean mPageInfoFromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004039
4040 // as SSLCertificateOnError has different style for landscape / portrait,
4041 // we have to re-open it when configuration changed
4042 private AlertDialog mSSLCertificateOnErrorDialog;
4043 private WebView mSSLCertificateOnErrorView;
4044 private SslErrorHandler mSSLCertificateOnErrorHandler;
4045 private SslError mSSLCertificateOnErrorError;
4046
4047 // as SSLCertificate has different style for landscape / portrait, we
4048 // have to re-open it when configuration changed
4049 private AlertDialog mSSLCertificateDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07004050 private Tab mSSLCertificateView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004051
4052 // as HttpAuthentication has different style for landscape / portrait, we
4053 // have to re-open it when configuration changed
Bjorn Bringert25738922010-10-12 10:56:20 +01004054 private HttpAuthenticationDialog mHttpAuthenticationDialog;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004055
4056 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
4057 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08004058 ViewGroup.LayoutParams.MATCH_PARENT,
4059 ViewGroup.LayoutParams.MATCH_PARENT);
Andrei Popescuadc008d2009-06-26 14:11:30 +01004060 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
4061 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08004062 ViewGroup.LayoutParams.MATCH_PARENT,
4063 ViewGroup.LayoutParams.MATCH_PARENT,
Andrei Popescuadc008d2009-06-26 14:11:30 +01004064 Gravity.CENTER);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07004065 // Google search
4066 final static String QuickSearch_G = "http://www.google.com/m?q=%s";
The Android Open Source Project0c908882009-03-03 19:32:16 -08004067
4068 final static String QUERY_PLACE_HOLDER = "%s";
4069
4070 // "source" parameter for Google search through search key
4071 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
4072 // "source" parameter for Google search through goto menu
4073 final static String GOOGLE_SEARCH_SOURCE_GOTO = "browser-goto";
4074 // "source" parameter for Google search through simplily type
4075 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
4076 // "source" parameter for Google search suggested by the browser
4077 final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
4078 // "source" parameter for Google search from unknown source
4079 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown";
4080
4081 private final static String LOGTAG = "browser";
4082
The Android Open Source Project0c908882009-03-03 19:32:16 -08004083 private String mLastEnteredUrl;
4084
4085 private PowerManager.WakeLock mWakeLock;
4086 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
4087
4088 private Toast mStopToast;
4089
Leon Scroggins571b3762010-05-26 10:25:01 -04004090 private TitleBarBase mTitleBar;
Michael Kolba2b2ba82010-08-04 17:54:03 -07004091 private TabBar mTabBar;
Leon Scroggins81db3662009-06-04 17:45:11 -04004092
Ben Murdochbff2d602009-07-01 20:19:05 +01004093 private LinearLayout mErrorConsoleContainer = null;
4094 private boolean mShouldShowErrorConsole = false;
4095
The Android Open Source Project0c908882009-03-03 19:32:16 -08004096 // As the ids are dynamically created, we can't guarantee that they will
4097 // be in sequence, so this static array maps ids to a window number.
4098 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
4099 { R.id.window_one_menu_id, R.id.window_two_menu_id, R.id.window_three_menu_id,
4100 R.id.window_four_menu_id, R.id.window_five_menu_id, R.id.window_six_menu_id,
4101 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
4102
4103 // monitor platform changes
4104 private IntentFilter mNetworkStateChangedFilter;
4105 private BroadcastReceiver mNetworkStateIntentReceiver;
4106
Bjorn Bringerta7611812010-03-24 11:12:02 +00004107 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
4108
The Android Open Source Project0c908882009-03-03 19:32:16 -08004109 // activity requestCode
Nicolas Roard78a98e42009-05-11 13:34:17 +01004110 final static int PREFERENCES_PAGE = 3;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04004111 final static int FILE_SELECTED = 4;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004112
Andrei Popescu540035d2009-09-18 18:59:20 +01004113 // the default <video> poster
4114 private Bitmap mDefaultVideoPoster;
4115 // the video progress view
4116 private View mVideoProgressView;
4117
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004118 /**
4119 * A UrlData class to abstract how the content will be set to WebView.
4120 * This base class uses loadUrl to show the content.
4121 */
Leon Scroggins6eac63e2010-03-15 18:19:14 -04004122 /* package */ static class UrlData {
Grace Kloba068e48b2010-01-26 18:11:27 -08004123 final String mUrl;
4124 final Map<String, String> mHeaders;
Leon Scroggins58d56c62010-01-28 15:12:40 -05004125 final Intent mVoiceIntent;
Grace Kloba60e095c2009-06-16 11:50:55 -07004126
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004127 UrlData(String url) {
4128 this.mUrl = url;
Grace Kloba068e48b2010-01-26 18:11:27 -08004129 this.mHeaders = null;
Leon Scroggins58d56c62010-01-28 15:12:40 -05004130 this.mVoiceIntent = null;
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004131 }
Grace Kloba60e095c2009-06-16 11:50:55 -07004132
Leon Scroggins58d56c62010-01-28 15:12:40 -05004133 UrlData(String url, Map<String, String> headers, Intent intent) {
Grace Kloba068e48b2010-01-26 18:11:27 -08004134 this.mUrl = url;
4135 this.mHeaders = headers;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -05004136 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
4137 .equals(intent.getAction())) {
Leon Scroggins58d56c62010-01-28 15:12:40 -05004138 this.mVoiceIntent = intent;
4139 } else {
4140 this.mVoiceIntent = null;
4141 }
Grace Kloba60e095c2009-06-16 11:50:55 -07004142 }
4143
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004144 boolean isEmpty() {
Leon Scroggins58d56c62010-01-28 15:12:40 -05004145 return mVoiceIntent == null && (mUrl == null || mUrl.length() == 0);
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004146 }
4147
Leon Scroggins92472e82010-02-17 16:32:28 -05004148 /**
4149 * Load this UrlData into the given Tab. Use loadUrlDataIn to update
4150 * the title bar as well.
4151 */
Leon Scroggins58d56c62010-01-28 15:12:40 -05004152 public void loadIn(Tab t) {
4153 if (mVoiceIntent != null) {
4154 t.activateVoiceSearchMode(mVoiceIntent);
4155 } else {
4156 t.getWebView().loadUrl(mUrl, mHeaders);
4157 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004158 }
4159 };
4160
Leon Scroggins1f005d32009-08-10 17:36:42 -04004161 /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08004162}