blob: b882338f57a59a8bf1701c104275dac34bc356f6 [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;
Michael Kolbc7485ae2010-09-03 10:10:58 -070027import android.app.Dialog;
Steve Howard5a862fc2010-09-28 12:52:06 -070028import android.app.DownloadManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.app.ProgressDialog;
30import android.app.SearchManager;
31import android.content.ActivityNotFoundException;
32import android.content.BroadcastReceiver;
Dianne Hackborn80f32622010-08-05 14:17:53 -070033import android.content.ClipboardManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.content.ComponentName;
Leon Scroggins58d56c62010-01-28 15:12:40 -050035import android.content.ContentProvider;
36import android.content.ContentProviderClient;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.content.ContentResolver;
38import android.content.ContentValues;
39import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
42import android.content.IntentFilter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import android.content.pm.PackageManager;
44import android.content.pm.ResolveInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.content.res.Configuration;
46import android.content.res.Resources;
47import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080048import android.graphics.Bitmap;
Andrei Popescu540035d2009-09-18 18:59:20 +010049import android.graphics.BitmapFactory;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050import android.graphics.Canvas;
The Android Open Source Project0c908882009-03-03 19:32:16 -080051import android.graphics.Picture;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040052import android.graphics.PixelFormat;
The Android Open Source Project0c908882009-03-03 19:32:16 -080053import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080054import android.net.ConnectivityManager;
Andrei Popescu56199cc2010-01-12 22:39:16 +000055import android.net.NetworkInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080056import android.net.Uri;
57import android.net.WebAddress;
The Android Open Source Project0c908882009-03-03 19:32:16 -080058import android.net.http.SslCertificate;
59import android.net.http.SslError;
60import android.os.AsyncTask;
61import android.os.Bundle;
62import android.os.Debug;
63import android.os.Environment;
64import android.os.Handler;
The Android Open Source Project0c908882009-03-03 19:32:16 -080065import android.os.Message;
66import android.os.PowerManager;
67import android.os.Process;
The Android Open Source Project0c908882009-03-03 19:32:16 -080068import android.os.SystemClock;
The Android Open Source Project0c908882009-03-03 19:32:16 -080069import android.provider.Browser;
Michael Kolb370a4f32010-10-06 10:45:32 -070070import android.provider.BrowserContract;
Jeff Hamilton1a805652010-09-07 12:36:30 -070071import android.provider.BrowserContract.Images;
Michael Kolbe421c242010-10-04 19:29:01 -070072import android.provider.ContactsContract;
Michael Kolba2b2ba82010-08-04 17:54:03 -070073import android.provider.ContactsContract.Intents.Insert;
The Android Open Source Project0c908882009-03-03 19:32:16 -080074import android.provider.Downloads;
75import android.provider.MediaStore;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050076import android.speech.RecognizerResultsIntent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080077import android.text.TextUtils;
78import android.text.format.DateFormat;
The Android Open Source Project0c908882009-03-03 19:32:16 -080079import android.util.Log;
Dianne Hackborn385effd2010-02-24 20:03:04 -080080import android.util.Patterns;
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -040081import android.view.ActionMode;
The Android Open Source Project0c908882009-03-03 19:32:16 -080082import android.view.ContextMenu;
Michael Kolba2b2ba82010-08-04 17:54:03 -070083import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080084import android.view.Gravity;
85import android.view.KeyEvent;
86import android.view.LayoutInflater;
87import android.view.Menu;
88import android.view.MenuInflater;
89import android.view.MenuItem;
Michael Kolba2b2ba82010-08-04 17:54:03 -070090import android.view.MenuItem.OnMenuItemClickListener;
The Android Open Source Project0c908882009-03-03 19:32:16 -080091import android.view.View;
92import android.view.ViewGroup;
93import android.view.Window;
94import android.view.WindowManager;
Svetoslav Ganov2b345992010-05-06 06:13:54 -070095import android.view.accessibility.AccessibilityManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080096import android.webkit.CookieManager;
97import android.webkit.CookieSyncManager;
98import android.webkit.DownloadListener;
99import android.webkit.HttpAuthHandler;
100import android.webkit.SslErrorHandler;
101import android.webkit.URLUtil;
Leon Clarkecb6cc862009-09-29 18:35:13 +0100102import android.webkit.ValueCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800103import android.webkit.WebChromeClient;
104import android.webkit.WebHistoryItem;
105import android.webkit.WebIconDatabase;
Leon Scroggins88d08032010-10-21 15:17:10 -0400106import android.webkit.WebSettings;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800107import android.webkit.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800108import android.widget.EditText;
109import android.widget.FrameLayout;
110import android.widget.LinearLayout;
111import android.widget.TextView;
112import android.widget.Toast;
113
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400114import java.io.ByteArrayOutputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800115import java.io.File;
Ben Murdoch4f75ba22009-10-27 11:48:28 +0000116import java.io.IOException;
117import java.io.InputStream;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118import java.net.MalformedURLException;
Dianne Hackborn99189432009-06-17 18:06:18 -0700119import java.net.URISyntaxException;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800120import java.net.URL;
121import java.net.URLEncoder;
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700122import java.util.Calendar;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123import java.util.Date;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124import java.util.HashMap;
Grace Kloba00f54c52010-01-27 14:53:51 -0800125import java.util.Iterator;
Grace Kloba068e48b2010-01-26 18:11:27 -0800126import java.util.Map;
Michael Kolbfe251992010-07-08 15:41:55 -0700127import java.util.Vector;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800128import java.util.regex.Matcher;
129import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800130
131public class BrowserActivity extends Activity
Michael Kolbe421c242010-10-04 19:29:01 -0700132 implements View.OnCreateContextMenuListener, DownloadListener,
Leon Scroggins88d08032010-10-21 15:17:10 -0400133 BookmarksHistoryCallbacks {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800134
Dave Bort31a6d1c2009-04-13 15:56:49 -0700135 /* Define some aliases to make these debugging flags easier to refer to.
136 * This file imports android.provider.Browser, so we can't just refer to "Browser.DEBUG".
137 */
138 private final static boolean DEBUG = com.android.browser.Browser.DEBUG;
139 private final static boolean LOGV_ENABLED = com.android.browser.Browser.LOGV_ENABLED;
140 private final static boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
141
Cary Clarka9771242009-08-11 16:42:26 -0400142 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800143 @Override
144 public Void doInBackground(File... files) {
145 if (files != null) {
146 for (File f : files) {
Cary Clarkd6be1752009-08-12 12:56:42 -0400147 if (!f.delete()) {
148 Log.e(LOGTAG, f.getPath() + " was not deleted");
149 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800150 }
151 }
152 return null;
153 }
154 }
155
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400156 /**
157 * This layout holds everything you see below the status bar, including the
158 * error console, the custom view container, and the webviews.
159 */
160 private FrameLayout mBrowserFrameLayout;
Leon Scroggins81db3662009-06-04 17:45:11 -0400161
Michael Kolbe421c242010-10-04 19:29:01 -0700162 private CombinedBookmarkHistoryView mComboView;
163
Leon Scrogginsd746a942010-05-19 13:21:44 -0400164 private boolean mXLargeScreenSize;
165
Jeff Davidson43610292010-07-16 16:03:58 -0700166 private Boolean mIsProviderPresent = null;
167 private Uri mRlzUri = null;
168
Grace Kloba22ac16e2009-10-07 18:00:23 -0700169 @Override
170 public void onCreate(Bundle icicle) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700171 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800172 Log.v(LOGTAG, this + " onStart");
173 }
174 super.onCreate(icicle);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800175 // test the browser in OpenGL
176 // requestWindowFeature(Window.FEATURE_OPENGL);
177
Mike Reedd334bf52010-01-26 15:21:44 -0500178 // enable this to test the browser in 32bit
179 if (false) {
180 getWindow().setFormat(PixelFormat.RGBX_8888);
181 BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
182 }
183
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100184 if (((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE)).isEnabled()) {
Svetoslav Ganov2b345992010-05-06 06:13:54 -0700185 setDefaultKeyMode(DEFAULT_KEYS_DISABLE);
186 } else {
187 setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
188 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800189
190 mResolver = getContentResolver();
191
Leon Scroggins IIIcc14c8c2010-09-27 12:45:34 -0400192 // Keep a settings instance handy.
193 mSettings = BrowserSettings.getInstance();
194
Grace Kloba0923d692009-09-23 21:37:25 -0700195 // If this was a web search request, pass it on to the default web
196 // search provider and finish this activity.
197 if (handleWebSearchIntent(getIntent())) {
198 finish();
199 return;
200 }
201
Michael Kolb0506f2d2010-10-14 16:20:16 -0700202 mSecLockIcon = getResources().getDrawable(R.drawable.ic_secure);
203 mMixLockIcon = getResources().getDrawable(R.drawable.ic_partial_secure);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800204
Michael Kolbed217742010-08-10 17:52:34 -0700205 // Create the tab control and our initial tab
206 mTabControl = new TabControl(this);
207
208 mXLargeScreenSize = (getResources().getConfiguration().screenLayout
209 & Configuration.SCREENLAYOUT_SIZE_MASK)
210 == Configuration.SCREENLAYOUT_SIZE_XLARGE;
211
Leon Scroggins81db3662009-06-04 17:45:11 -0400212 FrameLayout frameLayout = (FrameLayout) getWindow().getDecorView()
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100213 .findViewById(android.R.id.content);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400214 mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(this)
215 .inflate(R.layout.custom_screen, null);
216 mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(
217 R.id.main_content);
218 mErrorConsoleContainer = (LinearLayout) mBrowserFrameLayout
219 .findViewById(R.id.error_console);
220 mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
221 .findViewById(R.id.fullscreen_custom_content);
222 frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
Michael Kolbe0a36662010-06-29 10:37:12 -0700223
Leon Scrogginsd746a942010-05-19 13:21:44 -0400224 if (mXLargeScreenSize) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700225 mTitleBar = new TitleBarXLarge(this);
226 mTitleBar.setProgress(100);
227 mFakeTitleBar = new TitleBarXLarge(this);
Michael Kolbed217742010-08-10 17:52:34 -0700228 ActionBar actionBar = getActionBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700229 mTabBar = new TabBar(this, mTabControl, (TitleBarXLarge) mFakeTitleBar);
Michael Kolbed217742010-08-10 17:52:34 -0700230 actionBar.setCustomNavigationMode(mTabBar);
Michael Kolb68775752010-08-19 12:42:01 -0700231 // disable built in zoom controls
232 mTabControl.setDisplayZoomControls(false);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400233 } else {
Leon Scroggins571b3762010-05-26 10:25:01 -0400234 mTitleBar = new TitleBar(this);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400235 // mTitleBar will be always be shown in the fully loaded mode on
236 // phone
237 mTitleBar.setProgress(100);
Leon Scrogginsd746a942010-05-19 13:21:44 -0400238 mFakeTitleBar = new TitleBar(this);
239 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800240
The Android Open Source Project0c908882009-03-03 19:32:16 -0800241 // Open the icon database and retain all the bookmark urls for favicons
242 retainIconsOnStartup();
243
The Android Open Source Project0c908882009-03-03 19:32:16 -0800244 mSettings.setTabControl(mTabControl);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800245
246 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
247 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
248
Patrick Scott6adacc92010-03-05 08:24:51 -0500249 // Find out if the network is currently up.
250 ConnectivityManager cm = (ConnectivityManager) getSystemService(
251 Context.CONNECTIVITY_SERVICE);
252 NetworkInfo info = cm.getActiveNetworkInfo();
253 if (info != null) {
254 mIsNetworkUp = info.isAvailable();
255 }
256
Grace Klobaa34f6862009-07-31 16:28:17 -0700257 /* enables registration for changes in network status from
258 http stack */
259 mNetworkStateChangedFilter = new IntentFilter();
260 mNetworkStateChangedFilter.addAction(
261 ConnectivityManager.CONNECTIVITY_ACTION);
262 mNetworkStateIntentReceiver = new BroadcastReceiver() {
263 @Override
264 public void onReceive(Context context, Intent intent) {
265 if (intent.getAction().equals(
266 ConnectivityManager.CONNECTIVITY_ACTION)) {
Andrei Popescue4c98462010-02-19 15:44:13 +0000267
268 NetworkInfo info = intent.getParcelableExtra(
269 ConnectivityManager.EXTRA_NETWORK_INFO);
270 String typeName = info.getTypeName();
271 String subtypeName = info.getSubtypeName();
272 sendNetworkType(typeName.toLowerCase(),
273 (subtypeName != null ? subtypeName.toLowerCase() : ""));
274
275 onNetworkToggle(info.isAvailable());
Grace Klobaa34f6862009-07-31 16:28:17 -0700276 }
277 }
278 };
279
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700280 // Unless the last browser usage was within 24 hours, destroy any
281 // remaining incognito tabs.
282
283 Calendar lastActiveDate = icicle != null ? (Calendar) icicle.getSerializable("lastActiveDate") : null;
284 Calendar today = Calendar.getInstance();
285 Calendar yesterday = Calendar.getInstance();
286 yesterday.add(Calendar.DATE, -1);
287
288 boolean dontRestoreIncognitoTabs = lastActiveDate == null
289 || lastActiveDate.before(yesterday)
290 || lastActiveDate.after(today);
291
292 if (!mTabControl.restoreState(icicle, dontRestoreIncognitoTabs)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800293 // clear up the thumbnail directory if we can't restore the state as
294 // none of the files in the directory are referenced any more.
295 new ClearThumbnails().execute(
296 mTabControl.getThumbnailDir().listFiles());
Grace Klobaaab3f092009-07-30 12:29:51 -0700297 // there is no quit on Android. But if we can't restore the state,
298 // we can treat it as a new Browser, remove the old session cookies.
299 CookieManager.getInstance().removeSessionCookie();
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700300 // remove any incognito files
301 WebView.cleanupPrivateBrowsingFiles(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800302 final Intent intent = getIntent();
303 final Bundle extra = intent.getExtras();
304 // Create an initial tab.
305 // If the intent is ACTION_VIEW and data is not null, the Browser is
306 // invoked to view the content by another application. In this case,
307 // the tab will be close when exit.
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700308 UrlData urlData = getUrlDataFromIntent(intent);
309
Leon Scroggins58d56c62010-01-28 15:12:40 -0500310 String action = intent.getAction();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700311 final Tab t = mTabControl.createNewTab(
Leon Scroggins58d56c62010-01-28 15:12:40 -0500312 (Intent.ACTION_VIEW.equals(action) &&
313 intent.getData() != null)
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500314 || RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
315 .equals(action),
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700316 intent.getStringExtra(Browser.EXTRA_APPLICATION_ID),
317 urlData.mUrl, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800318 mTabControl.setCurrentTab(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800319 attachTabToContentView(t);
320 WebView webView = t.getWebView();
321 if (extra != null) {
322 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
323 if (scale > 0 && scale <= 1000) {
324 webView.setInitialScale(scale);
325 }
326 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800327
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700328 if (urlData.isEmpty()) {
Shimeng (Simon) Wang98d5fce2010-03-16 13:23:39 -0700329 loadUrl(webView, mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800330 } else {
Patrick Scott9d53da02010-02-19 10:19:01 -0500331 loadUrlDataIn(t, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800332 }
333 } else {
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700334 if (dontRestoreIncognitoTabs) {
335 WebView.cleanupPrivateBrowsingFiles(this);
336 }
337
The Android Open Source Project0c908882009-03-03 19:32:16 -0800338 // TabControl.restoreState() will create a new tab even if
Leon Scroggins1f005d32009-08-10 17:36:42 -0400339 // restoring the state fails.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800340 attachTabToContentView(mTabControl.getCurrentTab());
341 }
Grace Kloba615c6c92009-08-03 10:22:44 -0700342
Cary Clarkb4b83182010-07-01 12:36:56 -0400343 // Delete old thumbnails to save space
344 File dir = mTabControl.getThumbnailDir();
345 if (dir.exists()) {
346 for (String child : dir.list()) {
347 File f = new File(dir, child);
348 f.delete();
349 }
350 }
351
Feng Qianb3c02da2009-06-29 15:58:08 -0700352 // Read JavaScript flags if it exists.
353 String jsFlags = mSettings.getJsFlags();
354 if (jsFlags.trim().length() != 0) {
355 mTabControl.getCurrentWebView().setJsFlags(jsFlags);
356 }
Bjorn Bringerta7611812010-03-24 11:12:02 +0000357
358 // Start watching the default geolocation permissions
359 mSystemAllowGeolocationOrigins
360 = new SystemAllowGeolocationOrigins(getApplicationContext());
361 mSystemAllowGeolocationOrigins.start();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800362 }
363
Michael Kolba2b2ba82010-08-04 17:54:03 -0700364 ScrollListener getScrollListener() {
365 return mTabBar;
366 }
367
Leon Scroggins58d56c62010-01-28 15:12:40 -0500368 /**
369 * Feed the previously stored results strings to the BrowserProvider so that
370 * the SearchDialog will show them instead of the standard searches.
371 * @param result String to show on the editable line of the SearchDialog.
372 */
373 /* package */ void showVoiceSearchResults(String result) {
374 ContentProviderClient client = mResolver.acquireContentProviderClient(
375 Browser.BOOKMARKS_URI);
376 ContentProvider prov = client.getLocalContentProvider();
377 BrowserProvider bp = (BrowserProvider) prov;
378 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
379 client.release();
380
Leon Scrogginsfbb3f152010-03-09 17:26:56 -0500381 Bundle bundle = createGoogleSearchSourceBundle(
382 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
383 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
384 startSearch(result, false, bundle, false);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500385 }
386
The Android Open Source Project0c908882009-03-03 19:32:16 -0800387 @Override
388 protected void onNewIntent(Intent intent) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700389 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800390 // When a tab is closed on exit, the current tab index is set to -1.
391 // Reset before proceed as Browser requires the current tab to be set.
392 if (current == null) {
393 // Try to reset the tab in case the index was incorrect.
394 current = mTabControl.getTab(0);
395 if (current == null) {
396 // No tabs at all so just ignore this intent.
397 return;
398 }
399 mTabControl.setCurrentTab(current);
400 attachTabToContentView(current);
401 resetTitleAndIcon(current.getWebView());
402 }
403 final String action = intent.getAction();
404 final int flags = intent.getFlags();
405 if (Intent.ACTION_MAIN.equals(action) ||
406 (flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
407 // just resume the browser
408 return;
409 }
Leon Scrogginsb8a844d2010-03-18 15:06:15 -0400410 // In case the SearchDialog is open.
411 ((SearchManager) getSystemService(Context.SEARCH_SERVICE))
412 .stopSearch();
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500413 boolean activateVoiceSearch = RecognizerResultsIntent
414 .ACTION_VOICE_SEARCH_RESULTS.equals(action);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800415 if (Intent.ACTION_VIEW.equals(action)
416 || Intent.ACTION_SEARCH.equals(action)
417 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
Leon Scroggins58d56c62010-01-28 15:12:40 -0500418 || Intent.ACTION_WEB_SEARCH.equals(action)
419 || activateVoiceSearch) {
Leon Scroggins3b18ce32010-02-08 17:35:59 -0500420 if (current.isInVoiceSearchMode()) {
421 String title = current.getVoiceDisplayTitle();
422 if (title != null && title.equals(intent.getStringExtra(
423 SearchManager.QUERY))) {
424 // The user submitted the same search as the last voice
425 // search, so do nothing.
426 return;
427 }
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500428 if (Intent.ACTION_SEARCH.equals(action)
429 && current.voiceSearchSourceIsGoogle()) {
430 Intent logIntent = new Intent(
431 LoggingEvents.ACTION_LOG_EVENT);
432 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
433 LoggingEvents.VoiceSearch.QUERY_UPDATED);
434 logIntent.putExtra(
435 LoggingEvents.VoiceSearch.EXTRA_QUERY_UPDATED_VALUE,
436 intent.getDataString());
437 sendBroadcast(logIntent);
438 // Note, onPageStarted will revert the voice title bar
439 // When http://b/issue?id=2379215 is fixed, we should update
440 // the title bar here.
441 }
Leon Scroggins3b18ce32010-02-08 17:35:59 -0500442 }
Satish Sampath565505b2009-05-29 15:37:27 +0100443 // If this was a search request (e.g. search query directly typed into the address bar),
444 // pass it on to the default web search provider.
445 if (handleWebSearchIntent(intent)) {
446 return;
447 }
448
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700449 UrlData urlData = getUrlDataFromIntent(intent);
450 if (urlData.isEmpty()) {
451 urlData = new UrlData(mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800452 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700453
Grace Klobacc634032009-07-28 15:58:19 -0700454 final String appId = intent
455 .getStringExtra(Browser.EXTRA_APPLICATION_ID);
Leon Scroggins47208682010-04-07 17:59:48 -0400456 if ((Intent.ACTION_VIEW.equals(action)
457 // If a voice search has no appId, it means that it came
458 // from the browser. In that case, reuse the current tab.
459 || (activateVoiceSearch && appId != null))
Grace Klobacc634032009-07-28 15:58:19 -0700460 && !getPackageName().equals(appId)
461 && (flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700462 Tab appTab = mTabControl.getTabFromId(appId);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700463 if (appTab != null) {
464 Log.i(LOGTAG, "Reusing tab for " + appId);
465 // Dismiss the subwindow if applicable.
466 dismissSubWindow(appTab);
467 // Since we might kill the WebView, remove it from the
468 // content view first.
469 removeTabFromContentView(appTab);
470 // Recreate the main WebView after destroying the old one.
471 // If the WebView has the same original url and is on that
472 // page, it can be reused.
473 boolean needsLoad =
Leon Scroggins6eac63e2010-03-15 18:19:14 -0400474 mTabControl.recreateWebView(appTab, urlData);
Ben Murdochbff2d602009-07-01 20:19:05 +0100475
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700476 if (current != appTab) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400477 switchToTab(mTabControl.getTabIndex(appTab));
478 if (needsLoad) {
Patrick Scott9d53da02010-02-19 10:19:01 -0500479 loadUrlDataIn(appTab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400480 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700481 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400482 // If the tab was the current tab, we have to attach
483 // it to the view system again.
484 attachTabToContentView(appTab);
485 if (needsLoad) {
Patrick Scott9d53da02010-02-19 10:19:01 -0500486 loadUrlDataIn(appTab, urlData);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700487 }
488 }
489 return;
Patrick Scottcd115892009-07-16 09:42:58 -0400490 } else {
491 // No matching application tab, try to find a regular tab
492 // with a matching url.
493 appTab = mTabControl.findUnusedTabWithUrl(urlData.mUrl);
Leon Scroggins25515f82009-08-19 15:31:58 -0400494 if (appTab != null) {
495 if (current != appTab) {
496 switchToTab(mTabControl.getTabIndex(appTab));
497 }
498 // Otherwise, we are already viewing the correct tab.
Patrick Scottcd115892009-07-16 09:42:58 -0400499 } else {
500 // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
501 // will be opened in a new tab unless we have reached
502 // MAX_TABS. Then the url will be opened in the current
503 // tab. If a new tab is created, it will have "true" for
504 // exit on close.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400505 openTabAndShow(urlData, true, appId);
Patrick Scottcd115892009-07-16 09:42:58 -0400506 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700507 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800508 } else {
Grace Kloba638d3f42009-11-23 10:35:04 -0800509 if (!urlData.isEmpty()
510 && urlData.mUrl.startsWith("about:debug")) {
511 if ("about:debug.dom".equals(urlData.mUrl)) {
512 current.getWebView().dumpDomTree(false);
513 } else if ("about:debug.dom.file".equals(urlData.mUrl)) {
514 current.getWebView().dumpDomTree(true);
515 } else if ("about:debug.render".equals(urlData.mUrl)) {
516 current.getWebView().dumpRenderTree(false);
517 } else if ("about:debug.render.file".equals(urlData.mUrl)) {
518 current.getWebView().dumpRenderTree(true);
519 } else if ("about:debug.display".equals(urlData.mUrl)) {
520 current.getWebView().dumpDisplayTree();
521 } else {
522 mSettings.toggleDebugSettings();
523 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800524 return;
525 }
Leon Scroggins1f005d32009-08-10 17:36:42 -0400526 // Get rid of the subwindow if it exists
527 dismissSubWindow(current);
Leon Scroggins8588d152010-04-15 11:01:53 -0400528 // If the current Tab is being used as an application tab,
529 // remove the association, since the new Intent means that it is
530 // no longer associated with that application.
531 current.setAppId(null);
Patrick Scott9d53da02010-02-19 10:19:01 -0500532 loadUrlDataIn(current, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800533 }
534 }
535 }
536
Satish Sampath565505b2009-05-29 15:37:27 +0100537 /**
538 * Launches the default web search activity with the query parameters if the given intent's data
539 * are identified as plain search terms and not URLs/shortcuts.
540 * @return true if the intent was handled and web search activity was launched, false if not.
541 */
542 private boolean handleWebSearchIntent(Intent intent) {
543 if (intent == null) return false;
544
545 String url = null;
546 final String action = intent.getAction();
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500547 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS.equals(
548 action)) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500549 return false;
550 }
Satish Sampath565505b2009-05-29 15:37:27 +0100551 if (Intent.ACTION_VIEW.equals(action)) {
Grace Kloba1e705052009-09-29 13:13:36 -0700552 Uri data = intent.getData();
553 if (data != null) url = data.toString();
Satish Sampath565505b2009-05-29 15:37:27 +0100554 } else if (Intent.ACTION_SEARCH.equals(action)
555 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
556 || Intent.ACTION_WEB_SEARCH.equals(action)) {
557 url = intent.getStringExtra(SearchManager.QUERY);
558 }
Bjorn Bringert04851702009-09-22 10:36:01 +0100559 return handleWebSearchRequest(url, intent.getBundleExtra(SearchManager.APP_DATA),
560 intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
Satish Sampath565505b2009-05-29 15:37:27 +0100561 }
562
563 /**
564 * Launches the default web search activity with the query parameters if the given url string
565 * was identified as plain search terms and not URL/shortcut.
566 * @return true if the request was handled and web search activity was launched, false if not.
567 */
Bjorn Bringert04851702009-09-22 10:36:01 +0100568 private boolean handleWebSearchRequest(String inUrl, Bundle appData, String extraData) {
Satish Sampath565505b2009-05-29 15:37:27 +0100569 if (inUrl == null) return false;
570
571 // In general, we shouldn't modify URL from Intent.
572 // But currently, we get the user-typed URL from search box as well.
573 String url = fixUrl(inUrl).trim();
574
Leon Scroggins956cf012010-10-07 13:51:57 -0400575 // URLs are handled by the regular flow of control, so
Satish Sampath565505b2009-05-29 15:37:27 +0100576 // return early.
Dan Egnor5ee906c2009-11-18 12:11:49 -0800577 if (Patterns.WEB_URL.matcher(url).matches()
Leon Scroggins956cf012010-10-07 13:51:57 -0400578 || ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
Satish Sampath565505b2009-05-29 15:37:27 +0100579 return false;
580 }
581
Leon Scroggins8d06e362010-03-24 14:45:57 -0400582 final ContentResolver cr = mResolver;
583 final String newUrl = url;
Elliott Slaughter627d96f2010-08-18 16:35:30 -0700584 if (mTabControl == null || !mTabControl.getCurrentWebView().isPrivateBrowsingEnabled()) {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700585 new AsyncTask<Void, Void, Void>() {
586 @Override
587 protected Void doInBackground(Void... unused) {
588 Browser.updateVisitedHistory(cr, newUrl, false);
589 Browser.addSearchUrl(cr, newUrl);
590 return null;
591 }
592 }.execute();
593 }
Satish Sampath565505b2009-05-29 15:37:27 +0100594
Bjorn Bringertd69f51d2010-09-13 14:06:41 +0100595 SearchEngine searchEngine = mSettings.getSearchEngine();
596 if (searchEngine == null) return false;
597 searchEngine.startSearch(this, url, appData, extraData);
Satish Sampath565505b2009-05-29 15:37:27 +0100598
599 return true;
600 }
601
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700602 private UrlData getUrlDataFromIntent(Intent intent) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500603 String url = "";
Grace Kloba068e48b2010-01-26 18:11:27 -0800604 Map<String, String> headers = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800605 if (intent != null) {
606 final String action = intent.getAction();
607 if (Intent.ACTION_VIEW.equals(action)) {
608 url = smartUrlFilter(intent.getData());
609 if (url != null && url.startsWith("content:")) {
610 /* Append mimetype so webview knows how to display */
611 String mimeType = intent.resolveType(getContentResolver());
612 if (mimeType != null) {
613 url += "?" + mimeType;
614 }
615 }
Grace Kloba068e48b2010-01-26 18:11:27 -0800616 if (url != null && url.startsWith("http")) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800617 final Bundle pairs = intent
618 .getBundleExtra(Browser.EXTRA_HEADERS);
Grace Kloba2d508ed2010-01-28 11:39:15 -0800619 if (pairs != null && !pairs.isEmpty()) {
Grace Kloba00f54c52010-01-27 14:53:51 -0800620 Iterator<String> iter = pairs.keySet().iterator();
Grace Kloba068e48b2010-01-26 18:11:27 -0800621 headers = new HashMap<String, String>();
Grace Kloba00f54c52010-01-27 14:53:51 -0800622 while (iter.hasNext()) {
623 String key = iter.next();
624 headers.put(key, pairs.getString(key));
Grace Kloba068e48b2010-01-26 18:11:27 -0800625 }
626 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -0700627 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800628 } else if (Intent.ACTION_SEARCH.equals(action)
629 || MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)
630 || Intent.ACTION_WEB_SEARCH.equals(action)) {
631 url = intent.getStringExtra(SearchManager.QUERY);
632 if (url != null) {
633 mLastEnteredUrl = url;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800634 // In general, we shouldn't modify URL from Intent.
635 // But currently, we get the user-typed URL from search box as well.
636 url = fixUrl(url);
637 url = smartUrlFilter(url);
Leon Scroggins8d06e362010-03-24 14:45:57 -0400638 final ContentResolver cr = mResolver;
639 final String newUrl = url;
Elliott Slaughter8389e992010-08-20 15:44:08 -0700640 if (mTabControl == null
641 || mTabControl.getCurrentWebView() == null
642 || !mTabControl.getCurrentWebView().isPrivateBrowsingEnabled()) {
643 new AsyncTask<Void, Void, Void>() {
644 @Override
645 protected Void doInBackground(Void... unused) {
646 Browser.updateVisitedHistory(cr, newUrl, false);
647 return null;
648 }
649 }.execute();
650 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800651 String searchSource = "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&";
652 if (url.contains(searchSource)) {
653 String source = null;
654 final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
655 if (appData != null) {
Bjorn Bringert10d1cca2010-02-10 14:22:12 +0000656 source = appData.getString(Search.SOURCE);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800657 }
658 if (TextUtils.isEmpty(source)) {
659 source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
660 }
661 url = url.replace(searchSource, "&source=android-"+source+"&");
662 }
663 }
664 }
665 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500666 return new UrlData(url, headers, intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800667 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500668 /* package */ void showVoiceTitleBar(String title) {
669 mTitleBar.setInVoiceMode(true);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500670 mTitleBar.setDisplayTitle(title);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700671 mFakeTitleBar.setInVoiceMode(true);
672 mFakeTitleBar.setDisplayTitle(title);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500673 }
674 /* package */ void revertVoiceTitleBar() {
675 mTitleBar.setInVoiceMode(false);
Leon Scroggins003a5dd2010-03-10 12:13:14 -0500676 mTitleBar.setDisplayTitle(mUrl);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700677 mFakeTitleBar.setInVoiceMode(false);
678 mFakeTitleBar.setDisplayTitle(mUrl);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500679 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800680 /* package */ static String fixUrl(String inUrl) {
Cary Clark652ff872009-09-10 13:34:44 -0400681 // FIXME: Converting the url to lower case
682 // duplicates functionality in smartUrlFilter().
683 // However, changing all current callers of fixUrl to
684 // call smartUrlFilter in addition may have unwanted
685 // consequences, and is deferred for now.
686 int colon = inUrl.indexOf(':');
687 boolean allLower = true;
688 for (int index = 0; index < colon; index++) {
689 char ch = inUrl.charAt(index);
690 if (!Character.isLetter(ch)) {
691 break;
692 }
693 allLower &= Character.isLowerCase(ch);
694 if (index == colon - 1 && !allLower) {
695 inUrl = inUrl.substring(0, colon).toLowerCase()
696 + inUrl.substring(colon);
697 }
698 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800699 if (inUrl.startsWith("http://") || inUrl.startsWith("https://"))
700 return inUrl;
701 if (inUrl.startsWith("http:") ||
702 inUrl.startsWith("https:")) {
703 if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) {
704 inUrl = inUrl.replaceFirst("/", "//");
705 } else inUrl = inUrl.replaceFirst(":", "://");
706 }
707 return inUrl;
708 }
709
Grace Kloba22ac16e2009-10-07 18:00:23 -0700710 @Override
711 protected void onResume() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800712 super.onResume();
Dave Bort31a6d1c2009-04-13 15:56:49 -0700713 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800714 Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
715 }
716
717 if (!mActivityInPause) {
718 Log.e(LOGTAG, "BrowserActivity is already resumed.");
719 return;
720 }
721
Mike Reed7bfa63b2009-05-28 11:08:32 -0400722 mTabControl.resumeCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800723 mActivityInPause = false;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400724 resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800725
726 if (mWakeLock.isHeld()) {
727 mHandler.removeMessages(RELEASE_WAKELOCK);
728 mWakeLock.release();
729 }
730
The Android Open Source Project0c908882009-03-03 19:32:16 -0800731 registerReceiver(mNetworkStateIntentReceiver,
732 mNetworkStateChangedFilter);
733 WebView.enablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800734 }
735
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400736 /**
737 * Since the actual title bar is embedded in the WebView, and removing it
Leon Scrogginsfe87bd32009-10-06 10:10:00 -0400738 * would change its appearance, use a different TitleBar to show overlayed
739 * at the top of the screen, when the menu is open or the page is loading.
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400740 */
Michael Kolba2b2ba82010-08-04 17:54:03 -0700741 private TitleBarBase mFakeTitleBar;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400742
743 /**
744 * Keeps track of whether the options menu is open. This is important in
745 * determining whether to show or hide the title bar overlay.
746 */
747 private boolean mOptionsMenuOpen;
748
749 /**
750 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
751 * of whether the configuration has changed. The first onMenuOpened call
752 * after a configuration change is simply a reopening of the same menu
753 * (i.e. mIconView did not change).
754 */
755 private boolean mConfigChanged;
756
757 /**
758 * Whether or not the options menu is in its smaller, icon menu form. When
759 * true, we want the title bar overlay to be up. When false, we do not.
760 * Only meaningful if mOptionsMenuOpen is true.
761 */
762 private boolean mIconView;
763
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400764 @Override
765 public boolean onMenuOpened(int featureId, Menu menu) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400766 if (Window.FEATURE_OPTIONS_PANEL == featureId) {
767 if (mOptionsMenuOpen) {
768 if (mConfigChanged) {
769 // We do not need to make any changes to the state of the
770 // title bar, since the only thing that happened was a
771 // change in orientation
772 mConfigChanged = false;
773 } else {
774 if (mIconView) {
775 // Switching the menu to expanded view, so hide the
776 // title bar.
777 hideFakeTitleBar();
778 mIconView = false;
779 } else {
780 // Switching the menu back to icon view, so show the
781 // title bar once again.
782 showFakeTitleBar();
783 mIconView = true;
784 }
785 }
786 } else {
787 // The options menu is closed, so open it, and show the title
788 showFakeTitleBar();
789 mOptionsMenuOpen = true;
790 mConfigChanged = false;
791 mIconView = true;
792 }
793 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400794 return true;
795 }
796
Michael Kolba2b2ba82010-08-04 17:54:03 -0700797 void showFakeTitleBar() {
798 if (!isFakeTitleBarShowing() && mActiveTabsPage == null && !mActivityInPause) {
Grace Kloba847c25b2010-03-30 16:00:26 -0700799 WebView mainView = mTabControl.getCurrentWebView();
800 // if there is no current WebView, don't show the faked title bar;
Grace Kloba65190702010-04-02 23:37:26 -0700801 if (mainView == null) {
Cary Clarka0464552009-09-29 13:00:45 -0400802 return;
803 }
Leon Scroggins79e36d92010-04-29 16:01:46 +0100804 // Do not need to check for null, since the current tab will have
805 // at least a main WebView, or we would have returned above.
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -0400806 if (isInCustomActionMode()) {
807 // Do not show the fake title bar, while a custom ActionMode
808 // (i.e. find or select) is showing.
Leon Scroggins79e36d92010-04-29 16:01:46 +0100809 return;
810 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700811 if (mXLargeScreenSize) {
812 mContentView.addView(mFakeTitleBar);
813 mTabBar.onShowTitleBar();
814 } else {
815 WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400816
Michael Kolba2b2ba82010-08-04 17:54:03 -0700817 // Add the title bar to the window manager so it can receive
818 // touches
819 // while the menu is up
820 WindowManager.LayoutParams params =
821 new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
822 ViewGroup.LayoutParams.WRAP_CONTENT,
823 WindowManager.LayoutParams.TYPE_APPLICATION,
824 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
825 PixelFormat.TRANSLUCENT);
826 params.gravity = Gravity.TOP;
827 boolean atTop = mainView.getScrollY() == 0;
828 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
829 manager.addView(mFakeTitleBar, params);
830 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400831 }
832 }
833
834 @Override
835 public void onOptionsMenuClosed(Menu menu) {
836 mOptionsMenuOpen = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -0400837 if (!mInLoad) {
838 hideFakeTitleBar();
839 } else if (!mIconView) {
840 // The page is currently loading, and we are in expanded mode, so
841 // we were not showing the menu. Show it once again. It will be
842 // removed when the page finishes.
843 showFakeTitleBar();
844 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400845 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700846
Michael Kolba2b2ba82010-08-04 17:54:03 -0700847 void stopScrolling() {
848 ((ScrollWebView) mTabControl.getCurrentWebView()).stopScroll();
849 }
850
851 void hideFakeTitleBar() {
852 if (!isFakeTitleBarShowing()) return;
853 if (mXLargeScreenSize) {
854 mContentView.removeView(mFakeTitleBar);
855 mTabBar.onHideTitleBar();
856 } else {
857 WindowManager.LayoutParams params =
858 (WindowManager.LayoutParams) mFakeTitleBar.getLayoutParams();
859 WebView mainView = mTabControl.getCurrentWebView();
860 // Although we decided whether or not to animate based on the
861 // current
862 // scroll position, the scroll position may have changed since the
863 // fake title bar was displayed. Make sure it has the appropriate
864 // animation/lack thereof before removing.
865 params.windowAnimations =
866 mainView != null && mainView.getScrollY() == 0 ? 0 : R.style.TitleBar;
867 WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
868 manager.updateViewLayout(mFakeTitleBar, params);
869 manager.removeView(mFakeTitleBar);
870 }
871 }
872
873 boolean isFakeTitleBarShowing() {
874 return (mFakeTitleBar.getParent() != null);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400875 }
876
The Android Open Source Project0c908882009-03-03 19:32:16 -0800877 /**
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400878 * Special method for the fake title bar to call when displaying its context
879 * menu, since it is in its own Window, and its parent does not show a
880 * context menu.
881 */
882 /* package */ void showTitleBarContextMenu() {
Cary Clark65f4a3c2009-09-28 17:05:06 -0400883 if (null == mTitleBar.getParent()) {
884 return;
885 }
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400886 openContextMenu(mTitleBar);
887 }
888
Leon Scrogginsb2b19f52009-10-09 16:10:00 -0400889 @Override
890 public void onContextMenuClosed(Menu menu) {
891 super.onContextMenuClosed(menu);
892 if (mInLoad) {
893 showFakeTitleBar();
894 }
895 }
896
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400897 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800898 * onSaveInstanceState(Bundle map)
899 * onSaveInstanceState is called right before onStop(). The map contains
900 * the saved state.
901 */
Grace Kloba22ac16e2009-10-07 18:00:23 -0700902 @Override
903 protected void onSaveInstanceState(Bundle outState) {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700904 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800905 Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
906 }
907 // the default implementation requires each view to have an id. As the
908 // browser handles the state itself and it doesn't use id for the views,
909 // don't call the default implementation. Otherwise it will trigger the
910 // warning like this, "couldn't save which view has focus because the
911 // focused view XXX has no id".
912
913 // Save all the tabs
914 mTabControl.saveState(outState);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700915
916 // Save time so that we know how old incognito tabs (if any) are.
917 outState.putSerializable("lastActiveDate", Calendar.getInstance());
The Android Open Source Project0c908882009-03-03 19:32:16 -0800918 }
919
Grace Kloba22ac16e2009-10-07 18:00:23 -0700920 @Override
921 protected void onPause() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800922 super.onPause();
923
924 if (mActivityInPause) {
925 Log.e(LOGTAG, "BrowserActivity is already paused.");
926 return;
927 }
928
Mike Reed7bfa63b2009-05-28 11:08:32 -0400929 mTabControl.pauseCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800930 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -0400931 if (mTabControl.getCurrentIndex() >= 0 && !pauseWebViewTimers()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800932 mWakeLock.acquire();
933 mHandler.sendMessageDelayed(mHandler
934 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
935 }
936
Leon Scrogginsa2ab6a72009-09-11 11:49:52 -0400937 // FIXME: This removes the active tabs page and resets the menu to
938 // MAIN_MENU. A better solution might be to do this work in onNewIntent
939 // but then we would need to save it in onSaveInstanceState and restore
940 // it in onCreate/onRestoreInstanceState
941 if (mActiveTabsPage != null) {
942 removeActiveTabPage(true);
943 }
944
The Android Open Source Project0c908882009-03-03 19:32:16 -0800945 cancelStopToast();
946
947 // unregister network state listener
948 unregisterReceiver(mNetworkStateIntentReceiver);
949 WebView.disablePlatformNotifications();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800950 }
951
Grace Kloba22ac16e2009-10-07 18:00:23 -0700952 @Override
953 protected void onDestroy() {
Dave Bort31a6d1c2009-04-13 15:56:49 -0700954 if (LOGV_ENABLED) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800955 Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
956 }
957 super.onDestroy();
Grace Kloba0923d692009-09-23 21:37:25 -0700958
Leon Scroggins8d5fa432009-10-02 15:55:59 -0400959 if (mUploadMessage != null) {
960 mUploadMessage.onReceiveValue(null);
961 mUploadMessage = null;
962 }
963
Grace Kloba0923d692009-09-23 21:37:25 -0700964 if (mTabControl == null) return;
965
Grace Kloba1fc98a32009-10-21 13:23:08 -0700966 // Remove the fake title bar if it is there
967 hideFakeTitleBar();
968
The Android Open Source Project0c908882009-03-03 19:32:16 -0800969 // Remove the current tab and sub window
Grace Kloba22ac16e2009-10-07 18:00:23 -0700970 Tab t = mTabControl.getCurrentTab();
Patrick Scottfb5e77f2009-04-08 19:17:37 -0700971 if (t != null) {
972 dismissSubWindow(t);
973 removeTabFromContentView(t);
974 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800975 // Destroy all the tabs
976 mTabControl.destroy();
977 WebIconDatabase.getInstance().close();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800978
Bjorn Bringerta7611812010-03-24 11:12:02 +0000979 // Stop watching the default geolocation permissions
980 mSystemAllowGeolocationOrigins.stop();
981 mSystemAllowGeolocationOrigins = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800982 }
983
984 @Override
985 public void onConfigurationChanged(Configuration newConfig) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400986 mConfigChanged = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800987 super.onConfigurationChanged(newConfig);
988
989 if (mPageInfoDialog != null) {
990 mPageInfoDialog.dismiss();
991 showPageInfo(
992 mPageInfoView,
Leon Scrogginsc7b92f82010-01-11 18:17:31 -0500993 mPageInfoFromShowSSLCertificateOnError);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800994 }
995 if (mSSLCertificateDialog != null) {
996 mSSLCertificateDialog.dismiss();
997 showSSLCertificate(
998 mSSLCertificateView);
999 }
1000 if (mSSLCertificateOnErrorDialog != null) {
1001 mSSLCertificateOnErrorDialog.dismiss();
1002 showSSLCertificateOnError(
1003 mSSLCertificateOnErrorView,
1004 mSSLCertificateOnErrorHandler,
1005 mSSLCertificateOnErrorError);
1006 }
1007 if (mHttpAuthenticationDialog != null) {
Bjorn Bringert25738922010-10-12 10:56:20 +01001008 mHttpAuthenticationDialog.reshow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001009 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001010 }
1011
Grace Kloba22ac16e2009-10-07 18:00:23 -07001012 @Override
1013 public void onLowMemory() {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001014 super.onLowMemory();
1015 mTabControl.freeMemory();
1016 }
1017
Cary Clarkff4d92c2010-03-25 11:17:03 -04001018 private void resumeWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001019 Tab tab = mTabControl.getCurrentTab();
Cary Clarkff4d92c2010-03-25 11:17:03 -04001020 if (tab == null) return; // monkey can trigger this
Grace Kloba22ac16e2009-10-07 18:00:23 -07001021 boolean inLoad = tab.inLoad();
1022 if ((!mActivityInPause && !inLoad) || (mActivityInPause && inLoad)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001023 CookieSyncManager.getInstance().startSync();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001024 WebView w = tab.getWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001025 if (w != null) {
1026 w.resumeTimers();
1027 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001028 }
1029 }
1030
Mike Reed7bfa63b2009-05-28 11:08:32 -04001031 private boolean pauseWebViewTimers() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001032 Tab tab = mTabControl.getCurrentTab();
1033 boolean inLoad = tab.inLoad();
1034 if (mActivityInPause && !inLoad) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001035 CookieSyncManager.getInstance().stopSync();
1036 WebView w = mTabControl.getCurrentWebView();
1037 if (w != null) {
1038 w.pauseTimers();
1039 }
1040 return true;
1041 } else {
1042 return false;
1043 }
1044 }
1045
The Android Open Source Project0c908882009-03-03 19:32:16 -08001046 // Open the icon database and retain all the icons for visited sites.
1047 private void retainIconsOnStartup() {
1048 final WebIconDatabase db = WebIconDatabase.getInstance();
1049 db.open(getDir("icons", 0).getPath());
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001050 Cursor c = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001051 try {
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001052 c = Browser.getAllBookmarks(mResolver);
1053 if (c.moveToFirst()) {
1054 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
1055 do {
1056 String url = c.getString(urlIndex);
1057 db.retainIconForPageUrl(url);
1058 } while (c.moveToNext());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001059 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001060 } catch (IllegalStateException e) {
1061 Log.e(LOGTAG, "retainIconsOnStartup", e);
Leon Scroggins2c0f6112010-03-12 18:09:39 -05001062 } finally {
1063 if (c!= null) c.close();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001064 }
1065 }
1066
1067 // Helper method for getting the top window.
1068 WebView getTopWindow() {
1069 return mTabControl.getCurrentTopWebView();
1070 }
1071
Grace Kloba22ac16e2009-10-07 18:00:23 -07001072 TabControl getTabControl() {
1073 return mTabControl;
1074 }
1075
The Android Open Source Project0c908882009-03-03 19:32:16 -08001076 @Override
1077 public boolean onCreateOptionsMenu(Menu menu) {
1078 super.onCreateOptionsMenu(menu);
1079
1080 MenuInflater inflater = getMenuInflater();
1081 inflater.inflate(R.menu.browser, menu);
1082 mMenu = menu;
1083 updateInLoadMenuItems();
1084 return true;
1085 }
1086
1087 /**
1088 * As the menu can be open when loading state changes
1089 * we must manually update the state of the stop/reload menu
1090 * item
1091 */
1092 private void updateInLoadMenuItems() {
1093 if (mMenu == null) {
1094 return;
1095 }
Michael Kolbe0a36662010-06-29 10:37:12 -07001096 MenuItem dest = mMenu.findItem(R.id.stop_reload_menu_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001097 MenuItem src = mInLoad ?
1098 mMenu.findItem(R.id.stop_menu_id):
Michael Kolbe0a36662010-06-29 10:37:12 -07001099 mMenu.findItem(R.id.reload_menu_id);
1100 if (src != null) {
1101 dest.setIcon(src.getIcon());
1102 dest.setTitle(src.getTitle());
1103 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001104 }
1105
1106 @Override
1107 public boolean onContextItemSelected(MenuItem item) {
1108 // chording is not an issue with context menus, but we use the same
1109 // options selector, so set mCanChord to true so we can access them.
1110 mCanChord = true;
1111 int id = item.getItemId();
Leon Scroggins96afcb12009-12-10 12:35:56 -05001112 boolean result = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001113 switch (id) {
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001114 // For the context menu from the title bar
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001115 case R.id.title_bar_copy_page_url:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001116 Tab currentTab = mTabControl.getCurrentTab();
1117 if (null == currentTab) {
1118 result = false;
1119 break;
1120 }
1121 WebView mainView = currentTab.getWebView();
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001122 if (null == mainView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001123 result = false;
1124 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001125 }
Leon Scroggins96afcb12009-12-10 12:35:56 -05001126 copy(mainView.getUrl());
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001127 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001128 // -- Browser context menu
1129 case R.id.open_context_menu_id:
The Android Open Source Project0c908882009-03-03 19:32:16 -08001130 case R.id.bookmark_context_menu_id:
1131 case R.id.save_link_context_menu_id:
1132 case R.id.share_link_context_menu_id:
1133 case R.id.copy_link_context_menu_id:
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001134 final WebView webView = getTopWindow();
1135 if (null == webView) {
Leon Scroggins96afcb12009-12-10 12:35:56 -05001136 result = false;
1137 break;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -04001138 }
1139 final HashMap hrefMap = new HashMap();
1140 hrefMap.put("webview", webView);
1141 final Message msg = mHandler.obtainMessage(
1142 FOCUS_NODE_HREF, id, 0, hrefMap);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001143 webView.requestFocusNodeHref(msg);
1144 break;
1145
1146 default:
1147 // For other context menus
Leon Scroggins96afcb12009-12-10 12:35:56 -05001148 result = onOptionsItemSelected(item);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001149 }
1150 mCanChord = false;
Leon Scroggins96afcb12009-12-10 12:35:56 -05001151 return result;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001152 }
1153
1154 private Bundle createGoogleSearchSourceBundle(String source) {
1155 Bundle bundle = new Bundle();
Bjorn Bringert10d1cca2010-02-10 14:22:12 +00001156 bundle.putString(Search.SOURCE, source);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001157 return bundle;
1158 }
1159
Leon Scroggins8ad29922010-02-16 12:33:55 -05001160 /* package */ void editUrl() {
Leon Scroggins68579392009-09-15 15:31:54 -04001161 if (mOptionsMenuOpen) closeOptionsMenu();
Leon Scroggins5bbe9802009-07-31 13:10:55 -04001162 String url = (getTopWindow() == null) ? null : getTopWindow().getUrl();
Grace Kloba83f47342009-07-20 10:44:31 -07001163 startSearch(mSettings.getHomePage().equals(url) ? null : url, true,
Leon Scroggins8ad29922010-02-16 12:33:55 -05001164 null, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001165 }
1166
Leon Scroggins8ad29922010-02-16 12:33:55 -05001167 /**
1168 * Overriding this to insert a local information bundle
1169 */
The Android Open Source Project0c908882009-03-03 19:32:16 -08001170 @Override
1171 public void startSearch(String initialQuery, boolean selectInitialQuery,
1172 Bundle appSearchData, boolean globalSearch) {
1173 if (appSearchData == null) {
1174 appSearchData = createGoogleSearchSourceBundle(GOOGLE_SEARCH_SOURCE_TYPE);
1175 }
Leon Scroggins III430057d2010-09-14 10:57:37 -04001176
1177 SearchEngine searchEngine = mSettings.getSearchEngine();
1178 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
1179 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
1180 }
1181
The Android Open Source Project0c908882009-03-03 19:32:16 -08001182 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1183 }
1184
Leon Scroggins1f005d32009-08-10 17:36:42 -04001185 /**
1186 * Switch tabs. Called by the TitleBarSet when sliding the title bar
1187 * results in changing tabs.
Leon Scroggins160a7e72009-08-14 18:28:01 -04001188 * @param index Index of the tab to change to, as defined by
1189 * mTabControl.getTabIndex(Tab t).
1190 * @return boolean True if we successfully switched to a different tab. If
1191 * the indexth tab is null, or if that tab is the same as
1192 * the current one, return false.
Leon Scroggins1f005d32009-08-10 17:36:42 -04001193 */
Leon Scroggins160a7e72009-08-14 18:28:01 -04001194 /* package */ boolean switchToTab(int index) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001195 Tab tab = mTabControl.getTab(index);
1196 Tab currentTab = mTabControl.getCurrentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001197 if (tab == null || tab == currentTab) {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001198 return false;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001199 }
1200 if (currentTab != null) {
1201 // currentTab may be null if it was just removed. In that case,
1202 // we do not need to remove it
1203 removeTabFromContentView(currentTab);
1204 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001205 mTabControl.setCurrentTab(tab);
1206 attachTabToContentView(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001207 resetTitleIconAndProgress();
1208 updateLockIconToLatest();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001209 return true;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001210 }
1211
Grace Kloba22ac16e2009-10-07 18:00:23 -07001212 /* package */ Tab openTabToHomePage() {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001213 return openTabAndShow(mSettings.getHomePage(), false, null);
1214 }
1215
Leon Scroggins1f005d32009-08-10 17:36:42 -04001216 /* package */ void closeCurrentWindow() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001217 final Tab current = mTabControl.getCurrentTab();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001218 if (mTabControl.getTabCount() == 1) {
Leon Scroggins30444232009-09-04 18:36:20 -04001219 // This is the last tab. Open a new one, with the home
1220 // page and close the current one.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001221 openTabToHomePage();
Leon Scroggins160a7e72009-08-14 18:28:01 -04001222 closeTab(current);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001223 return;
1224 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001225 final Tab parent = current.getParentTab();
Leon Scroggins1f005d32009-08-10 17:36:42 -04001226 int indexToShow = -1;
1227 if (parent != null) {
1228 indexToShow = mTabControl.getTabIndex(parent);
1229 } else {
Leon Scroggins160a7e72009-08-14 18:28:01 -04001230 final int currentIndex = mTabControl.getCurrentIndex();
1231 // Try to move to the tab to the right
1232 indexToShow = currentIndex + 1;
1233 if (indexToShow > mTabControl.getTabCount() - 1) {
1234 // Try to move to the tab to the left
1235 indexToShow = currentIndex - 1;
Leon Scroggins1f005d32009-08-10 17:36:42 -04001236 }
1237 }
Leon Scroggins160a7e72009-08-14 18:28:01 -04001238 if (switchToTab(indexToShow)) {
1239 // Close window
1240 closeTab(current);
1241 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001242 }
1243
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001244 private ActiveTabsPage mActiveTabsPage;
1245
1246 /**
1247 * Remove the active tabs page.
1248 * @param needToAttach If true, the active tabs page did not attach a tab
1249 * to the content view, so we need to do that here.
1250 */
1251 /* package */ void removeActiveTabPage(boolean needToAttach) {
1252 mContentView.removeView(mActiveTabsPage);
Leon Scrogginsd746a942010-05-19 13:21:44 -04001253 mTitleBar.setVisibility(View.VISIBLE);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001254 mActiveTabsPage = null;
1255 mMenuState = R.id.MAIN_MENU;
1256 if (needToAttach) {
1257 attachTabToContentView(mTabControl.getCurrentTab());
1258 }
1259 getTopWindow().requestFocus();
1260 }
1261
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -04001262 @Override
Adam Powelld7973052010-11-15 21:10:48 -08001263 public void onActionModeStarted(ActionMode mode) {
1264 super.onActionModeStarted(mode);
Adam Powell6c6e86f2010-11-16 11:06:24 -08001265 mActionMode = mode;
Leon Scroggins4fc12cb2010-08-24 14:24:05 -04001266 hideFakeTitleBar();
1267 // Would like to change the MENU, but onEndActionMode may not be called
Adam Powelld7973052010-11-15 21:10:48 -08001268 // TODO onActionModeFinished will notify when an action mode ends
Cary Clark01cfcdd2010-06-04 16:36:45 -04001269 }
1270
The Android Open Source Project0c908882009-03-03 19:32:16 -08001271 @Override
1272 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb370a4f32010-10-06 10:45:32 -07001273 if (item.getGroupId() != R.id.CONTEXT_MENU) {
1274 // menu remains active, so ensure comboview is dismissed
1275 // if main menu option is selected
1276 removeComboView();
1277 }
Michael Kolbed217742010-08-10 17:52:34 -07001278 // check the action bar button before mCanChord check, as the prepare call
1279 // doesn't come for action bar buttons
1280 if (item.getItemId() == R.id.newtab) {
Michael Kolb300b7f02010-08-25 13:47:24 -07001281 openTabToHomePage();
Michael Kolbed217742010-08-10 17:52:34 -07001282 return true;
1283 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001284 if (!mCanChord) {
1285 // The user has already fired a shortcut with this hold down of the
1286 // menu key.
1287 return false;
1288 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001289 if (null == getTopWindow()) {
Leon Scroggins0d7ae0e2009-06-05 11:04:45 -04001290 return false;
1291 }
Grace Kloba6ee9c492009-07-13 10:04:34 -07001292 if (mMenuIsDown) {
1293 // The shortcut action consumes the MENU. Even if it is still down,
1294 // it won't trigger the next shortcut action. In the case of the
1295 // shortcut action triggering a new activity, like Bookmarks, we
1296 // won't get onKeyUp for MENU. So it is important to reset it here.
1297 mMenuIsDown = false;
1298 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001299 switch (item.getItemId()) {
1300 // -- Main menu
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001301 case R.id.new_tab_menu_id:
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001302 openTabToHomePage();
Leon Scrogginsa81a7642009-08-31 17:05:41 -04001303 break;
1304
Michael Kolbae62fd42010-08-18 16:33:28 -07001305 case R.id.incognito_menu_id:
1306 openIncognitoTab();
1307 break;
1308
Leon Scroggins64b80f32009-08-07 12:03:34 -04001309 case R.id.goto_menu_id:
Leon Scroggins8ad29922010-02-16 12:33:55 -05001310 editUrl();
Leon Scrogginsb3a5bed2009-09-28 11:21:56 -04001311 break;
1312
1313 case R.id.bookmarks_menu_id:
Michael Kolbe421c242010-10-04 19:29:01 -07001314 bookmarksOrHistoryPicker(false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001315 break;
1316
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001317 case R.id.active_tabs_menu_id:
1318 mActiveTabsPage = new ActiveTabsPage(this, mTabControl);
1319 removeTabFromContentView(mTabControl.getCurrentTab());
Leon Scrogginsd746a942010-05-19 13:21:44 -04001320 mTitleBar.setVisibility(View.GONE);
Leon Scroggins43de6162009-09-14 19:59:58 -04001321 hideFakeTitleBar();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001322 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
1323 mActiveTabsPage.requestFocus();
1324 mMenuState = EMPTY_MENU;
1325 break;
1326
Leon Scroggins1f005d32009-08-10 17:36:42 -04001327 case R.id.add_bookmark_menu_id:
Michael Kolb370a4f32010-10-06 10:45:32 -07001328 bookmarkCurrentPage(AddBookmarkPage.DEFAULT_FOLDER_ID);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001329 break;
1330
1331 case R.id.stop_reload_menu_id:
1332 if (mInLoad) {
1333 stopLoading();
1334 } else {
1335 getTopWindow().reload();
1336 }
1337 break;
1338
1339 case R.id.back_menu_id:
1340 getTopWindow().goBack();
1341 break;
1342
1343 case R.id.forward_menu_id:
1344 getTopWindow().goForward();
1345 break;
1346
1347 case R.id.close_menu_id:
1348 // Close the subwindow if it exists.
1349 if (mTabControl.getCurrentSubWindow() != null) {
1350 dismissSubWindow(mTabControl.getCurrentTab());
1351 break;
1352 }
Leon Scroggins1f005d32009-08-10 17:36:42 -04001353 closeCurrentWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001354 break;
1355
1356 case R.id.homepage_menu_id:
Grace Kloba22ac16e2009-10-07 18:00:23 -07001357 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001358 if (current != null) {
1359 dismissSubWindow(current);
Leon Scroggins92472e82010-02-17 16:32:28 -05001360 loadUrl(current.getWebView(), mSettings.getHomePage());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001361 }
1362 break;
1363
1364 case R.id.preferences_menu_id:
1365 Intent intent = new Intent(this,
1366 BrowserPreferencesPage.class);
Leon Scrogginsd5304942009-12-10 16:11:39 -05001367 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1368 getTopWindow().getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001369 startActivityForResult(intent, PREFERENCES_PAGE);
1370 break;
1371
1372 case R.id.find_menu_id:
Leon Scroggins4fc12cb2010-08-24 14:24:05 -04001373 getTopWindow().showFindDialog(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001374 break;
1375
The Android Open Source Project0c908882009-03-03 19:32:16 -08001376 case R.id.page_info_menu_id:
1377 showPageInfo(mTabControl.getCurrentTab(), false);
1378 break;
1379
1380 case R.id.classic_history_menu_id:
Michael Kolbe421c242010-10-04 19:29:01 -07001381 bookmarksOrHistoryPicker(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001382 break;
1383
Leon Scroggins96afcb12009-12-10 12:35:56 -05001384 case R.id.title_bar_share_page_url:
The Android Open Source Project0c908882009-03-03 19:32:16 -08001385 case R.id.share_page_menu_id:
Leon Scroggins96afcb12009-12-10 12:35:56 -05001386 Tab currentTab = mTabControl.getCurrentTab();
1387 if (null == currentTab) {
1388 mCanChord = false;
1389 return false;
1390 }
1391 currentTab.populatePickerData();
1392 sharePage(this, currentTab.getTitle(),
1393 currentTab.getUrl(), currentTab.getFavicon(),
Ben Murdoch87cc65d2010-06-29 20:34:10 +01001394 createScreenshot(currentTab.getWebView(), getDesiredThumbnailWidth(this),
1395 getDesiredThumbnailHeight(this)));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001396 break;
1397
1398 case R.id.dump_nav_menu_id:
1399 getTopWindow().debugDump();
1400 break;
1401
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001402 case R.id.dump_counters_menu_id:
1403 getTopWindow().dumpV8Counters();
1404 break;
1405
The Android Open Source Project0c908882009-03-03 19:32:16 -08001406 case R.id.zoom_in_menu_id:
1407 getTopWindow().zoomIn();
1408 break;
1409
1410 case R.id.zoom_out_menu_id:
1411 getTopWindow().zoomOut();
1412 break;
1413
1414 case R.id.view_downloads_menu_id:
Leon Scroggins IIIa682a3c2010-09-27 12:32:40 -04001415 viewDownloads();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001416 break;
1417
The Android Open Source Project0c908882009-03-03 19:32:16 -08001418 case R.id.window_one_menu_id:
1419 case R.id.window_two_menu_id:
1420 case R.id.window_three_menu_id:
1421 case R.id.window_four_menu_id:
1422 case R.id.window_five_menu_id:
1423 case R.id.window_six_menu_id:
1424 case R.id.window_seven_menu_id:
1425 case R.id.window_eight_menu_id:
1426 {
1427 int menuid = item.getItemId();
1428 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1429 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001430 Tab desiredTab = mTabControl.getTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001431 if (desiredTab != null &&
1432 desiredTab != mTabControl.getCurrentTab()) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001433 switchToTab(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001434 }
1435 break;
1436 }
1437 }
1438 }
1439 break;
1440
1441 default:
1442 if (!super.onOptionsItemSelected(item)) {
1443 return false;
1444 }
1445 // Otherwise fall through.
1446 }
1447 mCanChord = false;
1448 return true;
1449 }
1450
Michael Kolb370a4f32010-10-06 10:45:32 -07001451 /**
1452 * add the current page as a bookmark to the given folder id
1453 * @param folderId use -1 for the default folder
1454 */
1455 /* package */ void bookmarkCurrentPage(long folderId) {
Leon Scroggins571b3762010-05-26 10:25:01 -04001456 Intent i = new Intent(BrowserActivity.this,
1457 AddBookmarkPage.class);
1458 WebView w = getTopWindow();
Leon Scrogginsbc922852010-10-22 12:15:27 -04001459 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1460 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
Leon Scroggins88d08032010-10-21 15:17:10 -04001461 String touchIconUrl = w.getTouchIconUrl();
1462 if (touchIconUrl != null) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001463 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -04001464 WebSettings settings = w.getSettings();
1465 if (settings != null) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04001466 i.putExtra(AddBookmarkPage.USER_AGENT,
1467 settings.getUserAgentString());
Leon Scroggins88d08032010-10-21 15:17:10 -04001468 }
1469 }
Leon Scrogginsbc922852010-10-22 12:15:27 -04001470 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1471 createScreenshot(w, getDesiredThumbnailWidth(this),
Ben Murdoch87cc65d2010-06-29 20:34:10 +01001472 getDesiredThumbnailHeight(this)));
Leon Scrogginsbc922852010-10-22 12:15:27 -04001473 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
Michael Kolb370a4f32010-10-06 10:45:32 -07001474 i.putExtra(BrowserContract.Bookmarks.PARENT,
1475 folderId);
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -04001476 // Put the dialog at the upper right of the screen, covering the
1477 // star on the title bar.
1478 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
Leon Scroggins571b3762010-05-26 10:25:01 -04001479 startActivity(i);
1480 }
1481
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -04001482 /*
1483 * True if a custom ActionMode (i.e. find or select) is in use.
1484 */
1485 private boolean isInCustomActionMode() {
1486 return mActionMode != null;
Cary Clark01cfcdd2010-06-04 16:36:45 -04001487 }
1488
1489 /*
Leon Scroggins4fc12cb2010-08-24 14:24:05 -04001490 * End the current ActionMode.
Cary Clark01cfcdd2010-06-04 16:36:45 -04001491 */
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -04001492 void endActionMode() {
1493 if (mActionMode != null) {
Leon Scroggins4fc12cb2010-08-24 14:24:05 -04001494 ActionMode mode = mActionMode;
1495 onEndActionMode();
1496 mode.finish();
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -04001497 }
1498 }
1499
1500 /*
1501 * Called by find and select when they are finished. Replace title bars
1502 * as necessary.
1503 */
1504 public void onEndActionMode() {
1505 if (!isInCustomActionMode()) return;
Leon Scroggins79e36d92010-04-29 16:01:46 +01001506 if (mInLoad) {
1507 // The title bar was hidden, because otherwise it would cover up the
Michael Kolba2b2ba82010-08-04 17:54:03 -07001508 // find or select dialog. Now that the dialog has been removed,
Cary Clark01cfcdd2010-06-04 16:36:45 -04001509 // show the fake title bar once again.
Leon Scroggins79e36d92010-04-29 16:01:46 +01001510 showFakeTitleBar();
1511 }
Leon Scroggins4fc12cb2010-08-24 14:24:05 -04001512 // Would like to return the menu state to normal, but this does not
1513 // necessarily get called.
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -04001514 mActionMode = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001515 }
1516
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -04001517 // For select and find, we keep track of the ActionMode so that
1518 // finish() can be called as desired.
1519 private ActionMode mActionMode;
1520
Grace Kloba22ac16e2009-10-07 18:00:23 -07001521 @Override
1522 public boolean onPrepareOptionsMenu(Menu menu) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001523 // This happens when the user begins to hold down the menu key, so
1524 // allow them to chord to get a shortcut.
1525 mCanChord = true;
1526 // Note: setVisible will decide whether an item is visible; while
1527 // setEnabled() will decide whether an item is enabled, which also means
1528 // whether the matching shortcut key will function.
1529 super.onPrepareOptionsMenu(menu);
1530 switch (mMenuState) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001531 case EMPTY_MENU:
1532 if (mCurrentMenuState != mMenuState) {
1533 menu.setGroupVisible(R.id.MAIN_MENU, false);
1534 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1535 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001536 }
1537 break;
1538 default:
1539 if (mCurrentMenuState != mMenuState) {
1540 menu.setGroupVisible(R.id.MAIN_MENU, true);
1541 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1542 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001543 }
1544 final WebView w = getTopWindow();
1545 boolean canGoBack = false;
1546 boolean canGoForward = false;
1547 boolean isHome = false;
1548 if (w != null) {
1549 canGoBack = w.canGoBack();
1550 canGoForward = w.canGoForward();
1551 isHome = mSettings.getHomePage().equals(w.getUrl());
1552 }
1553 final MenuItem back = menu.findItem(R.id.back_menu_id);
1554 back.setEnabled(canGoBack);
1555
1556 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1557 home.setEnabled(!isHome);
1558
Michael Kolbe0a36662010-06-29 10:37:12 -07001559 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1560 forward.setEnabled(canGoForward);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001561
Michael Kolbed217742010-08-10 17:52:34 -07001562 if (!mXLargeScreenSize) {
1563 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
1564 newtab.setEnabled(mTabControl.canCreateNewTab());
1565 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001566 // decide whether to show the share link option
1567 PackageManager pm = getPackageManager();
1568 Intent send = new Intent(Intent.ACTION_SEND);
1569 send.setType("text/plain");
1570 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1571 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1572
The Android Open Source Project0c908882009-03-03 19:32:16 -08001573 boolean isNavDump = mSettings.isNavDump();
1574 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1575 nav.setVisible(isNavDump);
1576 nav.setEnabled(isNavDump);
Andrei Popescu7a8b88b2010-02-02 00:30:38 +00001577
1578 boolean showDebugSettings = mSettings.showDebugSettings();
1579 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1580 counter.setVisible(showDebugSettings);
1581 counter.setEnabled(showDebugSettings);
1582
The Android Open Source Project0c908882009-03-03 19:32:16 -08001583 break;
1584 }
1585 mCurrentMenuState = mMenuState;
1586 return true;
1587 }
1588
1589 @Override
1590 public void onCreateContextMenu(ContextMenu menu, View v,
1591 ContextMenuInfo menuInfo) {
Leon Scroggins571b3762010-05-26 10:25:01 -04001592 if (v instanceof TitleBarBase) {
Leon Scroggins4e9f89b2010-02-22 16:54:14 -05001593 return;
1594 }
Michael Kolb772927a2010-10-13 19:23:42 -07001595 if (!(v instanceof WebView)) {
1596 return;
1597 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001598 WebView webview = (WebView) v;
1599 WebView.HitTestResult result = webview.getHitTestResult();
1600 if (result == null) {
1601 return;
1602 }
1603
1604 int type = result.getType();
1605 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1606 Log.w(LOGTAG,
1607 "We should not show context menu when nothing is touched");
1608 return;
1609 }
1610 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1611 // let TextView handles context menu
1612 return;
1613 }
1614
1615 // Note, http://b/issue?id=1106666 is requesting that
1616 // an inflated menu can be used again. This is not available
1617 // yet, so inflate each time (yuk!)
1618 MenuInflater inflater = getMenuInflater();
1619 inflater.inflate(R.menu.browsercontext, menu);
1620
1621 // Show the correct menu group
Leon Scroggins III9e997c72010-05-26 13:25:16 -04001622 final String extra = result.getExtra();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001623 menu.setGroupVisible(R.id.PHONE_MENU,
1624 type == WebView.HitTestResult.PHONE_TYPE);
1625 menu.setGroupVisible(R.id.EMAIL_MENU,
1626 type == WebView.HitTestResult.EMAIL_TYPE);
1627 menu.setGroupVisible(R.id.GEO_MENU,
1628 type == WebView.HitTestResult.GEO_TYPE);
1629 menu.setGroupVisible(R.id.IMAGE_MENU,
1630 type == WebView.HitTestResult.IMAGE_TYPE
1631 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1632 menu.setGroupVisible(R.id.ANCHOR_MENU,
1633 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1634 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1635
1636 // Setup custom handling depending on the type
1637 switch (type) {
1638 case WebView.HitTestResult.PHONE_TYPE:
1639 menu.setHeaderTitle(Uri.decode(extra));
1640 menu.findItem(R.id.dial_context_menu_id).setIntent(
1641 new Intent(Intent.ACTION_VIEW, Uri
1642 .parse(WebView.SCHEME_TEL + extra)));
1643 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1644 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
Cary Clark5e335a32009-09-22 14:53:11 -04001645 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001646 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1647 addIntent);
1648 menu.findItem(R.id.copy_phone_context_menu_id).setOnMenuItemClickListener(
1649 new Copy(extra));
1650 break;
1651
1652 case WebView.HitTestResult.EMAIL_TYPE:
1653 menu.setHeaderTitle(extra);
1654 menu.findItem(R.id.email_context_menu_id).setIntent(
1655 new Intent(Intent.ACTION_VIEW, Uri
1656 .parse(WebView.SCHEME_MAILTO + extra)));
1657 menu.findItem(R.id.copy_mail_context_menu_id).setOnMenuItemClickListener(
1658 new Copy(extra));
1659 break;
1660
1661 case WebView.HitTestResult.GEO_TYPE:
1662 menu.setHeaderTitle(extra);
1663 menu.findItem(R.id.map_context_menu_id).setIntent(
1664 new Intent(Intent.ACTION_VIEW, Uri
1665 .parse(WebView.SCHEME_GEO
1666 + URLEncoder.encode(extra))));
1667 menu.findItem(R.id.copy_geo_context_menu_id).setOnMenuItemClickListener(
1668 new Copy(extra));
1669 break;
1670
1671 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1672 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
1673 TextView titleView = (TextView) LayoutInflater.from(this)
1674 .inflate(android.R.layout.browser_link_context_header,
1675 null);
1676 titleView.setText(extra);
1677 menu.setHeaderView(titleView);
1678 // decide whether to show the open link in new tab option
Leon Scroggins III9e997c72010-05-26 13:25:16 -04001679 boolean showNewTab = mTabControl.canCreateNewTab();
1680 MenuItem newTabItem
1681 = menu.findItem(R.id.open_newtab_context_menu_id);
1682 newTabItem.setVisible(showNewTab);
1683 if (showNewTab) {
1684 newTabItem.setOnMenuItemClickListener(
1685 new MenuItem.OnMenuItemClickListener() {
1686 public boolean onMenuItemClick(MenuItem item) {
1687 final Tab parent = mTabControl.getCurrentTab();
Michael Kolb68792c82010-08-09 16:39:18 -07001688 final Tab newTab = openTab(extra, false);
Leon Scroggins III9e997c72010-05-26 13:25:16 -04001689 if (newTab != parent) {
1690 parent.addChildTab(newTab);
1691 }
1692 return true;
1693 }
1694 });
1695 }
Ben Murdochde353622009-10-12 10:29:00 +01001696 menu.findItem(R.id.bookmark_context_menu_id).setVisible(
1697 Bookmarks.urlHasAcceptableScheme(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001698 PackageManager pm = getPackageManager();
1699 Intent send = new Intent(Intent.ACTION_SEND);
1700 send.setType("text/plain");
1701 ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
1702 menu.findItem(R.id.share_link_context_menu_id).setVisible(ri != null);
1703 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1704 break;
1705 }
1706 // otherwise fall through to handle image part
1707 case WebView.HitTestResult.IMAGE_TYPE:
1708 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1709 menu.setHeaderTitle(extra);
1710 }
1711 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1712 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1713 menu.findItem(R.id.download_context_menu_id).
1714 setOnMenuItemClickListener(new Download(extra));
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001715 menu.findItem(R.id.set_wallpaper_context_menu_id).
1716 setOnMenuItemClickListener(new SetAsWallpaper(extra));
The Android Open Source Project0c908882009-03-03 19:32:16 -08001717 break;
1718
1719 default:
1720 Log.w(LOGTAG, "We should not get here.");
1721 break;
1722 }
Leon Scrogginsb2b19f52009-10-09 16:10:00 -04001723 hideFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001724 }
1725
The Android Open Source Project0c908882009-03-03 19:32:16 -08001726 // Attach the given tab to the content view.
Grace Klobac928c302009-09-17 11:51:21 -07001727 // this should only be called for the current tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728 private void attachTabToContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001729 // Attach the container that contains the main WebView and any other UI
1730 // associated with the tab.
Patrick Scottd0119532009-09-17 08:00:31 -04001731 t.attachTabToContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001732
1733 if (mShouldShowErrorConsole) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001734 ErrorConsoleView errorConsole = t.getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01001735 if (errorConsole.numberOfErrors() == 0) {
1736 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
1737 } else {
1738 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1739 }
1740
1741 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08001742 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01001743 ViewGroup.LayoutParams.WRAP_CONTENT));
1744 }
1745
Michael Kolba2b2ba82010-08-04 17:54:03 -07001746 WebView view = t.getWebView();
1747 view.setEmbeddedTitleBar(mTitleBar);
Leon Scroggins58d56c62010-01-28 15:12:40 -05001748 if (t.isInVoiceSearchMode()) {
1749 showVoiceTitleBar(t.getVoiceDisplayTitle());
1750 } else {
1751 revertVoiceTitleBar();
1752 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001753 // Request focus on the top window.
1754 t.getTopWindow().requestFocus();
Michael Kolba2b2ba82010-08-04 17:54:03 -07001755 if (mTabControl.getTabChangeListener() != null) {
1756 mTabControl.getTabChangeListener().onCurrentTab(t);
1757 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001758 }
1759
1760 // Attach a sub window to the main WebView of the given tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001761 void attachSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001762 t.attachSubWindow(mContentView);
1763 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001764 }
1765
1766 // Remove the given tab from the content view.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001767 private void removeTabFromContentView(Tab t) {
Steve Block2bc69912009-07-30 14:45:13 +01001768 // Remove the container that contains the main WebView.
Patrick Scottd0119532009-09-17 08:00:31 -04001769 t.removeTabFromContentView(mContentView);
Ben Murdochbff2d602009-07-01 20:19:05 +01001770
Grace Kloba22ac16e2009-10-07 18:00:23 -07001771 ErrorConsoleView errorConsole = t.getErrorConsole(false);
1772 if (errorConsole != null) {
1773 mErrorConsoleContainer.removeView(errorConsole);
Ben Murdochbff2d602009-07-01 20:19:05 +01001774 }
1775
Michael Kolba2b2ba82010-08-04 17:54:03 -07001776 WebView view = t.getWebView();
1777 if (view != null) {
1778 view.setEmbeddedTitleBar(null);
Leon Scrogginsbb85b902009-09-14 19:27:20 -04001779 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001780 }
1781
1782 // Remove the sub window if it exists. Also called by TabControl when the
1783 // user clicks the 'X' to dismiss a sub window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001784 /* package */ void dismissSubWindow(Tab t) {
Patrick Scottd0119532009-09-17 08:00:31 -04001785 t.removeSubWindow(mContentView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001786 // dismiss the subwindow. This will destroy the WebView.
1787 t.dismissSubWindow();
Patrick Scottd0119532009-09-17 08:00:31 -04001788 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001789 }
1790
Leon Scroggins1f005d32009-08-10 17:36:42 -04001791 // A wrapper function of {@link #openTabAndShow(UrlData, boolean, String)}
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07001792 // that accepts url as string.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001793 private Tab openTabAndShow(String url, boolean closeOnExit, String appId) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001794 return openTabAndShow(new UrlData(url), closeOnExit, appId);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001795 }
1796
1797 // This method does a ton of stuff. It will attempt to create a new tab
1798 // if we haven't reached MAX_TABS. Otherwise it uses the current tab. If
Leon Scroggins1f005d32009-08-10 17:36:42 -04001799 // url isn't null, it will load the given url.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001800 /* package */Tab openTabAndShow(UrlData urlData, boolean closeOnExit,
1801 String appId) {
1802 final Tab currentTab = mTabControl.getCurrentTab();
1803 if (mTabControl.canCreateNewTab()) {
1804 final Tab tab = mTabControl.createNewTab(closeOnExit, appId,
Elliott Slaughterf0f03952010-07-14 18:10:36 -07001805 urlData.mUrl, false);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001806 WebView webview = tab.getWebView();
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001807 // If the last tab was removed from the active tabs page, currentTab
1808 // will be null.
1809 if (currentTab != null) {
1810 removeTabFromContentView(currentTab);
1811 }
Patrick Scott8bbd69f2009-08-14 13:35:53 -04001812 // We must set the new tab as the current tab to reflect the old
1813 // animation behavior.
1814 mTabControl.setCurrentTab(tab);
Grace Klobaeb6eef42009-09-15 17:56:32 -07001815 attachTabToContentView(tab);
Leon Scroggins160a7e72009-08-14 18:28:01 -04001816 if (!urlData.isEmpty()) {
Patrick Scott9d53da02010-02-19 10:19:01 -05001817 loadUrlDataIn(tab, urlData);
Leon Scroggins1f005d32009-08-10 17:36:42 -04001818 }
1819 return tab;
1820 } else {
1821 // Get rid of the subwindow if it exists
1822 dismissSubWindow(currentTab);
1823 if (!urlData.isEmpty()) {
1824 // Load the given url.
Patrick Scott9d53da02010-02-19 10:19:01 -05001825 loadUrlDataIn(currentTab, urlData);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001826 }
Leon Scroggins58d56c62010-01-28 15:12:40 -05001827 return currentTab;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001828 }
1829 }
1830
Michael Kolb68792c82010-08-09 16:39:18 -07001831 private Tab openTab(String url, boolean forceForeground) {
1832 if (mSettings.openInBackground() && !forceForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001833 Tab t = mTabControl.createNewTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001834 if (t != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001835 WebView view = t.getWebView();
Leon Scroggins92472e82010-02-17 16:32:28 -05001836 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001837 }
Grace Klobac9181842009-04-14 08:53:22 -07001838 return t;
The Android Open Source Project0c908882009-03-03 19:32:16 -08001839 } else {
Leon Scroggins1f005d32009-08-10 17:36:42 -04001840 return openTabAndShow(url, false, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001841 }
1842 }
1843
Elliott Slaughterf0f03952010-07-14 18:10:36 -07001844 /* package */ Tab openIncognitoTab() {
1845 if (mTabControl.canCreateNewTab()) {
1846 Tab currentTab = mTabControl.getCurrentTab();
1847 Tab tab = mTabControl.createNewTab(false, null, null, true);
1848 if (currentTab != null) {
1849 removeTabFromContentView(currentTab);
1850 }
1851 mTabControl.setCurrentTab(tab);
1852 attachTabToContentView(tab);
1853 return tab;
1854 }
1855 return null;
1856 }
1857
The Android Open Source Project0c908882009-03-03 19:32:16 -08001858 private class Copy implements OnMenuItemClickListener {
1859 private CharSequence mText;
1860
1861 public boolean onMenuItemClick(MenuItem item) {
1862 copy(mText);
1863 return true;
1864 }
1865
1866 public Copy(CharSequence toCopy) {
1867 mText = toCopy;
1868 }
1869 }
1870
1871 private class Download implements OnMenuItemClickListener {
1872 private String mText;
1873
1874 public boolean onMenuItemClick(MenuItem item) {
1875 onDownloadStartNoStream(mText, null, null, null, -1);
1876 return true;
1877 }
1878
1879 public Download(String toDownload) {
1880 mText = toDownload;
1881 }
1882 }
1883
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001884 private class SetAsWallpaper extends Thread implements
1885 OnMenuItemClickListener, DialogInterface.OnCancelListener {
1886 private URL mUrl;
1887 private ProgressDialog mWallpaperProgress;
1888 private boolean mCanceled = false;
1889
1890 public SetAsWallpaper(String url) {
1891 try {
1892 mUrl = new URL(url);
1893 } catch (MalformedURLException e) {
1894 mUrl = null;
1895 }
1896 }
1897
1898 public void onCancel(DialogInterface dialog) {
1899 mCanceled = true;
1900 }
1901
1902 public boolean onMenuItemClick(MenuItem item) {
1903 if (mUrl != null) {
1904 // The user may have tried to set a image with a large file size as their
1905 // background so it may take a few moments to perform the operation. Display
1906 // a progress spinner while it is working.
1907 mWallpaperProgress = new ProgressDialog(BrowserActivity.this);
1908 mWallpaperProgress.setIndeterminate(true);
1909 mWallpaperProgress.setMessage(getText(R.string.progress_dialog_setting_wallpaper));
1910 mWallpaperProgress.setCancelable(true);
1911 mWallpaperProgress.setOnCancelListener(this);
1912 mWallpaperProgress.show();
1913 start();
1914 }
1915 return true;
1916 }
1917
Michael Kolbe0a36662010-06-29 10:37:12 -07001918 @Override
Ben Murdoch4f75ba22009-10-27 11:48:28 +00001919 public void run() {
1920 Drawable oldWallpaper = BrowserActivity.this.getWallpaper();
1921 try {
1922 // TODO: This will cause the resource to be downloaded again, when we
1923 // should in most cases be able to grab it from the cache. To fix this
1924 // we should query WebCore to see if we can access a cached version and
1925 // instead open an input stream on that. This pattern could also be used
1926 // in the download manager where the same problem exists.
1927 InputStream inputstream = mUrl.openStream();
1928 if (inputstream != null) {
1929 setWallpaper(inputstream);
1930 }
1931 } catch (IOException e) {
1932 Log.e(LOGTAG, "Unable to set new wallpaper");
1933 // Act as though the user canceled the operation so we try to
1934 // restore the old wallpaper.
1935 mCanceled = true;
1936 }
1937
1938 if (mCanceled) {
1939 // Restore the old wallpaper if the user cancelled whilst we were setting
1940 // the new wallpaper.
1941 int width = oldWallpaper.getIntrinsicWidth();
1942 int height = oldWallpaper.getIntrinsicHeight();
1943 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1944 Canvas canvas = new Canvas(bm);
1945 oldWallpaper.setBounds(0, 0, width, height);
1946 oldWallpaper.draw(canvas);
1947 try {
1948 setWallpaper(bm);
1949 } catch (IOException e) {
1950 Log.e(LOGTAG, "Unable to restore old wallpaper.");
1951 }
1952 mCanceled = false;
1953 }
1954
1955 if (mWallpaperProgress.isShowing()) {
1956 mWallpaperProgress.dismiss();
1957 }
1958 }
1959 }
1960
The Android Open Source Project0c908882009-03-03 19:32:16 -08001961 private void copy(CharSequence text) {
Dianne Hackborn80f32622010-08-05 14:17:53 -07001962 ClipboardManager cm = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
1963 cm.setText(text);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001964 }
1965
1966 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08001967 * Resets the browser title-view to whatever it must be
1968 * (for example, if we had a loading error)
1969 * When we have a new page, we call resetTitle, when we
1970 * have to reset the titlebar to whatever it used to be
1971 * (for example, if the user chose to stop loading), we
1972 * call resetTitleAndRevertLockIcon.
1973 */
1974 /* package */ void resetTitleAndRevertLockIcon() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001975 mTabControl.getCurrentTab().revertLockIcon();
1976 updateLockIconToLatest();
The Android Open Source Project0c908882009-03-03 19:32:16 -08001977 resetTitleIconAndProgress();
1978 }
1979
1980 /**
1981 * Reset the title, favicon, and progress.
1982 */
1983 private void resetTitleIconAndProgress() {
1984 WebView current = mTabControl.getCurrentWebView();
1985 if (current == null) {
1986 return;
1987 }
1988 resetTitleAndIcon(current);
1989 int progress = current.getProgress();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001990 current.getWebChromeClient().onProgressChanged(current, progress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001991 }
1992
1993 // Reset the title and the icon based on the given item.
1994 private void resetTitleAndIcon(WebView view) {
1995 WebHistoryItem item = view.copyBackForwardList().getCurrentItem();
1996 if (item != null) {
Leon Scroggins68579392009-09-15 15:31:54 -04001997 setUrlTitle(item.getUrl(), item.getTitle());
The Android Open Source Project0c908882009-03-03 19:32:16 -08001998 setFavicon(item.getFavicon());
1999 } else {
Leon Scroggins68579392009-09-15 15:31:54 -04002000 setUrlTitle(null, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002001 setFavicon(null);
2002 }
2003 }
2004
2005 /**
2006 * Sets a title composed of the URL and the title string.
2007 * @param url The URL of the site being loaded.
2008 * @param title The title of the site being loaded.
2009 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002010 void setUrlTitle(String url, String title) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002011 mUrl = url;
2012 mTitle = title;
2013
Leon Scroggins58d56c62010-01-28 15:12:40 -05002014 // If we are in voice search mode, the title has already been set.
2015 if (mTabControl.getCurrentTab().isInVoiceSearchMode()) return;
2016 mTitleBar.setDisplayTitle(url);
Michael Kolba2b2ba82010-08-04 17:54:03 -07002017 mFakeTitleBar.setDisplayTitle(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002018 }
2019
2020 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08002021 * @param url The URL to build a title version of the URL from.
2022 * @return The title version of the URL or null if fails.
2023 * The title version of the URL can be either the URL hostname,
2024 * or the hostname with an "https://" prefix (for secure URLs),
2025 * or an empty string if, for example, the URL in question is a
2026 * file:// URL with no hostname.
2027 */
Leon Scroggins32e14a62009-06-11 10:26:34 -04002028 /* package */ static String buildTitleUrl(String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002029 String titleUrl = null;
2030
2031 if (url != null) {
2032 try {
2033 // parse the url string
2034 URL urlObj = new URL(url);
2035 if (urlObj != null) {
2036 titleUrl = "";
2037
2038 String protocol = urlObj.getProtocol();
2039 String host = urlObj.getHost();
2040
2041 if (host != null && 0 < host.length()) {
2042 titleUrl = host;
2043 if (protocol != null) {
2044 // if a secure site, add an "https://" prefix!
2045 if (protocol.equalsIgnoreCase("https")) {
2046 titleUrl = protocol + "://" + host;
2047 }
2048 }
2049 }
2050 }
2051 } catch (MalformedURLException e) {}
2052 }
2053
2054 return titleUrl;
2055 }
2056
2057 // Set the favicon in the title bar.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002058 void setFavicon(Bitmap icon) {
Leon Scroggins68579392009-09-15 15:31:54 -04002059 mTitleBar.setFavicon(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -07002060 mFakeTitleBar.setFavicon(icon);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002061 }
2062
2063 /**
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002064 * Close the tab, remove its associated title bar, and adjust mTabControl's
2065 * current tab to a valid value.
Leon Scroggins1f005d32009-08-10 17:36:42 -04002066 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07002067 /* package */ void closeTab(Tab t) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002068 int currentIndex = mTabControl.getCurrentIndex();
2069 int removeIndex = mTabControl.getTabIndex(t);
Leon Scroggins1f005d32009-08-10 17:36:42 -04002070 mTabControl.removeTab(t);
Leon Scroggins0a64ba52009-09-08 15:35:33 -04002071 if (currentIndex >= removeIndex && currentIndex != 0) {
2072 currentIndex--;
2073 }
2074 mTabControl.setCurrentTab(mTabControl.getTab(currentIndex));
Andrei Popescua5bf1de2009-09-23 16:39:23 +01002075 resetTitleIconAndProgress();
Leon Scroggins654899b2010-06-25 16:25:23 -04002076 updateLockIconToLatest();
Elliott Slaughtere440a882010-08-20 13:54:45 -07002077
2078 if (!mTabControl.hasAnyOpenIncognitoTabs()) {
2079 WebView.cleanupPrivateBrowsingFiles(this);
2080 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002081 }
2082
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05002083 /* package */ void goBackOnePageOrQuit() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002084 Tab current = mTabControl.getCurrentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002085 if (current == null) {
2086 /*
2087 * Instead of finishing the activity, simply push this to the back
2088 * of the stack and let ActivityManager to choose the foreground
2089 * activity. As BrowserActivity is singleTask, it will be always the
2090 * root of the task. So we can use either true or false for
2091 * moveTaskToBack().
2092 */
2093 moveTaskToBack(true);
Grace Kloba00d85e72009-09-23 18:50:05 -07002094 return;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002095 }
2096 WebView w = current.getWebView();
2097 if (w.canGoBack()) {
2098 w.goBack();
2099 } else {
2100 // Check to see if we are closing a window that was created by
2101 // another window. If so, we switch back to that window.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002102 Tab parent = current.getParentTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002103 if (parent != null) {
Leon Scroggins1f005d32009-08-10 17:36:42 -04002104 switchToTab(mTabControl.getTabIndex(parent));
2105 // Now we close the other tab
2106 closeTab(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002107 } else {
2108 if (current.closeOnExit()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002109 // force the tab's inLoad() to be false as we are going to
2110 // either finish the activity or remove the tab. This will
2111 // ensure pauseWebViewTimers() taking action.
2112 mTabControl.getCurrentTab().clearInLoad();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002113 if (mTabControl.getTabCount() == 1) {
2114 finish();
2115 return;
2116 }
Mike Reed7bfa63b2009-05-28 11:08:32 -04002117 // call pauseWebViewTimers() now, we won't be able to call
2118 // it in onPause() as the WebView won't be valid.
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002119 // Temporarily change mActivityInPause to be true as
2120 // pauseWebViewTimers() will do nothing if mActivityInPause
2121 // is false.
Grace Kloba918e1d72009-08-13 14:55:06 -07002122 boolean savedState = mActivityInPause;
2123 if (savedState) {
Grace Klobaec1b5ad2009-08-18 08:42:32 -07002124 Log.e(LOGTAG, "BrowserActivity is already paused "
2125 + "while handing goBackOnePageOrQuit.");
Grace Kloba918e1d72009-08-13 14:55:06 -07002126 }
2127 mActivityInPause = true;
Mike Reed7bfa63b2009-05-28 11:08:32 -04002128 pauseWebViewTimers();
Grace Kloba918e1d72009-08-13 14:55:06 -07002129 mActivityInPause = savedState;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002130 removeTabFromContentView(current);
2131 mTabControl.removeTab(current);
2132 }
2133 /*
2134 * Instead of finishing the activity, simply push this to the back
2135 * of the stack and let ActivityManager to choose the foreground
2136 * activity. As BrowserActivity is singleTask, it will be always the
2137 * root of the task. So we can use either true or false for
2138 * moveTaskToBack().
2139 */
2140 moveTaskToBack(true);
2141 }
2142 }
2143 }
2144
Grace Kloba22ac16e2009-10-07 18:00:23 -07002145 boolean isMenuDown() {
2146 return mMenuIsDown;
2147 }
2148
Grace Kloba5942df02009-09-18 11:48:29 -07002149 @Override
2150 public boolean onKeyDown(int keyCode, KeyEvent event) {
Leon Scrogginsf65b50d2009-12-08 10:44:28 -05002151 // Even if MENU is already held down, we need to call to super to open
2152 // the IME on long press.
2153 if (KeyEvent.KEYCODE_MENU == keyCode) {
2154 mMenuIsDown = true;
2155 return super.onKeyDown(keyCode, event);
2156 }
Grace Kloba5942df02009-09-18 11:48:29 -07002157 // The default key mode is DEFAULT_KEYS_SEARCH_LOCAL. As the MENU is
2158 // still down, we don't want to trigger the search. Pretend to consume
2159 // the key and do nothing.
2160 if (mMenuIsDown) return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002161
Grace Kloba5942df02009-09-18 11:48:29 -07002162 switch(keyCode) {
Grace Kloba5942df02009-09-18 11:48:29 -07002163 case KeyEvent.KEYCODE_SPACE:
Grace Klobada0fe552009-09-22 18:17:24 -07002164 // WebView/WebTextView handle the keys in the KeyDown. As
2165 // the Activity's shortcut keys are only handled when WebView
2166 // doesn't, have to do it in onKeyDown instead of onKeyUp.
2167 if (event.isShiftPressed()) {
2168 getTopWindow().pageUp(false);
2169 } else {
2170 getTopWindow().pageDown(false);
2171 }
Grace Kloba5942df02009-09-18 11:48:29 -07002172 return true;
2173 case KeyEvent.KEYCODE_BACK:
2174 if (event.getRepeatCount() == 0) {
2175 event.startTracking();
2176 return true;
2177 } else if (mCustomView == null && mActiveTabsPage == null
Michael Kolbe421c242010-10-04 19:29:01 -07002178 && mComboView == null
Grace Kloba5942df02009-09-18 11:48:29 -07002179 && event.isLongPress()) {
Michael Kolbe421c242010-10-04 19:29:01 -07002180 bookmarksOrHistoryPicker(true);
Grace Kloba5942df02009-09-18 11:48:29 -07002181 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002182 }
Grace Kloba5942df02009-09-18 11:48:29 -07002183 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002184 }
Grace Kloba5942df02009-09-18 11:48:29 -07002185 return super.onKeyDown(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002186 }
2187
Grace Kloba5942df02009-09-18 11:48:29 -07002188 @Override
2189 public boolean onKeyUp(int keyCode, KeyEvent event) {
2190 switch(keyCode) {
2191 case KeyEvent.KEYCODE_MENU:
2192 mMenuIsDown = false;
2193 break;
Grace Kloba5942df02009-09-18 11:48:29 -07002194 case KeyEvent.KEYCODE_BACK:
2195 if (event.isTracking() && !event.isCanceled()) {
2196 if (mCustomView != null) {
2197 // if a custom view is showing, hide it
Grace Kloba22ac16e2009-10-07 18:00:23 -07002198 mTabControl.getCurrentWebView().getWebChromeClient()
2199 .onHideCustomView();
Grace Kloba5942df02009-09-18 11:48:29 -07002200 } else if (mActiveTabsPage != null) {
2201 // if tab page is showing, hide it
2202 removeActiveTabPage(true);
Michael Kolbe421c242010-10-04 19:29:01 -07002203 } else if (mComboView != null) {
Michael Kolb370a4f32010-10-06 10:45:32 -07002204 if (!mComboView.onBackPressed()) {
2205 removeComboView();
2206 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002207 } else {
Grace Kloba5942df02009-09-18 11:48:29 -07002208 WebView subwindow = mTabControl.getCurrentSubWindow();
2209 if (subwindow != null) {
2210 if (subwindow.canGoBack()) {
2211 subwindow.goBack();
2212 } else {
2213 dismissSubWindow(mTabControl.getCurrentTab());
2214 }
2215 } else {
2216 goBackOnePageOrQuit();
2217 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002218 }
Grace Kloba5942df02009-09-18 11:48:29 -07002219 return true;
2220 }
2221 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002222 }
Grace Kloba5942df02009-09-18 11:48:29 -07002223 return super.onKeyUp(keyCode, event);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002224 }
2225
Leon Scroggins68579392009-09-15 15:31:54 -04002226 /* package */ void stopLoading() {
Ben Murdochb7cc8b42009-09-28 10:59:09 +01002227 mDidStopLoad = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002228 resetTitleAndRevertLockIcon();
2229 WebView w = getTopWindow();
2230 w.stopLoading();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002231 // FIXME: before refactor, it is using mWebViewClient. So I keep the
2232 // same logic here. But for subwindow case, should we call into the main
2233 // WebView's onPageFinished as we never call its onPageStarted and if
2234 // the page finishes itself, we don't call onPageFinished.
2235 mTabControl.getCurrentWebView().getWebViewClient().onPageFinished(w,
2236 w.getUrl());
The Android Open Source Project0c908882009-03-03 19:32:16 -08002237
2238 cancelStopToast();
2239 mStopToast = Toast
2240 .makeText(this, R.string.stopping, Toast.LENGTH_SHORT);
2241 mStopToast.show();
2242 }
2243
Grace Kloba22ac16e2009-10-07 18:00:23 -07002244 boolean didUserStopLoading() {
2245 return mDidStopLoad;
2246 }
2247
The Android Open Source Project0c908882009-03-03 19:32:16 -08002248 private void cancelStopToast() {
2249 if (mStopToast != null) {
2250 mStopToast.cancel();
2251 mStopToast = null;
2252 }
2253 }
2254
Grace Kloba22ac16e2009-10-07 18:00:23 -07002255 // called by a UI or non-UI thread to post the message
2256 public void postMessage(int what, int arg1, int arg2, Object obj,
2257 long delayMillis) {
2258 mHandler.sendMessageDelayed(mHandler.obtainMessage(what, arg1, arg2,
2259 obj), delayMillis);
2260 }
2261
2262 // called by a UI or non-UI thread to remove the message
2263 void removeMessages(int what, Object object) {
2264 mHandler.removeMessages(what, object);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002265 }
2266
2267 // public message ids
2268 public final static int LOAD_URL = 1001;
2269 public final static int STOP_LOAD = 1002;
2270
2271 // Message Ids
2272 private static final int FOCUS_NODE_HREF = 102;
Grace Kloba92c18a52009-07-31 23:48:32 -07002273 private static final int RELEASE_WAKELOCK = 107;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002274
Grace Kloba22ac16e2009-10-07 18:00:23 -07002275 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
Ben Murdoch2694e232009-09-29 09:41:11 +01002276
Michael Kolb300b7f02010-08-25 13:47:24 -07002277 private static final int OPEN_BOOKMARKS = 201;
2278
The Android Open Source Project0c908882009-03-03 19:32:16 -08002279 // Private handler for handling javascript and saving passwords
2280 private Handler mHandler = new Handler() {
2281
Michael Kolbe0a36662010-06-29 10:37:12 -07002282 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -08002283 public void handleMessage(Message msg) {
2284 switch (msg.what) {
Michael Kolb300b7f02010-08-25 13:47:24 -07002285 case OPEN_BOOKMARKS:
Michael Kolbe421c242010-10-04 19:29:01 -07002286 bookmarksOrHistoryPicker(false);
Michael Kolb300b7f02010-08-25 13:47:24 -07002287 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002288 case FOCUS_NODE_HREF:
Ben Murdoch2694e232009-09-29 09:41:11 +01002289 {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002290 String url = (String) msg.getData().get("url");
Ben Murdoch90d088c2009-11-17 18:14:04 +00002291 String title = (String) msg.getData().get("title");
The Android Open Source Project0c908882009-03-03 19:32:16 -08002292 if (url == null || url.length() == 0) {
2293 break;
2294 }
2295 HashMap focusNodeMap = (HashMap) msg.obj;
2296 WebView view = (WebView) focusNodeMap.get("webview");
2297 // Only apply the action if the top window did not change.
2298 if (getTopWindow() != view) {
2299 break;
2300 }
2301 switch (msg.arg1) {
2302 case R.id.open_context_menu_id:
2303 case R.id.view_image_context_menu_id:
Leon Scroggins92472e82010-02-17 16:32:28 -05002304 loadUrlFromContext(getTopWindow(), url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002305 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002306 case R.id.bookmark_context_menu_id:
2307 Intent intent = new Intent(BrowserActivity.this,
2308 AddBookmarkPage.class);
Leon Scrogginsbc922852010-10-22 12:15:27 -04002309 intent.putExtra(BrowserContract.Bookmarks.URL, url);
2310 intent.putExtra(BrowserContract.Bookmarks.TITLE,
2311 title);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002312 startActivity(intent);
2313 break;
2314 case R.id.share_link_context_menu_id:
Leon Scroggins3e204452010-05-10 11:06:03 -04002315 sharePage(BrowserActivity.this, title, url, null,
Leon Scroggins96afcb12009-12-10 12:35:56 -05002316 null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002317 break;
2318 case R.id.copy_link_context_menu_id:
2319 copy(url);
2320 break;
2321 case R.id.save_link_context_menu_id:
2322 case R.id.download_context_menu_id:
2323 onDownloadStartNoStream(url, null, null, null, -1);
2324 break;
2325 }
2326 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002327 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002328
2329 case LOAD_URL:
Leon Scroggins92472e82010-02-17 16:32:28 -05002330 loadUrlFromContext(getTopWindow(), (String) msg.obj);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002331 break;
2332
2333 case STOP_LOAD:
2334 stopLoading();
2335 break;
2336
The Android Open Source Project0c908882009-03-03 19:32:16 -08002337 case RELEASE_WAKELOCK:
2338 if (mWakeLock.isHeld()) {
2339 mWakeLock.release();
Grace Kloba5d0e02e2009-10-05 15:15:36 -07002340 // if we reach here, Browser should be still in the
2341 // background loading after WAKELOCK_TIMEOUT (5-min).
2342 // To avoid burning the battery, stop loading.
2343 mTabControl.stopAllLoading();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002344 }
2345 break;
Ben Murdoch2694e232009-09-29 09:41:11 +01002346
2347 case UPDATE_BOOKMARK_THUMBNAIL:
2348 WebView view = (WebView) msg.obj;
2349 if (view != null) {
2350 updateScreenshot(view);
2351 }
2352 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002353 }
2354 }
2355 };
2356
Leon Scroggins96afcb12009-12-10 12:35:56 -05002357 /**
2358 * Share a page, providing the title, url, favicon, and a screenshot. Uses
2359 * an {@link Intent} to launch the Activity chooser.
2360 * @param c Context used to launch a new Activity.
2361 * @param title Title of the page. Stored in the Intent with
Paul Westbrook03e6d392010-02-12 10:33:29 -08002362 * {@link Intent#EXTRA_SUBJECT}
Leon Scroggins96afcb12009-12-10 12:35:56 -05002363 * @param url URL of the page. Stored in the Intent with
2364 * {@link Intent#EXTRA_TEXT}
2365 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
2366 * with {@link Browser#EXTRA_SHARE_FAVICON}
2367 * @param screenshot Bitmap of a screenshot of the page. Stored in the
2368 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
2369 */
2370 public static final void sharePage(Context c, String title, String url,
2371 Bitmap favicon, Bitmap screenshot) {
2372 Intent send = new Intent(Intent.ACTION_SEND);
2373 send.setType("text/plain");
2374 send.putExtra(Intent.EXTRA_TEXT, url);
Paul Westbrook03e6d392010-02-12 10:33:29 -08002375 send.putExtra(Intent.EXTRA_SUBJECT, title);
Leon Scroggins96afcb12009-12-10 12:35:56 -05002376 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
2377 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
2378 try {
2379 c.startActivity(Intent.createChooser(send, c.getString(
2380 R.string.choosertitle_sharevia)));
2381 } catch(android.content.ActivityNotFoundException ex) {
2382 // if no app handles it, do nothing
2383 }
2384 }
2385
Leon Scroggins89c6d362009-07-15 16:54:37 -04002386 private void updateScreenshot(WebView view) {
2387 // If this is a bookmarked site, add a screenshot to the database.
2388 // FIXME: When should we update? Every time?
2389 // FIXME: Would like to make sure there is actually something to
2390 // draw, but the API for that (WebViewCore.pictureReady()) is not
2391 // currently accessible here.
Ben Murdochaac7aa62009-09-17 16:57:40 +01002392
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002393 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(this),
2394 getDesiredThumbnailHeight(this));
Patrick Scottcb192b52010-04-14 14:38:55 -04002395 if (bm == null) {
2396 return;
Leon Scroggins89c6d362009-07-15 16:54:37 -04002397 }
Patrick Scottcb192b52010-04-14 14:38:55 -04002398
2399 final ContentResolver cr = getContentResolver();
2400 final String url = view.getUrl();
2401 final String originalUrl = view.getOriginalUrl();
2402
2403 new AsyncTask<Void, Void, Void>() {
2404 @Override
2405 protected Void doInBackground(Void... unused) {
Jeff Hamilton1a805652010-09-07 12:36:30 -07002406 Cursor cursor = null;
Patrick Scottcb192b52010-04-14 14:38:55 -04002407 try {
Jeff Hamilton1a805652010-09-07 12:36:30 -07002408 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2409 if (cursor != null && cursor.moveToFirst()) {
2410 final ByteArrayOutputStream os = new ByteArrayOutputStream();
2411 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
2412
2413 ContentValues values = new ContentValues();
2414 values.put(Images.THUMBNAIL, os.toByteArray());
2415 values.put(Images.URL, cursor.getString(0));
2416
2417 do {
2418 cr.update(Images.CONTENT_URI, values, null, null);
2419 } while (cursor.moveToNext());
Patrick Scottcb192b52010-04-14 14:38:55 -04002420 }
2421 } catch (IllegalStateException e) {
2422 // Ignore
2423 } finally {
Jeff Hamilton1a805652010-09-07 12:36:30 -07002424 if (cursor != null) cursor.close();
Patrick Scottcb192b52010-04-14 14:38:55 -04002425 }
2426 return null;
2427 }
2428 }.execute();
Leon Scroggins89c6d362009-07-15 16:54:37 -04002429 }
2430
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002431 /**
Leon Scrogginsf8551612009-09-24 16:06:02 -04002432 * Return the desired width for thumbnail screenshots, which are stored in
2433 * the database, and used on the bookmarks screen.
2434 * @param context Context for finding out the density of the screen.
Jeff Hamiltonf3026372010-08-19 00:12:27 -05002435 * @return desired width for thumbnail screenshot.
Leon Scrogginsf8551612009-09-24 16:06:02 -04002436 */
2437 /* package */ static int getDesiredThumbnailWidth(Context context) {
Jeff Hamiltonf3026372010-08-19 00:12:27 -05002438 return context.getResources().getDimensionPixelOffset(R.dimen.bookmarkThumbnailWidth);
Leon Scrogginsf8551612009-09-24 16:06:02 -04002439 }
2440
2441 /**
2442 * Return the desired height for thumbnail screenshots, which are stored in
2443 * the database, and used on the bookmarks screen.
2444 * @param context Context for finding out the density of the screen.
Jeff Hamiltonf3026372010-08-19 00:12:27 -05002445 * @return desired height for thumbnail screenshot.
Leon Scrogginsf8551612009-09-24 16:06:02 -04002446 */
2447 /* package */ static int getDesiredThumbnailHeight(Context context) {
Jeff Hamiltonf3026372010-08-19 00:12:27 -05002448 return context.getResources().getDimensionPixelOffset(R.dimen.bookmarkThumbnailHeight);
Leon Scrogginsf8551612009-09-24 16:06:02 -04002449 }
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002450
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002451 private Bitmap createScreenshot(WebView view, int width, int height) {
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002452 Picture thumbnail = view.capturePicture();
Leon Scroggins45800572009-09-29 16:38:47 -04002453 if (thumbnail == null) {
2454 return null;
2455 }
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002456 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002457 Canvas canvas = new Canvas(bm);
2458 // May need to tweak these values to determine what is the
2459 // best scale factor
Ben Murdoch2694e232009-09-29 09:41:11 +01002460 int thumbnailWidth = thumbnail.getWidth();
Ben Murdochae59c3f2009-10-20 18:30:28 +01002461 int thumbnailHeight = thumbnail.getHeight();
2462 float scaleFactorX = 1.0f;
2463 float scaleFactorY = 1.0f;
Ben Murdoch2694e232009-09-29 09:41:11 +01002464 if (thumbnailWidth > 0) {
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002465 scaleFactorX = (float) width / (float)thumbnailWidth;
Ben Murdochae59c3f2009-10-20 18:30:28 +01002466 } else {
2467 return null;
Leon Scroggins06ec5f22009-09-17 12:46:04 -04002468 }
Ben Murdochae59c3f2009-10-20 18:30:28 +01002469
2470 if (view.getWidth() > view.getHeight() &&
2471 thumbnailHeight < view.getHeight() && thumbnailHeight > 0) {
2472 // If the device is in landscape and the page is shorter
2473 // than the height of the view, stretch the thumbnail to fill the
2474 // space.
Ben Murdoch87cc65d2010-06-29 20:34:10 +01002475 scaleFactorY = (float) height / (float)thumbnailHeight;
Ben Murdochae59c3f2009-10-20 18:30:28 +01002476 } else {
2477 // In the portrait case, this looks nice.
2478 scaleFactorY = scaleFactorX;
2479 }
2480
2481 canvas.scale(scaleFactorX, scaleFactorY);
2482
Ben Murdochdcc2b6f2009-09-21 14:29:20 +01002483 thumbnail.draw(canvas);
2484 return bm;
2485 }
2486
The Android Open Source Project0c908882009-03-03 19:32:16 -08002487 // -------------------------------------------------------------------------
Grace Kloba22ac16e2009-10-07 18:00:23 -07002488 // Helper function for WebViewClient.
The Android Open Source Project0c908882009-03-03 19:32:16 -08002489 //-------------------------------------------------------------------------
2490
2491 // Use in overrideUrlLoading
2492 /* package */ final static String SCHEME_WTAI = "wtai://wp/";
2493 /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
2494 /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;";
2495 /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;";
2496
Leon Scroggins92472e82010-02-17 16:32:28 -05002497 // Keep this initial progress in sync with initialProgressValue (* 100)
2498 // in ProgressTracker.cpp
2499 private final static int INITIAL_PROGRESS = 10;
2500
Grace Kloba22ac16e2009-10-07 18:00:23 -07002501 void onPageStarted(WebView view, String url, Bitmap favicon) {
2502 // when BrowserActivity just starts, onPageStarted may be called before
2503 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
2504 // to start the timer. As we won't switch tabs while an activity is in
2505 // pause state, we can ensure calling resume and pause in pair.
2506 if (mActivityInPause) resumeWebViewTimers();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002507
Grace Kloba22ac16e2009-10-07 18:00:23 -07002508 resetLockIcon(url);
2509 setUrlTitle(url, null);
2510 setFavicon(favicon);
Leon Scroggins8cf8f682009-11-04 11:13:50 -08002511 // Show some progress so that the user knows the page is beginning to
2512 // load
Leon Scroggins92472e82010-02-17 16:32:28 -05002513 onProgressChanged(view, INITIAL_PROGRESS);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002514 mDidStopLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07002515 if (!mIsNetworkUp) createAndShowNetworkDialog();
Leon Scroggins III8e4fbf12010-08-17 16:58:15 -04002516 endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002517 if (mSettings.isTracing()) {
2518 String host;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002519 try {
Grace Kloba22ac16e2009-10-07 18:00:23 -07002520 WebAddress uri = new WebAddress(url);
Bjorn Bringert131ab512010-10-12 16:25:47 +01002521 host = uri.getHost();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002522 } catch (android.net.ParseException ex) {
2523 host = "browser";
The Android Open Source Project0c908882009-03-03 19:32:16 -08002524 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002525 host = host.replace('.', '_');
2526 host += ".trace";
2527 mInTrace = true;
2528 Debug.startMethodTracing(host, 20 * 1024 * 1024);
2529 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002530
Grace Kloba22ac16e2009-10-07 18:00:23 -07002531 // Performance probe
2532 if (false) {
2533 mStart = SystemClock.uptimeMillis();
2534 mProcessStart = Process.getElapsedCpuTime();
2535 long[] sysCpu = new long[7];
2536 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2537 sysCpu, null)) {
2538 mUserStart = sysCpu[0] + sysCpu[1];
2539 mSystemStart = sysCpu[2];
2540 mIdleStart = sysCpu[3];
2541 mIrqStart = sysCpu[4] + sysCpu[5] + sysCpu[6];
2542 }
2543 mUiStart = SystemClock.currentThreadTimeMillis();
2544 }
2545 }
2546
2547 void onPageFinished(WebView view, String url) {
2548 // Reset the title and icon in case we stopped a provisional load.
2549 resetTitleAndIcon(view);
2550 // Update the lock icon image only once we are done loading
2551 updateLockIconToLatest();
2552 // pause the WebView timer and release the wake lock if it is finished
2553 // while BrowserActivity is in pause state.
2554 if (mActivityInPause && pauseWebViewTimers()) {
2555 if (mWakeLock.isHeld()) {
2556 mHandler.removeMessages(RELEASE_WAKELOCK);
2557 mWakeLock.release();
2558 }
2559 }
2560
2561 // Performance probe
2562 if (false) {
2563 long[] sysCpu = new long[7];
2564 if (Process.readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null,
2565 sysCpu, null)) {
2566 String uiInfo = "UI thread used "
2567 + (SystemClock.currentThreadTimeMillis() - mUiStart)
2568 + " ms";
2569 if (LOGD_ENABLED) {
2570 Log.d(LOGTAG, uiInfo);
2571 }
2572 //The string that gets written to the log
2573 String performanceString = "It took total "
2574 + (SystemClock.uptimeMillis() - mStart)
2575 + " ms clock time to load the page."
2576 + "\nbrowser process used "
2577 + (Process.getElapsedCpuTime() - mProcessStart)
2578 + " ms, user processes used "
2579 + (sysCpu[0] + sysCpu[1] - mUserStart) * 10
2580 + " ms, kernel used "
2581 + (sysCpu[2] - mSystemStart) * 10
2582 + " ms, idle took " + (sysCpu[3] - mIdleStart) * 10
2583 + " ms and irq took "
2584 + (sysCpu[4] + sysCpu[5] + sysCpu[6] - mIrqStart)
2585 * 10 + " ms, " + uiInfo;
2586 if (LOGD_ENABLED) {
2587 Log.d(LOGTAG, performanceString + "\nWebpage: " + url);
2588 }
2589 if (url != null) {
2590 // strip the url to maintain consistency
2591 String newUrl = new String(url);
2592 if (newUrl.startsWith("http://www.")) {
2593 newUrl = newUrl.substring(11);
2594 } else if (newUrl.startsWith("http://")) {
2595 newUrl = newUrl.substring(7);
2596 } else if (newUrl.startsWith("https://www.")) {
2597 newUrl = newUrl.substring(12);
2598 } else if (newUrl.startsWith("https://")) {
2599 newUrl = newUrl.substring(8);
2600 }
2601 if (LOGD_ENABLED) {
2602 Log.d(LOGTAG, newUrl + " loaded");
2603 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002604 }
2605 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002606 }
Grace Kloba5b078b52009-06-24 20:23:41 -07002607
Grace Kloba22ac16e2009-10-07 18:00:23 -07002608 if (mInTrace) {
2609 mInTrace = false;
2610 Debug.stopMethodTracing();
2611 }
2612 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002613
Grace Klobae7fe26b2010-06-28 16:23:33 -07002614 private void closeEmptyChildTab() {
2615 Tab current = mTabControl.getCurrentTab();
2616 if (current != null
2617 && current.getWebView().copyBackForwardList().getSize() == 0) {
2618 Tab parent = current.getParentTab();
2619 if (parent != null) {
2620 switchToTab(mTabControl.getTabIndex(parent));
2621 closeTab(current);
2622 }
2623 }
2624 }
2625
Grace Kloba22ac16e2009-10-07 18:00:23 -07002626 boolean shouldOverrideUrlLoading(WebView view, String url) {
Jeff Hamilton47654f42010-09-07 09:57:51 -05002627 if (view.isPrivateBrowsingEnabled()) {
2628 // Don't allow urls to leave the browser app when in private browsing mode
2629 loadUrl(view, url);
2630 return true;
2631 }
2632
Grace Kloba22ac16e2009-10-07 18:00:23 -07002633 if (url.startsWith(SCHEME_WTAI)) {
2634 // wtai://wp/mc;number
2635 // number=string(phone-number)
2636 if (url.startsWith(SCHEME_WTAI_MC)) {
2637 Intent intent = new Intent(Intent.ACTION_VIEW,
2638 Uri.parse(WebView.SCHEME_TEL +
2639 url.substring(SCHEME_WTAI_MC.length())));
2640 startActivity(intent);
Grace Klobae7fe26b2010-06-28 16:23:33 -07002641 // before leaving BrowserActivity, close the empty child tab.
2642 // If a new tab is created through JavaScript open to load this
2643 // url, we would like to close it as we will load this url in a
2644 // different Activity.
2645 closeEmptyChildTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002646 return true;
2647 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002648 // wtai://wp/sd;dtmf
2649 // dtmf=string(dialstring)
2650 if (url.startsWith(SCHEME_WTAI_SD)) {
2651 // TODO: only send when there is active voice connection
2652 return false;
2653 }
2654 // wtai://wp/ap;number;name
2655 // number=string(phone-number)
2656 // name=string
2657 if (url.startsWith(SCHEME_WTAI_AP)) {
2658 // TODO
2659 return false;
2660 }
2661 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002662
Grace Kloba22ac16e2009-10-07 18:00:23 -07002663 // The "about:" schemes are internal to the browser; don't want these to
2664 // be dispatched to other apps.
2665 if (url.startsWith("about:")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08002666 return false;
2667 }
2668
Jeff Davidson43610292010-07-16 16:03:58 -07002669 // If this is a Google search, attempt to add an RLZ string (if one isn't already present).
2670 if (rlzProviderPresent()) {
2671 Uri siteUri = Uri.parse(url);
2672 if (needsRlzString(siteUri)) {
2673 String rlz = null;
2674 Cursor cur = null;
2675 try {
2676 cur = getContentResolver().query(getRlzUri(), null, null, null, null);
2677 if (cur != null && cur.moveToFirst() && !cur.isNull(0)) {
2678 url = siteUri.buildUpon()
2679 .appendQueryParameter("rlz", cur.getString(0))
2680 .build().toString();
2681 }
2682 } finally {
2683 if (cur != null) {
2684 cur.close();
2685 }
2686 }
2687 loadUrl(view, url);
2688 return true;
2689 }
2690 }
2691
Grace Kloba22ac16e2009-10-07 18:00:23 -07002692 Intent intent;
2693 // perform generic parsing of the URI to turn it into an Intent.
2694 try {
2695 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
2696 } catch (URISyntaxException ex) {
2697 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
2698 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002699 }
2700
Grace Kloba22ac16e2009-10-07 18:00:23 -07002701 // check whether the intent can be resolved. If not, we will see
2702 // whether we can download it from the Market.
2703 if (getPackageManager().resolveActivity(intent, 0) == null) {
2704 String packagename = intent.getPackage();
2705 if (packagename != null) {
2706 intent = new Intent(Intent.ACTION_VIEW, Uri
2707 .parse("market://search?q=pname:" + packagename));
2708 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2709 startActivity(intent);
Grace Klobae7fe26b2010-06-28 16:23:33 -07002710 // before leaving BrowserActivity, close the empty child tab.
2711 // If a new tab is created through JavaScript open to load this
2712 // url, we would like to close it as we will load this url in a
2713 // different Activity.
2714 closeEmptyChildTab();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002715 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08002716 } else {
2717 return false;
2718 }
2719 }
2720
Grace Kloba22ac16e2009-10-07 18:00:23 -07002721 // sanitize the Intent, ensuring web pages can not bypass browser
2722 // security (only access to BROWSABLE activities).
2723 intent.addCategory(Intent.CATEGORY_BROWSABLE);
2724 intent.setComponent(null);
2725 try {
2726 if (startActivityIfNeeded(intent, -1)) {
Grace Klobae7fe26b2010-06-28 16:23:33 -07002727 // before leaving BrowserActivity, close the empty child tab.
2728 // If a new tab is created through JavaScript open to load this
2729 // url, we would like to close it as we will load this url in a
2730 // different Activity.
2731 closeEmptyChildTab();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002732 return true;
2733 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002734 } catch (ActivityNotFoundException ex) {
2735 // ignore the error. If no application can handle the URL,
2736 // eg about:blank, assume the browser can handle it.
2737 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002738
Grace Kloba22ac16e2009-10-07 18:00:23 -07002739 if (mMenuIsDown) {
Michael Kolb68792c82010-08-09 16:39:18 -07002740 openTab(url, false);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002741 closeOptionsMenu();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002742 return true;
2743 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002744 return false;
2745 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002746
Jeff Davidson43610292010-07-16 16:03:58 -07002747 // Determine whether the RLZ provider is present on the system.
2748 private boolean rlzProviderPresent() {
2749 if (mIsProviderPresent == null) {
2750 PackageManager pm = getPackageManager();
2751 mIsProviderPresent = pm.resolveContentProvider(BrowserSettings.RLZ_PROVIDER, 0) != null;
2752 }
2753 return mIsProviderPresent;
2754 }
2755
2756 // Retrieve the RLZ access point string and cache the URI used to retrieve RLZ values.
2757 private Uri getRlzUri() {
2758 if (mRlzUri == null) {
2759 String ap = getResources().getString(R.string.rlz_access_point);
2760 mRlzUri = Uri.withAppendedPath(BrowserSettings.RLZ_PROVIDER_URI, ap);
2761 }
2762 return mRlzUri;
2763 }
2764
2765 // Determine if this URI appears to be for a Google search and does not have an RLZ parameter.
2766 // Taken largely from Chrome source, src/chrome/browser/google_url_tracker.cc
2767 private static boolean needsRlzString(Uri uri) {
Bill Napierc50e71a2010-08-27 14:03:03 -07002768 String scheme = uri.getScheme();
2769 if (("http".equals(scheme) || "https".equals(scheme)) &&
2770 (uri.getQueryParameter("q") != null) && (uri.getQueryParameter("rlz") == null)) {
Jeff Davidson43610292010-07-16 16:03:58 -07002771 String host = uri.getHost();
2772 if (host == null) {
2773 return false;
2774 }
2775 String[] hostComponents = host.split("\\.");
2776
2777 if (hostComponents.length < 2) {
2778 return false;
2779 }
2780 int googleComponent = hostComponents.length - 2;
2781 String component = hostComponents[googleComponent];
2782 if (!"google".equals(component)) {
2783 if (hostComponents.length < 3 ||
2784 (!"co".equals(component) && !"com".equals(component))) {
2785 return false;
2786 }
2787 googleComponent = hostComponents.length - 3;
2788 if (!"google".equals(hostComponents[googleComponent])) {
2789 return false;
2790 }
2791 }
2792
2793 // Google corp network handling.
2794 if (googleComponent > 0 && "corp".equals(hostComponents[googleComponent - 1])) {
2795 return false;
2796 }
2797
2798 return true;
2799 }
2800 return false;
2801 }
2802
Grace Kloba22ac16e2009-10-07 18:00:23 -07002803 // -------------------------------------------------------------------------
2804 // Helper function for WebChromeClient
2805 // -------------------------------------------------------------------------
The Android Open Source Project0c908882009-03-03 19:32:16 -08002806
Grace Kloba22ac16e2009-10-07 18:00:23 -07002807 void onProgressChanged(WebView view, int newProgress) {
Michael Kolba2b2ba82010-08-04 17:54:03 -07002808
2809 // On the phone, the fake title bar will always cover up the
2810 // regular title bar (or the regular one is offscreen), so only the
2811 // fake title bar needs to change its progress
2812 mFakeTitleBar.setProgress(newProgress);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002813
Grace Kloba22ac16e2009-10-07 18:00:23 -07002814 if (newProgress == 100) {
2815 // onProgressChanged() may continue to be called after the main
2816 // frame has finished loading, as any remaining sub frames continue
2817 // to load. We'll only get called once though with newProgress as
2818 // 100 when everything is loaded. (onPageFinished is called once
2819 // when the main frame completes loading regardless of the state of
2820 // any sub frames so calls to onProgressChanges may continue after
2821 // onPageFinished has executed)
2822 if (mInLoad) {
2823 mInLoad = false;
Leon Scrogginsa27ff192009-09-14 12:58:04 -04002824 updateInLoadMenuItems();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002825 // If the options menu is open, leave the title bar
2826 if (!mOptionsMenuOpen || !mIconView) {
2827 hideFakeTitleBar();
The Android Open Source Projectcb9a0bb2009-03-11 12:11:58 -07002828 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002829 }
Leon Scrogginsaa37e7f2010-03-05 11:51:20 -05002830 } else {
2831 if (!mInLoad) {
2832 // onPageFinished may have already been called but a subframe is
2833 // still loading and updating the progress. Reset mInLoad and
2834 // update the menu items.
2835 mInLoad = true;
2836 updateInLoadMenuItems();
2837 }
2838 // When the page first begins to load, the Activity may still be
2839 // paused, in which case showFakeTitleBar will do nothing. Call
2840 // again as the page continues to load so that it will be shown.
2841 // (Calling it will the fake title bar is already showing will also
2842 // do nothing.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002843 if (!mOptionsMenuOpen || mIconView) {
2844 // This page has begun to load, so show the title bar
2845 showFakeTitleBar();
The Android Open Source Project0c908882009-03-03 19:32:16 -08002846 }
2847 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002848 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002849
Grace Kloba22ac16e2009-10-07 18:00:23 -07002850 void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002851 // if a view already exists then immediately terminate the new one
2852 if (mCustomView != null) {
2853 callback.onCustomViewHidden();
Grace Kloba22ac16e2009-10-07 18:00:23 -07002854 return;
Derek Sollenberger8ff57db2009-12-08 15:39:55 -05002855 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002856
2857 // Add the custom view to its container.
2858 mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
2859 mCustomView = view;
2860 mCustomViewCallback = callback;
2861 // Save the menu state and set it to empty while the custom
2862 // view is showing.
2863 mOldMenuState = mMenuState;
2864 mMenuState = EMPTY_MENU;
2865 // Hide the content view.
2866 mContentView.setVisibility(View.GONE);
2867 // Finally show the custom view container.
Andrei Popescu163ab742009-10-20 17:58:23 +01002868 setStatusBarVisibility(false);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002869 mCustomViewContainer.setVisibility(View.VISIBLE);
2870 mCustomViewContainer.bringToFront();
2871 }
2872
2873 void onHideCustomView() {
2874 if (mCustomView == null)
2875 return;
2876
2877 // Hide the custom view.
2878 mCustomView.setVisibility(View.GONE);
2879 // Remove the custom view from its container.
2880 mCustomViewContainer.removeView(mCustomView);
2881 mCustomView = null;
2882 // Reset the old menu state.
2883 mMenuState = mOldMenuState;
2884 mOldMenuState = EMPTY_MENU;
2885 mCustomViewContainer.setVisibility(View.GONE);
2886 mCustomViewCallback.onCustomViewHidden();
2887 // Show the content view.
Andrei Popescu163ab742009-10-20 17:58:23 +01002888 setStatusBarVisibility(true);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002889 mContentView.setVisibility(View.VISIBLE);
2890 }
2891
2892 Bitmap getDefaultVideoPoster() {
2893 if (mDefaultVideoPoster == null) {
2894 mDefaultVideoPoster = BitmapFactory.decodeResource(
2895 getResources(), R.drawable.default_video_poster);
Patrick Scott3918d442009-08-04 13:22:29 -04002896 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002897 return mDefaultVideoPoster;
2898 }
Patrick Scott3918d442009-08-04 13:22:29 -04002899
Grace Kloba22ac16e2009-10-07 18:00:23 -07002900 View getVideoLoadingProgressView() {
2901 if (mVideoProgressView == null) {
2902 LayoutInflater inflater = LayoutInflater.from(BrowserActivity.this);
2903 mVideoProgressView = inflater.inflate(
2904 R.layout.video_loading_progress, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08002905 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002906 return mVideoProgressView;
2907 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08002908
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002909 /*
2910 * The Object used to inform the WebView of the file to upload.
2911 */
2912 private ValueCallback<Uri> mUploadMessage;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002913 private String mCameraFilePath;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04002914
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002915 void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
2916
2917 final String imageMimeType = "image/*";
Ben Murdoch6af492a2010-06-15 12:38:17 +01002918 final String videoMimeType = "video/*";
Ben Murdoch039bd472010-07-21 21:26:57 +01002919 final String audioMimeType = "audio/*";
Ben Murdochb50d7402010-07-16 12:42:22 +01002920 final String mediaSourceKey = "capture";
Ben Murdoch6af492a2010-06-15 12:38:17 +01002921 final String mediaSourceValueCamera = "camera";
Ben Murdochb50d7402010-07-16 12:42:22 +01002922 final String mediaSourceValueFileSystem = "filesystem";
Ben Murdoch6af492a2010-06-15 12:38:17 +01002923 final String mediaSourceValueCamcorder = "camcorder";
Ben Murdoch039bd472010-07-21 21:26:57 +01002924 final String mediaSourceValueMicrophone = "microphone";
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002925
Ben Murdoch039bd472010-07-21 21:26:57 +01002926 // media source can be 'filesystem' or 'camera' or 'camcorder' or 'microphone'.
Ben Murdoch6af492a2010-06-15 12:38:17 +01002927 String mediaSource = "";
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002928
Ben Murdoch6af492a2010-06-15 12:38:17 +01002929 // We add the camera intent if there was no accept type (or '*/*' or 'image/*').
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002930 boolean addCameraIntent = true;
Ben Murdoch6af492a2010-06-15 12:38:17 +01002931 // We add the camcorder intent if there was no accept type (or '*/*' or 'video/*').
2932 boolean addCamcorderIntent = true;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002933
Ben Murdoch6af492a2010-06-15 12:38:17 +01002934 if (mUploadMessage != null) {
2935 // Already a file picker operation in progress.
2936 return;
2937 }
2938
Grace Kloba22ac16e2009-10-07 18:00:23 -07002939 mUploadMessage = uploadMsg;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002940
2941 // Parse the accept type.
2942 String params[] = acceptType.split(";");
2943 String mimeType = params[0];
2944
2945 for (String p : params) {
2946 String[] keyValue = p.split("=");
2947 if (keyValue.length == 2) {
2948 // Process key=value parameters.
Ben Murdoch6af492a2010-06-15 12:38:17 +01002949 if (mediaSourceKey.equals(keyValue[0])) {
2950 mediaSource = keyValue[1];
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002951 }
2952 }
2953 }
2954
2955 // This intent will display the standard OPENABLE file picker.
Grace Kloba22ac16e2009-10-07 18:00:23 -07002956 Intent i = new Intent(Intent.ACTION_GET_CONTENT);
2957 i.addCategory(Intent.CATEGORY_OPENABLE);
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002958
2959 // Create an intent to add to the standard file picker that will
2960 // capture an image from the camera. We'll combine this intent with
2961 // the standard OPENABLE picker unless the web developer specifically
2962 // requested the camera or gallery be opened by passing a parameter
2963 // in the accept type.
2964 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
2965 File externalDataDir = Environment.getExternalStoragePublicDirectory(
2966 Environment.DIRECTORY_DCIM);
2967 File cameraDataDir = new File(externalDataDir.getAbsolutePath() +
2968 File.separator + "browser-photos");
2969 cameraDataDir.mkdirs();
2970 mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator +
2971 System.currentTimeMillis() + ".jpg";
2972 cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCameraFilePath)));
2973
Ben Murdoch6af492a2010-06-15 12:38:17 +01002974 Intent camcorderIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
2975
Ben Murdoch039bd472010-07-21 21:26:57 +01002976 Intent soundRecIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
2977
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002978 if (mimeType.equals(imageMimeType)) {
2979 i.setType(imageMimeType);
Ben Murdoch6af492a2010-06-15 12:38:17 +01002980 addCamcorderIntent = false;
2981 if (mediaSource.equals(mediaSourceValueCamera)) {
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002982 // Specified 'image/*' and requested the camera, so go ahead and launch the camera
2983 // directly.
2984 BrowserActivity.this.startActivityForResult(cameraIntent, FILE_SELECTED);
2985 return;
Ben Murdochb50d7402010-07-16 12:42:22 +01002986 } else if (mediaSource.equals(mediaSourceValueFileSystem)) {
2987 // Specified filesytem as the source, so don't want to consider the camera.
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01002988 addCameraIntent = false;
2989 }
Ben Murdoch6af492a2010-06-15 12:38:17 +01002990 } else if (mimeType.equals(videoMimeType)) {
2991 i.setType(videoMimeType);
2992 addCameraIntent = false;
2993 // The camcorder saves it's own file and returns it to us in the intent, so
2994 // we don't need to generate one here.
2995 mCameraFilePath = null;
2996
2997 if (mediaSource.equals(mediaSourceValueCamcorder)) {
Ben Murdoch039bd472010-07-21 21:26:57 +01002998 // Specified 'video/*' and requested the camcorder, so go ahead and launch the
2999 // camcorder directly.
Ben Murdoch6af492a2010-06-15 12:38:17 +01003000 BrowserActivity.this.startActivityForResult(camcorderIntent, FILE_SELECTED);
3001 return;
Ben Murdochb50d7402010-07-16 12:42:22 +01003002 } else if (mediaSource.equals(mediaSourceValueFileSystem)) {
3003 // Specified filesystem as the source, so don't want to consider the camcorder.
Ben Murdoch6af492a2010-06-15 12:38:17 +01003004 addCamcorderIntent = false;
3005 }
Ben Murdoch039bd472010-07-21 21:26:57 +01003006 } else if (mimeType.equals(audioMimeType)) {
3007 i.setType(audioMimeType);
3008 addCameraIntent = false;
3009 addCamcorderIntent = false;
3010 if (mediaSource.equals(mediaSourceValueMicrophone)) {
3011 // Specified 'audio/*' and requested microphone, so go ahead and launch the sound
3012 // recorder.
3013 BrowserActivity.this.startActivityForResult(soundRecIntent, FILE_SELECTED);
3014 return;
3015 }
3016 // On a default system, there is no single option to open an audio "gallery". Both the
3017 // sound recorder and music browser respond to the OPENABLE/audio/* intent unlike the
3018 // image/* and video/* OPENABLE intents where the image / video gallery are the only
3019 // respondants (and so the user is not prompted by default).
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003020 } else {
3021 i.setType("*/*");
3022 }
3023
Ben Murdoch6af492a2010-06-15 12:38:17 +01003024 // Combine the chooser and the extra choices (like camera or camcorder)
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003025 Intent chooser = new Intent(Intent.ACTION_CHOOSER);
3026 chooser.putExtra(Intent.EXTRA_INTENT, i);
3027
Ben Murdoch6af492a2010-06-15 12:38:17 +01003028 Vector<Intent> extraInitialIntents = new Vector<Intent>(0);
3029
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003030 if (addCameraIntent) {
Ben Murdoch6af492a2010-06-15 12:38:17 +01003031 extraInitialIntents.add(cameraIntent);
3032 }
3033
3034 if (addCamcorderIntent) {
3035 extraInitialIntents.add(camcorderIntent);
3036 }
3037
3038 if (extraInitialIntents.size() > 0) {
3039 Intent[] extraIntents = new Intent[extraInitialIntents.size()];
3040 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraInitialIntents.toArray(extraIntents));
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003041 }
3042
3043 chooser.putExtra(Intent.EXTRA_TITLE, getString(R.string.choose_upload));
3044 BrowserActivity.this.startActivityForResult(chooser, FILE_SELECTED);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003045 }
3046
3047 // -------------------------------------------------------------------------
3048 // Implement functions for DownloadListener
3049 // -------------------------------------------------------------------------
3050
The Android Open Source Project0c908882009-03-03 19:32:16 -08003051 /**
3052 * Notify the host application a download should be done, or that
3053 * the data should be streamed if a streaming viewer is available.
3054 * @param url The full url to the content that should be downloaded
3055 * @param contentDisposition Content-disposition http header, if
3056 * present.
3057 * @param mimetype The mimetype of the content reported by the server
3058 * @param contentLength The file size reported by the server
3059 */
3060 public void onDownloadStart(String url, String userAgent,
3061 String contentDisposition, String mimetype, long contentLength) {
3062 // if we're dealing wih A/V content that's not explicitly marked
3063 // for download, check if it's streamable.
3064 if (contentDisposition == null
Patrick Scotte1fb9662009-08-31 14:31:52 -04003065 || !contentDisposition.regionMatches(
3066 true, 0, "attachment", 0, 10)) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003067 // query the package manager to see if there's a registered handler
3068 // that matches.
3069 Intent intent = new Intent(Intent.ACTION_VIEW);
3070 intent.setDataAndType(Uri.parse(url), mimetype);
Patrick Scotte1fb9662009-08-31 14:31:52 -04003071 ResolveInfo info = getPackageManager().resolveActivity(intent,
3072 PackageManager.MATCH_DEFAULT_ONLY);
3073 if (info != null) {
3074 ComponentName myName = getComponentName();
3075 // If we resolved to ourselves, we don't want to attempt to
3076 // load the url only to try and download it again.
3077 if (!myName.getPackageName().equals(
3078 info.activityInfo.packageName)
3079 || !myName.getClassName().equals(
3080 info.activityInfo.name)) {
3081 // someone (other than us) knows how to handle this mime
3082 // type with this scheme, don't download.
3083 try {
3084 startActivity(intent);
3085 return;
3086 } catch (ActivityNotFoundException ex) {
3087 if (LOGD_ENABLED) {
3088 Log.d(LOGTAG, "activity not found for " + mimetype
3089 + " over " + Uri.parse(url).getScheme(),
3090 ex);
3091 }
3092 // Best behavior is to fall back to a download in this
3093 // case
The Android Open Source Project0c908882009-03-03 19:32:16 -08003094 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003095 }
3096 }
3097 }
3098 onDownloadStartNoStream(url, userAgent, contentDisposition, mimetype, contentLength);
3099 }
3100
Kristian Monsenfa52d172010-03-25 18:29:21 +00003101 // This is to work around the fact that java.net.URI throws Exceptions
3102 // instead of just encoding URL's properly
3103 // Helper method for onDownloadStartNoStream
3104 private static String encodePath(String path) {
3105 char[] chars = path.toCharArray();
3106
3107 boolean needed = false;
3108 for (char c : chars) {
3109 if (c == '[' || c == ']') {
3110 needed = true;
3111 break;
3112 }
3113 }
3114 if (needed == false) {
3115 return path;
3116 }
3117
3118 StringBuilder sb = new StringBuilder("");
3119 for (char c : chars) {
3120 if (c == '[' || c == ']') {
3121 sb.append('%');
3122 sb.append(Integer.toHexString(c));
3123 } else {
3124 sb.append(c);
3125 }
3126 }
3127
3128 return sb.toString();
3129 }
3130
The Android Open Source Project0c908882009-03-03 19:32:16 -08003131 /**
3132 * Notify the host application a download should be done, even if there
3133 * is a streaming viewer available for thise type.
3134 * @param url The full url to the content that should be downloaded
3135 * @param contentDisposition Content-disposition http header, if
3136 * present.
3137 * @param mimetype The mimetype of the content reported by the server
3138 * @param contentLength The file size reported by the server
3139 */
3140 /*package */ void onDownloadStartNoStream(String url, String userAgent,
3141 String contentDisposition, String mimetype, long contentLength) {
3142
3143 String filename = URLUtil.guessFileName(url,
3144 contentDisposition, mimetype);
3145
3146 // Check to see if we have an SDCard
3147 String status = Environment.getExternalStorageState();
3148 if (!status.equals(Environment.MEDIA_MOUNTED)) {
3149 int title;
3150 String msg;
3151
3152 // Check to see if the SDCard is busy, same as the music app
3153 if (status.equals(Environment.MEDIA_SHARED)) {
3154 msg = getString(R.string.download_sdcard_busy_dlg_msg);
3155 title = R.string.download_sdcard_busy_dlg_title;
3156 } else {
3157 msg = getString(R.string.download_no_sdcard_dlg_msg, filename);
3158 title = R.string.download_no_sdcard_dlg_title;
3159 }
3160
3161 new AlertDialog.Builder(this)
3162 .setTitle(title)
3163 .setIcon(android.R.drawable.ic_dialog_alert)
3164 .setMessage(msg)
3165 .setPositiveButton(R.string.ok, null)
3166 .show();
3167 return;
3168 }
3169
Kristian Monsenfa52d172010-03-25 18:29:21 +00003170 // java.net.URI is a lot stricter than KURL so we have to encode some
3171 // extra characters. Fix for b 2538060 and b 1634719
3172 WebAddress webAddress;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003173 try {
Kristian Monsenfa52d172010-03-25 18:29:21 +00003174 webAddress = new WebAddress(url);
Bjorn Bringert131ab512010-10-12 16:25:47 +01003175 webAddress.setPath(encodePath(webAddress.getPath()));
The Android Open Source Project0c908882009-03-03 19:32:16 -08003176 } catch (Exception e) {
Kristian Monsenfa52d172010-03-25 18:29:21 +00003177 // This only happens for very bad urls, we want to chatch the
3178 // exception here
3179 Log.e(LOGTAG, "Exception trying to parse url:" + url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003180 return;
3181 }
3182
Leon Scrogginsc6076532010-10-22 15:06:59 -04003183 String addressString = webAddress.toString();
3184 Uri uri = Uri.parse(addressString);
3185 DownloadManager.Request request = new DownloadManager.Request(uri);
3186 request.setMimeType(mimetype);
3187 request.setDestinationInExternalFilesDir(this, null, filename);
Vasu Nori1d88aa02010-11-04 18:16:10 -07003188 // let this downloaded file be scanned by MediaScanner - so that it can show up
3189 // in Gallery app, for example.
3190 request.allowScanningByMediaScanner();
Leon Scrogginsc6076532010-10-22 15:06:59 -04003191 request.setDescription(webAddress.getHost());
Vasu Noric54d3282010-10-29 13:31:09 -07003192 String cookies = CookieManager.getInstance().getCookie(url);
3193 request.addRequestHeader("cookie", cookies);
Leon Scroggins09ccfc72010-10-28 16:22:24 -04003194 request.setNotificationVisibility(
3195 DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003196 if (mimetype == null) {
Leon Scrogginsc6076532010-10-22 15:06:59 -04003197 ContentValues values = new ContentValues();
3198 values.put(FetchUrlMimeType.URI, addressString);
3199 // XXX: Have to use the old url since the cookies were stored using the
3200 // old percent-encoded url.
Leon Scrogginsc6076532010-10-22 15:06:59 -04003201 values.put(FetchUrlMimeType.COOKIE_DATA, cookies);
3202 values.put(FetchUrlMimeType.USER_AGENT, userAgent);
3203
The Android Open Source Project0c908882009-03-03 19:32:16 -08003204 // We must have long pressed on a link or image to download it. We
3205 // are not sure of the mimetype in this case, so do a head request
Leon Scrogginsc6076532010-10-22 15:06:59 -04003206 new FetchUrlMimeType(this, request).execute(values);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003207 } else {
Leon Scrogginsc6076532010-10-22 15:06:59 -04003208 DownloadManager manager
3209 = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
3210 manager.enqueue(request);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003211 }
Leon Scroggins9191a7f2010-03-18 14:44:44 -04003212 Toast.makeText(this, R.string.download_pending, Toast.LENGTH_SHORT)
3213 .show();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003214 }
3215
Grace Kloba22ac16e2009-10-07 18:00:23 -07003216 // -------------------------------------------------------------------------
3217
The Android Open Source Project0c908882009-03-03 19:32:16 -08003218 /**
3219 * Resets the lock icon. This method is called when we start a new load and
3220 * know the url to be loaded.
3221 */
3222 private void resetLockIcon(String url) {
3223 // Save the lock-icon state (we revert to it if the load gets cancelled)
Grace Kloba22ac16e2009-10-07 18:00:23 -07003224 mTabControl.getCurrentTab().resetLockIcon(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003225 updateLockIconImage(LOCK_ICON_UNSECURE);
3226 }
3227
The Android Open Source Project0c908882009-03-03 19:32:16 -08003228 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003229 * Update the lock icon to correspond to our latest state.
3230 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003231 private void updateLockIconToLatest() {
Leon Scroggins91f65fc2010-06-29 13:52:39 -04003232 Tab t = mTabControl.getCurrentTab();
3233 if (t != null) {
3234 updateLockIconImage(t.getLockIconType());
3235 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -04003236 }
3237
3238 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -08003239 * Updates the lock-icon image in the title-bar.
3240 */
3241 private void updateLockIconImage(int lockIconType) {
3242 Drawable d = null;
3243 if (lockIconType == LOCK_ICON_SECURE) {
3244 d = mSecLockIcon;
3245 } else if (lockIconType == LOCK_ICON_MIXED) {
3246 d = mMixLockIcon;
3247 }
Leon Scroggins68579392009-09-15 15:31:54 -04003248 mTitleBar.setLock(d);
Michael Kolba2b2ba82010-08-04 17:54:03 -07003249 mFakeTitleBar.setLock(d);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003250 }
3251
3252 /**
3253 * Displays a page-info dialog.
3254 * @param tab The tab to show info about
3255 * @param fromShowSSLCertificateOnError The flag that indicates whether
3256 * this dialog was opened from the SSL-certificate-on-error dialog or
3257 * not. This is important, since we need to know whether to return to
3258 * the parent dialog or simply dismiss.
3259 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003260 private void showPageInfo(final Tab tab,
The Android Open Source Project0c908882009-03-03 19:32:16 -08003261 final boolean fromShowSSLCertificateOnError) {
3262 final LayoutInflater factory = LayoutInflater
3263 .from(this);
3264
3265 final View pageInfoView = factory.inflate(R.layout.page_info, null);
3266
3267 final WebView view = tab.getWebView();
3268
3269 String url = null;
3270 String title = null;
3271
3272 if (view == null) {
3273 url = tab.getUrl();
3274 title = tab.getTitle();
3275 } else if (view == mTabControl.getCurrentWebView()) {
3276 // Use the cached title and url if this is the current WebView
3277 url = mUrl;
3278 title = mTitle;
3279 } else {
3280 url = view.getUrl();
3281 title = view.getTitle();
3282 }
3283
3284 if (url == null) {
3285 url = "";
3286 }
3287 if (title == null) {
3288 title = "";
3289 }
3290
3291 ((TextView) pageInfoView.findViewById(R.id.address)).setText(url);
3292 ((TextView) pageInfoView.findViewById(R.id.title)).setText(title);
3293
3294 mPageInfoView = tab;
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05003295 mPageInfoFromShowSSLCertificateOnError = fromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003296
3297 AlertDialog.Builder alertDialogBuilder =
3298 new AlertDialog.Builder(this)
3299 .setTitle(R.string.page_info).setIcon(android.R.drawable.ic_dialog_info)
3300 .setView(pageInfoView)
3301 .setPositiveButton(
3302 R.string.ok,
3303 new DialogInterface.OnClickListener() {
3304 public void onClick(DialogInterface dialog,
3305 int whichButton) {
3306 mPageInfoDialog = null;
3307 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003308
3309 // if we came here from the SSL error dialog
3310 if (fromShowSSLCertificateOnError) {
3311 // go back to the SSL error dialog
3312 showSSLCertificateOnError(
3313 mSSLCertificateOnErrorView,
3314 mSSLCertificateOnErrorHandler,
3315 mSSLCertificateOnErrorError);
3316 }
3317 }
3318 })
3319 .setOnCancelListener(
3320 new DialogInterface.OnCancelListener() {
3321 public void onCancel(DialogInterface dialog) {
3322 mPageInfoDialog = null;
3323 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003324
3325 // if we came here from the SSL error dialog
3326 if (fromShowSSLCertificateOnError) {
3327 // go back to the SSL error dialog
3328 showSSLCertificateOnError(
3329 mSSLCertificateOnErrorView,
3330 mSSLCertificateOnErrorHandler,
3331 mSSLCertificateOnErrorError);
3332 }
3333 }
3334 });
3335
3336 // if we have a main top-level page SSL certificate set or a certificate
3337 // error
3338 if (fromShowSSLCertificateOnError ||
3339 (view != null && view.getCertificate() != null)) {
3340 // add a 'View Certificate' button
3341 alertDialogBuilder.setNeutralButton(
3342 R.string.view_certificate,
3343 new DialogInterface.OnClickListener() {
3344 public void onClick(DialogInterface dialog,
3345 int whichButton) {
3346 mPageInfoDialog = null;
3347 mPageInfoView = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003348
3349 // if we came here from the SSL error dialog
3350 if (fromShowSSLCertificateOnError) {
3351 // go back to the SSL error dialog
3352 showSSLCertificateOnError(
3353 mSSLCertificateOnErrorView,
3354 mSSLCertificateOnErrorHandler,
3355 mSSLCertificateOnErrorError);
3356 } else {
3357 // otherwise, display the top-most certificate from
3358 // the chain
3359 if (view.getCertificate() != null) {
3360 showSSLCertificate(tab);
3361 }
3362 }
3363 }
3364 });
3365 }
3366
3367 mPageInfoDialog = alertDialogBuilder.show();
3368 }
3369
3370 /**
3371 * Displays the main top-level page SSL certificate dialog
3372 * (accessible from the Page-Info dialog).
3373 * @param tab The tab to show certificate for.
3374 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003375 private void showSSLCertificate(final Tab tab) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003376 final View certificateView =
3377 inflateCertificateView(tab.getWebView().getCertificate());
3378 if (certificateView == null) {
3379 return;
3380 }
3381
3382 LayoutInflater factory = LayoutInflater.from(this);
3383
3384 final LinearLayout placeholder =
3385 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3386
3387 LinearLayout ll = (LinearLayout) factory.inflate(
3388 R.layout.ssl_success, placeholder);
3389 ((TextView)ll.findViewById(R.id.success))
3390 .setText(R.string.ssl_certificate_is_valid);
3391
3392 mSSLCertificateView = tab;
3393 mSSLCertificateDialog =
3394 new AlertDialog.Builder(this)
3395 .setTitle(R.string.ssl_certificate).setIcon(
3396 R.drawable.ic_dialog_browser_certificate_secure)
3397 .setView(certificateView)
3398 .setPositiveButton(R.string.ok,
3399 new DialogInterface.OnClickListener() {
3400 public void onClick(DialogInterface dialog,
3401 int whichButton) {
3402 mSSLCertificateDialog = null;
3403 mSSLCertificateView = null;
3404
3405 showPageInfo(tab, false);
3406 }
3407 })
3408 .setOnCancelListener(
3409 new DialogInterface.OnCancelListener() {
3410 public void onCancel(DialogInterface dialog) {
3411 mSSLCertificateDialog = null;
3412 mSSLCertificateView = null;
3413
3414 showPageInfo(tab, false);
3415 }
3416 })
3417 .show();
3418 }
3419
3420 /**
3421 * Displays the SSL error certificate dialog.
3422 * @param view The target web-view.
3423 * @param handler The SSL error handler responsible for cancelling the
3424 * connection that resulted in an SSL error or proceeding per user request.
3425 * @param error The SSL error object.
3426 */
Grace Kloba22ac16e2009-10-07 18:00:23 -07003427 void showSSLCertificateOnError(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003428 final WebView view, final SslErrorHandler handler, final SslError error) {
3429
3430 final View certificateView =
3431 inflateCertificateView(error.getCertificate());
3432 if (certificateView == null) {
3433 return;
3434 }
3435
3436 LayoutInflater factory = LayoutInflater.from(this);
3437
3438 final LinearLayout placeholder =
3439 (LinearLayout)certificateView.findViewById(R.id.placeholder);
3440
3441 if (error.hasError(SslError.SSL_UNTRUSTED)) {
3442 LinearLayout ll = (LinearLayout)factory
3443 .inflate(R.layout.ssl_warning, placeholder);
3444 ((TextView)ll.findViewById(R.id.warning))
3445 .setText(R.string.ssl_untrusted);
3446 }
3447
3448 if (error.hasError(SslError.SSL_IDMISMATCH)) {
3449 LinearLayout ll = (LinearLayout)factory
3450 .inflate(R.layout.ssl_warning, placeholder);
3451 ((TextView)ll.findViewById(R.id.warning))
3452 .setText(R.string.ssl_mismatch);
3453 }
3454
3455 if (error.hasError(SslError.SSL_EXPIRED)) {
3456 LinearLayout ll = (LinearLayout)factory
3457 .inflate(R.layout.ssl_warning, placeholder);
3458 ((TextView)ll.findViewById(R.id.warning))
3459 .setText(R.string.ssl_expired);
3460 }
3461
3462 if (error.hasError(SslError.SSL_NOTYETVALID)) {
3463 LinearLayout ll = (LinearLayout)factory
3464 .inflate(R.layout.ssl_warning, placeholder);
3465 ((TextView)ll.findViewById(R.id.warning))
3466 .setText(R.string.ssl_not_yet_valid);
3467 }
3468
3469 mSSLCertificateOnErrorHandler = handler;
3470 mSSLCertificateOnErrorView = view;
3471 mSSLCertificateOnErrorError = error;
3472 mSSLCertificateOnErrorDialog =
3473 new AlertDialog.Builder(this)
3474 .setTitle(R.string.ssl_certificate).setIcon(
3475 R.drawable.ic_dialog_browser_certificate_partially_secure)
3476 .setView(certificateView)
3477 .setPositiveButton(R.string.ok,
3478 new DialogInterface.OnClickListener() {
3479 public void onClick(DialogInterface dialog,
3480 int whichButton) {
3481 mSSLCertificateOnErrorDialog = null;
3482 mSSLCertificateOnErrorView = null;
3483 mSSLCertificateOnErrorHandler = null;
3484 mSSLCertificateOnErrorError = null;
3485
Grace Kloba22ac16e2009-10-07 18:00:23 -07003486 view.getWebViewClient().onReceivedSslError(
3487 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003488 }
3489 })
3490 .setNeutralButton(R.string.page_info_view,
3491 new DialogInterface.OnClickListener() {
3492 public void onClick(DialogInterface dialog,
3493 int whichButton) {
3494 mSSLCertificateOnErrorDialog = null;
3495
3496 // do not clear the dialog state: we will
3497 // need to show the dialog again once the
3498 // user is done exploring the page-info details
3499
3500 showPageInfo(mTabControl.getTabFromView(view),
3501 true);
3502 }
3503 })
3504 .setOnCancelListener(
3505 new DialogInterface.OnCancelListener() {
3506 public void onCancel(DialogInterface dialog) {
3507 mSSLCertificateOnErrorDialog = null;
3508 mSSLCertificateOnErrorView = null;
3509 mSSLCertificateOnErrorHandler = null;
3510 mSSLCertificateOnErrorError = null;
3511
Grace Kloba22ac16e2009-10-07 18:00:23 -07003512 view.getWebViewClient().onReceivedSslError(
3513 view, handler, error);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003514 }
3515 })
3516 .show();
3517 }
3518
3519 /**
3520 * Inflates the SSL certificate view (helper method).
3521 * @param certificate The SSL certificate.
3522 * @return The resultant certificate view with issued-to, issued-by,
3523 * issued-on, expires-on, and possibly other fields set.
3524 * If the input certificate is null, returns null.
3525 */
3526 private View inflateCertificateView(SslCertificate certificate) {
3527 if (certificate == null) {
3528 return null;
3529 }
3530
3531 LayoutInflater factory = LayoutInflater.from(this);
3532
3533 View certificateView = factory.inflate(
3534 R.layout.ssl_certificate, null);
3535
3536 // issued to:
3537 SslCertificate.DName issuedTo = certificate.getIssuedTo();
3538 if (issuedTo != null) {
3539 ((TextView) certificateView.findViewById(R.id.to_common))
3540 .setText(issuedTo.getCName());
3541 ((TextView) certificateView.findViewById(R.id.to_org))
3542 .setText(issuedTo.getOName());
3543 ((TextView) certificateView.findViewById(R.id.to_org_unit))
3544 .setText(issuedTo.getUName());
3545 }
3546
3547 // issued by:
3548 SslCertificate.DName issuedBy = certificate.getIssuedBy();
3549 if (issuedBy != null) {
3550 ((TextView) certificateView.findViewById(R.id.by_common))
3551 .setText(issuedBy.getCName());
3552 ((TextView) certificateView.findViewById(R.id.by_org))
3553 .setText(issuedBy.getOName());
3554 ((TextView) certificateView.findViewById(R.id.by_org_unit))
3555 .setText(issuedBy.getUName());
3556 }
3557
3558 // issued on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003559 String issuedOn = formatCertificateDate(
3560 certificate.getValidNotBeforeDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003561 ((TextView) certificateView.findViewById(R.id.issued_on))
3562 .setText(issuedOn);
3563
3564 // expires on:
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003565 String expiresOn = formatCertificateDate(
3566 certificate.getValidNotAfterDate());
The Android Open Source Project0c908882009-03-03 19:32:16 -08003567 ((TextView) certificateView.findViewById(R.id.expires_on))
3568 .setText(expiresOn);
3569
3570 return certificateView;
3571 }
3572
3573 /**
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003574 * Formats the certificate date to a properly localized date string.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003575 * @return Properly localized version of the certificate date string and
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003576 * the "" if it fails to localize.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003577 */
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003578 private String formatCertificateDate(Date certificateDate) {
3579 if (certificateDate == null) {
3580 return "";
The Android Open Source Project0c908882009-03-03 19:32:16 -08003581 }
Brian Carlstrom95a2e4a2010-03-02 10:02:34 -08003582 String formattedDate = DateFormat.getDateFormat(this).format(certificateDate);
3583 if (formattedDate == null) {
3584 return "";
3585 }
3586 return formattedDate;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003587 }
3588
3589 /**
3590 * Displays an http-authentication dialog.
3591 */
Bjorn Bringert25738922010-10-12 10:56:20 +01003592 void showHttpAuthentication(final HttpAuthHandler handler, String host, String realm) {
3593 mHttpAuthenticationDialog = new HttpAuthenticationDialog(this, host, realm);
3594 mHttpAuthenticationDialog.setOkListener(new HttpAuthenticationDialog.OkListener() {
3595 public void onOk(String host, String realm, String username, String password) {
3596 BrowserActivity.this.setHttpAuthUsernamePassword(host, realm, username, password);
3597 handler.proceed(username, password);
3598 mHttpAuthenticationDialog = null;
3599 }
3600 });
3601 mHttpAuthenticationDialog.setCancelListener(new HttpAuthenticationDialog.CancelListener() {
3602 public void onCancel() {
3603 handler.cancel();
3604 BrowserActivity.this.resetTitleAndRevertLockIcon();
3605 mHttpAuthenticationDialog = null;
3606 }
3607 });
3608 mHttpAuthenticationDialog.show();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003609 }
3610
3611 public int getProgress() {
3612 WebView w = mTabControl.getCurrentWebView();
3613 if (w != null) {
3614 return w.getProgress();
3615 } else {
3616 return 100;
3617 }
3618 }
3619
3620 /**
3621 * Set HTTP authentication password.
3622 *
3623 * @param host The host for the password
3624 * @param realm The realm for the password
3625 * @param username The username for the password. If it is null, it means
3626 * password can't be saved.
3627 * @param password The password
3628 */
3629 public void setHttpAuthUsernamePassword(String host, String realm,
3630 String username,
3631 String password) {
Steve Block95a53b22010-03-25 17:24:58 +00003632 WebView w = getTopWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003633 if (w != null) {
3634 w.setHttpAuthUsernamePassword(host, realm, username, password);
3635 }
3636 }
3637
3638 /**
3639 * connectivity manager says net has come or gone... inform the user
3640 * @param up true if net has come up, false if net has gone down
3641 */
3642 public void onNetworkToggle(boolean up) {
3643 if (up == mIsNetworkUp) {
3644 return;
3645 } else if (up) {
3646 mIsNetworkUp = true;
3647 if (mAlertDialog != null) {
3648 mAlertDialog.cancel();
3649 mAlertDialog = null;
3650 }
3651 } else {
3652 mIsNetworkUp = false;
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003653 if (mInLoad) {
3654 createAndShowNetworkDialog();
3655 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003656 }
3657 WebView w = mTabControl.getCurrentWebView();
3658 if (w != null) {
3659 w.setNetworkAvailable(up);
3660 }
3661 }
3662
Grace Kloba22ac16e2009-10-07 18:00:23 -07003663 boolean isNetworkUp() {
3664 return mIsNetworkUp;
3665 }
3666
Patrick Scotteb6ab2a2009-09-16 10:00:17 -04003667 // This method shows the network dialog alerting the user that the net is
3668 // down. It will only show the dialog if mAlertDialog is null.
3669 private void createAndShowNetworkDialog() {
3670 if (mAlertDialog == null) {
3671 mAlertDialog = new AlertDialog.Builder(this)
3672 .setTitle(R.string.loadSuspendedTitle)
3673 .setMessage(R.string.loadSuspended)
3674 .setPositiveButton(R.string.ok, null)
3675 .show();
3676 }
3677 }
3678
Michael Kolbe421c242010-10-04 19:29:01 -07003679 /**
3680 * callback from ComboPage when bookmark/history selection
3681 */
3682 @Override
3683 public void onUrlSelected(String url, boolean newTab) {
3684 removeComboView();
3685 if (!TextUtils.isEmpty(url)) {
3686 if (newTab) {
3687 openTab(url, false);
3688 } else {
3689 final Tab currentTab = mTabControl.getCurrentTab();
3690 dismissSubWindow(currentTab);
3691 loadUrl(getTopWindow(), url);
3692 }
3693 }
3694 }
3695
3696 /**
3697 * callback from ComboPage when dismissed
3698 */
3699 @Override
3700 public void onComboCanceled() {
3701 removeComboView();
3702 }
3703
3704 /**
3705 * dismiss the ComboPage
3706 */
3707 /* package */ void removeComboView() {
3708 if (mComboView != null) {
3709 mContentView.removeView(mComboView);
3710 mTitleBar.setVisibility(View.VISIBLE);
3711 mMenuState = R.id.MAIN_MENU;
3712 attachTabToContentView(mTabControl.getCurrentTab());
3713 getTopWindow().requestFocus();
3714 mComboView = null;
3715 }
3716 }
3717
3718 /**
3719 * callback from ComboPage when clear history is requested
3720 */
3721 public void onRemoveParentChildRelationships() {
3722 mTabControl.removeParentChildRelationShips();
3723 }
3724
The Android Open Source Project0c908882009-03-03 19:32:16 -08003725 @Override
3726 protected void onActivityResult(int requestCode, int resultCode,
3727 Intent intent) {
Grace Klobabb394f32009-11-19 10:26:37 -08003728 if (getTopWindow() == null) return;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003729 switch (requestCode) {
Leon Scrogginsfde97462010-01-11 13:06:21 -05003730 case PREFERENCES_PAGE:
3731 if (resultCode == RESULT_OK && intent != null) {
3732 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
3733 if (BrowserSettings.PREF_CLEAR_HISTORY.equals(action)) {
3734 mTabControl.removeParentChildRelationShips();
3735 }
3736 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08003737 break;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003738 // Choose a file from the file picker.
3739 case FILE_SELECTED:
3740 if (null == mUploadMessage) break;
3741 Uri result = intent == null || resultCode != RESULT_OK ? null
3742 : intent.getData();
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003743
3744 // As we ask the camera to save the result of the user taking
3745 // a picture, the camera application does not return anything other
3746 // than RESULT_OK. So we need to check whether the file we expected
3747 // was written to disk in the in the case that we
3748 // did not get an intent returned but did get a RESULT_OK. If it was,
3749 // we assume that this result has came back from the camera.
3750 if (result == null && intent == null && resultCode == RESULT_OK) {
3751 File cameraFile = new File(mCameraFilePath);
3752 if (cameraFile.exists()) {
3753 result = Uri.fromFile(cameraFile);
Ben Murdoch07d34732010-05-27 18:34:46 +01003754 // Broadcast to the media scanner that we have a new photo
3755 // so it will be added into the gallery for the user.
3756 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003757 }
3758 }
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003759 mUploadMessage.onReceiveValue(result);
3760 mUploadMessage = null;
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01003761 mCameraFilePath = null;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04003762 break;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003763 default:
3764 break;
3765 }
Leon Scroggins30444232009-09-04 18:36:20 -04003766 getTopWindow().requestFocus();
The Android Open Source Project0c908882009-03-03 19:32:16 -08003767 }
3768
3769 /*
3770 * This method is called as a result of the user selecting the options
Leon Scrogginsf08809b2010-01-21 09:27:46 -05003771 * menu to see the download window. It shows the download window on top of
3772 * the current window.
The Android Open Source Project0c908882009-03-03 19:32:16 -08003773 */
Leon Scroggins IIIa682a3c2010-09-27 12:32:40 -04003774 private void viewDownloads() {
3775 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
3776 startActivity(intent);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003777 }
3778
Leon Scroggins160a7e72009-08-14 18:28:01 -04003779 /**
3780 * Open the Go page.
3781 * @param startWithHistory If true, open starting on the history tab.
3782 * Otherwise, start with the bookmarks tab.
Leon Scroggins160a7e72009-08-14 18:28:01 -04003783 */
Michael Kolbe421c242010-10-04 19:29:01 -07003784 /* package */ void bookmarksOrHistoryPicker(boolean startWithHistory) {
Leon Scrogginsbc922852010-10-22 12:15:27 -04003785 if (mTabControl.getCurrentWebView() == null) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003786 return;
3787 }
Michael Kolbe421c242010-10-04 19:29:01 -07003788 Bundle extras = new Bundle();
Leon Scroggins30444232009-09-04 18:36:20 -04003789 // Disable opening in a new window if we have maxed out the windows
Leon Scrogginsbc922852010-10-22 12:15:27 -04003790 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
3791 !mTabControl.canCreateNewTab());
Michael Kolbe421c242010-10-04 19:29:01 -07003792
3793 mComboView = new CombinedBookmarkHistoryView(this,
3794 startWithHistory ? CombinedBookmarkHistoryView.FRAGMENT_ID_HISTORY
3795 : CombinedBookmarkHistoryView.FRAGMENT_ID_BOOKMARKS,
3796 extras);
3797 removeTabFromContentView(mTabControl.getCurrentTab());
3798 mTitleBar.setVisibility(View.GONE);
3799 hideFakeTitleBar();
3800 mContentView.addView(mComboView, COVER_SCREEN_PARAMS);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003801 }
3802
3803 // Called when loading from context menu or LOAD_URL message
Leon Scroggins92472e82010-02-17 16:32:28 -05003804 private void loadUrlFromContext(WebView view, String url) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003805 // In case the user enters nothing.
3806 if (url != null && url.length() != 0 && view != null) {
3807 url = smartUrlFilter(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07003808 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
Leon Scroggins92472e82010-02-17 16:32:28 -05003809 loadUrl(view, url);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003810 }
3811 }
3812 }
3813
Leon Scroggins92472e82010-02-17 16:32:28 -05003814 /**
3815 * Load the URL into the given WebView and update the title bar
3816 * to reflect the new load. Call this instead of WebView.loadUrl
3817 * directly.
3818 * @param view The WebView used to load url.
3819 * @param url The URL to load.
3820 */
3821 private void loadUrl(WebView view, String url) {
3822 updateTitleBarForNewLoad(view, url);
3823 view.loadUrl(url);
3824 }
3825
3826 /**
3827 * Load UrlData into a Tab and update the title bar to reflect the new
3828 * load. Call this instead of UrlData.loadIn directly.
3829 * @param t The Tab used to load.
3830 * @param data The UrlData being loaded.
3831 */
3832 private void loadUrlDataIn(Tab t, UrlData data) {
3833 updateTitleBarForNewLoad(t.getWebView(), data.mUrl);
3834 data.loadIn(t);
3835 }
3836
3837 /**
3838 * If the WebView is the top window, update the title bar to reflect
3839 * loading the new URL. i.e. set its text, clear the favicon (which
3840 * will be set once the page begins loading), and set the progress to
3841 * INITIAL_PROGRESS to show that the page has begun to load. Called
3842 * by loadUrl and loadUrlDataIn.
3843 * @param view The WebView that is starting a load.
3844 * @param url The URL that is being loaded.
3845 */
3846 private void updateTitleBarForNewLoad(WebView view, String url) {
3847 if (view == getTopWindow()) {
3848 setUrlTitle(url, null);
3849 setFavicon(null);
3850 onProgressChanged(view, INITIAL_PROGRESS);
3851 }
3852 }
3853
The Android Open Source Project0c908882009-03-03 19:32:16 -08003854 private String smartUrlFilter(Uri inUri) {
3855 if (inUri != null) {
3856 return smartUrlFilter(inUri.toString());
3857 }
3858 return null;
3859 }
3860
Feng Qianb34f87a2009-03-24 21:27:26 -07003861 protected static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile(
The Android Open Source Project0c908882009-03-03 19:32:16 -08003862 "(?i)" + // switch on case insensitive matching
3863 "(" + // begin group for schema
3864 "(?:http|https|file):\\/\\/" +
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07003865 "|(?:inline|data|about|content|javascript):" +
The Android Open Source Project0c908882009-03-03 19:32:16 -08003866 ")" +
3867 "(.*)" );
3868
3869 /**
3870 * Attempts to determine whether user input is a URL or search
3871 * terms. Anything with a space is passed to search.
3872 *
3873 * Converts to lowercase any mistakenly uppercased schema (i.e.,
3874 * "Http://" converts to "http://"
3875 *
3876 * @return Original or modified URL
3877 *
3878 */
3879 String smartUrlFilter(String url) {
3880
3881 String inUrl = url.trim();
3882 boolean hasSpace = inUrl.indexOf(' ') != -1;
3883
3884 Matcher matcher = ACCEPTED_URI_SCHEMA.matcher(inUrl);
3885 if (matcher.matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003886 // force scheme to lowercase
3887 String scheme = matcher.group(1);
3888 String lcScheme = scheme.toLowerCase();
3889 if (!lcScheme.equals(scheme)) {
Mitsuru Oshima123ecfb2009-05-18 19:11:14 -07003890 inUrl = lcScheme + matcher.group(2);
3891 }
3892 if (hasSpace) {
3893 inUrl = inUrl.replace(" ", "%20");
The Android Open Source Project0c908882009-03-03 19:32:16 -08003894 }
3895 return inUrl;
3896 }
Leon Scroggins956cf012010-10-07 13:51:57 -04003897 if (!hasSpace) {
Dan Egnor5ee906c2009-11-18 12:11:49 -08003898 if (Patterns.WEB_URL.matcher(inUrl).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08003899 return URLUtil.guessUrl(inUrl);
3900 }
3901 }
3902
Leon Scroggins956cf012010-10-07 13:51:57 -04003903 // FIXME: Is this the correct place to add to searches?
3904 // what if someone else calls this function?
3905
The Android Open Source Project0c908882009-03-03 19:32:16 -08003906 Browser.addSearchUrl(mResolver, inUrl);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07003907 return URLUtil.composeSearchUrl(inUrl, QuickSearch_G, QUERY_PLACE_HOLDER);
The Android Open Source Project0c908882009-03-03 19:32:16 -08003908 }
3909
Ben Murdochbff2d602009-07-01 20:19:05 +01003910 /* package */ void setShouldShowErrorConsole(boolean flag) {
3911 if (flag == mShouldShowErrorConsole) {
3912 // Nothing to do.
3913 return;
3914 }
Leon Scroggins IIIa9e35b62010-09-16 15:30:15 -04003915 Tab t = mTabControl.getCurrentTab();
3916 if (t == null) {
3917 // There is no current tab so we cannot toggle the error console
3918 return;
3919 }
Ben Murdochbff2d602009-07-01 20:19:05 +01003920
3921 mShouldShowErrorConsole = flag;
3922
Leon Scroggins IIIa9e35b62010-09-16 15:30:15 -04003923 ErrorConsoleView errorConsole = t.getErrorConsole(true);
Ben Murdochbff2d602009-07-01 20:19:05 +01003924
3925 if (flag) {
3926 // Setting the show state of the console will cause it's the layout to be inflated.
3927 if (errorConsole.numberOfErrors() > 0) {
3928 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
3929 } else {
3930 errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
3931 }
3932
3933 // Now we can add it to the main view.
3934 mErrorConsoleContainer.addView(errorConsole,
Romain Guy15b8ec62010-01-08 15:06:43 -08003935 new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
Ben Murdochbff2d602009-07-01 20:19:05 +01003936 ViewGroup.LayoutParams.WRAP_CONTENT));
3937 } else {
3938 mErrorConsoleContainer.removeView(errorConsole);
3939 }
3940
3941 }
3942
Grace Kloba22ac16e2009-10-07 18:00:23 -07003943 boolean shouldShowErrorConsole() {
3944 return mShouldShowErrorConsole;
3945 }
3946
Andrei Popescu163ab742009-10-20 17:58:23 +01003947 private void setStatusBarVisibility(boolean visible) {
3948 int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
3949 getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
3950 }
3951
Andrei Popescu56199cc2010-01-12 22:39:16 +00003952
3953 private void sendNetworkType(String type, String subtype) {
3954 WebView w = mTabControl.getCurrentWebView();
3955 if (w != null) {
3956 w.setNetworkType(type, subtype);
3957 }
3958 }
3959
Grace Klobaeb6eef42009-09-15 17:56:32 -07003960 final static int LOCK_ICON_UNSECURE = 0;
3961 final static int LOCK_ICON_SECURE = 1;
3962 final static int LOCK_ICON_MIXED = 2;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003963
The Android Open Source Project0c908882009-03-03 19:32:16 -08003964 private BrowserSettings mSettings;
3965 private TabControl mTabControl;
3966 private ContentResolver mResolver;
3967 private FrameLayout mContentView;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003968 private View mCustomView;
3969 private FrameLayout mCustomViewContainer;
Andrei Popescuc9b55562009-07-07 10:51:15 +01003970 private WebChromeClient.CustomViewCallback mCustomViewCallback;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003971
3972 // FIXME, temp address onPrepareMenu performance problem. When we move everything out of
3973 // view, we should rewrite this.
3974 private int mCurrentMenuState = 0;
3975 private int mMenuState = R.id.MAIN_MENU;
Andrei Popescuadc008d2009-06-26 14:11:30 +01003976 private int mOldMenuState = EMPTY_MENU;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003977 private static final int EMPTY_MENU = -1;
3978 private Menu mMenu;
3979
The Android Open Source Project0c908882009-03-03 19:32:16 -08003980 // Used to prevent chording to result in firing two shortcuts immediately
3981 // one after another. Fixes bug 1211714.
3982 boolean mCanChord;
3983
3984 private boolean mInLoad;
3985 private boolean mIsNetworkUp;
Ben Murdochb7cc8b42009-09-28 10:59:09 +01003986 private boolean mDidStopLoad;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003987
Cary Clark1f10cbf2010-03-22 11:45:23 -04003988 /* package */ boolean mActivityInPause = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -08003989
3990 private boolean mMenuIsDown;
3991
The Android Open Source Project0c908882009-03-03 19:32:16 -08003992 private static boolean mInTrace;
3993
3994 // Performance probe
3995 private static final int[] SYSTEM_CPU_FORMAT = new int[] {
3996 Process.PROC_SPACE_TERM | Process.PROC_COMBINE,
3997 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 1: user time
3998 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 2: nice time
3999 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 3: sys time
4000 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 4: idle time
4001 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 5: iowait time
4002 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG, // 6: irq time
4003 Process.PROC_SPACE_TERM | Process.PROC_OUT_LONG // 7: softirq time
4004 };
4005
4006 private long mStart;
4007 private long mProcessStart;
4008 private long mUserStart;
4009 private long mSystemStart;
4010 private long mIdleStart;
4011 private long mIrqStart;
4012
4013 private long mUiStart;
4014
4015 private Drawable mMixLockIcon;
4016 private Drawable mSecLockIcon;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004017
4018 /* hold a ref so we can auto-cancel if necessary */
4019 private AlertDialog mAlertDialog;
4020
The Android Open Source Project0c908882009-03-03 19:32:16 -08004021 // The up-to-date URL and title (these can be different from those stored
4022 // in WebView, since it takes some time for the information in WebView to
4023 // get updated)
4024 private String mUrl;
4025 private String mTitle;
4026
4027 // As PageInfo has different style for landscape / portrait, we have
4028 // to re-open it when configuration changed
4029 private AlertDialog mPageInfoDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07004030 private Tab mPageInfoView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004031 // If the Page-Info dialog is launched from the SSL-certificate-on-error
4032 // dialog, we should not just dismiss it, but should get back to the
4033 // SSL-certificate-on-error dialog. This flag is used to store this state
Leon Scrogginsc7b92f82010-01-11 18:17:31 -05004034 private boolean mPageInfoFromShowSSLCertificateOnError;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004035
4036 // as SSLCertificateOnError has different style for landscape / portrait,
4037 // we have to re-open it when configuration changed
4038 private AlertDialog mSSLCertificateOnErrorDialog;
4039 private WebView mSSLCertificateOnErrorView;
4040 private SslErrorHandler mSSLCertificateOnErrorHandler;
4041 private SslError mSSLCertificateOnErrorError;
4042
4043 // as SSLCertificate has different style for landscape / portrait, we
4044 // have to re-open it when configuration changed
4045 private AlertDialog mSSLCertificateDialog;
Grace Kloba22ac16e2009-10-07 18:00:23 -07004046 private Tab mSSLCertificateView;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004047
4048 // as HttpAuthentication has different style for landscape / portrait, we
4049 // have to re-open it when configuration changed
Bjorn Bringert25738922010-10-12 10:56:20 +01004050 private HttpAuthenticationDialog mHttpAuthenticationDialog;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004051
4052 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
4053 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08004054 ViewGroup.LayoutParams.MATCH_PARENT,
4055 ViewGroup.LayoutParams.MATCH_PARENT);
Andrei Popescuadc008d2009-06-26 14:11:30 +01004056 /*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
4057 new FrameLayout.LayoutParams(
Romain Guy15b8ec62010-01-08 15:06:43 -08004058 ViewGroup.LayoutParams.MATCH_PARENT,
4059 ViewGroup.LayoutParams.MATCH_PARENT,
Andrei Popescuadc008d2009-06-26 14:11:30 +01004060 Gravity.CENTER);
Grace Kloba47fdfdb2009-06-30 11:15:34 -07004061 // Google search
4062 final static String QuickSearch_G = "http://www.google.com/m?q=%s";
The Android Open Source Project0c908882009-03-03 19:32:16 -08004063
4064 final static String QUERY_PLACE_HOLDER = "%s";
4065
4066 // "source" parameter for Google search through search key
4067 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
4068 // "source" parameter for Google search through goto menu
4069 final static String GOOGLE_SEARCH_SOURCE_GOTO = "browser-goto";
4070 // "source" parameter for Google search through simplily type
4071 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
4072 // "source" parameter for Google search suggested by the browser
4073 final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
4074 // "source" parameter for Google search from unknown source
4075 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown";
4076
4077 private final static String LOGTAG = "browser";
4078
The Android Open Source Project0c908882009-03-03 19:32:16 -08004079 private String mLastEnteredUrl;
4080
4081 private PowerManager.WakeLock mWakeLock;
4082 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
4083
4084 private Toast mStopToast;
4085
Leon Scroggins571b3762010-05-26 10:25:01 -04004086 private TitleBarBase mTitleBar;
Michael Kolba2b2ba82010-08-04 17:54:03 -07004087 private TabBar mTabBar;
Leon Scroggins81db3662009-06-04 17:45:11 -04004088
Ben Murdochbff2d602009-07-01 20:19:05 +01004089 private LinearLayout mErrorConsoleContainer = null;
4090 private boolean mShouldShowErrorConsole = false;
4091
The Android Open Source Project0c908882009-03-03 19:32:16 -08004092 // As the ids are dynamically created, we can't guarantee that they will
4093 // be in sequence, so this static array maps ids to a window number.
4094 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
4095 { R.id.window_one_menu_id, R.id.window_two_menu_id, R.id.window_three_menu_id,
4096 R.id.window_four_menu_id, R.id.window_five_menu_id, R.id.window_six_menu_id,
4097 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
4098
4099 // monitor platform changes
4100 private IntentFilter mNetworkStateChangedFilter;
4101 private BroadcastReceiver mNetworkStateIntentReceiver;
4102
Bjorn Bringerta7611812010-03-24 11:12:02 +00004103 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
4104
The Android Open Source Project0c908882009-03-03 19:32:16 -08004105 // activity requestCode
Nicolas Roard78a98e42009-05-11 13:34:17 +01004106 final static int PREFERENCES_PAGE = 3;
Leon Scroggins8d5fa432009-10-02 15:55:59 -04004107 final static int FILE_SELECTED = 4;
The Android Open Source Project0c908882009-03-03 19:32:16 -08004108
Andrei Popescu540035d2009-09-18 18:59:20 +01004109 // the default <video> poster
4110 private Bitmap mDefaultVideoPoster;
4111 // the video progress view
4112 private View mVideoProgressView;
4113
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004114 /**
4115 * A UrlData class to abstract how the content will be set to WebView.
4116 * This base class uses loadUrl to show the content.
4117 */
Leon Scroggins6eac63e2010-03-15 18:19:14 -04004118 /* package */ static class UrlData {
Grace Kloba068e48b2010-01-26 18:11:27 -08004119 final String mUrl;
4120 final Map<String, String> mHeaders;
Leon Scroggins58d56c62010-01-28 15:12:40 -05004121 final Intent mVoiceIntent;
Grace Kloba60e095c2009-06-16 11:50:55 -07004122
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004123 UrlData(String url) {
4124 this.mUrl = url;
Grace Kloba068e48b2010-01-26 18:11:27 -08004125 this.mHeaders = null;
Leon Scroggins58d56c62010-01-28 15:12:40 -05004126 this.mVoiceIntent = null;
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004127 }
Grace Kloba60e095c2009-06-16 11:50:55 -07004128
Leon Scroggins58d56c62010-01-28 15:12:40 -05004129 UrlData(String url, Map<String, String> headers, Intent intent) {
Grace Kloba068e48b2010-01-26 18:11:27 -08004130 this.mUrl = url;
4131 this.mHeaders = headers;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -05004132 if (RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
4133 .equals(intent.getAction())) {
Leon Scroggins58d56c62010-01-28 15:12:40 -05004134 this.mVoiceIntent = intent;
4135 } else {
4136 this.mVoiceIntent = null;
4137 }
Grace Kloba60e095c2009-06-16 11:50:55 -07004138 }
4139
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004140 boolean isEmpty() {
Leon Scroggins58d56c62010-01-28 15:12:40 -05004141 return mVoiceIntent == null && (mUrl == null || mUrl.length() == 0);
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004142 }
4143
Leon Scroggins92472e82010-02-17 16:32:28 -05004144 /**
4145 * Load this UrlData into the given Tab. Use loadUrlDataIn to update
4146 * the title bar as well.
4147 */
Leon Scroggins58d56c62010-01-28 15:12:40 -05004148 public void loadIn(Tab t) {
4149 if (mVoiceIntent != null) {
4150 t.activateVoiceSearchMode(mVoiceIntent);
4151 } else {
4152 t.getWebView().loadUrl(mUrl, mHeaders);
4153 }
Mitsuru Oshima25ad8ab2009-06-10 16:26:07 -07004154 }
4155 };
4156
Leon Scroggins1f005d32009-08-10 17:36:42 -04004157 /* package */ static final UrlData EMPTY_URL_DATA = new UrlData(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08004158}