blob: e3ca09a377ebdd694c57f7d354ba340656df56d1 [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);
John Reck24f18262011-06-17 14:47:20 -0700643 if (!outState.isEmpty()) {
644 // Save time so that we know how old incognito tabs (if any) are.
645 outState.putSerializable("lastActiveDate", Calendar.getInstance());
646 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700647 }
648
649 void onResume() {
650 if (!mActivityPaused) {
651 Log.e(LOGTAG, "BrowserActivity is already resumed.");
652 return;
653 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700654 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800655 Tab current = mTabControl.getCurrentTab();
656 if (current != null) {
657 current.resume();
658 resumeWebViewTimers(current);
659 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700660 if (mWakeLock.isHeld()) {
661 mHandler.removeMessages(RELEASE_WAKELOCK);
662 mWakeLock.release();
663 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200664
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 mUi.onResume();
666 mNetworkHandler.onResume();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200667 mNfcHandler.onResume();
Michael Kolb8233fac2010-10-26 16:08:53 -0700668 WebView.enablePlatformNotifications();
669 }
670
Michael Kolb70976932010-11-30 11:34:01 -0800671 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800672 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800673 * @param tab guaranteed non-null
674 */
675 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700676 boolean inLoad = tab.inPageLoad();
677 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
678 CookieSyncManager.getInstance().startSync();
679 WebView w = tab.getWebView();
680 if (w != null) {
681 w.resumeTimers();
682 }
683 }
684 }
685
Michael Kolb70976932010-11-30 11:34:01 -0800686 /**
687 * Pause all WebView timers using the WebView of the given tab
688 * @param tab
689 * @return true if the timers are paused or tab is null
690 */
691 private boolean pauseWebViewTimers(Tab tab) {
692 if (tab == null) {
693 return true;
694 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700695 CookieSyncManager.getInstance().stopSync();
696 WebView w = getCurrentWebView();
697 if (w != null) {
698 w.pauseTimers();
699 }
700 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700701 }
Michael Kolb70976932010-11-30 11:34:01 -0800702 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 }
704
705 void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800706 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700707 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
708 mUploadHandler = null;
709 }
710 if (mTabControl == null) return;
711 mUi.onDestroy();
712 // Remove the current tab and sub window
713 Tab t = mTabControl.getCurrentTab();
714 if (t != null) {
715 dismissSubWindow(t);
716 removeTab(t);
717 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500718 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700719 // Destroy all the tabs
720 mTabControl.destroy();
721 WebIconDatabase.getInstance().close();
722 // Stop watching the default geolocation permissions
723 mSystemAllowGeolocationOrigins.stop();
724 mSystemAllowGeolocationOrigins = null;
725 }
726
727 protected boolean isActivityPaused() {
728 return mActivityPaused;
729 }
730
731 protected void onLowMemory() {
732 mTabControl.freeMemory();
733 }
734
735 @Override
736 public boolean shouldShowErrorConsole() {
737 return mShouldShowErrorConsole;
738 }
739
740 protected void setShouldShowErrorConsole(boolean show) {
741 if (show == mShouldShowErrorConsole) {
742 // Nothing to do.
743 return;
744 }
745 mShouldShowErrorConsole = show;
746 Tab t = mTabControl.getCurrentTab();
747 if (t == null) {
748 // There is no current tab so we cannot toggle the error console
749 return;
750 }
751 mUi.setShouldShowErrorConsole(t, show);
752 }
753
754 @Override
755 public void stopLoading() {
756 mLoadStopped = true;
757 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700758 WebView w = getCurrentTopWebView();
759 w.stopLoading();
Michael Kolb8233fac2010-10-26 16:08:53 -0700760 mUi.onPageStopped(tab);
761 }
762
763 boolean didUserStopLoading() {
764 return mLoadStopped;
765 }
766
767 // WebViewController
768
769 @Override
John Reck324d4402011-01-11 16:56:42 -0800770 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700771
772 // We've started to load a new page. If there was a pending message
773 // to save a screenshot then we will now take the new page and save
774 // an incorrect screenshot. Therefore, remove any pending thumbnail
775 // messages from the queue.
776 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800777 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700778
779 // reset sync timer to avoid sync starts during loading a page
780 CookieSyncManager.getInstance().resetSync();
781
782 if (!mNetworkHandler.isNetworkUp()) {
783 view.setNetworkAvailable(false);
784 }
785
786 // when BrowserActivity just starts, onPageStarted may be called before
787 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
788 // to start the timer. As we won't switch tabs while an activity is in
789 // pause state, we can ensure calling resume and pause in pair.
790 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800791 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700792 }
793 mLoadStopped = false;
794 if (!mNetworkHandler.isNetworkUp()) {
795 mNetworkHandler.createAndShowNetworkDialog();
796 }
797 endActionMode();
798
John Reck30c714c2010-12-16 17:30:34 -0800799 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700800
John Reck324d4402011-01-11 16:56:42 -0800801 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700802 // update the bookmark database for favicon
803 maybeUpdateFavicon(tab, null, url, favicon);
804
805 Performance.tracePageStart(url);
806
807 // Performance probe
808 if (false) {
809 Performance.onPageStarted();
810 }
811
812 }
813
814 @Override
John Reck324d4402011-01-11 16:56:42 -0800815 public void onPageFinished(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800816 mUi.onTabDataChanged(tab);
John Reck324d4402011-01-11 16:56:42 -0800817 if (!tab.isPrivateBrowsingEnabled()
John Reckd8c74522011-06-14 08:45:00 -0700818 && !TextUtils.isEmpty(tab.getUrl())
819 && !tab.isSnapshot()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700820 if (tab.inForeground() && !didUserStopLoading()
821 || !tab.inForeground()) {
822 // Only update the bookmark screenshot if the user did not
823 // cancel the load early.
824 mHandler.sendMessageDelayed(mHandler.obtainMessage(
John Reck34ef2672011-02-10 11:30:55 -0800825 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Michael Kolb8233fac2010-10-26 16:08:53 -0700826 500);
827 }
828 }
829 // pause the WebView timer and release the wake lock if it is finished
830 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800831 if (mActivityPaused && pauseWebViewTimers(tab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700832 if (mWakeLock.isHeld()) {
833 mHandler.removeMessages(RELEASE_WAKELOCK);
834 mWakeLock.release();
835 }
836 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200837
Michael Kolb8233fac2010-10-26 16:08:53 -0700838 // Performance probe
839 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800840 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700841 }
842
843 Performance.tracePageFinished();
844 }
845
846 @Override
John Reck30c714c2010-12-16 17:30:34 -0800847 public void onProgressChanged(Tab tab) {
848 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700849
850 if (newProgress == 100) {
851 CookieSyncManager.getInstance().sync();
852 // onProgressChanged() may continue to be called after the main
853 // frame has finished loading, as any remaining sub frames continue
854 // to load. We'll only get called once though with newProgress as
855 // 100 when everything is loaded. (onPageFinished is called once
856 // when the main frame completes loading regardless of the state of
857 // any sub frames so calls to onProgressChanges may continue after
858 // onPageFinished has executed)
859 if (mInLoad) {
860 mInLoad = false;
861 updateInLoadMenuItems(mCachedMenu);
862 }
863 } else {
864 if (!mInLoad) {
865 // onPageFinished may have already been called but a subframe is
866 // still loading and updating the progress. Reset mInLoad and
867 // update the menu items.
868 mInLoad = true;
869 updateInLoadMenuItems(mCachedMenu);
870 }
871 }
John Reck30c714c2010-12-16 17:30:34 -0800872 mUi.onProgressChanged(tab);
873 }
874
875 @Override
876 public void onUpdatedLockIcon(Tab tab) {
877 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 }
879
880 @Override
881 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800882 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800883 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800884 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700885 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
886 return;
887 }
888 // Update the title in the history database if not in private browsing mode
889 if (!tab.isPrivateBrowsingEnabled()) {
John Reck0ebd3ac2010-12-09 11:14:04 -0800890 mDataController.updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700891 }
892 }
893
894 @Override
895 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800896 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700897 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
898 }
899
900 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800901 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
902 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700903 }
904
905 @Override
906 public boolean shouldOverrideKeyEvent(KeyEvent event) {
907 if (mMenuIsDown) {
908 // only check shortcut key when MENU is held
909 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
910 event);
911 } else {
912 return false;
913 }
914 }
915
916 @Override
917 public void onUnhandledKeyEvent(KeyEvent event) {
918 if (!isActivityPaused()) {
919 if (event.getAction() == KeyEvent.ACTION_DOWN) {
920 mActivity.onKeyDown(event.getKeyCode(), event);
921 } else {
922 mActivity.onKeyUp(event.getKeyCode(), event);
923 }
924 }
925 }
926
927 @Override
John Reck324d4402011-01-11 16:56:42 -0800928 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700929 // Don't save anything in private browsing mode
930 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800931 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700932
John Reck324d4402011-01-11 16:56:42 -0800933 if (TextUtils.isEmpty(url)
934 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700935 return;
936 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800937 mDataController.updateVisitedHistory(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 WebIconDatabase.getInstance().retainIconForPageUrl(url);
John Reck847b5322011-04-14 17:02:18 -0700939 if (!mActivityPaused) {
940 // Since we clear the state in onPause, don't backup the current
941 // state if we are already paused
942 mCrashRecoveryHandler.backupState();
943 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700944 }
945
946 @Override
947 public void getVisitedHistory(final ValueCallback<String[]> callback) {
948 AsyncTask<Void, Void, String[]> task =
949 new AsyncTask<Void, Void, String[]>() {
950 @Override
951 public String[] doInBackground(Void... unused) {
952 return Browser.getVisitedHistory(mActivity.getContentResolver());
953 }
954 @Override
955 public void onPostExecute(String[] result) {
956 callback.onReceiveValue(result);
957 }
958 };
959 task.execute();
960 }
961
962 @Override
963 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
964 final HttpAuthHandler handler, final String host,
965 final String realm) {
966 String username = null;
967 String password = null;
968
969 boolean reuseHttpAuthUsernamePassword
970 = handler.useHttpAuthUsernamePassword();
971
972 if (reuseHttpAuthUsernamePassword && view != null) {
973 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
974 if (credentials != null && credentials.length == 2) {
975 username = credentials[0];
976 password = credentials[1];
977 }
978 }
979
980 if (username != null && password != null) {
981 handler.proceed(username, password);
982 } else {
983 if (tab.inForeground()) {
984 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
985 } else {
986 handler.cancel();
987 }
988 }
989 }
990
991 @Override
992 public void onDownloadStart(Tab tab, String url, String userAgent,
993 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000994 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -0500995 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000996 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
997 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700998 // This Tab was opened for the sole purpose of downloading a
999 // file. Remove it.
1000 if (tab == mTabControl.getCurrentTab()) {
1001 // In this case, the Tab is still on top.
1002 goBackOnePageOrQuit();
1003 } else {
1004 // In this case, it is not.
1005 closeTab(tab);
1006 }
1007 }
1008 }
1009
1010 @Override
1011 public Bitmap getDefaultVideoPoster() {
1012 return mUi.getDefaultVideoPoster();
1013 }
1014
1015 @Override
1016 public View getVideoLoadingProgressView() {
1017 return mUi.getVideoLoadingProgressView();
1018 }
1019
1020 @Override
1021 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1022 SslError error) {
1023 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1024 }
1025
Patrick Scott92066772011-03-10 08:46:27 -05001026 @Override
1027 public void showAutoLogin(Tab tab) {
1028 assert tab.inForeground();
1029 // Update the title bar to show the auto-login request.
1030 mUi.showAutoLogin(tab);
1031 }
1032
1033 @Override
1034 public void hideAutoLogin(Tab tab) {
1035 assert tab.inForeground();
1036 mUi.hideAutoLogin(tab);
1037 }
1038
Michael Kolb8233fac2010-10-26 16:08:53 -07001039 // helper method
1040
1041 /*
1042 * Update the favorites icon if the private browsing isn't enabled and the
1043 * icon is valid.
1044 */
1045 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1046 final String url, Bitmap favicon) {
1047 if (favicon == null) {
1048 return;
1049 }
1050 if (!tab.isPrivateBrowsingEnabled()) {
1051 Bookmarks.updateFavicon(mActivity
1052 .getContentResolver(), originalUrl, url, favicon);
1053 }
1054 }
1055
Leon Scroggins4cd97792010-12-03 15:31:56 -05001056 @Override
1057 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001058 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001059 mUi.bookmarkedStatusHasChanged(tab);
1060 }
1061
Michael Kolb8233fac2010-10-26 16:08:53 -07001062 // end WebViewController
1063
1064 protected void pageUp() {
1065 getCurrentTopWebView().pageUp(false);
1066 }
1067
1068 protected void pageDown() {
1069 getCurrentTopWebView().pageDown(false);
1070 }
1071
1072 // callback from phone title bar
1073 public void editUrl() {
1074 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001075 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001076 }
1077
Michael Kolbcfa3af52010-12-14 10:36:11 -08001078 public void startVoiceSearch() {
1079 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1080 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1081 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1082 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1083 mActivity.getComponentName().flattenToString());
1084 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001085 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001086 mActivity.startActivity(intent);
1087 }
1088
Michael Kolb11d19782011-03-20 10:17:40 -07001089 @Override
1090 public void activateVoiceSearchMode(String title, List<String> results) {
1091 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001092 }
1093
1094 public void revertVoiceSearchMode(Tab tab) {
1095 mUi.revertVoiceTitleBar(tab);
1096 }
1097
Michael Kolb736990c2011-03-20 10:01:20 -07001098 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001099 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001100 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1101 }
1102
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001103 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001104 WebChromeClient.CustomViewCallback callback) {
1105 if (tab.inForeground()) {
1106 if (mUi.isCustomViewShowing()) {
1107 callback.onCustomViewHidden();
1108 return;
1109 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001110 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001111 // Save the menu state and set it to empty while the custom
1112 // view is showing.
1113 mOldMenuState = mMenuState;
1114 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001115 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001116 }
1117 }
1118
1119 @Override
1120 public void hideCustomView() {
1121 if (mUi.isCustomViewShowing()) {
1122 mUi.onHideCustomView();
1123 // Reset the old menu state.
1124 mMenuState = mOldMenuState;
1125 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001126 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001127 }
1128 }
1129
1130 protected void onActivityResult(int requestCode, int resultCode,
1131 Intent intent) {
1132 if (getCurrentTopWebView() == null) return;
1133 switch (requestCode) {
1134 case PREFERENCES_PAGE:
1135 if (resultCode == Activity.RESULT_OK && intent != null) {
1136 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001137 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001138 mTabControl.removeParentChildRelationShips();
1139 }
1140 }
1141 break;
1142 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001143 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001144 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001145 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001146 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001147 case AUTOFILL_SETUP:
1148 // Determine whether a profile was actually set up or not
1149 // and if so, send the message back to the WebTextView to
1150 // fill the form with the new profile.
1151 if (getSettings().getAutoFillProfile() != null) {
1152 mAutoFillSetupMessage.sendToTarget();
1153 mAutoFillSetupMessage = null;
1154 }
1155 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001156 default:
1157 break;
1158 }
1159 getCurrentTopWebView().requestFocus();
1160 }
1161
1162 /**
1163 * Open the Go page.
1164 * @param startWithHistory If true, open starting on the history tab.
1165 * Otherwise, start with the bookmarks tab.
1166 */
1167 @Override
1168 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1169 if (mTabControl.getCurrentWebView() == null) {
1170 return;
1171 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001172 // clear action mode
1173 if (isInCustomActionMode()) {
1174 endActionMode();
1175 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001176 Bundle extras = new Bundle();
1177 // Disable opening in a new window if we have maxed out the windows
1178 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1179 !mTabControl.canCreateNewTab());
1180 mUi.showComboView(startWithHistory, extras);
1181 }
1182
1183 // combo view callbacks
1184
1185 /**
1186 * callback from ComboPage when clear history is requested
1187 */
1188 public void onRemoveParentChildRelationships() {
1189 mTabControl.removeParentChildRelationShips();
1190 }
1191
1192 /**
1193 * callback from ComboPage when bookmark/history selection
1194 */
1195 @Override
1196 public void onUrlSelected(String url, boolean newTab) {
1197 removeComboView();
1198 if (!TextUtils.isEmpty(url)) {
1199 if (newTab) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07001200 final Tab parent = mTabControl.getCurrentTab();
1201 openTab(url,
1202 (parent != null) && parent.isPrivateBrowsingEnabled(),
1203 !mSettings.openInBackground(),
1204 true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001205 } else {
1206 final Tab currentTab = mTabControl.getCurrentTab();
1207 dismissSubWindow(currentTab);
1208 loadUrl(getCurrentTopWebView(), url);
1209 }
1210 }
1211 }
1212
1213 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001214 * dismiss the ComboPage
1215 */
1216 @Override
1217 public void removeComboView() {
1218 mUi.hideComboView();
1219 }
1220
1221 // active tabs page handling
1222
1223 protected void showActiveTabsPage() {
1224 mMenuState = EMPTY_MENU;
1225 mUi.showActiveTabsPage();
1226 }
1227
1228 /**
1229 * Remove the active tabs page.
1230 * @param needToAttach If true, the active tabs page did not attach a tab
1231 * to the content view, so we need to do that here.
1232 */
1233 @Override
1234 public void removeActiveTabsPage(boolean needToAttach) {
1235 mMenuState = R.id.MAIN_MENU;
1236 mUi.removeActiveTabsPage();
1237 if (needToAttach) {
1238 setActiveTab(mTabControl.getCurrentTab());
1239 }
1240 getCurrentTopWebView().requestFocus();
1241 }
1242
1243 // key handling
1244 protected void onBackKey() {
1245 if (!mUi.onBackKey()) {
1246 WebView subwindow = mTabControl.getCurrentSubWindow();
1247 if (subwindow != null) {
1248 if (subwindow.canGoBack()) {
1249 subwindow.goBack();
1250 } else {
1251 dismissSubWindow(mTabControl.getCurrentTab());
1252 }
1253 } else {
1254 goBackOnePageOrQuit();
1255 }
1256 }
1257 }
1258
Michael Kolb4bd767d2011-05-27 11:33:55 -07001259 protected boolean onMenuKey() {
1260 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001261 }
1262
Michael Kolb8233fac2010-10-26 16:08:53 -07001263 // menu handling and state
1264 // TODO: maybe put into separate handler
1265
1266 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckb3417f02011-01-14 11:01:05 -08001267 if (mOptionsMenuHandler != null) {
1268 return mOptionsMenuHandler.onCreateOptionsMenu(menu);
1269 }
1270
John Reckd73c5a22010-12-22 10:22:50 -08001271 if (mMenuState == EMPTY_MENU) {
1272 return false;
1273 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001274 MenuInflater inflater = mActivity.getMenuInflater();
1275 inflater.inflate(R.menu.browser, menu);
1276 updateInLoadMenuItems(menu);
1277 // hold on to the menu reference here; it is used by the page callbacks
1278 // to update the menu based on loading state
1279 mCachedMenu = menu;
1280 return true;
1281 }
1282
1283 protected void onCreateContextMenu(ContextMenu menu, View v,
1284 ContextMenuInfo menuInfo) {
1285 if (v instanceof TitleBarBase) {
1286 return;
1287 }
1288 if (!(v instanceof WebView)) {
1289 return;
1290 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001291 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001292 WebView.HitTestResult result = webview.getHitTestResult();
1293 if (result == null) {
1294 return;
1295 }
1296
1297 int type = result.getType();
1298 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1299 Log.w(LOGTAG,
1300 "We should not show context menu when nothing is touched");
1301 return;
1302 }
1303 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1304 // let TextView handles context menu
1305 return;
1306 }
1307
1308 // Note, http://b/issue?id=1106666 is requesting that
1309 // an inflated menu can be used again. This is not available
1310 // yet, so inflate each time (yuk!)
1311 MenuInflater inflater = mActivity.getMenuInflater();
1312 inflater.inflate(R.menu.browsercontext, menu);
1313
1314 // Show the correct menu group
1315 final String extra = result.getExtra();
1316 menu.setGroupVisible(R.id.PHONE_MENU,
1317 type == WebView.HitTestResult.PHONE_TYPE);
1318 menu.setGroupVisible(R.id.EMAIL_MENU,
1319 type == WebView.HitTestResult.EMAIL_TYPE);
1320 menu.setGroupVisible(R.id.GEO_MENU,
1321 type == WebView.HitTestResult.GEO_TYPE);
1322 menu.setGroupVisible(R.id.IMAGE_MENU,
1323 type == WebView.HitTestResult.IMAGE_TYPE
1324 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1325 menu.setGroupVisible(R.id.ANCHOR_MENU,
1326 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1327 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001328 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1329 || type == WebView.HitTestResult.PHONE_TYPE
1330 || type == WebView.HitTestResult.EMAIL_TYPE
1331 || type == WebView.HitTestResult.GEO_TYPE;
1332 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1333 if (hitText) {
1334 menu.findItem(R.id.select_text_menu_id)
1335 .setOnMenuItemClickListener(new SelectText(webview));
1336 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001337 // Setup custom handling depending on the type
1338 switch (type) {
1339 case WebView.HitTestResult.PHONE_TYPE:
1340 menu.setHeaderTitle(Uri.decode(extra));
1341 menu.findItem(R.id.dial_context_menu_id).setIntent(
1342 new Intent(Intent.ACTION_VIEW, Uri
1343 .parse(WebView.SCHEME_TEL + extra)));
1344 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1345 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1346 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1347 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1348 addIntent);
1349 menu.findItem(R.id.copy_phone_context_menu_id)
1350 .setOnMenuItemClickListener(
1351 new Copy(extra));
1352 break;
1353
1354 case WebView.HitTestResult.EMAIL_TYPE:
1355 menu.setHeaderTitle(extra);
1356 menu.findItem(R.id.email_context_menu_id).setIntent(
1357 new Intent(Intent.ACTION_VIEW, Uri
1358 .parse(WebView.SCHEME_MAILTO + extra)));
1359 menu.findItem(R.id.copy_mail_context_menu_id)
1360 .setOnMenuItemClickListener(
1361 new Copy(extra));
1362 break;
1363
1364 case WebView.HitTestResult.GEO_TYPE:
1365 menu.setHeaderTitle(extra);
1366 menu.findItem(R.id.map_context_menu_id).setIntent(
1367 new Intent(Intent.ACTION_VIEW, Uri
1368 .parse(WebView.SCHEME_GEO
1369 + URLEncoder.encode(extra))));
1370 menu.findItem(R.id.copy_geo_context_menu_id)
1371 .setOnMenuItemClickListener(
1372 new Copy(extra));
1373 break;
1374
1375 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1376 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001377 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001378 // decide whether to show the open link in new tab option
1379 boolean showNewTab = mTabControl.canCreateNewTab();
1380 MenuItem newTabItem
1381 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001382 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001383 ? R.string.contextmenu_openlink_newwindow_background
1384 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001385 newTabItem.setVisible(showNewTab);
1386 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001387 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1388 newTabItem.setOnMenuItemClickListener(
1389 new MenuItem.OnMenuItemClickListener() {
1390 @Override
1391 public boolean onMenuItemClick(MenuItem item) {
1392 final HashMap<String, WebView> hrefMap =
1393 new HashMap<String, WebView>();
1394 hrefMap.put("webview", webview);
1395 final Message msg = mHandler.obtainMessage(
1396 FOCUS_NODE_HREF,
1397 R.id.open_newtab_context_menu_id,
1398 0, hrefMap);
1399 webview.requestFocusNodeHref(msg);
1400 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001401 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001402 });
1403 } else {
1404 newTabItem.setOnMenuItemClickListener(
1405 new MenuItem.OnMenuItemClickListener() {
1406 @Override
1407 public boolean onMenuItemClick(MenuItem item) {
1408 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001409 openTab(extra, parent,
1410 !mSettings.openInBackground(),
1411 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001412 return true;
1413 }
1414 });
1415 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001416 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001417 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1418 break;
1419 }
1420 // otherwise fall through to handle image part
1421 case WebView.HitTestResult.IMAGE_TYPE:
1422 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1423 menu.setHeaderTitle(extra);
1424 }
1425 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1426 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1427 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001428 setOnMenuItemClickListener(
1429 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001430 menu.findItem(R.id.set_wallpaper_context_menu_id).
1431 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1432 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001433 break;
1434
1435 default:
1436 Log.w(LOGTAG, "We should not get here.");
1437 break;
1438 }
1439 //update the ui
1440 mUi.onContextMenuCreated(menu);
1441 }
1442
1443 /**
1444 * As the menu can be open when loading state changes
1445 * we must manually update the state of the stop/reload menu
1446 * item
1447 */
1448 private void updateInLoadMenuItems(Menu menu) {
1449 if (menu == null) {
1450 return;
1451 }
1452 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1453 MenuItem src = mInLoad ?
1454 menu.findItem(R.id.stop_menu_id):
1455 menu.findItem(R.id.reload_menu_id);
1456 if (src != null) {
1457 dest.setIcon(src.getIcon());
1458 dest.setTitle(src.getTitle());
1459 }
1460 }
1461
John Reckb3417f02011-01-14 11:01:05 -08001462 boolean onPrepareOptionsMenu(Menu menu) {
1463 if (mOptionsMenuHandler != null) {
1464 return mOptionsMenuHandler.onPrepareOptionsMenu(menu);
1465 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001466 // Note: setVisible will decide whether an item is visible; while
1467 // setEnabled() will decide whether an item is enabled, which also means
1468 // whether the matching shortcut key will function.
1469 switch (mMenuState) {
1470 case EMPTY_MENU:
1471 if (mCurrentMenuState != mMenuState) {
1472 menu.setGroupVisible(R.id.MAIN_MENU, false);
1473 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1474 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1475 }
1476 break;
1477 default:
1478 if (mCurrentMenuState != mMenuState) {
1479 menu.setGroupVisible(R.id.MAIN_MENU, true);
1480 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1481 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1482 }
1483 final WebView w = getCurrentTopWebView();
1484 boolean canGoBack = false;
1485 boolean canGoForward = false;
1486 boolean isHome = false;
1487 if (w != null) {
1488 canGoBack = w.canGoBack();
1489 canGoForward = w.canGoForward();
1490 isHome = mSettings.getHomePage().equals(w.getUrl());
1491 }
1492 final MenuItem back = menu.findItem(R.id.back_menu_id);
1493 back.setEnabled(canGoBack);
1494
1495 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1496 home.setEnabled(!isHome);
1497
1498 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1499 forward.setEnabled(canGoForward);
1500
1501 // decide whether to show the share link option
1502 PackageManager pm = mActivity.getPackageManager();
1503 Intent send = new Intent(Intent.ACTION_SEND);
1504 send.setType("text/plain");
1505 ResolveInfo ri = pm.resolveActivity(send,
1506 PackageManager.MATCH_DEFAULT_ONLY);
1507 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1508
John Reck35e9dd62011-04-25 09:01:54 -07001509 boolean isNavDump = mSettings.enableNavDump();
Michael Kolb8233fac2010-10-26 16:08:53 -07001510 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1511 nav.setVisible(isNavDump);
1512 nav.setEnabled(isNavDump);
1513
John Reck35e9dd62011-04-25 09:01:54 -07001514 boolean showDebugSettings = mSettings.isDebugEnabled();
Michael Kolb8233fac2010-10-26 16:08:53 -07001515 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1516 counter.setVisible(showDebugSettings);
1517 counter.setEnabled(showDebugSettings);
1518
John Reckb3417f02011-01-14 11:01:05 -08001519 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
1520 newtab.setEnabled(getTabControl().canCreateNewTab());
Michael Kolb8233fac2010-10-26 16:08:53 -07001521
Leon Scroggins81983762011-02-11 15:17:36 -05001522 MenuItem archive = menu.findItem(R.id.save_webarchive_menu_id);
John Reck51d8bad2011-02-28 15:47:47 -08001523 Tab tab = getTabControl().getCurrentTab();
1524 String url = tab != null ? tab.getUrl() : null;
1525 archive.setVisible(!TextUtils.isEmpty(url)
1526 && !url.endsWith(".webarchivexml"));
Michael Kolb8233fac2010-10-26 16:08:53 -07001527 break;
1528 }
1529 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001530 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001531 }
1532
1533 public boolean onOptionsItemSelected(MenuItem item) {
John Reckb3417f02011-01-14 11:01:05 -08001534 if (mOptionsMenuHandler != null &&
1535 mOptionsMenuHandler.onOptionsItemSelected(item)) {
1536 return true;
1537 }
1538
Michael Kolb8233fac2010-10-26 16:08:53 -07001539 if (item.getGroupId() != R.id.CONTEXT_MENU) {
1540 // menu remains active, so ensure comboview is dismissed
1541 // if main menu option is selected
1542 removeComboView();
1543 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001544 if (null == getCurrentTopWebView()) {
1545 return false;
1546 }
1547 if (mMenuIsDown) {
1548 // The shortcut action consumes the MENU. Even if it is still down,
1549 // it won't trigger the next shortcut action. In the case of the
1550 // shortcut action triggering a new activity, like Bookmarks, we
1551 // won't get onKeyUp for MENU. So it is important to reset it here.
1552 mMenuIsDown = false;
1553 }
1554 switch (item.getItemId()) {
1555 // -- Main menu
1556 case R.id.new_tab_menu_id:
1557 openTabToHomePage();
1558 break;
1559
1560 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001561 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001562 break;
1563
1564 case R.id.goto_menu_id:
1565 editUrl();
1566 break;
1567
1568 case R.id.bookmarks_menu_id:
1569 bookmarksOrHistoryPicker(false);
1570 break;
1571
1572 case R.id.active_tabs_menu_id:
1573 showActiveTabsPage();
1574 break;
1575
1576 case R.id.add_bookmark_menu_id:
John Reck3ffc5ca2011-06-10 15:56:06 -07001577 bookmarkCurrentPage(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001578 break;
1579
1580 case R.id.stop_reload_menu_id:
1581 if (mInLoad) {
1582 stopLoading();
1583 } else {
1584 getCurrentTopWebView().reload();
1585 }
1586 break;
1587
1588 case R.id.back_menu_id:
1589 getCurrentTopWebView().goBack();
1590 break;
1591
1592 case R.id.forward_menu_id:
1593 getCurrentTopWebView().goForward();
1594 break;
1595
1596 case R.id.close_menu_id:
1597 // Close the subwindow if it exists.
1598 if (mTabControl.getCurrentSubWindow() != null) {
1599 dismissSubWindow(mTabControl.getCurrentTab());
1600 break;
1601 }
1602 closeCurrentTab();
1603 break;
1604
1605 case R.id.homepage_menu_id:
1606 Tab current = mTabControl.getCurrentTab();
1607 if (current != null) {
1608 dismissSubWindow(current);
1609 loadUrl(current.getWebView(), mSettings.getHomePage());
1610 }
1611 break;
1612
1613 case R.id.preferences_menu_id:
1614 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1615 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1616 getCurrentTopWebView().getUrl());
1617 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1618 break;
1619
1620 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001621 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001622 break;
1623
John Reckf33b1632011-06-04 20:00:23 -07001624 case R.id.freeze_tab_menu_id:
1625 // TODO: Show error messages
John Reck541f55a2011-06-07 16:34:43 -07001626 Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001627 if (source == null) break;
John Reckd8c74522011-06-14 08:45:00 -07001628 final ContentResolver cr = mActivity.getContentResolver();
1629 final ContentValues values = source.createSnapshotValues();
1630 new AsyncTask<Tab, Void, Long>() {
1631 @Override
1632 protected Long doInBackground(Tab... params) {
1633 Tab t = params[0];
1634 if (values == null) {
1635 return t.isSnapshot()
1636 ? ((SnapshotTab)t).getSnapshotId()
1637 : -1;
1638 }
1639 Uri result = cr.insert(Snapshots.CONTENT_URI, values);
1640 long id = ContentUris.parseId(result);
1641 return id;
1642 }
1643
1644 protected void onPostExecute(Long id) {
1645 if (id > 0) {
1646 createNewSnapshotTab(id, true);
1647 }
1648 };
1649 }.execute(source);
John Reckf33b1632011-06-04 20:00:23 -07001650 break;
1651
Leon Scrogginsac993842011-02-02 12:54:07 -05001652 case R.id.save_webarchive_menu_id:
1653 String state = Environment.getExternalStorageState();
1654 if (!Environment.MEDIA_MOUNTED.equals(state)) {
1655 Log.e(LOGTAG, "External storage not mounted");
1656 Toast.makeText(mActivity, R.string.webarchive_failed,
1657 Toast.LENGTH_SHORT).show();
1658 break;
1659 }
1660 final String directory = Environment.getExternalStoragePublicDirectory(
1661 Environment.DIRECTORY_DOWNLOADS) + File.separator;
1662 File dir = new File(directory);
1663 if (!dir.exists() && !dir.mkdirs()) {
1664 Log.e(LOGTAG, "Save as Web Archive: mkdirs for " + directory + " failed!");
1665 Toast.makeText(mActivity, R.string.webarchive_failed,
1666 Toast.LENGTH_SHORT).show();
1667 break;
1668 }
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001669 final WebView topWebView = getCurrentTopWebView();
Leon Scrogginsac993842011-02-02 12:54:07 -05001670 final String title = topWebView.getTitle();
John Reck51d8bad2011-02-28 15:47:47 -08001671 final String url = topWebView.getUrl();
Leon Scrogginsac993842011-02-02 12:54:07 -05001672 topWebView.saveWebArchive(directory, true,
1673 new ValueCallback<String>() {
1674 @Override
1675 public void onReceiveValue(final String value) {
1676 if (value != null) {
1677 File file = new File(value);
1678 final long length = file.length();
1679 if (file.exists() && length > 0) {
Leon Scroggins1cb96552011-02-11 14:22:57 -05001680 Toast.makeText(mActivity, R.string.webarchive_saved,
1681 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001682 final DownloadManager manager = (DownloadManager) mActivity
1683 .getSystemService(Context.DOWNLOAD_SERVICE);
1684 new Thread("Add WebArchive to download manager") {
1685 @Override
1686 public void run() {
Vasu Noria9e30a72011-03-07 11:37:34 -08001687 manager.addCompletedDownload(
1688 null == title ? value : title,
Leon Scrogginsac993842011-02-02 12:54:07 -05001689 value, true, "application/x-webarchive-xml",
1690 value, length, true);
1691 }
1692 }.start();
1693 return;
1694 }
1695 }
John Reck51d8bad2011-02-28 15:47:47 -08001696 DownloadHandler.onDownloadStartNoStream(mActivity,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001697 url, null, null, null, topWebView.isPrivateBrowsingEnabled());
Leon Scrogginsac993842011-02-02 12:54:07 -05001698 }
1699 });
1700 break;
1701
Michael Kolb8233fac2010-10-26 16:08:53 -07001702 case R.id.page_info_menu_id:
1703 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(),
1704 false);
1705 break;
1706
1707 case R.id.classic_history_menu_id:
1708 bookmarksOrHistoryPicker(true);
1709 break;
1710
1711 case R.id.title_bar_share_page_url:
1712 case R.id.share_page_menu_id:
1713 Tab currentTab = mTabControl.getCurrentTab();
1714 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001715 return false;
1716 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001717 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001718 break;
1719
1720 case R.id.dump_nav_menu_id:
1721 getCurrentTopWebView().debugDump();
1722 break;
1723
1724 case R.id.dump_counters_menu_id:
1725 getCurrentTopWebView().dumpV8Counters();
1726 break;
1727
1728 case R.id.zoom_in_menu_id:
1729 getCurrentTopWebView().zoomIn();
1730 break;
1731
1732 case R.id.zoom_out_menu_id:
1733 getCurrentTopWebView().zoomOut();
1734 break;
1735
1736 case R.id.view_downloads_menu_id:
1737 viewDownloads();
1738 break;
1739
1740 case R.id.window_one_menu_id:
1741 case R.id.window_two_menu_id:
1742 case R.id.window_three_menu_id:
1743 case R.id.window_four_menu_id:
1744 case R.id.window_five_menu_id:
1745 case R.id.window_six_menu_id:
1746 case R.id.window_seven_menu_id:
1747 case R.id.window_eight_menu_id:
1748 {
1749 int menuid = item.getItemId();
1750 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1751 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1752 Tab desiredTab = mTabControl.getTab(id);
1753 if (desiredTab != null &&
1754 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001755 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001756 }
1757 break;
1758 }
1759 }
1760 }
1761 break;
1762
1763 default:
1764 return false;
1765 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001766 return true;
1767 }
1768
1769 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001770 // Let the History and Bookmark fragments handle menus they created.
1771 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1772 return false;
1773 }
1774
Michael Kolb8233fac2010-10-26 16:08:53 -07001775 int id = item.getItemId();
1776 boolean result = true;
1777 switch (id) {
1778 // For the context menu from the title bar
1779 case R.id.title_bar_copy_page_url:
1780 Tab currentTab = mTabControl.getCurrentTab();
1781 if (null == currentTab) {
1782 result = false;
1783 break;
1784 }
1785 WebView mainView = currentTab.getWebView();
1786 if (null == mainView) {
1787 result = false;
1788 break;
1789 }
1790 copy(mainView.getUrl());
1791 break;
1792 // -- Browser context menu
1793 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001794 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001795 case R.id.copy_link_context_menu_id:
1796 final WebView webView = getCurrentTopWebView();
1797 if (null == webView) {
1798 result = false;
1799 break;
1800 }
1801 final HashMap<String, WebView> hrefMap =
1802 new HashMap<String, WebView>();
1803 hrefMap.put("webview", webView);
1804 final Message msg = mHandler.obtainMessage(
1805 FOCUS_NODE_HREF, id, 0, hrefMap);
1806 webView.requestFocusNodeHref(msg);
1807 break;
1808
1809 default:
1810 // For other context menus
1811 result = onOptionsItemSelected(item);
1812 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001813 return result;
1814 }
1815
1816 /**
1817 * support programmatically opening the context menu
1818 */
1819 public void openContextMenu(View view) {
1820 mActivity.openContextMenu(view);
1821 }
1822
1823 /**
1824 * programmatically open the options menu
1825 */
1826 public void openOptionsMenu() {
1827 mActivity.openOptionsMenu();
1828 }
1829
1830 public boolean onMenuOpened(int featureId, Menu menu) {
1831 if (mOptionsMenuOpen) {
1832 if (mConfigChanged) {
1833 // We do not need to make any changes to the state of the
1834 // title bar, since the only thing that happened was a
1835 // change in orientation
1836 mConfigChanged = false;
1837 } else {
1838 if (!mExtendedMenuOpen) {
1839 mExtendedMenuOpen = true;
1840 mUi.onExtendedMenuOpened();
1841 } else {
1842 // Switching the menu back to icon view, so show the
1843 // title bar once again.
1844 mExtendedMenuOpen = false;
1845 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001846 }
1847 }
1848 } else {
1849 // The options menu is closed, so open it, and show the title
1850 mOptionsMenuOpen = true;
1851 mConfigChanged = false;
1852 mExtendedMenuOpen = false;
1853 mUi.onOptionsMenuOpened();
1854 }
1855 return true;
1856 }
1857
1858 public void onOptionsMenuClosed(Menu menu) {
1859 mOptionsMenuOpen = false;
1860 mUi.onOptionsMenuClosed(mInLoad);
1861 }
1862
1863 public void onContextMenuClosed(Menu menu) {
1864 mUi.onContextMenuClosed(menu, mInLoad);
1865 }
1866
1867 // Helper method for getting the top window.
1868 @Override
1869 public WebView getCurrentTopWebView() {
1870 return mTabControl.getCurrentTopWebView();
1871 }
1872
1873 @Override
1874 public WebView getCurrentWebView() {
1875 return mTabControl.getCurrentWebView();
1876 }
1877
1878 /*
1879 * This method is called as a result of the user selecting the options
1880 * menu to see the download window. It shows the download window on top of
1881 * the current window.
1882 */
1883 void viewDownloads() {
1884 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1885 mActivity.startActivity(intent);
1886 }
1887
1888 // action mode
1889
1890 void onActionModeStarted(ActionMode mode) {
1891 mUi.onActionModeStarted(mode);
1892 mActionMode = mode;
1893 }
1894
1895 /*
1896 * True if a custom ActionMode (i.e. find or select) is in use.
1897 */
1898 @Override
1899 public boolean isInCustomActionMode() {
1900 return mActionMode != null;
1901 }
1902
1903 /*
1904 * End the current ActionMode.
1905 */
1906 @Override
1907 public void endActionMode() {
1908 if (mActionMode != null) {
1909 mActionMode.finish();
1910 }
1911 }
1912
1913 /*
1914 * Called by find and select when they are finished. Replace title bars
1915 * as necessary.
1916 */
1917 public void onActionModeFinished(ActionMode mode) {
1918 if (!isInCustomActionMode()) return;
1919 mUi.onActionModeFinished(mInLoad);
1920 mActionMode = null;
1921 }
1922
1923 boolean isInLoad() {
1924 return mInLoad;
1925 }
1926
1927 // bookmark handling
1928
1929 /**
1930 * add the current page as a bookmark to the given folder id
1931 * @param folderId use -1 for the default folder
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001932 * @param canBeAnEdit If true, check to see whether the site is already
1933 * bookmarked, and if it is, edit that bookmark. If false, and
1934 * the site is already bookmarked, do not attempt to edit the
1935 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001936 */
1937 @Override
John Reck3ffc5ca2011-06-10 15:56:06 -07001938 public void bookmarkCurrentPage(boolean canBeAnEdit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001939 Intent i = new Intent(mActivity,
1940 AddBookmarkPage.class);
1941 WebView w = getCurrentTopWebView();
1942 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1943 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1944 String touchIconUrl = w.getTouchIconUrl();
1945 if (touchIconUrl != null) {
1946 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1947 WebSettings settings = w.getSettings();
1948 if (settings != null) {
1949 i.putExtra(AddBookmarkPage.USER_AGENT,
1950 settings.getUserAgentString());
1951 }
1952 }
1953 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1954 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1955 getDesiredThumbnailHeight(mActivity)));
1956 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001957 if (canBeAnEdit) {
1958 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1959 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001960 // Put the dialog at the upper right of the screen, covering the
1961 // star on the title bar.
1962 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
1963 mActivity.startActivity(i);
1964 }
1965
1966 // file chooser
1967 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1968 mUploadHandler = new UploadHandler(this);
1969 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1970 }
1971
1972 // thumbnails
1973
1974 /**
1975 * Return the desired width for thumbnail screenshots, which are stored in
1976 * the database, and used on the bookmarks screen.
1977 * @param context Context for finding out the density of the screen.
1978 * @return desired width for thumbnail screenshot.
1979 */
1980 static int getDesiredThumbnailWidth(Context context) {
1981 return context.getResources().getDimensionPixelOffset(
1982 R.dimen.bookmarkThumbnailWidth);
1983 }
1984
1985 /**
1986 * Return the desired height for thumbnail screenshots, which are stored in
1987 * the database, and used on the bookmarks screen.
1988 * @param context Context for finding out the density of the screen.
1989 * @return desired height for thumbnail screenshot.
1990 */
1991 static int getDesiredThumbnailHeight(Context context) {
1992 return context.getResources().getDimensionPixelOffset(
1993 R.dimen.bookmarkThumbnailHeight);
1994 }
1995
Michael Kolb1acef692011-03-08 14:12:06 -08001996 static Bitmap createScreenshot(Tab tab, int width, int height) {
1997 if ((tab != null) && (tab.getWebView() != null)) {
1998 return createScreenshot(tab.getWebView(), width, height);
1999 }
2000 return null;
2001 }
2002
Michael Kolb8233fac2010-10-26 16:08:53 -07002003 private static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08002004 // We render to a bitmap 2x the desired size so that we can then
2005 // re-scale it with filtering since canvas.scale doesn't filter
2006 // This helps reduce aliasing at the cost of being slightly blurry
2007 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07002008 Picture thumbnail = view.capturePicture();
2009 if (thumbnail == null) {
2010 return null;
2011 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002012 width *= filter_scale;
2013 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07002014 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
2015 Canvas canvas = new Canvas(bm);
2016 // May need to tweak these values to determine what is the
2017 // best scale factor
2018 int thumbnailWidth = thumbnail.getWidth();
2019 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08002020 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08002021 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08002022 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07002023 } else {
2024 return null;
2025 }
John Reckfe49ab42010-11-16 17:09:37 -08002026
Michael Kolbeb95db42011-03-03 10:38:40 -08002027 float scaleFactorY = (float) height / (float)thumbnailHeight;
2028 if (scaleFactorY > scaleFactor) {
2029 // The picture is narrower than the requested AR
2030 // Center the thumnail and crop the sides
2031 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08002032 float wx = (thumbnailWidth * scaleFactor) - width;
2033 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07002034 }
2035
John Reckfe49ab42010-11-16 17:09:37 -08002036 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07002037
2038 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08002039 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
2040 height / filter_scale, true);
2041 bm.recycle();
2042 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002043 }
2044
John Reck34ef2672011-02-10 11:30:55 -08002045 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002046 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002047 // FIXME: Would like to make sure there is actually something to
2048 // draw, but the API for that (WebViewCore.pictureReady()) is not
2049 // currently accessible here.
2050
John Reck34ef2672011-02-10 11:30:55 -08002051 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002052 if (view == null) {
2053 // Tab was destroyed
2054 return;
2055 }
John Reck34ef2672011-02-10 11:30:55 -08002056 final String url = tab.getUrl();
2057 final String originalUrl = view.getOriginalUrl();
2058
2059 if (TextUtils.isEmpty(url)) {
2060 return;
2061 }
2062
2063 // Only update thumbnails for web urls (http(s)://), not for
2064 // about:, javascript:, data:, etc...
2065 // Unless it is a bookmarked site, then always update
2066 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2067 return;
2068 }
2069
Michael Kolb8233fac2010-10-26 16:08:53 -07002070 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2071 getDesiredThumbnailHeight(mActivity));
2072 if (bm == null) {
2073 return;
2074 }
2075
2076 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002077 new AsyncTask<Void, Void, Void>() {
2078 @Override
2079 protected Void doInBackground(Void... unused) {
2080 Cursor cursor = null;
2081 try {
2082 // TODO: Clean this up
2083 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2084 if (cursor != null && cursor.moveToFirst()) {
2085 final ByteArrayOutputStream os =
2086 new ByteArrayOutputStream();
2087 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002088
John Reck34ef2672011-02-10 11:30:55 -08002089 ContentValues values = new ContentValues();
2090 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002091
John Reck34ef2672011-02-10 11:30:55 -08002092 do {
John Reck617fd832011-02-16 14:35:59 -08002093 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002094 cr.update(Images.CONTENT_URI, values, null, null);
2095 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002096 }
John Reck34ef2672011-02-10 11:30:55 -08002097 } catch (IllegalStateException e) {
2098 // Ignore
2099 } finally {
2100 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002101 }
John Reck34ef2672011-02-10 11:30:55 -08002102 return null;
2103 }
2104 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002105 }
2106
2107 private class Copy implements OnMenuItemClickListener {
2108 private CharSequence mText;
2109
2110 public boolean onMenuItemClick(MenuItem item) {
2111 copy(mText);
2112 return true;
2113 }
2114
2115 public Copy(CharSequence toCopy) {
2116 mText = toCopy;
2117 }
2118 }
2119
Leon Scroggins63c02662010-11-18 15:16:27 -05002120 private static class Download implements OnMenuItemClickListener {
2121 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002122 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002123 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002124
2125 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002126 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002127 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002128 return true;
2129 }
2130
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002131 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002132 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002133 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002134 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002135 }
2136 }
2137
Cary Clark8974d282010-11-22 10:46:05 -05002138 private static class SelectText implements OnMenuItemClickListener {
2139 private WebView mWebView;
2140
2141 public boolean onMenuItemClick(MenuItem item) {
2142 if (mWebView != null) {
2143 return mWebView.selectText();
2144 }
2145 return false;
2146 }
2147
2148 public SelectText(WebView webView) {
2149 mWebView = webView;
2150 }
2151
2152 }
2153
Michael Kolb8233fac2010-10-26 16:08:53 -07002154 /********************** TODO: UI stuff *****************************/
2155
2156 // these methods have been copied, they still need to be cleaned up
2157
2158 /****************** tabs ***************************************************/
2159
2160 // basic tab interactions:
2161
2162 // it is assumed that tabcontrol already knows about the tab
2163 protected void addTab(Tab tab) {
2164 mUi.addTab(tab);
2165 }
2166
2167 protected void removeTab(Tab tab) {
2168 mUi.removeTab(tab);
2169 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002170 mCrashRecoveryHandler.backupState();
John Reckd8c74522011-06-14 08:45:00 -07002171 if (tab.isSnapshot()) {
2172 SnapshotTab st = (SnapshotTab) tab;
2173 final Uri uri = ContentUris.withAppendedId(
2174 Snapshots.CONTENT_URI, st.getSnapshotId());
2175 final ContentResolver cr = mActivity.getContentResolver();
2176 new Thread() {
2177 @Override
2178 public void run() {
2179 cr.delete(uri, null, null);
2180 }
2181 }.start();
2182 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002183 }
2184
Michael Kolbf2055602011-04-09 17:20:03 -07002185 @Override
2186 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002187 // monkey protection against delayed start
2188 if (tab != null) {
2189 mTabControl.setCurrentTab(tab);
2190 // the tab is guaranteed to have a webview after setCurrentTab
2191 mUi.setActiveTab(tab);
2192 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002193 }
2194
2195 protected void closeEmptyChildTab() {
2196 Tab current = mTabControl.getCurrentTab();
2197 if (current != null
2198 && current.getWebView().copyBackForwardList().getSize() == 0) {
Michael Kolbc831b632011-05-11 09:30:34 -07002199 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002200 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002201 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002202 closeTab(current);
2203 }
2204 }
2205 }
2206
2207 protected void reuseTab(Tab appTab, String appId, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002208 // Dismiss the subwindow if applicable.
2209 dismissSubWindow(appTab);
2210 // Since we might kill the WebView, remove it from the
2211 // content view first.
2212 mUi.detachTab(appTab);
2213 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002214 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002215 // TODO: analyze why the remove and add are necessary
2216 mUi.attachTab(appTab);
2217 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002218 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002219 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002220 } else {
2221 // If the tab was the current tab, we have to attach
2222 // it to the view system again.
2223 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002224 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002225 }
2226 }
2227
2228 // Remove the sub window if it exists. Also called by TabControl when the
2229 // user clicks the 'X' to dismiss a sub window.
2230 public void dismissSubWindow(Tab tab) {
2231 removeSubWindow(tab);
2232 // dismiss the subwindow. This will destroy the WebView.
2233 tab.dismissSubWindow();
2234 getCurrentTopWebView().requestFocus();
2235 }
2236
2237 @Override
2238 public void removeSubWindow(Tab t) {
2239 if (t.getSubWebView() != null) {
2240 mUi.removeSubWindow(t.getSubViewContainer());
2241 }
2242 }
2243
2244 @Override
2245 public void attachSubWindow(Tab tab) {
2246 if (tab.getSubWebView() != null) {
2247 mUi.attachSubWindow(tab.getSubViewContainer());
2248 getCurrentTopWebView().requestFocus();
2249 }
2250 }
2251
Michael Kolb7bcafde2011-05-09 13:55:59 -07002252 // open a non inconito tab with the given url data
2253 // and set as active tab
2254 public Tab openTab(UrlData urlData) {
2255 Tab tab = createNewTab(false, true, true);
2256 if ((tab != null) && !urlData.isEmpty()) {
2257 loadUrlDataIn(tab, urlData);
2258 }
2259 return tab;
2260 }
2261
Michael Kolb843510f2010-12-09 10:51:49 -08002262 @Override
2263 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002264 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002265 }
2266
Michael Kolb8233fac2010-10-26 16:08:53 -07002267 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002268 public Tab openIncognitoTab() {
2269 return openTab(INCOGNITO_URI, true, true, false);
2270 }
2271
2272 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002273 public Tab openTab(String url, boolean incognito, boolean setActive,
2274 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002275 return openTab(url, incognito, setActive, useCurrent, null);
2276 }
2277
2278 @Override
2279 public Tab openTab(String url, Tab parent, boolean setActive,
2280 boolean useCurrent) {
2281 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2282 setActive, useCurrent, parent);
2283 }
2284
2285 public Tab openTab(String url, boolean incognito, boolean setActive,
2286 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002287 Tab tab = createNewTab(incognito, setActive, useCurrent);
2288 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002289 if (parent != null && parent != tab) {
2290 parent.addChildTab(tab);
2291 }
Michael Kolba4261fd2011-05-05 11:27:37 -07002292 WebView w = tab.getWebView();
Michael Kolb519d2282011-05-09 17:03:19 -07002293 if (url != null) {
2294 loadUrl(w, url);
2295 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002296 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002297 return tab;
2298 }
2299
2300 // this method will attempt to create a new tab
2301 // incognito: private browsing tab
2302 // setActive: ste tab as current tab
2303 // useCurrent: if no new tab can be created, return current tab
2304 private Tab createNewTab(boolean incognito, boolean setActive,
2305 boolean useCurrent) {
2306 Tab tab = null;
2307 if (mTabControl.canCreateNewTab()) {
2308 tab = mTabControl.createNewTab(incognito);
2309 addTab(tab);
2310 if (setActive) {
2311 setActiveTab(tab);
2312 }
2313 } else {
2314 if (useCurrent) {
2315 tab = mTabControl.getCurrentTab();
2316 // Get rid of the subwindow if it exists
2317 dismissSubWindow(tab);
2318 } else {
2319 mUi.showMaxTabsWarning();
2320 }
2321 }
2322 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002323 }
2324
John Reckd8c74522011-06-14 08:45:00 -07002325 private SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2326 SnapshotTab tab = mTabControl.createSnapshotTab(snapshotId);
2327 addTab(tab);
2328 if (setActive) {
2329 setActiveTab(tab);
2330 }
2331 return tab;
2332 }
2333
Michael Kolb8233fac2010-10-26 16:08:53 -07002334 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002335 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002336 * @return boolean True if we successfully switched to a different tab. If
2337 * the indexth tab is null, or if that tab is the same as
2338 * the current one, return false.
2339 */
2340 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002341 public boolean switchToTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002342 // hide combo view if open
2343 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002344 Tab currentTab = mTabControl.getCurrentTab();
2345 if (tab == null || tab == currentTab) {
2346 return false;
2347 }
2348 setActiveTab(tab);
2349 return true;
2350 }
2351
2352 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002353 public void closeCurrentTab() {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002354 // hide combo view if open
2355 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002356 if (mTabControl.getTabCount() == 1) {
John Reck958b2422010-12-03 17:56:17 -08002357 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002358 return;
2359 }
Michael Kolbc831b632011-05-11 09:30:34 -07002360 final Tab current = mTabControl.getCurrentTab();
2361 final int pos = mTabControl.getCurrentPosition();
2362 Tab newTab = current.getParent();
2363 if (newTab == null) {
2364 newTab = mTabControl.getTab(pos + 1);
2365 if (newTab == null) {
2366 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002367 }
2368 }
Michael Kolbc831b632011-05-11 09:30:34 -07002369 if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002370 // Close window
2371 closeTab(current);
2372 }
2373 }
2374
2375 /**
2376 * Close the tab, remove its associated title bar, and adjust mTabControl's
2377 * current tab to a valid value.
2378 */
2379 @Override
2380 public void closeTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002381 // hide combo view if open
2382 removeComboView();
Michael Kolb2d59c322011-01-25 13:18:55 -08002383 removeTab(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002384 }
2385
2386 /**************** TODO: Url loading clean up *******************************/
2387
2388 // Called when loading from context menu or LOAD_URL message
2389 protected void loadUrlFromContext(WebView view, String url) {
2390 // In case the user enters nothing.
2391 if (url != null && url.length() != 0 && view != null) {
2392 url = UrlUtils.smartUrlFilter(url);
2393 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
2394 loadUrl(view, url);
2395 }
2396 }
2397 }
2398
2399 /**
2400 * Load the URL into the given WebView and update the title bar
2401 * to reflect the new load. Call this instead of WebView.loadUrl
2402 * directly.
2403 * @param view The WebView used to load url.
2404 * @param url The URL to load.
2405 */
2406 protected void loadUrl(WebView view, String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002407 view.loadUrl(url);
2408 }
2409
2410 /**
2411 * Load UrlData into a Tab and update the title bar to reflect the new
2412 * load. Call this instead of UrlData.loadIn directly.
2413 * @param t The Tab used to load.
2414 * @param data The UrlData being loaded.
2415 */
2416 protected void loadUrlDataIn(Tab t, UrlData data) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002417 data.loadIn(t);
2418 }
2419
John Reck30c714c2010-12-16 17:30:34 -08002420 @Override
2421 public void onUserCanceledSsl(Tab tab) {
2422 WebView web = tab.getWebView();
2423 // TODO: Figure out the "right" behavior
2424 if (web.canGoBack()) {
2425 web.goBack();
2426 } else {
2427 web.loadUrl(mSettings.getHomePage());
2428 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002429 }
2430
2431 void goBackOnePageOrQuit() {
2432 Tab current = mTabControl.getCurrentTab();
2433 if (current == null) {
2434 /*
2435 * Instead of finishing the activity, simply push this to the back
2436 * of the stack and let ActivityManager to choose the foreground
2437 * activity. As BrowserActivity is singleTask, it will be always the
2438 * root of the task. So we can use either true or false for
2439 * moveTaskToBack().
2440 */
2441 mActivity.moveTaskToBack(true);
2442 return;
2443 }
2444 WebView w = current.getWebView();
2445 if (w.canGoBack()) {
2446 w.goBack();
2447 } else {
2448 // Check to see if we are closing a window that was created by
2449 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002450 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002451 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002452 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002453 // Now we close the other tab
2454 closeTab(current);
2455 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002456 /*
2457 * Instead of finishing the activity, simply push this to the back
2458 * of the stack and let ActivityManager to choose the foreground
2459 * activity. As BrowserActivity is singleTask, it will be always the
2460 * root of the task. So we can use either true or false for
2461 * moveTaskToBack().
2462 */
2463 mActivity.moveTaskToBack(true);
2464 }
2465 }
2466 }
2467
2468 /**
2469 * Feed the previously stored results strings to the BrowserProvider so that
2470 * the SearchDialog will show them instead of the standard searches.
2471 * @param result String to show on the editable line of the SearchDialog.
2472 */
2473 @Override
2474 public void showVoiceSearchResults(String result) {
2475 ContentProviderClient client = mActivity.getContentResolver()
2476 .acquireContentProviderClient(Browser.BOOKMARKS_URI);
2477 ContentProvider prov = client.getLocalContentProvider();
2478 BrowserProvider bp = (BrowserProvider) prov;
2479 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
2480 client.release();
2481
2482 Bundle bundle = createGoogleSearchSourceBundle(
2483 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
2484 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
2485 startSearch(result, false, bundle, false);
2486 }
2487
2488 private void startSearch(String initialQuery, boolean selectInitialQuery,
2489 Bundle appSearchData, boolean globalSearch) {
2490 if (appSearchData == null) {
2491 appSearchData = createGoogleSearchSourceBundle(
2492 GOOGLE_SEARCH_SOURCE_TYPE);
2493 }
2494
2495 SearchEngine searchEngine = mSettings.getSearchEngine();
2496 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
2497 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
2498 }
2499 mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData,
2500 globalSearch);
2501 }
2502
2503 private Bundle createGoogleSearchSourceBundle(String source) {
2504 Bundle bundle = new Bundle();
2505 bundle.putString(Search.SOURCE, source);
2506 return bundle;
2507 }
2508
2509 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002510 * helper method for key handler
2511 * returns the current tab if it can't advance
2512 */
Michael Kolbc831b632011-05-11 09:30:34 -07002513 private Tab getNextTab() {
2514 return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1,
2515 mTabControl.getCurrentPosition() + 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002516 }
2517
2518 /**
2519 * helper method for key handler
2520 * returns the current tab if it can't advance
2521 */
Michael Kolbc831b632011-05-11 09:30:34 -07002522 private Tab getPrevTab() {
2523 return mTabControl.getTab(Math.max(0,
2524 mTabControl.getCurrentPosition() - 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002525 }
2526
2527 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002528 * handle key events in browser
2529 *
2530 * @param keyCode
2531 * @param event
2532 * @return true if handled, false to pass to super
2533 */
2534 boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002535 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002536 // Even if MENU is already held down, we need to call to super to open
2537 // the IME on long press.
Michael Kolb2814a362011-05-19 15:49:41 -07002538 if (KeyEvent.KEYCODE_MENU == keyCode) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002539 if (mOptionsMenuHandler != null) {
2540 return false;
2541 } else {
2542 event.startTracking();
2543 return true;
2544 }
Michael Kolb2814a362011-05-19 15:49:41 -07002545 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002546 if (!noModifiers
2547 && ((KeyEvent.KEYCODE_MENU == keyCode)
2548 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2549 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002550 mMenuIsDown = true;
2551 return false;
2552 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002553
Cary Clark8ff8c662010-12-29 15:03:05 -05002554 WebView webView = getCurrentTopWebView();
2555 if (webView == null) return false;
2556
Cary Clark160bbb92011-01-10 11:17:07 -05002557 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2558 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002559
Michael Kolb8233fac2010-10-26 16:08:53 -07002560 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002561 case KeyEvent.KEYCODE_TAB:
2562 if (event.isCtrlPressed()) {
2563 if (event.isShiftPressed()) {
2564 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002565 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002566 } else {
2567 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002568 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002569 }
2570 return true;
2571 }
2572 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002573 case KeyEvent.KEYCODE_SPACE:
2574 // WebView/WebTextView handle the keys in the KeyDown. As
2575 // the Activity's shortcut keys are only handled when WebView
2576 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002577 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002578 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002579 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002580 pageDown();
2581 }
2582 return true;
2583 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002584 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002585 event.startTracking();
2586 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002587 case KeyEvent.KEYCODE_DPAD_LEFT:
2588 if (ctrl) {
2589 webView.goBack();
2590 return true;
2591 }
2592 break;
2593 case KeyEvent.KEYCODE_DPAD_RIGHT:
2594 if (ctrl) {
2595 webView.goForward();
2596 return true;
2597 }
2598 break;
2599 case KeyEvent.KEYCODE_A:
2600 if (ctrl) {
2601 webView.selectAll();
2602 return true;
2603 }
2604 break;
Michael Kolba4183062011-01-16 10:43:21 -08002605// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002606 case KeyEvent.KEYCODE_C:
2607 if (ctrl) {
2608 webView.copySelection();
2609 return true;
2610 }
2611 break;
Michael Kolba4183062011-01-16 10:43:21 -08002612// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002613// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002614// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002615// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002616// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002617// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002618// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002619// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002620// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002621// case KeyEvent.KEYCODE_M: // unused
2622// case KeyEvent.KEYCODE_N: // in Chrome: new window
2623// case KeyEvent.KEYCODE_O: // in Chrome: open file
2624// case KeyEvent.KEYCODE_P: // in Chrome: print page
2625// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002626// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002627// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2628 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002629 // we can't use the ctrl/shift flags, they check for
2630 // exclusive use of a modifier
2631 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002632 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002633 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002634 } else {
2635 openTabToHomePage();
2636 }
2637 return true;
2638 }
2639 break;
2640// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2641// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb1a2eba42011-03-16 16:42:49 -07002642 case KeyEvent.KEYCODE_W: // in Chrome: close tab
2643 if (ctrl) {
2644 closeCurrentTab();
2645 return true;
2646 }
2647 break;
Cary Clark8ff8c662010-12-29 15:03:05 -05002648// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2649// case KeyEvent.KEYCODE_Y: // unused
2650// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002651 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002652 // it is a regular key and webview is not null
2653 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002654 }
2655
John Recke6bf4ab2011-02-24 15:48:05 -08002656 boolean onKeyLongPress(int keyCode, KeyEvent event) {
2657 switch(keyCode) {
2658 case KeyEvent.KEYCODE_BACK:
2659 if (mUi.showsWeb()) {
2660 bookmarksOrHistoryPicker(true);
2661 return true;
2662 }
2663 break;
2664 }
2665 return false;
2666 }
2667
Michael Kolb8233fac2010-10-26 16:08:53 -07002668 boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb2814a362011-05-19 15:49:41 -07002669 if (KeyEvent.KEYCODE_MENU == keyCode) {
2670 mMenuIsDown = false;
2671 if (event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002672 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002673 }
2674 }
Cary Clark160bbb92011-01-10 11:17:07 -05002675 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002676 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002677 case KeyEvent.KEYCODE_BACK:
2678 if (event.isTracking() && !event.isCanceled()) {
2679 onBackKey();
2680 return true;
2681 }
2682 break;
2683 }
2684 return false;
2685 }
2686
2687 public boolean isMenuDown() {
2688 return mMenuIsDown;
2689 }
2690
Ben Murdoch8029a772010-11-16 11:58:21 +00002691 public void setupAutoFill(Message message) {
2692 // Open the settings activity at the AutoFill profile fragment so that
2693 // the user can create a new profile. When they return, we will dispatch
2694 // the message so that we can autofill the form using their new profile.
2695 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2696 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2697 AutoFillSettingsFragment.class.getName());
2698 mAutoFillSetupMessage = message;
2699 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2700 }
John Reckb3417f02011-01-14 11:01:05 -08002701
2702 @Override
2703 public void registerOptionsMenuHandler(OptionsMenuHandler handler) {
2704 mOptionsMenuHandler = handler;
2705 }
2706
2707 @Override
2708 public void unregisterOptionsMenuHandler(OptionsMenuHandler handler) {
2709 if (mOptionsMenuHandler == handler) {
2710 mOptionsMenuHandler = null;
2711 }
2712 }
2713
Narayan Kamath5119edd2011-02-23 15:49:17 +00002714 @Override
2715 public void registerDropdownChangeListener(DropdownChangeListener d) {
2716 mUi.registerDropdownChangeListener(d);
2717 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002718}