blob: 731da18d571a5821bd81f914d0b2109b98904bdf [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;
John Reck30b065e2011-07-19 10:58:05 -070033import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050034import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070035import android.database.Cursor;
36import android.database.sqlite.SQLiteDatabase;
Michael Kolb8233fac2010-10-26 16:08:53 -070037import android.graphics.Bitmap;
38import android.graphics.Canvas;
39import android.graphics.Picture;
40import android.net.Uri;
41import android.net.http.SslError;
42import android.os.AsyncTask;
43import android.os.Bundle;
44import android.os.Handler;
45import android.os.Message;
46import android.os.PowerManager;
47import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000048import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070049import android.provider.Browser;
50import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.provider.BrowserContract.Images;
52import android.provider.ContactsContract;
53import android.provider.ContactsContract.Intents.Insert;
Michael Kolbcfa3af52010-12-14 10:36:11 -080054import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070055import android.text.TextUtils;
56import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080057import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070058import android.view.ActionMode;
59import android.view.ContextMenu;
60import android.view.ContextMenu.ContextMenuInfo;
61import android.view.Gravity;
62import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070063import android.view.Menu;
64import android.view.MenuInflater;
65import android.view.MenuItem;
66import android.view.MenuItem.OnMenuItemClickListener;
67import android.view.View;
68import android.webkit.CookieManager;
69import android.webkit.CookieSyncManager;
70import android.webkit.HttpAuthHandler;
71import android.webkit.SslErrorHandler;
72import android.webkit.ValueCallback;
73import android.webkit.WebChromeClient;
74import android.webkit.WebIconDatabase;
75import android.webkit.WebSettings;
76import android.webkit.WebView;
Leon Scrogginsac993842011-02-02 12:54:07 -050077import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070078
Michael Kolb4bd767d2011-05-27 11:33:55 -070079import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070080import com.android.browser.UI.ComboViews;
Michael Kolb4bd767d2011-05-27 11:33:55 -070081import com.android.browser.UI.DropdownChangeListener;
82import com.android.browser.provider.BrowserProvider;
John Reck8cc92352011-07-06 17:41:52 -070083import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070084import com.android.browser.search.SearchEngine;
85import com.android.common.Search;
86
Michael Kolb8233fac2010-10-26 16:08:53 -070087import java.io.ByteArrayOutputStream;
88import java.io.File;
89import java.net.URLEncoder;
90import java.util.Calendar;
91import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080092import java.util.List;
John Reck26b18322011-06-21 13:08:58 -070093import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -070094
95/**
96 * Controller for browser
97 */
98public class Controller
99 implements WebViewController, UiController {
100
101 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800102 private static final String SEND_APP_ID_EXTRA =
103 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700104 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800105
Michael Kolb8233fac2010-10-26 16:08:53 -0700106
107 // public message ids
108 public final static int LOAD_URL = 1001;
109 public final static int STOP_LOAD = 1002;
110
111 // Message Ids
112 private static final int FOCUS_NODE_HREF = 102;
113 private static final int RELEASE_WAKELOCK = 107;
114
115 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
116
117 private static final int OPEN_BOOKMARKS = 201;
118
119 private static final int EMPTY_MENU = -1;
120
Michael Kolb8233fac2010-10-26 16:08:53 -0700121 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700122 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700123 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;
150
151 private WakeLock mWakeLock;
152
153 private UrlHandler mUrlHandler;
154 private UploadHandler mUploadHandler;
155 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 private PageDialogsHandler mPageDialogsHandler;
157 private NetworkStateHandler mNetworkHandler;
Martijn Coenenb2f93552011-06-14 10:48:35 +0200158 private NfcHandler mNfcHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700159
Ben Murdoch8029a772010-11-16 11:58:21 +0000160 private Message mAutoFillSetupMessage;
161
Michael Kolb8233fac2010-10-26 16:08:53 -0700162 private boolean mShouldShowErrorConsole;
163
164 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
165
166 // FIXME, temp address onPrepareMenu performance problem.
167 // When we move everything out of view, we should rewrite this.
168 private int mCurrentMenuState = 0;
169 private int mMenuState = R.id.MAIN_MENU;
170 private int mOldMenuState = EMPTY_MENU;
171 private Menu mCachedMenu;
172
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 private boolean mMenuIsDown;
174
175 // For select and find, we keep track of the ActionMode so that
176 // finish() can be called as desired.
177 private ActionMode mActionMode;
178
179 /**
180 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
181 * of whether the configuration has changed. The first onMenuOpened call
182 * after a configuration change is simply a reopening of the same menu
183 * (i.e. mIconView did not change).
184 */
185 private boolean mConfigChanged;
186
187 /**
188 * Keeps track of whether the options menu is open. This is important in
189 * determining whether to show or hide the title bar overlay
190 */
191 private boolean mOptionsMenuOpen;
192
193 /**
194 * Whether or not the options menu is in its bigger, popup menu form. When
195 * true, we want the title bar overlay to be gone. When false, we do not.
196 * Only meaningful if mOptionsMenuOpen is true.
197 */
198 private boolean mExtendedMenuOpen;
199
200 private boolean mInLoad;
201
202 private boolean mActivityPaused = true;
203 private boolean mLoadStopped;
204
205 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500206 // Checks to see when the bookmarks database has changed, and updates the
207 // Tabs' notion of whether they represent bookmarked sites.
208 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700209 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700210
John Reck30b065e2011-07-19 10:58:05 -0700211 private boolean mSimulateActionBarOverlayMode;
212
Michael Kolb8233fac2010-10-26 16:08:53 -0700213 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();
230 mTabControl = new TabControl(this);
231 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700232 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
Michael Kolb14612442011-06-24 13:06:29 -0700233 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700234
235 mUrlHandler = new UrlHandler(this);
236 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700237 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200238 mNfcHandler = new NfcHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700239
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500241 mBookmarksObserver = new ContentObserver(mHandler) {
242 @Override
243 public void onChange(boolean selfChange) {
244 int size = mTabControl.getTabCount();
245 for (int i = 0; i < size; i++) {
246 mTabControl.getTab(i).updateBookmarkedStatus();
247 }
248 }
249
250 };
251 browser.getContentResolver().registerContentObserver(
252 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700253
254 mNetworkHandler = new NetworkStateHandler(mActivity, this);
255 // Start watching the default geolocation permissions
256 mSystemAllowGeolocationOrigins =
257 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
258 mSystemAllowGeolocationOrigins.start();
259
260 retainIconsOnStartup();
John Reck30b065e2011-07-19 10:58:05 -0700261 mSimulateActionBarOverlayMode = !BrowserActivity.isTablet(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 }
263
Patrick Scott7d50a932011-02-04 09:27:26 -0500264 void start(final Bundle icicle, final Intent intent) {
Guang Zhu9e78f512011-05-04 11:45:11 -0700265 boolean noCrashRecovery = intent.getBooleanExtra(NO_CRASH_RECOVERY, false);
266 if (icicle != null || noCrashRecovery) {
John Reck847b5322011-04-14 17:02:18 -0700267 doStart(icicle, intent);
268 } else {
269 mCrashRecoveryHandler.startRecovery(intent);
270 }
271 }
272
273 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700274 // Unless the last browser usage was within 24 hours, destroy any
275 // remaining incognito tabs.
276
277 Calendar lastActiveDate = icicle != null ?
278 (Calendar) icicle.getSerializable("lastActiveDate") : null;
279 Calendar today = Calendar.getInstance();
280 Calendar yesterday = Calendar.getInstance();
281 yesterday.add(Calendar.DATE, -1);
282
Patrick Scott7d50a932011-02-04 09:27:26 -0500283 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700284 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800285 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700286
Patrick Scott7d50a932011-02-04 09:27:26 -0500287 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700288 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500289 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000290
Michael Kolbc831b632011-05-11 09:30:34 -0700291 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500292 // Not able to restore so we go ahead and clear session cookies. We
293 // must do this before trying to login the user as we don't want to
294 // clear any session cookies set during login.
295 CookieManager.getInstance().removeSessionCookie();
296 }
297
Patrick Scottd43e75a2011-03-14 14:47:23 -0400298 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500299 new Runnable() {
300 @Override public void run() {
Michael Kolbc831b632011-05-11 09:30:34 -0700301 onPreloginFinished(icicle, intent, currentTabId, restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500302 }
303 });
304 }
305
Michael Kolbc831b632011-05-11 09:30:34 -0700306 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500307 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700308 if (currentTabId == -1) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700309 final Bundle extra = intent.getExtras();
310 // Create an initial tab.
311 // If the intent is ACTION_VIEW and data is not null, the Browser is
312 // invoked to view the content by another application. In this case,
313 // the tab will be close when exit.
Michael Kolb14612442011-06-24 13:06:29 -0700314 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
Michael Kolb7bcafde2011-05-09 13:55:59 -0700315 Tab t = null;
316 if (urlData.isEmpty()) {
317 t = openTabToHomePage();
318 } else {
319 t = openTab(urlData);
320 }
321 if (t != null) {
322 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
323 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700324 WebView webView = t.getWebView();
325 if (extra != null) {
326 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
327 if (scale > 0 && scale <= 1000) {
328 webView.setInitialScale(scale);
329 }
330 }
John Reckd8c74522011-06-14 08:45:00 -0700331 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700332 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700333 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500334 mUi.needsRestoreAllTabs());
Michael Kolb1bf23132010-11-19 12:55:12 -0800335 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700336 // TabControl.restoreState() will create a new tab even if
337 // restoring the state fails.
338 setActiveTab(mTabControl.getCurrentTab());
John Reckdb22ec42011-06-29 11:31:24 -0700339 // Handle the intent
340 mIntentHandler.onNewIntent(intent);
Michael Kolb8233fac2010-10-26 16:08:53 -0700341 }
342 // clear up the thumbnail directory, which is no longer used;
343 // ideally this should only be run once after an upgrade from
344 // a previous version of the browser
345 new ClearThumbnails().execute(mTabControl.getThumbnailDir()
346 .listFiles());
347 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700348 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700349 if (jsFlags.trim().length() != 0) {
350 getCurrentWebView().setJsFlags(jsFlags);
351 }
John Reck439c9a52010-12-14 10:04:39 -0800352 if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
353 bookmarksOrHistoryPicker(false);
354 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700355 }
356
Michael Kolb1514bb72010-11-22 09:11:48 -0800357 @Override
358 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700359 return mFactory;
360 }
361
362 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800363 public void onSetWebView(Tab tab, WebView view) {
364 mUi.onSetWebView(tab, view);
365 }
366
367 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800368 public void createSubWindow(Tab tab) {
369 endActionMode();
370 WebView mainView = tab.getWebView();
371 WebView subView = mFactory.createWebView((mainView == null)
372 ? false
373 : mainView.isPrivateBrowsingEnabled());
374 mUi.createSubWindow(tab, subView);
375 }
376
377 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700378 public Context getContext() {
379 return mActivity;
380 }
381
382 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700383 public Activity getActivity() {
384 return mActivity;
385 }
386
387 void setUi(UI ui) {
388 mUi = ui;
389 }
390
391 BrowserSettings getSettings() {
392 return mSettings;
393 }
394
395 IntentHandler getIntentHandler() {
396 return mIntentHandler;
397 }
398
399 @Override
400 public UI getUi() {
401 return mUi;
402 }
403
404 int getMaxTabs() {
405 return mActivity.getResources().getInteger(R.integer.max_tabs);
406 }
407
408 @Override
409 public TabControl getTabControl() {
410 return mTabControl;
411 }
412
Michael Kolb1bf23132010-11-19 12:55:12 -0800413 @Override
414 public List<Tab> getTabs() {
415 return mTabControl.getTabs();
416 }
417
Michael Kolb8233fac2010-10-26 16:08:53 -0700418 // Open the icon database and retain all the icons for visited sites.
Ben Murdoch9446b932010-11-25 16:20:14 +0000419 // This is done on a background thread so as not to stall startup.
Michael Kolb8233fac2010-10-26 16:08:53 -0700420 private void retainIconsOnStartup() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000421 // WebIconDatabase needs to be retrieved on the UI thread so that if
422 // it has not been created successfully yet the Handler is started on the
423 // UI thread.
424 new RetainIconsOnStartupTask(WebIconDatabase.getInstance()).execute();
425 }
426
427 private class RetainIconsOnStartupTask extends AsyncTask<Void, Void, Void> {
428 private WebIconDatabase mDb;
429
430 public RetainIconsOnStartupTask(WebIconDatabase db) {
431 mDb = db;
432 }
433
John Recka00cbbd2010-12-16 12:38:19 -0800434 @Override
Ben Murdoch9446b932010-11-25 16:20:14 +0000435 protected Void doInBackground(Void... unused) {
436 mDb.open(mActivity.getDir("icons", 0).getPath());
437 Cursor c = null;
438 try {
439 c = Browser.getAllBookmarks(mActivity.getContentResolver());
440 if (c.moveToFirst()) {
441 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
442 do {
443 String url = c.getString(urlIndex);
444 mDb.retainIconForPageUrl(url);
445 } while (c.moveToNext());
446 }
447 } catch (IllegalStateException e) {
448 Log.e(LOGTAG, "retainIconsOnStartup", e);
449 } finally {
450 if (c != null) c.close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700451 }
Ben Murdoch9446b932010-11-25 16:20:14 +0000452
453 return null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700454 }
455 }
456
457 private void startHandler() {
458 mHandler = new Handler() {
459
460 @Override
461 public void handleMessage(Message msg) {
462 switch (msg.what) {
463 case OPEN_BOOKMARKS:
464 bookmarksOrHistoryPicker(false);
465 break;
466 case FOCUS_NODE_HREF:
467 {
468 String url = (String) msg.getData().get("url");
469 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500470 String src = (String) msg.getData().get("src");
471 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700472 if (TextUtils.isEmpty(url)) {
473 break;
474 }
475 HashMap focusNodeMap = (HashMap) msg.obj;
476 WebView view = (WebView) focusNodeMap.get("webview");
477 // Only apply the action if the top window did not change.
478 if (getCurrentTopWebView() != view) {
479 break;
480 }
481 switch (msg.arg1) {
482 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700483 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700484 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500485 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700486 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500487 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500488 case R.id.open_newtab_context_menu_id:
489 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700490 openTab(url, parent,
491 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500492 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700493 case R.id.copy_link_context_menu_id:
494 copy(url);
495 break;
496 case R.id.save_link_context_menu_id:
497 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500498 DownloadHandler.onDownloadStartNoStream(
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000499 mActivity, url, null, null, null,
500 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700501 break;
502 }
503 break;
504 }
505
506 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700507 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700508 break;
509
510 case STOP_LOAD:
511 stopLoading();
512 break;
513
514 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700515 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700516 mWakeLock.release();
517 // if we reach here, Browser should be still in the
518 // background loading after WAKELOCK_TIMEOUT (5-min).
519 // To avoid burning the battery, stop loading.
520 mTabControl.stopAllLoading();
521 }
522 break;
523
524 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800525 Tab tab = (Tab) msg.obj;
526 if (tab != null) {
527 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700528 }
529 break;
530 }
531 }
532 };
533
534 }
535
John Reckef654f12011-07-12 16:42:08 -0700536 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200537 public Tab getCurrentTab() {
538 return mTabControl.getCurrentTab();
539 }
540
Michael Kolbba99c5d2010-11-29 14:57:41 -0800541 @Override
542 public void shareCurrentPage() {
543 shareCurrentPage(mTabControl.getCurrentTab());
544 }
545
546 private void shareCurrentPage(Tab tab) {
547 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800548 sharePage(mActivity, tab.getTitle(),
549 tab.getUrl(), tab.getFavicon(),
550 createScreenshot(tab.getWebView(),
551 getDesiredThumbnailWidth(mActivity),
552 getDesiredThumbnailHeight(mActivity)));
553 }
554 }
555
Michael Kolb8233fac2010-10-26 16:08:53 -0700556 /**
557 * Share a page, providing the title, url, favicon, and a screenshot. Uses
558 * an {@link Intent} to launch the Activity chooser.
559 * @param c Context used to launch a new Activity.
560 * @param title Title of the page. Stored in the Intent with
561 * {@link Intent#EXTRA_SUBJECT}
562 * @param url URL of the page. Stored in the Intent with
563 * {@link Intent#EXTRA_TEXT}
564 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
565 * with {@link Browser#EXTRA_SHARE_FAVICON}
566 * @param screenshot Bitmap of a screenshot of the page. Stored in the
567 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
568 */
569 static final void sharePage(Context c, String title, String url,
570 Bitmap favicon, Bitmap screenshot) {
571 Intent send = new Intent(Intent.ACTION_SEND);
572 send.setType("text/plain");
573 send.putExtra(Intent.EXTRA_TEXT, url);
574 send.putExtra(Intent.EXTRA_SUBJECT, title);
575 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
576 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
577 try {
578 c.startActivity(Intent.createChooser(send, c.getString(
579 R.string.choosertitle_sharevia)));
580 } catch(android.content.ActivityNotFoundException ex) {
581 // if no app handles it, do nothing
582 }
583 }
584
585 private void copy(CharSequence text) {
586 ClipboardManager cm = (ClipboardManager) mActivity
587 .getSystemService(Context.CLIPBOARD_SERVICE);
588 cm.setText(text);
589 }
590
591 // lifecycle
592
593 protected void onConfgurationChanged(Configuration config) {
594 mConfigChanged = true;
595 if (mPageDialogsHandler != null) {
596 mPageDialogsHandler.onConfigurationChanged(config);
597 }
598 mUi.onConfigurationChanged(config);
599 }
600
601 @Override
602 public void handleNewIntent(Intent intent) {
603 mIntentHandler.onNewIntent(intent);
604 }
605
606 protected void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800607 if (mUi.isCustomViewShowing()) {
608 hideCustomView();
609 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700610 if (mActivityPaused) {
611 Log.e(LOGTAG, "BrowserActivity is already paused.");
612 return;
613 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700614 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800615 Tab tab = mTabControl.getCurrentTab();
616 if (tab != null) {
617 tab.pause();
618 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700619 if (mWakeLock == null) {
620 PowerManager pm = (PowerManager) mActivity
621 .getSystemService(Context.POWER_SERVICE);
622 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
623 }
Michael Kolb70976932010-11-30 11:34:01 -0800624 mWakeLock.acquire();
625 mHandler.sendMessageDelayed(mHandler
626 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
627 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700628 }
629 mUi.onPause();
630 mNetworkHandler.onPause();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200631 mNfcHandler.onPause();
Michael Kolb8233fac2010-10-26 16:08:53 -0700632
633 WebView.disablePlatformNotifications();
John Reck378a4102011-06-09 16:23:01 -0700634 mCrashRecoveryHandler.backupState();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200635
Michael Kolb8233fac2010-10-26 16:08:53 -0700636 }
637
John Reckaed9c542011-05-27 16:08:53 -0700638 void onSaveInstanceState(Bundle outState, boolean saveImages) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 // the default implementation requires each view to have an id. As the
640 // browser handles the state itself and it doesn't use id for the views,
641 // don't call the default implementation. Otherwise it will trigger the
642 // warning like this, "couldn't save which view has focus because the
643 // focused view XXX has no id".
644
645 // Save all the tabs
John Reckaed9c542011-05-27 16:08:53 -0700646 mTabControl.saveState(outState, saveImages);
John Reck24f18262011-06-17 14:47:20 -0700647 if (!outState.isEmpty()) {
648 // Save time so that we know how old incognito tabs (if any) are.
649 outState.putSerializable("lastActiveDate", Calendar.getInstance());
650 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700651 }
652
653 void onResume() {
654 if (!mActivityPaused) {
655 Log.e(LOGTAG, "BrowserActivity is already resumed.");
656 return;
657 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700658 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800659 Tab current = mTabControl.getCurrentTab();
660 if (current != null) {
661 current.resume();
662 resumeWebViewTimers(current);
663 }
John Reckf57c0292011-07-21 18:15:39 -0700664 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200665
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 mUi.onResume();
667 mNetworkHandler.onResume();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200668 mNfcHandler.onResume();
Michael Kolb8233fac2010-10-26 16:08:53 -0700669 WebView.enablePlatformNotifications();
670 }
671
John Reckf57c0292011-07-21 18:15:39 -0700672 private void releaseWakeLock() {
673 if (mWakeLock != null && mWakeLock.isHeld()) {
674 mHandler.removeMessages(RELEASE_WAKELOCK);
675 mWakeLock.release();
676 }
677 }
678
Michael Kolb70976932010-11-30 11:34:01 -0800679 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800680 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800681 * @param tab guaranteed non-null
682 */
683 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700684 boolean inLoad = tab.inPageLoad();
685 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
686 CookieSyncManager.getInstance().startSync();
687 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100688 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700689 }
690 }
691
Michael Kolb70976932010-11-30 11:34:01 -0800692 /**
693 * Pause all WebView timers using the WebView of the given tab
694 * @param tab
695 * @return true if the timers are paused or tab is null
696 */
697 private boolean pauseWebViewTimers(Tab tab) {
698 if (tab == null) {
699 return true;
700 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700701 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100702 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700704 }
Michael Kolb70976932010-11-30 11:34:01 -0800705 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700706 }
707
708 void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800709 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700710 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
711 mUploadHandler = null;
712 }
713 if (mTabControl == null) return;
714 mUi.onDestroy();
715 // Remove the current tab and sub window
716 Tab t = mTabControl.getCurrentTab();
717 if (t != null) {
718 dismissSubWindow(t);
719 removeTab(t);
720 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500721 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700722 // Destroy all the tabs
723 mTabControl.destroy();
724 WebIconDatabase.getInstance().close();
725 // Stop watching the default geolocation permissions
726 mSystemAllowGeolocationOrigins.stop();
727 mSystemAllowGeolocationOrigins = null;
728 }
729
730 protected boolean isActivityPaused() {
731 return mActivityPaused;
732 }
733
734 protected void onLowMemory() {
735 mTabControl.freeMemory();
736 }
737
738 @Override
739 public boolean shouldShowErrorConsole() {
740 return mShouldShowErrorConsole;
741 }
742
743 protected void setShouldShowErrorConsole(boolean show) {
744 if (show == mShouldShowErrorConsole) {
745 // Nothing to do.
746 return;
747 }
748 mShouldShowErrorConsole = show;
749 Tab t = mTabControl.getCurrentTab();
750 if (t == null) {
751 // There is no current tab so we cannot toggle the error console
752 return;
753 }
754 mUi.setShouldShowErrorConsole(t, show);
755 }
756
757 @Override
758 public void stopLoading() {
759 mLoadStopped = true;
760 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700761 WebView w = getCurrentTopWebView();
762 w.stopLoading();
Michael Kolb8233fac2010-10-26 16:08:53 -0700763 mUi.onPageStopped(tab);
764 }
765
766 boolean didUserStopLoading() {
767 return mLoadStopped;
768 }
769
770 // WebViewController
771
772 @Override
John Reck324d4402011-01-11 16:56:42 -0800773 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700774
775 // We've started to load a new page. If there was a pending message
776 // to save a screenshot then we will now take the new page and save
777 // an incorrect screenshot. Therefore, remove any pending thumbnail
778 // messages from the queue.
779 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800780 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700781
782 // reset sync timer to avoid sync starts during loading a page
783 CookieSyncManager.getInstance().resetSync();
784
785 if (!mNetworkHandler.isNetworkUp()) {
786 view.setNetworkAvailable(false);
787 }
788
789 // when BrowserActivity just starts, onPageStarted may be called before
790 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
791 // to start the timer. As we won't switch tabs while an activity is in
792 // pause state, we can ensure calling resume and pause in pair.
793 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800794 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700795 }
796 mLoadStopped = false;
797 if (!mNetworkHandler.isNetworkUp()) {
798 mNetworkHandler.createAndShowNetworkDialog();
799 }
800 endActionMode();
801
John Reck30c714c2010-12-16 17:30:34 -0800802 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700803
John Reck324d4402011-01-11 16:56:42 -0800804 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700805 // update the bookmark database for favicon
806 maybeUpdateFavicon(tab, null, url, favicon);
807
808 Performance.tracePageStart(url);
809
810 // Performance probe
811 if (false) {
812 Performance.onPageStarted();
813 }
814
815 }
816
817 @Override
John Reck324d4402011-01-11 16:56:42 -0800818 public void onPageFinished(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800819 mUi.onTabDataChanged(tab);
John Reck324d4402011-01-11 16:56:42 -0800820 if (!tab.isPrivateBrowsingEnabled()
John Reckd8c74522011-06-14 08:45:00 -0700821 && !TextUtils.isEmpty(tab.getUrl())
822 && !tab.isSnapshot()) {
John Recka1696282011-07-08 14:10:37 -0700823 // Only update the bookmark screenshot if the user did not
824 // cancel the load early and there is not already
825 // a pending update for the tab.
Michael Kolb8233fac2010-10-26 16:08:53 -0700826 if (tab.inForeground() && !didUserStopLoading()
827 || !tab.inForeground()) {
John Recka1696282011-07-08 14:10:37 -0700828 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
829 mHandler.sendMessageDelayed(mHandler.obtainMessage(
830 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
831 500);
832 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700833 }
834 }
835 // pause the WebView timer and release the wake lock if it is finished
836 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800837 if (mActivityPaused && pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700838 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200840
Michael Kolb8233fac2010-10-26 16:08:53 -0700841 // Performance probe
842 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800843 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700844 }
845
846 Performance.tracePageFinished();
847 }
848
849 @Override
John Reck30c714c2010-12-16 17:30:34 -0800850 public void onProgressChanged(Tab tab) {
851 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700852
853 if (newProgress == 100) {
854 CookieSyncManager.getInstance().sync();
855 // onProgressChanged() may continue to be called after the main
856 // frame has finished loading, as any remaining sub frames continue
857 // to load. We'll only get called once though with newProgress as
858 // 100 when everything is loaded. (onPageFinished is called once
859 // when the main frame completes loading regardless of the state of
860 // any sub frames so calls to onProgressChanges may continue after
861 // onPageFinished has executed)
862 if (mInLoad) {
863 mInLoad = false;
864 updateInLoadMenuItems(mCachedMenu);
865 }
866 } else {
867 if (!mInLoad) {
868 // onPageFinished may have already been called but a subframe is
869 // still loading and updating the progress. Reset mInLoad and
870 // update the menu items.
871 mInLoad = true;
872 updateInLoadMenuItems(mCachedMenu);
873 }
874 }
John Reck30c714c2010-12-16 17:30:34 -0800875 mUi.onProgressChanged(tab);
876 }
877
878 @Override
879 public void onUpdatedLockIcon(Tab tab) {
880 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700881 }
882
883 @Override
884 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800885 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800886 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800887 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700888 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
889 return;
890 }
891 // Update the title in the history database if not in private browsing mode
892 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -0700893 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700894 }
895 }
896
897 @Override
898 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800899 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700900 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
901 }
902
903 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800904 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
905 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700906 }
907
908 @Override
909 public boolean shouldOverrideKeyEvent(KeyEvent event) {
910 if (mMenuIsDown) {
911 // only check shortcut key when MENU is held
912 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
913 event);
914 } else {
915 return false;
916 }
917 }
918
919 @Override
920 public void onUnhandledKeyEvent(KeyEvent event) {
921 if (!isActivityPaused()) {
922 if (event.getAction() == KeyEvent.ACTION_DOWN) {
923 mActivity.onKeyDown(event.getKeyCode(), event);
924 } else {
925 mActivity.onKeyUp(event.getKeyCode(), event);
926 }
927 }
928 }
929
930 @Override
John Reck324d4402011-01-11 16:56:42 -0800931 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700932 // Don't save anything in private browsing mode
933 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800934 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700935
John Reck324d4402011-01-11 16:56:42 -0800936 if (TextUtils.isEmpty(url)
937 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 return;
939 }
John Reckf57c0292011-07-21 18:15:39 -0700940 DataController.getInstance(mActivity).updateVisitedHistory(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700941 WebIconDatabase.getInstance().retainIconForPageUrl(url);
John Reck847b5322011-04-14 17:02:18 -0700942 if (!mActivityPaused) {
943 // Since we clear the state in onPause, don't backup the current
944 // state if we are already paused
945 mCrashRecoveryHandler.backupState();
946 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700947 }
948
949 @Override
950 public void getVisitedHistory(final ValueCallback<String[]> callback) {
951 AsyncTask<Void, Void, String[]> task =
952 new AsyncTask<Void, Void, String[]>() {
953 @Override
954 public String[] doInBackground(Void... unused) {
955 return Browser.getVisitedHistory(mActivity.getContentResolver());
956 }
957 @Override
958 public void onPostExecute(String[] result) {
959 callback.onReceiveValue(result);
960 }
961 };
962 task.execute();
963 }
964
965 @Override
966 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
967 final HttpAuthHandler handler, final String host,
968 final String realm) {
969 String username = null;
970 String password = null;
971
972 boolean reuseHttpAuthUsernamePassword
973 = handler.useHttpAuthUsernamePassword();
974
975 if (reuseHttpAuthUsernamePassword && view != null) {
976 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
977 if (credentials != null && credentials.length == 2) {
978 username = credentials[0];
979 password = credentials[1];
980 }
981 }
982
983 if (username != null && password != null) {
984 handler.proceed(username, password);
985 } else {
986 if (tab.inForeground()) {
987 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
988 } else {
989 handler.cancel();
990 }
991 }
992 }
993
994 @Override
995 public void onDownloadStart(Tab tab, String url, String userAgent,
996 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000997 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -0500998 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000999 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
1000 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001001 // This Tab was opened for the sole purpose of downloading a
1002 // file. Remove it.
1003 if (tab == mTabControl.getCurrentTab()) {
1004 // In this case, the Tab is still on top.
1005 goBackOnePageOrQuit();
1006 } else {
1007 // In this case, it is not.
1008 closeTab(tab);
1009 }
1010 }
1011 }
1012
1013 @Override
1014 public Bitmap getDefaultVideoPoster() {
1015 return mUi.getDefaultVideoPoster();
1016 }
1017
1018 @Override
1019 public View getVideoLoadingProgressView() {
1020 return mUi.getVideoLoadingProgressView();
1021 }
1022
1023 @Override
1024 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1025 SslError error) {
1026 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1027 }
1028
Patrick Scott92066772011-03-10 08:46:27 -05001029 @Override
1030 public void showAutoLogin(Tab tab) {
1031 assert tab.inForeground();
1032 // Update the title bar to show the auto-login request.
1033 mUi.showAutoLogin(tab);
1034 }
1035
1036 @Override
1037 public void hideAutoLogin(Tab tab) {
1038 assert tab.inForeground();
1039 mUi.hideAutoLogin(tab);
1040 }
1041
Michael Kolb8233fac2010-10-26 16:08:53 -07001042 // helper method
1043
1044 /*
1045 * Update the favorites icon if the private browsing isn't enabled and the
1046 * icon is valid.
1047 */
1048 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1049 final String url, Bitmap favicon) {
1050 if (favicon == null) {
1051 return;
1052 }
1053 if (!tab.isPrivateBrowsingEnabled()) {
1054 Bookmarks.updateFavicon(mActivity
1055 .getContentResolver(), originalUrl, url, favicon);
1056 }
1057 }
1058
Leon Scroggins4cd97792010-12-03 15:31:56 -05001059 @Override
1060 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001061 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001062 mUi.bookmarkedStatusHasChanged(tab);
1063 }
1064
Michael Kolb8233fac2010-10-26 16:08:53 -07001065 // end WebViewController
1066
1067 protected void pageUp() {
1068 getCurrentTopWebView().pageUp(false);
1069 }
1070
1071 protected void pageDown() {
1072 getCurrentTopWebView().pageDown(false);
1073 }
1074
1075 // callback from phone title bar
1076 public void editUrl() {
1077 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001078 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001079 }
1080
Michael Kolbcfa3af52010-12-14 10:36:11 -08001081 public void startVoiceSearch() {
1082 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1083 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1084 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1085 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1086 mActivity.getComponentName().flattenToString());
1087 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001088 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001089 mActivity.startActivity(intent);
1090 }
1091
Michael Kolb11d19782011-03-20 10:17:40 -07001092 @Override
1093 public void activateVoiceSearchMode(String title, List<String> results) {
1094 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001095 }
1096
1097 public void revertVoiceSearchMode(Tab tab) {
1098 mUi.revertVoiceTitleBar(tab);
1099 }
1100
Michael Kolb736990c2011-03-20 10:01:20 -07001101 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001102 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001103 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1104 }
1105
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001106 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001107 WebChromeClient.CustomViewCallback callback) {
1108 if (tab.inForeground()) {
1109 if (mUi.isCustomViewShowing()) {
1110 callback.onCustomViewHidden();
1111 return;
1112 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001113 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001114 // Save the menu state and set it to empty while the custom
1115 // view is showing.
1116 mOldMenuState = mMenuState;
1117 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001118 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001119 }
1120 }
1121
1122 @Override
1123 public void hideCustomView() {
1124 if (mUi.isCustomViewShowing()) {
1125 mUi.onHideCustomView();
1126 // Reset the old menu state.
1127 mMenuState = mOldMenuState;
1128 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001129 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001130 }
1131 }
1132
1133 protected void onActivityResult(int requestCode, int resultCode,
1134 Intent intent) {
1135 if (getCurrentTopWebView() == null) return;
1136 switch (requestCode) {
1137 case PREFERENCES_PAGE:
1138 if (resultCode == Activity.RESULT_OK && intent != null) {
1139 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001140 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001141 mTabControl.removeParentChildRelationShips();
1142 }
1143 }
1144 break;
1145 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001146 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001147 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001148 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001149 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001150 case AUTOFILL_SETUP:
1151 // Determine whether a profile was actually set up or not
1152 // and if so, send the message back to the WebTextView to
1153 // fill the form with the new profile.
1154 if (getSettings().getAutoFillProfile() != null) {
1155 mAutoFillSetupMessage.sendToTarget();
1156 mAutoFillSetupMessage = null;
1157 }
1158 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001159 case COMBO_VIEW:
1160 if (intent == null || resultCode != Activity.RESULT_OK) {
1161 break;
1162 }
1163 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1164 Tab t = getCurrentTab();
1165 Uri uri = intent.getData();
1166 loadUrl(t, uri.toString());
1167 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1168 String[] urls = intent.getStringArrayExtra(
1169 ComboViewActivity.EXTRA_OPEN_ALL);
1170 Tab parent = getCurrentTab();
1171 for (String url : urls) {
1172 parent = openTab(url, parent,
1173 !mSettings.openInBackground(), true);
1174 }
1175 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1176 long id = intent.getLongExtra(
1177 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1178 if (id >= 0) {
1179 createNewSnapshotTab(id, true);
1180 }
1181 }
1182 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001183 default:
1184 break;
1185 }
1186 getCurrentTopWebView().requestFocus();
1187 }
1188
1189 /**
1190 * Open the Go page.
1191 * @param startWithHistory If true, open starting on the history tab.
1192 * Otherwise, start with the bookmarks tab.
1193 */
1194 @Override
1195 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1196 if (mTabControl.getCurrentWebView() == null) {
1197 return;
1198 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001199 // clear action mode
1200 if (isInCustomActionMode()) {
1201 endActionMode();
1202 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001203 Bundle extras = new Bundle();
1204 // Disable opening in a new window if we have maxed out the windows
1205 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1206 !mTabControl.canCreateNewTab());
John Reck2bc80422011-06-30 15:11:49 -07001207 mUi.showComboView(startWithHistory
1208 ? ComboViews.History : ComboViews.Bookmarks, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001209 }
1210
1211 // combo view callbacks
1212
1213 /**
1214 * callback from ComboPage when clear history is requested
1215 */
1216 public void onRemoveParentChildRelationships() {
1217 mTabControl.removeParentChildRelationShips();
1218 }
1219
Michael Kolb8233fac2010-10-26 16:08:53 -07001220 // key handling
1221 protected void onBackKey() {
1222 if (!mUi.onBackKey()) {
1223 WebView subwindow = mTabControl.getCurrentSubWindow();
1224 if (subwindow != null) {
1225 if (subwindow.canGoBack()) {
1226 subwindow.goBack();
1227 } else {
1228 dismissSubWindow(mTabControl.getCurrentTab());
1229 }
1230 } else {
1231 goBackOnePageOrQuit();
1232 }
1233 }
1234 }
1235
Michael Kolb4bd767d2011-05-27 11:33:55 -07001236 protected boolean onMenuKey() {
1237 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001238 }
1239
Michael Kolb8233fac2010-10-26 16:08:53 -07001240 // menu handling and state
1241 // TODO: maybe put into separate handler
1242
1243 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001244 if (mMenuState == EMPTY_MENU) {
1245 return false;
1246 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001247 MenuInflater inflater = mActivity.getMenuInflater();
1248 inflater.inflate(R.menu.browser, menu);
1249 updateInLoadMenuItems(menu);
1250 // hold on to the menu reference here; it is used by the page callbacks
1251 // to update the menu based on loading state
1252 mCachedMenu = menu;
1253 return true;
1254 }
1255
1256 protected void onCreateContextMenu(ContextMenu menu, View v,
1257 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001258 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001259 return;
1260 }
1261 if (!(v instanceof WebView)) {
1262 return;
1263 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001264 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001265 WebView.HitTestResult result = webview.getHitTestResult();
1266 if (result == null) {
1267 return;
1268 }
1269
1270 int type = result.getType();
1271 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1272 Log.w(LOGTAG,
1273 "We should not show context menu when nothing is touched");
1274 return;
1275 }
1276 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1277 // let TextView handles context menu
1278 return;
1279 }
1280
1281 // Note, http://b/issue?id=1106666 is requesting that
1282 // an inflated menu can be used again. This is not available
1283 // yet, so inflate each time (yuk!)
1284 MenuInflater inflater = mActivity.getMenuInflater();
1285 inflater.inflate(R.menu.browsercontext, menu);
1286
1287 // Show the correct menu group
1288 final String extra = result.getExtra();
1289 menu.setGroupVisible(R.id.PHONE_MENU,
1290 type == WebView.HitTestResult.PHONE_TYPE);
1291 menu.setGroupVisible(R.id.EMAIL_MENU,
1292 type == WebView.HitTestResult.EMAIL_TYPE);
1293 menu.setGroupVisible(R.id.GEO_MENU,
1294 type == WebView.HitTestResult.GEO_TYPE);
1295 menu.setGroupVisible(R.id.IMAGE_MENU,
1296 type == WebView.HitTestResult.IMAGE_TYPE
1297 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1298 menu.setGroupVisible(R.id.ANCHOR_MENU,
1299 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1300 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001301 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1302 || type == WebView.HitTestResult.PHONE_TYPE
1303 || type == WebView.HitTestResult.EMAIL_TYPE
1304 || type == WebView.HitTestResult.GEO_TYPE;
1305 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1306 if (hitText) {
1307 menu.findItem(R.id.select_text_menu_id)
1308 .setOnMenuItemClickListener(new SelectText(webview));
1309 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001310 // Setup custom handling depending on the type
1311 switch (type) {
1312 case WebView.HitTestResult.PHONE_TYPE:
1313 menu.setHeaderTitle(Uri.decode(extra));
1314 menu.findItem(R.id.dial_context_menu_id).setIntent(
1315 new Intent(Intent.ACTION_VIEW, Uri
1316 .parse(WebView.SCHEME_TEL + extra)));
1317 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1318 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1319 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1320 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1321 addIntent);
1322 menu.findItem(R.id.copy_phone_context_menu_id)
1323 .setOnMenuItemClickListener(
1324 new Copy(extra));
1325 break;
1326
1327 case WebView.HitTestResult.EMAIL_TYPE:
1328 menu.setHeaderTitle(extra);
1329 menu.findItem(R.id.email_context_menu_id).setIntent(
1330 new Intent(Intent.ACTION_VIEW, Uri
1331 .parse(WebView.SCHEME_MAILTO + extra)));
1332 menu.findItem(R.id.copy_mail_context_menu_id)
1333 .setOnMenuItemClickListener(
1334 new Copy(extra));
1335 break;
1336
1337 case WebView.HitTestResult.GEO_TYPE:
1338 menu.setHeaderTitle(extra);
1339 menu.findItem(R.id.map_context_menu_id).setIntent(
1340 new Intent(Intent.ACTION_VIEW, Uri
1341 .parse(WebView.SCHEME_GEO
1342 + URLEncoder.encode(extra))));
1343 menu.findItem(R.id.copy_geo_context_menu_id)
1344 .setOnMenuItemClickListener(
1345 new Copy(extra));
1346 break;
1347
1348 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1349 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001350 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001351 // decide whether to show the open link in new tab option
1352 boolean showNewTab = mTabControl.canCreateNewTab();
1353 MenuItem newTabItem
1354 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001355 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001356 ? R.string.contextmenu_openlink_newwindow_background
1357 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001358 newTabItem.setVisible(showNewTab);
1359 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001360 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1361 newTabItem.setOnMenuItemClickListener(
1362 new MenuItem.OnMenuItemClickListener() {
1363 @Override
1364 public boolean onMenuItemClick(MenuItem item) {
1365 final HashMap<String, WebView> hrefMap =
1366 new HashMap<String, WebView>();
1367 hrefMap.put("webview", webview);
1368 final Message msg = mHandler.obtainMessage(
1369 FOCUS_NODE_HREF,
1370 R.id.open_newtab_context_menu_id,
1371 0, hrefMap);
1372 webview.requestFocusNodeHref(msg);
1373 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001374 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001375 });
1376 } else {
1377 newTabItem.setOnMenuItemClickListener(
1378 new MenuItem.OnMenuItemClickListener() {
1379 @Override
1380 public boolean onMenuItemClick(MenuItem item) {
1381 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001382 openTab(extra, parent,
1383 !mSettings.openInBackground(),
1384 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001385 return true;
1386 }
1387 });
1388 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001389 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001390 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1391 break;
1392 }
1393 // otherwise fall through to handle image part
1394 case WebView.HitTestResult.IMAGE_TYPE:
1395 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1396 menu.setHeaderTitle(extra);
1397 }
1398 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1399 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1400 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001401 setOnMenuItemClickListener(
1402 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001403 menu.findItem(R.id.set_wallpaper_context_menu_id).
1404 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1405 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001406 break;
1407
1408 default:
1409 Log.w(LOGTAG, "We should not get here.");
1410 break;
1411 }
1412 //update the ui
1413 mUi.onContextMenuCreated(menu);
1414 }
1415
1416 /**
1417 * As the menu can be open when loading state changes
1418 * we must manually update the state of the stop/reload menu
1419 * item
1420 */
1421 private void updateInLoadMenuItems(Menu menu) {
1422 if (menu == null) {
1423 return;
1424 }
1425 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1426 MenuItem src = mInLoad ?
1427 menu.findItem(R.id.stop_menu_id):
1428 menu.findItem(R.id.reload_menu_id);
1429 if (src != null) {
1430 dest.setIcon(src.getIcon());
1431 dest.setTitle(src.getTitle());
1432 }
1433 }
1434
John Reckb3417f02011-01-14 11:01:05 -08001435 boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001436 // Note: setVisible will decide whether an item is visible; while
1437 // setEnabled() will decide whether an item is enabled, which also means
1438 // whether the matching shortcut key will function.
1439 switch (mMenuState) {
1440 case EMPTY_MENU:
1441 if (mCurrentMenuState != mMenuState) {
1442 menu.setGroupVisible(R.id.MAIN_MENU, false);
1443 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1444 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1445 }
1446 break;
1447 default:
1448 if (mCurrentMenuState != mMenuState) {
1449 menu.setGroupVisible(R.id.MAIN_MENU, true);
1450 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1451 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1452 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001453 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001454 break;
1455 }
1456 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001457 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001458 }
1459
Michael Kolb4bf79712011-07-14 14:18:12 -07001460 @Override
1461 public void updateMenuState(Tab tab, Menu menu) {
1462 boolean canGoBack = false;
1463 boolean canGoForward = false;
1464 boolean isHome = false;
1465 if (tab != null) {
1466 canGoBack = tab.canGoBack();
1467 canGoForward = tab.canGoForward();
1468 isHome = mSettings.getHomePage().equals(tab.getUrl());
1469 }
1470 final MenuItem back = menu.findItem(R.id.back_menu_id);
1471 back.setEnabled(canGoBack);
1472
1473 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1474 home.setEnabled(!isHome);
1475
1476 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1477 forward.setEnabled(canGoForward);
1478
1479 final MenuItem source = menu.findItem(mInLoad ? R.id.stop_menu_id : R.id.reload_menu_id);
1480 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001481 if (source != null && dest != null) {
1482 dest.setTitle(source.getTitle());
1483 dest.setIcon(source.getIcon());
1484 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001485
1486 // decide whether to show the share link option
1487 PackageManager pm = mActivity.getPackageManager();
1488 Intent send = new Intent(Intent.ACTION_SEND);
1489 send.setType("text/plain");
1490 ResolveInfo ri = pm.resolveActivity(send,
1491 PackageManager.MATCH_DEFAULT_ONLY);
1492 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1493
1494 boolean isNavDump = mSettings.enableNavDump();
1495 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1496 nav.setVisible(isNavDump);
1497 nav.setEnabled(isNavDump);
1498
1499 boolean showDebugSettings = mSettings.isDebugEnabled();
1500 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1501 counter.setVisible(showDebugSettings);
1502 counter.setEnabled(showDebugSettings);
1503
1504 MenuItem saveSnapshot = menu.findItem(R.id.save_snapshot_menu_id);
1505 saveSnapshot.setVisible(tab != null && !tab.isSnapshot());
1506 }
1507
Michael Kolb8233fac2010-10-26 16:08:53 -07001508 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001509 if (null == getCurrentTopWebView()) {
1510 return false;
1511 }
1512 if (mMenuIsDown) {
1513 // The shortcut action consumes the MENU. Even if it is still down,
1514 // it won't trigger the next shortcut action. In the case of the
1515 // shortcut action triggering a new activity, like Bookmarks, we
1516 // won't get onKeyUp for MENU. So it is important to reset it here.
1517 mMenuIsDown = false;
1518 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001519 if (mUi.onOptionsItemSelected(item)) {
1520 // ui callback handled it
1521 return true;
1522 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001523 switch (item.getItemId()) {
1524 // -- Main menu
1525 case R.id.new_tab_menu_id:
1526 openTabToHomePage();
1527 break;
1528
1529 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001530 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001531 break;
1532
1533 case R.id.goto_menu_id:
1534 editUrl();
1535 break;
1536
1537 case R.id.bookmarks_menu_id:
1538 bookmarksOrHistoryPicker(false);
1539 break;
1540
Michael Kolb8233fac2010-10-26 16:08:53 -07001541 case R.id.add_bookmark_menu_id:
John Reckd3e4d5b2011-07-13 15:48:43 -07001542 mActivity.startActivity(createBookmarkCurrentPageIntent(false));
Michael Kolb8233fac2010-10-26 16:08:53 -07001543 break;
1544
1545 case R.id.stop_reload_menu_id:
1546 if (mInLoad) {
1547 stopLoading();
1548 } else {
1549 getCurrentTopWebView().reload();
1550 }
1551 break;
1552
1553 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001554 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001555 break;
1556
1557 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001558 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001559 break;
1560
1561 case R.id.close_menu_id:
1562 // Close the subwindow if it exists.
1563 if (mTabControl.getCurrentSubWindow() != null) {
1564 dismissSubWindow(mTabControl.getCurrentTab());
1565 break;
1566 }
1567 closeCurrentTab();
1568 break;
1569
1570 case R.id.homepage_menu_id:
1571 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001572 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001573 break;
1574
1575 case R.id.preferences_menu_id:
1576 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1577 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1578 getCurrentTopWebView().getUrl());
1579 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1580 break;
1581
1582 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001583 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001584 break;
1585
John Reck2bc80422011-06-30 15:11:49 -07001586 case R.id.save_snapshot_menu_id:
1587 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001588 if (source == null) break;
John Reckd8c74522011-06-14 08:45:00 -07001589 final ContentResolver cr = mActivity.getContentResolver();
1590 final ContentValues values = source.createSnapshotValues();
John Reck2bc80422011-06-30 15:11:49 -07001591 if (values != null) {
1592 new AsyncTask<Tab, Void, Long>() {
John Reckd8c74522011-06-14 08:45:00 -07001593
John Reck2bc80422011-06-30 15:11:49 -07001594 @Override
1595 protected Long doInBackground(Tab... params) {
1596 Uri result = cr.insert(Snapshots.CONTENT_URI, values);
1597 long id = ContentUris.parseId(result);
1598 return id;
John Reckd8c74522011-06-14 08:45:00 -07001599 }
John Reckf33b1632011-06-04 20:00:23 -07001600
John Reck2bc80422011-06-30 15:11:49 -07001601 @Override
1602 protected void onPostExecute(Long id) {
1603 Bundle b = new Bundle();
1604 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1605 mUi.showComboView(ComboViews.Snapshots, b);
1606 };
1607 }.execute(source);
1608 } else {
1609 Toast.makeText(mActivity, R.string.snapshot_failed,
Leon Scrogginsac993842011-02-02 12:54:07 -05001610 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001611 }
Leon Scrogginsac993842011-02-02 12:54:07 -05001612 break;
1613
Michael Kolb8233fac2010-10-26 16:08:53 -07001614 case R.id.page_info_menu_id:
Huahui Wuae0c0412011-06-28 10:17:05 -07001615 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
Michael Kolb8233fac2010-10-26 16:08:53 -07001616 break;
1617
1618 case R.id.classic_history_menu_id:
1619 bookmarksOrHistoryPicker(true);
1620 break;
1621
1622 case R.id.title_bar_share_page_url:
1623 case R.id.share_page_menu_id:
1624 Tab currentTab = mTabControl.getCurrentTab();
1625 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001626 return false;
1627 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001628 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001629 break;
1630
1631 case R.id.dump_nav_menu_id:
1632 getCurrentTopWebView().debugDump();
1633 break;
1634
1635 case R.id.dump_counters_menu_id:
1636 getCurrentTopWebView().dumpV8Counters();
1637 break;
1638
1639 case R.id.zoom_in_menu_id:
1640 getCurrentTopWebView().zoomIn();
1641 break;
1642
1643 case R.id.zoom_out_menu_id:
1644 getCurrentTopWebView().zoomOut();
1645 break;
1646
1647 case R.id.view_downloads_menu_id:
1648 viewDownloads();
1649 break;
1650
1651 case R.id.window_one_menu_id:
1652 case R.id.window_two_menu_id:
1653 case R.id.window_three_menu_id:
1654 case R.id.window_four_menu_id:
1655 case R.id.window_five_menu_id:
1656 case R.id.window_six_menu_id:
1657 case R.id.window_seven_menu_id:
1658 case R.id.window_eight_menu_id:
1659 {
1660 int menuid = item.getItemId();
1661 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1662 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1663 Tab desiredTab = mTabControl.getTab(id);
1664 if (desiredTab != null &&
1665 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001666 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001667 }
1668 break;
1669 }
1670 }
1671 }
1672 break;
1673
1674 default:
1675 return false;
1676 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001677 return true;
1678 }
1679
1680 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001681 // Let the History and Bookmark fragments handle menus they created.
1682 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1683 return false;
1684 }
1685
Michael Kolb8233fac2010-10-26 16:08:53 -07001686 int id = item.getItemId();
1687 boolean result = true;
1688 switch (id) {
1689 // For the context menu from the title bar
1690 case R.id.title_bar_copy_page_url:
1691 Tab currentTab = mTabControl.getCurrentTab();
1692 if (null == currentTab) {
1693 result = false;
1694 break;
1695 }
1696 WebView mainView = currentTab.getWebView();
1697 if (null == mainView) {
1698 result = false;
1699 break;
1700 }
1701 copy(mainView.getUrl());
1702 break;
1703 // -- Browser context menu
1704 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001705 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001706 case R.id.copy_link_context_menu_id:
1707 final WebView webView = getCurrentTopWebView();
1708 if (null == webView) {
1709 result = false;
1710 break;
1711 }
1712 final HashMap<String, WebView> hrefMap =
1713 new HashMap<String, WebView>();
1714 hrefMap.put("webview", webView);
1715 final Message msg = mHandler.obtainMessage(
1716 FOCUS_NODE_HREF, id, 0, hrefMap);
1717 webView.requestFocusNodeHref(msg);
1718 break;
1719
1720 default:
1721 // For other context menus
1722 result = onOptionsItemSelected(item);
1723 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001724 return result;
1725 }
1726
1727 /**
1728 * support programmatically opening the context menu
1729 */
1730 public void openContextMenu(View view) {
1731 mActivity.openContextMenu(view);
1732 }
1733
1734 /**
1735 * programmatically open the options menu
1736 */
1737 public void openOptionsMenu() {
1738 mActivity.openOptionsMenu();
1739 }
1740
1741 public boolean onMenuOpened(int featureId, Menu menu) {
1742 if (mOptionsMenuOpen) {
1743 if (mConfigChanged) {
1744 // We do not need to make any changes to the state of the
1745 // title bar, since the only thing that happened was a
1746 // change in orientation
1747 mConfigChanged = false;
1748 } else {
1749 if (!mExtendedMenuOpen) {
1750 mExtendedMenuOpen = true;
1751 mUi.onExtendedMenuOpened();
1752 } else {
1753 // Switching the menu back to icon view, so show the
1754 // title bar once again.
1755 mExtendedMenuOpen = false;
1756 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001757 }
1758 }
1759 } else {
1760 // The options menu is closed, so open it, and show the title
1761 mOptionsMenuOpen = true;
1762 mConfigChanged = false;
1763 mExtendedMenuOpen = false;
1764 mUi.onOptionsMenuOpened();
1765 }
1766 return true;
1767 }
1768
1769 public void onOptionsMenuClosed(Menu menu) {
1770 mOptionsMenuOpen = false;
1771 mUi.onOptionsMenuClosed(mInLoad);
1772 }
1773
1774 public void onContextMenuClosed(Menu menu) {
1775 mUi.onContextMenuClosed(menu, mInLoad);
1776 }
1777
1778 // Helper method for getting the top window.
1779 @Override
1780 public WebView getCurrentTopWebView() {
1781 return mTabControl.getCurrentTopWebView();
1782 }
1783
1784 @Override
1785 public WebView getCurrentWebView() {
1786 return mTabControl.getCurrentWebView();
1787 }
1788
1789 /*
1790 * This method is called as a result of the user selecting the options
1791 * menu to see the download window. It shows the download window on top of
1792 * the current window.
1793 */
1794 void viewDownloads() {
1795 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1796 mActivity.startActivity(intent);
1797 }
1798
John Reck30b065e2011-07-19 10:58:05 -07001799 int getActionModeHeight() {
1800 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1801 new int[] { android.R.attr.actionBarSize });
1802 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1803 actionBarSizeTypedArray.recycle();
1804 return size;
1805 }
1806
Michael Kolb8233fac2010-10-26 16:08:53 -07001807 // action mode
1808
1809 void onActionModeStarted(ActionMode mode) {
1810 mUi.onActionModeStarted(mode);
1811 mActionMode = mode;
John Reck30b065e2011-07-19 10:58:05 -07001812 if (mSimulateActionBarOverlayMode) {
1813 WebView web = getCurrentWebView();
1814 // Simulate overlay mode by scrolling the webview the amount it will be
1815 // pushed down. Actual overlay mode doesn't work for us as otherwise
1816 // the CAB will, well, overlay the content, which breaks things like
1817 // find on page.
1818 int scrollBy = getActionModeHeight();
1819 web.scrollBy(0, scrollBy);
1820 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001821 }
1822
1823 /*
1824 * True if a custom ActionMode (i.e. find or select) is in use.
1825 */
1826 @Override
1827 public boolean isInCustomActionMode() {
1828 return mActionMode != null;
1829 }
1830
1831 /*
1832 * End the current ActionMode.
1833 */
1834 @Override
1835 public void endActionMode() {
1836 if (mActionMode != null) {
1837 mActionMode.finish();
1838 }
1839 }
1840
1841 /*
1842 * Called by find and select when they are finished. Replace title bars
1843 * as necessary.
1844 */
1845 public void onActionModeFinished(ActionMode mode) {
1846 if (!isInCustomActionMode()) return;
1847 mUi.onActionModeFinished(mInLoad);
1848 mActionMode = null;
John Reck30b065e2011-07-19 10:58:05 -07001849 if (mSimulateActionBarOverlayMode) {
1850 WebView web = getCurrentWebView();
1851 int scrollBy = getActionModeHeight();
1852 web.scrollBy(0, -scrollBy);
1853 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001854 }
1855
1856 boolean isInLoad() {
1857 return mInLoad;
1858 }
1859
1860 // bookmark handling
1861
1862 /**
1863 * add the current page as a bookmark to the given folder id
1864 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07001865 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001866 * bookmarked, and if it is, edit that bookmark. If false, and
1867 * the site is already bookmarked, do not attempt to edit the
1868 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001869 */
1870 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07001871 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001872 Intent i = new Intent(mActivity,
1873 AddBookmarkPage.class);
1874 WebView w = getCurrentTopWebView();
1875 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1876 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1877 String touchIconUrl = w.getTouchIconUrl();
1878 if (touchIconUrl != null) {
1879 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1880 WebSettings settings = w.getSettings();
1881 if (settings != null) {
1882 i.putExtra(AddBookmarkPage.USER_AGENT,
1883 settings.getUserAgentString());
1884 }
1885 }
1886 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1887 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1888 getDesiredThumbnailHeight(mActivity)));
1889 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07001890 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001891 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1892 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001893 // Put the dialog at the upper right of the screen, covering the
1894 // star on the title bar.
1895 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07001896 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07001897 }
1898
1899 // file chooser
1900 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1901 mUploadHandler = new UploadHandler(this);
1902 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1903 }
1904
1905 // thumbnails
1906
1907 /**
1908 * Return the desired width for thumbnail screenshots, which are stored in
1909 * the database, and used on the bookmarks screen.
1910 * @param context Context for finding out the density of the screen.
1911 * @return desired width for thumbnail screenshot.
1912 */
1913 static int getDesiredThumbnailWidth(Context context) {
1914 return context.getResources().getDimensionPixelOffset(
1915 R.dimen.bookmarkThumbnailWidth);
1916 }
1917
1918 /**
1919 * Return the desired height for thumbnail screenshots, which are stored in
1920 * the database, and used on the bookmarks screen.
1921 * @param context Context for finding out the density of the screen.
1922 * @return desired height for thumbnail screenshot.
1923 */
1924 static int getDesiredThumbnailHeight(Context context) {
1925 return context.getResources().getDimensionPixelOffset(
1926 R.dimen.bookmarkThumbnailHeight);
1927 }
1928
Michael Kolb1acef692011-03-08 14:12:06 -08001929 static Bitmap createScreenshot(Tab tab, int width, int height) {
1930 if ((tab != null) && (tab.getWebView() != null)) {
1931 return createScreenshot(tab.getWebView(), width, height);
1932 }
1933 return null;
1934 }
1935
John Reck8cc92352011-07-06 17:41:52 -07001936 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08001937 // We render to a bitmap 2x the desired size so that we can then
1938 // re-scale it with filtering since canvas.scale doesn't filter
1939 // This helps reduce aliasing at the cost of being slightly blurry
1940 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07001941 Picture thumbnail = view.capturePicture();
1942 if (thumbnail == null) {
1943 return null;
1944 }
John Reck5c6ac2f2011-01-05 10:18:03 -08001945 width *= filter_scale;
1946 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07001947 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1948 Canvas canvas = new Canvas(bm);
1949 // May need to tweak these values to determine what is the
1950 // best scale factor
1951 int thumbnailWidth = thumbnail.getWidth();
1952 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08001953 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08001954 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08001955 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07001956 } else {
1957 return null;
1958 }
John Reckfe49ab42010-11-16 17:09:37 -08001959
Michael Kolbeb95db42011-03-03 10:38:40 -08001960 float scaleFactorY = (float) height / (float)thumbnailHeight;
1961 if (scaleFactorY > scaleFactor) {
1962 // The picture is narrower than the requested AR
1963 // Center the thumnail and crop the sides
1964 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08001965 float wx = (thumbnailWidth * scaleFactor) - width;
1966 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07001967 }
1968
John Reckfe49ab42010-11-16 17:09:37 -08001969 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07001970
1971 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08001972 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
1973 height / filter_scale, true);
1974 bm.recycle();
1975 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07001976 }
1977
John Reck34ef2672011-02-10 11:30:55 -08001978 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001979 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07001980 // FIXME: Would like to make sure there is actually something to
1981 // draw, but the API for that (WebViewCore.pictureReady()) is not
1982 // currently accessible here.
1983
John Reck34ef2672011-02-10 11:30:55 -08001984 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08001985 if (view == null) {
1986 // Tab was destroyed
1987 return;
1988 }
John Reck34ef2672011-02-10 11:30:55 -08001989 final String url = tab.getUrl();
1990 final String originalUrl = view.getOriginalUrl();
1991
1992 if (TextUtils.isEmpty(url)) {
1993 return;
1994 }
1995
1996 // Only update thumbnails for web urls (http(s)://), not for
1997 // about:, javascript:, data:, etc...
1998 // Unless it is a bookmarked site, then always update
1999 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2000 return;
2001 }
2002
Michael Kolb8233fac2010-10-26 16:08:53 -07002003 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2004 getDesiredThumbnailHeight(mActivity));
2005 if (bm == null) {
2006 return;
2007 }
2008
2009 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002010 new AsyncTask<Void, Void, Void>() {
2011 @Override
2012 protected Void doInBackground(Void... unused) {
2013 Cursor cursor = null;
2014 try {
2015 // TODO: Clean this up
2016 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2017 if (cursor != null && cursor.moveToFirst()) {
2018 final ByteArrayOutputStream os =
2019 new ByteArrayOutputStream();
2020 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002021
John Reck34ef2672011-02-10 11:30:55 -08002022 ContentValues values = new ContentValues();
2023 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002024
John Reck34ef2672011-02-10 11:30:55 -08002025 do {
John Reck617fd832011-02-16 14:35:59 -08002026 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002027 cr.update(Images.CONTENT_URI, values, null, null);
2028 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002029 }
John Reck34ef2672011-02-10 11:30:55 -08002030 } catch (IllegalStateException e) {
2031 // Ignore
2032 } finally {
2033 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002034 }
John Reck34ef2672011-02-10 11:30:55 -08002035 return null;
2036 }
2037 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002038 }
2039
2040 private class Copy implements OnMenuItemClickListener {
2041 private CharSequence mText;
2042
2043 public boolean onMenuItemClick(MenuItem item) {
2044 copy(mText);
2045 return true;
2046 }
2047
2048 public Copy(CharSequence toCopy) {
2049 mText = toCopy;
2050 }
2051 }
2052
Leon Scroggins63c02662010-11-18 15:16:27 -05002053 private static class Download implements OnMenuItemClickListener {
2054 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002055 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002056 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002057
2058 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002059 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002060 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002061 return true;
2062 }
2063
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002064 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002065 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002066 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002067 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002068 }
2069 }
2070
Cary Clark8974d282010-11-22 10:46:05 -05002071 private static class SelectText implements OnMenuItemClickListener {
2072 private WebView mWebView;
2073
2074 public boolean onMenuItemClick(MenuItem item) {
2075 if (mWebView != null) {
2076 return mWebView.selectText();
2077 }
2078 return false;
2079 }
2080
2081 public SelectText(WebView webView) {
2082 mWebView = webView;
2083 }
2084
2085 }
2086
Michael Kolb8233fac2010-10-26 16:08:53 -07002087 /********************** TODO: UI stuff *****************************/
2088
2089 // these methods have been copied, they still need to be cleaned up
2090
2091 /****************** tabs ***************************************************/
2092
2093 // basic tab interactions:
2094
2095 // it is assumed that tabcontrol already knows about the tab
2096 protected void addTab(Tab tab) {
2097 mUi.addTab(tab);
2098 }
2099
2100 protected void removeTab(Tab tab) {
2101 mUi.removeTab(tab);
2102 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002103 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002104 }
2105
Michael Kolbf2055602011-04-09 17:20:03 -07002106 @Override
2107 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002108 // monkey protection against delayed start
2109 if (tab != null) {
2110 mTabControl.setCurrentTab(tab);
2111 // the tab is guaranteed to have a webview after setCurrentTab
2112 mUi.setActiveTab(tab);
2113 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002114 }
2115
2116 protected void closeEmptyChildTab() {
2117 Tab current = mTabControl.getCurrentTab();
2118 if (current != null
2119 && current.getWebView().copyBackForwardList().getSize() == 0) {
Michael Kolbc831b632011-05-11 09:30:34 -07002120 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002121 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002122 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002123 closeTab(current);
2124 }
2125 }
2126 }
2127
John Reck26b18322011-06-21 13:08:58 -07002128 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002129 // Dismiss the subwindow if applicable.
2130 dismissSubWindow(appTab);
2131 // Since we might kill the WebView, remove it from the
2132 // content view first.
2133 mUi.detachTab(appTab);
2134 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002135 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002136 // TODO: analyze why the remove and add are necessary
2137 mUi.attachTab(appTab);
2138 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002139 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002140 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002141 } else {
2142 // If the tab was the current tab, we have to attach
2143 // it to the view system again.
2144 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002145 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002146 }
2147 }
2148
2149 // Remove the sub window if it exists. Also called by TabControl when the
2150 // user clicks the 'X' to dismiss a sub window.
2151 public void dismissSubWindow(Tab tab) {
2152 removeSubWindow(tab);
2153 // dismiss the subwindow. This will destroy the WebView.
2154 tab.dismissSubWindow();
2155 getCurrentTopWebView().requestFocus();
2156 }
2157
2158 @Override
2159 public void removeSubWindow(Tab t) {
2160 if (t.getSubWebView() != null) {
2161 mUi.removeSubWindow(t.getSubViewContainer());
2162 }
2163 }
2164
2165 @Override
2166 public void attachSubWindow(Tab tab) {
2167 if (tab.getSubWebView() != null) {
2168 mUi.attachSubWindow(tab.getSubViewContainer());
2169 getCurrentTopWebView().requestFocus();
2170 }
2171 }
2172
Mathew Inwood29721c22011-06-29 17:55:24 +01002173 private Tab showPreloadedTab(final UrlData urlData) {
2174 if (!urlData.isPreloaded()) {
2175 return null;
2176 }
2177 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2178 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2179 if (sbQuery != null) {
2180 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
2181 // Could not submit query. Fallback to regular tab creation
2182 tabControl.destroy();
2183 return null;
2184 }
2185 }
2186 Tab t = tabControl.getTab();
2187 mTabControl.addPreloadedTab(t);
2188 addTab(t);
2189 setActiveTab(t);
2190 return t;
2191 }
2192
Michael Kolb7bcafde2011-05-09 13:55:59 -07002193 // open a non inconito tab with the given url data
2194 // and set as active tab
2195 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002196 Tab tab = showPreloadedTab(urlData);
2197 if (tab == null) {
2198 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002199 if ((tab != null) && !urlData.isEmpty()) {
2200 loadUrlDataIn(tab, urlData);
2201 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002202 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002203 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002204 }
2205
Michael Kolb843510f2010-12-09 10:51:49 -08002206 @Override
2207 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002208 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002209 }
2210
Michael Kolb8233fac2010-10-26 16:08:53 -07002211 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002212 public Tab openIncognitoTab() {
2213 return openTab(INCOGNITO_URI, true, true, false);
2214 }
2215
2216 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002217 public Tab openTab(String url, boolean incognito, boolean setActive,
2218 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002219 return openTab(url, incognito, setActive, useCurrent, null);
2220 }
2221
2222 @Override
2223 public Tab openTab(String url, Tab parent, boolean setActive,
2224 boolean useCurrent) {
2225 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2226 setActive, useCurrent, parent);
2227 }
2228
2229 public Tab openTab(String url, boolean incognito, boolean setActive,
2230 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002231 Tab tab = createNewTab(incognito, setActive, useCurrent);
2232 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002233 if (parent != null && parent != tab) {
2234 parent.addChildTab(tab);
2235 }
Michael Kolb519d2282011-05-09 17:03:19 -07002236 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002237 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002238 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002239 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002240 return tab;
2241 }
2242
2243 // this method will attempt to create a new tab
2244 // incognito: private browsing tab
2245 // setActive: ste tab as current tab
2246 // useCurrent: if no new tab can be created, return current tab
2247 private Tab createNewTab(boolean incognito, boolean setActive,
2248 boolean useCurrent) {
2249 Tab tab = null;
2250 if (mTabControl.canCreateNewTab()) {
2251 tab = mTabControl.createNewTab(incognito);
2252 addTab(tab);
2253 if (setActive) {
2254 setActiveTab(tab);
2255 }
2256 } else {
2257 if (useCurrent) {
2258 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002259 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002260 } else {
2261 mUi.showMaxTabsWarning();
2262 }
2263 }
2264 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002265 }
2266
John Reck2bc80422011-06-30 15:11:49 -07002267 @Override
2268 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2269 SnapshotTab tab = null;
2270 if (mTabControl.canCreateNewTab()) {
2271 tab = mTabControl.createSnapshotTab(snapshotId);
2272 addTab(tab);
2273 if (setActive) {
2274 setActiveTab(tab);
2275 }
2276 } else {
2277 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002278 }
2279 return tab;
2280 }
2281
Michael Kolb8233fac2010-10-26 16:08:53 -07002282 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002283 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002284 * @return boolean True if we successfully switched to a different tab. If
2285 * the indexth tab is null, or if that tab is the same as
2286 * the current one, return false.
2287 */
2288 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002289 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002290 Tab currentTab = mTabControl.getCurrentTab();
2291 if (tab == null || tab == currentTab) {
2292 return false;
2293 }
2294 setActiveTab(tab);
2295 return true;
2296 }
2297
2298 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002299 public void closeCurrentTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002300 if (mTabControl.getTabCount() == 1) {
John Reckf630b552011-07-17 14:18:51 -07002301 CrashRecoveryHandler.clearState(mActivity);
John Reck958b2422010-12-03 17:56:17 -08002302 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002303 return;
2304 }
Michael Kolbc831b632011-05-11 09:30:34 -07002305 final Tab current = mTabControl.getCurrentTab();
2306 final int pos = mTabControl.getCurrentPosition();
2307 Tab newTab = current.getParent();
2308 if (newTab == null) {
2309 newTab = mTabControl.getTab(pos + 1);
2310 if (newTab == null) {
2311 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002312 }
2313 }
Michael Kolbc831b632011-05-11 09:30:34 -07002314 if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002315 // Close window
2316 closeTab(current);
2317 }
2318 }
2319
2320 /**
2321 * Close the tab, remove its associated title bar, and adjust mTabControl's
2322 * current tab to a valid value.
2323 */
2324 @Override
2325 public void closeTab(Tab tab) {
Michael Kolb2d59c322011-01-25 13:18:55 -08002326 removeTab(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002327 }
2328
Michael Kolb8233fac2010-10-26 16:08:53 -07002329 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002330 protected void loadUrlFromContext(String url) {
2331 Tab tab = getCurrentTab();
2332 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002333 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002334 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002335 url = UrlUtils.smartUrlFilter(url);
2336 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002337 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002338 }
2339 }
2340 }
2341
2342 /**
2343 * Load the URL into the given WebView and update the title bar
2344 * to reflect the new load. Call this instead of WebView.loadUrl
2345 * directly.
2346 * @param view The WebView used to load url.
2347 * @param url The URL to load.
2348 */
John Reck71e51422011-07-01 16:49:28 -07002349 @Override
2350 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002351 loadUrl(tab, url, null);
2352 }
2353
2354 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2355 if (tab != null) {
2356 dismissSubWindow(tab);
2357 tab.loadUrl(url, headers);
2358 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002359 }
2360
2361 /**
2362 * Load UrlData into a Tab and update the title bar to reflect the new
2363 * load. Call this instead of UrlData.loadIn directly.
2364 * @param t The Tab used to load.
2365 * @param data The UrlData being loaded.
2366 */
2367 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002368 if (data != null) {
2369 if (data.mVoiceIntent != null) {
2370 t.activateVoiceSearchMode(data.mVoiceIntent);
Michael Kolb14612442011-06-24 13:06:29 -07002371 } else if (data.isPreloaded()) {
2372 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002373 } else {
2374 loadUrl(t, data.mUrl, data.mHeaders);
2375 }
2376 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002377 }
2378
John Reck30c714c2010-12-16 17:30:34 -08002379 @Override
2380 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002381 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002382 if (tab.canGoBack()) {
2383 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002384 } else {
John Reckef654f12011-07-12 16:42:08 -07002385 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002386 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002387 }
2388
2389 void goBackOnePageOrQuit() {
2390 Tab current = mTabControl.getCurrentTab();
2391 if (current == null) {
2392 /*
2393 * Instead of finishing the activity, simply push this to the back
2394 * of the stack and let ActivityManager to choose the foreground
2395 * activity. As BrowserActivity is singleTask, it will be always the
2396 * root of the task. So we can use either true or false for
2397 * moveTaskToBack().
2398 */
2399 mActivity.moveTaskToBack(true);
2400 return;
2401 }
John Reckef654f12011-07-12 16:42:08 -07002402 if (current.canGoBack()) {
2403 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002404 } else {
2405 // Check to see if we are closing a window that was created by
2406 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002407 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002408 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002409 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002410 // Now we close the other tab
2411 closeTab(current);
2412 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002413 /*
2414 * Instead of finishing the activity, simply push this to the back
2415 * of the stack and let ActivityManager to choose the foreground
2416 * activity. As BrowserActivity is singleTask, it will be always the
2417 * root of the task. So we can use either true or false for
2418 * moveTaskToBack().
2419 */
2420 mActivity.moveTaskToBack(true);
2421 }
2422 }
2423 }
2424
2425 /**
2426 * Feed the previously stored results strings to the BrowserProvider so that
2427 * the SearchDialog will show them instead of the standard searches.
2428 * @param result String to show on the editable line of the SearchDialog.
2429 */
2430 @Override
2431 public void showVoiceSearchResults(String result) {
2432 ContentProviderClient client = mActivity.getContentResolver()
2433 .acquireContentProviderClient(Browser.BOOKMARKS_URI);
2434 ContentProvider prov = client.getLocalContentProvider();
2435 BrowserProvider bp = (BrowserProvider) prov;
2436 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
2437 client.release();
2438
2439 Bundle bundle = createGoogleSearchSourceBundle(
2440 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
2441 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
2442 startSearch(result, false, bundle, false);
2443 }
2444
2445 private void startSearch(String initialQuery, boolean selectInitialQuery,
2446 Bundle appSearchData, boolean globalSearch) {
2447 if (appSearchData == null) {
2448 appSearchData = createGoogleSearchSourceBundle(
2449 GOOGLE_SEARCH_SOURCE_TYPE);
2450 }
2451
2452 SearchEngine searchEngine = mSettings.getSearchEngine();
2453 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
2454 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
2455 }
2456 mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData,
2457 globalSearch);
2458 }
2459
2460 private Bundle createGoogleSearchSourceBundle(String source) {
2461 Bundle bundle = new Bundle();
2462 bundle.putString(Search.SOURCE, source);
2463 return bundle;
2464 }
2465
2466 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002467 * helper method for key handler
2468 * returns the current tab if it can't advance
2469 */
Michael Kolbc831b632011-05-11 09:30:34 -07002470 private Tab getNextTab() {
2471 return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1,
2472 mTabControl.getCurrentPosition() + 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002473 }
2474
2475 /**
2476 * helper method for key handler
2477 * returns the current tab if it can't advance
2478 */
Michael Kolbc831b632011-05-11 09:30:34 -07002479 private Tab getPrevTab() {
2480 return mTabControl.getTab(Math.max(0,
2481 mTabControl.getCurrentPosition() - 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002482 }
2483
2484 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002485 * handle key events in browser
2486 *
2487 * @param keyCode
2488 * @param event
2489 * @return true if handled, false to pass to super
2490 */
2491 boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002492 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002493 // Even if MENU is already held down, we need to call to super to open
2494 // the IME on long press.
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002495 if (!noModifiers
2496 && ((KeyEvent.KEYCODE_MENU == keyCode)
2497 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2498 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002499 mMenuIsDown = true;
2500 return false;
2501 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002502
Cary Clark8ff8c662010-12-29 15:03:05 -05002503 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002504 Tab tab = getCurrentTab();
2505 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002506
Cary Clark160bbb92011-01-10 11:17:07 -05002507 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2508 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002509
Michael Kolb8233fac2010-10-26 16:08:53 -07002510 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002511 case KeyEvent.KEYCODE_TAB:
2512 if (event.isCtrlPressed()) {
2513 if (event.isShiftPressed()) {
2514 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002515 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002516 } else {
2517 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002518 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002519 }
2520 return true;
2521 }
2522 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002523 case KeyEvent.KEYCODE_SPACE:
2524 // WebView/WebTextView handle the keys in the KeyDown. As
2525 // the Activity's shortcut keys are only handled when WebView
2526 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002527 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002528 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002529 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002530 pageDown();
2531 }
2532 return true;
2533 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002534 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002535 event.startTracking();
2536 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002537 case KeyEvent.KEYCODE_FORWARD:
2538 if (!noModifiers) break;
2539 tab.goForward();
2540 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002541 case KeyEvent.KEYCODE_DPAD_LEFT:
2542 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002543 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002544 return true;
2545 }
2546 break;
2547 case KeyEvent.KEYCODE_DPAD_RIGHT:
2548 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002549 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002550 return true;
2551 }
2552 break;
2553 case KeyEvent.KEYCODE_A:
2554 if (ctrl) {
2555 webView.selectAll();
2556 return true;
2557 }
2558 break;
Michael Kolba4183062011-01-16 10:43:21 -08002559// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002560 case KeyEvent.KEYCODE_C:
2561 if (ctrl) {
2562 webView.copySelection();
2563 return true;
2564 }
2565 break;
Michael Kolba4183062011-01-16 10:43:21 -08002566// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002567// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002568// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002569// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002570// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002571// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002572// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002573// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002574// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002575// case KeyEvent.KEYCODE_M: // unused
2576// case KeyEvent.KEYCODE_N: // in Chrome: new window
2577// case KeyEvent.KEYCODE_O: // in Chrome: open file
2578// case KeyEvent.KEYCODE_P: // in Chrome: print page
2579// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002580// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002581// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2582 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002583 // we can't use the ctrl/shift flags, they check for
2584 // exclusive use of a modifier
2585 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002586 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002587 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002588 } else {
2589 openTabToHomePage();
2590 }
2591 return true;
2592 }
2593 break;
2594// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2595// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb1a2eba42011-03-16 16:42:49 -07002596 case KeyEvent.KEYCODE_W: // in Chrome: close tab
2597 if (ctrl) {
2598 closeCurrentTab();
2599 return true;
2600 }
2601 break;
Cary Clark8ff8c662010-12-29 15:03:05 -05002602// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2603// case KeyEvent.KEYCODE_Y: // unused
2604// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002605 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002606 // it is a regular key and webview is not null
2607 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 }
2609
John Recke6bf4ab2011-02-24 15:48:05 -08002610 boolean onKeyLongPress(int keyCode, KeyEvent event) {
2611 switch(keyCode) {
2612 case KeyEvent.KEYCODE_BACK:
2613 if (mUi.showsWeb()) {
2614 bookmarksOrHistoryPicker(true);
2615 return true;
2616 }
2617 break;
2618 }
2619 return false;
2620 }
2621
Michael Kolb8233fac2010-10-26 16:08:53 -07002622 boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb2814a362011-05-19 15:49:41 -07002623 if (KeyEvent.KEYCODE_MENU == keyCode) {
2624 mMenuIsDown = false;
2625 if (event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002626 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002627 }
2628 }
Cary Clark160bbb92011-01-10 11:17:07 -05002629 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002630 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002631 case KeyEvent.KEYCODE_BACK:
2632 if (event.isTracking() && !event.isCanceled()) {
2633 onBackKey();
2634 return true;
2635 }
2636 break;
2637 }
2638 return false;
2639 }
2640
2641 public boolean isMenuDown() {
2642 return mMenuIsDown;
2643 }
2644
Ben Murdoch8029a772010-11-16 11:58:21 +00002645 public void setupAutoFill(Message message) {
2646 // Open the settings activity at the AutoFill profile fragment so that
2647 // the user can create a new profile. When they return, we will dispatch
2648 // the message so that we can autofill the form using their new profile.
2649 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2650 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2651 AutoFillSettingsFragment.class.getName());
2652 mAutoFillSetupMessage = message;
2653 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2654 }
John Reckb3417f02011-01-14 11:01:05 -08002655
2656 @Override
Narayan Kamath5119edd2011-02-23 15:49:17 +00002657 public void registerDropdownChangeListener(DropdownChangeListener d) {
2658 mUi.registerDropdownChangeListener(d);
2659 }
Michael Kolbfbc579a2011-07-07 15:59:33 -07002660
2661 public boolean onSearchRequested() {
2662 mUi.editUrl(false);
2663 return true;
2664 }
2665
Michael Kolb8233fac2010-10-26 16:08:53 -07002666}