blob: 911f724373c23f3973074903ad9a438c9a7d1986 [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;
Martijn Coenenb2f93552011-06-14 10:48:35 +020041import android.nfc.NfcAdapter;
Michael Kolb8233fac2010-10-26 16:08:53 -070042import android.os.AsyncTask;
43import android.os.Bundle;
Leon Scrogginsac993842011-02-02 12:54:07 -050044import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070045import android.os.Handler;
46import android.os.Message;
47import android.os.PowerManager;
48import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000049import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.provider.Browser;
51import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070052import android.provider.BrowserContract.Images;
53import android.provider.ContactsContract;
54import android.provider.ContactsContract.Intents.Insert;
Michael Kolbcfa3af52010-12-14 10:36:11 -080055import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070056import android.text.TextUtils;
57import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080058import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070059import android.view.ActionMode;
60import android.view.ContextMenu;
61import android.view.ContextMenu.ContextMenuInfo;
62import android.view.Gravity;
63import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070064import android.view.Menu;
65import android.view.MenuInflater;
66import android.view.MenuItem;
67import android.view.MenuItem.OnMenuItemClickListener;
68import android.view.View;
69import android.webkit.CookieManager;
70import android.webkit.CookieSyncManager;
71import android.webkit.HttpAuthHandler;
72import android.webkit.SslErrorHandler;
73import android.webkit.ValueCallback;
74import android.webkit.WebChromeClient;
75import android.webkit.WebIconDatabase;
76import android.webkit.WebSettings;
77import android.webkit.WebView;
Leon Scrogginsac993842011-02-02 12:54:07 -050078import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070079
Michael Kolb4bd767d2011-05-27 11:33:55 -070080import com.android.browser.IntentHandler.UrlData;
81import com.android.browser.UI.DropdownChangeListener;
82import com.android.browser.provider.BrowserProvider;
John Reckd8c74522011-06-14 08:45:00 -070083import com.android.browser.provider.BrowserProvider2.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070084import com.android.browser.search.SearchEngine;
85import com.android.common.Search;
86
John Reckf33b1632011-06-04 20:00:23 -070087import java.io.ByteArrayInputStream;
Michael Kolb8233fac2010-10-26 16:08:53 -070088import java.io.ByteArrayOutputStream;
89import java.io.File;
John Reckf33b1632011-06-04 20:00:23 -070090import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -070091import java.net.URLEncoder;
92import java.util.Calendar;
93import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080094import java.util.List;
Michael Kolb8233fac2010-10-26 16:08:53 -070095
96/**
97 * Controller for browser
98 */
99public class Controller
100 implements WebViewController, UiController {
101
102 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800103 private static final String SEND_APP_ID_EXTRA =
104 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700105 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800106
Michael Kolb8233fac2010-10-26 16:08:53 -0700107
108 // public message ids
109 public final static int LOAD_URL = 1001;
110 public final static int STOP_LOAD = 1002;
111
112 // Message Ids
113 private static final int FOCUS_NODE_HREF = 102;
114 private static final int RELEASE_WAKELOCK = 107;
115
116 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
117
118 private static final int OPEN_BOOKMARKS = 201;
119
120 private static final int EMPTY_MENU = -1;
121
Michael Kolb8233fac2010-10-26 16:08:53 -0700122 // activity requestCode
123 final static int PREFERENCES_PAGE = 3;
124 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000125 final static int AUTOFILL_SETUP = 5;
126
Michael Kolb8233fac2010-10-26 16:08:53 -0700127 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
128
129 // As the ids are dynamically created, we can't guarantee that they will
130 // be in sequence, so this static array maps ids to a window number.
131 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
132 { R.id.window_one_menu_id, R.id.window_two_menu_id,
133 R.id.window_three_menu_id, R.id.window_four_menu_id,
134 R.id.window_five_menu_id, R.id.window_six_menu_id,
135 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
136
137 // "source" parameter for Google search through search key
138 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
139 // "source" parameter for Google search through simplily type
140 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
141
Guang Zhu9e78f512011-05-04 11:45:11 -0700142 // "no-crash-recovery" parameter in intetnt to suppress crash recovery
143 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
144
Michael Kolb8233fac2010-10-26 16:08:53 -0700145 private Activity mActivity;
146 private UI mUi;
147 private TabControl mTabControl;
148 private BrowserSettings mSettings;
149 private WebViewFactory mFactory;
John Reckb3417f02011-01-14 11:01:05 -0800150 private OptionsMenuHandler mOptionsMenuHandler = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700151
152 private WakeLock mWakeLock;
153
154 private UrlHandler mUrlHandler;
155 private UploadHandler mUploadHandler;
156 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700157 private PageDialogsHandler mPageDialogsHandler;
158 private NetworkStateHandler mNetworkHandler;
Martijn Coenenb2f93552011-06-14 10:48:35 +0200159 private NfcHandler mNfcHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700160
Ben Murdoch8029a772010-11-16 11:58:21 +0000161 private Message mAutoFillSetupMessage;
162
Michael Kolb8233fac2010-10-26 16:08:53 -0700163 private boolean mShouldShowErrorConsole;
164
165 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
166
167 // FIXME, temp address onPrepareMenu performance problem.
168 // When we move everything out of view, we should rewrite this.
169 private int mCurrentMenuState = 0;
170 private int mMenuState = R.id.MAIN_MENU;
171 private int mOldMenuState = EMPTY_MENU;
172 private Menu mCachedMenu;
173
Michael Kolb8233fac2010-10-26 16:08:53 -0700174 private boolean mMenuIsDown;
175
176 // For select and find, we keep track of the ActionMode so that
177 // finish() can be called as desired.
178 private ActionMode mActionMode;
179
180 /**
181 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
182 * of whether the configuration has changed. The first onMenuOpened call
183 * after a configuration change is simply a reopening of the same menu
184 * (i.e. mIconView did not change).
185 */
186 private boolean mConfigChanged;
187
188 /**
189 * Keeps track of whether the options menu is open. This is important in
190 * determining whether to show or hide the title bar overlay
191 */
192 private boolean mOptionsMenuOpen;
193
194 /**
195 * Whether or not the options menu is in its bigger, popup menu form. When
196 * true, we want the title bar overlay to be gone. When false, we do not.
197 * Only meaningful if mOptionsMenuOpen is true.
198 */
199 private boolean mExtendedMenuOpen;
200
201 private boolean mInLoad;
202
203 private boolean mActivityPaused = true;
204 private boolean mLoadStopped;
205
206 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500207 // Checks to see when the bookmarks database has changed, and updates the
208 // Tabs' notion of whether they represent bookmarked sites.
209 private ContentObserver mBookmarksObserver;
John Reck0ebd3ac2010-12-09 11:14:04 -0800210 private DataController mDataController;
John Reck847b5322011-04-14 17:02:18 -0700211 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700212
213 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
214 @Override
215 public Void doInBackground(File... files) {
216 if (files != null) {
217 for (File f : files) {
218 if (!f.delete()) {
219 Log.e(LOGTAG, f.getPath() + " was not deleted");
220 }
221 }
222 }
223 return null;
224 }
225 }
226
227 public Controller(Activity browser) {
228 mActivity = browser;
229 mSettings = BrowserSettings.getInstance();
John Reck0ebd3ac2010-12-09 11:14:04 -0800230 mDataController = DataController.getInstance(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700231 mTabControl = new TabControl(this);
232 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700233 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700234
235 mUrlHandler = new UrlHandler(this);
236 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700237 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200238 mNfcHandler = new NfcHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700239
240 PowerManager pm = (PowerManager) mActivity
241 .getSystemService(Context.POWER_SERVICE);
242 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
243
244 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500245 mBookmarksObserver = new ContentObserver(mHandler) {
246 @Override
247 public void onChange(boolean selfChange) {
248 int size = mTabControl.getTabCount();
249 for (int i = 0; i < size; i++) {
250 mTabControl.getTab(i).updateBookmarkedStatus();
251 }
252 }
253
254 };
255 browser.getContentResolver().registerContentObserver(
256 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700257
258 mNetworkHandler = new NetworkStateHandler(mActivity, this);
259 // Start watching the default geolocation permissions
260 mSystemAllowGeolocationOrigins =
261 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
262 mSystemAllowGeolocationOrigins.start();
263
264 retainIconsOnStartup();
265 }
266
Patrick Scott7d50a932011-02-04 09:27:26 -0500267 void start(final Bundle icicle, final Intent intent) {
Guang Zhu9e78f512011-05-04 11:45:11 -0700268 boolean noCrashRecovery = intent.getBooleanExtra(NO_CRASH_RECOVERY, false);
269 if (icicle != null || noCrashRecovery) {
John Reck847b5322011-04-14 17:02:18 -0700270 doStart(icicle, intent);
271 } else {
272 mCrashRecoveryHandler.startRecovery(intent);
273 }
274 }
275
276 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 // Unless the last browser usage was within 24 hours, destroy any
278 // remaining incognito tabs.
279
280 Calendar lastActiveDate = icicle != null ?
281 (Calendar) icicle.getSerializable("lastActiveDate") : null;
282 Calendar today = Calendar.getInstance();
283 Calendar yesterday = Calendar.getInstance();
284 yesterday.add(Calendar.DATE, -1);
285
Patrick Scott7d50a932011-02-04 09:27:26 -0500286 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700287 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800288 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700289
Patrick Scott7d50a932011-02-04 09:27:26 -0500290 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700291 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500292 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000293
Michael Kolbc831b632011-05-11 09:30:34 -0700294 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500295 // Not able to restore so we go ahead and clear session cookies. We
296 // must do this before trying to login the user as we don't want to
297 // clear any session cookies set during login.
298 CookieManager.getInstance().removeSessionCookie();
299 }
300
Patrick Scottd43e75a2011-03-14 14:47:23 -0400301 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500302 new Runnable() {
303 @Override public void run() {
Michael Kolbc831b632011-05-11 09:30:34 -0700304 onPreloginFinished(icicle, intent, currentTabId, restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500305 }
306 });
307 }
308
Michael Kolbc831b632011-05-11 09:30:34 -0700309 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500310 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700311 if (currentTabId == -1) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700312 final Bundle extra = intent.getExtras();
313 // Create an initial tab.
314 // If the intent is ACTION_VIEW and data is not null, the Browser is
315 // invoked to view the content by another application. In this case,
316 // the tab will be close when exit.
317 UrlData urlData = mIntentHandler.getUrlDataFromIntent(intent);
Michael Kolb7bcafde2011-05-09 13:55:59 -0700318 Tab t = null;
319 if (urlData.isEmpty()) {
320 t = openTabToHomePage();
321 } else {
322 t = openTab(urlData);
323 }
324 if (t != null) {
325 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
326 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700327 WebView webView = t.getWebView();
328 if (extra != null) {
329 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
330 if (scale > 0 && scale <= 1000) {
331 webView.setInitialScale(scale);
332 }
333 }
John Reckd8c74522011-06-14 08:45:00 -0700334 mTabControl.loadSnapshotTabs();
335 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700336 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700337 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500338 mUi.needsRestoreAllTabs());
Michael Kolb1bf23132010-11-19 12:55:12 -0800339 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700340 // TabControl.restoreState() will create a new tab even if
341 // restoring the state fails.
342 setActiveTab(mTabControl.getCurrentTab());
343 }
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
359 void setWebViewFactory(WebViewFactory factory) {
360 mFactory = factory;
361 }
362
Michael Kolb1514bb72010-11-22 09:11:48 -0800363 @Override
364 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700365 return mFactory;
366 }
367
368 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800369 public void onSetWebView(Tab tab, WebView view) {
370 mUi.onSetWebView(tab, view);
371 }
372
373 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800374 public void createSubWindow(Tab tab) {
375 endActionMode();
376 WebView mainView = tab.getWebView();
377 WebView subView = mFactory.createWebView((mainView == null)
378 ? false
379 : mainView.isPrivateBrowsingEnabled());
380 mUi.createSubWindow(tab, subView);
381 }
382
383 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700384 public Activity getActivity() {
385 return mActivity;
386 }
387
388 void setUi(UI ui) {
389 mUi = ui;
390 }
391
392 BrowserSettings getSettings() {
393 return mSettings;
394 }
395
396 IntentHandler getIntentHandler() {
397 return mIntentHandler;
398 }
399
400 @Override
401 public UI getUi() {
402 return mUi;
403 }
404
405 int getMaxTabs() {
406 return mActivity.getResources().getInteger(R.integer.max_tabs);
407 }
408
409 @Override
410 public TabControl getTabControl() {
411 return mTabControl;
412 }
413
Michael Kolb1bf23132010-11-19 12:55:12 -0800414 @Override
415 public List<Tab> getTabs() {
416 return mTabControl.getTabs();
417 }
418
Michael Kolb8233fac2010-10-26 16:08:53 -0700419 // Open the icon database and retain all the icons for visited sites.
Ben Murdoch9446b932010-11-25 16:20:14 +0000420 // This is done on a background thread so as not to stall startup.
Michael Kolb8233fac2010-10-26 16:08:53 -0700421 private void retainIconsOnStartup() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000422 // WebIconDatabase needs to be retrieved on the UI thread so that if
423 // it has not been created successfully yet the Handler is started on the
424 // UI thread.
425 new RetainIconsOnStartupTask(WebIconDatabase.getInstance()).execute();
426 }
427
428 private class RetainIconsOnStartupTask extends AsyncTask<Void, Void, Void> {
429 private WebIconDatabase mDb;
430
431 public RetainIconsOnStartupTask(WebIconDatabase db) {
432 mDb = db;
433 }
434
John Recka00cbbd2010-12-16 12:38:19 -0800435 @Override
Ben Murdoch9446b932010-11-25 16:20:14 +0000436 protected Void doInBackground(Void... unused) {
437 mDb.open(mActivity.getDir("icons", 0).getPath());
438 Cursor c = null;
439 try {
440 c = Browser.getAllBookmarks(mActivity.getContentResolver());
441 if (c.moveToFirst()) {
442 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
443 do {
444 String url = c.getString(urlIndex);
445 mDb.retainIconForPageUrl(url);
446 } while (c.moveToNext());
447 }
448 } catch (IllegalStateException e) {
449 Log.e(LOGTAG, "retainIconsOnStartup", e);
450 } finally {
451 if (c != null) c.close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700452 }
Ben Murdoch9446b932010-11-25 16:20:14 +0000453
454 return null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700455 }
456 }
457
458 private void startHandler() {
459 mHandler = new Handler() {
460
461 @Override
462 public void handleMessage(Message msg) {
463 switch (msg.what) {
464 case OPEN_BOOKMARKS:
465 bookmarksOrHistoryPicker(false);
466 break;
467 case FOCUS_NODE_HREF:
468 {
469 String url = (String) msg.getData().get("url");
470 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500471 String src = (String) msg.getData().get("src");
472 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700473 if (TextUtils.isEmpty(url)) {
474 break;
475 }
476 HashMap focusNodeMap = (HashMap) msg.obj;
477 WebView view = (WebView) focusNodeMap.get("webview");
478 // Only apply the action if the top window did not change.
479 if (getCurrentTopWebView() != view) {
480 break;
481 }
482 switch (msg.arg1) {
483 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -0700484 loadUrlFromContext(getCurrentTopWebView(), url);
485 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500486 case R.id.view_image_context_menu_id:
487 loadUrlFromContext(getCurrentTopWebView(), src);
488 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500489 case R.id.open_newtab_context_menu_id:
490 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700491 openTab(url, parent,
492 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500493 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700494 case R.id.copy_link_context_menu_id:
495 copy(url);
496 break;
497 case R.id.save_link_context_menu_id:
498 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500499 DownloadHandler.onDownloadStartNoStream(
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000500 mActivity, url, null, null, null,
501 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700502 break;
503 }
504 break;
505 }
506
507 case LOAD_URL:
508 loadUrlFromContext(getCurrentTopWebView(), (String) msg.obj);
509 break;
510
511 case STOP_LOAD:
512 stopLoading();
513 break;
514
515 case RELEASE_WAKELOCK:
516 if (mWakeLock.isHeld()) {
517 mWakeLock.release();
518 // if we reach here, Browser should be still in the
519 // background loading after WAKELOCK_TIMEOUT (5-min).
520 // To avoid burning the battery, stop loading.
521 mTabControl.stopAllLoading();
522 }
523 break;
524
525 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800526 Tab tab = (Tab) msg.obj;
527 if (tab != null) {
528 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700529 }
530 break;
531 }
532 }
533 };
534
535 }
536
Martijn Coenenb2f93552011-06-14 10:48:35 +0200537
538 public Tab getCurrentTab() {
539 return mTabControl.getCurrentTab();
540 }
541
Michael Kolbba99c5d2010-11-29 14:57:41 -0800542 @Override
543 public void shareCurrentPage() {
544 shareCurrentPage(mTabControl.getCurrentTab());
545 }
546
547 private void shareCurrentPage(Tab tab) {
548 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800549 sharePage(mActivity, tab.getTitle(),
550 tab.getUrl(), tab.getFavicon(),
551 createScreenshot(tab.getWebView(),
552 getDesiredThumbnailWidth(mActivity),
553 getDesiredThumbnailHeight(mActivity)));
554 }
555 }
556
Michael Kolb8233fac2010-10-26 16:08:53 -0700557 /**
558 * Share a page, providing the title, url, favicon, and a screenshot. Uses
559 * an {@link Intent} to launch the Activity chooser.
560 * @param c Context used to launch a new Activity.
561 * @param title Title of the page. Stored in the Intent with
562 * {@link Intent#EXTRA_SUBJECT}
563 * @param url URL of the page. Stored in the Intent with
564 * {@link Intent#EXTRA_TEXT}
565 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
566 * with {@link Browser#EXTRA_SHARE_FAVICON}
567 * @param screenshot Bitmap of a screenshot of the page. Stored in the
568 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
569 */
570 static final void sharePage(Context c, String title, String url,
571 Bitmap favicon, Bitmap screenshot) {
572 Intent send = new Intent(Intent.ACTION_SEND);
573 send.setType("text/plain");
574 send.putExtra(Intent.EXTRA_TEXT, url);
575 send.putExtra(Intent.EXTRA_SUBJECT, title);
576 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
577 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
578 try {
579 c.startActivity(Intent.createChooser(send, c.getString(
580 R.string.choosertitle_sharevia)));
581 } catch(android.content.ActivityNotFoundException ex) {
582 // if no app handles it, do nothing
583 }
584 }
585
586 private void copy(CharSequence text) {
587 ClipboardManager cm = (ClipboardManager) mActivity
588 .getSystemService(Context.CLIPBOARD_SERVICE);
589 cm.setText(text);
590 }
591
592 // lifecycle
593
594 protected void onConfgurationChanged(Configuration config) {
595 mConfigChanged = true;
596 if (mPageDialogsHandler != null) {
597 mPageDialogsHandler.onConfigurationChanged(config);
598 }
599 mUi.onConfigurationChanged(config);
600 }
601
602 @Override
603 public void handleNewIntent(Intent intent) {
604 mIntentHandler.onNewIntent(intent);
605 }
606
607 protected void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800608 if (mUi.isCustomViewShowing()) {
609 hideCustomView();
610 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700611 if (mActivityPaused) {
612 Log.e(LOGTAG, "BrowserActivity is already paused.");
613 return;
614 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700615 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800616 Tab tab = mTabControl.getCurrentTab();
617 if (tab != null) {
618 tab.pause();
619 if (!pauseWebViewTimers(tab)) {
620 mWakeLock.acquire();
621 mHandler.sendMessageDelayed(mHandler
622 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
623 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700624 }
625 mUi.onPause();
626 mNetworkHandler.onPause();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200627 mNfcHandler.onPause();
Michael Kolb8233fac2010-10-26 16:08:53 -0700628
629 WebView.disablePlatformNotifications();
John Reck378a4102011-06-09 16:23:01 -0700630 mCrashRecoveryHandler.backupState();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200631
Michael Kolb8233fac2010-10-26 16:08:53 -0700632 }
633
John Reckaed9c542011-05-27 16:08:53 -0700634 void onSaveInstanceState(Bundle outState, boolean saveImages) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 // the default implementation requires each view to have an id. As the
636 // browser handles the state itself and it doesn't use id for the views,
637 // don't call the default implementation. Otherwise it will trigger the
638 // warning like this, "couldn't save which view has focus because the
639 // focused view XXX has no id".
640
641 // Save all the tabs
John Reckaed9c542011-05-27 16:08:53 -0700642 mTabControl.saveState(outState, saveImages);
Michael Kolb8233fac2010-10-26 16:08:53 -0700643 // Save time so that we know how old incognito tabs (if any) are.
644 outState.putSerializable("lastActiveDate", Calendar.getInstance());
645 }
646
647 void onResume() {
648 if (!mActivityPaused) {
649 Log.e(LOGTAG, "BrowserActivity is already resumed.");
650 return;
651 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800653 Tab current = mTabControl.getCurrentTab();
654 if (current != null) {
655 current.resume();
656 resumeWebViewTimers(current);
657 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700658 if (mWakeLock.isHeld()) {
659 mHandler.removeMessages(RELEASE_WAKELOCK);
660 mWakeLock.release();
661 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200662
Michael Kolb8233fac2010-10-26 16:08:53 -0700663 mUi.onResume();
664 mNetworkHandler.onResume();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200665 mNfcHandler.onResume();
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 WebView.enablePlatformNotifications();
667 }
668
Michael Kolb70976932010-11-30 11:34:01 -0800669 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800670 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800671 * @param tab guaranteed non-null
672 */
673 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700674 boolean inLoad = tab.inPageLoad();
675 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
676 CookieSyncManager.getInstance().startSync();
677 WebView w = tab.getWebView();
678 if (w != null) {
679 w.resumeTimers();
680 }
681 }
682 }
683
Michael Kolb70976932010-11-30 11:34:01 -0800684 /**
685 * Pause all WebView timers using the WebView of the given tab
686 * @param tab
687 * @return true if the timers are paused or tab is null
688 */
689 private boolean pauseWebViewTimers(Tab tab) {
690 if (tab == null) {
691 return true;
692 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700693 CookieSyncManager.getInstance().stopSync();
694 WebView w = getCurrentWebView();
695 if (w != null) {
696 w.pauseTimers();
697 }
698 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700699 }
Michael Kolb70976932010-11-30 11:34:01 -0800700 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700701 }
702
703 void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800704 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
706 mUploadHandler = null;
707 }
708 if (mTabControl == null) return;
709 mUi.onDestroy();
710 // Remove the current tab and sub window
711 Tab t = mTabControl.getCurrentTab();
712 if (t != null) {
713 dismissSubWindow(t);
714 removeTab(t);
715 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500716 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700717 // Destroy all the tabs
718 mTabControl.destroy();
719 WebIconDatabase.getInstance().close();
720 // Stop watching the default geolocation permissions
721 mSystemAllowGeolocationOrigins.stop();
722 mSystemAllowGeolocationOrigins = null;
723 }
724
725 protected boolean isActivityPaused() {
726 return mActivityPaused;
727 }
728
729 protected void onLowMemory() {
730 mTabControl.freeMemory();
731 }
732
733 @Override
734 public boolean shouldShowErrorConsole() {
735 return mShouldShowErrorConsole;
736 }
737
738 protected void setShouldShowErrorConsole(boolean show) {
739 if (show == mShouldShowErrorConsole) {
740 // Nothing to do.
741 return;
742 }
743 mShouldShowErrorConsole = show;
744 Tab t = mTabControl.getCurrentTab();
745 if (t == null) {
746 // There is no current tab so we cannot toggle the error console
747 return;
748 }
749 mUi.setShouldShowErrorConsole(t, show);
750 }
751
752 @Override
753 public void stopLoading() {
754 mLoadStopped = true;
755 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700756 WebView w = getCurrentTopWebView();
757 w.stopLoading();
Michael Kolb8233fac2010-10-26 16:08:53 -0700758 mUi.onPageStopped(tab);
759 }
760
761 boolean didUserStopLoading() {
762 return mLoadStopped;
763 }
764
765 // WebViewController
766
767 @Override
John Reck324d4402011-01-11 16:56:42 -0800768 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700769
770 // We've started to load a new page. If there was a pending message
771 // to save a screenshot then we will now take the new page and save
772 // an incorrect screenshot. Therefore, remove any pending thumbnail
773 // messages from the queue.
774 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800775 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700776
777 // reset sync timer to avoid sync starts during loading a page
778 CookieSyncManager.getInstance().resetSync();
779
780 if (!mNetworkHandler.isNetworkUp()) {
781 view.setNetworkAvailable(false);
782 }
783
784 // when BrowserActivity just starts, onPageStarted may be called before
785 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
786 // to start the timer. As we won't switch tabs while an activity is in
787 // pause state, we can ensure calling resume and pause in pair.
788 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800789 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700790 }
791 mLoadStopped = false;
792 if (!mNetworkHandler.isNetworkUp()) {
793 mNetworkHandler.createAndShowNetworkDialog();
794 }
795 endActionMode();
796
John Reck30c714c2010-12-16 17:30:34 -0800797 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700798
John Reck324d4402011-01-11 16:56:42 -0800799 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700800 // update the bookmark database for favicon
801 maybeUpdateFavicon(tab, null, url, favicon);
802
803 Performance.tracePageStart(url);
804
805 // Performance probe
806 if (false) {
807 Performance.onPageStarted();
808 }
809
810 }
811
812 @Override
John Reck324d4402011-01-11 16:56:42 -0800813 public void onPageFinished(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800814 mUi.onTabDataChanged(tab);
John Reck324d4402011-01-11 16:56:42 -0800815 if (!tab.isPrivateBrowsingEnabled()
John Reckd8c74522011-06-14 08:45:00 -0700816 && !TextUtils.isEmpty(tab.getUrl())
817 && !tab.isSnapshot()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700818 if (tab.inForeground() && !didUserStopLoading()
819 || !tab.inForeground()) {
820 // Only update the bookmark screenshot if the user did not
821 // cancel the load early.
822 mHandler.sendMessageDelayed(mHandler.obtainMessage(
John Reck34ef2672011-02-10 11:30:55 -0800823 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Michael Kolb8233fac2010-10-26 16:08:53 -0700824 500);
825 }
826 }
827 // pause the WebView timer and release the wake lock if it is finished
828 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800829 if (mActivityPaused && pauseWebViewTimers(tab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700830 if (mWakeLock.isHeld()) {
831 mHandler.removeMessages(RELEASE_WAKELOCK);
832 mWakeLock.release();
833 }
834 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200835
Michael Kolb8233fac2010-10-26 16:08:53 -0700836 // Performance probe
837 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800838 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 }
840
841 Performance.tracePageFinished();
842 }
843
844 @Override
John Reck30c714c2010-12-16 17:30:34 -0800845 public void onProgressChanged(Tab tab) {
846 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700847
848 if (newProgress == 100) {
849 CookieSyncManager.getInstance().sync();
850 // onProgressChanged() may continue to be called after the main
851 // frame has finished loading, as any remaining sub frames continue
852 // to load. We'll only get called once though with newProgress as
853 // 100 when everything is loaded. (onPageFinished is called once
854 // when the main frame completes loading regardless of the state of
855 // any sub frames so calls to onProgressChanges may continue after
856 // onPageFinished has executed)
857 if (mInLoad) {
858 mInLoad = false;
859 updateInLoadMenuItems(mCachedMenu);
860 }
861 } else {
862 if (!mInLoad) {
863 // onPageFinished may have already been called but a subframe is
864 // still loading and updating the progress. Reset mInLoad and
865 // update the menu items.
866 mInLoad = true;
867 updateInLoadMenuItems(mCachedMenu);
868 }
869 }
John Reck30c714c2010-12-16 17:30:34 -0800870 mUi.onProgressChanged(tab);
871 }
872
873 @Override
874 public void onUpdatedLockIcon(Tab tab) {
875 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700876 }
877
878 @Override
879 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800880 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800881 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800882 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
884 return;
885 }
886 // Update the title in the history database if not in private browsing mode
887 if (!tab.isPrivateBrowsingEnabled()) {
John Reck0ebd3ac2010-12-09 11:14:04 -0800888 mDataController.updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700889 }
890 }
891
892 @Override
893 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800894 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700895 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
896 }
897
898 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800899 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
900 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700901 }
902
903 @Override
904 public boolean shouldOverrideKeyEvent(KeyEvent event) {
905 if (mMenuIsDown) {
906 // only check shortcut key when MENU is held
907 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
908 event);
909 } else {
910 return false;
911 }
912 }
913
914 @Override
915 public void onUnhandledKeyEvent(KeyEvent event) {
916 if (!isActivityPaused()) {
917 if (event.getAction() == KeyEvent.ACTION_DOWN) {
918 mActivity.onKeyDown(event.getKeyCode(), event);
919 } else {
920 mActivity.onKeyUp(event.getKeyCode(), event);
921 }
922 }
923 }
924
925 @Override
John Reck324d4402011-01-11 16:56:42 -0800926 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700927 // Don't save anything in private browsing mode
928 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800929 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700930
John Reck324d4402011-01-11 16:56:42 -0800931 if (TextUtils.isEmpty(url)
932 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700933 return;
934 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800935 mDataController.updateVisitedHistory(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700936 WebIconDatabase.getInstance().retainIconForPageUrl(url);
John Reck847b5322011-04-14 17:02:18 -0700937 if (!mActivityPaused) {
938 // Since we clear the state in onPause, don't backup the current
939 // state if we are already paused
940 mCrashRecoveryHandler.backupState();
941 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700942 }
943
944 @Override
945 public void getVisitedHistory(final ValueCallback<String[]> callback) {
946 AsyncTask<Void, Void, String[]> task =
947 new AsyncTask<Void, Void, String[]>() {
948 @Override
949 public String[] doInBackground(Void... unused) {
950 return Browser.getVisitedHistory(mActivity.getContentResolver());
951 }
952 @Override
953 public void onPostExecute(String[] result) {
954 callback.onReceiveValue(result);
955 }
956 };
957 task.execute();
958 }
959
960 @Override
961 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
962 final HttpAuthHandler handler, final String host,
963 final String realm) {
964 String username = null;
965 String password = null;
966
967 boolean reuseHttpAuthUsernamePassword
968 = handler.useHttpAuthUsernamePassword();
969
970 if (reuseHttpAuthUsernamePassword && view != null) {
971 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
972 if (credentials != null && credentials.length == 2) {
973 username = credentials[0];
974 password = credentials[1];
975 }
976 }
977
978 if (username != null && password != null) {
979 handler.proceed(username, password);
980 } else {
981 if (tab.inForeground()) {
982 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
983 } else {
984 handler.cancel();
985 }
986 }
987 }
988
989 @Override
990 public void onDownloadStart(Tab tab, String url, String userAgent,
991 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000992 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -0500993 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000994 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
995 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700996 // This Tab was opened for the sole purpose of downloading a
997 // file. Remove it.
998 if (tab == mTabControl.getCurrentTab()) {
999 // In this case, the Tab is still on top.
1000 goBackOnePageOrQuit();
1001 } else {
1002 // In this case, it is not.
1003 closeTab(tab);
1004 }
1005 }
1006 }
1007
1008 @Override
1009 public Bitmap getDefaultVideoPoster() {
1010 return mUi.getDefaultVideoPoster();
1011 }
1012
1013 @Override
1014 public View getVideoLoadingProgressView() {
1015 return mUi.getVideoLoadingProgressView();
1016 }
1017
1018 @Override
1019 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1020 SslError error) {
1021 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1022 }
1023
Patrick Scott92066772011-03-10 08:46:27 -05001024 @Override
1025 public void showAutoLogin(Tab tab) {
1026 assert tab.inForeground();
1027 // Update the title bar to show the auto-login request.
1028 mUi.showAutoLogin(tab);
1029 }
1030
1031 @Override
1032 public void hideAutoLogin(Tab tab) {
1033 assert tab.inForeground();
1034 mUi.hideAutoLogin(tab);
1035 }
1036
Michael Kolb8233fac2010-10-26 16:08:53 -07001037 // helper method
1038
1039 /*
1040 * Update the favorites icon if the private browsing isn't enabled and the
1041 * icon is valid.
1042 */
1043 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1044 final String url, Bitmap favicon) {
1045 if (favicon == null) {
1046 return;
1047 }
1048 if (!tab.isPrivateBrowsingEnabled()) {
1049 Bookmarks.updateFavicon(mActivity
1050 .getContentResolver(), originalUrl, url, favicon);
1051 }
1052 }
1053
Leon Scroggins4cd97792010-12-03 15:31:56 -05001054 @Override
1055 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001056 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001057 mUi.bookmarkedStatusHasChanged(tab);
1058 }
1059
Michael Kolb8233fac2010-10-26 16:08:53 -07001060 // end WebViewController
1061
1062 protected void pageUp() {
1063 getCurrentTopWebView().pageUp(false);
1064 }
1065
1066 protected void pageDown() {
1067 getCurrentTopWebView().pageDown(false);
1068 }
1069
1070 // callback from phone title bar
1071 public void editUrl() {
1072 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001073 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001074 }
1075
Michael Kolbcfa3af52010-12-14 10:36:11 -08001076 public void startVoiceSearch() {
1077 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1078 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1079 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1080 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1081 mActivity.getComponentName().flattenToString());
1082 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001083 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001084 mActivity.startActivity(intent);
1085 }
1086
Michael Kolb11d19782011-03-20 10:17:40 -07001087 @Override
1088 public void activateVoiceSearchMode(String title, List<String> results) {
1089 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001090 }
1091
1092 public void revertVoiceSearchMode(Tab tab) {
1093 mUi.revertVoiceTitleBar(tab);
1094 }
1095
Michael Kolb736990c2011-03-20 10:01:20 -07001096 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001097 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001098 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1099 }
1100
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001101 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001102 WebChromeClient.CustomViewCallback callback) {
1103 if (tab.inForeground()) {
1104 if (mUi.isCustomViewShowing()) {
1105 callback.onCustomViewHidden();
1106 return;
1107 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001108 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001109 // Save the menu state and set it to empty while the custom
1110 // view is showing.
1111 mOldMenuState = mMenuState;
1112 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001113 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001114 }
1115 }
1116
1117 @Override
1118 public void hideCustomView() {
1119 if (mUi.isCustomViewShowing()) {
1120 mUi.onHideCustomView();
1121 // Reset the old menu state.
1122 mMenuState = mOldMenuState;
1123 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001124 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001125 }
1126 }
1127
1128 protected void onActivityResult(int requestCode, int resultCode,
1129 Intent intent) {
1130 if (getCurrentTopWebView() == null) return;
1131 switch (requestCode) {
1132 case PREFERENCES_PAGE:
1133 if (resultCode == Activity.RESULT_OK && intent != null) {
1134 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001135 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001136 mTabControl.removeParentChildRelationShips();
1137 }
1138 }
1139 break;
1140 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001141 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001142 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001143 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001144 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001145 case AUTOFILL_SETUP:
1146 // Determine whether a profile was actually set up or not
1147 // and if so, send the message back to the WebTextView to
1148 // fill the form with the new profile.
1149 if (getSettings().getAutoFillProfile() != null) {
1150 mAutoFillSetupMessage.sendToTarget();
1151 mAutoFillSetupMessage = null;
1152 }
1153 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001154 default:
1155 break;
1156 }
1157 getCurrentTopWebView().requestFocus();
1158 }
1159
1160 /**
1161 * Open the Go page.
1162 * @param startWithHistory If true, open starting on the history tab.
1163 * Otherwise, start with the bookmarks tab.
1164 */
1165 @Override
1166 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1167 if (mTabControl.getCurrentWebView() == null) {
1168 return;
1169 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001170 // clear action mode
1171 if (isInCustomActionMode()) {
1172 endActionMode();
1173 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001174 Bundle extras = new Bundle();
1175 // Disable opening in a new window if we have maxed out the windows
1176 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1177 !mTabControl.canCreateNewTab());
1178 mUi.showComboView(startWithHistory, extras);
1179 }
1180
1181 // combo view callbacks
1182
1183 /**
1184 * callback from ComboPage when clear history is requested
1185 */
1186 public void onRemoveParentChildRelationships() {
1187 mTabControl.removeParentChildRelationShips();
1188 }
1189
1190 /**
1191 * callback from ComboPage when bookmark/history selection
1192 */
1193 @Override
1194 public void onUrlSelected(String url, boolean newTab) {
1195 removeComboView();
1196 if (!TextUtils.isEmpty(url)) {
1197 if (newTab) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07001198 final Tab parent = mTabControl.getCurrentTab();
1199 openTab(url,
1200 (parent != null) && parent.isPrivateBrowsingEnabled(),
1201 !mSettings.openInBackground(),
1202 true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001203 } else {
1204 final Tab currentTab = mTabControl.getCurrentTab();
1205 dismissSubWindow(currentTab);
1206 loadUrl(getCurrentTopWebView(), url);
1207 }
1208 }
1209 }
1210
1211 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001212 * dismiss the ComboPage
1213 */
1214 @Override
1215 public void removeComboView() {
1216 mUi.hideComboView();
1217 }
1218
1219 // active tabs page handling
1220
1221 protected void showActiveTabsPage() {
1222 mMenuState = EMPTY_MENU;
1223 mUi.showActiveTabsPage();
1224 }
1225
1226 /**
1227 * Remove the active tabs page.
1228 * @param needToAttach If true, the active tabs page did not attach a tab
1229 * to the content view, so we need to do that here.
1230 */
1231 @Override
1232 public void removeActiveTabsPage(boolean needToAttach) {
1233 mMenuState = R.id.MAIN_MENU;
1234 mUi.removeActiveTabsPage();
1235 if (needToAttach) {
1236 setActiveTab(mTabControl.getCurrentTab());
1237 }
1238 getCurrentTopWebView().requestFocus();
1239 }
1240
1241 // key handling
1242 protected void onBackKey() {
1243 if (!mUi.onBackKey()) {
1244 WebView subwindow = mTabControl.getCurrentSubWindow();
1245 if (subwindow != null) {
1246 if (subwindow.canGoBack()) {
1247 subwindow.goBack();
1248 } else {
1249 dismissSubWindow(mTabControl.getCurrentTab());
1250 }
1251 } else {
1252 goBackOnePageOrQuit();
1253 }
1254 }
1255 }
1256
Michael Kolb4bd767d2011-05-27 11:33:55 -07001257 protected boolean onMenuKey() {
1258 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001259 }
1260
Michael Kolb8233fac2010-10-26 16:08:53 -07001261 // menu handling and state
1262 // TODO: maybe put into separate handler
1263
1264 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckb3417f02011-01-14 11:01:05 -08001265 if (mOptionsMenuHandler != null) {
1266 return mOptionsMenuHandler.onCreateOptionsMenu(menu);
1267 }
1268
John Reckd73c5a22010-12-22 10:22:50 -08001269 if (mMenuState == EMPTY_MENU) {
1270 return false;
1271 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001272 MenuInflater inflater = mActivity.getMenuInflater();
1273 inflater.inflate(R.menu.browser, menu);
1274 updateInLoadMenuItems(menu);
1275 // hold on to the menu reference here; it is used by the page callbacks
1276 // to update the menu based on loading state
1277 mCachedMenu = menu;
1278 return true;
1279 }
1280
1281 protected void onCreateContextMenu(ContextMenu menu, View v,
1282 ContextMenuInfo menuInfo) {
1283 if (v instanceof TitleBarBase) {
1284 return;
1285 }
1286 if (!(v instanceof WebView)) {
1287 return;
1288 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001289 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001290 WebView.HitTestResult result = webview.getHitTestResult();
1291 if (result == null) {
1292 return;
1293 }
1294
1295 int type = result.getType();
1296 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1297 Log.w(LOGTAG,
1298 "We should not show context menu when nothing is touched");
1299 return;
1300 }
1301 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1302 // let TextView handles context menu
1303 return;
1304 }
1305
1306 // Note, http://b/issue?id=1106666 is requesting that
1307 // an inflated menu can be used again. This is not available
1308 // yet, so inflate each time (yuk!)
1309 MenuInflater inflater = mActivity.getMenuInflater();
1310 inflater.inflate(R.menu.browsercontext, menu);
1311
1312 // Show the correct menu group
1313 final String extra = result.getExtra();
1314 menu.setGroupVisible(R.id.PHONE_MENU,
1315 type == WebView.HitTestResult.PHONE_TYPE);
1316 menu.setGroupVisible(R.id.EMAIL_MENU,
1317 type == WebView.HitTestResult.EMAIL_TYPE);
1318 menu.setGroupVisible(R.id.GEO_MENU,
1319 type == WebView.HitTestResult.GEO_TYPE);
1320 menu.setGroupVisible(R.id.IMAGE_MENU,
1321 type == WebView.HitTestResult.IMAGE_TYPE
1322 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1323 menu.setGroupVisible(R.id.ANCHOR_MENU,
1324 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1325 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001326 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1327 || type == WebView.HitTestResult.PHONE_TYPE
1328 || type == WebView.HitTestResult.EMAIL_TYPE
1329 || type == WebView.HitTestResult.GEO_TYPE;
1330 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1331 if (hitText) {
1332 menu.findItem(R.id.select_text_menu_id)
1333 .setOnMenuItemClickListener(new SelectText(webview));
1334 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001335 // Setup custom handling depending on the type
1336 switch (type) {
1337 case WebView.HitTestResult.PHONE_TYPE:
1338 menu.setHeaderTitle(Uri.decode(extra));
1339 menu.findItem(R.id.dial_context_menu_id).setIntent(
1340 new Intent(Intent.ACTION_VIEW, Uri
1341 .parse(WebView.SCHEME_TEL + extra)));
1342 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1343 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1344 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1345 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1346 addIntent);
1347 menu.findItem(R.id.copy_phone_context_menu_id)
1348 .setOnMenuItemClickListener(
1349 new Copy(extra));
1350 break;
1351
1352 case WebView.HitTestResult.EMAIL_TYPE:
1353 menu.setHeaderTitle(extra);
1354 menu.findItem(R.id.email_context_menu_id).setIntent(
1355 new Intent(Intent.ACTION_VIEW, Uri
1356 .parse(WebView.SCHEME_MAILTO + extra)));
1357 menu.findItem(R.id.copy_mail_context_menu_id)
1358 .setOnMenuItemClickListener(
1359 new Copy(extra));
1360 break;
1361
1362 case WebView.HitTestResult.GEO_TYPE:
1363 menu.setHeaderTitle(extra);
1364 menu.findItem(R.id.map_context_menu_id).setIntent(
1365 new Intent(Intent.ACTION_VIEW, Uri
1366 .parse(WebView.SCHEME_GEO
1367 + URLEncoder.encode(extra))));
1368 menu.findItem(R.id.copy_geo_context_menu_id)
1369 .setOnMenuItemClickListener(
1370 new Copy(extra));
1371 break;
1372
1373 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1374 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001375 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001376 // decide whether to show the open link in new tab option
1377 boolean showNewTab = mTabControl.canCreateNewTab();
1378 MenuItem newTabItem
1379 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001380 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001381 ? R.string.contextmenu_openlink_newwindow_background
1382 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001383 newTabItem.setVisible(showNewTab);
1384 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001385 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1386 newTabItem.setOnMenuItemClickListener(
1387 new MenuItem.OnMenuItemClickListener() {
1388 @Override
1389 public boolean onMenuItemClick(MenuItem item) {
1390 final HashMap<String, WebView> hrefMap =
1391 new HashMap<String, WebView>();
1392 hrefMap.put("webview", webview);
1393 final Message msg = mHandler.obtainMessage(
1394 FOCUS_NODE_HREF,
1395 R.id.open_newtab_context_menu_id,
1396 0, hrefMap);
1397 webview.requestFocusNodeHref(msg);
1398 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001399 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001400 });
1401 } else {
1402 newTabItem.setOnMenuItemClickListener(
1403 new MenuItem.OnMenuItemClickListener() {
1404 @Override
1405 public boolean onMenuItemClick(MenuItem item) {
1406 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001407 openTab(extra, parent,
1408 !mSettings.openInBackground(),
1409 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001410 return true;
1411 }
1412 });
1413 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001414 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001415 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1416 break;
1417 }
1418 // otherwise fall through to handle image part
1419 case WebView.HitTestResult.IMAGE_TYPE:
1420 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1421 menu.setHeaderTitle(extra);
1422 }
1423 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1424 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1425 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001426 setOnMenuItemClickListener(
1427 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001428 menu.findItem(R.id.set_wallpaper_context_menu_id).
1429 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1430 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001431 break;
1432
1433 default:
1434 Log.w(LOGTAG, "We should not get here.");
1435 break;
1436 }
1437 //update the ui
1438 mUi.onContextMenuCreated(menu);
1439 }
1440
1441 /**
1442 * As the menu can be open when loading state changes
1443 * we must manually update the state of the stop/reload menu
1444 * item
1445 */
1446 private void updateInLoadMenuItems(Menu menu) {
1447 if (menu == null) {
1448 return;
1449 }
1450 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1451 MenuItem src = mInLoad ?
1452 menu.findItem(R.id.stop_menu_id):
1453 menu.findItem(R.id.reload_menu_id);
1454 if (src != null) {
1455 dest.setIcon(src.getIcon());
1456 dest.setTitle(src.getTitle());
1457 }
1458 }
1459
John Reckb3417f02011-01-14 11:01:05 -08001460 boolean onPrepareOptionsMenu(Menu menu) {
1461 if (mOptionsMenuHandler != null) {
1462 return mOptionsMenuHandler.onPrepareOptionsMenu(menu);
1463 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001464 // Note: setVisible will decide whether an item is visible; while
1465 // setEnabled() will decide whether an item is enabled, which also means
1466 // whether the matching shortcut key will function.
1467 switch (mMenuState) {
1468 case EMPTY_MENU:
1469 if (mCurrentMenuState != mMenuState) {
1470 menu.setGroupVisible(R.id.MAIN_MENU, false);
1471 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1472 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1473 }
1474 break;
1475 default:
1476 if (mCurrentMenuState != mMenuState) {
1477 menu.setGroupVisible(R.id.MAIN_MENU, true);
1478 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1479 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1480 }
1481 final WebView w = getCurrentTopWebView();
1482 boolean canGoBack = false;
1483 boolean canGoForward = false;
1484 boolean isHome = false;
1485 if (w != null) {
1486 canGoBack = w.canGoBack();
1487 canGoForward = w.canGoForward();
1488 isHome = mSettings.getHomePage().equals(w.getUrl());
1489 }
1490 final MenuItem back = menu.findItem(R.id.back_menu_id);
1491 back.setEnabled(canGoBack);
1492
1493 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1494 home.setEnabled(!isHome);
1495
1496 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1497 forward.setEnabled(canGoForward);
1498
1499 // decide whether to show the share link option
1500 PackageManager pm = mActivity.getPackageManager();
1501 Intent send = new Intent(Intent.ACTION_SEND);
1502 send.setType("text/plain");
1503 ResolveInfo ri = pm.resolveActivity(send,
1504 PackageManager.MATCH_DEFAULT_ONLY);
1505 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1506
John Reck35e9dd62011-04-25 09:01:54 -07001507 boolean isNavDump = mSettings.enableNavDump();
Michael Kolb8233fac2010-10-26 16:08:53 -07001508 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1509 nav.setVisible(isNavDump);
1510 nav.setEnabled(isNavDump);
1511
John Reck35e9dd62011-04-25 09:01:54 -07001512 boolean showDebugSettings = mSettings.isDebugEnabled();
Michael Kolb8233fac2010-10-26 16:08:53 -07001513 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1514 counter.setVisible(showDebugSettings);
1515 counter.setEnabled(showDebugSettings);
1516
John Reckb3417f02011-01-14 11:01:05 -08001517 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
1518 newtab.setEnabled(getTabControl().canCreateNewTab());
Michael Kolb8233fac2010-10-26 16:08:53 -07001519
Leon Scroggins81983762011-02-11 15:17:36 -05001520 MenuItem archive = menu.findItem(R.id.save_webarchive_menu_id);
John Reck51d8bad2011-02-28 15:47:47 -08001521 Tab tab = getTabControl().getCurrentTab();
1522 String url = tab != null ? tab.getUrl() : null;
1523 archive.setVisible(!TextUtils.isEmpty(url)
1524 && !url.endsWith(".webarchivexml"));
Michael Kolb8233fac2010-10-26 16:08:53 -07001525 break;
1526 }
1527 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001528 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001529 }
1530
1531 public boolean onOptionsItemSelected(MenuItem item) {
John Reckb3417f02011-01-14 11:01:05 -08001532 if (mOptionsMenuHandler != null &&
1533 mOptionsMenuHandler.onOptionsItemSelected(item)) {
1534 return true;
1535 }
1536
Michael Kolb8233fac2010-10-26 16:08:53 -07001537 if (item.getGroupId() != R.id.CONTEXT_MENU) {
1538 // menu remains active, so ensure comboview is dismissed
1539 // if main menu option is selected
1540 removeComboView();
1541 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001542 if (null == getCurrentTopWebView()) {
1543 return false;
1544 }
1545 if (mMenuIsDown) {
1546 // The shortcut action consumes the MENU. Even if it is still down,
1547 // it won't trigger the next shortcut action. In the case of the
1548 // shortcut action triggering a new activity, like Bookmarks, we
1549 // won't get onKeyUp for MENU. So it is important to reset it here.
1550 mMenuIsDown = false;
1551 }
1552 switch (item.getItemId()) {
1553 // -- Main menu
1554 case R.id.new_tab_menu_id:
1555 openTabToHomePage();
1556 break;
1557
1558 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001559 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001560 break;
1561
1562 case R.id.goto_menu_id:
1563 editUrl();
1564 break;
1565
1566 case R.id.bookmarks_menu_id:
1567 bookmarksOrHistoryPicker(false);
1568 break;
1569
1570 case R.id.active_tabs_menu_id:
1571 showActiveTabsPage();
1572 break;
1573
1574 case R.id.add_bookmark_menu_id:
John Reck3ffc5ca2011-06-10 15:56:06 -07001575 bookmarkCurrentPage(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001576 break;
1577
1578 case R.id.stop_reload_menu_id:
1579 if (mInLoad) {
1580 stopLoading();
1581 } else {
1582 getCurrentTopWebView().reload();
1583 }
1584 break;
1585
1586 case R.id.back_menu_id:
1587 getCurrentTopWebView().goBack();
1588 break;
1589
1590 case R.id.forward_menu_id:
1591 getCurrentTopWebView().goForward();
1592 break;
1593
1594 case R.id.close_menu_id:
1595 // Close the subwindow if it exists.
1596 if (mTabControl.getCurrentSubWindow() != null) {
1597 dismissSubWindow(mTabControl.getCurrentTab());
1598 break;
1599 }
1600 closeCurrentTab();
1601 break;
1602
1603 case R.id.homepage_menu_id:
1604 Tab current = mTabControl.getCurrentTab();
1605 if (current != null) {
1606 dismissSubWindow(current);
1607 loadUrl(current.getWebView(), mSettings.getHomePage());
1608 }
1609 break;
1610
1611 case R.id.preferences_menu_id:
1612 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1613 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1614 getCurrentTopWebView().getUrl());
1615 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1616 break;
1617
1618 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001619 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001620 break;
1621
John Reckf33b1632011-06-04 20:00:23 -07001622 case R.id.freeze_tab_menu_id:
1623 // TODO: Show error messages
John Reck541f55a2011-06-07 16:34:43 -07001624 Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001625 if (source == null) break;
John Reckd8c74522011-06-14 08:45:00 -07001626 final ContentResolver cr = mActivity.getContentResolver();
1627 final ContentValues values = source.createSnapshotValues();
1628 new AsyncTask<Tab, Void, Long>() {
1629 @Override
1630 protected Long doInBackground(Tab... params) {
1631 Tab t = params[0];
1632 if (values == null) {
1633 return t.isSnapshot()
1634 ? ((SnapshotTab)t).getSnapshotId()
1635 : -1;
1636 }
1637 Uri result = cr.insert(Snapshots.CONTENT_URI, values);
1638 long id = ContentUris.parseId(result);
1639 return id;
1640 }
1641
1642 protected void onPostExecute(Long id) {
1643 if (id > 0) {
1644 createNewSnapshotTab(id, true);
1645 }
1646 };
1647 }.execute(source);
John Reckf33b1632011-06-04 20:00:23 -07001648 break;
1649
Leon Scrogginsac993842011-02-02 12:54:07 -05001650 case R.id.save_webarchive_menu_id:
1651 String state = Environment.getExternalStorageState();
1652 if (!Environment.MEDIA_MOUNTED.equals(state)) {
1653 Log.e(LOGTAG, "External storage not mounted");
1654 Toast.makeText(mActivity, R.string.webarchive_failed,
1655 Toast.LENGTH_SHORT).show();
1656 break;
1657 }
1658 final String directory = Environment.getExternalStoragePublicDirectory(
1659 Environment.DIRECTORY_DOWNLOADS) + File.separator;
1660 File dir = new File(directory);
1661 if (!dir.exists() && !dir.mkdirs()) {
1662 Log.e(LOGTAG, "Save as Web Archive: mkdirs for " + directory + " failed!");
1663 Toast.makeText(mActivity, R.string.webarchive_failed,
1664 Toast.LENGTH_SHORT).show();
1665 break;
1666 }
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001667 final WebView topWebView = getCurrentTopWebView();
Leon Scrogginsac993842011-02-02 12:54:07 -05001668 final String title = topWebView.getTitle();
John Reck51d8bad2011-02-28 15:47:47 -08001669 final String url = topWebView.getUrl();
Leon Scrogginsac993842011-02-02 12:54:07 -05001670 topWebView.saveWebArchive(directory, true,
1671 new ValueCallback<String>() {
1672 @Override
1673 public void onReceiveValue(final String value) {
1674 if (value != null) {
1675 File file = new File(value);
1676 final long length = file.length();
1677 if (file.exists() && length > 0) {
Leon Scroggins1cb96552011-02-11 14:22:57 -05001678 Toast.makeText(mActivity, R.string.webarchive_saved,
1679 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001680 final DownloadManager manager = (DownloadManager) mActivity
1681 .getSystemService(Context.DOWNLOAD_SERVICE);
1682 new Thread("Add WebArchive to download manager") {
1683 @Override
1684 public void run() {
Vasu Noria9e30a72011-03-07 11:37:34 -08001685 manager.addCompletedDownload(
1686 null == title ? value : title,
Leon Scrogginsac993842011-02-02 12:54:07 -05001687 value, true, "application/x-webarchive-xml",
1688 value, length, true);
1689 }
1690 }.start();
1691 return;
1692 }
1693 }
John Reck51d8bad2011-02-28 15:47:47 -08001694 DownloadHandler.onDownloadStartNoStream(mActivity,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001695 url, null, null, null, topWebView.isPrivateBrowsingEnabled());
Leon Scrogginsac993842011-02-02 12:54:07 -05001696 }
1697 });
1698 break;
1699
Michael Kolb8233fac2010-10-26 16:08:53 -07001700 case R.id.page_info_menu_id:
1701 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(),
1702 false);
1703 break;
1704
1705 case R.id.classic_history_menu_id:
1706 bookmarksOrHistoryPicker(true);
1707 break;
1708
1709 case R.id.title_bar_share_page_url:
1710 case R.id.share_page_menu_id:
1711 Tab currentTab = mTabControl.getCurrentTab();
1712 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001713 return false;
1714 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001715 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001716 break;
1717
1718 case R.id.dump_nav_menu_id:
1719 getCurrentTopWebView().debugDump();
1720 break;
1721
1722 case R.id.dump_counters_menu_id:
1723 getCurrentTopWebView().dumpV8Counters();
1724 break;
1725
1726 case R.id.zoom_in_menu_id:
1727 getCurrentTopWebView().zoomIn();
1728 break;
1729
1730 case R.id.zoom_out_menu_id:
1731 getCurrentTopWebView().zoomOut();
1732 break;
1733
1734 case R.id.view_downloads_menu_id:
1735 viewDownloads();
1736 break;
1737
1738 case R.id.window_one_menu_id:
1739 case R.id.window_two_menu_id:
1740 case R.id.window_three_menu_id:
1741 case R.id.window_four_menu_id:
1742 case R.id.window_five_menu_id:
1743 case R.id.window_six_menu_id:
1744 case R.id.window_seven_menu_id:
1745 case R.id.window_eight_menu_id:
1746 {
1747 int menuid = item.getItemId();
1748 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1749 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1750 Tab desiredTab = mTabControl.getTab(id);
1751 if (desiredTab != null &&
1752 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001753 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001754 }
1755 break;
1756 }
1757 }
1758 }
1759 break;
1760
1761 default:
1762 return false;
1763 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001764 return true;
1765 }
1766
1767 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001768 // Let the History and Bookmark fragments handle menus they created.
1769 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1770 return false;
1771 }
1772
Michael Kolb8233fac2010-10-26 16:08:53 -07001773 int id = item.getItemId();
1774 boolean result = true;
1775 switch (id) {
1776 // For the context menu from the title bar
1777 case R.id.title_bar_copy_page_url:
1778 Tab currentTab = mTabControl.getCurrentTab();
1779 if (null == currentTab) {
1780 result = false;
1781 break;
1782 }
1783 WebView mainView = currentTab.getWebView();
1784 if (null == mainView) {
1785 result = false;
1786 break;
1787 }
1788 copy(mainView.getUrl());
1789 break;
1790 // -- Browser context menu
1791 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001792 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001793 case R.id.copy_link_context_menu_id:
1794 final WebView webView = getCurrentTopWebView();
1795 if (null == webView) {
1796 result = false;
1797 break;
1798 }
1799 final HashMap<String, WebView> hrefMap =
1800 new HashMap<String, WebView>();
1801 hrefMap.put("webview", webView);
1802 final Message msg = mHandler.obtainMessage(
1803 FOCUS_NODE_HREF, id, 0, hrefMap);
1804 webView.requestFocusNodeHref(msg);
1805 break;
1806
1807 default:
1808 // For other context menus
1809 result = onOptionsItemSelected(item);
1810 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001811 return result;
1812 }
1813
1814 /**
1815 * support programmatically opening the context menu
1816 */
1817 public void openContextMenu(View view) {
1818 mActivity.openContextMenu(view);
1819 }
1820
1821 /**
1822 * programmatically open the options menu
1823 */
1824 public void openOptionsMenu() {
1825 mActivity.openOptionsMenu();
1826 }
1827
1828 public boolean onMenuOpened(int featureId, Menu menu) {
1829 if (mOptionsMenuOpen) {
1830 if (mConfigChanged) {
1831 // We do not need to make any changes to the state of the
1832 // title bar, since the only thing that happened was a
1833 // change in orientation
1834 mConfigChanged = false;
1835 } else {
1836 if (!mExtendedMenuOpen) {
1837 mExtendedMenuOpen = true;
1838 mUi.onExtendedMenuOpened();
1839 } else {
1840 // Switching the menu back to icon view, so show the
1841 // title bar once again.
1842 mExtendedMenuOpen = false;
1843 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001844 }
1845 }
1846 } else {
1847 // The options menu is closed, so open it, and show the title
1848 mOptionsMenuOpen = true;
1849 mConfigChanged = false;
1850 mExtendedMenuOpen = false;
1851 mUi.onOptionsMenuOpened();
1852 }
1853 return true;
1854 }
1855
1856 public void onOptionsMenuClosed(Menu menu) {
1857 mOptionsMenuOpen = false;
1858 mUi.onOptionsMenuClosed(mInLoad);
1859 }
1860
1861 public void onContextMenuClosed(Menu menu) {
1862 mUi.onContextMenuClosed(menu, mInLoad);
1863 }
1864
1865 // Helper method for getting the top window.
1866 @Override
1867 public WebView getCurrentTopWebView() {
1868 return mTabControl.getCurrentTopWebView();
1869 }
1870
1871 @Override
1872 public WebView getCurrentWebView() {
1873 return mTabControl.getCurrentWebView();
1874 }
1875
1876 /*
1877 * This method is called as a result of the user selecting the options
1878 * menu to see the download window. It shows the download window on top of
1879 * the current window.
1880 */
1881 void viewDownloads() {
1882 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1883 mActivity.startActivity(intent);
1884 }
1885
1886 // action mode
1887
1888 void onActionModeStarted(ActionMode mode) {
1889 mUi.onActionModeStarted(mode);
1890 mActionMode = mode;
1891 }
1892
1893 /*
1894 * True if a custom ActionMode (i.e. find or select) is in use.
1895 */
1896 @Override
1897 public boolean isInCustomActionMode() {
1898 return mActionMode != null;
1899 }
1900
1901 /*
1902 * End the current ActionMode.
1903 */
1904 @Override
1905 public void endActionMode() {
1906 if (mActionMode != null) {
1907 mActionMode.finish();
1908 }
1909 }
1910
1911 /*
1912 * Called by find and select when they are finished. Replace title bars
1913 * as necessary.
1914 */
1915 public void onActionModeFinished(ActionMode mode) {
1916 if (!isInCustomActionMode()) return;
1917 mUi.onActionModeFinished(mInLoad);
1918 mActionMode = null;
1919 }
1920
1921 boolean isInLoad() {
1922 return mInLoad;
1923 }
1924
1925 // bookmark handling
1926
1927 /**
1928 * add the current page as a bookmark to the given folder id
1929 * @param folderId use -1 for the default folder
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001930 * @param canBeAnEdit If true, check to see whether the site is already
1931 * bookmarked, and if it is, edit that bookmark. If false, and
1932 * the site is already bookmarked, do not attempt to edit the
1933 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001934 */
1935 @Override
John Reck3ffc5ca2011-06-10 15:56:06 -07001936 public void bookmarkCurrentPage(boolean canBeAnEdit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001937 Intent i = new Intent(mActivity,
1938 AddBookmarkPage.class);
1939 WebView w = getCurrentTopWebView();
1940 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1941 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1942 String touchIconUrl = w.getTouchIconUrl();
1943 if (touchIconUrl != null) {
1944 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1945 WebSettings settings = w.getSettings();
1946 if (settings != null) {
1947 i.putExtra(AddBookmarkPage.USER_AGENT,
1948 settings.getUserAgentString());
1949 }
1950 }
1951 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1952 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1953 getDesiredThumbnailHeight(mActivity)));
1954 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001955 if (canBeAnEdit) {
1956 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1957 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001958 // Put the dialog at the upper right of the screen, covering the
1959 // star on the title bar.
1960 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
1961 mActivity.startActivity(i);
1962 }
1963
1964 // file chooser
1965 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1966 mUploadHandler = new UploadHandler(this);
1967 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1968 }
1969
1970 // thumbnails
1971
1972 /**
1973 * Return the desired width for thumbnail screenshots, which are stored in
1974 * the database, and used on the bookmarks screen.
1975 * @param context Context for finding out the density of the screen.
1976 * @return desired width for thumbnail screenshot.
1977 */
1978 static int getDesiredThumbnailWidth(Context context) {
1979 return context.getResources().getDimensionPixelOffset(
1980 R.dimen.bookmarkThumbnailWidth);
1981 }
1982
1983 /**
1984 * Return the desired height for thumbnail screenshots, which are stored in
1985 * the database, and used on the bookmarks screen.
1986 * @param context Context for finding out the density of the screen.
1987 * @return desired height for thumbnail screenshot.
1988 */
1989 static int getDesiredThumbnailHeight(Context context) {
1990 return context.getResources().getDimensionPixelOffset(
1991 R.dimen.bookmarkThumbnailHeight);
1992 }
1993
Michael Kolb1acef692011-03-08 14:12:06 -08001994 static Bitmap createScreenshot(Tab tab, int width, int height) {
1995 if ((tab != null) && (tab.getWebView() != null)) {
1996 return createScreenshot(tab.getWebView(), width, height);
1997 }
1998 return null;
1999 }
2000
Michael Kolb8233fac2010-10-26 16:08:53 -07002001 private static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08002002 // We render to a bitmap 2x the desired size so that we can then
2003 // re-scale it with filtering since canvas.scale doesn't filter
2004 // This helps reduce aliasing at the cost of being slightly blurry
2005 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07002006 Picture thumbnail = view.capturePicture();
2007 if (thumbnail == null) {
2008 return null;
2009 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002010 width *= filter_scale;
2011 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07002012 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
2013 Canvas canvas = new Canvas(bm);
2014 // May need to tweak these values to determine what is the
2015 // best scale factor
2016 int thumbnailWidth = thumbnail.getWidth();
2017 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08002018 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08002019 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08002020 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07002021 } else {
2022 return null;
2023 }
John Reckfe49ab42010-11-16 17:09:37 -08002024
Michael Kolbeb95db42011-03-03 10:38:40 -08002025 float scaleFactorY = (float) height / (float)thumbnailHeight;
2026 if (scaleFactorY > scaleFactor) {
2027 // The picture is narrower than the requested AR
2028 // Center the thumnail and crop the sides
2029 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08002030 float wx = (thumbnailWidth * scaleFactor) - width;
2031 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07002032 }
2033
John Reckfe49ab42010-11-16 17:09:37 -08002034 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07002035
2036 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08002037 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
2038 height / filter_scale, true);
2039 bm.recycle();
2040 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002041 }
2042
John Reck34ef2672011-02-10 11:30:55 -08002043 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002044 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002045 // FIXME: Would like to make sure there is actually something to
2046 // draw, but the API for that (WebViewCore.pictureReady()) is not
2047 // currently accessible here.
2048
John Reck34ef2672011-02-10 11:30:55 -08002049 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002050 if (view == null) {
2051 // Tab was destroyed
2052 return;
2053 }
John Reck34ef2672011-02-10 11:30:55 -08002054 final String url = tab.getUrl();
2055 final String originalUrl = view.getOriginalUrl();
2056
2057 if (TextUtils.isEmpty(url)) {
2058 return;
2059 }
2060
2061 // Only update thumbnails for web urls (http(s)://), not for
2062 // about:, javascript:, data:, etc...
2063 // Unless it is a bookmarked site, then always update
2064 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2065 return;
2066 }
2067
Michael Kolb8233fac2010-10-26 16:08:53 -07002068 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2069 getDesiredThumbnailHeight(mActivity));
2070 if (bm == null) {
2071 return;
2072 }
2073
2074 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002075 new AsyncTask<Void, Void, Void>() {
2076 @Override
2077 protected Void doInBackground(Void... unused) {
2078 Cursor cursor = null;
2079 try {
2080 // TODO: Clean this up
2081 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2082 if (cursor != null && cursor.moveToFirst()) {
2083 final ByteArrayOutputStream os =
2084 new ByteArrayOutputStream();
2085 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002086
John Reck34ef2672011-02-10 11:30:55 -08002087 ContentValues values = new ContentValues();
2088 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002089
John Reck34ef2672011-02-10 11:30:55 -08002090 do {
John Reck617fd832011-02-16 14:35:59 -08002091 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002092 cr.update(Images.CONTENT_URI, values, null, null);
2093 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002094 }
John Reck34ef2672011-02-10 11:30:55 -08002095 } catch (IllegalStateException e) {
2096 // Ignore
2097 } finally {
2098 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002099 }
John Reck34ef2672011-02-10 11:30:55 -08002100 return null;
2101 }
2102 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002103 }
2104
2105 private class Copy implements OnMenuItemClickListener {
2106 private CharSequence mText;
2107
2108 public boolean onMenuItemClick(MenuItem item) {
2109 copy(mText);
2110 return true;
2111 }
2112
2113 public Copy(CharSequence toCopy) {
2114 mText = toCopy;
2115 }
2116 }
2117
Leon Scroggins63c02662010-11-18 15:16:27 -05002118 private static class Download implements OnMenuItemClickListener {
2119 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002120 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002121 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002122
2123 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002124 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002125 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002126 return true;
2127 }
2128
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002129 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002130 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002131 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002132 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002133 }
2134 }
2135
Cary Clark8974d282010-11-22 10:46:05 -05002136 private static class SelectText implements OnMenuItemClickListener {
2137 private WebView mWebView;
2138
2139 public boolean onMenuItemClick(MenuItem item) {
2140 if (mWebView != null) {
2141 return mWebView.selectText();
2142 }
2143 return false;
2144 }
2145
2146 public SelectText(WebView webView) {
2147 mWebView = webView;
2148 }
2149
2150 }
2151
Michael Kolb8233fac2010-10-26 16:08:53 -07002152 /********************** TODO: UI stuff *****************************/
2153
2154 // these methods have been copied, they still need to be cleaned up
2155
2156 /****************** tabs ***************************************************/
2157
2158 // basic tab interactions:
2159
2160 // it is assumed that tabcontrol already knows about the tab
2161 protected void addTab(Tab tab) {
2162 mUi.addTab(tab);
2163 }
2164
2165 protected void removeTab(Tab tab) {
2166 mUi.removeTab(tab);
2167 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002168 mCrashRecoveryHandler.backupState();
John Reckd8c74522011-06-14 08:45:00 -07002169 if (tab.isSnapshot()) {
2170 SnapshotTab st = (SnapshotTab) tab;
2171 final Uri uri = ContentUris.withAppendedId(
2172 Snapshots.CONTENT_URI, st.getSnapshotId());
2173 final ContentResolver cr = mActivity.getContentResolver();
2174 new Thread() {
2175 @Override
2176 public void run() {
2177 cr.delete(uri, null, null);
2178 }
2179 }.start();
2180 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002181 }
2182
Michael Kolbf2055602011-04-09 17:20:03 -07002183 @Override
2184 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002185 // monkey protection against delayed start
2186 if (tab != null) {
2187 mTabControl.setCurrentTab(tab);
2188 // the tab is guaranteed to have a webview after setCurrentTab
2189 mUi.setActiveTab(tab);
2190 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002191 }
2192
2193 protected void closeEmptyChildTab() {
2194 Tab current = mTabControl.getCurrentTab();
2195 if (current != null
2196 && current.getWebView().copyBackForwardList().getSize() == 0) {
Michael Kolbc831b632011-05-11 09:30:34 -07002197 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002198 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002199 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002200 closeTab(current);
2201 }
2202 }
2203 }
2204
2205 protected void reuseTab(Tab appTab, String appId, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002206 // Dismiss the subwindow if applicable.
2207 dismissSubWindow(appTab);
2208 // Since we might kill the WebView, remove it from the
2209 // content view first.
2210 mUi.detachTab(appTab);
2211 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002212 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002213 // TODO: analyze why the remove and add are necessary
2214 mUi.attachTab(appTab);
2215 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002216 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002217 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002218 } else {
2219 // If the tab was the current tab, we have to attach
2220 // it to the view system again.
2221 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002222 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002223 }
2224 }
2225
2226 // Remove the sub window if it exists. Also called by TabControl when the
2227 // user clicks the 'X' to dismiss a sub window.
2228 public void dismissSubWindow(Tab tab) {
2229 removeSubWindow(tab);
2230 // dismiss the subwindow. This will destroy the WebView.
2231 tab.dismissSubWindow();
2232 getCurrentTopWebView().requestFocus();
2233 }
2234
2235 @Override
2236 public void removeSubWindow(Tab t) {
2237 if (t.getSubWebView() != null) {
2238 mUi.removeSubWindow(t.getSubViewContainer());
2239 }
2240 }
2241
2242 @Override
2243 public void attachSubWindow(Tab tab) {
2244 if (tab.getSubWebView() != null) {
2245 mUi.attachSubWindow(tab.getSubViewContainer());
2246 getCurrentTopWebView().requestFocus();
2247 }
2248 }
2249
Michael Kolb7bcafde2011-05-09 13:55:59 -07002250 // open a non inconito tab with the given url data
2251 // and set as active tab
2252 public Tab openTab(UrlData urlData) {
2253 Tab tab = createNewTab(false, true, true);
2254 if ((tab != null) && !urlData.isEmpty()) {
2255 loadUrlDataIn(tab, urlData);
2256 }
2257 return tab;
2258 }
2259
Michael Kolb843510f2010-12-09 10:51:49 -08002260 @Override
2261 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002262 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002263 }
2264
Michael Kolb8233fac2010-10-26 16:08:53 -07002265 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002266 public Tab openIncognitoTab() {
2267 return openTab(INCOGNITO_URI, true, true, false);
2268 }
2269
2270 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002271 public Tab openTab(String url, boolean incognito, boolean setActive,
2272 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002273 return openTab(url, incognito, setActive, useCurrent, null);
2274 }
2275
2276 @Override
2277 public Tab openTab(String url, Tab parent, boolean setActive,
2278 boolean useCurrent) {
2279 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2280 setActive, useCurrent, parent);
2281 }
2282
2283 public Tab openTab(String url, boolean incognito, boolean setActive,
2284 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002285 Tab tab = createNewTab(incognito, setActive, useCurrent);
2286 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002287 if (parent != null && parent != tab) {
2288 parent.addChildTab(tab);
2289 }
Michael Kolba4261fd2011-05-05 11:27:37 -07002290 WebView w = tab.getWebView();
Michael Kolb519d2282011-05-09 17:03:19 -07002291 if (url != null) {
2292 loadUrl(w, url);
2293 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002294 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002295 return tab;
2296 }
2297
2298 // this method will attempt to create a new tab
2299 // incognito: private browsing tab
2300 // setActive: ste tab as current tab
2301 // useCurrent: if no new tab can be created, return current tab
2302 private Tab createNewTab(boolean incognito, boolean setActive,
2303 boolean useCurrent) {
2304 Tab tab = null;
2305 if (mTabControl.canCreateNewTab()) {
2306 tab = mTabControl.createNewTab(incognito);
2307 addTab(tab);
2308 if (setActive) {
2309 setActiveTab(tab);
2310 }
2311 } else {
2312 if (useCurrent) {
2313 tab = mTabControl.getCurrentTab();
2314 // Get rid of the subwindow if it exists
2315 dismissSubWindow(tab);
2316 } else {
2317 mUi.showMaxTabsWarning();
2318 }
2319 }
2320 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002321 }
2322
John Reckd8c74522011-06-14 08:45:00 -07002323 private SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2324 SnapshotTab tab = mTabControl.createSnapshotTab(snapshotId);
2325 addTab(tab);
2326 if (setActive) {
2327 setActiveTab(tab);
2328 }
2329 return tab;
2330 }
2331
Michael Kolb8233fac2010-10-26 16:08:53 -07002332 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002333 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002334 * @return boolean True if we successfully switched to a different tab. If
2335 * the indexth tab is null, or if that tab is the same as
2336 * the current one, return false.
2337 */
2338 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002339 public boolean switchToTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002340 // hide combo view if open
2341 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002342 Tab currentTab = mTabControl.getCurrentTab();
2343 if (tab == null || tab == currentTab) {
2344 return false;
2345 }
2346 setActiveTab(tab);
2347 return true;
2348 }
2349
2350 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002351 public void closeCurrentTab() {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002352 // hide combo view if open
2353 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002354 if (mTabControl.getTabCount() == 1) {
John Reck958b2422010-12-03 17:56:17 -08002355 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002356 return;
2357 }
Michael Kolbc831b632011-05-11 09:30:34 -07002358 final Tab current = mTabControl.getCurrentTab();
2359 final int pos = mTabControl.getCurrentPosition();
2360 Tab newTab = current.getParent();
2361 if (newTab == null) {
2362 newTab = mTabControl.getTab(pos + 1);
2363 if (newTab == null) {
2364 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002365 }
2366 }
Michael Kolbc831b632011-05-11 09:30:34 -07002367 if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002368 // Close window
2369 closeTab(current);
2370 }
2371 }
2372
2373 /**
2374 * Close the tab, remove its associated title bar, and adjust mTabControl's
2375 * current tab to a valid value.
2376 */
2377 @Override
2378 public void closeTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002379 // hide combo view if open
2380 removeComboView();
Michael Kolb2d59c322011-01-25 13:18:55 -08002381 removeTab(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002382 }
2383
2384 /**************** TODO: Url loading clean up *******************************/
2385
2386 // Called when loading from context menu or LOAD_URL message
2387 protected void loadUrlFromContext(WebView view, String url) {
2388 // In case the user enters nothing.
2389 if (url != null && url.length() != 0 && view != null) {
2390 url = UrlUtils.smartUrlFilter(url);
2391 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
2392 loadUrl(view, url);
2393 }
2394 }
2395 }
2396
2397 /**
2398 * Load the URL into the given WebView and update the title bar
2399 * to reflect the new load. Call this instead of WebView.loadUrl
2400 * directly.
2401 * @param view The WebView used to load url.
2402 * @param url The URL to load.
2403 */
2404 protected void loadUrl(WebView view, String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002405 view.loadUrl(url);
2406 }
2407
2408 /**
2409 * Load UrlData into a Tab and update the title bar to reflect the new
2410 * load. Call this instead of UrlData.loadIn directly.
2411 * @param t The Tab used to load.
2412 * @param data The UrlData being loaded.
2413 */
2414 protected void loadUrlDataIn(Tab t, UrlData data) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002415 data.loadIn(t);
2416 }
2417
John Reck30c714c2010-12-16 17:30:34 -08002418 @Override
2419 public void onUserCanceledSsl(Tab tab) {
2420 WebView web = tab.getWebView();
2421 // TODO: Figure out the "right" behavior
2422 if (web.canGoBack()) {
2423 web.goBack();
2424 } else {
2425 web.loadUrl(mSettings.getHomePage());
2426 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002427 }
2428
2429 void goBackOnePageOrQuit() {
2430 Tab current = mTabControl.getCurrentTab();
2431 if (current == null) {
2432 /*
2433 * Instead of finishing the activity, simply push this to the back
2434 * of the stack and let ActivityManager to choose the foreground
2435 * activity. As BrowserActivity is singleTask, it will be always the
2436 * root of the task. So we can use either true or false for
2437 * moveTaskToBack().
2438 */
2439 mActivity.moveTaskToBack(true);
2440 return;
2441 }
2442 WebView w = current.getWebView();
2443 if (w.canGoBack()) {
2444 w.goBack();
2445 } else {
2446 // Check to see if we are closing a window that was created by
2447 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002448 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002449 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002450 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002451 // Now we close the other tab
2452 closeTab(current);
2453 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002454 /*
2455 * Instead of finishing the activity, simply push this to the back
2456 * of the stack and let ActivityManager to choose the foreground
2457 * activity. As BrowserActivity is singleTask, it will be always the
2458 * root of the task. So we can use either true or false for
2459 * moveTaskToBack().
2460 */
2461 mActivity.moveTaskToBack(true);
2462 }
2463 }
2464 }
2465
2466 /**
2467 * Feed the previously stored results strings to the BrowserProvider so that
2468 * the SearchDialog will show them instead of the standard searches.
2469 * @param result String to show on the editable line of the SearchDialog.
2470 */
2471 @Override
2472 public void showVoiceSearchResults(String result) {
2473 ContentProviderClient client = mActivity.getContentResolver()
2474 .acquireContentProviderClient(Browser.BOOKMARKS_URI);
2475 ContentProvider prov = client.getLocalContentProvider();
2476 BrowserProvider bp = (BrowserProvider) prov;
2477 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
2478 client.release();
2479
2480 Bundle bundle = createGoogleSearchSourceBundle(
2481 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
2482 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
2483 startSearch(result, false, bundle, false);
2484 }
2485
2486 private void startSearch(String initialQuery, boolean selectInitialQuery,
2487 Bundle appSearchData, boolean globalSearch) {
2488 if (appSearchData == null) {
2489 appSearchData = createGoogleSearchSourceBundle(
2490 GOOGLE_SEARCH_SOURCE_TYPE);
2491 }
2492
2493 SearchEngine searchEngine = mSettings.getSearchEngine();
2494 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
2495 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
2496 }
2497 mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData,
2498 globalSearch);
2499 }
2500
2501 private Bundle createGoogleSearchSourceBundle(String source) {
2502 Bundle bundle = new Bundle();
2503 bundle.putString(Search.SOURCE, source);
2504 return bundle;
2505 }
2506
2507 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002508 * helper method for key handler
2509 * returns the current tab if it can't advance
2510 */
Michael Kolbc831b632011-05-11 09:30:34 -07002511 private Tab getNextTab() {
2512 return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1,
2513 mTabControl.getCurrentPosition() + 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002514 }
2515
2516 /**
2517 * helper method for key handler
2518 * returns the current tab if it can't advance
2519 */
Michael Kolbc831b632011-05-11 09:30:34 -07002520 private Tab getPrevTab() {
2521 return mTabControl.getTab(Math.max(0,
2522 mTabControl.getCurrentPosition() - 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002523 }
2524
2525 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002526 * handle key events in browser
2527 *
2528 * @param keyCode
2529 * @param event
2530 * @return true if handled, false to pass to super
2531 */
2532 boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002533 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002534 // Even if MENU is already held down, we need to call to super to open
2535 // the IME on long press.
Michael Kolb2814a362011-05-19 15:49:41 -07002536 if (KeyEvent.KEYCODE_MENU == keyCode) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002537 if (mOptionsMenuHandler != null) {
2538 return false;
2539 } else {
2540 event.startTracking();
2541 return true;
2542 }
Michael Kolb2814a362011-05-19 15:49:41 -07002543 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002544 if (!noModifiers
2545 && ((KeyEvent.KEYCODE_MENU == keyCode)
2546 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2547 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002548 mMenuIsDown = true;
2549 return false;
2550 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002551
Cary Clark8ff8c662010-12-29 15:03:05 -05002552 WebView webView = getCurrentTopWebView();
2553 if (webView == null) return false;
2554
Cary Clark160bbb92011-01-10 11:17:07 -05002555 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2556 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002557
Michael Kolb8233fac2010-10-26 16:08:53 -07002558 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002559 case KeyEvent.KEYCODE_TAB:
2560 if (event.isCtrlPressed()) {
2561 if (event.isShiftPressed()) {
2562 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002563 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002564 } else {
2565 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002566 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002567 }
2568 return true;
2569 }
2570 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002571 case KeyEvent.KEYCODE_SPACE:
2572 // WebView/WebTextView handle the keys in the KeyDown. As
2573 // the Activity's shortcut keys are only handled when WebView
2574 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002575 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002576 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002577 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002578 pageDown();
2579 }
2580 return true;
2581 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002582 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002583 event.startTracking();
2584 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002585 case KeyEvent.KEYCODE_DPAD_LEFT:
2586 if (ctrl) {
2587 webView.goBack();
2588 return true;
2589 }
2590 break;
2591 case KeyEvent.KEYCODE_DPAD_RIGHT:
2592 if (ctrl) {
2593 webView.goForward();
2594 return true;
2595 }
2596 break;
2597 case KeyEvent.KEYCODE_A:
2598 if (ctrl) {
2599 webView.selectAll();
2600 return true;
2601 }
2602 break;
Michael Kolba4183062011-01-16 10:43:21 -08002603// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002604 case KeyEvent.KEYCODE_C:
2605 if (ctrl) {
2606 webView.copySelection();
2607 return true;
2608 }
2609 break;
Michael Kolba4183062011-01-16 10:43:21 -08002610// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002611// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002612// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002613// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002614// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002615// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002616// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002617// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002618// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002619// case KeyEvent.KEYCODE_M: // unused
2620// case KeyEvent.KEYCODE_N: // in Chrome: new window
2621// case KeyEvent.KEYCODE_O: // in Chrome: open file
2622// case KeyEvent.KEYCODE_P: // in Chrome: print page
2623// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002624// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002625// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2626 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002627 // we can't use the ctrl/shift flags, they check for
2628 // exclusive use of a modifier
2629 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002630 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002631 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002632 } else {
2633 openTabToHomePage();
2634 }
2635 return true;
2636 }
2637 break;
2638// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2639// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb1a2eba42011-03-16 16:42:49 -07002640 case KeyEvent.KEYCODE_W: // in Chrome: close tab
2641 if (ctrl) {
2642 closeCurrentTab();
2643 return true;
2644 }
2645 break;
Cary Clark8ff8c662010-12-29 15:03:05 -05002646// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2647// case KeyEvent.KEYCODE_Y: // unused
2648// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002649 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002650 // it is a regular key and webview is not null
2651 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002652 }
2653
John Recke6bf4ab2011-02-24 15:48:05 -08002654 boolean onKeyLongPress(int keyCode, KeyEvent event) {
2655 switch(keyCode) {
2656 case KeyEvent.KEYCODE_BACK:
2657 if (mUi.showsWeb()) {
2658 bookmarksOrHistoryPicker(true);
2659 return true;
2660 }
2661 break;
2662 }
2663 return false;
2664 }
2665
Michael Kolb8233fac2010-10-26 16:08:53 -07002666 boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb2814a362011-05-19 15:49:41 -07002667 if (KeyEvent.KEYCODE_MENU == keyCode) {
2668 mMenuIsDown = false;
2669 if (event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002670 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002671 }
2672 }
Cary Clark160bbb92011-01-10 11:17:07 -05002673 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002674 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002675 case KeyEvent.KEYCODE_BACK:
2676 if (event.isTracking() && !event.isCanceled()) {
2677 onBackKey();
2678 return true;
2679 }
2680 break;
2681 }
2682 return false;
2683 }
2684
2685 public boolean isMenuDown() {
2686 return mMenuIsDown;
2687 }
2688
Ben Murdoch8029a772010-11-16 11:58:21 +00002689 public void setupAutoFill(Message message) {
2690 // Open the settings activity at the AutoFill profile fragment so that
2691 // the user can create a new profile. When they return, we will dispatch
2692 // the message so that we can autofill the form using their new profile.
2693 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2694 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2695 AutoFillSettingsFragment.class.getName());
2696 mAutoFillSetupMessage = message;
2697 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2698 }
John Reckb3417f02011-01-14 11:01:05 -08002699
2700 @Override
2701 public void registerOptionsMenuHandler(OptionsMenuHandler handler) {
2702 mOptionsMenuHandler = handler;
2703 }
2704
2705 @Override
2706 public void unregisterOptionsMenuHandler(OptionsMenuHandler handler) {
2707 if (mOptionsMenuHandler == handler) {
2708 mOptionsMenuHandler = null;
2709 }
2710 }
2711
Narayan Kamath5119edd2011-02-23 15:49:17 +00002712 @Override
2713 public void registerDropdownChangeListener(DropdownChangeListener d) {
2714 mUi.registerDropdownChangeListener(d);
2715 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002716}