blob: 3398af096269746ca43ec082291d5f9242214c3f [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 Reckd8c74522011-06-14 08:45:00 -070082import com.android.browser.provider.BrowserProvider2.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
Martijn Coenenb2f93552011-06-14 10:48:35 +0200538
539 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();
681 if (w != null) {
682 w.resumeTimers();
683 }
684 }
685 }
686
Michael Kolb70976932010-11-30 11:34:01 -0800687 /**
688 * Pause all WebView timers using the WebView of the given tab
689 * @param tab
690 * @return true if the timers are paused or tab is null
691 */
692 private boolean pauseWebViewTimers(Tab tab) {
693 if (tab == null) {
694 return true;
695 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700696 CookieSyncManager.getInstance().stopSync();
697 WebView w = getCurrentWebView();
698 if (w != null) {
699 w.pauseTimers();
700 }
701 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700702 }
Michael Kolb70976932010-11-30 11:34:01 -0800703 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700704 }
705
706 void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800707 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700708 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
709 mUploadHandler = null;
710 }
711 if (mTabControl == null) return;
712 mUi.onDestroy();
713 // Remove the current tab and sub window
714 Tab t = mTabControl.getCurrentTab();
715 if (t != null) {
716 dismissSubWindow(t);
717 removeTab(t);
718 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500719 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700720 // Destroy all the tabs
721 mTabControl.destroy();
722 WebIconDatabase.getInstance().close();
723 // Stop watching the default geolocation permissions
724 mSystemAllowGeolocationOrigins.stop();
725 mSystemAllowGeolocationOrigins = null;
726 }
727
728 protected boolean isActivityPaused() {
729 return mActivityPaused;
730 }
731
732 protected void onLowMemory() {
733 mTabControl.freeMemory();
734 }
735
736 @Override
737 public boolean shouldShowErrorConsole() {
738 return mShouldShowErrorConsole;
739 }
740
741 protected void setShouldShowErrorConsole(boolean show) {
742 if (show == mShouldShowErrorConsole) {
743 // Nothing to do.
744 return;
745 }
746 mShouldShowErrorConsole = show;
747 Tab t = mTabControl.getCurrentTab();
748 if (t == null) {
749 // There is no current tab so we cannot toggle the error console
750 return;
751 }
752 mUi.setShouldShowErrorConsole(t, show);
753 }
754
755 @Override
756 public void stopLoading() {
757 mLoadStopped = true;
758 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700759 WebView w = getCurrentTopWebView();
760 w.stopLoading();
Michael Kolb8233fac2010-10-26 16:08:53 -0700761 mUi.onPageStopped(tab);
762 }
763
764 boolean didUserStopLoading() {
765 return mLoadStopped;
766 }
767
768 // WebViewController
769
770 @Override
John Reck324d4402011-01-11 16:56:42 -0800771 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700772
773 // We've started to load a new page. If there was a pending message
774 // to save a screenshot then we will now take the new page and save
775 // an incorrect screenshot. Therefore, remove any pending thumbnail
776 // messages from the queue.
777 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800778 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700779
780 // reset sync timer to avoid sync starts during loading a page
781 CookieSyncManager.getInstance().resetSync();
782
783 if (!mNetworkHandler.isNetworkUp()) {
784 view.setNetworkAvailable(false);
785 }
786
787 // when BrowserActivity just starts, onPageStarted may be called before
788 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
789 // to start the timer. As we won't switch tabs while an activity is in
790 // pause state, we can ensure calling resume and pause in pair.
791 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800792 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700793 }
794 mLoadStopped = false;
795 if (!mNetworkHandler.isNetworkUp()) {
796 mNetworkHandler.createAndShowNetworkDialog();
797 }
798 endActionMode();
799
John Reck30c714c2010-12-16 17:30:34 -0800800 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700801
John Reck324d4402011-01-11 16:56:42 -0800802 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700803 // update the bookmark database for favicon
804 maybeUpdateFavicon(tab, null, url, favicon);
805
806 Performance.tracePageStart(url);
807
808 // Performance probe
809 if (false) {
810 Performance.onPageStarted();
811 }
812
813 }
814
815 @Override
John Reck324d4402011-01-11 16:56:42 -0800816 public void onPageFinished(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800817 mUi.onTabDataChanged(tab);
John Reck324d4402011-01-11 16:56:42 -0800818 if (!tab.isPrivateBrowsingEnabled()
John Reckd8c74522011-06-14 08:45:00 -0700819 && !TextUtils.isEmpty(tab.getUrl())
820 && !tab.isSnapshot()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700821 if (tab.inForeground() && !didUserStopLoading()
822 || !tab.inForeground()) {
823 // Only update the bookmark screenshot if the user did not
824 // cancel the load early.
825 mHandler.sendMessageDelayed(mHandler.obtainMessage(
John Reck34ef2672011-02-10 11:30:55 -0800826 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Michael Kolb8233fac2010-10-26 16:08:53 -0700827 500);
828 }
829 }
830 // pause the WebView timer and release the wake lock if it is finished
831 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800832 if (mActivityPaused && pauseWebViewTimers(tab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700833 if (mWakeLock.isHeld()) {
834 mHandler.removeMessages(RELEASE_WAKELOCK);
835 mWakeLock.release();
836 }
837 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200838
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 // Performance probe
840 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800841 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 }
843
844 Performance.tracePageFinished();
845 }
846
847 @Override
John Reck30c714c2010-12-16 17:30:34 -0800848 public void onProgressChanged(Tab tab) {
849 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700850
851 if (newProgress == 100) {
852 CookieSyncManager.getInstance().sync();
853 // onProgressChanged() may continue to be called after the main
854 // frame has finished loading, as any remaining sub frames continue
855 // to load. We'll only get called once though with newProgress as
856 // 100 when everything is loaded. (onPageFinished is called once
857 // when the main frame completes loading regardless of the state of
858 // any sub frames so calls to onProgressChanges may continue after
859 // onPageFinished has executed)
860 if (mInLoad) {
861 mInLoad = false;
862 updateInLoadMenuItems(mCachedMenu);
863 }
864 } else {
865 if (!mInLoad) {
866 // onPageFinished may have already been called but a subframe is
867 // still loading and updating the progress. Reset mInLoad and
868 // update the menu items.
869 mInLoad = true;
870 updateInLoadMenuItems(mCachedMenu);
871 }
872 }
John Reck30c714c2010-12-16 17:30:34 -0800873 mUi.onProgressChanged(tab);
874 }
875
876 @Override
877 public void onUpdatedLockIcon(Tab tab) {
878 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700879 }
880
881 @Override
882 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800883 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800884 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800885 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700886 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
887 return;
888 }
889 // Update the title in the history database if not in private browsing mode
890 if (!tab.isPrivateBrowsingEnabled()) {
John Reck0ebd3ac2010-12-09 11:14:04 -0800891 mDataController.updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700892 }
893 }
894
895 @Override
896 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800897 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700898 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
899 }
900
901 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800902 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
903 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700904 }
905
906 @Override
907 public boolean shouldOverrideKeyEvent(KeyEvent event) {
908 if (mMenuIsDown) {
909 // only check shortcut key when MENU is held
910 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
911 event);
912 } else {
913 return false;
914 }
915 }
916
917 @Override
918 public void onUnhandledKeyEvent(KeyEvent event) {
919 if (!isActivityPaused()) {
920 if (event.getAction() == KeyEvent.ACTION_DOWN) {
921 mActivity.onKeyDown(event.getKeyCode(), event);
922 } else {
923 mActivity.onKeyUp(event.getKeyCode(), event);
924 }
925 }
926 }
927
928 @Override
John Reck324d4402011-01-11 16:56:42 -0800929 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700930 // Don't save anything in private browsing mode
931 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800932 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700933
John Reck324d4402011-01-11 16:56:42 -0800934 if (TextUtils.isEmpty(url)
935 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700936 return;
937 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800938 mDataController.updateVisitedHistory(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700939 WebIconDatabase.getInstance().retainIconForPageUrl(url);
John Reck847b5322011-04-14 17:02:18 -0700940 if (!mActivityPaused) {
941 // Since we clear the state in onPause, don't backup the current
942 // state if we are already paused
943 mCrashRecoveryHandler.backupState();
944 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700945 }
946
947 @Override
948 public void getVisitedHistory(final ValueCallback<String[]> callback) {
949 AsyncTask<Void, Void, String[]> task =
950 new AsyncTask<Void, Void, String[]>() {
951 @Override
952 public String[] doInBackground(Void... unused) {
953 return Browser.getVisitedHistory(mActivity.getContentResolver());
954 }
955 @Override
956 public void onPostExecute(String[] result) {
957 callback.onReceiveValue(result);
958 }
959 };
960 task.execute();
961 }
962
963 @Override
964 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
965 final HttpAuthHandler handler, final String host,
966 final String realm) {
967 String username = null;
968 String password = null;
969
970 boolean reuseHttpAuthUsernamePassword
971 = handler.useHttpAuthUsernamePassword();
972
973 if (reuseHttpAuthUsernamePassword && view != null) {
974 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
975 if (credentials != null && credentials.length == 2) {
976 username = credentials[0];
977 password = credentials[1];
978 }
979 }
980
981 if (username != null && password != null) {
982 handler.proceed(username, password);
983 } else {
984 if (tab.inForeground()) {
985 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
986 } else {
987 handler.cancel();
988 }
989 }
990 }
991
992 @Override
993 public void onDownloadStart(Tab tab, String url, String userAgent,
994 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000995 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -0500996 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000997 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
998 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700999 // This Tab was opened for the sole purpose of downloading a
1000 // file. Remove it.
1001 if (tab == mTabControl.getCurrentTab()) {
1002 // In this case, the Tab is still on top.
1003 goBackOnePageOrQuit();
1004 } else {
1005 // In this case, it is not.
1006 closeTab(tab);
1007 }
1008 }
1009 }
1010
1011 @Override
1012 public Bitmap getDefaultVideoPoster() {
1013 return mUi.getDefaultVideoPoster();
1014 }
1015
1016 @Override
1017 public View getVideoLoadingProgressView() {
1018 return mUi.getVideoLoadingProgressView();
1019 }
1020
1021 @Override
1022 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1023 SslError error) {
1024 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1025 }
1026
Patrick Scott92066772011-03-10 08:46:27 -05001027 @Override
1028 public void showAutoLogin(Tab tab) {
1029 assert tab.inForeground();
1030 // Update the title bar to show the auto-login request.
1031 mUi.showAutoLogin(tab);
1032 }
1033
1034 @Override
1035 public void hideAutoLogin(Tab tab) {
1036 assert tab.inForeground();
1037 mUi.hideAutoLogin(tab);
1038 }
1039
Michael Kolb8233fac2010-10-26 16:08:53 -07001040 // helper method
1041
1042 /*
1043 * Update the favorites icon if the private browsing isn't enabled and the
1044 * icon is valid.
1045 */
1046 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1047 final String url, Bitmap favicon) {
1048 if (favicon == null) {
1049 return;
1050 }
1051 if (!tab.isPrivateBrowsingEnabled()) {
1052 Bookmarks.updateFavicon(mActivity
1053 .getContentResolver(), originalUrl, url, favicon);
1054 }
1055 }
1056
Leon Scroggins4cd97792010-12-03 15:31:56 -05001057 @Override
1058 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001059 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001060 mUi.bookmarkedStatusHasChanged(tab);
1061 }
1062
Michael Kolb8233fac2010-10-26 16:08:53 -07001063 // end WebViewController
1064
1065 protected void pageUp() {
1066 getCurrentTopWebView().pageUp(false);
1067 }
1068
1069 protected void pageDown() {
1070 getCurrentTopWebView().pageDown(false);
1071 }
1072
1073 // callback from phone title bar
1074 public void editUrl() {
1075 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001076 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001077 }
1078
Michael Kolbcfa3af52010-12-14 10:36:11 -08001079 public void startVoiceSearch() {
1080 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1081 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1082 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1083 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1084 mActivity.getComponentName().flattenToString());
1085 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001086 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001087 mActivity.startActivity(intent);
1088 }
1089
Michael Kolb11d19782011-03-20 10:17:40 -07001090 @Override
1091 public void activateVoiceSearchMode(String title, List<String> results) {
1092 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001093 }
1094
1095 public void revertVoiceSearchMode(Tab tab) {
1096 mUi.revertVoiceTitleBar(tab);
1097 }
1098
Michael Kolb736990c2011-03-20 10:01:20 -07001099 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001100 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001101 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1102 }
1103
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001104 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001105 WebChromeClient.CustomViewCallback callback) {
1106 if (tab.inForeground()) {
1107 if (mUi.isCustomViewShowing()) {
1108 callback.onCustomViewHidden();
1109 return;
1110 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001111 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001112 // Save the menu state and set it to empty while the custom
1113 // view is showing.
1114 mOldMenuState = mMenuState;
1115 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001116 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001117 }
1118 }
1119
1120 @Override
1121 public void hideCustomView() {
1122 if (mUi.isCustomViewShowing()) {
1123 mUi.onHideCustomView();
1124 // Reset the old menu state.
1125 mMenuState = mOldMenuState;
1126 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001127 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001128 }
1129 }
1130
1131 protected void onActivityResult(int requestCode, int resultCode,
1132 Intent intent) {
1133 if (getCurrentTopWebView() == null) return;
1134 switch (requestCode) {
1135 case PREFERENCES_PAGE:
1136 if (resultCode == Activity.RESULT_OK && intent != null) {
1137 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001138 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001139 mTabControl.removeParentChildRelationShips();
1140 }
1141 }
1142 break;
1143 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001144 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001145 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001146 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001147 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001148 case AUTOFILL_SETUP:
1149 // Determine whether a profile was actually set up or not
1150 // and if so, send the message back to the WebTextView to
1151 // fill the form with the new profile.
1152 if (getSettings().getAutoFillProfile() != null) {
1153 mAutoFillSetupMessage.sendToTarget();
1154 mAutoFillSetupMessage = null;
1155 }
1156 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001157 default:
1158 break;
1159 }
1160 getCurrentTopWebView().requestFocus();
1161 }
1162
1163 /**
1164 * Open the Go page.
1165 * @param startWithHistory If true, open starting on the history tab.
1166 * Otherwise, start with the bookmarks tab.
1167 */
1168 @Override
1169 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1170 if (mTabControl.getCurrentWebView() == null) {
1171 return;
1172 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001173 // clear action mode
1174 if (isInCustomActionMode()) {
1175 endActionMode();
1176 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001177 Bundle extras = new Bundle();
1178 // Disable opening in a new window if we have maxed out the windows
1179 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1180 !mTabControl.canCreateNewTab());
John Reck2bc80422011-06-30 15:11:49 -07001181 mUi.showComboView(startWithHistory
1182 ? ComboViews.History : ComboViews.Bookmarks, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001183 }
1184
1185 // combo view callbacks
1186
1187 /**
1188 * callback from ComboPage when clear history is requested
1189 */
1190 public void onRemoveParentChildRelationships() {
1191 mTabControl.removeParentChildRelationShips();
1192 }
1193
1194 /**
1195 * callback from ComboPage when bookmark/history selection
1196 */
1197 @Override
1198 public void onUrlSelected(String url, boolean newTab) {
1199 removeComboView();
1200 if (!TextUtils.isEmpty(url)) {
1201 if (newTab) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07001202 final Tab parent = mTabControl.getCurrentTab();
1203 openTab(url,
1204 (parent != null) && parent.isPrivateBrowsingEnabled(),
1205 !mSettings.openInBackground(),
1206 true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001207 } else {
1208 final Tab currentTab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001209 loadUrl(currentTab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001210 }
1211 }
1212 }
1213
1214 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001215 * dismiss the ComboPage
1216 */
1217 @Override
1218 public void removeComboView() {
1219 mUi.hideComboView();
1220 }
1221
1222 // active tabs page handling
1223
1224 protected void showActiveTabsPage() {
1225 mMenuState = EMPTY_MENU;
1226 mUi.showActiveTabsPage();
1227 }
1228
1229 /**
1230 * Remove the active tabs page.
1231 * @param needToAttach If true, the active tabs page did not attach a tab
1232 * to the content view, so we need to do that here.
1233 */
1234 @Override
1235 public void removeActiveTabsPage(boolean needToAttach) {
1236 mMenuState = R.id.MAIN_MENU;
1237 mUi.removeActiveTabsPage();
1238 if (needToAttach) {
1239 setActiveTab(mTabControl.getCurrentTab());
1240 }
1241 getCurrentTopWebView().requestFocus();
1242 }
1243
1244 // key handling
1245 protected void onBackKey() {
1246 if (!mUi.onBackKey()) {
1247 WebView subwindow = mTabControl.getCurrentSubWindow();
1248 if (subwindow != null) {
1249 if (subwindow.canGoBack()) {
1250 subwindow.goBack();
1251 } else {
1252 dismissSubWindow(mTabControl.getCurrentTab());
1253 }
1254 } else {
1255 goBackOnePageOrQuit();
1256 }
1257 }
1258 }
1259
Michael Kolb4bd767d2011-05-27 11:33:55 -07001260 protected boolean onMenuKey() {
1261 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001262 }
1263
Michael Kolb8233fac2010-10-26 16:08:53 -07001264 // menu handling and state
1265 // TODO: maybe put into separate handler
1266
1267 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckb3417f02011-01-14 11:01:05 -08001268 if (mOptionsMenuHandler != null) {
1269 return mOptionsMenuHandler.onCreateOptionsMenu(menu);
1270 }
1271
John Reckd73c5a22010-12-22 10:22:50 -08001272 if (mMenuState == EMPTY_MENU) {
1273 return false;
1274 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001275 MenuInflater inflater = mActivity.getMenuInflater();
1276 inflater.inflate(R.menu.browser, menu);
1277 updateInLoadMenuItems(menu);
1278 // hold on to the menu reference here; it is used by the page callbacks
1279 // to update the menu based on loading state
1280 mCachedMenu = menu;
1281 return true;
1282 }
1283
1284 protected void onCreateContextMenu(ContextMenu menu, View v,
1285 ContextMenuInfo menuInfo) {
1286 if (v instanceof TitleBarBase) {
1287 return;
1288 }
1289 if (!(v instanceof WebView)) {
1290 return;
1291 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001292 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001293 WebView.HitTestResult result = webview.getHitTestResult();
1294 if (result == null) {
1295 return;
1296 }
1297
1298 int type = result.getType();
1299 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1300 Log.w(LOGTAG,
1301 "We should not show context menu when nothing is touched");
1302 return;
1303 }
1304 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1305 // let TextView handles context menu
1306 return;
1307 }
1308
1309 // Note, http://b/issue?id=1106666 is requesting that
1310 // an inflated menu can be used again. This is not available
1311 // yet, so inflate each time (yuk!)
1312 MenuInflater inflater = mActivity.getMenuInflater();
1313 inflater.inflate(R.menu.browsercontext, menu);
1314
1315 // Show the correct menu group
1316 final String extra = result.getExtra();
1317 menu.setGroupVisible(R.id.PHONE_MENU,
1318 type == WebView.HitTestResult.PHONE_TYPE);
1319 menu.setGroupVisible(R.id.EMAIL_MENU,
1320 type == WebView.HitTestResult.EMAIL_TYPE);
1321 menu.setGroupVisible(R.id.GEO_MENU,
1322 type == WebView.HitTestResult.GEO_TYPE);
1323 menu.setGroupVisible(R.id.IMAGE_MENU,
1324 type == WebView.HitTestResult.IMAGE_TYPE
1325 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1326 menu.setGroupVisible(R.id.ANCHOR_MENU,
1327 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1328 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001329 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1330 || type == WebView.HitTestResult.PHONE_TYPE
1331 || type == WebView.HitTestResult.EMAIL_TYPE
1332 || type == WebView.HitTestResult.GEO_TYPE;
1333 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1334 if (hitText) {
1335 menu.findItem(R.id.select_text_menu_id)
1336 .setOnMenuItemClickListener(new SelectText(webview));
1337 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001338 // Setup custom handling depending on the type
1339 switch (type) {
1340 case WebView.HitTestResult.PHONE_TYPE:
1341 menu.setHeaderTitle(Uri.decode(extra));
1342 menu.findItem(R.id.dial_context_menu_id).setIntent(
1343 new Intent(Intent.ACTION_VIEW, Uri
1344 .parse(WebView.SCHEME_TEL + extra)));
1345 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1346 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1347 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1348 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1349 addIntent);
1350 menu.findItem(R.id.copy_phone_context_menu_id)
1351 .setOnMenuItemClickListener(
1352 new Copy(extra));
1353 break;
1354
1355 case WebView.HitTestResult.EMAIL_TYPE:
1356 menu.setHeaderTitle(extra);
1357 menu.findItem(R.id.email_context_menu_id).setIntent(
1358 new Intent(Intent.ACTION_VIEW, Uri
1359 .parse(WebView.SCHEME_MAILTO + extra)));
1360 menu.findItem(R.id.copy_mail_context_menu_id)
1361 .setOnMenuItemClickListener(
1362 new Copy(extra));
1363 break;
1364
1365 case WebView.HitTestResult.GEO_TYPE:
1366 menu.setHeaderTitle(extra);
1367 menu.findItem(R.id.map_context_menu_id).setIntent(
1368 new Intent(Intent.ACTION_VIEW, Uri
1369 .parse(WebView.SCHEME_GEO
1370 + URLEncoder.encode(extra))));
1371 menu.findItem(R.id.copy_geo_context_menu_id)
1372 .setOnMenuItemClickListener(
1373 new Copy(extra));
1374 break;
1375
1376 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1377 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001378 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001379 // decide whether to show the open link in new tab option
1380 boolean showNewTab = mTabControl.canCreateNewTab();
1381 MenuItem newTabItem
1382 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001383 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001384 ? R.string.contextmenu_openlink_newwindow_background
1385 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001386 newTabItem.setVisible(showNewTab);
1387 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001388 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1389 newTabItem.setOnMenuItemClickListener(
1390 new MenuItem.OnMenuItemClickListener() {
1391 @Override
1392 public boolean onMenuItemClick(MenuItem item) {
1393 final HashMap<String, WebView> hrefMap =
1394 new HashMap<String, WebView>();
1395 hrefMap.put("webview", webview);
1396 final Message msg = mHandler.obtainMessage(
1397 FOCUS_NODE_HREF,
1398 R.id.open_newtab_context_menu_id,
1399 0, hrefMap);
1400 webview.requestFocusNodeHref(msg);
1401 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001402 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001403 });
1404 } else {
1405 newTabItem.setOnMenuItemClickListener(
1406 new MenuItem.OnMenuItemClickListener() {
1407 @Override
1408 public boolean onMenuItemClick(MenuItem item) {
1409 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001410 openTab(extra, parent,
1411 !mSettings.openInBackground(),
1412 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001413 return true;
1414 }
1415 });
1416 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001417 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001418 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1419 break;
1420 }
1421 // otherwise fall through to handle image part
1422 case WebView.HitTestResult.IMAGE_TYPE:
1423 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1424 menu.setHeaderTitle(extra);
1425 }
1426 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1427 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1428 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001429 setOnMenuItemClickListener(
1430 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001431 menu.findItem(R.id.set_wallpaper_context_menu_id).
1432 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1433 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001434 break;
1435
1436 default:
1437 Log.w(LOGTAG, "We should not get here.");
1438 break;
1439 }
1440 //update the ui
1441 mUi.onContextMenuCreated(menu);
1442 }
1443
1444 /**
1445 * As the menu can be open when loading state changes
1446 * we must manually update the state of the stop/reload menu
1447 * item
1448 */
1449 private void updateInLoadMenuItems(Menu menu) {
1450 if (menu == null) {
1451 return;
1452 }
1453 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1454 MenuItem src = mInLoad ?
1455 menu.findItem(R.id.stop_menu_id):
1456 menu.findItem(R.id.reload_menu_id);
1457 if (src != null) {
1458 dest.setIcon(src.getIcon());
1459 dest.setTitle(src.getTitle());
1460 }
1461 }
1462
John Reckb3417f02011-01-14 11:01:05 -08001463 boolean onPrepareOptionsMenu(Menu menu) {
1464 if (mOptionsMenuHandler != null) {
1465 return mOptionsMenuHandler.onPrepareOptionsMenu(menu);
1466 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001467 // Note: setVisible will decide whether an item is visible; while
1468 // setEnabled() will decide whether an item is enabled, which also means
1469 // whether the matching shortcut key will function.
1470 switch (mMenuState) {
1471 case EMPTY_MENU:
1472 if (mCurrentMenuState != mMenuState) {
1473 menu.setGroupVisible(R.id.MAIN_MENU, false);
1474 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1475 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1476 }
1477 break;
1478 default:
1479 if (mCurrentMenuState != mMenuState) {
1480 menu.setGroupVisible(R.id.MAIN_MENU, true);
1481 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1482 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1483 }
1484 final WebView w = getCurrentTopWebView();
1485 boolean canGoBack = false;
1486 boolean canGoForward = false;
1487 boolean isHome = false;
1488 if (w != null) {
1489 canGoBack = w.canGoBack();
1490 canGoForward = w.canGoForward();
1491 isHome = mSettings.getHomePage().equals(w.getUrl());
1492 }
1493 final MenuItem back = menu.findItem(R.id.back_menu_id);
1494 back.setEnabled(canGoBack);
1495
1496 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1497 home.setEnabled(!isHome);
1498
1499 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1500 forward.setEnabled(canGoForward);
1501
1502 // decide whether to show the share link option
1503 PackageManager pm = mActivity.getPackageManager();
1504 Intent send = new Intent(Intent.ACTION_SEND);
1505 send.setType("text/plain");
1506 ResolveInfo ri = pm.resolveActivity(send,
1507 PackageManager.MATCH_DEFAULT_ONLY);
1508 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1509
John Reck35e9dd62011-04-25 09:01:54 -07001510 boolean isNavDump = mSettings.enableNavDump();
Michael Kolb8233fac2010-10-26 16:08:53 -07001511 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1512 nav.setVisible(isNavDump);
1513 nav.setEnabled(isNavDump);
1514
John Reck35e9dd62011-04-25 09:01:54 -07001515 boolean showDebugSettings = mSettings.isDebugEnabled();
Michael Kolb8233fac2010-10-26 16:08:53 -07001516 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1517 counter.setVisible(showDebugSettings);
1518 counter.setEnabled(showDebugSettings);
1519
John Reckb3417f02011-01-14 11:01:05 -08001520 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
1521 newtab.setEnabled(getTabControl().canCreateNewTab());
Michael Kolb8233fac2010-10-26 16:08:53 -07001522
John Reck2bc80422011-06-30 15:11:49 -07001523 MenuItem saveSnapshot = menu.findItem(R.id.save_snapshot_menu_id);
1524 Tab tab = getCurrentTab();
1525 saveSnapshot.setVisible(tab != null && !tab.isSnapshot());
Michael Kolb8233fac2010-10-26 16:08:53 -07001526 break;
1527 }
1528 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001529 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001530 }
1531
1532 public boolean onOptionsItemSelected(MenuItem item) {
John Reckb3417f02011-01-14 11:01:05 -08001533 if (mOptionsMenuHandler != null &&
1534 mOptionsMenuHandler.onOptionsItemSelected(item)) {
1535 return true;
1536 }
1537
Michael Kolb8233fac2010-10-26 16:08:53 -07001538 if (item.getGroupId() != R.id.CONTEXT_MENU) {
1539 // menu remains active, so ensure comboview is dismissed
1540 // if main menu option is selected
1541 removeComboView();
1542 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001543 if (null == getCurrentTopWebView()) {
1544 return false;
1545 }
1546 if (mMenuIsDown) {
1547 // The shortcut action consumes the MENU. Even if it is still down,
1548 // it won't trigger the next shortcut action. In the case of the
1549 // shortcut action triggering a new activity, like Bookmarks, we
1550 // won't get onKeyUp for MENU. So it is important to reset it here.
1551 mMenuIsDown = false;
1552 }
1553 switch (item.getItemId()) {
1554 // -- Main menu
1555 case R.id.new_tab_menu_id:
1556 openTabToHomePage();
1557 break;
1558
1559 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001560 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001561 break;
1562
1563 case R.id.goto_menu_id:
1564 editUrl();
1565 break;
1566
1567 case R.id.bookmarks_menu_id:
1568 bookmarksOrHistoryPicker(false);
1569 break;
1570
1571 case R.id.active_tabs_menu_id:
1572 showActiveTabsPage();
1573 break;
1574
1575 case R.id.add_bookmark_menu_id:
John Reck3ffc5ca2011-06-10 15:56:06 -07001576 bookmarkCurrentPage(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001577 break;
1578
1579 case R.id.stop_reload_menu_id:
1580 if (mInLoad) {
1581 stopLoading();
1582 } else {
1583 getCurrentTopWebView().reload();
1584 }
1585 break;
1586
1587 case R.id.back_menu_id:
1588 getCurrentTopWebView().goBack();
1589 break;
1590
1591 case R.id.forward_menu_id:
1592 getCurrentTopWebView().goForward();
1593 break;
1594
1595 case R.id.close_menu_id:
1596 // Close the subwindow if it exists.
1597 if (mTabControl.getCurrentSubWindow() != null) {
1598 dismissSubWindow(mTabControl.getCurrentTab());
1599 break;
1600 }
1601 closeCurrentTab();
1602 break;
1603
1604 case R.id.homepage_menu_id:
1605 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001606 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001607 break;
1608
1609 case R.id.preferences_menu_id:
1610 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1611 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1612 getCurrentTopWebView().getUrl());
1613 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1614 break;
1615
1616 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001617 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001618 break;
1619
John Reck2bc80422011-06-30 15:11:49 -07001620 case R.id.save_snapshot_menu_id:
1621 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001622 if (source == null) break;
John Reckd8c74522011-06-14 08:45:00 -07001623 final ContentResolver cr = mActivity.getContentResolver();
1624 final ContentValues values = source.createSnapshotValues();
John Reck2bc80422011-06-30 15:11:49 -07001625 if (values != null) {
1626 new AsyncTask<Tab, Void, Long>() {
John Reckd8c74522011-06-14 08:45:00 -07001627
John Reck2bc80422011-06-30 15:11:49 -07001628 @Override
1629 protected Long doInBackground(Tab... params) {
1630 Uri result = cr.insert(Snapshots.CONTENT_URI, values);
1631 long id = ContentUris.parseId(result);
1632 return id;
John Reckd8c74522011-06-14 08:45:00 -07001633 }
John Reckf33b1632011-06-04 20:00:23 -07001634
John Reck2bc80422011-06-30 15:11:49 -07001635 @Override
1636 protected void onPostExecute(Long id) {
1637 Bundle b = new Bundle();
1638 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1639 mUi.showComboView(ComboViews.Snapshots, b);
1640 };
1641 }.execute(source);
1642 } else {
1643 Toast.makeText(mActivity, R.string.snapshot_failed,
Leon Scrogginsac993842011-02-02 12:54:07 -05001644 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001645 }
Leon Scrogginsac993842011-02-02 12:54:07 -05001646 break;
1647
Michael Kolb8233fac2010-10-26 16:08:53 -07001648 case R.id.page_info_menu_id:
Huahui Wuae0c0412011-06-28 10:17:05 -07001649 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
Michael Kolb8233fac2010-10-26 16:08:53 -07001650 break;
1651
1652 case R.id.classic_history_menu_id:
1653 bookmarksOrHistoryPicker(true);
1654 break;
1655
1656 case R.id.title_bar_share_page_url:
1657 case R.id.share_page_menu_id:
1658 Tab currentTab = mTabControl.getCurrentTab();
1659 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001660 return false;
1661 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001662 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001663 break;
1664
1665 case R.id.dump_nav_menu_id:
1666 getCurrentTopWebView().debugDump();
1667 break;
1668
1669 case R.id.dump_counters_menu_id:
1670 getCurrentTopWebView().dumpV8Counters();
1671 break;
1672
1673 case R.id.zoom_in_menu_id:
1674 getCurrentTopWebView().zoomIn();
1675 break;
1676
1677 case R.id.zoom_out_menu_id:
1678 getCurrentTopWebView().zoomOut();
1679 break;
1680
1681 case R.id.view_downloads_menu_id:
1682 viewDownloads();
1683 break;
1684
1685 case R.id.window_one_menu_id:
1686 case R.id.window_two_menu_id:
1687 case R.id.window_three_menu_id:
1688 case R.id.window_four_menu_id:
1689 case R.id.window_five_menu_id:
1690 case R.id.window_six_menu_id:
1691 case R.id.window_seven_menu_id:
1692 case R.id.window_eight_menu_id:
1693 {
1694 int menuid = item.getItemId();
1695 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1696 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1697 Tab desiredTab = mTabControl.getTab(id);
1698 if (desiredTab != null &&
1699 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001700 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001701 }
1702 break;
1703 }
1704 }
1705 }
1706 break;
1707
1708 default:
1709 return false;
1710 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001711 return true;
1712 }
1713
1714 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001715 // Let the History and Bookmark fragments handle menus they created.
1716 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1717 return false;
1718 }
1719
Michael Kolb8233fac2010-10-26 16:08:53 -07001720 int id = item.getItemId();
1721 boolean result = true;
1722 switch (id) {
1723 // For the context menu from the title bar
1724 case R.id.title_bar_copy_page_url:
1725 Tab currentTab = mTabControl.getCurrentTab();
1726 if (null == currentTab) {
1727 result = false;
1728 break;
1729 }
1730 WebView mainView = currentTab.getWebView();
1731 if (null == mainView) {
1732 result = false;
1733 break;
1734 }
1735 copy(mainView.getUrl());
1736 break;
1737 // -- Browser context menu
1738 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001739 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001740 case R.id.copy_link_context_menu_id:
1741 final WebView webView = getCurrentTopWebView();
1742 if (null == webView) {
1743 result = false;
1744 break;
1745 }
1746 final HashMap<String, WebView> hrefMap =
1747 new HashMap<String, WebView>();
1748 hrefMap.put("webview", webView);
1749 final Message msg = mHandler.obtainMessage(
1750 FOCUS_NODE_HREF, id, 0, hrefMap);
1751 webView.requestFocusNodeHref(msg);
1752 break;
1753
1754 default:
1755 // For other context menus
1756 result = onOptionsItemSelected(item);
1757 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001758 return result;
1759 }
1760
1761 /**
1762 * support programmatically opening the context menu
1763 */
1764 public void openContextMenu(View view) {
1765 mActivity.openContextMenu(view);
1766 }
1767
1768 /**
1769 * programmatically open the options menu
1770 */
1771 public void openOptionsMenu() {
1772 mActivity.openOptionsMenu();
1773 }
1774
1775 public boolean onMenuOpened(int featureId, Menu menu) {
1776 if (mOptionsMenuOpen) {
1777 if (mConfigChanged) {
1778 // We do not need to make any changes to the state of the
1779 // title bar, since the only thing that happened was a
1780 // change in orientation
1781 mConfigChanged = false;
1782 } else {
1783 if (!mExtendedMenuOpen) {
1784 mExtendedMenuOpen = true;
1785 mUi.onExtendedMenuOpened();
1786 } else {
1787 // Switching the menu back to icon view, so show the
1788 // title bar once again.
1789 mExtendedMenuOpen = false;
1790 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001791 }
1792 }
1793 } else {
1794 // The options menu is closed, so open it, and show the title
1795 mOptionsMenuOpen = true;
1796 mConfigChanged = false;
1797 mExtendedMenuOpen = false;
1798 mUi.onOptionsMenuOpened();
1799 }
1800 return true;
1801 }
1802
1803 public void onOptionsMenuClosed(Menu menu) {
1804 mOptionsMenuOpen = false;
1805 mUi.onOptionsMenuClosed(mInLoad);
1806 }
1807
1808 public void onContextMenuClosed(Menu menu) {
1809 mUi.onContextMenuClosed(menu, mInLoad);
1810 }
1811
1812 // Helper method for getting the top window.
1813 @Override
1814 public WebView getCurrentTopWebView() {
1815 return mTabControl.getCurrentTopWebView();
1816 }
1817
1818 @Override
1819 public WebView getCurrentWebView() {
1820 return mTabControl.getCurrentWebView();
1821 }
1822
1823 /*
1824 * This method is called as a result of the user selecting the options
1825 * menu to see the download window. It shows the download window on top of
1826 * the current window.
1827 */
1828 void viewDownloads() {
1829 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1830 mActivity.startActivity(intent);
1831 }
1832
1833 // action mode
1834
1835 void onActionModeStarted(ActionMode mode) {
1836 mUi.onActionModeStarted(mode);
1837 mActionMode = mode;
1838 }
1839
1840 /*
1841 * True if a custom ActionMode (i.e. find or select) is in use.
1842 */
1843 @Override
1844 public boolean isInCustomActionMode() {
1845 return mActionMode != null;
1846 }
1847
1848 /*
1849 * End the current ActionMode.
1850 */
1851 @Override
1852 public void endActionMode() {
1853 if (mActionMode != null) {
1854 mActionMode.finish();
1855 }
1856 }
1857
1858 /*
1859 * Called by find and select when they are finished. Replace title bars
1860 * as necessary.
1861 */
1862 public void onActionModeFinished(ActionMode mode) {
1863 if (!isInCustomActionMode()) return;
1864 mUi.onActionModeFinished(mInLoad);
1865 mActionMode = null;
1866 }
1867
1868 boolean isInLoad() {
1869 return mInLoad;
1870 }
1871
1872 // bookmark handling
1873
1874 /**
1875 * add the current page as a bookmark to the given folder id
1876 * @param folderId use -1 for the default folder
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001877 * @param canBeAnEdit If true, check to see whether the site is already
1878 * bookmarked, and if it is, edit that bookmark. If false, and
1879 * the site is already bookmarked, do not attempt to edit the
1880 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001881 */
1882 @Override
John Reck3ffc5ca2011-06-10 15:56:06 -07001883 public void bookmarkCurrentPage(boolean canBeAnEdit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001884 Intent i = new Intent(mActivity,
1885 AddBookmarkPage.class);
1886 WebView w = getCurrentTopWebView();
1887 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1888 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1889 String touchIconUrl = w.getTouchIconUrl();
1890 if (touchIconUrl != null) {
1891 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1892 WebSettings settings = w.getSettings();
1893 if (settings != null) {
1894 i.putExtra(AddBookmarkPage.USER_AGENT,
1895 settings.getUserAgentString());
1896 }
1897 }
1898 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1899 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1900 getDesiredThumbnailHeight(mActivity)));
1901 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001902 if (canBeAnEdit) {
1903 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1904 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001905 // Put the dialog at the upper right of the screen, covering the
1906 // star on the title bar.
1907 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
1908 mActivity.startActivity(i);
1909 }
1910
1911 // file chooser
1912 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1913 mUploadHandler = new UploadHandler(this);
1914 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1915 }
1916
1917 // thumbnails
1918
1919 /**
1920 * Return the desired width for thumbnail screenshots, which are stored in
1921 * the database, and used on the bookmarks screen.
1922 * @param context Context for finding out the density of the screen.
1923 * @return desired width for thumbnail screenshot.
1924 */
1925 static int getDesiredThumbnailWidth(Context context) {
1926 return context.getResources().getDimensionPixelOffset(
1927 R.dimen.bookmarkThumbnailWidth);
1928 }
1929
1930 /**
1931 * Return the desired height for thumbnail screenshots, which are stored in
1932 * the database, and used on the bookmarks screen.
1933 * @param context Context for finding out the density of the screen.
1934 * @return desired height for thumbnail screenshot.
1935 */
1936 static int getDesiredThumbnailHeight(Context context) {
1937 return context.getResources().getDimensionPixelOffset(
1938 R.dimen.bookmarkThumbnailHeight);
1939 }
1940
Michael Kolb1acef692011-03-08 14:12:06 -08001941 static Bitmap createScreenshot(Tab tab, int width, int height) {
1942 if ((tab != null) && (tab.getWebView() != null)) {
1943 return createScreenshot(tab.getWebView(), width, height);
1944 }
1945 return null;
1946 }
1947
Michael Kolb8233fac2010-10-26 16:08:53 -07001948 private static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08001949 // We render to a bitmap 2x the desired size so that we can then
1950 // re-scale it with filtering since canvas.scale doesn't filter
1951 // This helps reduce aliasing at the cost of being slightly blurry
1952 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07001953 Picture thumbnail = view.capturePicture();
1954 if (thumbnail == null) {
1955 return null;
1956 }
John Reck5c6ac2f2011-01-05 10:18:03 -08001957 width *= filter_scale;
1958 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07001959 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1960 Canvas canvas = new Canvas(bm);
1961 // May need to tweak these values to determine what is the
1962 // best scale factor
1963 int thumbnailWidth = thumbnail.getWidth();
1964 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08001965 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08001966 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08001967 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07001968 } else {
1969 return null;
1970 }
John Reckfe49ab42010-11-16 17:09:37 -08001971
Michael Kolbeb95db42011-03-03 10:38:40 -08001972 float scaleFactorY = (float) height / (float)thumbnailHeight;
1973 if (scaleFactorY > scaleFactor) {
1974 // The picture is narrower than the requested AR
1975 // Center the thumnail and crop the sides
1976 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08001977 float wx = (thumbnailWidth * scaleFactor) - width;
1978 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07001979 }
1980
John Reckfe49ab42010-11-16 17:09:37 -08001981 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07001982
1983 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08001984 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
1985 height / filter_scale, true);
1986 bm.recycle();
1987 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07001988 }
1989
John Reck34ef2672011-02-10 11:30:55 -08001990 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001991 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07001992 // FIXME: Would like to make sure there is actually something to
1993 // draw, but the API for that (WebViewCore.pictureReady()) is not
1994 // currently accessible here.
1995
John Reck34ef2672011-02-10 11:30:55 -08001996 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08001997 if (view == null) {
1998 // Tab was destroyed
1999 return;
2000 }
John Reck34ef2672011-02-10 11:30:55 -08002001 final String url = tab.getUrl();
2002 final String originalUrl = view.getOriginalUrl();
2003
2004 if (TextUtils.isEmpty(url)) {
2005 return;
2006 }
2007
2008 // Only update thumbnails for web urls (http(s)://), not for
2009 // about:, javascript:, data:, etc...
2010 // Unless it is a bookmarked site, then always update
2011 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2012 return;
2013 }
2014
Michael Kolb8233fac2010-10-26 16:08:53 -07002015 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2016 getDesiredThumbnailHeight(mActivity));
2017 if (bm == null) {
2018 return;
2019 }
2020
2021 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002022 new AsyncTask<Void, Void, Void>() {
2023 @Override
2024 protected Void doInBackground(Void... unused) {
2025 Cursor cursor = null;
2026 try {
2027 // TODO: Clean this up
2028 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2029 if (cursor != null && cursor.moveToFirst()) {
2030 final ByteArrayOutputStream os =
2031 new ByteArrayOutputStream();
2032 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002033
John Reck34ef2672011-02-10 11:30:55 -08002034 ContentValues values = new ContentValues();
2035 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002036
John Reck34ef2672011-02-10 11:30:55 -08002037 do {
John Reck617fd832011-02-16 14:35:59 -08002038 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002039 cr.update(Images.CONTENT_URI, values, null, null);
2040 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002041 }
John Reck34ef2672011-02-10 11:30:55 -08002042 } catch (IllegalStateException e) {
2043 // Ignore
2044 } finally {
2045 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002046 }
John Reck34ef2672011-02-10 11:30:55 -08002047 return null;
2048 }
2049 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002050 }
2051
2052 private class Copy implements OnMenuItemClickListener {
2053 private CharSequence mText;
2054
2055 public boolean onMenuItemClick(MenuItem item) {
2056 copy(mText);
2057 return true;
2058 }
2059
2060 public Copy(CharSequence toCopy) {
2061 mText = toCopy;
2062 }
2063 }
2064
Leon Scroggins63c02662010-11-18 15:16:27 -05002065 private static class Download implements OnMenuItemClickListener {
2066 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002067 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002068 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002069
2070 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002071 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002072 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002073 return true;
2074 }
2075
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002076 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002077 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002078 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002079 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002080 }
2081 }
2082
Cary Clark8974d282010-11-22 10:46:05 -05002083 private static class SelectText implements OnMenuItemClickListener {
2084 private WebView mWebView;
2085
2086 public boolean onMenuItemClick(MenuItem item) {
2087 if (mWebView != null) {
2088 return mWebView.selectText();
2089 }
2090 return false;
2091 }
2092
2093 public SelectText(WebView webView) {
2094 mWebView = webView;
2095 }
2096
2097 }
2098
Michael Kolb8233fac2010-10-26 16:08:53 -07002099 /********************** TODO: UI stuff *****************************/
2100
2101 // these methods have been copied, they still need to be cleaned up
2102
2103 /****************** tabs ***************************************************/
2104
2105 // basic tab interactions:
2106
2107 // it is assumed that tabcontrol already knows about the tab
2108 protected void addTab(Tab tab) {
2109 mUi.addTab(tab);
2110 }
2111
2112 protected void removeTab(Tab tab) {
2113 mUi.removeTab(tab);
2114 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002115 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002116 }
2117
Michael Kolbf2055602011-04-09 17:20:03 -07002118 @Override
2119 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002120 // monkey protection against delayed start
2121 if (tab != null) {
2122 mTabControl.setCurrentTab(tab);
2123 // the tab is guaranteed to have a webview after setCurrentTab
2124 mUi.setActiveTab(tab);
2125 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002126 }
2127
2128 protected void closeEmptyChildTab() {
2129 Tab current = mTabControl.getCurrentTab();
2130 if (current != null
2131 && current.getWebView().copyBackForwardList().getSize() == 0) {
Michael Kolbc831b632011-05-11 09:30:34 -07002132 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002133 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002134 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002135 closeTab(current);
2136 }
2137 }
2138 }
2139
John Reck26b18322011-06-21 13:08:58 -07002140 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002141 // Dismiss the subwindow if applicable.
2142 dismissSubWindow(appTab);
2143 // Since we might kill the WebView, remove it from the
2144 // content view first.
2145 mUi.detachTab(appTab);
2146 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002147 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002148 // TODO: analyze why the remove and add are necessary
2149 mUi.attachTab(appTab);
2150 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002151 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002152 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002153 } else {
2154 // If the tab was the current tab, we have to attach
2155 // it to the view system again.
2156 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002157 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002158 }
2159 }
2160
2161 // Remove the sub window if it exists. Also called by TabControl when the
2162 // user clicks the 'X' to dismiss a sub window.
2163 public void dismissSubWindow(Tab tab) {
2164 removeSubWindow(tab);
2165 // dismiss the subwindow. This will destroy the WebView.
2166 tab.dismissSubWindow();
2167 getCurrentTopWebView().requestFocus();
2168 }
2169
2170 @Override
2171 public void removeSubWindow(Tab t) {
2172 if (t.getSubWebView() != null) {
2173 mUi.removeSubWindow(t.getSubViewContainer());
2174 }
2175 }
2176
2177 @Override
2178 public void attachSubWindow(Tab tab) {
2179 if (tab.getSubWebView() != null) {
2180 mUi.attachSubWindow(tab.getSubViewContainer());
2181 getCurrentTopWebView().requestFocus();
2182 }
2183 }
2184
Michael Kolb7bcafde2011-05-09 13:55:59 -07002185 // open a non inconito tab with the given url data
2186 // and set as active tab
2187 public Tab openTab(UrlData urlData) {
Michael Kolb14612442011-06-24 13:06:29 -07002188 if (urlData.isPreloaded()) {
2189 Tab tab = urlData.getPreloadedTab();
2190 tab.getWebView().clearHistory();
2191 mTabControl.addPreloadedTab(tab);
2192 setActiveTab(tab);
2193 return tab;
2194 } else {
2195 Tab tab = createNewTab(false, true, true);
2196 if ((tab != null) && !urlData.isEmpty()) {
2197 loadUrlDataIn(tab, urlData);
2198 }
2199 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002200 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002201 }
2202
Michael Kolb843510f2010-12-09 10:51:49 -08002203 @Override
2204 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002205 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002206 }
2207
Michael Kolb8233fac2010-10-26 16:08:53 -07002208 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002209 public Tab openIncognitoTab() {
2210 return openTab(INCOGNITO_URI, true, true, false);
2211 }
2212
2213 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002214 public Tab openTab(String url, boolean incognito, boolean setActive,
2215 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002216 return openTab(url, incognito, setActive, useCurrent, null);
2217 }
2218
2219 @Override
2220 public Tab openTab(String url, Tab parent, boolean setActive,
2221 boolean useCurrent) {
2222 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2223 setActive, useCurrent, parent);
2224 }
2225
2226 public Tab openTab(String url, boolean incognito, boolean setActive,
2227 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002228 Tab tab = createNewTab(incognito, setActive, useCurrent);
2229 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002230 if (parent != null && parent != tab) {
2231 parent.addChildTab(tab);
2232 }
Michael Kolb519d2282011-05-09 17:03:19 -07002233 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002234 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002235 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002236 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002237 return tab;
2238 }
2239
2240 // this method will attempt to create a new tab
2241 // incognito: private browsing tab
2242 // setActive: ste tab as current tab
2243 // useCurrent: if no new tab can be created, return current tab
2244 private Tab createNewTab(boolean incognito, boolean setActive,
2245 boolean useCurrent) {
2246 Tab tab = null;
2247 if (mTabControl.canCreateNewTab()) {
2248 tab = mTabControl.createNewTab(incognito);
2249 addTab(tab);
2250 if (setActive) {
2251 setActiveTab(tab);
2252 }
2253 } else {
2254 if (useCurrent) {
2255 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002256 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002257 } else {
2258 mUi.showMaxTabsWarning();
2259 }
2260 }
2261 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002262 }
2263
John Reck2bc80422011-06-30 15:11:49 -07002264 @Override
2265 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2266 SnapshotTab tab = null;
2267 if (mTabControl.canCreateNewTab()) {
2268 tab = mTabControl.createSnapshotTab(snapshotId);
2269 addTab(tab);
2270 if (setActive) {
2271 setActiveTab(tab);
2272 }
2273 } else {
2274 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002275 }
2276 return tab;
2277 }
2278
Michael Kolb8233fac2010-10-26 16:08:53 -07002279 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002280 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002281 * @return boolean True if we successfully switched to a different tab. If
2282 * the indexth tab is null, or if that tab is the same as
2283 * the current one, return false.
2284 */
2285 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002286 public boolean switchToTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002287 // hide combo view if open
2288 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002289 Tab currentTab = mTabControl.getCurrentTab();
2290 if (tab == null || tab == currentTab) {
2291 return false;
2292 }
2293 setActiveTab(tab);
2294 return true;
2295 }
2296
2297 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002298 public void closeCurrentTab() {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002299 // hide combo view if open
2300 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002301 if (mTabControl.getTabCount() == 1) {
John Reck958b2422010-12-03 17:56:17 -08002302 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002303 return;
2304 }
Michael Kolbc831b632011-05-11 09:30:34 -07002305 final Tab current = mTabControl.getCurrentTab();
2306 final int pos = mTabControl.getCurrentPosition();
2307 Tab newTab = current.getParent();
2308 if (newTab == null) {
2309 newTab = mTabControl.getTab(pos + 1);
2310 if (newTab == null) {
2311 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002312 }
2313 }
Michael Kolbc831b632011-05-11 09:30:34 -07002314 if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002315 // Close window
2316 closeTab(current);
2317 }
2318 }
2319
2320 /**
2321 * Close the tab, remove its associated title bar, and adjust mTabControl's
2322 * current tab to a valid value.
2323 */
2324 @Override
2325 public void closeTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002326 // hide combo view if open
2327 removeComboView();
Michael Kolb2d59c322011-01-25 13:18:55 -08002328 removeTab(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002329 }
2330
Michael Kolb8233fac2010-10-26 16:08:53 -07002331 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002332 protected void loadUrlFromContext(String url) {
2333 Tab tab = getCurrentTab();
2334 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002335 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002336 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002337 url = UrlUtils.smartUrlFilter(url);
2338 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002339 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002340 }
2341 }
2342 }
2343
2344 /**
2345 * Load the URL into the given WebView and update the title bar
2346 * to reflect the new load. Call this instead of WebView.loadUrl
2347 * directly.
2348 * @param view The WebView used to load url.
2349 * @param url The URL to load.
2350 */
John Reck71e51422011-07-01 16:49:28 -07002351 @Override
2352 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002353 loadUrl(tab, url, null);
2354 }
2355
2356 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2357 if (tab != null) {
2358 dismissSubWindow(tab);
2359 tab.loadUrl(url, headers);
2360 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002361 }
2362
2363 /**
2364 * Load UrlData into a Tab and update the title bar to reflect the new
2365 * load. Call this instead of UrlData.loadIn directly.
2366 * @param t The Tab used to load.
2367 * @param data The UrlData being loaded.
2368 */
2369 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002370 if (data != null) {
2371 if (data.mVoiceIntent != null) {
2372 t.activateVoiceSearchMode(data.mVoiceIntent);
Michael Kolb14612442011-06-24 13:06:29 -07002373 } else if (data.isPreloaded()) {
2374 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002375 } else {
2376 loadUrl(t, data.mUrl, data.mHeaders);
2377 }
2378 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002379 }
2380
John Reck30c714c2010-12-16 17:30:34 -08002381 @Override
2382 public void onUserCanceledSsl(Tab tab) {
2383 WebView web = tab.getWebView();
2384 // TODO: Figure out the "right" behavior
2385 if (web.canGoBack()) {
2386 web.goBack();
2387 } else {
2388 web.loadUrl(mSettings.getHomePage());
2389 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002390 }
2391
2392 void goBackOnePageOrQuit() {
2393 Tab current = mTabControl.getCurrentTab();
2394 if (current == null) {
2395 /*
2396 * Instead of finishing the activity, simply push this to the back
2397 * of the stack and let ActivityManager to choose the foreground
2398 * activity. As BrowserActivity is singleTask, it will be always the
2399 * root of the task. So we can use either true or false for
2400 * moveTaskToBack().
2401 */
2402 mActivity.moveTaskToBack(true);
2403 return;
2404 }
2405 WebView w = current.getWebView();
2406 if (w.canGoBack()) {
2407 w.goBack();
2408 } else {
2409 // Check to see if we are closing a window that was created by
2410 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002411 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002412 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002413 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002414 // Now we close the other tab
2415 closeTab(current);
2416 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002417 /*
2418 * Instead of finishing the activity, simply push this to the back
2419 * of the stack and let ActivityManager to choose the foreground
2420 * activity. As BrowserActivity is singleTask, it will be always the
2421 * root of the task. So we can use either true or false for
2422 * moveTaskToBack().
2423 */
2424 mActivity.moveTaskToBack(true);
2425 }
2426 }
2427 }
2428
2429 /**
2430 * Feed the previously stored results strings to the BrowserProvider so that
2431 * the SearchDialog will show them instead of the standard searches.
2432 * @param result String to show on the editable line of the SearchDialog.
2433 */
2434 @Override
2435 public void showVoiceSearchResults(String result) {
2436 ContentProviderClient client = mActivity.getContentResolver()
2437 .acquireContentProviderClient(Browser.BOOKMARKS_URI);
2438 ContentProvider prov = client.getLocalContentProvider();
2439 BrowserProvider bp = (BrowserProvider) prov;
2440 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
2441 client.release();
2442
2443 Bundle bundle = createGoogleSearchSourceBundle(
2444 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
2445 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
2446 startSearch(result, false, bundle, false);
2447 }
2448
2449 private void startSearch(String initialQuery, boolean selectInitialQuery,
2450 Bundle appSearchData, boolean globalSearch) {
2451 if (appSearchData == null) {
2452 appSearchData = createGoogleSearchSourceBundle(
2453 GOOGLE_SEARCH_SOURCE_TYPE);
2454 }
2455
2456 SearchEngine searchEngine = mSettings.getSearchEngine();
2457 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
2458 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
2459 }
2460 mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData,
2461 globalSearch);
2462 }
2463
2464 private Bundle createGoogleSearchSourceBundle(String source) {
2465 Bundle bundle = new Bundle();
2466 bundle.putString(Search.SOURCE, source);
2467 return bundle;
2468 }
2469
2470 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002471 * helper method for key handler
2472 * returns the current tab if it can't advance
2473 */
Michael Kolbc831b632011-05-11 09:30:34 -07002474 private Tab getNextTab() {
2475 return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1,
2476 mTabControl.getCurrentPosition() + 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002477 }
2478
2479 /**
2480 * helper method for key handler
2481 * returns the current tab if it can't advance
2482 */
Michael Kolbc831b632011-05-11 09:30:34 -07002483 private Tab getPrevTab() {
2484 return mTabControl.getTab(Math.max(0,
2485 mTabControl.getCurrentPosition() - 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002486 }
2487
2488 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002489 * handle key events in browser
2490 *
2491 * @param keyCode
2492 * @param event
2493 * @return true if handled, false to pass to super
2494 */
2495 boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002496 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002497 // Even if MENU is already held down, we need to call to super to open
2498 // the IME on long press.
Michael Kolb2814a362011-05-19 15:49:41 -07002499 if (KeyEvent.KEYCODE_MENU == keyCode) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002500 if (mOptionsMenuHandler != null) {
2501 return false;
2502 } else {
2503 event.startTracking();
2504 return true;
2505 }
Michael Kolb2814a362011-05-19 15:49:41 -07002506 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002507 if (!noModifiers
2508 && ((KeyEvent.KEYCODE_MENU == keyCode)
2509 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2510 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002511 mMenuIsDown = true;
2512 return false;
2513 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002514
Cary Clark8ff8c662010-12-29 15:03:05 -05002515 WebView webView = getCurrentTopWebView();
2516 if (webView == null) return false;
2517
Cary Clark160bbb92011-01-10 11:17:07 -05002518 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2519 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002520
Michael Kolb8233fac2010-10-26 16:08:53 -07002521 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002522 case KeyEvent.KEYCODE_TAB:
2523 if (event.isCtrlPressed()) {
2524 if (event.isShiftPressed()) {
2525 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002526 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002527 } else {
2528 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002529 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002530 }
2531 return true;
2532 }
2533 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002534 case KeyEvent.KEYCODE_SPACE:
2535 // WebView/WebTextView handle the keys in the KeyDown. As
2536 // the Activity's shortcut keys are only handled when WebView
2537 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002538 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002539 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002540 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002541 pageDown();
2542 }
2543 return true;
2544 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002545 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002546 event.startTracking();
2547 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002548 case KeyEvent.KEYCODE_DPAD_LEFT:
2549 if (ctrl) {
2550 webView.goBack();
2551 return true;
2552 }
2553 break;
2554 case KeyEvent.KEYCODE_DPAD_RIGHT:
2555 if (ctrl) {
2556 webView.goForward();
2557 return true;
2558 }
2559 break;
2560 case KeyEvent.KEYCODE_A:
2561 if (ctrl) {
2562 webView.selectAll();
2563 return true;
2564 }
2565 break;
Michael Kolba4183062011-01-16 10:43:21 -08002566// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002567 case KeyEvent.KEYCODE_C:
2568 if (ctrl) {
2569 webView.copySelection();
2570 return true;
2571 }
2572 break;
Michael Kolba4183062011-01-16 10:43:21 -08002573// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002574// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002575// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002576// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002577// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002578// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002579// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002580// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002581// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002582// case KeyEvent.KEYCODE_M: // unused
2583// case KeyEvent.KEYCODE_N: // in Chrome: new window
2584// case KeyEvent.KEYCODE_O: // in Chrome: open file
2585// case KeyEvent.KEYCODE_P: // in Chrome: print page
2586// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002587// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002588// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2589 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002590 // we can't use the ctrl/shift flags, they check for
2591 // exclusive use of a modifier
2592 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002593 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002594 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002595 } else {
2596 openTabToHomePage();
2597 }
2598 return true;
2599 }
2600 break;
2601// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2602// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb1a2eba42011-03-16 16:42:49 -07002603 case KeyEvent.KEYCODE_W: // in Chrome: close tab
2604 if (ctrl) {
2605 closeCurrentTab();
2606 return true;
2607 }
2608 break;
Cary Clark8ff8c662010-12-29 15:03:05 -05002609// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2610// case KeyEvent.KEYCODE_Y: // unused
2611// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002612 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002613 // it is a regular key and webview is not null
2614 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002615 }
2616
John Recke6bf4ab2011-02-24 15:48:05 -08002617 boolean onKeyLongPress(int keyCode, KeyEvent event) {
2618 switch(keyCode) {
2619 case KeyEvent.KEYCODE_BACK:
2620 if (mUi.showsWeb()) {
2621 bookmarksOrHistoryPicker(true);
2622 return true;
2623 }
2624 break;
2625 }
2626 return false;
2627 }
2628
Michael Kolb8233fac2010-10-26 16:08:53 -07002629 boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb2814a362011-05-19 15:49:41 -07002630 if (KeyEvent.KEYCODE_MENU == keyCode) {
2631 mMenuIsDown = false;
2632 if (event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002633 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002634 }
2635 }
Cary Clark160bbb92011-01-10 11:17:07 -05002636 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002637 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002638 case KeyEvent.KEYCODE_BACK:
2639 if (event.isTracking() && !event.isCanceled()) {
2640 onBackKey();
2641 return true;
2642 }
2643 break;
2644 }
2645 return false;
2646 }
2647
2648 public boolean isMenuDown() {
2649 return mMenuIsDown;
2650 }
2651
Ben Murdoch8029a772010-11-16 11:58:21 +00002652 public void setupAutoFill(Message message) {
2653 // Open the settings activity at the AutoFill profile fragment so that
2654 // the user can create a new profile. When they return, we will dispatch
2655 // the message so that we can autofill the form using their new profile.
2656 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2657 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2658 AutoFillSettingsFragment.class.getName());
2659 mAutoFillSetupMessage = message;
2660 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2661 }
John Reckb3417f02011-01-14 11:01:05 -08002662
2663 @Override
2664 public void registerOptionsMenuHandler(OptionsMenuHandler handler) {
2665 mOptionsMenuHandler = handler;
2666 }
2667
2668 @Override
2669 public void unregisterOptionsMenuHandler(OptionsMenuHandler handler) {
2670 if (mOptionsMenuHandler == handler) {
2671 mOptionsMenuHandler = null;
2672 }
2673 }
2674
Narayan Kamath5119edd2011-02-23 15:49:17 +00002675 @Override
2676 public void registerDropdownChangeListener(DropdownChangeListener d) {
2677 mUi.registerDropdownChangeListener(d);
2678 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002679}