blob: 75129496f210fd4acd9ba2cc416f1b5949d07794 [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;
70import android.webkit.SslErrorHandler;
71import android.webkit.ValueCallback;
72import android.webkit.WebChromeClient;
73import android.webkit.WebIconDatabase;
74import android.webkit.WebSettings;
75import android.webkit.WebView;
Leon Scrogginsac993842011-02-02 12:54:07 -050076import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070077
Michael Kolb4bd767d2011-05-27 11:33:55 -070078import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070079import com.android.browser.UI.ComboViews;
Michael Kolb4bd767d2011-05-27 11:33:55 -070080import com.android.browser.UI.DropdownChangeListener;
81import com.android.browser.provider.BrowserProvider;
John Reck8cc92352011-07-06 17:41:52 -070082import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070083import com.android.browser.search.SearchEngine;
84import com.android.common.Search;
85
Michael Kolb8233fac2010-10-26 16:08:53 -070086import java.io.ByteArrayOutputStream;
87import java.io.File;
88import java.net.URLEncoder;
89import java.util.Calendar;
90import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080091import java.util.List;
John Reck26b18322011-06-21 13:08:58 -070092import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -070093
94/**
95 * Controller for browser
96 */
97public class Controller
98 implements WebViewController, UiController {
99
100 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800101 private static final String SEND_APP_ID_EXTRA =
102 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700103 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800104
Michael Kolb8233fac2010-10-26 16:08:53 -0700105
106 // public message ids
107 public final static int LOAD_URL = 1001;
108 public final static int STOP_LOAD = 1002;
109
110 // Message Ids
111 private static final int FOCUS_NODE_HREF = 102;
112 private static final int RELEASE_WAKELOCK = 107;
113
114 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
115
116 private static final int OPEN_BOOKMARKS = 201;
117
118 private static final int EMPTY_MENU = -1;
119
Michael Kolb8233fac2010-10-26 16:08:53 -0700120 // activity requestCode
121 final static int PREFERENCES_PAGE = 3;
122 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000123 final static int AUTOFILL_SETUP = 5;
124
Michael Kolb8233fac2010-10-26 16:08:53 -0700125 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
126
127 // As the ids are dynamically created, we can't guarantee that they will
128 // be in sequence, so this static array maps ids to a window number.
129 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
130 { R.id.window_one_menu_id, R.id.window_two_menu_id,
131 R.id.window_three_menu_id, R.id.window_four_menu_id,
132 R.id.window_five_menu_id, R.id.window_six_menu_id,
133 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
134
135 // "source" parameter for Google search through search key
136 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
137 // "source" parameter for Google search through simplily type
138 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
139
Guang Zhu9e78f512011-05-04 11:45:11 -0700140 // "no-crash-recovery" parameter in intetnt to suppress crash recovery
141 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
142
Michael Kolb8233fac2010-10-26 16:08:53 -0700143 private Activity mActivity;
144 private UI mUi;
145 private TabControl mTabControl;
146 private BrowserSettings mSettings;
147 private WebViewFactory mFactory;
John Reckb3417f02011-01-14 11:01:05 -0800148 private OptionsMenuHandler mOptionsMenuHandler = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700149
150 private WakeLock mWakeLock;
151
152 private UrlHandler mUrlHandler;
153 private UploadHandler mUploadHandler;
154 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700155 private PageDialogsHandler mPageDialogsHandler;
156 private NetworkStateHandler mNetworkHandler;
Martijn Coenenb2f93552011-06-14 10:48:35 +0200157 private NfcHandler mNfcHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700158
Ben Murdoch8029a772010-11-16 11:58:21 +0000159 private Message mAutoFillSetupMessage;
160
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 private boolean mShouldShowErrorConsole;
162
163 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
164
165 // FIXME, temp address onPrepareMenu performance problem.
166 // When we move everything out of view, we should rewrite this.
167 private int mCurrentMenuState = 0;
168 private int mMenuState = R.id.MAIN_MENU;
169 private int mOldMenuState = EMPTY_MENU;
170 private Menu mCachedMenu;
171
Michael Kolb8233fac2010-10-26 16:08:53 -0700172 private boolean mMenuIsDown;
173
174 // For select and find, we keep track of the ActionMode so that
175 // finish() can be called as desired.
176 private ActionMode mActionMode;
177
178 /**
179 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
180 * of whether the configuration has changed. The first onMenuOpened call
181 * after a configuration change is simply a reopening of the same menu
182 * (i.e. mIconView did not change).
183 */
184 private boolean mConfigChanged;
185
186 /**
187 * Keeps track of whether the options menu is open. This is important in
188 * determining whether to show or hide the title bar overlay
189 */
190 private boolean mOptionsMenuOpen;
191
192 /**
193 * Whether or not the options menu is in its bigger, popup menu form. When
194 * true, we want the title bar overlay to be gone. When false, we do not.
195 * Only meaningful if mOptionsMenuOpen is true.
196 */
197 private boolean mExtendedMenuOpen;
198
199 private boolean mInLoad;
200
201 private boolean mActivityPaused = true;
202 private boolean mLoadStopped;
203
204 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500205 // Checks to see when the bookmarks database has changed, and updates the
206 // Tabs' notion of whether they represent bookmarked sites.
207 private ContentObserver mBookmarksObserver;
John Reck0ebd3ac2010-12-09 11:14:04 -0800208 private DataController mDataController;
John Reck847b5322011-04-14 17:02:18 -0700209 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700210
211 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
212 @Override
213 public Void doInBackground(File... files) {
214 if (files != null) {
215 for (File f : files) {
216 if (!f.delete()) {
217 Log.e(LOGTAG, f.getPath() + " was not deleted");
218 }
219 }
220 }
221 return null;
222 }
223 }
224
225 public Controller(Activity browser) {
226 mActivity = browser;
227 mSettings = BrowserSettings.getInstance();
John Reck0ebd3ac2010-12-09 11:14:04 -0800228 mDataController = DataController.getInstance(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700229 mTabControl = new TabControl(this);
230 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700231 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
Michael Kolb14612442011-06-24 13:06:29 -0700232 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700233
234 mUrlHandler = new UrlHandler(this);
235 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200237 mNfcHandler = new NfcHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700238
239 PowerManager pm = (PowerManager) mActivity
240 .getSystemService(Context.POWER_SERVICE);
241 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
242
243 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500244 mBookmarksObserver = new ContentObserver(mHandler) {
245 @Override
246 public void onChange(boolean selfChange) {
247 int size = mTabControl.getTabCount();
248 for (int i = 0; i < size; i++) {
249 mTabControl.getTab(i).updateBookmarkedStatus();
250 }
251 }
252
253 };
254 browser.getContentResolver().registerContentObserver(
255 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700256
257 mNetworkHandler = new NetworkStateHandler(mActivity, this);
258 // Start watching the default geolocation permissions
259 mSystemAllowGeolocationOrigins =
260 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
261 mSystemAllowGeolocationOrigins.start();
262
263 retainIconsOnStartup();
264 }
265
Patrick Scott7d50a932011-02-04 09:27:26 -0500266 void start(final Bundle icicle, final Intent intent) {
Guang Zhu9e78f512011-05-04 11:45:11 -0700267 boolean noCrashRecovery = intent.getBooleanExtra(NO_CRASH_RECOVERY, false);
268 if (icicle != null || noCrashRecovery) {
John Reck847b5322011-04-14 17:02:18 -0700269 doStart(icicle, intent);
270 } else {
271 mCrashRecoveryHandler.startRecovery(intent);
272 }
273 }
274
275 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700276 // Unless the last browser usage was within 24 hours, destroy any
277 // remaining incognito tabs.
278
279 Calendar lastActiveDate = icicle != null ?
280 (Calendar) icicle.getSerializable("lastActiveDate") : null;
281 Calendar today = Calendar.getInstance();
282 Calendar yesterday = Calendar.getInstance();
283 yesterday.add(Calendar.DATE, -1);
284
Patrick Scott7d50a932011-02-04 09:27:26 -0500285 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700286 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800287 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700288
Patrick Scott7d50a932011-02-04 09:27:26 -0500289 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700290 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500291 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000292
Michael Kolbc831b632011-05-11 09:30:34 -0700293 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500294 // Not able to restore so we go ahead and clear session cookies. We
295 // must do this before trying to login the user as we don't want to
296 // clear any session cookies set during login.
297 CookieManager.getInstance().removeSessionCookie();
298 }
299
Patrick Scottd43e75a2011-03-14 14:47:23 -0400300 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500301 new Runnable() {
302 @Override public void run() {
Michael Kolbc831b632011-05-11 09:30:34 -0700303 onPreloginFinished(icicle, intent, currentTabId, restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500304 }
305 });
306 }
307
Michael Kolbc831b632011-05-11 09:30:34 -0700308 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500309 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700310 if (currentTabId == -1) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700311 final Bundle extra = intent.getExtras();
312 // Create an initial tab.
313 // If the intent is ACTION_VIEW and data is not null, the Browser is
314 // invoked to view the content by another application. In this case,
315 // the tab will be close when exit.
Michael Kolb14612442011-06-24 13:06:29 -0700316 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
Michael Kolb7bcafde2011-05-09 13:55:59 -0700317 Tab t = null;
318 if (urlData.isEmpty()) {
319 t = openTabToHomePage();
320 } else {
321 t = openTab(urlData);
322 }
323 if (t != null) {
324 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
325 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700326 WebView webView = t.getWebView();
327 if (extra != null) {
328 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
329 if (scale > 0 && scale <= 1000) {
330 webView.setInitialScale(scale);
331 }
332 }
John Reckd8c74522011-06-14 08:45:00 -0700333 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700334 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700335 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500336 mUi.needsRestoreAllTabs());
Michael Kolb1bf23132010-11-19 12:55:12 -0800337 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700338 // TabControl.restoreState() will create a new tab even if
339 // restoring the state fails.
340 setActiveTab(mTabControl.getCurrentTab());
John Reckdb22ec42011-06-29 11:31:24 -0700341 // Handle the intent
342 mIntentHandler.onNewIntent(intent);
Michael Kolb8233fac2010-10-26 16:08:53 -0700343 }
344 // clear up the thumbnail directory, which is no longer used;
345 // ideally this should only be run once after an upgrade from
346 // a previous version of the browser
347 new ClearThumbnails().execute(mTabControl.getThumbnailDir()
348 .listFiles());
349 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700350 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700351 if (jsFlags.trim().length() != 0) {
352 getCurrentWebView().setJsFlags(jsFlags);
353 }
John Reck439c9a52010-12-14 10:04:39 -0800354 if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
355 bookmarksOrHistoryPicker(false);
356 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700357 }
358
Michael Kolb1514bb72010-11-22 09:11:48 -0800359 @Override
360 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700361 return mFactory;
362 }
363
364 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800365 public void onSetWebView(Tab tab, WebView view) {
366 mUi.onSetWebView(tab, view);
367 }
368
369 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800370 public void createSubWindow(Tab tab) {
371 endActionMode();
372 WebView mainView = tab.getWebView();
373 WebView subView = mFactory.createWebView((mainView == null)
374 ? false
375 : mainView.isPrivateBrowsingEnabled());
376 mUi.createSubWindow(tab, subView);
377 }
378
379 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700380 public Context getContext() {
381 return mActivity;
382 }
383
384 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700385 public Activity getActivity() {
386 return mActivity;
387 }
388
389 void setUi(UI ui) {
390 mUi = ui;
391 }
392
393 BrowserSettings getSettings() {
394 return mSettings;
395 }
396
397 IntentHandler getIntentHandler() {
398 return mIntentHandler;
399 }
400
401 @Override
402 public UI getUi() {
403 return mUi;
404 }
405
406 int getMaxTabs() {
407 return mActivity.getResources().getInteger(R.integer.max_tabs);
408 }
409
410 @Override
411 public TabControl getTabControl() {
412 return mTabControl;
413 }
414
Michael Kolb1bf23132010-11-19 12:55:12 -0800415 @Override
416 public List<Tab> getTabs() {
417 return mTabControl.getTabs();
418 }
419
Michael Kolb8233fac2010-10-26 16:08:53 -0700420 // Open the icon database and retain all the icons for visited sites.
Ben Murdoch9446b932010-11-25 16:20:14 +0000421 // This is done on a background thread so as not to stall startup.
Michael Kolb8233fac2010-10-26 16:08:53 -0700422 private void retainIconsOnStartup() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000423 // WebIconDatabase needs to be retrieved on the UI thread so that if
424 // it has not been created successfully yet the Handler is started on the
425 // UI thread.
426 new RetainIconsOnStartupTask(WebIconDatabase.getInstance()).execute();
427 }
428
429 private class RetainIconsOnStartupTask extends AsyncTask<Void, Void, Void> {
430 private WebIconDatabase mDb;
431
432 public RetainIconsOnStartupTask(WebIconDatabase db) {
433 mDb = db;
434 }
435
John Recka00cbbd2010-12-16 12:38:19 -0800436 @Override
Ben Murdoch9446b932010-11-25 16:20:14 +0000437 protected Void doInBackground(Void... unused) {
438 mDb.open(mActivity.getDir("icons", 0).getPath());
439 Cursor c = null;
440 try {
441 c = Browser.getAllBookmarks(mActivity.getContentResolver());
442 if (c.moveToFirst()) {
443 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
444 do {
445 String url = c.getString(urlIndex);
446 mDb.retainIconForPageUrl(url);
447 } while (c.moveToNext());
448 }
449 } catch (IllegalStateException e) {
450 Log.e(LOGTAG, "retainIconsOnStartup", e);
451 } finally {
452 if (c != null) c.close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700453 }
Ben Murdoch9446b932010-11-25 16:20:14 +0000454
455 return null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700456 }
457 }
458
459 private void startHandler() {
460 mHandler = new Handler() {
461
462 @Override
463 public void handleMessage(Message msg) {
464 switch (msg.what) {
465 case OPEN_BOOKMARKS:
466 bookmarksOrHistoryPicker(false);
467 break;
468 case FOCUS_NODE_HREF:
469 {
470 String url = (String) msg.getData().get("url");
471 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500472 String src = (String) msg.getData().get("src");
473 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700474 if (TextUtils.isEmpty(url)) {
475 break;
476 }
477 HashMap focusNodeMap = (HashMap) msg.obj;
478 WebView view = (WebView) focusNodeMap.get("webview");
479 // Only apply the action if the top window did not change.
480 if (getCurrentTopWebView() != view) {
481 break;
482 }
483 switch (msg.arg1) {
484 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700485 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700486 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500487 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700488 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500489 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500490 case R.id.open_newtab_context_menu_id:
491 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700492 openTab(url, parent,
493 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500494 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700495 case R.id.copy_link_context_menu_id:
496 copy(url);
497 break;
498 case R.id.save_link_context_menu_id:
499 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500500 DownloadHandler.onDownloadStartNoStream(
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000501 mActivity, url, null, null, null,
502 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700503 break;
504 }
505 break;
506 }
507
508 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700509 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700510 break;
511
512 case STOP_LOAD:
513 stopLoading();
514 break;
515
516 case RELEASE_WAKELOCK:
517 if (mWakeLock.isHeld()) {
518 mWakeLock.release();
519 // if we reach here, Browser should be still in the
520 // background loading after WAKELOCK_TIMEOUT (5-min).
521 // To avoid burning the battery, stop loading.
522 mTabControl.stopAllLoading();
523 }
524 break;
525
526 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800527 Tab tab = (Tab) msg.obj;
528 if (tab != null) {
529 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700530 }
531 break;
532 }
533 }
534 };
535
536 }
537
John Reckef654f12011-07-12 16:42:08 -0700538 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200539 public Tab getCurrentTab() {
540 return mTabControl.getCurrentTab();
541 }
542
Michael Kolbba99c5d2010-11-29 14:57:41 -0800543 @Override
544 public void shareCurrentPage() {
545 shareCurrentPage(mTabControl.getCurrentTab());
546 }
547
548 private void shareCurrentPage(Tab tab) {
549 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800550 sharePage(mActivity, tab.getTitle(),
551 tab.getUrl(), tab.getFavicon(),
552 createScreenshot(tab.getWebView(),
553 getDesiredThumbnailWidth(mActivity),
554 getDesiredThumbnailHeight(mActivity)));
555 }
556 }
557
Michael Kolb8233fac2010-10-26 16:08:53 -0700558 /**
559 * Share a page, providing the title, url, favicon, and a screenshot. Uses
560 * an {@link Intent} to launch the Activity chooser.
561 * @param c Context used to launch a new Activity.
562 * @param title Title of the page. Stored in the Intent with
563 * {@link Intent#EXTRA_SUBJECT}
564 * @param url URL of the page. Stored in the Intent with
565 * {@link Intent#EXTRA_TEXT}
566 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
567 * with {@link Browser#EXTRA_SHARE_FAVICON}
568 * @param screenshot Bitmap of a screenshot of the page. Stored in the
569 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
570 */
571 static final void sharePage(Context c, String title, String url,
572 Bitmap favicon, Bitmap screenshot) {
573 Intent send = new Intent(Intent.ACTION_SEND);
574 send.setType("text/plain");
575 send.putExtra(Intent.EXTRA_TEXT, url);
576 send.putExtra(Intent.EXTRA_SUBJECT, title);
577 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
578 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
579 try {
580 c.startActivity(Intent.createChooser(send, c.getString(
581 R.string.choosertitle_sharevia)));
582 } catch(android.content.ActivityNotFoundException ex) {
583 // if no app handles it, do nothing
584 }
585 }
586
587 private void copy(CharSequence text) {
588 ClipboardManager cm = (ClipboardManager) mActivity
589 .getSystemService(Context.CLIPBOARD_SERVICE);
590 cm.setText(text);
591 }
592
593 // lifecycle
594
595 protected void onConfgurationChanged(Configuration config) {
596 mConfigChanged = true;
597 if (mPageDialogsHandler != null) {
598 mPageDialogsHandler.onConfigurationChanged(config);
599 }
600 mUi.onConfigurationChanged(config);
601 }
602
603 @Override
604 public void handleNewIntent(Intent intent) {
605 mIntentHandler.onNewIntent(intent);
606 }
607
608 protected void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800609 if (mUi.isCustomViewShowing()) {
610 hideCustomView();
611 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700612 if (mActivityPaused) {
613 Log.e(LOGTAG, "BrowserActivity is already paused.");
614 return;
615 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700616 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800617 Tab tab = mTabControl.getCurrentTab();
618 if (tab != null) {
619 tab.pause();
620 if (!pauseWebViewTimers(tab)) {
621 mWakeLock.acquire();
622 mHandler.sendMessageDelayed(mHandler
623 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
624 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 }
626 mUi.onPause();
627 mNetworkHandler.onPause();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200628 mNfcHandler.onPause();
Michael Kolb8233fac2010-10-26 16:08:53 -0700629
630 WebView.disablePlatformNotifications();
John Reck378a4102011-06-09 16:23:01 -0700631 mCrashRecoveryHandler.backupState();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200632
Michael Kolb8233fac2010-10-26 16:08:53 -0700633 }
634
John Reckaed9c542011-05-27 16:08:53 -0700635 void onSaveInstanceState(Bundle outState, boolean saveImages) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700636 // the default implementation requires each view to have an id. As the
637 // browser handles the state itself and it doesn't use id for the views,
638 // don't call the default implementation. Otherwise it will trigger the
639 // warning like this, "couldn't save which view has focus because the
640 // focused view XXX has no id".
641
642 // Save all the tabs
John Reckaed9c542011-05-27 16:08:53 -0700643 mTabControl.saveState(outState, saveImages);
John Reck24f18262011-06-17 14:47:20 -0700644 if (!outState.isEmpty()) {
645 // Save time so that we know how old incognito tabs (if any) are.
646 outState.putSerializable("lastActiveDate", Calendar.getInstance());
647 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700648 }
649
650 void onResume() {
651 if (!mActivityPaused) {
652 Log.e(LOGTAG, "BrowserActivity is already resumed.");
653 return;
654 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700655 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800656 Tab current = mTabControl.getCurrentTab();
657 if (current != null) {
658 current.resume();
659 resumeWebViewTimers(current);
660 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 if (mWakeLock.isHeld()) {
662 mHandler.removeMessages(RELEASE_WAKELOCK);
663 mWakeLock.release();
664 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200665
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 mUi.onResume();
667 mNetworkHandler.onResume();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200668 mNfcHandler.onResume();
Michael Kolb8233fac2010-10-26 16:08:53 -0700669 WebView.enablePlatformNotifications();
670 }
671
Michael Kolb70976932010-11-30 11:34:01 -0800672 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800673 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800674 * @param tab guaranteed non-null
675 */
676 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700677 boolean inLoad = tab.inPageLoad();
678 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
679 CookieSyncManager.getInstance().startSync();
680 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100681 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700682 }
683 }
684
Michael Kolb70976932010-11-30 11:34:01 -0800685 /**
686 * Pause all WebView timers using the WebView of the given tab
687 * @param tab
688 * @return true if the timers are paused or tab is null
689 */
690 private boolean pauseWebViewTimers(Tab tab) {
691 if (tab == null) {
692 return true;
693 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700694 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100695 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700696 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 }
Michael Kolb70976932010-11-30 11:34:01 -0800698 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700699 }
700
701 void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800702 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
704 mUploadHandler = null;
705 }
706 if (mTabControl == null) return;
707 mUi.onDestroy();
708 // Remove the current tab and sub window
709 Tab t = mTabControl.getCurrentTab();
710 if (t != null) {
711 dismissSubWindow(t);
712 removeTab(t);
713 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500714 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700715 // Destroy all the tabs
716 mTabControl.destroy();
717 WebIconDatabase.getInstance().close();
718 // Stop watching the default geolocation permissions
719 mSystemAllowGeolocationOrigins.stop();
720 mSystemAllowGeolocationOrigins = null;
721 }
722
723 protected boolean isActivityPaused() {
724 return mActivityPaused;
725 }
726
727 protected void onLowMemory() {
728 mTabControl.freeMemory();
729 }
730
731 @Override
732 public boolean shouldShowErrorConsole() {
733 return mShouldShowErrorConsole;
734 }
735
736 protected void setShouldShowErrorConsole(boolean show) {
737 if (show == mShouldShowErrorConsole) {
738 // Nothing to do.
739 return;
740 }
741 mShouldShowErrorConsole = show;
742 Tab t = mTabControl.getCurrentTab();
743 if (t == null) {
744 // There is no current tab so we cannot toggle the error console
745 return;
746 }
747 mUi.setShouldShowErrorConsole(t, show);
748 }
749
750 @Override
751 public void stopLoading() {
752 mLoadStopped = true;
753 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700754 WebView w = getCurrentTopWebView();
755 w.stopLoading();
Michael Kolb8233fac2010-10-26 16:08:53 -0700756 mUi.onPageStopped(tab);
757 }
758
759 boolean didUserStopLoading() {
760 return mLoadStopped;
761 }
762
763 // WebViewController
764
765 @Override
John Reck324d4402011-01-11 16:56:42 -0800766 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700767
768 // We've started to load a new page. If there was a pending message
769 // to save a screenshot then we will now take the new page and save
770 // an incorrect screenshot. Therefore, remove any pending thumbnail
771 // messages from the queue.
772 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800773 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700774
775 // reset sync timer to avoid sync starts during loading a page
776 CookieSyncManager.getInstance().resetSync();
777
778 if (!mNetworkHandler.isNetworkUp()) {
779 view.setNetworkAvailable(false);
780 }
781
782 // when BrowserActivity just starts, onPageStarted may be called before
783 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
784 // to start the timer. As we won't switch tabs while an activity is in
785 // pause state, we can ensure calling resume and pause in pair.
786 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800787 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700788 }
789 mLoadStopped = false;
790 if (!mNetworkHandler.isNetworkUp()) {
791 mNetworkHandler.createAndShowNetworkDialog();
792 }
793 endActionMode();
794
John Reck30c714c2010-12-16 17:30:34 -0800795 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700796
John Reck324d4402011-01-11 16:56:42 -0800797 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700798 // update the bookmark database for favicon
799 maybeUpdateFavicon(tab, null, url, favicon);
800
801 Performance.tracePageStart(url);
802
803 // Performance probe
804 if (false) {
805 Performance.onPageStarted();
806 }
807
808 }
809
810 @Override
John Reck324d4402011-01-11 16:56:42 -0800811 public void onPageFinished(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800812 mUi.onTabDataChanged(tab);
John Reck324d4402011-01-11 16:56:42 -0800813 if (!tab.isPrivateBrowsingEnabled()
John Reckd8c74522011-06-14 08:45:00 -0700814 && !TextUtils.isEmpty(tab.getUrl())
815 && !tab.isSnapshot()) {
John Recka1696282011-07-08 14:10:37 -0700816 // Only update the bookmark screenshot if the user did not
817 // cancel the load early and there is not already
818 // a pending update for the tab.
Michael Kolb8233fac2010-10-26 16:08:53 -0700819 if (tab.inForeground() && !didUserStopLoading()
820 || !tab.inForeground()) {
John Recka1696282011-07-08 14:10:37 -0700821 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
822 mHandler.sendMessageDelayed(mHandler.obtainMessage(
823 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
824 500);
825 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700826 }
827 }
828 // pause the WebView timer and release the wake lock if it is finished
829 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800830 if (mActivityPaused && pauseWebViewTimers(tab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700831 if (mWakeLock.isHeld()) {
832 mHandler.removeMessages(RELEASE_WAKELOCK);
833 mWakeLock.release();
834 }
835 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200836
Michael Kolb8233fac2010-10-26 16:08:53 -0700837 // Performance probe
838 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800839 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 }
841
842 Performance.tracePageFinished();
843 }
844
845 @Override
John Reck30c714c2010-12-16 17:30:34 -0800846 public void onProgressChanged(Tab tab) {
847 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700848
849 if (newProgress == 100) {
850 CookieSyncManager.getInstance().sync();
851 // onProgressChanged() may continue to be called after the main
852 // frame has finished loading, as any remaining sub frames continue
853 // to load. We'll only get called once though with newProgress as
854 // 100 when everything is loaded. (onPageFinished is called once
855 // when the main frame completes loading regardless of the state of
856 // any sub frames so calls to onProgressChanges may continue after
857 // onPageFinished has executed)
858 if (mInLoad) {
859 mInLoad = false;
860 updateInLoadMenuItems(mCachedMenu);
861 }
862 } else {
863 if (!mInLoad) {
864 // onPageFinished may have already been called but a subframe is
865 // still loading and updating the progress. Reset mInLoad and
866 // update the menu items.
867 mInLoad = true;
868 updateInLoadMenuItems(mCachedMenu);
869 }
870 }
John Reck30c714c2010-12-16 17:30:34 -0800871 mUi.onProgressChanged(tab);
872 }
873
874 @Override
875 public void onUpdatedLockIcon(Tab tab) {
876 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700877 }
878
879 @Override
880 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800881 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800882 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800883 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700884 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
885 return;
886 }
887 // Update the title in the history database if not in private browsing mode
888 if (!tab.isPrivateBrowsingEnabled()) {
John Reck0ebd3ac2010-12-09 11:14:04 -0800889 mDataController.updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700890 }
891 }
892
893 @Override
894 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800895 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700896 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
897 }
898
899 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800900 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
901 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700902 }
903
904 @Override
905 public boolean shouldOverrideKeyEvent(KeyEvent event) {
906 if (mMenuIsDown) {
907 // only check shortcut key when MENU is held
908 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
909 event);
910 } else {
911 return false;
912 }
913 }
914
915 @Override
916 public void onUnhandledKeyEvent(KeyEvent event) {
917 if (!isActivityPaused()) {
918 if (event.getAction() == KeyEvent.ACTION_DOWN) {
919 mActivity.onKeyDown(event.getKeyCode(), event);
920 } else {
921 mActivity.onKeyUp(event.getKeyCode(), event);
922 }
923 }
924 }
925
926 @Override
John Reck324d4402011-01-11 16:56:42 -0800927 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700928 // Don't save anything in private browsing mode
929 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800930 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700931
John Reck324d4402011-01-11 16:56:42 -0800932 if (TextUtils.isEmpty(url)
933 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700934 return;
935 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800936 mDataController.updateVisitedHistory(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700937 WebIconDatabase.getInstance().retainIconForPageUrl(url);
John Reck847b5322011-04-14 17:02:18 -0700938 if (!mActivityPaused) {
939 // Since we clear the state in onPause, don't backup the current
940 // state if we are already paused
941 mCrashRecoveryHandler.backupState();
942 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700943 }
944
945 @Override
946 public void getVisitedHistory(final ValueCallback<String[]> callback) {
947 AsyncTask<Void, Void, String[]> task =
948 new AsyncTask<Void, Void, String[]>() {
949 @Override
950 public String[] doInBackground(Void... unused) {
951 return Browser.getVisitedHistory(mActivity.getContentResolver());
952 }
953 @Override
954 public void onPostExecute(String[] result) {
955 callback.onReceiveValue(result);
956 }
957 };
958 task.execute();
959 }
960
961 @Override
962 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
963 final HttpAuthHandler handler, final String host,
964 final String realm) {
965 String username = null;
966 String password = null;
967
968 boolean reuseHttpAuthUsernamePassword
969 = handler.useHttpAuthUsernamePassword();
970
971 if (reuseHttpAuthUsernamePassword && view != null) {
972 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
973 if (credentials != null && credentials.length == 2) {
974 username = credentials[0];
975 password = credentials[1];
976 }
977 }
978
979 if (username != null && password != null) {
980 handler.proceed(username, password);
981 } else {
982 if (tab.inForeground()) {
983 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
984 } else {
985 handler.cancel();
986 }
987 }
988 }
989
990 @Override
991 public void onDownloadStart(Tab tab, String url, String userAgent,
992 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000993 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -0500994 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000995 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
996 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700997 // This Tab was opened for the sole purpose of downloading a
998 // file. Remove it.
999 if (tab == mTabControl.getCurrentTab()) {
1000 // In this case, the Tab is still on top.
1001 goBackOnePageOrQuit();
1002 } else {
1003 // In this case, it is not.
1004 closeTab(tab);
1005 }
1006 }
1007 }
1008
1009 @Override
1010 public Bitmap getDefaultVideoPoster() {
1011 return mUi.getDefaultVideoPoster();
1012 }
1013
1014 @Override
1015 public View getVideoLoadingProgressView() {
1016 return mUi.getVideoLoadingProgressView();
1017 }
1018
1019 @Override
1020 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1021 SslError error) {
1022 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1023 }
1024
Patrick Scott92066772011-03-10 08:46:27 -05001025 @Override
1026 public void showAutoLogin(Tab tab) {
1027 assert tab.inForeground();
1028 // Update the title bar to show the auto-login request.
1029 mUi.showAutoLogin(tab);
1030 }
1031
1032 @Override
1033 public void hideAutoLogin(Tab tab) {
1034 assert tab.inForeground();
1035 mUi.hideAutoLogin(tab);
1036 }
1037
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 // helper method
1039
1040 /*
1041 * Update the favorites icon if the private browsing isn't enabled and the
1042 * icon is valid.
1043 */
1044 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1045 final String url, Bitmap favicon) {
1046 if (favicon == null) {
1047 return;
1048 }
1049 if (!tab.isPrivateBrowsingEnabled()) {
1050 Bookmarks.updateFavicon(mActivity
1051 .getContentResolver(), originalUrl, url, favicon);
1052 }
1053 }
1054
Leon Scroggins4cd97792010-12-03 15:31:56 -05001055 @Override
1056 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001057 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001058 mUi.bookmarkedStatusHasChanged(tab);
1059 }
1060
Michael Kolb8233fac2010-10-26 16:08:53 -07001061 // end WebViewController
1062
1063 protected void pageUp() {
1064 getCurrentTopWebView().pageUp(false);
1065 }
1066
1067 protected void pageDown() {
1068 getCurrentTopWebView().pageDown(false);
1069 }
1070
1071 // callback from phone title bar
1072 public void editUrl() {
1073 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001074 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001075 }
1076
Michael Kolbcfa3af52010-12-14 10:36:11 -08001077 public void startVoiceSearch() {
1078 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1079 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1080 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1081 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1082 mActivity.getComponentName().flattenToString());
1083 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001084 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001085 mActivity.startActivity(intent);
1086 }
1087
Michael Kolb11d19782011-03-20 10:17:40 -07001088 @Override
1089 public void activateVoiceSearchMode(String title, List<String> results) {
1090 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001091 }
1092
1093 public void revertVoiceSearchMode(Tab tab) {
1094 mUi.revertVoiceTitleBar(tab);
1095 }
1096
Michael Kolb736990c2011-03-20 10:01:20 -07001097 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001098 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001099 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1100 }
1101
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001102 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 WebChromeClient.CustomViewCallback callback) {
1104 if (tab.inForeground()) {
1105 if (mUi.isCustomViewShowing()) {
1106 callback.onCustomViewHidden();
1107 return;
1108 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001109 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001110 // Save the menu state and set it to empty while the custom
1111 // view is showing.
1112 mOldMenuState = mMenuState;
1113 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001114 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001115 }
1116 }
1117
1118 @Override
1119 public void hideCustomView() {
1120 if (mUi.isCustomViewShowing()) {
1121 mUi.onHideCustomView();
1122 // Reset the old menu state.
1123 mMenuState = mOldMenuState;
1124 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001125 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001126 }
1127 }
1128
1129 protected void onActivityResult(int requestCode, int resultCode,
1130 Intent intent) {
1131 if (getCurrentTopWebView() == null) return;
1132 switch (requestCode) {
1133 case PREFERENCES_PAGE:
1134 if (resultCode == Activity.RESULT_OK && intent != null) {
1135 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001136 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001137 mTabControl.removeParentChildRelationShips();
1138 }
1139 }
1140 break;
1141 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001142 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001143 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001144 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001145 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001146 case AUTOFILL_SETUP:
1147 // Determine whether a profile was actually set up or not
1148 // and if so, send the message back to the WebTextView to
1149 // fill the form with the new profile.
1150 if (getSettings().getAutoFillProfile() != null) {
1151 mAutoFillSetupMessage.sendToTarget();
1152 mAutoFillSetupMessage = null;
1153 }
1154 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001155 default:
1156 break;
1157 }
1158 getCurrentTopWebView().requestFocus();
1159 }
1160
1161 /**
1162 * Open the Go page.
1163 * @param startWithHistory If true, open starting on the history tab.
1164 * Otherwise, start with the bookmarks tab.
1165 */
1166 @Override
1167 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1168 if (mTabControl.getCurrentWebView() == null) {
1169 return;
1170 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001171 // clear action mode
1172 if (isInCustomActionMode()) {
1173 endActionMode();
1174 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001175 Bundle extras = new Bundle();
1176 // Disable opening in a new window if we have maxed out the windows
1177 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1178 !mTabControl.canCreateNewTab());
John Reck2bc80422011-06-30 15:11:49 -07001179 mUi.showComboView(startWithHistory
1180 ? ComboViews.History : ComboViews.Bookmarks, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001181 }
1182
1183 // combo view callbacks
1184
1185 /**
1186 * callback from ComboPage when clear history is requested
1187 */
1188 public void onRemoveParentChildRelationships() {
1189 mTabControl.removeParentChildRelationShips();
1190 }
1191
1192 /**
1193 * callback from ComboPage when bookmark/history selection
1194 */
1195 @Override
1196 public void onUrlSelected(String url, boolean newTab) {
1197 removeComboView();
1198 if (!TextUtils.isEmpty(url)) {
1199 if (newTab) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07001200 final Tab parent = mTabControl.getCurrentTab();
1201 openTab(url,
1202 (parent != null) && parent.isPrivateBrowsingEnabled(),
1203 !mSettings.openInBackground(),
1204 true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001205 } else {
1206 final Tab currentTab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001207 loadUrl(currentTab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001208 }
1209 }
1210 }
1211
1212 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001213 * dismiss the ComboPage
1214 */
1215 @Override
1216 public void removeComboView() {
1217 mUi.hideComboView();
1218 }
1219
Michael Kolb8233fac2010-10-26 16:08:53 -07001220 // key handling
1221 protected void onBackKey() {
1222 if (!mUi.onBackKey()) {
1223 WebView subwindow = mTabControl.getCurrentSubWindow();
1224 if (subwindow != null) {
1225 if (subwindow.canGoBack()) {
1226 subwindow.goBack();
1227 } else {
1228 dismissSubWindow(mTabControl.getCurrentTab());
1229 }
1230 } else {
1231 goBackOnePageOrQuit();
1232 }
1233 }
1234 }
1235
Michael Kolb4bd767d2011-05-27 11:33:55 -07001236 protected boolean onMenuKey() {
1237 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001238 }
1239
Michael Kolb8233fac2010-10-26 16:08:53 -07001240 // menu handling and state
1241 // TODO: maybe put into separate handler
1242
1243 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckb3417f02011-01-14 11:01:05 -08001244 if (mOptionsMenuHandler != null) {
1245 return mOptionsMenuHandler.onCreateOptionsMenu(menu);
1246 }
1247
John Reckd73c5a22010-12-22 10:22:50 -08001248 if (mMenuState == EMPTY_MENU) {
1249 return false;
1250 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001251 MenuInflater inflater = mActivity.getMenuInflater();
1252 inflater.inflate(R.menu.browser, menu);
1253 updateInLoadMenuItems(menu);
1254 // hold on to the menu reference here; it is used by the page callbacks
1255 // to update the menu based on loading state
1256 mCachedMenu = menu;
1257 return true;
1258 }
1259
1260 protected void onCreateContextMenu(ContextMenu menu, View v,
1261 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001262 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001263 return;
1264 }
1265 if (!(v instanceof WebView)) {
1266 return;
1267 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001268 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001269 WebView.HitTestResult result = webview.getHitTestResult();
1270 if (result == null) {
1271 return;
1272 }
1273
1274 int type = result.getType();
1275 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1276 Log.w(LOGTAG,
1277 "We should not show context menu when nothing is touched");
1278 return;
1279 }
1280 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1281 // let TextView handles context menu
1282 return;
1283 }
1284
1285 // Note, http://b/issue?id=1106666 is requesting that
1286 // an inflated menu can be used again. This is not available
1287 // yet, so inflate each time (yuk!)
1288 MenuInflater inflater = mActivity.getMenuInflater();
1289 inflater.inflate(R.menu.browsercontext, menu);
1290
1291 // Show the correct menu group
1292 final String extra = result.getExtra();
1293 menu.setGroupVisible(R.id.PHONE_MENU,
1294 type == WebView.HitTestResult.PHONE_TYPE);
1295 menu.setGroupVisible(R.id.EMAIL_MENU,
1296 type == WebView.HitTestResult.EMAIL_TYPE);
1297 menu.setGroupVisible(R.id.GEO_MENU,
1298 type == WebView.HitTestResult.GEO_TYPE);
1299 menu.setGroupVisible(R.id.IMAGE_MENU,
1300 type == WebView.HitTestResult.IMAGE_TYPE
1301 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1302 menu.setGroupVisible(R.id.ANCHOR_MENU,
1303 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1304 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001305 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1306 || type == WebView.HitTestResult.PHONE_TYPE
1307 || type == WebView.HitTestResult.EMAIL_TYPE
1308 || type == WebView.HitTestResult.GEO_TYPE;
1309 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1310 if (hitText) {
1311 menu.findItem(R.id.select_text_menu_id)
1312 .setOnMenuItemClickListener(new SelectText(webview));
1313 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001314 // Setup custom handling depending on the type
1315 switch (type) {
1316 case WebView.HitTestResult.PHONE_TYPE:
1317 menu.setHeaderTitle(Uri.decode(extra));
1318 menu.findItem(R.id.dial_context_menu_id).setIntent(
1319 new Intent(Intent.ACTION_VIEW, Uri
1320 .parse(WebView.SCHEME_TEL + extra)));
1321 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1322 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1323 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1324 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1325 addIntent);
1326 menu.findItem(R.id.copy_phone_context_menu_id)
1327 .setOnMenuItemClickListener(
1328 new Copy(extra));
1329 break;
1330
1331 case WebView.HitTestResult.EMAIL_TYPE:
1332 menu.setHeaderTitle(extra);
1333 menu.findItem(R.id.email_context_menu_id).setIntent(
1334 new Intent(Intent.ACTION_VIEW, Uri
1335 .parse(WebView.SCHEME_MAILTO + extra)));
1336 menu.findItem(R.id.copy_mail_context_menu_id)
1337 .setOnMenuItemClickListener(
1338 new Copy(extra));
1339 break;
1340
1341 case WebView.HitTestResult.GEO_TYPE:
1342 menu.setHeaderTitle(extra);
1343 menu.findItem(R.id.map_context_menu_id).setIntent(
1344 new Intent(Intent.ACTION_VIEW, Uri
1345 .parse(WebView.SCHEME_GEO
1346 + URLEncoder.encode(extra))));
1347 menu.findItem(R.id.copy_geo_context_menu_id)
1348 .setOnMenuItemClickListener(
1349 new Copy(extra));
1350 break;
1351
1352 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1353 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001354 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001355 // decide whether to show the open link in new tab option
1356 boolean showNewTab = mTabControl.canCreateNewTab();
1357 MenuItem newTabItem
1358 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001359 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001360 ? R.string.contextmenu_openlink_newwindow_background
1361 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001362 newTabItem.setVisible(showNewTab);
1363 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001364 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1365 newTabItem.setOnMenuItemClickListener(
1366 new MenuItem.OnMenuItemClickListener() {
1367 @Override
1368 public boolean onMenuItemClick(MenuItem item) {
1369 final HashMap<String, WebView> hrefMap =
1370 new HashMap<String, WebView>();
1371 hrefMap.put("webview", webview);
1372 final Message msg = mHandler.obtainMessage(
1373 FOCUS_NODE_HREF,
1374 R.id.open_newtab_context_menu_id,
1375 0, hrefMap);
1376 webview.requestFocusNodeHref(msg);
1377 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001378 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001379 });
1380 } else {
1381 newTabItem.setOnMenuItemClickListener(
1382 new MenuItem.OnMenuItemClickListener() {
1383 @Override
1384 public boolean onMenuItemClick(MenuItem item) {
1385 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001386 openTab(extra, parent,
1387 !mSettings.openInBackground(),
1388 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001389 return true;
1390 }
1391 });
1392 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001393 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001394 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1395 break;
1396 }
1397 // otherwise fall through to handle image part
1398 case WebView.HitTestResult.IMAGE_TYPE:
1399 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1400 menu.setHeaderTitle(extra);
1401 }
1402 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1403 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1404 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001405 setOnMenuItemClickListener(
1406 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001407 menu.findItem(R.id.set_wallpaper_context_menu_id).
1408 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1409 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 break;
1411
1412 default:
1413 Log.w(LOGTAG, "We should not get here.");
1414 break;
1415 }
1416 //update the ui
1417 mUi.onContextMenuCreated(menu);
1418 }
1419
1420 /**
1421 * As the menu can be open when loading state changes
1422 * we must manually update the state of the stop/reload menu
1423 * item
1424 */
1425 private void updateInLoadMenuItems(Menu menu) {
1426 if (menu == null) {
1427 return;
1428 }
1429 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1430 MenuItem src = mInLoad ?
1431 menu.findItem(R.id.stop_menu_id):
1432 menu.findItem(R.id.reload_menu_id);
1433 if (src != null) {
1434 dest.setIcon(src.getIcon());
1435 dest.setTitle(src.getTitle());
1436 }
1437 }
1438
John Reckb3417f02011-01-14 11:01:05 -08001439 boolean onPrepareOptionsMenu(Menu menu) {
1440 if (mOptionsMenuHandler != null) {
1441 return mOptionsMenuHandler.onPrepareOptionsMenu(menu);
1442 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001443 // Note: setVisible will decide whether an item is visible; while
1444 // setEnabled() will decide whether an item is enabled, which also means
1445 // whether the matching shortcut key will function.
1446 switch (mMenuState) {
1447 case EMPTY_MENU:
1448 if (mCurrentMenuState != mMenuState) {
1449 menu.setGroupVisible(R.id.MAIN_MENU, false);
1450 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1451 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1452 }
1453 break;
1454 default:
1455 if (mCurrentMenuState != mMenuState) {
1456 menu.setGroupVisible(R.id.MAIN_MENU, true);
1457 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1458 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1459 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001460 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001461 break;
1462 }
1463 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001464 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001465 }
1466
Michael Kolb4bf79712011-07-14 14:18:12 -07001467 @Override
1468 public void updateMenuState(Tab tab, Menu menu) {
1469 boolean canGoBack = false;
1470 boolean canGoForward = false;
1471 boolean isHome = false;
1472 if (tab != null) {
1473 canGoBack = tab.canGoBack();
1474 canGoForward = tab.canGoForward();
1475 isHome = mSettings.getHomePage().equals(tab.getUrl());
1476 }
1477 final MenuItem back = menu.findItem(R.id.back_menu_id);
1478 back.setEnabled(canGoBack);
1479
1480 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1481 home.setEnabled(!isHome);
1482
1483 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1484 forward.setEnabled(canGoForward);
1485
1486 final MenuItem source = menu.findItem(mInLoad ? R.id.stop_menu_id : R.id.reload_menu_id);
1487 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001488 if (source != null && dest != null) {
1489 dest.setTitle(source.getTitle());
1490 dest.setIcon(source.getIcon());
1491 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001492
1493 // decide whether to show the share link option
1494 PackageManager pm = mActivity.getPackageManager();
1495 Intent send = new Intent(Intent.ACTION_SEND);
1496 send.setType("text/plain");
1497 ResolveInfo ri = pm.resolveActivity(send,
1498 PackageManager.MATCH_DEFAULT_ONLY);
1499 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1500
1501 boolean isNavDump = mSettings.enableNavDump();
1502 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1503 nav.setVisible(isNavDump);
1504 nav.setEnabled(isNavDump);
1505
1506 boolean showDebugSettings = mSettings.isDebugEnabled();
1507 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1508 counter.setVisible(showDebugSettings);
1509 counter.setEnabled(showDebugSettings);
1510
1511 MenuItem saveSnapshot = menu.findItem(R.id.save_snapshot_menu_id);
1512 saveSnapshot.setVisible(tab != null && !tab.isSnapshot());
1513 }
1514
Michael Kolb8233fac2010-10-26 16:08:53 -07001515 public boolean onOptionsItemSelected(MenuItem item) {
John Reckb3417f02011-01-14 11:01:05 -08001516 if (mOptionsMenuHandler != null &&
1517 mOptionsMenuHandler.onOptionsItemSelected(item)) {
1518 return true;
1519 }
1520
Michael Kolb8233fac2010-10-26 16:08:53 -07001521 if (item.getGroupId() != R.id.CONTEXT_MENU) {
1522 // menu remains active, so ensure comboview is dismissed
1523 // if main menu option is selected
1524 removeComboView();
1525 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001526 if (null == getCurrentTopWebView()) {
1527 return false;
1528 }
1529 if (mMenuIsDown) {
1530 // The shortcut action consumes the MENU. Even if it is still down,
1531 // it won't trigger the next shortcut action. In the case of the
1532 // shortcut action triggering a new activity, like Bookmarks, we
1533 // won't get onKeyUp for MENU. So it is important to reset it here.
1534 mMenuIsDown = false;
1535 }
1536 switch (item.getItemId()) {
1537 // -- Main menu
1538 case R.id.new_tab_menu_id:
1539 openTabToHomePage();
1540 break;
1541
1542 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001543 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001544 break;
1545
1546 case R.id.goto_menu_id:
1547 editUrl();
1548 break;
1549
1550 case R.id.bookmarks_menu_id:
1551 bookmarksOrHistoryPicker(false);
1552 break;
1553
Michael Kolb8233fac2010-10-26 16:08:53 -07001554 case R.id.add_bookmark_menu_id:
John Reck3ffc5ca2011-06-10 15:56:06 -07001555 bookmarkCurrentPage(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001556 break;
1557
1558 case R.id.stop_reload_menu_id:
1559 if (mInLoad) {
1560 stopLoading();
1561 } else {
1562 getCurrentTopWebView().reload();
1563 }
1564 break;
1565
1566 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001567 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001568 break;
1569
1570 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001571 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001572 break;
1573
1574 case R.id.close_menu_id:
1575 // Close the subwindow if it exists.
1576 if (mTabControl.getCurrentSubWindow() != null) {
1577 dismissSubWindow(mTabControl.getCurrentTab());
1578 break;
1579 }
1580 closeCurrentTab();
1581 break;
1582
1583 case R.id.homepage_menu_id:
1584 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001585 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001586 break;
1587
1588 case R.id.preferences_menu_id:
1589 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1590 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1591 getCurrentTopWebView().getUrl());
1592 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1593 break;
1594
1595 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001596 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001597 break;
1598
John Reck2bc80422011-06-30 15:11:49 -07001599 case R.id.save_snapshot_menu_id:
1600 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001601 if (source == null) break;
John Reckd8c74522011-06-14 08:45:00 -07001602 final ContentResolver cr = mActivity.getContentResolver();
1603 final ContentValues values = source.createSnapshotValues();
John Reck2bc80422011-06-30 15:11:49 -07001604 if (values != null) {
1605 new AsyncTask<Tab, Void, Long>() {
John Reckd8c74522011-06-14 08:45:00 -07001606
John Reck2bc80422011-06-30 15:11:49 -07001607 @Override
1608 protected Long doInBackground(Tab... params) {
1609 Uri result = cr.insert(Snapshots.CONTENT_URI, values);
1610 long id = ContentUris.parseId(result);
1611 return id;
John Reckd8c74522011-06-14 08:45:00 -07001612 }
John Reckf33b1632011-06-04 20:00:23 -07001613
John Reck2bc80422011-06-30 15:11:49 -07001614 @Override
1615 protected void onPostExecute(Long id) {
1616 Bundle b = new Bundle();
1617 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1618 mUi.showComboView(ComboViews.Snapshots, b);
1619 };
1620 }.execute(source);
1621 } else {
1622 Toast.makeText(mActivity, R.string.snapshot_failed,
Leon Scrogginsac993842011-02-02 12:54:07 -05001623 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001624 }
Leon Scrogginsac993842011-02-02 12:54:07 -05001625 break;
1626
Michael Kolb8233fac2010-10-26 16:08:53 -07001627 case R.id.page_info_menu_id:
Huahui Wuae0c0412011-06-28 10:17:05 -07001628 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
Michael Kolb8233fac2010-10-26 16:08:53 -07001629 break;
1630
1631 case R.id.classic_history_menu_id:
1632 bookmarksOrHistoryPicker(true);
1633 break;
1634
1635 case R.id.title_bar_share_page_url:
1636 case R.id.share_page_menu_id:
1637 Tab currentTab = mTabControl.getCurrentTab();
1638 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001639 return false;
1640 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001641 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001642 break;
1643
1644 case R.id.dump_nav_menu_id:
1645 getCurrentTopWebView().debugDump();
1646 break;
1647
1648 case R.id.dump_counters_menu_id:
1649 getCurrentTopWebView().dumpV8Counters();
1650 break;
1651
1652 case R.id.zoom_in_menu_id:
1653 getCurrentTopWebView().zoomIn();
1654 break;
1655
1656 case R.id.zoom_out_menu_id:
1657 getCurrentTopWebView().zoomOut();
1658 break;
1659
1660 case R.id.view_downloads_menu_id:
1661 viewDownloads();
1662 break;
1663
1664 case R.id.window_one_menu_id:
1665 case R.id.window_two_menu_id:
1666 case R.id.window_three_menu_id:
1667 case R.id.window_four_menu_id:
1668 case R.id.window_five_menu_id:
1669 case R.id.window_six_menu_id:
1670 case R.id.window_seven_menu_id:
1671 case R.id.window_eight_menu_id:
1672 {
1673 int menuid = item.getItemId();
1674 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1675 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1676 Tab desiredTab = mTabControl.getTab(id);
1677 if (desiredTab != null &&
1678 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001679 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001680 }
1681 break;
1682 }
1683 }
1684 }
1685 break;
1686
1687 default:
1688 return false;
1689 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001690 return true;
1691 }
1692
1693 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001694 // Let the History and Bookmark fragments handle menus they created.
1695 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1696 return false;
1697 }
1698
Michael Kolb8233fac2010-10-26 16:08:53 -07001699 int id = item.getItemId();
1700 boolean result = true;
1701 switch (id) {
1702 // For the context menu from the title bar
1703 case R.id.title_bar_copy_page_url:
1704 Tab currentTab = mTabControl.getCurrentTab();
1705 if (null == currentTab) {
1706 result = false;
1707 break;
1708 }
1709 WebView mainView = currentTab.getWebView();
1710 if (null == mainView) {
1711 result = false;
1712 break;
1713 }
1714 copy(mainView.getUrl());
1715 break;
1716 // -- Browser context menu
1717 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001718 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001719 case R.id.copy_link_context_menu_id:
1720 final WebView webView = getCurrentTopWebView();
1721 if (null == webView) {
1722 result = false;
1723 break;
1724 }
1725 final HashMap<String, WebView> hrefMap =
1726 new HashMap<String, WebView>();
1727 hrefMap.put("webview", webView);
1728 final Message msg = mHandler.obtainMessage(
1729 FOCUS_NODE_HREF, id, 0, hrefMap);
1730 webView.requestFocusNodeHref(msg);
1731 break;
1732
1733 default:
1734 // For other context menus
1735 result = onOptionsItemSelected(item);
1736 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001737 return result;
1738 }
1739
1740 /**
1741 * support programmatically opening the context menu
1742 */
1743 public void openContextMenu(View view) {
1744 mActivity.openContextMenu(view);
1745 }
1746
1747 /**
1748 * programmatically open the options menu
1749 */
1750 public void openOptionsMenu() {
1751 mActivity.openOptionsMenu();
1752 }
1753
1754 public boolean onMenuOpened(int featureId, Menu menu) {
1755 if (mOptionsMenuOpen) {
1756 if (mConfigChanged) {
1757 // We do not need to make any changes to the state of the
1758 // title bar, since the only thing that happened was a
1759 // change in orientation
1760 mConfigChanged = false;
1761 } else {
1762 if (!mExtendedMenuOpen) {
1763 mExtendedMenuOpen = true;
1764 mUi.onExtendedMenuOpened();
1765 } else {
1766 // Switching the menu back to icon view, so show the
1767 // title bar once again.
1768 mExtendedMenuOpen = false;
1769 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001770 }
1771 }
1772 } else {
1773 // The options menu is closed, so open it, and show the title
1774 mOptionsMenuOpen = true;
1775 mConfigChanged = false;
1776 mExtendedMenuOpen = false;
1777 mUi.onOptionsMenuOpened();
1778 }
1779 return true;
1780 }
1781
1782 public void onOptionsMenuClosed(Menu menu) {
1783 mOptionsMenuOpen = false;
1784 mUi.onOptionsMenuClosed(mInLoad);
1785 }
1786
1787 public void onContextMenuClosed(Menu menu) {
1788 mUi.onContextMenuClosed(menu, mInLoad);
1789 }
1790
1791 // Helper method for getting the top window.
1792 @Override
1793 public WebView getCurrentTopWebView() {
1794 return mTabControl.getCurrentTopWebView();
1795 }
1796
1797 @Override
1798 public WebView getCurrentWebView() {
1799 return mTabControl.getCurrentWebView();
1800 }
1801
1802 /*
1803 * This method is called as a result of the user selecting the options
1804 * menu to see the download window. It shows the download window on top of
1805 * the current window.
1806 */
1807 void viewDownloads() {
1808 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1809 mActivity.startActivity(intent);
1810 }
1811
1812 // action mode
1813
1814 void onActionModeStarted(ActionMode mode) {
1815 mUi.onActionModeStarted(mode);
1816 mActionMode = mode;
1817 }
1818
1819 /*
1820 * True if a custom ActionMode (i.e. find or select) is in use.
1821 */
1822 @Override
1823 public boolean isInCustomActionMode() {
1824 return mActionMode != null;
1825 }
1826
1827 /*
1828 * End the current ActionMode.
1829 */
1830 @Override
1831 public void endActionMode() {
1832 if (mActionMode != null) {
1833 mActionMode.finish();
1834 }
1835 }
1836
1837 /*
1838 * Called by find and select when they are finished. Replace title bars
1839 * as necessary.
1840 */
1841 public void onActionModeFinished(ActionMode mode) {
1842 if (!isInCustomActionMode()) return;
1843 mUi.onActionModeFinished(mInLoad);
1844 mActionMode = null;
1845 }
1846
1847 boolean isInLoad() {
1848 return mInLoad;
1849 }
1850
1851 // bookmark handling
1852
1853 /**
1854 * add the current page as a bookmark to the given folder id
1855 * @param folderId use -1 for the default folder
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001856 * @param canBeAnEdit If true, check to see whether the site is already
1857 * bookmarked, and if it is, edit that bookmark. If false, and
1858 * the site is already bookmarked, do not attempt to edit the
1859 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001860 */
1861 @Override
John Reck3ffc5ca2011-06-10 15:56:06 -07001862 public void bookmarkCurrentPage(boolean canBeAnEdit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001863 Intent i = new Intent(mActivity,
1864 AddBookmarkPage.class);
1865 WebView w = getCurrentTopWebView();
1866 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1867 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1868 String touchIconUrl = w.getTouchIconUrl();
1869 if (touchIconUrl != null) {
1870 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1871 WebSettings settings = w.getSettings();
1872 if (settings != null) {
1873 i.putExtra(AddBookmarkPage.USER_AGENT,
1874 settings.getUserAgentString());
1875 }
1876 }
1877 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1878 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1879 getDesiredThumbnailHeight(mActivity)));
1880 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001881 if (canBeAnEdit) {
1882 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1883 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001884 // Put the dialog at the upper right of the screen, covering the
1885 // star on the title bar.
1886 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
1887 mActivity.startActivity(i);
1888 }
1889
1890 // file chooser
1891 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1892 mUploadHandler = new UploadHandler(this);
1893 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1894 }
1895
1896 // thumbnails
1897
1898 /**
1899 * Return the desired width for thumbnail screenshots, which are stored in
1900 * the database, and used on the bookmarks screen.
1901 * @param context Context for finding out the density of the screen.
1902 * @return desired width for thumbnail screenshot.
1903 */
1904 static int getDesiredThumbnailWidth(Context context) {
1905 return context.getResources().getDimensionPixelOffset(
1906 R.dimen.bookmarkThumbnailWidth);
1907 }
1908
1909 /**
1910 * Return the desired height for thumbnail screenshots, which are stored in
1911 * the database, and used on the bookmarks screen.
1912 * @param context Context for finding out the density of the screen.
1913 * @return desired height for thumbnail screenshot.
1914 */
1915 static int getDesiredThumbnailHeight(Context context) {
1916 return context.getResources().getDimensionPixelOffset(
1917 R.dimen.bookmarkThumbnailHeight);
1918 }
1919
Michael Kolb1acef692011-03-08 14:12:06 -08001920 static Bitmap createScreenshot(Tab tab, int width, int height) {
1921 if ((tab != null) && (tab.getWebView() != null)) {
1922 return createScreenshot(tab.getWebView(), width, height);
1923 }
1924 return null;
1925 }
1926
John Reck8cc92352011-07-06 17:41:52 -07001927 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08001928 // We render to a bitmap 2x the desired size so that we can then
1929 // re-scale it with filtering since canvas.scale doesn't filter
1930 // This helps reduce aliasing at the cost of being slightly blurry
1931 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07001932 Picture thumbnail = view.capturePicture();
1933 if (thumbnail == null) {
1934 return null;
1935 }
John Reck5c6ac2f2011-01-05 10:18:03 -08001936 width *= filter_scale;
1937 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07001938 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1939 Canvas canvas = new Canvas(bm);
1940 // May need to tweak these values to determine what is the
1941 // best scale factor
1942 int thumbnailWidth = thumbnail.getWidth();
1943 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08001944 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08001945 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08001946 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07001947 } else {
1948 return null;
1949 }
John Reckfe49ab42010-11-16 17:09:37 -08001950
Michael Kolbeb95db42011-03-03 10:38:40 -08001951 float scaleFactorY = (float) height / (float)thumbnailHeight;
1952 if (scaleFactorY > scaleFactor) {
1953 // The picture is narrower than the requested AR
1954 // Center the thumnail and crop the sides
1955 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08001956 float wx = (thumbnailWidth * scaleFactor) - width;
1957 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07001958 }
1959
John Reckfe49ab42010-11-16 17:09:37 -08001960 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07001961
1962 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08001963 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
1964 height / filter_scale, true);
1965 bm.recycle();
1966 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07001967 }
1968
John Reck34ef2672011-02-10 11:30:55 -08001969 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001970 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07001971 // FIXME: Would like to make sure there is actually something to
1972 // draw, but the API for that (WebViewCore.pictureReady()) is not
1973 // currently accessible here.
1974
John Reck34ef2672011-02-10 11:30:55 -08001975 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08001976 if (view == null) {
1977 // Tab was destroyed
1978 return;
1979 }
John Reck34ef2672011-02-10 11:30:55 -08001980 final String url = tab.getUrl();
1981 final String originalUrl = view.getOriginalUrl();
1982
1983 if (TextUtils.isEmpty(url)) {
1984 return;
1985 }
1986
1987 // Only update thumbnails for web urls (http(s)://), not for
1988 // about:, javascript:, data:, etc...
1989 // Unless it is a bookmarked site, then always update
1990 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
1991 return;
1992 }
1993
Michael Kolb8233fac2010-10-26 16:08:53 -07001994 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
1995 getDesiredThumbnailHeight(mActivity));
1996 if (bm == null) {
1997 return;
1998 }
1999
2000 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002001 new AsyncTask<Void, Void, Void>() {
2002 @Override
2003 protected Void doInBackground(Void... unused) {
2004 Cursor cursor = null;
2005 try {
2006 // TODO: Clean this up
2007 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2008 if (cursor != null && cursor.moveToFirst()) {
2009 final ByteArrayOutputStream os =
2010 new ByteArrayOutputStream();
2011 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002012
John Reck34ef2672011-02-10 11:30:55 -08002013 ContentValues values = new ContentValues();
2014 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002015
John Reck34ef2672011-02-10 11:30:55 -08002016 do {
John Reck617fd832011-02-16 14:35:59 -08002017 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002018 cr.update(Images.CONTENT_URI, values, null, null);
2019 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002020 }
John Reck34ef2672011-02-10 11:30:55 -08002021 } catch (IllegalStateException e) {
2022 // Ignore
2023 } finally {
2024 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002025 }
John Reck34ef2672011-02-10 11:30:55 -08002026 return null;
2027 }
2028 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002029 }
2030
2031 private class Copy implements OnMenuItemClickListener {
2032 private CharSequence mText;
2033
2034 public boolean onMenuItemClick(MenuItem item) {
2035 copy(mText);
2036 return true;
2037 }
2038
2039 public Copy(CharSequence toCopy) {
2040 mText = toCopy;
2041 }
2042 }
2043
Leon Scroggins63c02662010-11-18 15:16:27 -05002044 private static class Download implements OnMenuItemClickListener {
2045 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002046 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002047 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002048
2049 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002050 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002051 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002052 return true;
2053 }
2054
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002055 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002056 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002057 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002058 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002059 }
2060 }
2061
Cary Clark8974d282010-11-22 10:46:05 -05002062 private static class SelectText implements OnMenuItemClickListener {
2063 private WebView mWebView;
2064
2065 public boolean onMenuItemClick(MenuItem item) {
2066 if (mWebView != null) {
2067 return mWebView.selectText();
2068 }
2069 return false;
2070 }
2071
2072 public SelectText(WebView webView) {
2073 mWebView = webView;
2074 }
2075
2076 }
2077
Michael Kolb8233fac2010-10-26 16:08:53 -07002078 /********************** TODO: UI stuff *****************************/
2079
2080 // these methods have been copied, they still need to be cleaned up
2081
2082 /****************** tabs ***************************************************/
2083
2084 // basic tab interactions:
2085
2086 // it is assumed that tabcontrol already knows about the tab
2087 protected void addTab(Tab tab) {
2088 mUi.addTab(tab);
2089 }
2090
2091 protected void removeTab(Tab tab) {
2092 mUi.removeTab(tab);
2093 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002094 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002095 }
2096
Michael Kolbf2055602011-04-09 17:20:03 -07002097 @Override
2098 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002099 // monkey protection against delayed start
2100 if (tab != null) {
2101 mTabControl.setCurrentTab(tab);
2102 // the tab is guaranteed to have a webview after setCurrentTab
2103 mUi.setActiveTab(tab);
2104 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002105 }
2106
2107 protected void closeEmptyChildTab() {
2108 Tab current = mTabControl.getCurrentTab();
2109 if (current != null
2110 && current.getWebView().copyBackForwardList().getSize() == 0) {
Michael Kolbc831b632011-05-11 09:30:34 -07002111 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002112 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002113 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002114 closeTab(current);
2115 }
2116 }
2117 }
2118
John Reck26b18322011-06-21 13:08:58 -07002119 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002120 // Dismiss the subwindow if applicable.
2121 dismissSubWindow(appTab);
2122 // Since we might kill the WebView, remove it from the
2123 // content view first.
2124 mUi.detachTab(appTab);
2125 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002126 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002127 // TODO: analyze why the remove and add are necessary
2128 mUi.attachTab(appTab);
2129 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002130 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002131 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002132 } else {
2133 // If the tab was the current tab, we have to attach
2134 // it to the view system again.
2135 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002136 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002137 }
2138 }
2139
2140 // Remove the sub window if it exists. Also called by TabControl when the
2141 // user clicks the 'X' to dismiss a sub window.
2142 public void dismissSubWindow(Tab tab) {
2143 removeSubWindow(tab);
2144 // dismiss the subwindow. This will destroy the WebView.
2145 tab.dismissSubWindow();
2146 getCurrentTopWebView().requestFocus();
2147 }
2148
2149 @Override
2150 public void removeSubWindow(Tab t) {
2151 if (t.getSubWebView() != null) {
2152 mUi.removeSubWindow(t.getSubViewContainer());
2153 }
2154 }
2155
2156 @Override
2157 public void attachSubWindow(Tab tab) {
2158 if (tab.getSubWebView() != null) {
2159 mUi.attachSubWindow(tab.getSubViewContainer());
2160 getCurrentTopWebView().requestFocus();
2161 }
2162 }
2163
Mathew Inwood29721c22011-06-29 17:55:24 +01002164 private Tab showPreloadedTab(final UrlData urlData) {
2165 if (!urlData.isPreloaded()) {
2166 return null;
2167 }
2168 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2169 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2170 if (sbQuery != null) {
2171 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
2172 // Could not submit query. Fallback to regular tab creation
2173 tabControl.destroy();
2174 return null;
2175 }
2176 }
2177 Tab t = tabControl.getTab();
2178 mTabControl.addPreloadedTab(t);
2179 addTab(t);
2180 setActiveTab(t);
2181 return t;
2182 }
2183
Michael Kolb7bcafde2011-05-09 13:55:59 -07002184 // open a non inconito tab with the given url data
2185 // and set as active tab
2186 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002187 Tab tab = showPreloadedTab(urlData);
2188 if (tab == null) {
2189 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002190 if ((tab != null) && !urlData.isEmpty()) {
2191 loadUrlDataIn(tab, urlData);
2192 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002193 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002194 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002195 }
2196
Michael Kolb843510f2010-12-09 10:51:49 -08002197 @Override
2198 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002199 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002200 }
2201
Michael Kolb8233fac2010-10-26 16:08:53 -07002202 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002203 public Tab openIncognitoTab() {
2204 return openTab(INCOGNITO_URI, true, true, false);
2205 }
2206
2207 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002208 public Tab openTab(String url, boolean incognito, boolean setActive,
2209 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002210 return openTab(url, incognito, setActive, useCurrent, null);
2211 }
2212
2213 @Override
2214 public Tab openTab(String url, Tab parent, boolean setActive,
2215 boolean useCurrent) {
2216 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2217 setActive, useCurrent, parent);
2218 }
2219
2220 public Tab openTab(String url, boolean incognito, boolean setActive,
2221 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002222 Tab tab = createNewTab(incognito, setActive, useCurrent);
2223 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002224 if (parent != null && parent != tab) {
2225 parent.addChildTab(tab);
2226 }
Michael Kolb519d2282011-05-09 17:03:19 -07002227 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002228 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002229 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002230 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002231 return tab;
2232 }
2233
2234 // this method will attempt to create a new tab
2235 // incognito: private browsing tab
2236 // setActive: ste tab as current tab
2237 // useCurrent: if no new tab can be created, return current tab
2238 private Tab createNewTab(boolean incognito, boolean setActive,
2239 boolean useCurrent) {
2240 Tab tab = null;
2241 if (mTabControl.canCreateNewTab()) {
2242 tab = mTabControl.createNewTab(incognito);
2243 addTab(tab);
2244 if (setActive) {
2245 setActiveTab(tab);
2246 }
2247 } else {
2248 if (useCurrent) {
2249 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002250 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002251 } else {
2252 mUi.showMaxTabsWarning();
2253 }
2254 }
2255 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002256 }
2257
John Reck2bc80422011-06-30 15:11:49 -07002258 @Override
2259 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2260 SnapshotTab tab = null;
2261 if (mTabControl.canCreateNewTab()) {
2262 tab = mTabControl.createSnapshotTab(snapshotId);
2263 addTab(tab);
2264 if (setActive) {
2265 setActiveTab(tab);
2266 }
2267 } else {
2268 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002269 }
2270 return tab;
2271 }
2272
Michael Kolb8233fac2010-10-26 16:08:53 -07002273 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002274 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002275 * @return boolean True if we successfully switched to a different tab. If
2276 * the indexth tab is null, or if that tab is the same as
2277 * the current one, return false.
2278 */
2279 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002280 public boolean switchToTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002281 // hide combo view if open
2282 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002283 Tab currentTab = mTabControl.getCurrentTab();
2284 if (tab == null || tab == currentTab) {
2285 return false;
2286 }
2287 setActiveTab(tab);
2288 return true;
2289 }
2290
2291 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002292 public void closeCurrentTab() {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002293 // hide combo view if open
2294 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002295 if (mTabControl.getTabCount() == 1) {
John Reck958b2422010-12-03 17:56:17 -08002296 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002297 return;
2298 }
Michael Kolbc831b632011-05-11 09:30:34 -07002299 final Tab current = mTabControl.getCurrentTab();
2300 final int pos = mTabControl.getCurrentPosition();
2301 Tab newTab = current.getParent();
2302 if (newTab == null) {
2303 newTab = mTabControl.getTab(pos + 1);
2304 if (newTab == null) {
2305 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002306 }
2307 }
Michael Kolbc831b632011-05-11 09:30:34 -07002308 if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002309 // Close window
2310 closeTab(current);
2311 }
2312 }
2313
2314 /**
2315 * Close the tab, remove its associated title bar, and adjust mTabControl's
2316 * current tab to a valid value.
2317 */
2318 @Override
2319 public void closeTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002320 // hide combo view if open
2321 removeComboView();
Michael Kolb2d59c322011-01-25 13:18:55 -08002322 removeTab(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002323 }
2324
Michael Kolb8233fac2010-10-26 16:08:53 -07002325 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002326 protected void loadUrlFromContext(String url) {
2327 Tab tab = getCurrentTab();
2328 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002329 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002330 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002331 url = UrlUtils.smartUrlFilter(url);
2332 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002333 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002334 }
2335 }
2336 }
2337
2338 /**
2339 * Load the URL into the given WebView and update the title bar
2340 * to reflect the new load. Call this instead of WebView.loadUrl
2341 * directly.
2342 * @param view The WebView used to load url.
2343 * @param url The URL to load.
2344 */
John Reck71e51422011-07-01 16:49:28 -07002345 @Override
2346 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002347 loadUrl(tab, url, null);
2348 }
2349
2350 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2351 if (tab != null) {
2352 dismissSubWindow(tab);
2353 tab.loadUrl(url, headers);
2354 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002355 }
2356
2357 /**
2358 * Load UrlData into a Tab and update the title bar to reflect the new
2359 * load. Call this instead of UrlData.loadIn directly.
2360 * @param t The Tab used to load.
2361 * @param data The UrlData being loaded.
2362 */
2363 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002364 if (data != null) {
2365 if (data.mVoiceIntent != null) {
2366 t.activateVoiceSearchMode(data.mVoiceIntent);
Michael Kolb14612442011-06-24 13:06:29 -07002367 } else if (data.isPreloaded()) {
2368 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002369 } else {
2370 loadUrl(t, data.mUrl, data.mHeaders);
2371 }
2372 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002373 }
2374
John Reck30c714c2010-12-16 17:30:34 -08002375 @Override
2376 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002377 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002378 if (tab.canGoBack()) {
2379 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002380 } else {
John Reckef654f12011-07-12 16:42:08 -07002381 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002382 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002383 }
2384
2385 void goBackOnePageOrQuit() {
2386 Tab current = mTabControl.getCurrentTab();
2387 if (current == null) {
2388 /*
2389 * Instead of finishing the activity, simply push this to the back
2390 * of the stack and let ActivityManager to choose the foreground
2391 * activity. As BrowserActivity is singleTask, it will be always the
2392 * root of the task. So we can use either true or false for
2393 * moveTaskToBack().
2394 */
2395 mActivity.moveTaskToBack(true);
2396 return;
2397 }
John Reckef654f12011-07-12 16:42:08 -07002398 if (current.canGoBack()) {
2399 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002400 } else {
2401 // Check to see if we are closing a window that was created by
2402 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002403 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002404 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002405 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002406 // Now we close the other tab
2407 closeTab(current);
2408 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002409 /*
2410 * Instead of finishing the activity, simply push this to the back
2411 * of the stack and let ActivityManager to choose the foreground
2412 * activity. As BrowserActivity is singleTask, it will be always the
2413 * root of the task. So we can use either true or false for
2414 * moveTaskToBack().
2415 */
2416 mActivity.moveTaskToBack(true);
2417 }
2418 }
2419 }
2420
2421 /**
2422 * Feed the previously stored results strings to the BrowserProvider so that
2423 * the SearchDialog will show them instead of the standard searches.
2424 * @param result String to show on the editable line of the SearchDialog.
2425 */
2426 @Override
2427 public void showVoiceSearchResults(String result) {
2428 ContentProviderClient client = mActivity.getContentResolver()
2429 .acquireContentProviderClient(Browser.BOOKMARKS_URI);
2430 ContentProvider prov = client.getLocalContentProvider();
2431 BrowserProvider bp = (BrowserProvider) prov;
2432 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
2433 client.release();
2434
2435 Bundle bundle = createGoogleSearchSourceBundle(
2436 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
2437 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
2438 startSearch(result, false, bundle, false);
2439 }
2440
2441 private void startSearch(String initialQuery, boolean selectInitialQuery,
2442 Bundle appSearchData, boolean globalSearch) {
2443 if (appSearchData == null) {
2444 appSearchData = createGoogleSearchSourceBundle(
2445 GOOGLE_SEARCH_SOURCE_TYPE);
2446 }
2447
2448 SearchEngine searchEngine = mSettings.getSearchEngine();
2449 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
2450 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
2451 }
2452 mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData,
2453 globalSearch);
2454 }
2455
2456 private Bundle createGoogleSearchSourceBundle(String source) {
2457 Bundle bundle = new Bundle();
2458 bundle.putString(Search.SOURCE, source);
2459 return bundle;
2460 }
2461
2462 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002463 * helper method for key handler
2464 * returns the current tab if it can't advance
2465 */
Michael Kolbc831b632011-05-11 09:30:34 -07002466 private Tab getNextTab() {
2467 return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1,
2468 mTabControl.getCurrentPosition() + 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002469 }
2470
2471 /**
2472 * helper method for key handler
2473 * returns the current tab if it can't advance
2474 */
Michael Kolbc831b632011-05-11 09:30:34 -07002475 private Tab getPrevTab() {
2476 return mTabControl.getTab(Math.max(0,
2477 mTabControl.getCurrentPosition() - 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002478 }
2479
2480 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002481 * handle key events in browser
2482 *
2483 * @param keyCode
2484 * @param event
2485 * @return true if handled, false to pass to super
2486 */
2487 boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002488 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002489 // Even if MENU is already held down, we need to call to super to open
2490 // the IME on long press.
Michael Kolb2814a362011-05-19 15:49:41 -07002491 if (KeyEvent.KEYCODE_MENU == keyCode) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002492 if (mOptionsMenuHandler != null) {
2493 return false;
2494 } else {
2495 event.startTracking();
2496 return true;
2497 }
Michael Kolb2814a362011-05-19 15:49:41 -07002498 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002499 if (!noModifiers
2500 && ((KeyEvent.KEYCODE_MENU == keyCode)
2501 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2502 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002503 mMenuIsDown = true;
2504 return false;
2505 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002506
Cary Clark8ff8c662010-12-29 15:03:05 -05002507 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002508 Tab tab = getCurrentTab();
2509 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002510
Cary Clark160bbb92011-01-10 11:17:07 -05002511 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2512 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002513
Michael Kolb8233fac2010-10-26 16:08:53 -07002514 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002515 case KeyEvent.KEYCODE_TAB:
2516 if (event.isCtrlPressed()) {
2517 if (event.isShiftPressed()) {
2518 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002519 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002520 } else {
2521 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002522 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002523 }
2524 return true;
2525 }
2526 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002527 case KeyEvent.KEYCODE_SPACE:
2528 // WebView/WebTextView handle the keys in the KeyDown. As
2529 // the Activity's shortcut keys are only handled when WebView
2530 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002531 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002532 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002533 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002534 pageDown();
2535 }
2536 return true;
2537 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002538 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002539 event.startTracking();
2540 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002541 case KeyEvent.KEYCODE_FORWARD:
2542 if (!noModifiers) break;
2543 tab.goForward();
2544 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002545 case KeyEvent.KEYCODE_DPAD_LEFT:
2546 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002547 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002548 return true;
2549 }
2550 break;
2551 case KeyEvent.KEYCODE_DPAD_RIGHT:
2552 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002553 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002554 return true;
2555 }
2556 break;
2557 case KeyEvent.KEYCODE_A:
2558 if (ctrl) {
2559 webView.selectAll();
2560 return true;
2561 }
2562 break;
Michael Kolba4183062011-01-16 10:43:21 -08002563// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002564 case KeyEvent.KEYCODE_C:
2565 if (ctrl) {
2566 webView.copySelection();
2567 return true;
2568 }
2569 break;
Michael Kolba4183062011-01-16 10:43:21 -08002570// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002571// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002572// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002573// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002574// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002575// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002576// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002577// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002578// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002579// case KeyEvent.KEYCODE_M: // unused
2580// case KeyEvent.KEYCODE_N: // in Chrome: new window
2581// case KeyEvent.KEYCODE_O: // in Chrome: open file
2582// case KeyEvent.KEYCODE_P: // in Chrome: print page
2583// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002584// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002585// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2586 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002587 // we can't use the ctrl/shift flags, they check for
2588 // exclusive use of a modifier
2589 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002590 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002591 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002592 } else {
2593 openTabToHomePage();
2594 }
2595 return true;
2596 }
2597 break;
2598// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2599// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb1a2eba42011-03-16 16:42:49 -07002600 case KeyEvent.KEYCODE_W: // in Chrome: close tab
2601 if (ctrl) {
2602 closeCurrentTab();
2603 return true;
2604 }
2605 break;
Cary Clark8ff8c662010-12-29 15:03:05 -05002606// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2607// case KeyEvent.KEYCODE_Y: // unused
2608// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002609 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002610 // it is a regular key and webview is not null
2611 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002612 }
2613
John Recke6bf4ab2011-02-24 15:48:05 -08002614 boolean onKeyLongPress(int keyCode, KeyEvent event) {
2615 switch(keyCode) {
2616 case KeyEvent.KEYCODE_BACK:
2617 if (mUi.showsWeb()) {
2618 bookmarksOrHistoryPicker(true);
2619 return true;
2620 }
2621 break;
2622 }
2623 return false;
2624 }
2625
Michael Kolb8233fac2010-10-26 16:08:53 -07002626 boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb2814a362011-05-19 15:49:41 -07002627 if (KeyEvent.KEYCODE_MENU == keyCode) {
2628 mMenuIsDown = false;
2629 if (event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002630 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002631 }
2632 }
Cary Clark160bbb92011-01-10 11:17:07 -05002633 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002634 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002635 case KeyEvent.KEYCODE_BACK:
2636 if (event.isTracking() && !event.isCanceled()) {
2637 onBackKey();
2638 return true;
2639 }
2640 break;
2641 }
2642 return false;
2643 }
2644
2645 public boolean isMenuDown() {
2646 return mMenuIsDown;
2647 }
2648
Ben Murdoch8029a772010-11-16 11:58:21 +00002649 public void setupAutoFill(Message message) {
2650 // Open the settings activity at the AutoFill profile fragment so that
2651 // the user can create a new profile. When they return, we will dispatch
2652 // the message so that we can autofill the form using their new profile.
2653 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2654 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2655 AutoFillSettingsFragment.class.getName());
2656 mAutoFillSetupMessage = message;
2657 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2658 }
John Reckb3417f02011-01-14 11:01:05 -08002659
2660 @Override
2661 public void registerOptionsMenuHandler(OptionsMenuHandler handler) {
2662 mOptionsMenuHandler = handler;
2663 }
2664
2665 @Override
2666 public void unregisterOptionsMenuHandler(OptionsMenuHandler handler) {
2667 if (mOptionsMenuHandler == handler) {
2668 mOptionsMenuHandler = null;
2669 }
2670 }
2671
Narayan Kamath5119edd2011-02-23 15:49:17 +00002672 @Override
2673 public void registerDropdownChangeListener(DropdownChangeListener d) {
2674 mUi.registerDropdownChangeListener(d);
2675 }
Michael Kolbfbc579a2011-07-07 15:59:33 -07002676
2677 public boolean onSearchRequested() {
2678 mUi.editUrl(false);
2679 return true;
2680 }
2681
Michael Kolb8233fac2010-10-26 16:08:53 -07002682}