blob: b495cc0b3c18073370e64d6e35174f3875619968 [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 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 Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
20import android.app.DownloadManager;
21import android.app.SearchManager;
22import android.content.ClipboardManager;
23import android.content.ContentProvider;
24import android.content.ContentProviderClient;
25import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070026import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.content.ContentValues;
28import android.content.Context;
29import android.content.Intent;
30import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
32import android.content.res.Configuration;
Leon Scroggins1961ed22010-12-07 15:22:21 -050033import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import android.database.Cursor;
35import android.database.sqlite.SQLiteDatabase;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.graphics.Bitmap;
37import android.graphics.Canvas;
38import android.graphics.Picture;
39import android.net.Uri;
40import android.net.http.SslError;
41import android.os.AsyncTask;
42import android.os.Bundle;
43import android.os.Handler;
44import android.os.Message;
45import android.os.PowerManager;
46import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000047import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.provider.Browser;
49import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.provider.BrowserContract.Images;
51import android.provider.ContactsContract;
52import android.provider.ContactsContract.Intents.Insert;
Michael Kolbcfa3af52010-12-14 10:36:11 -080053import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.text.TextUtils;
55import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080056import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070057import android.view.ActionMode;
58import android.view.ContextMenu;
59import android.view.ContextMenu.ContextMenuInfo;
60import android.view.Gravity;
61import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070062import android.view.Menu;
63import android.view.MenuInflater;
64import android.view.MenuItem;
65import android.view.MenuItem.OnMenuItemClickListener;
66import android.view.View;
67import android.webkit.CookieManager;
68import android.webkit.CookieSyncManager;
69import android.webkit.HttpAuthHandler;
Mathew Inwood29721c22011-06-29 17:55:24 +010070import android.webkit.SearchBox;
Michael Kolb8233fac2010-10-26 16:08:53 -070071import android.webkit.SslErrorHandler;
72import android.webkit.ValueCallback;
73import android.webkit.WebChromeClient;
74import android.webkit.WebIconDatabase;
75import android.webkit.WebSettings;
76import android.webkit.WebView;
Leon Scrogginsac993842011-02-02 12:54:07 -050077import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070078
Michael Kolb4bd767d2011-05-27 11:33:55 -070079import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070080import com.android.browser.UI.ComboViews;
Michael Kolb4bd767d2011-05-27 11:33:55 -070081import com.android.browser.UI.DropdownChangeListener;
82import com.android.browser.provider.BrowserProvider;
John Reck8cc92352011-07-06 17:41:52 -070083import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070084import com.android.browser.search.SearchEngine;
85import com.android.common.Search;
86
Michael Kolb8233fac2010-10-26 16:08:53 -070087import java.io.ByteArrayOutputStream;
88import java.io.File;
89import java.net.URLEncoder;
90import java.util.Calendar;
91import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080092import java.util.List;
John Reck26b18322011-06-21 13:08:58 -070093import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -070094
95/**
96 * Controller for browser
97 */
98public class Controller
99 implements WebViewController, UiController {
100
101 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800102 private static final String SEND_APP_ID_EXTRA =
103 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700104 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800105
Michael Kolb8233fac2010-10-26 16:08:53 -0700106
107 // public message ids
108 public final static int LOAD_URL = 1001;
109 public final static int STOP_LOAD = 1002;
110
111 // Message Ids
112 private static final int FOCUS_NODE_HREF = 102;
113 private static final int RELEASE_WAKELOCK = 107;
114
115 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
116
117 private static final int OPEN_BOOKMARKS = 201;
118
119 private static final int EMPTY_MENU = -1;
120
Michael Kolb8233fac2010-10-26 16:08:53 -0700121 // activity requestCode
122 final static int PREFERENCES_PAGE = 3;
123 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000124 final static int AUTOFILL_SETUP = 5;
125
Michael Kolb8233fac2010-10-26 16:08:53 -0700126 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
127
128 // As the ids are dynamically created, we can't guarantee that they will
129 // be in sequence, so this static array maps ids to a window number.
130 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
131 { R.id.window_one_menu_id, R.id.window_two_menu_id,
132 R.id.window_three_menu_id, R.id.window_four_menu_id,
133 R.id.window_five_menu_id, R.id.window_six_menu_id,
134 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
135
136 // "source" parameter for Google search through search key
137 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
138 // "source" parameter for Google search through simplily type
139 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
140
Guang Zhu9e78f512011-05-04 11:45:11 -0700141 // "no-crash-recovery" parameter in intetnt to suppress crash recovery
142 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
143
Michael Kolb8233fac2010-10-26 16:08:53 -0700144 private Activity mActivity;
145 private UI mUi;
146 private TabControl mTabControl;
147 private BrowserSettings mSettings;
148 private WebViewFactory mFactory;
John Reckb3417f02011-01-14 11:01:05 -0800149 private OptionsMenuHandler mOptionsMenuHandler = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700150
151 private WakeLock mWakeLock;
152
153 private UrlHandler mUrlHandler;
154 private UploadHandler mUploadHandler;
155 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 private PageDialogsHandler mPageDialogsHandler;
157 private NetworkStateHandler mNetworkHandler;
Martijn Coenenb2f93552011-06-14 10:48:35 +0200158 private NfcHandler mNfcHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700159
Ben Murdoch8029a772010-11-16 11:58:21 +0000160 private Message mAutoFillSetupMessage;
161
Michael Kolb8233fac2010-10-26 16:08:53 -0700162 private boolean mShouldShowErrorConsole;
163
164 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
165
166 // FIXME, temp address onPrepareMenu performance problem.
167 // When we move everything out of view, we should rewrite this.
168 private int mCurrentMenuState = 0;
169 private int mMenuState = R.id.MAIN_MENU;
170 private int mOldMenuState = EMPTY_MENU;
171 private Menu mCachedMenu;
172
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 private boolean mMenuIsDown;
174
175 // For select and find, we keep track of the ActionMode so that
176 // finish() can be called as desired.
177 private ActionMode mActionMode;
178
179 /**
180 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
181 * of whether the configuration has changed. The first onMenuOpened call
182 * after a configuration change is simply a reopening of the same menu
183 * (i.e. mIconView did not change).
184 */
185 private boolean mConfigChanged;
186
187 /**
188 * Keeps track of whether the options menu is open. This is important in
189 * determining whether to show or hide the title bar overlay
190 */
191 private boolean mOptionsMenuOpen;
192
193 /**
194 * Whether or not the options menu is in its bigger, popup menu form. When
195 * true, we want the title bar overlay to be gone. When false, we do not.
196 * Only meaningful if mOptionsMenuOpen is true.
197 */
198 private boolean mExtendedMenuOpen;
199
200 private boolean mInLoad;
201
202 private boolean mActivityPaused = true;
203 private boolean mLoadStopped;
204
205 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500206 // Checks to see when the bookmarks database has changed, and updates the
207 // Tabs' notion of whether they represent bookmarked sites.
208 private ContentObserver mBookmarksObserver;
John Reck0ebd3ac2010-12-09 11:14:04 -0800209 private DataController mDataController;
John Reck847b5322011-04-14 17:02:18 -0700210 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700211
212 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
213 @Override
214 public Void doInBackground(File... files) {
215 if (files != null) {
216 for (File f : files) {
217 if (!f.delete()) {
218 Log.e(LOGTAG, f.getPath() + " was not deleted");
219 }
220 }
221 }
222 return null;
223 }
224 }
225
226 public Controller(Activity browser) {
227 mActivity = browser;
228 mSettings = BrowserSettings.getInstance();
John Reck0ebd3ac2010-12-09 11:14:04 -0800229 mDataController = DataController.getInstance(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700230 mTabControl = new TabControl(this);
231 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700232 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
Michael Kolb14612442011-06-24 13:06:29 -0700233 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700234
235 mUrlHandler = new UrlHandler(this);
236 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700237 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200238 mNfcHandler = new NfcHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700239
240 PowerManager pm = (PowerManager) mActivity
241 .getSystemService(Context.POWER_SERVICE);
242 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
243
244 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500245 mBookmarksObserver = new ContentObserver(mHandler) {
246 @Override
247 public void onChange(boolean selfChange) {
248 int size = mTabControl.getTabCount();
249 for (int i = 0; i < size; i++) {
250 mTabControl.getTab(i).updateBookmarkedStatus();
251 }
252 }
253
254 };
255 browser.getContentResolver().registerContentObserver(
256 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700257
258 mNetworkHandler = new NetworkStateHandler(mActivity, this);
259 // Start watching the default geolocation permissions
260 mSystemAllowGeolocationOrigins =
261 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
262 mSystemAllowGeolocationOrigins.start();
263
264 retainIconsOnStartup();
265 }
266
Patrick Scott7d50a932011-02-04 09:27:26 -0500267 void start(final Bundle icicle, final Intent intent) {
Guang Zhu9e78f512011-05-04 11:45:11 -0700268 boolean noCrashRecovery = intent.getBooleanExtra(NO_CRASH_RECOVERY, false);
269 if (icicle != null || noCrashRecovery) {
John Reck847b5322011-04-14 17:02:18 -0700270 doStart(icicle, intent);
271 } else {
272 mCrashRecoveryHandler.startRecovery(intent);
273 }
274 }
275
276 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 // Unless the last browser usage was within 24 hours, destroy any
278 // remaining incognito tabs.
279
280 Calendar lastActiveDate = icicle != null ?
281 (Calendar) icicle.getSerializable("lastActiveDate") : null;
282 Calendar today = Calendar.getInstance();
283 Calendar yesterday = Calendar.getInstance();
284 yesterday.add(Calendar.DATE, -1);
285
Patrick Scott7d50a932011-02-04 09:27:26 -0500286 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700287 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800288 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700289
Patrick Scott7d50a932011-02-04 09:27:26 -0500290 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700291 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500292 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000293
Michael Kolbc831b632011-05-11 09:30:34 -0700294 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500295 // Not able to restore so we go ahead and clear session cookies. We
296 // must do this before trying to login the user as we don't want to
297 // clear any session cookies set during login.
298 CookieManager.getInstance().removeSessionCookie();
299 }
300
Patrick Scottd43e75a2011-03-14 14:47:23 -0400301 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500302 new Runnable() {
303 @Override public void run() {
Michael Kolbc831b632011-05-11 09:30:34 -0700304 onPreloginFinished(icicle, intent, currentTabId, restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500305 }
306 });
307 }
308
Michael Kolbc831b632011-05-11 09:30:34 -0700309 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500310 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700311 if (currentTabId == -1) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700312 final Bundle extra = intent.getExtras();
313 // Create an initial tab.
314 // If the intent is ACTION_VIEW and data is not null, the Browser is
315 // invoked to view the content by another application. In this case,
316 // the tab will be close when exit.
Michael Kolb14612442011-06-24 13:06:29 -0700317 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
Michael Kolb7bcafde2011-05-09 13:55:59 -0700318 Tab t = null;
319 if (urlData.isEmpty()) {
320 t = openTabToHomePage();
321 } else {
322 t = openTab(urlData);
323 }
324 if (t != null) {
325 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
326 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700327 WebView webView = t.getWebView();
328 if (extra != null) {
329 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
330 if (scale > 0 && scale <= 1000) {
331 webView.setInitialScale(scale);
332 }
333 }
John Reckd8c74522011-06-14 08:45:00 -0700334 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700335 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700336 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500337 mUi.needsRestoreAllTabs());
Michael Kolb1bf23132010-11-19 12:55:12 -0800338 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700339 // TabControl.restoreState() will create a new tab even if
340 // restoring the state fails.
341 setActiveTab(mTabControl.getCurrentTab());
John Reckdb22ec42011-06-29 11:31:24 -0700342 // Handle the intent
343 mIntentHandler.onNewIntent(intent);
Michael Kolb8233fac2010-10-26 16:08:53 -0700344 }
345 // clear up the thumbnail directory, which is no longer used;
346 // ideally this should only be run once after an upgrade from
347 // a previous version of the browser
348 new ClearThumbnails().execute(mTabControl.getThumbnailDir()
349 .listFiles());
350 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700351 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700352 if (jsFlags.trim().length() != 0) {
353 getCurrentWebView().setJsFlags(jsFlags);
354 }
John Reck439c9a52010-12-14 10:04:39 -0800355 if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
356 bookmarksOrHistoryPicker(false);
357 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 }
359
Michael Kolb1514bb72010-11-22 09:11:48 -0800360 @Override
361 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700362 return mFactory;
363 }
364
365 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800366 public void onSetWebView(Tab tab, WebView view) {
367 mUi.onSetWebView(tab, view);
368 }
369
370 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800371 public void createSubWindow(Tab tab) {
372 endActionMode();
373 WebView mainView = tab.getWebView();
374 WebView subView = mFactory.createWebView((mainView == null)
375 ? false
376 : mainView.isPrivateBrowsingEnabled());
377 mUi.createSubWindow(tab, subView);
378 }
379
380 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700381 public Context getContext() {
382 return mActivity;
383 }
384
385 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700386 public Activity getActivity() {
387 return mActivity;
388 }
389
390 void setUi(UI ui) {
391 mUi = ui;
392 }
393
394 BrowserSettings getSettings() {
395 return mSettings;
396 }
397
398 IntentHandler getIntentHandler() {
399 return mIntentHandler;
400 }
401
402 @Override
403 public UI getUi() {
404 return mUi;
405 }
406
407 int getMaxTabs() {
408 return mActivity.getResources().getInteger(R.integer.max_tabs);
409 }
410
411 @Override
412 public TabControl getTabControl() {
413 return mTabControl;
414 }
415
Michael Kolb1bf23132010-11-19 12:55:12 -0800416 @Override
417 public List<Tab> getTabs() {
418 return mTabControl.getTabs();
419 }
420
Michael Kolb8233fac2010-10-26 16:08:53 -0700421 // Open the icon database and retain all the icons for visited sites.
Ben Murdoch9446b932010-11-25 16:20:14 +0000422 // This is done on a background thread so as not to stall startup.
Michael Kolb8233fac2010-10-26 16:08:53 -0700423 private void retainIconsOnStartup() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000424 // WebIconDatabase needs to be retrieved on the UI thread so that if
425 // it has not been created successfully yet the Handler is started on the
426 // UI thread.
427 new RetainIconsOnStartupTask(WebIconDatabase.getInstance()).execute();
428 }
429
430 private class RetainIconsOnStartupTask extends AsyncTask<Void, Void, Void> {
431 private WebIconDatabase mDb;
432
433 public RetainIconsOnStartupTask(WebIconDatabase db) {
434 mDb = db;
435 }
436
John Recka00cbbd2010-12-16 12:38:19 -0800437 @Override
Ben Murdoch9446b932010-11-25 16:20:14 +0000438 protected Void doInBackground(Void... unused) {
439 mDb.open(mActivity.getDir("icons", 0).getPath());
440 Cursor c = null;
441 try {
442 c = Browser.getAllBookmarks(mActivity.getContentResolver());
443 if (c.moveToFirst()) {
444 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
445 do {
446 String url = c.getString(urlIndex);
447 mDb.retainIconForPageUrl(url);
448 } while (c.moveToNext());
449 }
450 } catch (IllegalStateException e) {
451 Log.e(LOGTAG, "retainIconsOnStartup", e);
452 } finally {
453 if (c != null) c.close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700454 }
Ben Murdoch9446b932010-11-25 16:20:14 +0000455
456 return null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700457 }
458 }
459
460 private void startHandler() {
461 mHandler = new Handler() {
462
463 @Override
464 public void handleMessage(Message msg) {
465 switch (msg.what) {
466 case OPEN_BOOKMARKS:
467 bookmarksOrHistoryPicker(false);
468 break;
469 case FOCUS_NODE_HREF:
470 {
471 String url = (String) msg.getData().get("url");
472 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500473 String src = (String) msg.getData().get("src");
474 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700475 if (TextUtils.isEmpty(url)) {
476 break;
477 }
478 HashMap focusNodeMap = (HashMap) msg.obj;
479 WebView view = (WebView) focusNodeMap.get("webview");
480 // Only apply the action if the top window did not change.
481 if (getCurrentTopWebView() != view) {
482 break;
483 }
484 switch (msg.arg1) {
485 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700486 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700487 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500488 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700489 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500490 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500491 case R.id.open_newtab_context_menu_id:
492 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700493 openTab(url, parent,
494 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500495 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700496 case R.id.copy_link_context_menu_id:
497 copy(url);
498 break;
499 case R.id.save_link_context_menu_id:
500 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500501 DownloadHandler.onDownloadStartNoStream(
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000502 mActivity, url, null, null, null,
503 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 break;
505 }
506 break;
507 }
508
509 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700510 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 break;
512
513 case STOP_LOAD:
514 stopLoading();
515 break;
516
517 case RELEASE_WAKELOCK:
518 if (mWakeLock.isHeld()) {
519 mWakeLock.release();
520 // if we reach here, Browser should be still in the
521 // background loading after WAKELOCK_TIMEOUT (5-min).
522 // To avoid burning the battery, stop loading.
523 mTabControl.stopAllLoading();
524 }
525 break;
526
527 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800528 Tab tab = (Tab) msg.obj;
529 if (tab != null) {
530 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700531 }
532 break;
533 }
534 }
535 };
536
537 }
538
John Reckef654f12011-07-12 16:42:08 -0700539 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200540 public Tab getCurrentTab() {
541 return mTabControl.getCurrentTab();
542 }
543
Michael Kolbba99c5d2010-11-29 14:57:41 -0800544 @Override
545 public void shareCurrentPage() {
546 shareCurrentPage(mTabControl.getCurrentTab());
547 }
548
549 private void shareCurrentPage(Tab tab) {
550 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800551 sharePage(mActivity, tab.getTitle(),
552 tab.getUrl(), tab.getFavicon(),
553 createScreenshot(tab.getWebView(),
554 getDesiredThumbnailWidth(mActivity),
555 getDesiredThumbnailHeight(mActivity)));
556 }
557 }
558
Michael Kolb8233fac2010-10-26 16:08:53 -0700559 /**
560 * Share a page, providing the title, url, favicon, and a screenshot. Uses
561 * an {@link Intent} to launch the Activity chooser.
562 * @param c Context used to launch a new Activity.
563 * @param title Title of the page. Stored in the Intent with
564 * {@link Intent#EXTRA_SUBJECT}
565 * @param url URL of the page. Stored in the Intent with
566 * {@link Intent#EXTRA_TEXT}
567 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
568 * with {@link Browser#EXTRA_SHARE_FAVICON}
569 * @param screenshot Bitmap of a screenshot of the page. Stored in the
570 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
571 */
572 static final void sharePage(Context c, String title, String url,
573 Bitmap favicon, Bitmap screenshot) {
574 Intent send = new Intent(Intent.ACTION_SEND);
575 send.setType("text/plain");
576 send.putExtra(Intent.EXTRA_TEXT, url);
577 send.putExtra(Intent.EXTRA_SUBJECT, title);
578 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
579 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
580 try {
581 c.startActivity(Intent.createChooser(send, c.getString(
582 R.string.choosertitle_sharevia)));
583 } catch(android.content.ActivityNotFoundException ex) {
584 // if no app handles it, do nothing
585 }
586 }
587
588 private void copy(CharSequence text) {
589 ClipboardManager cm = (ClipboardManager) mActivity
590 .getSystemService(Context.CLIPBOARD_SERVICE);
591 cm.setText(text);
592 }
593
594 // lifecycle
595
596 protected void onConfgurationChanged(Configuration config) {
597 mConfigChanged = true;
598 if (mPageDialogsHandler != null) {
599 mPageDialogsHandler.onConfigurationChanged(config);
600 }
601 mUi.onConfigurationChanged(config);
602 }
603
604 @Override
605 public void handleNewIntent(Intent intent) {
606 mIntentHandler.onNewIntent(intent);
607 }
608
609 protected void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800610 if (mUi.isCustomViewShowing()) {
611 hideCustomView();
612 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700613 if (mActivityPaused) {
614 Log.e(LOGTAG, "BrowserActivity is already paused.");
615 return;
616 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700617 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800618 Tab tab = mTabControl.getCurrentTab();
619 if (tab != null) {
620 tab.pause();
621 if (!pauseWebViewTimers(tab)) {
622 mWakeLock.acquire();
623 mHandler.sendMessageDelayed(mHandler
624 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
625 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 }
627 mUi.onPause();
628 mNetworkHandler.onPause();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200629 mNfcHandler.onPause();
Michael Kolb8233fac2010-10-26 16:08:53 -0700630
631 WebView.disablePlatformNotifications();
John Reck378a4102011-06-09 16:23:01 -0700632 mCrashRecoveryHandler.backupState();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200633
Michael Kolb8233fac2010-10-26 16:08:53 -0700634 }
635
John Reckaed9c542011-05-27 16:08:53 -0700636 void onSaveInstanceState(Bundle outState, boolean saveImages) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700637 // the default implementation requires each view to have an id. As the
638 // browser handles the state itself and it doesn't use id for the views,
639 // don't call the default implementation. Otherwise it will trigger the
640 // warning like this, "couldn't save which view has focus because the
641 // focused view XXX has no id".
642
643 // Save all the tabs
John Reckaed9c542011-05-27 16:08:53 -0700644 mTabControl.saveState(outState, saveImages);
John Reck24f18262011-06-17 14:47:20 -0700645 if (!outState.isEmpty()) {
646 // Save time so that we know how old incognito tabs (if any) are.
647 outState.putSerializable("lastActiveDate", Calendar.getInstance());
648 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700649 }
650
651 void onResume() {
652 if (!mActivityPaused) {
653 Log.e(LOGTAG, "BrowserActivity is already resumed.");
654 return;
655 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700656 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800657 Tab current = mTabControl.getCurrentTab();
658 if (current != null) {
659 current.resume();
660 resumeWebViewTimers(current);
661 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700662 if (mWakeLock.isHeld()) {
663 mHandler.removeMessages(RELEASE_WAKELOCK);
664 mWakeLock.release();
665 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200666
Michael Kolb8233fac2010-10-26 16:08:53 -0700667 mUi.onResume();
668 mNetworkHandler.onResume();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200669 mNfcHandler.onResume();
Michael Kolb8233fac2010-10-26 16:08:53 -0700670 WebView.enablePlatformNotifications();
671 }
672
Michael Kolb70976932010-11-30 11:34:01 -0800673 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800674 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800675 * @param tab guaranteed non-null
676 */
677 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 boolean inLoad = tab.inPageLoad();
679 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
680 CookieSyncManager.getInstance().startSync();
681 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100682 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700683 }
684 }
685
Michael Kolb70976932010-11-30 11:34:01 -0800686 /**
687 * Pause all WebView timers using the WebView of the given tab
688 * @param tab
689 * @return true if the timers are paused or tab is null
690 */
691 private boolean pauseWebViewTimers(Tab tab) {
692 if (tab == null) {
693 return true;
694 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700695 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100696 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700698 }
Michael Kolb70976932010-11-30 11:34:01 -0800699 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700700 }
701
702 void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800703 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700704 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
705 mUploadHandler = null;
706 }
707 if (mTabControl == null) return;
708 mUi.onDestroy();
709 // Remove the current tab and sub window
710 Tab t = mTabControl.getCurrentTab();
711 if (t != null) {
712 dismissSubWindow(t);
713 removeTab(t);
714 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500715 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700716 // Destroy all the tabs
717 mTabControl.destroy();
718 WebIconDatabase.getInstance().close();
719 // Stop watching the default geolocation permissions
720 mSystemAllowGeolocationOrigins.stop();
721 mSystemAllowGeolocationOrigins = null;
722 }
723
724 protected boolean isActivityPaused() {
725 return mActivityPaused;
726 }
727
728 protected void onLowMemory() {
729 mTabControl.freeMemory();
730 }
731
732 @Override
733 public boolean shouldShowErrorConsole() {
734 return mShouldShowErrorConsole;
735 }
736
737 protected void setShouldShowErrorConsole(boolean show) {
738 if (show == mShouldShowErrorConsole) {
739 // Nothing to do.
740 return;
741 }
742 mShouldShowErrorConsole = show;
743 Tab t = mTabControl.getCurrentTab();
744 if (t == null) {
745 // There is no current tab so we cannot toggle the error console
746 return;
747 }
748 mUi.setShouldShowErrorConsole(t, show);
749 }
750
751 @Override
752 public void stopLoading() {
753 mLoadStopped = true;
754 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700755 WebView w = getCurrentTopWebView();
756 w.stopLoading();
Michael Kolb8233fac2010-10-26 16:08:53 -0700757 mUi.onPageStopped(tab);
758 }
759
760 boolean didUserStopLoading() {
761 return mLoadStopped;
762 }
763
764 // WebViewController
765
766 @Override
John Reck324d4402011-01-11 16:56:42 -0800767 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700768
769 // We've started to load a new page. If there was a pending message
770 // to save a screenshot then we will now take the new page and save
771 // an incorrect screenshot. Therefore, remove any pending thumbnail
772 // messages from the queue.
773 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800774 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700775
776 // reset sync timer to avoid sync starts during loading a page
777 CookieSyncManager.getInstance().resetSync();
778
779 if (!mNetworkHandler.isNetworkUp()) {
780 view.setNetworkAvailable(false);
781 }
782
783 // when BrowserActivity just starts, onPageStarted may be called before
784 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
785 // to start the timer. As we won't switch tabs while an activity is in
786 // pause state, we can ensure calling resume and pause in pair.
787 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800788 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700789 }
790 mLoadStopped = false;
791 if (!mNetworkHandler.isNetworkUp()) {
792 mNetworkHandler.createAndShowNetworkDialog();
793 }
794 endActionMode();
795
John Reck30c714c2010-12-16 17:30:34 -0800796 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700797
John Reck324d4402011-01-11 16:56:42 -0800798 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700799 // update the bookmark database for favicon
800 maybeUpdateFavicon(tab, null, url, favicon);
801
802 Performance.tracePageStart(url);
803
804 // Performance probe
805 if (false) {
806 Performance.onPageStarted();
807 }
808
809 }
810
811 @Override
John Reck324d4402011-01-11 16:56:42 -0800812 public void onPageFinished(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800813 mUi.onTabDataChanged(tab);
John Reck324d4402011-01-11 16:56:42 -0800814 if (!tab.isPrivateBrowsingEnabled()
John Reckd8c74522011-06-14 08:45:00 -0700815 && !TextUtils.isEmpty(tab.getUrl())
816 && !tab.isSnapshot()) {
John Recka1696282011-07-08 14:10:37 -0700817 // Only update the bookmark screenshot if the user did not
818 // cancel the load early and there is not already
819 // a pending update for the tab.
Michael Kolb8233fac2010-10-26 16:08:53 -0700820 if (tab.inForeground() && !didUserStopLoading()
821 || !tab.inForeground()) {
John Recka1696282011-07-08 14:10:37 -0700822 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
823 mHandler.sendMessageDelayed(mHandler.obtainMessage(
824 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
825 500);
826 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700827 }
828 }
829 // pause the WebView timer and release the wake lock if it is finished
830 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800831 if (mActivityPaused && pauseWebViewTimers(tab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700832 if (mWakeLock.isHeld()) {
833 mHandler.removeMessages(RELEASE_WAKELOCK);
834 mWakeLock.release();
835 }
836 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200837
Michael Kolb8233fac2010-10-26 16:08:53 -0700838 // Performance probe
839 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800840 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700841 }
842
843 Performance.tracePageFinished();
844 }
845
846 @Override
John Reck30c714c2010-12-16 17:30:34 -0800847 public void onProgressChanged(Tab tab) {
848 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700849
850 if (newProgress == 100) {
851 CookieSyncManager.getInstance().sync();
852 // onProgressChanged() may continue to be called after the main
853 // frame has finished loading, as any remaining sub frames continue
854 // to load. We'll only get called once though with newProgress as
855 // 100 when everything is loaded. (onPageFinished is called once
856 // when the main frame completes loading regardless of the state of
857 // any sub frames so calls to onProgressChanges may continue after
858 // onPageFinished has executed)
859 if (mInLoad) {
860 mInLoad = false;
861 updateInLoadMenuItems(mCachedMenu);
862 }
863 } else {
864 if (!mInLoad) {
865 // onPageFinished may have already been called but a subframe is
866 // still loading and updating the progress. Reset mInLoad and
867 // update the menu items.
868 mInLoad = true;
869 updateInLoadMenuItems(mCachedMenu);
870 }
871 }
John Reck30c714c2010-12-16 17:30:34 -0800872 mUi.onProgressChanged(tab);
873 }
874
875 @Override
876 public void onUpdatedLockIcon(Tab tab) {
877 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 }
879
880 @Override
881 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800882 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800883 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800884 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700885 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
886 return;
887 }
888 // Update the title in the history database if not in private browsing mode
889 if (!tab.isPrivateBrowsingEnabled()) {
John Reck0ebd3ac2010-12-09 11:14:04 -0800890 mDataController.updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700891 }
892 }
893
894 @Override
895 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800896 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700897 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
898 }
899
900 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800901 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
902 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700903 }
904
905 @Override
906 public boolean shouldOverrideKeyEvent(KeyEvent event) {
907 if (mMenuIsDown) {
908 // only check shortcut key when MENU is held
909 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
910 event);
911 } else {
912 return false;
913 }
914 }
915
916 @Override
917 public void onUnhandledKeyEvent(KeyEvent event) {
918 if (!isActivityPaused()) {
919 if (event.getAction() == KeyEvent.ACTION_DOWN) {
920 mActivity.onKeyDown(event.getKeyCode(), event);
921 } else {
922 mActivity.onKeyUp(event.getKeyCode(), event);
923 }
924 }
925 }
926
927 @Override
John Reck324d4402011-01-11 16:56:42 -0800928 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700929 // Don't save anything in private browsing mode
930 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800931 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700932
John Reck324d4402011-01-11 16:56:42 -0800933 if (TextUtils.isEmpty(url)
934 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700935 return;
936 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800937 mDataController.updateVisitedHistory(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 WebIconDatabase.getInstance().retainIconForPageUrl(url);
John Reck847b5322011-04-14 17:02:18 -0700939 if (!mActivityPaused) {
940 // Since we clear the state in onPause, don't backup the current
941 // state if we are already paused
942 mCrashRecoveryHandler.backupState();
943 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700944 }
945
946 @Override
947 public void getVisitedHistory(final ValueCallback<String[]> callback) {
948 AsyncTask<Void, Void, String[]> task =
949 new AsyncTask<Void, Void, String[]>() {
950 @Override
951 public String[] doInBackground(Void... unused) {
952 return Browser.getVisitedHistory(mActivity.getContentResolver());
953 }
954 @Override
955 public void onPostExecute(String[] result) {
956 callback.onReceiveValue(result);
957 }
958 };
959 task.execute();
960 }
961
962 @Override
963 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
964 final HttpAuthHandler handler, final String host,
965 final String realm) {
966 String username = null;
967 String password = null;
968
969 boolean reuseHttpAuthUsernamePassword
970 = handler.useHttpAuthUsernamePassword();
971
972 if (reuseHttpAuthUsernamePassword && view != null) {
973 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
974 if (credentials != null && credentials.length == 2) {
975 username = credentials[0];
976 password = credentials[1];
977 }
978 }
979
980 if (username != null && password != null) {
981 handler.proceed(username, password);
982 } else {
983 if (tab.inForeground()) {
984 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
985 } else {
986 handler.cancel();
987 }
988 }
989 }
990
991 @Override
992 public void onDownloadStart(Tab tab, String url, String userAgent,
993 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000994 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -0500995 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000996 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
997 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700998 // This Tab was opened for the sole purpose of downloading a
999 // file. Remove it.
1000 if (tab == mTabControl.getCurrentTab()) {
1001 // In this case, the Tab is still on top.
1002 goBackOnePageOrQuit();
1003 } else {
1004 // In this case, it is not.
1005 closeTab(tab);
1006 }
1007 }
1008 }
1009
1010 @Override
1011 public Bitmap getDefaultVideoPoster() {
1012 return mUi.getDefaultVideoPoster();
1013 }
1014
1015 @Override
1016 public View getVideoLoadingProgressView() {
1017 return mUi.getVideoLoadingProgressView();
1018 }
1019
1020 @Override
1021 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1022 SslError error) {
1023 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1024 }
1025
Patrick Scott92066772011-03-10 08:46:27 -05001026 @Override
1027 public void showAutoLogin(Tab tab) {
1028 assert tab.inForeground();
1029 // Update the title bar to show the auto-login request.
1030 mUi.showAutoLogin(tab);
1031 }
1032
1033 @Override
1034 public void hideAutoLogin(Tab tab) {
1035 assert tab.inForeground();
1036 mUi.hideAutoLogin(tab);
1037 }
1038
Michael Kolb8233fac2010-10-26 16:08:53 -07001039 // helper method
1040
1041 /*
1042 * Update the favorites icon if the private browsing isn't enabled and the
1043 * icon is valid.
1044 */
1045 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1046 final String url, Bitmap favicon) {
1047 if (favicon == null) {
1048 return;
1049 }
1050 if (!tab.isPrivateBrowsingEnabled()) {
1051 Bookmarks.updateFavicon(mActivity
1052 .getContentResolver(), originalUrl, url, favicon);
1053 }
1054 }
1055
Leon Scroggins4cd97792010-12-03 15:31:56 -05001056 @Override
1057 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001058 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001059 mUi.bookmarkedStatusHasChanged(tab);
1060 }
1061
Michael Kolb8233fac2010-10-26 16:08:53 -07001062 // end WebViewController
1063
1064 protected void pageUp() {
1065 getCurrentTopWebView().pageUp(false);
1066 }
1067
1068 protected void pageDown() {
1069 getCurrentTopWebView().pageDown(false);
1070 }
1071
1072 // callback from phone title bar
1073 public void editUrl() {
1074 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001075 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001076 }
1077
Michael Kolbcfa3af52010-12-14 10:36:11 -08001078 public void startVoiceSearch() {
1079 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1080 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1081 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1082 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1083 mActivity.getComponentName().flattenToString());
1084 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001085 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001086 mActivity.startActivity(intent);
1087 }
1088
Michael Kolb11d19782011-03-20 10:17:40 -07001089 @Override
1090 public void activateVoiceSearchMode(String title, List<String> results) {
1091 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001092 }
1093
1094 public void revertVoiceSearchMode(Tab tab) {
1095 mUi.revertVoiceTitleBar(tab);
1096 }
1097
Michael Kolb736990c2011-03-20 10:01:20 -07001098 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001099 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001100 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1101 }
1102
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001103 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001104 WebChromeClient.CustomViewCallback callback) {
1105 if (tab.inForeground()) {
1106 if (mUi.isCustomViewShowing()) {
1107 callback.onCustomViewHidden();
1108 return;
1109 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001110 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001111 // Save the menu state and set it to empty while the custom
1112 // view is showing.
1113 mOldMenuState = mMenuState;
1114 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001115 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001116 }
1117 }
1118
1119 @Override
1120 public void hideCustomView() {
1121 if (mUi.isCustomViewShowing()) {
1122 mUi.onHideCustomView();
1123 // Reset the old menu state.
1124 mMenuState = mOldMenuState;
1125 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001126 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001127 }
1128 }
1129
1130 protected void onActivityResult(int requestCode, int resultCode,
1131 Intent intent) {
1132 if (getCurrentTopWebView() == null) return;
1133 switch (requestCode) {
1134 case PREFERENCES_PAGE:
1135 if (resultCode == Activity.RESULT_OK && intent != null) {
1136 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001137 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001138 mTabControl.removeParentChildRelationShips();
1139 }
1140 }
1141 break;
1142 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001143 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001144 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001145 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001146 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001147 case AUTOFILL_SETUP:
1148 // Determine whether a profile was actually set up or not
1149 // and if so, send the message back to the WebTextView to
1150 // fill the form with the new profile.
1151 if (getSettings().getAutoFillProfile() != null) {
1152 mAutoFillSetupMessage.sendToTarget();
1153 mAutoFillSetupMessage = null;
1154 }
1155 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001156 default:
1157 break;
1158 }
1159 getCurrentTopWebView().requestFocus();
1160 }
1161
1162 /**
1163 * Open the Go page.
1164 * @param startWithHistory If true, open starting on the history tab.
1165 * Otherwise, start with the bookmarks tab.
1166 */
1167 @Override
1168 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1169 if (mTabControl.getCurrentWebView() == null) {
1170 return;
1171 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001172 // clear action mode
1173 if (isInCustomActionMode()) {
1174 endActionMode();
1175 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001176 Bundle extras = new Bundle();
1177 // Disable opening in a new window if we have maxed out the windows
1178 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1179 !mTabControl.canCreateNewTab());
John Reck2bc80422011-06-30 15:11:49 -07001180 mUi.showComboView(startWithHistory
1181 ? ComboViews.History : ComboViews.Bookmarks, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001182 }
1183
1184 // combo view callbacks
1185
1186 /**
1187 * callback from ComboPage when clear history is requested
1188 */
1189 public void onRemoveParentChildRelationships() {
1190 mTabControl.removeParentChildRelationShips();
1191 }
1192
1193 /**
1194 * callback from ComboPage when bookmark/history selection
1195 */
1196 @Override
1197 public void onUrlSelected(String url, boolean newTab) {
1198 removeComboView();
1199 if (!TextUtils.isEmpty(url)) {
1200 if (newTab) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07001201 final Tab parent = mTabControl.getCurrentTab();
1202 openTab(url,
1203 (parent != null) && parent.isPrivateBrowsingEnabled(),
1204 !mSettings.openInBackground(),
1205 true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001206 } else {
1207 final Tab currentTab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001208 loadUrl(currentTab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001209 }
1210 }
1211 }
1212
1213 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001214 * dismiss the ComboPage
1215 */
1216 @Override
1217 public void removeComboView() {
1218 mUi.hideComboView();
1219 }
1220
Michael Kolb8233fac2010-10-26 16:08:53 -07001221 // key handling
1222 protected void onBackKey() {
1223 if (!mUi.onBackKey()) {
1224 WebView subwindow = mTabControl.getCurrentSubWindow();
1225 if (subwindow != null) {
1226 if (subwindow.canGoBack()) {
1227 subwindow.goBack();
1228 } else {
1229 dismissSubWindow(mTabControl.getCurrentTab());
1230 }
1231 } else {
1232 goBackOnePageOrQuit();
1233 }
1234 }
1235 }
1236
Michael Kolb4bd767d2011-05-27 11:33:55 -07001237 protected boolean onMenuKey() {
1238 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001239 }
1240
Michael Kolb8233fac2010-10-26 16:08:53 -07001241 // menu handling and state
1242 // TODO: maybe put into separate handler
1243
1244 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckb3417f02011-01-14 11:01:05 -08001245 if (mOptionsMenuHandler != null) {
1246 return mOptionsMenuHandler.onCreateOptionsMenu(menu);
1247 }
1248
John Reckd73c5a22010-12-22 10:22:50 -08001249 if (mMenuState == EMPTY_MENU) {
1250 return false;
1251 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001252 MenuInflater inflater = mActivity.getMenuInflater();
1253 inflater.inflate(R.menu.browser, menu);
1254 updateInLoadMenuItems(menu);
1255 // hold on to the menu reference here; it is used by the page callbacks
1256 // to update the menu based on loading state
1257 mCachedMenu = menu;
1258 return true;
1259 }
1260
1261 protected void onCreateContextMenu(ContextMenu menu, View v,
1262 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001263 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001264 return;
1265 }
1266 if (!(v instanceof WebView)) {
1267 return;
1268 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001269 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001270 WebView.HitTestResult result = webview.getHitTestResult();
1271 if (result == null) {
1272 return;
1273 }
1274
1275 int type = result.getType();
1276 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1277 Log.w(LOGTAG,
1278 "We should not show context menu when nothing is touched");
1279 return;
1280 }
1281 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1282 // let TextView handles context menu
1283 return;
1284 }
1285
1286 // Note, http://b/issue?id=1106666 is requesting that
1287 // an inflated menu can be used again. This is not available
1288 // yet, so inflate each time (yuk!)
1289 MenuInflater inflater = mActivity.getMenuInflater();
1290 inflater.inflate(R.menu.browsercontext, menu);
1291
1292 // Show the correct menu group
1293 final String extra = result.getExtra();
1294 menu.setGroupVisible(R.id.PHONE_MENU,
1295 type == WebView.HitTestResult.PHONE_TYPE);
1296 menu.setGroupVisible(R.id.EMAIL_MENU,
1297 type == WebView.HitTestResult.EMAIL_TYPE);
1298 menu.setGroupVisible(R.id.GEO_MENU,
1299 type == WebView.HitTestResult.GEO_TYPE);
1300 menu.setGroupVisible(R.id.IMAGE_MENU,
1301 type == WebView.HitTestResult.IMAGE_TYPE
1302 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1303 menu.setGroupVisible(R.id.ANCHOR_MENU,
1304 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1305 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001306 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1307 || type == WebView.HitTestResult.PHONE_TYPE
1308 || type == WebView.HitTestResult.EMAIL_TYPE
1309 || type == WebView.HitTestResult.GEO_TYPE;
1310 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1311 if (hitText) {
1312 menu.findItem(R.id.select_text_menu_id)
1313 .setOnMenuItemClickListener(new SelectText(webview));
1314 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001315 // Setup custom handling depending on the type
1316 switch (type) {
1317 case WebView.HitTestResult.PHONE_TYPE:
1318 menu.setHeaderTitle(Uri.decode(extra));
1319 menu.findItem(R.id.dial_context_menu_id).setIntent(
1320 new Intent(Intent.ACTION_VIEW, Uri
1321 .parse(WebView.SCHEME_TEL + extra)));
1322 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1323 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1324 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1325 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1326 addIntent);
1327 menu.findItem(R.id.copy_phone_context_menu_id)
1328 .setOnMenuItemClickListener(
1329 new Copy(extra));
1330 break;
1331
1332 case WebView.HitTestResult.EMAIL_TYPE:
1333 menu.setHeaderTitle(extra);
1334 menu.findItem(R.id.email_context_menu_id).setIntent(
1335 new Intent(Intent.ACTION_VIEW, Uri
1336 .parse(WebView.SCHEME_MAILTO + extra)));
1337 menu.findItem(R.id.copy_mail_context_menu_id)
1338 .setOnMenuItemClickListener(
1339 new Copy(extra));
1340 break;
1341
1342 case WebView.HitTestResult.GEO_TYPE:
1343 menu.setHeaderTitle(extra);
1344 menu.findItem(R.id.map_context_menu_id).setIntent(
1345 new Intent(Intent.ACTION_VIEW, Uri
1346 .parse(WebView.SCHEME_GEO
1347 + URLEncoder.encode(extra))));
1348 menu.findItem(R.id.copy_geo_context_menu_id)
1349 .setOnMenuItemClickListener(
1350 new Copy(extra));
1351 break;
1352
1353 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1354 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001355 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001356 // decide whether to show the open link in new tab option
1357 boolean showNewTab = mTabControl.canCreateNewTab();
1358 MenuItem newTabItem
1359 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001360 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001361 ? R.string.contextmenu_openlink_newwindow_background
1362 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001363 newTabItem.setVisible(showNewTab);
1364 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001365 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1366 newTabItem.setOnMenuItemClickListener(
1367 new MenuItem.OnMenuItemClickListener() {
1368 @Override
1369 public boolean onMenuItemClick(MenuItem item) {
1370 final HashMap<String, WebView> hrefMap =
1371 new HashMap<String, WebView>();
1372 hrefMap.put("webview", webview);
1373 final Message msg = mHandler.obtainMessage(
1374 FOCUS_NODE_HREF,
1375 R.id.open_newtab_context_menu_id,
1376 0, hrefMap);
1377 webview.requestFocusNodeHref(msg);
1378 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001379 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001380 });
1381 } else {
1382 newTabItem.setOnMenuItemClickListener(
1383 new MenuItem.OnMenuItemClickListener() {
1384 @Override
1385 public boolean onMenuItemClick(MenuItem item) {
1386 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001387 openTab(extra, parent,
1388 !mSettings.openInBackground(),
1389 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001390 return true;
1391 }
1392 });
1393 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001394 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001395 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1396 break;
1397 }
1398 // otherwise fall through to handle image part
1399 case WebView.HitTestResult.IMAGE_TYPE:
1400 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1401 menu.setHeaderTitle(extra);
1402 }
1403 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1404 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1405 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001406 setOnMenuItemClickListener(
1407 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001408 menu.findItem(R.id.set_wallpaper_context_menu_id).
1409 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1410 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001411 break;
1412
1413 default:
1414 Log.w(LOGTAG, "We should not get here.");
1415 break;
1416 }
1417 //update the ui
1418 mUi.onContextMenuCreated(menu);
1419 }
1420
1421 /**
1422 * As the menu can be open when loading state changes
1423 * we must manually update the state of the stop/reload menu
1424 * item
1425 */
1426 private void updateInLoadMenuItems(Menu menu) {
1427 if (menu == null) {
1428 return;
1429 }
1430 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1431 MenuItem src = mInLoad ?
1432 menu.findItem(R.id.stop_menu_id):
1433 menu.findItem(R.id.reload_menu_id);
1434 if (src != null) {
1435 dest.setIcon(src.getIcon());
1436 dest.setTitle(src.getTitle());
1437 }
1438 }
1439
John Reckb3417f02011-01-14 11:01:05 -08001440 boolean onPrepareOptionsMenu(Menu menu) {
1441 if (mOptionsMenuHandler != null) {
1442 return mOptionsMenuHandler.onPrepareOptionsMenu(menu);
1443 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001444 // Note: setVisible will decide whether an item is visible; while
1445 // setEnabled() will decide whether an item is enabled, which also means
1446 // whether the matching shortcut key will function.
1447 switch (mMenuState) {
1448 case EMPTY_MENU:
1449 if (mCurrentMenuState != mMenuState) {
1450 menu.setGroupVisible(R.id.MAIN_MENU, false);
1451 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1452 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1453 }
1454 break;
1455 default:
1456 if (mCurrentMenuState != mMenuState) {
1457 menu.setGroupVisible(R.id.MAIN_MENU, true);
1458 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1459 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1460 }
John Reckef654f12011-07-12 16:42:08 -07001461 final Tab t = getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001462 boolean canGoBack = false;
1463 boolean canGoForward = false;
1464 boolean isHome = false;
John Reckef654f12011-07-12 16:42:08 -07001465 if (t != null) {
1466 canGoBack = t.canGoBack();
1467 canGoForward = t.canGoForward();
1468 isHome = mSettings.getHomePage().equals(t.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -07001469 }
1470 final MenuItem back = menu.findItem(R.id.back_menu_id);
1471 back.setEnabled(canGoBack);
1472
1473 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1474 home.setEnabled(!isHome);
1475
1476 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1477 forward.setEnabled(canGoForward);
1478
1479 // decide whether to show the share link option
1480 PackageManager pm = mActivity.getPackageManager();
1481 Intent send = new Intent(Intent.ACTION_SEND);
1482 send.setType("text/plain");
1483 ResolveInfo ri = pm.resolveActivity(send,
1484 PackageManager.MATCH_DEFAULT_ONLY);
1485 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1486
John Reck35e9dd62011-04-25 09:01:54 -07001487 boolean isNavDump = mSettings.enableNavDump();
Michael Kolb8233fac2010-10-26 16:08:53 -07001488 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1489 nav.setVisible(isNavDump);
1490 nav.setEnabled(isNavDump);
1491
John Reck35e9dd62011-04-25 09:01:54 -07001492 boolean showDebugSettings = mSettings.isDebugEnabled();
Michael Kolb8233fac2010-10-26 16:08:53 -07001493 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1494 counter.setVisible(showDebugSettings);
1495 counter.setEnabled(showDebugSettings);
1496
John Reckb3417f02011-01-14 11:01:05 -08001497 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
1498 newtab.setEnabled(getTabControl().canCreateNewTab());
Michael Kolb8233fac2010-10-26 16:08:53 -07001499
John Reck2bc80422011-06-30 15:11:49 -07001500 MenuItem saveSnapshot = menu.findItem(R.id.save_snapshot_menu_id);
1501 Tab tab = getCurrentTab();
1502 saveSnapshot.setVisible(tab != null && !tab.isSnapshot());
Michael Kolb8233fac2010-10-26 16:08:53 -07001503 break;
1504 }
1505 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001506 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001507 }
1508
1509 public boolean onOptionsItemSelected(MenuItem item) {
John Reckb3417f02011-01-14 11:01:05 -08001510 if (mOptionsMenuHandler != null &&
1511 mOptionsMenuHandler.onOptionsItemSelected(item)) {
1512 return true;
1513 }
1514
Michael Kolb8233fac2010-10-26 16:08:53 -07001515 if (item.getGroupId() != R.id.CONTEXT_MENU) {
1516 // menu remains active, so ensure comboview is dismissed
1517 // if main menu option is selected
1518 removeComboView();
1519 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001520 if (null == getCurrentTopWebView()) {
1521 return false;
1522 }
1523 if (mMenuIsDown) {
1524 // The shortcut action consumes the MENU. Even if it is still down,
1525 // it won't trigger the next shortcut action. In the case of the
1526 // shortcut action triggering a new activity, like Bookmarks, we
1527 // won't get onKeyUp for MENU. So it is important to reset it here.
1528 mMenuIsDown = false;
1529 }
1530 switch (item.getItemId()) {
1531 // -- Main menu
1532 case R.id.new_tab_menu_id:
1533 openTabToHomePage();
1534 break;
1535
1536 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001537 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001538 break;
1539
1540 case R.id.goto_menu_id:
1541 editUrl();
1542 break;
1543
1544 case R.id.bookmarks_menu_id:
1545 bookmarksOrHistoryPicker(false);
1546 break;
1547
Michael Kolb8233fac2010-10-26 16:08:53 -07001548 case R.id.add_bookmark_menu_id:
John Reck3ffc5ca2011-06-10 15:56:06 -07001549 bookmarkCurrentPage(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001550 break;
1551
1552 case R.id.stop_reload_menu_id:
1553 if (mInLoad) {
1554 stopLoading();
1555 } else {
1556 getCurrentTopWebView().reload();
1557 }
1558 break;
1559
1560 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001561 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001562 break;
1563
1564 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001565 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001566 break;
1567
1568 case R.id.close_menu_id:
1569 // Close the subwindow if it exists.
1570 if (mTabControl.getCurrentSubWindow() != null) {
1571 dismissSubWindow(mTabControl.getCurrentTab());
1572 break;
1573 }
1574 closeCurrentTab();
1575 break;
1576
1577 case R.id.homepage_menu_id:
1578 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001579 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001580 break;
1581
1582 case R.id.preferences_menu_id:
1583 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1584 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1585 getCurrentTopWebView().getUrl());
1586 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1587 break;
1588
1589 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001590 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001591 break;
1592
John Reck2bc80422011-06-30 15:11:49 -07001593 case R.id.save_snapshot_menu_id:
1594 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001595 if (source == null) break;
John Reckd8c74522011-06-14 08:45:00 -07001596 final ContentResolver cr = mActivity.getContentResolver();
1597 final ContentValues values = source.createSnapshotValues();
John Reck2bc80422011-06-30 15:11:49 -07001598 if (values != null) {
1599 new AsyncTask<Tab, Void, Long>() {
John Reckd8c74522011-06-14 08:45:00 -07001600
John Reck2bc80422011-06-30 15:11:49 -07001601 @Override
1602 protected Long doInBackground(Tab... params) {
1603 Uri result = cr.insert(Snapshots.CONTENT_URI, values);
1604 long id = ContentUris.parseId(result);
1605 return id;
John Reckd8c74522011-06-14 08:45:00 -07001606 }
John Reckf33b1632011-06-04 20:00:23 -07001607
John Reck2bc80422011-06-30 15:11:49 -07001608 @Override
1609 protected void onPostExecute(Long id) {
1610 Bundle b = new Bundle();
1611 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1612 mUi.showComboView(ComboViews.Snapshots, b);
1613 };
1614 }.execute(source);
1615 } else {
1616 Toast.makeText(mActivity, R.string.snapshot_failed,
Leon Scrogginsac993842011-02-02 12:54:07 -05001617 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001618 }
Leon Scrogginsac993842011-02-02 12:54:07 -05001619 break;
1620
Michael Kolb8233fac2010-10-26 16:08:53 -07001621 case R.id.page_info_menu_id:
Huahui Wuae0c0412011-06-28 10:17:05 -07001622 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
Michael Kolb8233fac2010-10-26 16:08:53 -07001623 break;
1624
1625 case R.id.classic_history_menu_id:
1626 bookmarksOrHistoryPicker(true);
1627 break;
1628
1629 case R.id.title_bar_share_page_url:
1630 case R.id.share_page_menu_id:
1631 Tab currentTab = mTabControl.getCurrentTab();
1632 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001633 return false;
1634 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001635 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001636 break;
1637
1638 case R.id.dump_nav_menu_id:
1639 getCurrentTopWebView().debugDump();
1640 break;
1641
1642 case R.id.dump_counters_menu_id:
1643 getCurrentTopWebView().dumpV8Counters();
1644 break;
1645
1646 case R.id.zoom_in_menu_id:
1647 getCurrentTopWebView().zoomIn();
1648 break;
1649
1650 case R.id.zoom_out_menu_id:
1651 getCurrentTopWebView().zoomOut();
1652 break;
1653
1654 case R.id.view_downloads_menu_id:
1655 viewDownloads();
1656 break;
1657
1658 case R.id.window_one_menu_id:
1659 case R.id.window_two_menu_id:
1660 case R.id.window_three_menu_id:
1661 case R.id.window_four_menu_id:
1662 case R.id.window_five_menu_id:
1663 case R.id.window_six_menu_id:
1664 case R.id.window_seven_menu_id:
1665 case R.id.window_eight_menu_id:
1666 {
1667 int menuid = item.getItemId();
1668 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1669 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1670 Tab desiredTab = mTabControl.getTab(id);
1671 if (desiredTab != null &&
1672 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001673 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001674 }
1675 break;
1676 }
1677 }
1678 }
1679 break;
1680
1681 default:
1682 return false;
1683 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001684 return true;
1685 }
1686
1687 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001688 // Let the History and Bookmark fragments handle menus they created.
1689 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1690 return false;
1691 }
1692
Michael Kolb8233fac2010-10-26 16:08:53 -07001693 int id = item.getItemId();
1694 boolean result = true;
1695 switch (id) {
1696 // For the context menu from the title bar
1697 case R.id.title_bar_copy_page_url:
1698 Tab currentTab = mTabControl.getCurrentTab();
1699 if (null == currentTab) {
1700 result = false;
1701 break;
1702 }
1703 WebView mainView = currentTab.getWebView();
1704 if (null == mainView) {
1705 result = false;
1706 break;
1707 }
1708 copy(mainView.getUrl());
1709 break;
1710 // -- Browser context menu
1711 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001712 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001713 case R.id.copy_link_context_menu_id:
1714 final WebView webView = getCurrentTopWebView();
1715 if (null == webView) {
1716 result = false;
1717 break;
1718 }
1719 final HashMap<String, WebView> hrefMap =
1720 new HashMap<String, WebView>();
1721 hrefMap.put("webview", webView);
1722 final Message msg = mHandler.obtainMessage(
1723 FOCUS_NODE_HREF, id, 0, hrefMap);
1724 webView.requestFocusNodeHref(msg);
1725 break;
1726
1727 default:
1728 // For other context menus
1729 result = onOptionsItemSelected(item);
1730 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001731 return result;
1732 }
1733
1734 /**
1735 * support programmatically opening the context menu
1736 */
1737 public void openContextMenu(View view) {
1738 mActivity.openContextMenu(view);
1739 }
1740
1741 /**
1742 * programmatically open the options menu
1743 */
1744 public void openOptionsMenu() {
1745 mActivity.openOptionsMenu();
1746 }
1747
1748 public boolean onMenuOpened(int featureId, Menu menu) {
1749 if (mOptionsMenuOpen) {
1750 if (mConfigChanged) {
1751 // We do not need to make any changes to the state of the
1752 // title bar, since the only thing that happened was a
1753 // change in orientation
1754 mConfigChanged = false;
1755 } else {
1756 if (!mExtendedMenuOpen) {
1757 mExtendedMenuOpen = true;
1758 mUi.onExtendedMenuOpened();
1759 } else {
1760 // Switching the menu back to icon view, so show the
1761 // title bar once again.
1762 mExtendedMenuOpen = false;
1763 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001764 }
1765 }
1766 } else {
1767 // The options menu is closed, so open it, and show the title
1768 mOptionsMenuOpen = true;
1769 mConfigChanged = false;
1770 mExtendedMenuOpen = false;
1771 mUi.onOptionsMenuOpened();
1772 }
1773 return true;
1774 }
1775
1776 public void onOptionsMenuClosed(Menu menu) {
1777 mOptionsMenuOpen = false;
1778 mUi.onOptionsMenuClosed(mInLoad);
1779 }
1780
1781 public void onContextMenuClosed(Menu menu) {
1782 mUi.onContextMenuClosed(menu, mInLoad);
1783 }
1784
1785 // Helper method for getting the top window.
1786 @Override
1787 public WebView getCurrentTopWebView() {
1788 return mTabControl.getCurrentTopWebView();
1789 }
1790
1791 @Override
1792 public WebView getCurrentWebView() {
1793 return mTabControl.getCurrentWebView();
1794 }
1795
1796 /*
1797 * This method is called as a result of the user selecting the options
1798 * menu to see the download window. It shows the download window on top of
1799 * the current window.
1800 */
1801 void viewDownloads() {
1802 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1803 mActivity.startActivity(intent);
1804 }
1805
1806 // action mode
1807
1808 void onActionModeStarted(ActionMode mode) {
1809 mUi.onActionModeStarted(mode);
1810 mActionMode = mode;
1811 }
1812
1813 /*
1814 * True if a custom ActionMode (i.e. find or select) is in use.
1815 */
1816 @Override
1817 public boolean isInCustomActionMode() {
1818 return mActionMode != null;
1819 }
1820
1821 /*
1822 * End the current ActionMode.
1823 */
1824 @Override
1825 public void endActionMode() {
1826 if (mActionMode != null) {
1827 mActionMode.finish();
1828 }
1829 }
1830
1831 /*
1832 * Called by find and select when they are finished. Replace title bars
1833 * as necessary.
1834 */
1835 public void onActionModeFinished(ActionMode mode) {
1836 if (!isInCustomActionMode()) return;
1837 mUi.onActionModeFinished(mInLoad);
1838 mActionMode = null;
1839 }
1840
1841 boolean isInLoad() {
1842 return mInLoad;
1843 }
1844
1845 // bookmark handling
1846
1847 /**
1848 * add the current page as a bookmark to the given folder id
1849 * @param folderId use -1 for the default folder
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001850 * @param canBeAnEdit If true, check to see whether the site is already
1851 * bookmarked, and if it is, edit that bookmark. If false, and
1852 * the site is already bookmarked, do not attempt to edit the
1853 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001854 */
1855 @Override
John Reck3ffc5ca2011-06-10 15:56:06 -07001856 public void bookmarkCurrentPage(boolean canBeAnEdit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001857 Intent i = new Intent(mActivity,
1858 AddBookmarkPage.class);
1859 WebView w = getCurrentTopWebView();
1860 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1861 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1862 String touchIconUrl = w.getTouchIconUrl();
1863 if (touchIconUrl != null) {
1864 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1865 WebSettings settings = w.getSettings();
1866 if (settings != null) {
1867 i.putExtra(AddBookmarkPage.USER_AGENT,
1868 settings.getUserAgentString());
1869 }
1870 }
1871 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1872 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1873 getDesiredThumbnailHeight(mActivity)));
1874 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001875 if (canBeAnEdit) {
1876 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1877 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001878 // Put the dialog at the upper right of the screen, covering the
1879 // star on the title bar.
1880 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
1881 mActivity.startActivity(i);
1882 }
1883
1884 // file chooser
1885 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1886 mUploadHandler = new UploadHandler(this);
1887 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1888 }
1889
1890 // thumbnails
1891
1892 /**
1893 * Return the desired width for thumbnail screenshots, which are stored in
1894 * the database, and used on the bookmarks screen.
1895 * @param context Context for finding out the density of the screen.
1896 * @return desired width for thumbnail screenshot.
1897 */
1898 static int getDesiredThumbnailWidth(Context context) {
1899 return context.getResources().getDimensionPixelOffset(
1900 R.dimen.bookmarkThumbnailWidth);
1901 }
1902
1903 /**
1904 * Return the desired height for thumbnail screenshots, which are stored in
1905 * the database, and used on the bookmarks screen.
1906 * @param context Context for finding out the density of the screen.
1907 * @return desired height for thumbnail screenshot.
1908 */
1909 static int getDesiredThumbnailHeight(Context context) {
1910 return context.getResources().getDimensionPixelOffset(
1911 R.dimen.bookmarkThumbnailHeight);
1912 }
1913
Michael Kolb1acef692011-03-08 14:12:06 -08001914 static Bitmap createScreenshot(Tab tab, int width, int height) {
1915 if ((tab != null) && (tab.getWebView() != null)) {
1916 return createScreenshot(tab.getWebView(), width, height);
1917 }
1918 return null;
1919 }
1920
John Reck8cc92352011-07-06 17:41:52 -07001921 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08001922 // We render to a bitmap 2x the desired size so that we can then
1923 // re-scale it with filtering since canvas.scale doesn't filter
1924 // This helps reduce aliasing at the cost of being slightly blurry
1925 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07001926 Picture thumbnail = view.capturePicture();
1927 if (thumbnail == null) {
1928 return null;
1929 }
John Reck5c6ac2f2011-01-05 10:18:03 -08001930 width *= filter_scale;
1931 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07001932 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1933 Canvas canvas = new Canvas(bm);
1934 // May need to tweak these values to determine what is the
1935 // best scale factor
1936 int thumbnailWidth = thumbnail.getWidth();
1937 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08001938 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08001939 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08001940 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07001941 } else {
1942 return null;
1943 }
John Reckfe49ab42010-11-16 17:09:37 -08001944
Michael Kolbeb95db42011-03-03 10:38:40 -08001945 float scaleFactorY = (float) height / (float)thumbnailHeight;
1946 if (scaleFactorY > scaleFactor) {
1947 // The picture is narrower than the requested AR
1948 // Center the thumnail and crop the sides
1949 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08001950 float wx = (thumbnailWidth * scaleFactor) - width;
1951 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07001952 }
1953
John Reckfe49ab42010-11-16 17:09:37 -08001954 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07001955
1956 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08001957 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
1958 height / filter_scale, true);
1959 bm.recycle();
1960 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07001961 }
1962
John Reck34ef2672011-02-10 11:30:55 -08001963 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001964 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07001965 // FIXME: Would like to make sure there is actually something to
1966 // draw, but the API for that (WebViewCore.pictureReady()) is not
1967 // currently accessible here.
1968
John Reck34ef2672011-02-10 11:30:55 -08001969 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08001970 if (view == null) {
1971 // Tab was destroyed
1972 return;
1973 }
John Reck34ef2672011-02-10 11:30:55 -08001974 final String url = tab.getUrl();
1975 final String originalUrl = view.getOriginalUrl();
1976
1977 if (TextUtils.isEmpty(url)) {
1978 return;
1979 }
1980
1981 // Only update thumbnails for web urls (http(s)://), not for
1982 // about:, javascript:, data:, etc...
1983 // Unless it is a bookmarked site, then always update
1984 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
1985 return;
1986 }
1987
Michael Kolb8233fac2010-10-26 16:08:53 -07001988 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
1989 getDesiredThumbnailHeight(mActivity));
1990 if (bm == null) {
1991 return;
1992 }
1993
1994 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08001995 new AsyncTask<Void, Void, Void>() {
1996 @Override
1997 protected Void doInBackground(Void... unused) {
1998 Cursor cursor = null;
1999 try {
2000 // TODO: Clean this up
2001 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2002 if (cursor != null && cursor.moveToFirst()) {
2003 final ByteArrayOutputStream os =
2004 new ByteArrayOutputStream();
2005 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002006
John Reck34ef2672011-02-10 11:30:55 -08002007 ContentValues values = new ContentValues();
2008 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002009
John Reck34ef2672011-02-10 11:30:55 -08002010 do {
John Reck617fd832011-02-16 14:35:59 -08002011 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002012 cr.update(Images.CONTENT_URI, values, null, null);
2013 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002014 }
John Reck34ef2672011-02-10 11:30:55 -08002015 } catch (IllegalStateException e) {
2016 // Ignore
2017 } finally {
2018 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002019 }
John Reck34ef2672011-02-10 11:30:55 -08002020 return null;
2021 }
2022 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002023 }
2024
2025 private class Copy implements OnMenuItemClickListener {
2026 private CharSequence mText;
2027
2028 public boolean onMenuItemClick(MenuItem item) {
2029 copy(mText);
2030 return true;
2031 }
2032
2033 public Copy(CharSequence toCopy) {
2034 mText = toCopy;
2035 }
2036 }
2037
Leon Scroggins63c02662010-11-18 15:16:27 -05002038 private static class Download implements OnMenuItemClickListener {
2039 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002040 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002041 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002042
2043 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002044 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002045 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002046 return true;
2047 }
2048
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002049 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002050 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002051 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002052 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002053 }
2054 }
2055
Cary Clark8974d282010-11-22 10:46:05 -05002056 private static class SelectText implements OnMenuItemClickListener {
2057 private WebView mWebView;
2058
2059 public boolean onMenuItemClick(MenuItem item) {
2060 if (mWebView != null) {
2061 return mWebView.selectText();
2062 }
2063 return false;
2064 }
2065
2066 public SelectText(WebView webView) {
2067 mWebView = webView;
2068 }
2069
2070 }
2071
Michael Kolb8233fac2010-10-26 16:08:53 -07002072 /********************** TODO: UI stuff *****************************/
2073
2074 // these methods have been copied, they still need to be cleaned up
2075
2076 /****************** tabs ***************************************************/
2077
2078 // basic tab interactions:
2079
2080 // it is assumed that tabcontrol already knows about the tab
2081 protected void addTab(Tab tab) {
2082 mUi.addTab(tab);
2083 }
2084
2085 protected void removeTab(Tab tab) {
2086 mUi.removeTab(tab);
2087 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002088 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002089 }
2090
Michael Kolbf2055602011-04-09 17:20:03 -07002091 @Override
2092 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002093 // monkey protection against delayed start
2094 if (tab != null) {
2095 mTabControl.setCurrentTab(tab);
2096 // the tab is guaranteed to have a webview after setCurrentTab
2097 mUi.setActiveTab(tab);
2098 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002099 }
2100
2101 protected void closeEmptyChildTab() {
2102 Tab current = mTabControl.getCurrentTab();
2103 if (current != null
2104 && current.getWebView().copyBackForwardList().getSize() == 0) {
Michael Kolbc831b632011-05-11 09:30:34 -07002105 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002106 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002107 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002108 closeTab(current);
2109 }
2110 }
2111 }
2112
John Reck26b18322011-06-21 13:08:58 -07002113 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002114 // Dismiss the subwindow if applicable.
2115 dismissSubWindow(appTab);
2116 // Since we might kill the WebView, remove it from the
2117 // content view first.
2118 mUi.detachTab(appTab);
2119 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002120 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002121 // TODO: analyze why the remove and add are necessary
2122 mUi.attachTab(appTab);
2123 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002124 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002125 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002126 } else {
2127 // If the tab was the current tab, we have to attach
2128 // it to the view system again.
2129 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002130 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002131 }
2132 }
2133
2134 // Remove the sub window if it exists. Also called by TabControl when the
2135 // user clicks the 'X' to dismiss a sub window.
2136 public void dismissSubWindow(Tab tab) {
2137 removeSubWindow(tab);
2138 // dismiss the subwindow. This will destroy the WebView.
2139 tab.dismissSubWindow();
2140 getCurrentTopWebView().requestFocus();
2141 }
2142
2143 @Override
2144 public void removeSubWindow(Tab t) {
2145 if (t.getSubWebView() != null) {
2146 mUi.removeSubWindow(t.getSubViewContainer());
2147 }
2148 }
2149
2150 @Override
2151 public void attachSubWindow(Tab tab) {
2152 if (tab.getSubWebView() != null) {
2153 mUi.attachSubWindow(tab.getSubViewContainer());
2154 getCurrentTopWebView().requestFocus();
2155 }
2156 }
2157
Mathew Inwood29721c22011-06-29 17:55:24 +01002158 private Tab showPreloadedTab(final UrlData urlData) {
2159 if (!urlData.isPreloaded()) {
2160 return null;
2161 }
2162 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2163 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2164 if (sbQuery != null) {
2165 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
2166 // Could not submit query. Fallback to regular tab creation
2167 tabControl.destroy();
2168 return null;
2169 }
2170 }
2171 Tab t = tabControl.getTab();
2172 mTabControl.addPreloadedTab(t);
2173 addTab(t);
2174 setActiveTab(t);
2175 return t;
2176 }
2177
Michael Kolb7bcafde2011-05-09 13:55:59 -07002178 // open a non inconito tab with the given url data
2179 // and set as active tab
2180 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002181 Tab tab = showPreloadedTab(urlData);
2182 if (tab == null) {
2183 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002184 if ((tab != null) && !urlData.isEmpty()) {
2185 loadUrlDataIn(tab, urlData);
2186 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002187 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002188 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002189 }
2190
Michael Kolb843510f2010-12-09 10:51:49 -08002191 @Override
2192 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002193 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002194 }
2195
Michael Kolb8233fac2010-10-26 16:08:53 -07002196 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002197 public Tab openIncognitoTab() {
2198 return openTab(INCOGNITO_URI, true, true, false);
2199 }
2200
2201 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002202 public Tab openTab(String url, boolean incognito, boolean setActive,
2203 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002204 return openTab(url, incognito, setActive, useCurrent, null);
2205 }
2206
2207 @Override
2208 public Tab openTab(String url, Tab parent, boolean setActive,
2209 boolean useCurrent) {
2210 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2211 setActive, useCurrent, parent);
2212 }
2213
2214 public Tab openTab(String url, boolean incognito, boolean setActive,
2215 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002216 Tab tab = createNewTab(incognito, setActive, useCurrent);
2217 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002218 if (parent != null && parent != tab) {
2219 parent.addChildTab(tab);
2220 }
Michael Kolb519d2282011-05-09 17:03:19 -07002221 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002222 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002223 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002224 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002225 return tab;
2226 }
2227
2228 // this method will attempt to create a new tab
2229 // incognito: private browsing tab
2230 // setActive: ste tab as current tab
2231 // useCurrent: if no new tab can be created, return current tab
2232 private Tab createNewTab(boolean incognito, boolean setActive,
2233 boolean useCurrent) {
2234 Tab tab = null;
2235 if (mTabControl.canCreateNewTab()) {
2236 tab = mTabControl.createNewTab(incognito);
2237 addTab(tab);
2238 if (setActive) {
2239 setActiveTab(tab);
2240 }
2241 } else {
2242 if (useCurrent) {
2243 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002244 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002245 } else {
2246 mUi.showMaxTabsWarning();
2247 }
2248 }
2249 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002250 }
2251
John Reck2bc80422011-06-30 15:11:49 -07002252 @Override
2253 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2254 SnapshotTab tab = null;
2255 if (mTabControl.canCreateNewTab()) {
2256 tab = mTabControl.createSnapshotTab(snapshotId);
2257 addTab(tab);
2258 if (setActive) {
2259 setActiveTab(tab);
2260 }
2261 } else {
2262 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002263 }
2264 return tab;
2265 }
2266
Michael Kolb8233fac2010-10-26 16:08:53 -07002267 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002268 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002269 * @return boolean True if we successfully switched to a different tab. If
2270 * the indexth tab is null, or if that tab is the same as
2271 * the current one, return false.
2272 */
2273 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002274 public boolean switchToTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002275 // hide combo view if open
2276 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002277 Tab currentTab = mTabControl.getCurrentTab();
2278 if (tab == null || tab == currentTab) {
2279 return false;
2280 }
2281 setActiveTab(tab);
2282 return true;
2283 }
2284
2285 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002286 public void closeCurrentTab() {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002287 // hide combo view if open
2288 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002289 if (mTabControl.getTabCount() == 1) {
John Reck958b2422010-12-03 17:56:17 -08002290 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002291 return;
2292 }
Michael Kolbc831b632011-05-11 09:30:34 -07002293 final Tab current = mTabControl.getCurrentTab();
2294 final int pos = mTabControl.getCurrentPosition();
2295 Tab newTab = current.getParent();
2296 if (newTab == null) {
2297 newTab = mTabControl.getTab(pos + 1);
2298 if (newTab == null) {
2299 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002300 }
2301 }
Michael Kolbc831b632011-05-11 09:30:34 -07002302 if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002303 // Close window
2304 closeTab(current);
2305 }
2306 }
2307
2308 /**
2309 * Close the tab, remove its associated title bar, and adjust mTabControl's
2310 * current tab to a valid value.
2311 */
2312 @Override
2313 public void closeTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002314 // hide combo view if open
2315 removeComboView();
Michael Kolb2d59c322011-01-25 13:18:55 -08002316 removeTab(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002317 }
2318
Michael Kolb8233fac2010-10-26 16:08:53 -07002319 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002320 protected void loadUrlFromContext(String url) {
2321 Tab tab = getCurrentTab();
2322 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002323 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002324 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002325 url = UrlUtils.smartUrlFilter(url);
2326 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002327 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002328 }
2329 }
2330 }
2331
2332 /**
2333 * Load the URL into the given WebView and update the title bar
2334 * to reflect the new load. Call this instead of WebView.loadUrl
2335 * directly.
2336 * @param view The WebView used to load url.
2337 * @param url The URL to load.
2338 */
John Reck71e51422011-07-01 16:49:28 -07002339 @Override
2340 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002341 loadUrl(tab, url, null);
2342 }
2343
2344 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2345 if (tab != null) {
2346 dismissSubWindow(tab);
2347 tab.loadUrl(url, headers);
2348 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002349 }
2350
2351 /**
2352 * Load UrlData into a Tab and update the title bar to reflect the new
2353 * load. Call this instead of UrlData.loadIn directly.
2354 * @param t The Tab used to load.
2355 * @param data The UrlData being loaded.
2356 */
2357 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002358 if (data != null) {
2359 if (data.mVoiceIntent != null) {
2360 t.activateVoiceSearchMode(data.mVoiceIntent);
Michael Kolb14612442011-06-24 13:06:29 -07002361 } else if (data.isPreloaded()) {
2362 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002363 } else {
2364 loadUrl(t, data.mUrl, data.mHeaders);
2365 }
2366 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002367 }
2368
John Reck30c714c2010-12-16 17:30:34 -08002369 @Override
2370 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002371 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002372 if (tab.canGoBack()) {
2373 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002374 } else {
John Reckef654f12011-07-12 16:42:08 -07002375 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002376 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002377 }
2378
2379 void goBackOnePageOrQuit() {
2380 Tab current = mTabControl.getCurrentTab();
2381 if (current == null) {
2382 /*
2383 * Instead of finishing the activity, simply push this to the back
2384 * of the stack and let ActivityManager to choose the foreground
2385 * activity. As BrowserActivity is singleTask, it will be always the
2386 * root of the task. So we can use either true or false for
2387 * moveTaskToBack().
2388 */
2389 mActivity.moveTaskToBack(true);
2390 return;
2391 }
John Reckef654f12011-07-12 16:42:08 -07002392 if (current.canGoBack()) {
2393 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002394 } else {
2395 // Check to see if we are closing a window that was created by
2396 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002397 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002398 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002399 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002400 // Now we close the other tab
2401 closeTab(current);
2402 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002403 /*
2404 * Instead of finishing the activity, simply push this to the back
2405 * of the stack and let ActivityManager to choose the foreground
2406 * activity. As BrowserActivity is singleTask, it will be always the
2407 * root of the task. So we can use either true or false for
2408 * moveTaskToBack().
2409 */
2410 mActivity.moveTaskToBack(true);
2411 }
2412 }
2413 }
2414
2415 /**
2416 * Feed the previously stored results strings to the BrowserProvider so that
2417 * the SearchDialog will show them instead of the standard searches.
2418 * @param result String to show on the editable line of the SearchDialog.
2419 */
2420 @Override
2421 public void showVoiceSearchResults(String result) {
2422 ContentProviderClient client = mActivity.getContentResolver()
2423 .acquireContentProviderClient(Browser.BOOKMARKS_URI);
2424 ContentProvider prov = client.getLocalContentProvider();
2425 BrowserProvider bp = (BrowserProvider) prov;
2426 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
2427 client.release();
2428
2429 Bundle bundle = createGoogleSearchSourceBundle(
2430 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
2431 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
2432 startSearch(result, false, bundle, false);
2433 }
2434
2435 private void startSearch(String initialQuery, boolean selectInitialQuery,
2436 Bundle appSearchData, boolean globalSearch) {
2437 if (appSearchData == null) {
2438 appSearchData = createGoogleSearchSourceBundle(
2439 GOOGLE_SEARCH_SOURCE_TYPE);
2440 }
2441
2442 SearchEngine searchEngine = mSettings.getSearchEngine();
2443 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
2444 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
2445 }
2446 mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData,
2447 globalSearch);
2448 }
2449
2450 private Bundle createGoogleSearchSourceBundle(String source) {
2451 Bundle bundle = new Bundle();
2452 bundle.putString(Search.SOURCE, source);
2453 return bundle;
2454 }
2455
2456 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002457 * helper method for key handler
2458 * returns the current tab if it can't advance
2459 */
Michael Kolbc831b632011-05-11 09:30:34 -07002460 private Tab getNextTab() {
2461 return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1,
2462 mTabControl.getCurrentPosition() + 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002463 }
2464
2465 /**
2466 * helper method for key handler
2467 * returns the current tab if it can't advance
2468 */
Michael Kolbc831b632011-05-11 09:30:34 -07002469 private Tab getPrevTab() {
2470 return mTabControl.getTab(Math.max(0,
2471 mTabControl.getCurrentPosition() - 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002472 }
2473
2474 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002475 * handle key events in browser
2476 *
2477 * @param keyCode
2478 * @param event
2479 * @return true if handled, false to pass to super
2480 */
2481 boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002482 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002483 // Even if MENU is already held down, we need to call to super to open
2484 // the IME on long press.
Michael Kolb2814a362011-05-19 15:49:41 -07002485 if (KeyEvent.KEYCODE_MENU == keyCode) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002486 if (mOptionsMenuHandler != null) {
2487 return false;
2488 } else {
2489 event.startTracking();
2490 return true;
2491 }
Michael Kolb2814a362011-05-19 15:49:41 -07002492 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002493 if (!noModifiers
2494 && ((KeyEvent.KEYCODE_MENU == keyCode)
2495 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2496 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002497 mMenuIsDown = true;
2498 return false;
2499 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002500
Cary Clark8ff8c662010-12-29 15:03:05 -05002501 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002502 Tab tab = getCurrentTab();
2503 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002504
Cary Clark160bbb92011-01-10 11:17:07 -05002505 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2506 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002507
Michael Kolb8233fac2010-10-26 16:08:53 -07002508 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002509 case KeyEvent.KEYCODE_TAB:
2510 if (event.isCtrlPressed()) {
2511 if (event.isShiftPressed()) {
2512 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002513 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002514 } else {
2515 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002516 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002517 }
2518 return true;
2519 }
2520 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002521 case KeyEvent.KEYCODE_SPACE:
2522 // WebView/WebTextView handle the keys in the KeyDown. As
2523 // the Activity's shortcut keys are only handled when WebView
2524 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002525 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002526 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002527 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002528 pageDown();
2529 }
2530 return true;
2531 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002532 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002533 event.startTracking();
2534 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002535 case KeyEvent.KEYCODE_DPAD_LEFT:
2536 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002537 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002538 return true;
2539 }
2540 break;
2541 case KeyEvent.KEYCODE_DPAD_RIGHT:
2542 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002543 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002544 return true;
2545 }
2546 break;
2547 case KeyEvent.KEYCODE_A:
2548 if (ctrl) {
2549 webView.selectAll();
2550 return true;
2551 }
2552 break;
Michael Kolba4183062011-01-16 10:43:21 -08002553// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002554 case KeyEvent.KEYCODE_C:
2555 if (ctrl) {
2556 webView.copySelection();
2557 return true;
2558 }
2559 break;
Michael Kolba4183062011-01-16 10:43:21 -08002560// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002561// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002562// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002563// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002564// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002565// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002566// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002567// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002568// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002569// case KeyEvent.KEYCODE_M: // unused
2570// case KeyEvent.KEYCODE_N: // in Chrome: new window
2571// case KeyEvent.KEYCODE_O: // in Chrome: open file
2572// case KeyEvent.KEYCODE_P: // in Chrome: print page
2573// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002574// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002575// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2576 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002577 // we can't use the ctrl/shift flags, they check for
2578 // exclusive use of a modifier
2579 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002580 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002581 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002582 } else {
2583 openTabToHomePage();
2584 }
2585 return true;
2586 }
2587 break;
2588// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2589// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb1a2eba42011-03-16 16:42:49 -07002590 case KeyEvent.KEYCODE_W: // in Chrome: close tab
2591 if (ctrl) {
2592 closeCurrentTab();
2593 return true;
2594 }
2595 break;
Cary Clark8ff8c662010-12-29 15:03:05 -05002596// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2597// case KeyEvent.KEYCODE_Y: // unused
2598// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002599 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002600 // it is a regular key and webview is not null
2601 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002602 }
2603
John Recke6bf4ab2011-02-24 15:48:05 -08002604 boolean onKeyLongPress(int keyCode, KeyEvent event) {
2605 switch(keyCode) {
2606 case KeyEvent.KEYCODE_BACK:
2607 if (mUi.showsWeb()) {
2608 bookmarksOrHistoryPicker(true);
2609 return true;
2610 }
2611 break;
2612 }
2613 return false;
2614 }
2615
Michael Kolb8233fac2010-10-26 16:08:53 -07002616 boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb2814a362011-05-19 15:49:41 -07002617 if (KeyEvent.KEYCODE_MENU == keyCode) {
2618 mMenuIsDown = false;
2619 if (event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002620 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002621 }
2622 }
Cary Clark160bbb92011-01-10 11:17:07 -05002623 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002624 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002625 case KeyEvent.KEYCODE_BACK:
2626 if (event.isTracking() && !event.isCanceled()) {
2627 onBackKey();
2628 return true;
2629 }
2630 break;
2631 }
2632 return false;
2633 }
2634
2635 public boolean isMenuDown() {
2636 return mMenuIsDown;
2637 }
2638
Ben Murdoch8029a772010-11-16 11:58:21 +00002639 public void setupAutoFill(Message message) {
2640 // Open the settings activity at the AutoFill profile fragment so that
2641 // the user can create a new profile. When they return, we will dispatch
2642 // the message so that we can autofill the form using their new profile.
2643 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2644 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2645 AutoFillSettingsFragment.class.getName());
2646 mAutoFillSetupMessage = message;
2647 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2648 }
John Reckb3417f02011-01-14 11:01:05 -08002649
2650 @Override
2651 public void registerOptionsMenuHandler(OptionsMenuHandler handler) {
2652 mOptionsMenuHandler = handler;
2653 }
2654
2655 @Override
2656 public void unregisterOptionsMenuHandler(OptionsMenuHandler handler) {
2657 if (mOptionsMenuHandler == handler) {
2658 mOptionsMenuHandler = null;
2659 }
2660 }
2661
Narayan Kamath5119edd2011-02-23 15:49:17 +00002662 @Override
2663 public void registerDropdownChangeListener(DropdownChangeListener d) {
2664 mUi.registerDropdownChangeListener(d);
2665 }
Michael Kolbfbc579a2011-07-07 15:59:33 -07002666
2667 public boolean onSearchRequested() {
2668 mUi.editUrl(false);
2669 return true;
2670 }
2671
Michael Kolb8233fac2010-10-26 16:08:53 -07002672}