blob: 323e0f3ff9143be3f4ece375812a38102cda4ed9 [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
20import android.app.DownloadManager;
21import android.app.SearchManager;
22import android.content.ClipboardManager;
23import android.content.ContentProvider;
24import android.content.ContentProviderClient;
25import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070026import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.content.ContentValues;
28import android.content.Context;
29import android.content.Intent;
30import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
32import android.content.res.Configuration;
Leon Scroggins1961ed22010-12-07 15:22:21 -050033import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import android.database.Cursor;
35import android.database.sqlite.SQLiteDatabase;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.graphics.Bitmap;
37import android.graphics.Canvas;
38import android.graphics.Picture;
39import android.net.Uri;
40import android.net.http.SslError;
41import android.os.AsyncTask;
42import android.os.Bundle;
43import android.os.Handler;
44import android.os.Message;
45import android.os.PowerManager;
46import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000047import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.provider.Browser;
49import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.provider.BrowserContract.Images;
51import android.provider.ContactsContract;
52import android.provider.ContactsContract.Intents.Insert;
Michael Kolbcfa3af52010-12-14 10:36:11 -080053import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.text.TextUtils;
55import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080056import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070057import android.view.ActionMode;
58import android.view.ContextMenu;
59import android.view.ContextMenu.ContextMenuInfo;
60import android.view.Gravity;
61import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070062import android.view.Menu;
63import android.view.MenuInflater;
64import android.view.MenuItem;
65import android.view.MenuItem.OnMenuItemClickListener;
66import android.view.View;
67import android.webkit.CookieManager;
68import android.webkit.CookieSyncManager;
69import android.webkit.HttpAuthHandler;
70import android.webkit.SslErrorHandler;
71import android.webkit.ValueCallback;
72import android.webkit.WebChromeClient;
73import android.webkit.WebIconDatabase;
74import android.webkit.WebSettings;
75import android.webkit.WebView;
Leon Scrogginsac993842011-02-02 12:54:07 -050076import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070077
Michael Kolb4bd767d2011-05-27 11:33:55 -070078import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070079import com.android.browser.UI.ComboViews;
Michael Kolb4bd767d2011-05-27 11:33:55 -070080import com.android.browser.UI.DropdownChangeListener;
81import com.android.browser.provider.BrowserProvider;
John Reck8cc92352011-07-06 17:41:52 -070082import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070083import com.android.browser.search.SearchEngine;
84import com.android.common.Search;
85
Michael Kolb8233fac2010-10-26 16:08:53 -070086import java.io.ByteArrayOutputStream;
87import java.io.File;
88import java.net.URLEncoder;
89import java.util.Calendar;
90import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080091import java.util.List;
John Reck26b18322011-06-21 13:08:58 -070092import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -070093
94/**
95 * Controller for browser
96 */
97public class Controller
98 implements WebViewController, UiController {
99
100 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800101 private static final String SEND_APP_ID_EXTRA =
102 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700103 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800104
Michael Kolb8233fac2010-10-26 16:08:53 -0700105
106 // public message ids
107 public final static int LOAD_URL = 1001;
108 public final static int STOP_LOAD = 1002;
109
110 // Message Ids
111 private static final int FOCUS_NODE_HREF = 102;
112 private static final int RELEASE_WAKELOCK = 107;
113
114 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
115
116 private static final int OPEN_BOOKMARKS = 201;
117
118 private static final int EMPTY_MENU = -1;
119
Michael Kolb8233fac2010-10-26 16:08:53 -0700120 // activity requestCode
121 final static int PREFERENCES_PAGE = 3;
122 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000123 final static int AUTOFILL_SETUP = 5;
124
Michael Kolb8233fac2010-10-26 16:08:53 -0700125 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
126
127 // As the ids are dynamically created, we can't guarantee that they will
128 // be in sequence, so this static array maps ids to a window number.
129 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
130 { R.id.window_one_menu_id, R.id.window_two_menu_id,
131 R.id.window_three_menu_id, R.id.window_four_menu_id,
132 R.id.window_five_menu_id, R.id.window_six_menu_id,
133 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
134
135 // "source" parameter for Google search through search key
136 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
137 // "source" parameter for Google search through simplily type
138 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
139
Guang Zhu9e78f512011-05-04 11:45:11 -0700140 // "no-crash-recovery" parameter in intetnt to suppress crash recovery
141 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
142
Michael Kolb8233fac2010-10-26 16:08:53 -0700143 private Activity mActivity;
144 private UI mUi;
145 private TabControl mTabControl;
146 private BrowserSettings mSettings;
147 private WebViewFactory mFactory;
John Reckb3417f02011-01-14 11:01:05 -0800148 private OptionsMenuHandler mOptionsMenuHandler = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700149
150 private WakeLock mWakeLock;
151
152 private UrlHandler mUrlHandler;
153 private UploadHandler mUploadHandler;
154 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700155 private PageDialogsHandler mPageDialogsHandler;
156 private NetworkStateHandler mNetworkHandler;
Martijn Coenenb2f93552011-06-14 10:48:35 +0200157 private NfcHandler mNfcHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700158
Ben Murdoch8029a772010-11-16 11:58:21 +0000159 private Message mAutoFillSetupMessage;
160
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 private boolean mShouldShowErrorConsole;
162
163 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
164
165 // FIXME, temp address onPrepareMenu performance problem.
166 // When we move everything out of view, we should rewrite this.
167 private int mCurrentMenuState = 0;
168 private int mMenuState = R.id.MAIN_MENU;
169 private int mOldMenuState = EMPTY_MENU;
170 private Menu mCachedMenu;
171
Michael Kolb8233fac2010-10-26 16:08:53 -0700172 private boolean mMenuIsDown;
173
174 // For select and find, we keep track of the ActionMode so that
175 // finish() can be called as desired.
176 private ActionMode mActionMode;
177
178 /**
179 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
180 * of whether the configuration has changed. The first onMenuOpened call
181 * after a configuration change is simply a reopening of the same menu
182 * (i.e. mIconView did not change).
183 */
184 private boolean mConfigChanged;
185
186 /**
187 * Keeps track of whether the options menu is open. This is important in
188 * determining whether to show or hide the title bar overlay
189 */
190 private boolean mOptionsMenuOpen;
191
192 /**
193 * Whether or not the options menu is in its bigger, popup menu form. When
194 * true, we want the title bar overlay to be gone. When false, we do not.
195 * Only meaningful if mOptionsMenuOpen is true.
196 */
197 private boolean mExtendedMenuOpen;
198
199 private boolean mInLoad;
200
201 private boolean mActivityPaused = true;
202 private boolean mLoadStopped;
203
204 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500205 // Checks to see when the bookmarks database has changed, and updates the
206 // Tabs' notion of whether they represent bookmarked sites.
207 private ContentObserver mBookmarksObserver;
John Reck0ebd3ac2010-12-09 11:14:04 -0800208 private DataController mDataController;
John Reck847b5322011-04-14 17:02:18 -0700209 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700210
211 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
212 @Override
213 public Void doInBackground(File... files) {
214 if (files != null) {
215 for (File f : files) {
216 if (!f.delete()) {
217 Log.e(LOGTAG, f.getPath() + " was not deleted");
218 }
219 }
220 }
221 return null;
222 }
223 }
224
225 public Controller(Activity browser) {
226 mActivity = browser;
227 mSettings = BrowserSettings.getInstance();
John Reck0ebd3ac2010-12-09 11:14:04 -0800228 mDataController = DataController.getInstance(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700229 mTabControl = new TabControl(this);
230 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700231 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
Michael Kolb14612442011-06-24 13:06:29 -0700232 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700233
234 mUrlHandler = new UrlHandler(this);
235 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200237 mNfcHandler = new NfcHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700238
239 PowerManager pm = (PowerManager) mActivity
240 .getSystemService(Context.POWER_SERVICE);
241 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
242
243 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500244 mBookmarksObserver = new ContentObserver(mHandler) {
245 @Override
246 public void onChange(boolean selfChange) {
247 int size = mTabControl.getTabCount();
248 for (int i = 0; i < size; i++) {
249 mTabControl.getTab(i).updateBookmarkedStatus();
250 }
251 }
252
253 };
254 browser.getContentResolver().registerContentObserver(
255 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700256
257 mNetworkHandler = new NetworkStateHandler(mActivity, this);
258 // Start watching the default geolocation permissions
259 mSystemAllowGeolocationOrigins =
260 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
261 mSystemAllowGeolocationOrigins.start();
262
263 retainIconsOnStartup();
264 }
265
Patrick Scott7d50a932011-02-04 09:27:26 -0500266 void start(final Bundle icicle, final Intent intent) {
Guang Zhu9e78f512011-05-04 11:45:11 -0700267 boolean noCrashRecovery = intent.getBooleanExtra(NO_CRASH_RECOVERY, false);
268 if (icicle != null || noCrashRecovery) {
John Reck847b5322011-04-14 17:02:18 -0700269 doStart(icicle, intent);
270 } else {
271 mCrashRecoveryHandler.startRecovery(intent);
272 }
273 }
274
275 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700276 // Unless the last browser usage was within 24 hours, destroy any
277 // remaining incognito tabs.
278
279 Calendar lastActiveDate = icicle != null ?
280 (Calendar) icicle.getSerializable("lastActiveDate") : null;
281 Calendar today = Calendar.getInstance();
282 Calendar yesterday = Calendar.getInstance();
283 yesterday.add(Calendar.DATE, -1);
284
Patrick Scott7d50a932011-02-04 09:27:26 -0500285 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700286 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800287 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700288
Patrick Scott7d50a932011-02-04 09:27:26 -0500289 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700290 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500291 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000292
Michael Kolbc831b632011-05-11 09:30:34 -0700293 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500294 // Not able to restore so we go ahead and clear session cookies. We
295 // must do this before trying to login the user as we don't want to
296 // clear any session cookies set during login.
297 CookieManager.getInstance().removeSessionCookie();
298 }
299
Patrick Scottd43e75a2011-03-14 14:47:23 -0400300 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500301 new Runnable() {
302 @Override public void run() {
Michael Kolbc831b632011-05-11 09:30:34 -0700303 onPreloginFinished(icicle, intent, currentTabId, restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500304 }
305 });
306 }
307
Michael Kolbc831b632011-05-11 09:30:34 -0700308 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500309 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700310 if (currentTabId == -1) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700311 final Bundle extra = intent.getExtras();
312 // Create an initial tab.
313 // If the intent is ACTION_VIEW and data is not null, the Browser is
314 // invoked to view the content by another application. In this case,
315 // the tab will be close when exit.
Michael Kolb14612442011-06-24 13:06:29 -0700316 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
Michael Kolb7bcafde2011-05-09 13:55:59 -0700317 Tab t = null;
318 if (urlData.isEmpty()) {
319 t = openTabToHomePage();
320 } else {
321 t = openTab(urlData);
322 }
323 if (t != null) {
324 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
325 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700326 WebView webView = t.getWebView();
327 if (extra != null) {
328 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
329 if (scale > 0 && scale <= 1000) {
330 webView.setInitialScale(scale);
331 }
332 }
John Reckd8c74522011-06-14 08:45:00 -0700333 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700334 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700335 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500336 mUi.needsRestoreAllTabs());
Michael Kolb1bf23132010-11-19 12:55:12 -0800337 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700338 // TabControl.restoreState() will create a new tab even if
339 // restoring the state fails.
340 setActiveTab(mTabControl.getCurrentTab());
John Reckdb22ec42011-06-29 11:31:24 -0700341 // Handle the intent
342 mIntentHandler.onNewIntent(intent);
Michael Kolb8233fac2010-10-26 16:08:53 -0700343 }
344 // clear up the thumbnail directory, which is no longer used;
345 // ideally this should only be run once after an upgrade from
346 // a previous version of the browser
347 new ClearThumbnails().execute(mTabControl.getThumbnailDir()
348 .listFiles());
349 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700350 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700351 if (jsFlags.trim().length() != 0) {
352 getCurrentWebView().setJsFlags(jsFlags);
353 }
John Reck439c9a52010-12-14 10:04:39 -0800354 if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
355 bookmarksOrHistoryPicker(false);
356 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700357 }
358
Michael Kolb1514bb72010-11-22 09:11:48 -0800359 @Override
360 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700361 return mFactory;
362 }
363
364 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800365 public void onSetWebView(Tab tab, WebView view) {
366 mUi.onSetWebView(tab, view);
367 }
368
369 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800370 public void createSubWindow(Tab tab) {
371 endActionMode();
372 WebView mainView = tab.getWebView();
373 WebView subView = mFactory.createWebView((mainView == null)
374 ? false
375 : mainView.isPrivateBrowsingEnabled());
376 mUi.createSubWindow(tab, subView);
377 }
378
379 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700380 public Context getContext() {
381 return mActivity;
382 }
383
384 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700385 public Activity getActivity() {
386 return mActivity;
387 }
388
389 void setUi(UI ui) {
390 mUi = ui;
391 }
392
393 BrowserSettings getSettings() {
394 return mSettings;
395 }
396
397 IntentHandler getIntentHandler() {
398 return mIntentHandler;
399 }
400
401 @Override
402 public UI getUi() {
403 return mUi;
404 }
405
406 int getMaxTabs() {
407 return mActivity.getResources().getInteger(R.integer.max_tabs);
408 }
409
410 @Override
411 public TabControl getTabControl() {
412 return mTabControl;
413 }
414
Michael Kolb1bf23132010-11-19 12:55:12 -0800415 @Override
416 public List<Tab> getTabs() {
417 return mTabControl.getTabs();
418 }
419
Michael Kolb8233fac2010-10-26 16:08:53 -0700420 // Open the icon database and retain all the icons for visited sites.
Ben Murdoch9446b932010-11-25 16:20:14 +0000421 // This is done on a background thread so as not to stall startup.
Michael Kolb8233fac2010-10-26 16:08:53 -0700422 private void retainIconsOnStartup() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000423 // WebIconDatabase needs to be retrieved on the UI thread so that if
424 // it has not been created successfully yet the Handler is started on the
425 // UI thread.
426 new RetainIconsOnStartupTask(WebIconDatabase.getInstance()).execute();
427 }
428
429 private class RetainIconsOnStartupTask extends AsyncTask<Void, Void, Void> {
430 private WebIconDatabase mDb;
431
432 public RetainIconsOnStartupTask(WebIconDatabase db) {
433 mDb = db;
434 }
435
John Recka00cbbd2010-12-16 12:38:19 -0800436 @Override
Ben Murdoch9446b932010-11-25 16:20:14 +0000437 protected Void doInBackground(Void... unused) {
438 mDb.open(mActivity.getDir("icons", 0).getPath());
439 Cursor c = null;
440 try {
441 c = Browser.getAllBookmarks(mActivity.getContentResolver());
442 if (c.moveToFirst()) {
443 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
444 do {
445 String url = c.getString(urlIndex);
446 mDb.retainIconForPageUrl(url);
447 } while (c.moveToNext());
448 }
449 } catch (IllegalStateException e) {
450 Log.e(LOGTAG, "retainIconsOnStartup", e);
451 } finally {
452 if (c != null) c.close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700453 }
Ben Murdoch9446b932010-11-25 16:20:14 +0000454
455 return null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700456 }
457 }
458
459 private void startHandler() {
460 mHandler = new Handler() {
461
462 @Override
463 public void handleMessage(Message msg) {
464 switch (msg.what) {
465 case OPEN_BOOKMARKS:
466 bookmarksOrHistoryPicker(false);
467 break;
468 case FOCUS_NODE_HREF:
469 {
470 String url = (String) msg.getData().get("url");
471 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500472 String src = (String) msg.getData().get("src");
473 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700474 if (TextUtils.isEmpty(url)) {
475 break;
476 }
477 HashMap focusNodeMap = (HashMap) msg.obj;
478 WebView view = (WebView) focusNodeMap.get("webview");
479 // Only apply the action if the top window did not change.
480 if (getCurrentTopWebView() != view) {
481 break;
482 }
483 switch (msg.arg1) {
484 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700485 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700486 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500487 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700488 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500489 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500490 case R.id.open_newtab_context_menu_id:
491 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700492 openTab(url, parent,
493 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500494 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700495 case R.id.copy_link_context_menu_id:
496 copy(url);
497 break;
498 case R.id.save_link_context_menu_id:
499 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500500 DownloadHandler.onDownloadStartNoStream(
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000501 mActivity, url, null, null, null,
502 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700503 break;
504 }
505 break;
506 }
507
508 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700509 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700510 break;
511
512 case STOP_LOAD:
513 stopLoading();
514 break;
515
516 case RELEASE_WAKELOCK:
517 if (mWakeLock.isHeld()) {
518 mWakeLock.release();
519 // if we reach here, Browser should be still in the
520 // background loading after WAKELOCK_TIMEOUT (5-min).
521 // To avoid burning the battery, stop loading.
522 mTabControl.stopAllLoading();
523 }
524 break;
525
526 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800527 Tab tab = (Tab) msg.obj;
528 if (tab != null) {
529 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700530 }
531 break;
532 }
533 }
534 };
535
536 }
537
John Reckef654f12011-07-12 16:42:08 -0700538 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200539 public Tab getCurrentTab() {
540 return mTabControl.getCurrentTab();
541 }
542
Michael Kolbba99c5d2010-11-29 14:57:41 -0800543 @Override
544 public void shareCurrentPage() {
545 shareCurrentPage(mTabControl.getCurrentTab());
546 }
547
548 private void shareCurrentPage(Tab tab) {
549 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800550 sharePage(mActivity, tab.getTitle(),
551 tab.getUrl(), tab.getFavicon(),
552 createScreenshot(tab.getWebView(),
553 getDesiredThumbnailWidth(mActivity),
554 getDesiredThumbnailHeight(mActivity)));
555 }
556 }
557
Michael Kolb8233fac2010-10-26 16:08:53 -0700558 /**
559 * Share a page, providing the title, url, favicon, and a screenshot. Uses
560 * an {@link Intent} to launch the Activity chooser.
561 * @param c Context used to launch a new Activity.
562 * @param title Title of the page. Stored in the Intent with
563 * {@link Intent#EXTRA_SUBJECT}
564 * @param url URL of the page. Stored in the Intent with
565 * {@link Intent#EXTRA_TEXT}
566 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
567 * with {@link Browser#EXTRA_SHARE_FAVICON}
568 * @param screenshot Bitmap of a screenshot of the page. Stored in the
569 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
570 */
571 static final void sharePage(Context c, String title, String url,
572 Bitmap favicon, Bitmap screenshot) {
573 Intent send = new Intent(Intent.ACTION_SEND);
574 send.setType("text/plain");
575 send.putExtra(Intent.EXTRA_TEXT, url);
576 send.putExtra(Intent.EXTRA_SUBJECT, title);
577 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
578 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
579 try {
580 c.startActivity(Intent.createChooser(send, c.getString(
581 R.string.choosertitle_sharevia)));
582 } catch(android.content.ActivityNotFoundException ex) {
583 // if no app handles it, do nothing
584 }
585 }
586
587 private void copy(CharSequence text) {
588 ClipboardManager cm = (ClipboardManager) mActivity
589 .getSystemService(Context.CLIPBOARD_SERVICE);
590 cm.setText(text);
591 }
592
593 // lifecycle
594
595 protected void onConfgurationChanged(Configuration config) {
596 mConfigChanged = true;
597 if (mPageDialogsHandler != null) {
598 mPageDialogsHandler.onConfigurationChanged(config);
599 }
600 mUi.onConfigurationChanged(config);
601 }
602
603 @Override
604 public void handleNewIntent(Intent intent) {
605 mIntentHandler.onNewIntent(intent);
606 }
607
608 protected void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800609 if (mUi.isCustomViewShowing()) {
610 hideCustomView();
611 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700612 if (mActivityPaused) {
613 Log.e(LOGTAG, "BrowserActivity is already paused.");
614 return;
615 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700616 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800617 Tab tab = mTabControl.getCurrentTab();
618 if (tab != null) {
619 tab.pause();
620 if (!pauseWebViewTimers(tab)) {
621 mWakeLock.acquire();
622 mHandler.sendMessageDelayed(mHandler
623 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
624 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 }
626 mUi.onPause();
627 mNetworkHandler.onPause();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200628 mNfcHandler.onPause();
Michael Kolb8233fac2010-10-26 16:08:53 -0700629
630 WebView.disablePlatformNotifications();
John Reck378a4102011-06-09 16:23:01 -0700631 mCrashRecoveryHandler.backupState();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200632
Michael Kolb8233fac2010-10-26 16:08:53 -0700633 }
634
John Reckaed9c542011-05-27 16:08:53 -0700635 void onSaveInstanceState(Bundle outState, boolean saveImages) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700636 // the default implementation requires each view to have an id. As the
637 // browser handles the state itself and it doesn't use id for the views,
638 // don't call the default implementation. Otherwise it will trigger the
639 // warning like this, "couldn't save which view has focus because the
640 // focused view XXX has no id".
641
642 // Save all the tabs
John Reckaed9c542011-05-27 16:08:53 -0700643 mTabControl.saveState(outState, saveImages);
John Reck24f18262011-06-17 14:47:20 -0700644 if (!outState.isEmpty()) {
645 // Save time so that we know how old incognito tabs (if any) are.
646 outState.putSerializable("lastActiveDate", Calendar.getInstance());
647 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700648 }
649
650 void onResume() {
651 if (!mActivityPaused) {
652 Log.e(LOGTAG, "BrowserActivity is already resumed.");
653 return;
654 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700655 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800656 Tab current = mTabControl.getCurrentTab();
657 if (current != null) {
658 current.resume();
659 resumeWebViewTimers(current);
660 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 if (mWakeLock.isHeld()) {
662 mHandler.removeMessages(RELEASE_WAKELOCK);
663 mWakeLock.release();
664 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200665
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 mUi.onResume();
667 mNetworkHandler.onResume();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200668 mNfcHandler.onResume();
Michael Kolb8233fac2010-10-26 16:08:53 -0700669 WebView.enablePlatformNotifications();
670 }
671
Michael Kolb70976932010-11-30 11:34:01 -0800672 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800673 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800674 * @param tab guaranteed non-null
675 */
676 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700677 boolean inLoad = tab.inPageLoad();
678 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
679 CookieSyncManager.getInstance().startSync();
680 WebView w = tab.getWebView();
681 if (w != null) {
682 w.resumeTimers();
683 }
684 }
685 }
686
Michael Kolb70976932010-11-30 11:34:01 -0800687 /**
688 * Pause all WebView timers using the WebView of the given tab
689 * @param tab
690 * @return true if the timers are paused or tab is null
691 */
692 private boolean pauseWebViewTimers(Tab tab) {
693 if (tab == null) {
694 return true;
695 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700696 CookieSyncManager.getInstance().stopSync();
697 WebView w = getCurrentWebView();
698 if (w != null) {
699 w.pauseTimers();
700 }
701 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700702 }
Michael Kolb70976932010-11-30 11:34:01 -0800703 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700704 }
705
706 void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800707 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700708 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
709 mUploadHandler = null;
710 }
711 if (mTabControl == null) return;
712 mUi.onDestroy();
713 // Remove the current tab and sub window
714 Tab t = mTabControl.getCurrentTab();
715 if (t != null) {
716 dismissSubWindow(t);
717 removeTab(t);
718 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500719 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700720 // Destroy all the tabs
721 mTabControl.destroy();
722 WebIconDatabase.getInstance().close();
723 // Stop watching the default geolocation permissions
724 mSystemAllowGeolocationOrigins.stop();
725 mSystemAllowGeolocationOrigins = null;
726 }
727
728 protected boolean isActivityPaused() {
729 return mActivityPaused;
730 }
731
732 protected void onLowMemory() {
733 mTabControl.freeMemory();
734 }
735
736 @Override
737 public boolean shouldShowErrorConsole() {
738 return mShouldShowErrorConsole;
739 }
740
741 protected void setShouldShowErrorConsole(boolean show) {
742 if (show == mShouldShowErrorConsole) {
743 // Nothing to do.
744 return;
745 }
746 mShouldShowErrorConsole = show;
747 Tab t = mTabControl.getCurrentTab();
748 if (t == null) {
749 // There is no current tab so we cannot toggle the error console
750 return;
751 }
752 mUi.setShouldShowErrorConsole(t, show);
753 }
754
755 @Override
756 public void stopLoading() {
757 mLoadStopped = true;
758 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700759 WebView w = getCurrentTopWebView();
760 w.stopLoading();
Michael Kolb8233fac2010-10-26 16:08:53 -0700761 mUi.onPageStopped(tab);
762 }
763
764 boolean didUserStopLoading() {
765 return mLoadStopped;
766 }
767
768 // WebViewController
769
770 @Override
John Reck324d4402011-01-11 16:56:42 -0800771 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700772
773 // We've started to load a new page. If there was a pending message
774 // to save a screenshot then we will now take the new page and save
775 // an incorrect screenshot. Therefore, remove any pending thumbnail
776 // messages from the queue.
777 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800778 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700779
780 // reset sync timer to avoid sync starts during loading a page
781 CookieSyncManager.getInstance().resetSync();
782
783 if (!mNetworkHandler.isNetworkUp()) {
784 view.setNetworkAvailable(false);
785 }
786
787 // when BrowserActivity just starts, onPageStarted may be called before
788 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
789 // to start the timer. As we won't switch tabs while an activity is in
790 // pause state, we can ensure calling resume and pause in pair.
791 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800792 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700793 }
794 mLoadStopped = false;
795 if (!mNetworkHandler.isNetworkUp()) {
796 mNetworkHandler.createAndShowNetworkDialog();
797 }
798 endActionMode();
799
John Reck30c714c2010-12-16 17:30:34 -0800800 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700801
John Reck324d4402011-01-11 16:56:42 -0800802 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700803 // update the bookmark database for favicon
804 maybeUpdateFavicon(tab, null, url, favicon);
805
806 Performance.tracePageStart(url);
807
808 // Performance probe
809 if (false) {
810 Performance.onPageStarted();
811 }
812
813 }
814
815 @Override
John Reck324d4402011-01-11 16:56:42 -0800816 public void onPageFinished(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800817 mUi.onTabDataChanged(tab);
John Reck324d4402011-01-11 16:56:42 -0800818 if (!tab.isPrivateBrowsingEnabled()
John Reckd8c74522011-06-14 08:45:00 -0700819 && !TextUtils.isEmpty(tab.getUrl())
820 && !tab.isSnapshot()) {
John Recka1696282011-07-08 14:10:37 -0700821 // Only update the bookmark screenshot if the user did not
822 // cancel the load early and there is not already
823 // a pending update for the tab.
Michael Kolb8233fac2010-10-26 16:08:53 -0700824 if (tab.inForeground() && !didUserStopLoading()
825 || !tab.inForeground()) {
John Recka1696282011-07-08 14:10:37 -0700826 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
827 mHandler.sendMessageDelayed(mHandler.obtainMessage(
828 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
829 500);
830 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700831 }
832 }
833 // pause the WebView timer and release the wake lock if it is finished
834 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800835 if (mActivityPaused && pauseWebViewTimers(tab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700836 if (mWakeLock.isHeld()) {
837 mHandler.removeMessages(RELEASE_WAKELOCK);
838 mWakeLock.release();
839 }
840 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200841
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 // Performance probe
843 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800844 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700845 }
846
847 Performance.tracePageFinished();
848 }
849
850 @Override
John Reck30c714c2010-12-16 17:30:34 -0800851 public void onProgressChanged(Tab tab) {
852 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700853
854 if (newProgress == 100) {
855 CookieSyncManager.getInstance().sync();
856 // onProgressChanged() may continue to be called after the main
857 // frame has finished loading, as any remaining sub frames continue
858 // to load. We'll only get called once though with newProgress as
859 // 100 when everything is loaded. (onPageFinished is called once
860 // when the main frame completes loading regardless of the state of
861 // any sub frames so calls to onProgressChanges may continue after
862 // onPageFinished has executed)
863 if (mInLoad) {
864 mInLoad = false;
865 updateInLoadMenuItems(mCachedMenu);
866 }
867 } else {
868 if (!mInLoad) {
869 // onPageFinished may have already been called but a subframe is
870 // still loading and updating the progress. Reset mInLoad and
871 // update the menu items.
872 mInLoad = true;
873 updateInLoadMenuItems(mCachedMenu);
874 }
875 }
John Reck30c714c2010-12-16 17:30:34 -0800876 mUi.onProgressChanged(tab);
877 }
878
879 @Override
880 public void onUpdatedLockIcon(Tab tab) {
881 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700882 }
883
884 @Override
885 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800886 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800887 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800888 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700889 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
890 return;
891 }
892 // Update the title in the history database if not in private browsing mode
893 if (!tab.isPrivateBrowsingEnabled()) {
John Reck0ebd3ac2010-12-09 11:14:04 -0800894 mDataController.updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700895 }
896 }
897
898 @Override
899 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800900 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700901 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
902 }
903
904 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800905 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
906 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700907 }
908
909 @Override
910 public boolean shouldOverrideKeyEvent(KeyEvent event) {
911 if (mMenuIsDown) {
912 // only check shortcut key when MENU is held
913 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
914 event);
915 } else {
916 return false;
917 }
918 }
919
920 @Override
921 public void onUnhandledKeyEvent(KeyEvent event) {
922 if (!isActivityPaused()) {
923 if (event.getAction() == KeyEvent.ACTION_DOWN) {
924 mActivity.onKeyDown(event.getKeyCode(), event);
925 } else {
926 mActivity.onKeyUp(event.getKeyCode(), event);
927 }
928 }
929 }
930
931 @Override
John Reck324d4402011-01-11 16:56:42 -0800932 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700933 // Don't save anything in private browsing mode
934 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800935 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700936
John Reck324d4402011-01-11 16:56:42 -0800937 if (TextUtils.isEmpty(url)
938 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700939 return;
940 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800941 mDataController.updateVisitedHistory(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700942 WebIconDatabase.getInstance().retainIconForPageUrl(url);
John Reck847b5322011-04-14 17:02:18 -0700943 if (!mActivityPaused) {
944 // Since we clear the state in onPause, don't backup the current
945 // state if we are already paused
946 mCrashRecoveryHandler.backupState();
947 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700948 }
949
950 @Override
951 public void getVisitedHistory(final ValueCallback<String[]> callback) {
952 AsyncTask<Void, Void, String[]> task =
953 new AsyncTask<Void, Void, String[]>() {
954 @Override
955 public String[] doInBackground(Void... unused) {
956 return Browser.getVisitedHistory(mActivity.getContentResolver());
957 }
958 @Override
959 public void onPostExecute(String[] result) {
960 callback.onReceiveValue(result);
961 }
962 };
963 task.execute();
964 }
965
966 @Override
967 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
968 final HttpAuthHandler handler, final String host,
969 final String realm) {
970 String username = null;
971 String password = null;
972
973 boolean reuseHttpAuthUsernamePassword
974 = handler.useHttpAuthUsernamePassword();
975
976 if (reuseHttpAuthUsernamePassword && view != null) {
977 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
978 if (credentials != null && credentials.length == 2) {
979 username = credentials[0];
980 password = credentials[1];
981 }
982 }
983
984 if (username != null && password != null) {
985 handler.proceed(username, password);
986 } else {
987 if (tab.inForeground()) {
988 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
989 } else {
990 handler.cancel();
991 }
992 }
993 }
994
995 @Override
996 public void onDownloadStart(Tab tab, String url, String userAgent,
997 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000998 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -0500999 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001000 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
1001 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001002 // This Tab was opened for the sole purpose of downloading a
1003 // file. Remove it.
1004 if (tab == mTabControl.getCurrentTab()) {
1005 // In this case, the Tab is still on top.
1006 goBackOnePageOrQuit();
1007 } else {
1008 // In this case, it is not.
1009 closeTab(tab);
1010 }
1011 }
1012 }
1013
1014 @Override
1015 public Bitmap getDefaultVideoPoster() {
1016 return mUi.getDefaultVideoPoster();
1017 }
1018
1019 @Override
1020 public View getVideoLoadingProgressView() {
1021 return mUi.getVideoLoadingProgressView();
1022 }
1023
1024 @Override
1025 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1026 SslError error) {
1027 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1028 }
1029
Patrick Scott92066772011-03-10 08:46:27 -05001030 @Override
1031 public void showAutoLogin(Tab tab) {
1032 assert tab.inForeground();
1033 // Update the title bar to show the auto-login request.
1034 mUi.showAutoLogin(tab);
1035 }
1036
1037 @Override
1038 public void hideAutoLogin(Tab tab) {
1039 assert tab.inForeground();
1040 mUi.hideAutoLogin(tab);
1041 }
1042
Michael Kolb8233fac2010-10-26 16:08:53 -07001043 // helper method
1044
1045 /*
1046 * Update the favorites icon if the private browsing isn't enabled and the
1047 * icon is valid.
1048 */
1049 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1050 final String url, Bitmap favicon) {
1051 if (favicon == null) {
1052 return;
1053 }
1054 if (!tab.isPrivateBrowsingEnabled()) {
1055 Bookmarks.updateFavicon(mActivity
1056 .getContentResolver(), originalUrl, url, favicon);
1057 }
1058 }
1059
Leon Scroggins4cd97792010-12-03 15:31:56 -05001060 @Override
1061 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001062 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001063 mUi.bookmarkedStatusHasChanged(tab);
1064 }
1065
Michael Kolb8233fac2010-10-26 16:08:53 -07001066 // end WebViewController
1067
1068 protected void pageUp() {
1069 getCurrentTopWebView().pageUp(false);
1070 }
1071
1072 protected void pageDown() {
1073 getCurrentTopWebView().pageDown(false);
1074 }
1075
1076 // callback from phone title bar
1077 public void editUrl() {
1078 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001079 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001080 }
1081
Michael Kolbcfa3af52010-12-14 10:36:11 -08001082 public void startVoiceSearch() {
1083 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1084 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1085 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1086 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1087 mActivity.getComponentName().flattenToString());
1088 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001089 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001090 mActivity.startActivity(intent);
1091 }
1092
Michael Kolb11d19782011-03-20 10:17:40 -07001093 @Override
1094 public void activateVoiceSearchMode(String title, List<String> results) {
1095 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001096 }
1097
1098 public void revertVoiceSearchMode(Tab tab) {
1099 mUi.revertVoiceTitleBar(tab);
1100 }
1101
Michael Kolb736990c2011-03-20 10:01:20 -07001102 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001103 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001104 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1105 }
1106
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001107 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001108 WebChromeClient.CustomViewCallback callback) {
1109 if (tab.inForeground()) {
1110 if (mUi.isCustomViewShowing()) {
1111 callback.onCustomViewHidden();
1112 return;
1113 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001114 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001115 // Save the menu state and set it to empty while the custom
1116 // view is showing.
1117 mOldMenuState = mMenuState;
1118 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001119 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001120 }
1121 }
1122
1123 @Override
1124 public void hideCustomView() {
1125 if (mUi.isCustomViewShowing()) {
1126 mUi.onHideCustomView();
1127 // Reset the old menu state.
1128 mMenuState = mOldMenuState;
1129 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001130 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001131 }
1132 }
1133
1134 protected void onActivityResult(int requestCode, int resultCode,
1135 Intent intent) {
1136 if (getCurrentTopWebView() == null) return;
1137 switch (requestCode) {
1138 case PREFERENCES_PAGE:
1139 if (resultCode == Activity.RESULT_OK && intent != null) {
1140 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001141 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001142 mTabControl.removeParentChildRelationShips();
1143 }
1144 }
1145 break;
1146 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001147 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001148 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001149 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001150 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001151 case AUTOFILL_SETUP:
1152 // Determine whether a profile was actually set up or not
1153 // and if so, send the message back to the WebTextView to
1154 // fill the form with the new profile.
1155 if (getSettings().getAutoFillProfile() != null) {
1156 mAutoFillSetupMessage.sendToTarget();
1157 mAutoFillSetupMessage = null;
1158 }
1159 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001160 default:
1161 break;
1162 }
1163 getCurrentTopWebView().requestFocus();
1164 }
1165
1166 /**
1167 * Open the Go page.
1168 * @param startWithHistory If true, open starting on the history tab.
1169 * Otherwise, start with the bookmarks tab.
1170 */
1171 @Override
1172 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1173 if (mTabControl.getCurrentWebView() == null) {
1174 return;
1175 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001176 // clear action mode
1177 if (isInCustomActionMode()) {
1178 endActionMode();
1179 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001180 Bundle extras = new Bundle();
1181 // Disable opening in a new window if we have maxed out the windows
1182 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1183 !mTabControl.canCreateNewTab());
John Reck2bc80422011-06-30 15:11:49 -07001184 mUi.showComboView(startWithHistory
1185 ? ComboViews.History : ComboViews.Bookmarks, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001186 }
1187
1188 // combo view callbacks
1189
1190 /**
1191 * callback from ComboPage when clear history is requested
1192 */
1193 public void onRemoveParentChildRelationships() {
1194 mTabControl.removeParentChildRelationShips();
1195 }
1196
1197 /**
1198 * callback from ComboPage when bookmark/history selection
1199 */
1200 @Override
1201 public void onUrlSelected(String url, boolean newTab) {
1202 removeComboView();
1203 if (!TextUtils.isEmpty(url)) {
1204 if (newTab) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07001205 final Tab parent = mTabControl.getCurrentTab();
1206 openTab(url,
1207 (parent != null) && parent.isPrivateBrowsingEnabled(),
1208 !mSettings.openInBackground(),
1209 true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001210 } else {
1211 final Tab currentTab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001212 loadUrl(currentTab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001213 }
1214 }
1215 }
1216
1217 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001218 * dismiss the ComboPage
1219 */
1220 @Override
1221 public void removeComboView() {
1222 mUi.hideComboView();
1223 }
1224
Michael Kolb8233fac2010-10-26 16:08:53 -07001225 // key handling
1226 protected void onBackKey() {
1227 if (!mUi.onBackKey()) {
1228 WebView subwindow = mTabControl.getCurrentSubWindow();
1229 if (subwindow != null) {
1230 if (subwindow.canGoBack()) {
1231 subwindow.goBack();
1232 } else {
1233 dismissSubWindow(mTabControl.getCurrentTab());
1234 }
1235 } else {
1236 goBackOnePageOrQuit();
1237 }
1238 }
1239 }
1240
Michael Kolb4bd767d2011-05-27 11:33:55 -07001241 protected boolean onMenuKey() {
1242 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001243 }
1244
Michael Kolb8233fac2010-10-26 16:08:53 -07001245 // menu handling and state
1246 // TODO: maybe put into separate handler
1247
1248 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckb3417f02011-01-14 11:01:05 -08001249 if (mOptionsMenuHandler != null) {
1250 return mOptionsMenuHandler.onCreateOptionsMenu(menu);
1251 }
1252
John Reckd73c5a22010-12-22 10:22:50 -08001253 if (mMenuState == EMPTY_MENU) {
1254 return false;
1255 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001256 MenuInflater inflater = mActivity.getMenuInflater();
1257 inflater.inflate(R.menu.browser, menu);
1258 updateInLoadMenuItems(menu);
1259 // hold on to the menu reference here; it is used by the page callbacks
1260 // to update the menu based on loading state
1261 mCachedMenu = menu;
1262 return true;
1263 }
1264
1265 protected void onCreateContextMenu(ContextMenu menu, View v,
1266 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001267 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001268 return;
1269 }
1270 if (!(v instanceof WebView)) {
1271 return;
1272 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001273 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001274 WebView.HitTestResult result = webview.getHitTestResult();
1275 if (result == null) {
1276 return;
1277 }
1278
1279 int type = result.getType();
1280 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1281 Log.w(LOGTAG,
1282 "We should not show context menu when nothing is touched");
1283 return;
1284 }
1285 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1286 // let TextView handles context menu
1287 return;
1288 }
1289
1290 // Note, http://b/issue?id=1106666 is requesting that
1291 // an inflated menu can be used again. This is not available
1292 // yet, so inflate each time (yuk!)
1293 MenuInflater inflater = mActivity.getMenuInflater();
1294 inflater.inflate(R.menu.browsercontext, menu);
1295
1296 // Show the correct menu group
1297 final String extra = result.getExtra();
1298 menu.setGroupVisible(R.id.PHONE_MENU,
1299 type == WebView.HitTestResult.PHONE_TYPE);
1300 menu.setGroupVisible(R.id.EMAIL_MENU,
1301 type == WebView.HitTestResult.EMAIL_TYPE);
1302 menu.setGroupVisible(R.id.GEO_MENU,
1303 type == WebView.HitTestResult.GEO_TYPE);
1304 menu.setGroupVisible(R.id.IMAGE_MENU,
1305 type == WebView.HitTestResult.IMAGE_TYPE
1306 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1307 menu.setGroupVisible(R.id.ANCHOR_MENU,
1308 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1309 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001310 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1311 || type == WebView.HitTestResult.PHONE_TYPE
1312 || type == WebView.HitTestResult.EMAIL_TYPE
1313 || type == WebView.HitTestResult.GEO_TYPE;
1314 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1315 if (hitText) {
1316 menu.findItem(R.id.select_text_menu_id)
1317 .setOnMenuItemClickListener(new SelectText(webview));
1318 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001319 // Setup custom handling depending on the type
1320 switch (type) {
1321 case WebView.HitTestResult.PHONE_TYPE:
1322 menu.setHeaderTitle(Uri.decode(extra));
1323 menu.findItem(R.id.dial_context_menu_id).setIntent(
1324 new Intent(Intent.ACTION_VIEW, Uri
1325 .parse(WebView.SCHEME_TEL + extra)));
1326 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1327 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1328 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1329 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1330 addIntent);
1331 menu.findItem(R.id.copy_phone_context_menu_id)
1332 .setOnMenuItemClickListener(
1333 new Copy(extra));
1334 break;
1335
1336 case WebView.HitTestResult.EMAIL_TYPE:
1337 menu.setHeaderTitle(extra);
1338 menu.findItem(R.id.email_context_menu_id).setIntent(
1339 new Intent(Intent.ACTION_VIEW, Uri
1340 .parse(WebView.SCHEME_MAILTO + extra)));
1341 menu.findItem(R.id.copy_mail_context_menu_id)
1342 .setOnMenuItemClickListener(
1343 new Copy(extra));
1344 break;
1345
1346 case WebView.HitTestResult.GEO_TYPE:
1347 menu.setHeaderTitle(extra);
1348 menu.findItem(R.id.map_context_menu_id).setIntent(
1349 new Intent(Intent.ACTION_VIEW, Uri
1350 .parse(WebView.SCHEME_GEO
1351 + URLEncoder.encode(extra))));
1352 menu.findItem(R.id.copy_geo_context_menu_id)
1353 .setOnMenuItemClickListener(
1354 new Copy(extra));
1355 break;
1356
1357 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1358 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001359 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001360 // decide whether to show the open link in new tab option
1361 boolean showNewTab = mTabControl.canCreateNewTab();
1362 MenuItem newTabItem
1363 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001364 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001365 ? R.string.contextmenu_openlink_newwindow_background
1366 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001367 newTabItem.setVisible(showNewTab);
1368 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001369 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1370 newTabItem.setOnMenuItemClickListener(
1371 new MenuItem.OnMenuItemClickListener() {
1372 @Override
1373 public boolean onMenuItemClick(MenuItem item) {
1374 final HashMap<String, WebView> hrefMap =
1375 new HashMap<String, WebView>();
1376 hrefMap.put("webview", webview);
1377 final Message msg = mHandler.obtainMessage(
1378 FOCUS_NODE_HREF,
1379 R.id.open_newtab_context_menu_id,
1380 0, hrefMap);
1381 webview.requestFocusNodeHref(msg);
1382 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001383 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001384 });
1385 } else {
1386 newTabItem.setOnMenuItemClickListener(
1387 new MenuItem.OnMenuItemClickListener() {
1388 @Override
1389 public boolean onMenuItemClick(MenuItem item) {
1390 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001391 openTab(extra, parent,
1392 !mSettings.openInBackground(),
1393 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001394 return true;
1395 }
1396 });
1397 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001398 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001399 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1400 break;
1401 }
1402 // otherwise fall through to handle image part
1403 case WebView.HitTestResult.IMAGE_TYPE:
1404 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1405 menu.setHeaderTitle(extra);
1406 }
1407 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1408 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1409 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001410 setOnMenuItemClickListener(
1411 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001412 menu.findItem(R.id.set_wallpaper_context_menu_id).
1413 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1414 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001415 break;
1416
1417 default:
1418 Log.w(LOGTAG, "We should not get here.");
1419 break;
1420 }
1421 //update the ui
1422 mUi.onContextMenuCreated(menu);
1423 }
1424
1425 /**
1426 * As the menu can be open when loading state changes
1427 * we must manually update the state of the stop/reload menu
1428 * item
1429 */
1430 private void updateInLoadMenuItems(Menu menu) {
1431 if (menu == null) {
1432 return;
1433 }
1434 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1435 MenuItem src = mInLoad ?
1436 menu.findItem(R.id.stop_menu_id):
1437 menu.findItem(R.id.reload_menu_id);
1438 if (src != null) {
1439 dest.setIcon(src.getIcon());
1440 dest.setTitle(src.getTitle());
1441 }
1442 }
1443
John Reckb3417f02011-01-14 11:01:05 -08001444 boolean onPrepareOptionsMenu(Menu menu) {
1445 if (mOptionsMenuHandler != null) {
1446 return mOptionsMenuHandler.onPrepareOptionsMenu(menu);
1447 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001448 // Note: setVisible will decide whether an item is visible; while
1449 // setEnabled() will decide whether an item is enabled, which also means
1450 // whether the matching shortcut key will function.
1451 switch (mMenuState) {
1452 case EMPTY_MENU:
1453 if (mCurrentMenuState != mMenuState) {
1454 menu.setGroupVisible(R.id.MAIN_MENU, false);
1455 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1456 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1457 }
1458 break;
1459 default:
1460 if (mCurrentMenuState != mMenuState) {
1461 menu.setGroupVisible(R.id.MAIN_MENU, true);
1462 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1463 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1464 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001465 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001466 break;
1467 }
1468 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001469 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001470 }
1471
Michael Kolb4bf79712011-07-14 14:18:12 -07001472 @Override
1473 public void updateMenuState(Tab tab, Menu menu) {
1474 boolean canGoBack = false;
1475 boolean canGoForward = false;
1476 boolean isHome = false;
1477 if (tab != null) {
1478 canGoBack = tab.canGoBack();
1479 canGoForward = tab.canGoForward();
1480 isHome = mSettings.getHomePage().equals(tab.getUrl());
1481 }
1482 final MenuItem back = menu.findItem(R.id.back_menu_id);
1483 back.setEnabled(canGoBack);
1484
1485 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1486 home.setEnabled(!isHome);
1487
1488 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1489 forward.setEnabled(canGoForward);
1490
1491 final MenuItem source = menu.findItem(mInLoad ? R.id.stop_menu_id : R.id.reload_menu_id);
1492 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1493 dest.setTitle(source.getTitle());
1494 dest.setIcon(source.getIcon());
1495
1496 // decide whether to show the share link option
1497 PackageManager pm = mActivity.getPackageManager();
1498 Intent send = new Intent(Intent.ACTION_SEND);
1499 send.setType("text/plain");
1500 ResolveInfo ri = pm.resolveActivity(send,
1501 PackageManager.MATCH_DEFAULT_ONLY);
1502 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1503
1504 boolean isNavDump = mSettings.enableNavDump();
1505 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1506 nav.setVisible(isNavDump);
1507 nav.setEnabled(isNavDump);
1508
1509 boolean showDebugSettings = mSettings.isDebugEnabled();
1510 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1511 counter.setVisible(showDebugSettings);
1512 counter.setEnabled(showDebugSettings);
1513
1514 MenuItem saveSnapshot = menu.findItem(R.id.save_snapshot_menu_id);
1515 saveSnapshot.setVisible(tab != null && !tab.isSnapshot());
1516 }
1517
Michael Kolb8233fac2010-10-26 16:08:53 -07001518 public boolean onOptionsItemSelected(MenuItem item) {
John Reckb3417f02011-01-14 11:01:05 -08001519 if (mOptionsMenuHandler != null &&
1520 mOptionsMenuHandler.onOptionsItemSelected(item)) {
1521 return true;
1522 }
1523
Michael Kolb8233fac2010-10-26 16:08:53 -07001524 if (item.getGroupId() != R.id.CONTEXT_MENU) {
1525 // menu remains active, so ensure comboview is dismissed
1526 // if main menu option is selected
1527 removeComboView();
1528 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001529 if (null == getCurrentTopWebView()) {
1530 return false;
1531 }
1532 if (mMenuIsDown) {
1533 // The shortcut action consumes the MENU. Even if it is still down,
1534 // it won't trigger the next shortcut action. In the case of the
1535 // shortcut action triggering a new activity, like Bookmarks, we
1536 // won't get onKeyUp for MENU. So it is important to reset it here.
1537 mMenuIsDown = false;
1538 }
1539 switch (item.getItemId()) {
1540 // -- Main menu
1541 case R.id.new_tab_menu_id:
1542 openTabToHomePage();
1543 break;
1544
1545 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001546 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001547 break;
1548
1549 case R.id.goto_menu_id:
1550 editUrl();
1551 break;
1552
1553 case R.id.bookmarks_menu_id:
1554 bookmarksOrHistoryPicker(false);
1555 break;
1556
Michael Kolb8233fac2010-10-26 16:08:53 -07001557 case R.id.add_bookmark_menu_id:
John Reck3ffc5ca2011-06-10 15:56:06 -07001558 bookmarkCurrentPage(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001559 break;
1560
1561 case R.id.stop_reload_menu_id:
1562 if (mInLoad) {
1563 stopLoading();
1564 } else {
1565 getCurrentTopWebView().reload();
1566 }
1567 break;
1568
1569 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001570 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001571 break;
1572
1573 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001574 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001575 break;
1576
1577 case R.id.close_menu_id:
1578 // Close the subwindow if it exists.
1579 if (mTabControl.getCurrentSubWindow() != null) {
1580 dismissSubWindow(mTabControl.getCurrentTab());
1581 break;
1582 }
1583 closeCurrentTab();
1584 break;
1585
1586 case R.id.homepage_menu_id:
1587 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001588 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001589 break;
1590
1591 case R.id.preferences_menu_id:
1592 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1593 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1594 getCurrentTopWebView().getUrl());
1595 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1596 break;
1597
1598 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001599 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001600 break;
1601
John Reck2bc80422011-06-30 15:11:49 -07001602 case R.id.save_snapshot_menu_id:
1603 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001604 if (source == null) break;
John Reckd8c74522011-06-14 08:45:00 -07001605 final ContentResolver cr = mActivity.getContentResolver();
1606 final ContentValues values = source.createSnapshotValues();
John Reck2bc80422011-06-30 15:11:49 -07001607 if (values != null) {
1608 new AsyncTask<Tab, Void, Long>() {
John Reckd8c74522011-06-14 08:45:00 -07001609
John Reck2bc80422011-06-30 15:11:49 -07001610 @Override
1611 protected Long doInBackground(Tab... params) {
1612 Uri result = cr.insert(Snapshots.CONTENT_URI, values);
1613 long id = ContentUris.parseId(result);
1614 return id;
John Reckd8c74522011-06-14 08:45:00 -07001615 }
John Reckf33b1632011-06-04 20:00:23 -07001616
John Reck2bc80422011-06-30 15:11:49 -07001617 @Override
1618 protected void onPostExecute(Long id) {
1619 Bundle b = new Bundle();
1620 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1621 mUi.showComboView(ComboViews.Snapshots, b);
1622 };
1623 }.execute(source);
1624 } else {
1625 Toast.makeText(mActivity, R.string.snapshot_failed,
Leon Scrogginsac993842011-02-02 12:54:07 -05001626 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001627 }
Leon Scrogginsac993842011-02-02 12:54:07 -05001628 break;
1629
Michael Kolb8233fac2010-10-26 16:08:53 -07001630 case R.id.page_info_menu_id:
Huahui Wuae0c0412011-06-28 10:17:05 -07001631 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
Michael Kolb8233fac2010-10-26 16:08:53 -07001632 break;
1633
1634 case R.id.classic_history_menu_id:
1635 bookmarksOrHistoryPicker(true);
1636 break;
1637
1638 case R.id.title_bar_share_page_url:
1639 case R.id.share_page_menu_id:
1640 Tab currentTab = mTabControl.getCurrentTab();
1641 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001642 return false;
1643 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001644 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001645 break;
1646
1647 case R.id.dump_nav_menu_id:
1648 getCurrentTopWebView().debugDump();
1649 break;
1650
1651 case R.id.dump_counters_menu_id:
1652 getCurrentTopWebView().dumpV8Counters();
1653 break;
1654
1655 case R.id.zoom_in_menu_id:
1656 getCurrentTopWebView().zoomIn();
1657 break;
1658
1659 case R.id.zoom_out_menu_id:
1660 getCurrentTopWebView().zoomOut();
1661 break;
1662
1663 case R.id.view_downloads_menu_id:
1664 viewDownloads();
1665 break;
1666
1667 case R.id.window_one_menu_id:
1668 case R.id.window_two_menu_id:
1669 case R.id.window_three_menu_id:
1670 case R.id.window_four_menu_id:
1671 case R.id.window_five_menu_id:
1672 case R.id.window_six_menu_id:
1673 case R.id.window_seven_menu_id:
1674 case R.id.window_eight_menu_id:
1675 {
1676 int menuid = item.getItemId();
1677 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1678 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1679 Tab desiredTab = mTabControl.getTab(id);
1680 if (desiredTab != null &&
1681 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001682 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001683 }
1684 break;
1685 }
1686 }
1687 }
1688 break;
1689
1690 default:
1691 return false;
1692 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001693 return true;
1694 }
1695
1696 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001697 // Let the History and Bookmark fragments handle menus they created.
1698 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1699 return false;
1700 }
1701
Michael Kolb8233fac2010-10-26 16:08:53 -07001702 int id = item.getItemId();
1703 boolean result = true;
1704 switch (id) {
1705 // For the context menu from the title bar
1706 case R.id.title_bar_copy_page_url:
1707 Tab currentTab = mTabControl.getCurrentTab();
1708 if (null == currentTab) {
1709 result = false;
1710 break;
1711 }
1712 WebView mainView = currentTab.getWebView();
1713 if (null == mainView) {
1714 result = false;
1715 break;
1716 }
1717 copy(mainView.getUrl());
1718 break;
1719 // -- Browser context menu
1720 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001721 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001722 case R.id.copy_link_context_menu_id:
1723 final WebView webView = getCurrentTopWebView();
1724 if (null == webView) {
1725 result = false;
1726 break;
1727 }
1728 final HashMap<String, WebView> hrefMap =
1729 new HashMap<String, WebView>();
1730 hrefMap.put("webview", webView);
1731 final Message msg = mHandler.obtainMessage(
1732 FOCUS_NODE_HREF, id, 0, hrefMap);
1733 webView.requestFocusNodeHref(msg);
1734 break;
1735
1736 default:
1737 // For other context menus
1738 result = onOptionsItemSelected(item);
1739 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001740 return result;
1741 }
1742
1743 /**
1744 * support programmatically opening the context menu
1745 */
1746 public void openContextMenu(View view) {
1747 mActivity.openContextMenu(view);
1748 }
1749
1750 /**
1751 * programmatically open the options menu
1752 */
1753 public void openOptionsMenu() {
1754 mActivity.openOptionsMenu();
1755 }
1756
1757 public boolean onMenuOpened(int featureId, Menu menu) {
1758 if (mOptionsMenuOpen) {
1759 if (mConfigChanged) {
1760 // We do not need to make any changes to the state of the
1761 // title bar, since the only thing that happened was a
1762 // change in orientation
1763 mConfigChanged = false;
1764 } else {
1765 if (!mExtendedMenuOpen) {
1766 mExtendedMenuOpen = true;
1767 mUi.onExtendedMenuOpened();
1768 } else {
1769 // Switching the menu back to icon view, so show the
1770 // title bar once again.
1771 mExtendedMenuOpen = false;
1772 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001773 }
1774 }
1775 } else {
1776 // The options menu is closed, so open it, and show the title
1777 mOptionsMenuOpen = true;
1778 mConfigChanged = false;
1779 mExtendedMenuOpen = false;
1780 mUi.onOptionsMenuOpened();
1781 }
1782 return true;
1783 }
1784
1785 public void onOptionsMenuClosed(Menu menu) {
1786 mOptionsMenuOpen = false;
1787 mUi.onOptionsMenuClosed(mInLoad);
1788 }
1789
1790 public void onContextMenuClosed(Menu menu) {
1791 mUi.onContextMenuClosed(menu, mInLoad);
1792 }
1793
1794 // Helper method for getting the top window.
1795 @Override
1796 public WebView getCurrentTopWebView() {
1797 return mTabControl.getCurrentTopWebView();
1798 }
1799
1800 @Override
1801 public WebView getCurrentWebView() {
1802 return mTabControl.getCurrentWebView();
1803 }
1804
1805 /*
1806 * This method is called as a result of the user selecting the options
1807 * menu to see the download window. It shows the download window on top of
1808 * the current window.
1809 */
1810 void viewDownloads() {
1811 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1812 mActivity.startActivity(intent);
1813 }
1814
1815 // action mode
1816
1817 void onActionModeStarted(ActionMode mode) {
1818 mUi.onActionModeStarted(mode);
1819 mActionMode = mode;
1820 }
1821
1822 /*
1823 * True if a custom ActionMode (i.e. find or select) is in use.
1824 */
1825 @Override
1826 public boolean isInCustomActionMode() {
1827 return mActionMode != null;
1828 }
1829
1830 /*
1831 * End the current ActionMode.
1832 */
1833 @Override
1834 public void endActionMode() {
1835 if (mActionMode != null) {
1836 mActionMode.finish();
1837 }
1838 }
1839
1840 /*
1841 * Called by find and select when they are finished. Replace title bars
1842 * as necessary.
1843 */
1844 public void onActionModeFinished(ActionMode mode) {
1845 if (!isInCustomActionMode()) return;
1846 mUi.onActionModeFinished(mInLoad);
1847 mActionMode = null;
1848 }
1849
1850 boolean isInLoad() {
1851 return mInLoad;
1852 }
1853
1854 // bookmark handling
1855
1856 /**
1857 * add the current page as a bookmark to the given folder id
1858 * @param folderId use -1 for the default folder
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001859 * @param canBeAnEdit If true, check to see whether the site is already
1860 * bookmarked, and if it is, edit that bookmark. If false, and
1861 * the site is already bookmarked, do not attempt to edit the
1862 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001863 */
1864 @Override
John Reck3ffc5ca2011-06-10 15:56:06 -07001865 public void bookmarkCurrentPage(boolean canBeAnEdit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001866 Intent i = new Intent(mActivity,
1867 AddBookmarkPage.class);
1868 WebView w = getCurrentTopWebView();
1869 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1870 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1871 String touchIconUrl = w.getTouchIconUrl();
1872 if (touchIconUrl != null) {
1873 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1874 WebSettings settings = w.getSettings();
1875 if (settings != null) {
1876 i.putExtra(AddBookmarkPage.USER_AGENT,
1877 settings.getUserAgentString());
1878 }
1879 }
1880 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1881 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1882 getDesiredThumbnailHeight(mActivity)));
1883 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001884 if (canBeAnEdit) {
1885 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1886 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001887 // Put the dialog at the upper right of the screen, covering the
1888 // star on the title bar.
1889 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
1890 mActivity.startActivity(i);
1891 }
1892
1893 // file chooser
1894 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1895 mUploadHandler = new UploadHandler(this);
1896 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1897 }
1898
1899 // thumbnails
1900
1901 /**
1902 * Return the desired width for thumbnail screenshots, which are stored in
1903 * the database, and used on the bookmarks screen.
1904 * @param context Context for finding out the density of the screen.
1905 * @return desired width for thumbnail screenshot.
1906 */
1907 static int getDesiredThumbnailWidth(Context context) {
1908 return context.getResources().getDimensionPixelOffset(
1909 R.dimen.bookmarkThumbnailWidth);
1910 }
1911
1912 /**
1913 * Return the desired height for thumbnail screenshots, which are stored in
1914 * the database, and used on the bookmarks screen.
1915 * @param context Context for finding out the density of the screen.
1916 * @return desired height for thumbnail screenshot.
1917 */
1918 static int getDesiredThumbnailHeight(Context context) {
1919 return context.getResources().getDimensionPixelOffset(
1920 R.dimen.bookmarkThumbnailHeight);
1921 }
1922
Michael Kolb1acef692011-03-08 14:12:06 -08001923 static Bitmap createScreenshot(Tab tab, int width, int height) {
1924 if ((tab != null) && (tab.getWebView() != null)) {
1925 return createScreenshot(tab.getWebView(), width, height);
1926 }
1927 return null;
1928 }
1929
John Reck8cc92352011-07-06 17:41:52 -07001930 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08001931 // We render to a bitmap 2x the desired size so that we can then
1932 // re-scale it with filtering since canvas.scale doesn't filter
1933 // This helps reduce aliasing at the cost of being slightly blurry
1934 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07001935 Picture thumbnail = view.capturePicture();
1936 if (thumbnail == null) {
1937 return null;
1938 }
John Reck5c6ac2f2011-01-05 10:18:03 -08001939 width *= filter_scale;
1940 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07001941 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1942 Canvas canvas = new Canvas(bm);
1943 // May need to tweak these values to determine what is the
1944 // best scale factor
1945 int thumbnailWidth = thumbnail.getWidth();
1946 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08001947 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08001948 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08001949 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07001950 } else {
1951 return null;
1952 }
John Reckfe49ab42010-11-16 17:09:37 -08001953
Michael Kolbeb95db42011-03-03 10:38:40 -08001954 float scaleFactorY = (float) height / (float)thumbnailHeight;
1955 if (scaleFactorY > scaleFactor) {
1956 // The picture is narrower than the requested AR
1957 // Center the thumnail and crop the sides
1958 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08001959 float wx = (thumbnailWidth * scaleFactor) - width;
1960 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07001961 }
1962
John Reckfe49ab42010-11-16 17:09:37 -08001963 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07001964
1965 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08001966 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
1967 height / filter_scale, true);
1968 bm.recycle();
1969 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07001970 }
1971
John Reck34ef2672011-02-10 11:30:55 -08001972 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001973 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07001974 // FIXME: Would like to make sure there is actually something to
1975 // draw, but the API for that (WebViewCore.pictureReady()) is not
1976 // currently accessible here.
1977
John Reck34ef2672011-02-10 11:30:55 -08001978 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08001979 if (view == null) {
1980 // Tab was destroyed
1981 return;
1982 }
John Reck34ef2672011-02-10 11:30:55 -08001983 final String url = tab.getUrl();
1984 final String originalUrl = view.getOriginalUrl();
1985
1986 if (TextUtils.isEmpty(url)) {
1987 return;
1988 }
1989
1990 // Only update thumbnails for web urls (http(s)://), not for
1991 // about:, javascript:, data:, etc...
1992 // Unless it is a bookmarked site, then always update
1993 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
1994 return;
1995 }
1996
Michael Kolb8233fac2010-10-26 16:08:53 -07001997 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
1998 getDesiredThumbnailHeight(mActivity));
1999 if (bm == null) {
2000 return;
2001 }
2002
2003 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002004 new AsyncTask<Void, Void, Void>() {
2005 @Override
2006 protected Void doInBackground(Void... unused) {
2007 Cursor cursor = null;
2008 try {
2009 // TODO: Clean this up
2010 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2011 if (cursor != null && cursor.moveToFirst()) {
2012 final ByteArrayOutputStream os =
2013 new ByteArrayOutputStream();
2014 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002015
John Reck34ef2672011-02-10 11:30:55 -08002016 ContentValues values = new ContentValues();
2017 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002018
John Reck34ef2672011-02-10 11:30:55 -08002019 do {
John Reck617fd832011-02-16 14:35:59 -08002020 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002021 cr.update(Images.CONTENT_URI, values, null, null);
2022 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002023 }
John Reck34ef2672011-02-10 11:30:55 -08002024 } catch (IllegalStateException e) {
2025 // Ignore
2026 } finally {
2027 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002028 }
John Reck34ef2672011-02-10 11:30:55 -08002029 return null;
2030 }
2031 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002032 }
2033
2034 private class Copy implements OnMenuItemClickListener {
2035 private CharSequence mText;
2036
2037 public boolean onMenuItemClick(MenuItem item) {
2038 copy(mText);
2039 return true;
2040 }
2041
2042 public Copy(CharSequence toCopy) {
2043 mText = toCopy;
2044 }
2045 }
2046
Leon Scroggins63c02662010-11-18 15:16:27 -05002047 private static class Download implements OnMenuItemClickListener {
2048 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002049 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002050 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002051
2052 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002053 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002054 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002055 return true;
2056 }
2057
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002058 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002059 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002060 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002061 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002062 }
2063 }
2064
Cary Clark8974d282010-11-22 10:46:05 -05002065 private static class SelectText implements OnMenuItemClickListener {
2066 private WebView mWebView;
2067
2068 public boolean onMenuItemClick(MenuItem item) {
2069 if (mWebView != null) {
2070 return mWebView.selectText();
2071 }
2072 return false;
2073 }
2074
2075 public SelectText(WebView webView) {
2076 mWebView = webView;
2077 }
2078
2079 }
2080
Michael Kolb8233fac2010-10-26 16:08:53 -07002081 /********************** TODO: UI stuff *****************************/
2082
2083 // these methods have been copied, they still need to be cleaned up
2084
2085 /****************** tabs ***************************************************/
2086
2087 // basic tab interactions:
2088
2089 // it is assumed that tabcontrol already knows about the tab
2090 protected void addTab(Tab tab) {
2091 mUi.addTab(tab);
2092 }
2093
2094 protected void removeTab(Tab tab) {
2095 mUi.removeTab(tab);
2096 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002097 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002098 }
2099
Michael Kolbf2055602011-04-09 17:20:03 -07002100 @Override
2101 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002102 // monkey protection against delayed start
2103 if (tab != null) {
2104 mTabControl.setCurrentTab(tab);
2105 // the tab is guaranteed to have a webview after setCurrentTab
2106 mUi.setActiveTab(tab);
2107 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002108 }
2109
2110 protected void closeEmptyChildTab() {
2111 Tab current = mTabControl.getCurrentTab();
2112 if (current != null
2113 && current.getWebView().copyBackForwardList().getSize() == 0) {
Michael Kolbc831b632011-05-11 09:30:34 -07002114 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002115 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002116 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002117 closeTab(current);
2118 }
2119 }
2120 }
2121
John Reck26b18322011-06-21 13:08:58 -07002122 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002123 // Dismiss the subwindow if applicable.
2124 dismissSubWindow(appTab);
2125 // Since we might kill the WebView, remove it from the
2126 // content view first.
2127 mUi.detachTab(appTab);
2128 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002129 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002130 // TODO: analyze why the remove and add are necessary
2131 mUi.attachTab(appTab);
2132 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002133 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002134 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002135 } else {
2136 // If the tab was the current tab, we have to attach
2137 // it to the view system again.
2138 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002139 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002140 }
2141 }
2142
2143 // Remove the sub window if it exists. Also called by TabControl when the
2144 // user clicks the 'X' to dismiss a sub window.
2145 public void dismissSubWindow(Tab tab) {
2146 removeSubWindow(tab);
2147 // dismiss the subwindow. This will destroy the WebView.
2148 tab.dismissSubWindow();
2149 getCurrentTopWebView().requestFocus();
2150 }
2151
2152 @Override
2153 public void removeSubWindow(Tab t) {
2154 if (t.getSubWebView() != null) {
2155 mUi.removeSubWindow(t.getSubViewContainer());
2156 }
2157 }
2158
2159 @Override
2160 public void attachSubWindow(Tab tab) {
2161 if (tab.getSubWebView() != null) {
2162 mUi.attachSubWindow(tab.getSubViewContainer());
2163 getCurrentTopWebView().requestFocus();
2164 }
2165 }
2166
Mathew Inwood29721c22011-06-29 17:55:24 +01002167 private Tab showPreloadedTab(final UrlData urlData) {
2168 if (!urlData.isPreloaded()) {
2169 return null;
2170 }
2171 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2172 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2173 if (sbQuery != null) {
2174 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
2175 // Could not submit query. Fallback to regular tab creation
2176 tabControl.destroy();
2177 return null;
2178 }
2179 }
2180 Tab t = tabControl.getTab();
2181 mTabControl.addPreloadedTab(t);
2182 addTab(t);
2183 setActiveTab(t);
2184 return t;
2185 }
2186
Michael Kolb7bcafde2011-05-09 13:55:59 -07002187 // open a non inconito tab with the given url data
2188 // and set as active tab
2189 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002190 Tab tab = showPreloadedTab(urlData);
2191 if (tab == null) {
2192 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002193 if ((tab != null) && !urlData.isEmpty()) {
2194 loadUrlDataIn(tab, urlData);
2195 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002196 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002197 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002198 }
2199
Michael Kolb843510f2010-12-09 10:51:49 -08002200 @Override
2201 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002202 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002203 }
2204
Michael Kolb8233fac2010-10-26 16:08:53 -07002205 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002206 public Tab openIncognitoTab() {
2207 return openTab(INCOGNITO_URI, true, true, false);
2208 }
2209
2210 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002211 public Tab openTab(String url, boolean incognito, boolean setActive,
2212 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002213 return openTab(url, incognito, setActive, useCurrent, null);
2214 }
2215
2216 @Override
2217 public Tab openTab(String url, Tab parent, boolean setActive,
2218 boolean useCurrent) {
2219 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2220 setActive, useCurrent, parent);
2221 }
2222
2223 public Tab openTab(String url, boolean incognito, boolean setActive,
2224 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002225 Tab tab = createNewTab(incognito, setActive, useCurrent);
2226 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002227 if (parent != null && parent != tab) {
2228 parent.addChildTab(tab);
2229 }
Michael Kolb519d2282011-05-09 17:03:19 -07002230 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002231 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002232 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002233 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002234 return tab;
2235 }
2236
2237 // this method will attempt to create a new tab
2238 // incognito: private browsing tab
2239 // setActive: ste tab as current tab
2240 // useCurrent: if no new tab can be created, return current tab
2241 private Tab createNewTab(boolean incognito, boolean setActive,
2242 boolean useCurrent) {
2243 Tab tab = null;
2244 if (mTabControl.canCreateNewTab()) {
2245 tab = mTabControl.createNewTab(incognito);
2246 addTab(tab);
2247 if (setActive) {
2248 setActiveTab(tab);
2249 }
2250 } else {
2251 if (useCurrent) {
2252 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002253 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002254 } else {
2255 mUi.showMaxTabsWarning();
2256 }
2257 }
2258 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002259 }
2260
John Reck2bc80422011-06-30 15:11:49 -07002261 @Override
2262 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2263 SnapshotTab tab = null;
2264 if (mTabControl.canCreateNewTab()) {
2265 tab = mTabControl.createSnapshotTab(snapshotId);
2266 addTab(tab);
2267 if (setActive) {
2268 setActiveTab(tab);
2269 }
2270 } else {
2271 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002272 }
2273 return tab;
2274 }
2275
Michael Kolb8233fac2010-10-26 16:08:53 -07002276 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002277 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002278 * @return boolean True if we successfully switched to a different tab. If
2279 * the indexth tab is null, or if that tab is the same as
2280 * the current one, return false.
2281 */
2282 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002283 public boolean switchToTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002284 // hide combo view if open
2285 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002286 Tab currentTab = mTabControl.getCurrentTab();
2287 if (tab == null || tab == currentTab) {
2288 return false;
2289 }
2290 setActiveTab(tab);
2291 return true;
2292 }
2293
2294 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002295 public void closeCurrentTab() {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002296 // hide combo view if open
2297 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002298 if (mTabControl.getTabCount() == 1) {
John Reck958b2422010-12-03 17:56:17 -08002299 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002300 return;
2301 }
Michael Kolbc831b632011-05-11 09:30:34 -07002302 final Tab current = mTabControl.getCurrentTab();
2303 final int pos = mTabControl.getCurrentPosition();
2304 Tab newTab = current.getParent();
2305 if (newTab == null) {
2306 newTab = mTabControl.getTab(pos + 1);
2307 if (newTab == null) {
2308 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002309 }
2310 }
Michael Kolbc831b632011-05-11 09:30:34 -07002311 if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002312 // Close window
2313 closeTab(current);
2314 }
2315 }
2316
2317 /**
2318 * Close the tab, remove its associated title bar, and adjust mTabControl's
2319 * current tab to a valid value.
2320 */
2321 @Override
2322 public void closeTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002323 // hide combo view if open
2324 removeComboView();
Michael Kolb2d59c322011-01-25 13:18:55 -08002325 removeTab(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002326 }
2327
Michael Kolb8233fac2010-10-26 16:08:53 -07002328 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002329 protected void loadUrlFromContext(String url) {
2330 Tab tab = getCurrentTab();
2331 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002332 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002333 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002334 url = UrlUtils.smartUrlFilter(url);
2335 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002336 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002337 }
2338 }
2339 }
2340
2341 /**
2342 * Load the URL into the given WebView and update the title bar
2343 * to reflect the new load. Call this instead of WebView.loadUrl
2344 * directly.
2345 * @param view The WebView used to load url.
2346 * @param url The URL to load.
2347 */
John Reck71e51422011-07-01 16:49:28 -07002348 @Override
2349 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002350 loadUrl(tab, url, null);
2351 }
2352
2353 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2354 if (tab != null) {
2355 dismissSubWindow(tab);
2356 tab.loadUrl(url, headers);
2357 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002358 }
2359
2360 /**
2361 * Load UrlData into a Tab and update the title bar to reflect the new
2362 * load. Call this instead of UrlData.loadIn directly.
2363 * @param t The Tab used to load.
2364 * @param data The UrlData being loaded.
2365 */
2366 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002367 if (data != null) {
2368 if (data.mVoiceIntent != null) {
2369 t.activateVoiceSearchMode(data.mVoiceIntent);
Michael Kolb14612442011-06-24 13:06:29 -07002370 } else if (data.isPreloaded()) {
2371 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002372 } else {
2373 loadUrl(t, data.mUrl, data.mHeaders);
2374 }
2375 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002376 }
2377
John Reck30c714c2010-12-16 17:30:34 -08002378 @Override
2379 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002380 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002381 if (tab.canGoBack()) {
2382 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002383 } else {
John Reckef654f12011-07-12 16:42:08 -07002384 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002385 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002386 }
2387
2388 void goBackOnePageOrQuit() {
2389 Tab current = mTabControl.getCurrentTab();
2390 if (current == null) {
2391 /*
2392 * Instead of finishing the activity, simply push this to the back
2393 * of the stack and let ActivityManager to choose the foreground
2394 * activity. As BrowserActivity is singleTask, it will be always the
2395 * root of the task. So we can use either true or false for
2396 * moveTaskToBack().
2397 */
2398 mActivity.moveTaskToBack(true);
2399 return;
2400 }
John Reckef654f12011-07-12 16:42:08 -07002401 if (current.canGoBack()) {
2402 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002403 } else {
2404 // Check to see if we are closing a window that was created by
2405 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002406 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002407 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002408 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002409 // Now we close the other tab
2410 closeTab(current);
2411 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002412 /*
2413 * Instead of finishing the activity, simply push this to the back
2414 * of the stack and let ActivityManager to choose the foreground
2415 * activity. As BrowserActivity is singleTask, it will be always the
2416 * root of the task. So we can use either true or false for
2417 * moveTaskToBack().
2418 */
2419 mActivity.moveTaskToBack(true);
2420 }
2421 }
2422 }
2423
2424 /**
2425 * Feed the previously stored results strings to the BrowserProvider so that
2426 * the SearchDialog will show them instead of the standard searches.
2427 * @param result String to show on the editable line of the SearchDialog.
2428 */
2429 @Override
2430 public void showVoiceSearchResults(String result) {
2431 ContentProviderClient client = mActivity.getContentResolver()
2432 .acquireContentProviderClient(Browser.BOOKMARKS_URI);
2433 ContentProvider prov = client.getLocalContentProvider();
2434 BrowserProvider bp = (BrowserProvider) prov;
2435 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
2436 client.release();
2437
2438 Bundle bundle = createGoogleSearchSourceBundle(
2439 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
2440 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
2441 startSearch(result, false, bundle, false);
2442 }
2443
2444 private void startSearch(String initialQuery, boolean selectInitialQuery,
2445 Bundle appSearchData, boolean globalSearch) {
2446 if (appSearchData == null) {
2447 appSearchData = createGoogleSearchSourceBundle(
2448 GOOGLE_SEARCH_SOURCE_TYPE);
2449 }
2450
2451 SearchEngine searchEngine = mSettings.getSearchEngine();
2452 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
2453 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
2454 }
2455 mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData,
2456 globalSearch);
2457 }
2458
2459 private Bundle createGoogleSearchSourceBundle(String source) {
2460 Bundle bundle = new Bundle();
2461 bundle.putString(Search.SOURCE, source);
2462 return bundle;
2463 }
2464
2465 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002466 * helper method for key handler
2467 * returns the current tab if it can't advance
2468 */
Michael Kolbc831b632011-05-11 09:30:34 -07002469 private Tab getNextTab() {
2470 return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1,
2471 mTabControl.getCurrentPosition() + 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002472 }
2473
2474 /**
2475 * helper method for key handler
2476 * returns the current tab if it can't advance
2477 */
Michael Kolbc831b632011-05-11 09:30:34 -07002478 private Tab getPrevTab() {
2479 return mTabControl.getTab(Math.max(0,
2480 mTabControl.getCurrentPosition() - 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002481 }
2482
2483 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002484 * handle key events in browser
2485 *
2486 * @param keyCode
2487 * @param event
2488 * @return true if handled, false to pass to super
2489 */
2490 boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002491 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002492 // Even if MENU is already held down, we need to call to super to open
2493 // the IME on long press.
Michael Kolb2814a362011-05-19 15:49:41 -07002494 if (KeyEvent.KEYCODE_MENU == keyCode) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002495 if (mOptionsMenuHandler != null) {
2496 return false;
2497 } else {
2498 event.startTracking();
2499 return true;
2500 }
Michael Kolb2814a362011-05-19 15:49:41 -07002501 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002502 if (!noModifiers
2503 && ((KeyEvent.KEYCODE_MENU == keyCode)
2504 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2505 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002506 mMenuIsDown = true;
2507 return false;
2508 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002509
Cary Clark8ff8c662010-12-29 15:03:05 -05002510 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002511 Tab tab = getCurrentTab();
2512 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002513
Cary Clark160bbb92011-01-10 11:17:07 -05002514 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2515 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002516
Michael Kolb8233fac2010-10-26 16:08:53 -07002517 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002518 case KeyEvent.KEYCODE_TAB:
2519 if (event.isCtrlPressed()) {
2520 if (event.isShiftPressed()) {
2521 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002522 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002523 } else {
2524 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002525 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002526 }
2527 return true;
2528 }
2529 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002530 case KeyEvent.KEYCODE_SPACE:
2531 // WebView/WebTextView handle the keys in the KeyDown. As
2532 // the Activity's shortcut keys are only handled when WebView
2533 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002534 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002535 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002536 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002537 pageDown();
2538 }
2539 return true;
2540 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002541 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002542 event.startTracking();
2543 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002544 case KeyEvent.KEYCODE_DPAD_LEFT:
2545 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002546 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002547 return true;
2548 }
2549 break;
2550 case KeyEvent.KEYCODE_DPAD_RIGHT:
2551 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002552 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002553 return true;
2554 }
2555 break;
2556 case KeyEvent.KEYCODE_A:
2557 if (ctrl) {
2558 webView.selectAll();
2559 return true;
2560 }
2561 break;
Michael Kolba4183062011-01-16 10:43:21 -08002562// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002563 case KeyEvent.KEYCODE_C:
2564 if (ctrl) {
2565 webView.copySelection();
2566 return true;
2567 }
2568 break;
Michael Kolba4183062011-01-16 10:43:21 -08002569// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002570// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002571// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002572// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002573// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002574// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002575// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002576// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002577// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002578// case KeyEvent.KEYCODE_M: // unused
2579// case KeyEvent.KEYCODE_N: // in Chrome: new window
2580// case KeyEvent.KEYCODE_O: // in Chrome: open file
2581// case KeyEvent.KEYCODE_P: // in Chrome: print page
2582// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002583// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002584// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2585 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002586 // we can't use the ctrl/shift flags, they check for
2587 // exclusive use of a modifier
2588 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002589 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002590 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002591 } else {
2592 openTabToHomePage();
2593 }
2594 return true;
2595 }
2596 break;
2597// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2598// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb1a2eba42011-03-16 16:42:49 -07002599 case KeyEvent.KEYCODE_W: // in Chrome: close tab
2600 if (ctrl) {
2601 closeCurrentTab();
2602 return true;
2603 }
2604 break;
Cary Clark8ff8c662010-12-29 15:03:05 -05002605// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2606// case KeyEvent.KEYCODE_Y: // unused
2607// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002609 // it is a regular key and webview is not null
2610 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002611 }
2612
John Recke6bf4ab2011-02-24 15:48:05 -08002613 boolean onKeyLongPress(int keyCode, KeyEvent event) {
2614 switch(keyCode) {
2615 case KeyEvent.KEYCODE_BACK:
2616 if (mUi.showsWeb()) {
2617 bookmarksOrHistoryPicker(true);
2618 return true;
2619 }
2620 break;
2621 }
2622 return false;
2623 }
2624
Michael Kolb8233fac2010-10-26 16:08:53 -07002625 boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb2814a362011-05-19 15:49:41 -07002626 if (KeyEvent.KEYCODE_MENU == keyCode) {
2627 mMenuIsDown = false;
2628 if (event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002629 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002630 }
2631 }
Cary Clark160bbb92011-01-10 11:17:07 -05002632 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002633 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002634 case KeyEvent.KEYCODE_BACK:
2635 if (event.isTracking() && !event.isCanceled()) {
2636 onBackKey();
2637 return true;
2638 }
2639 break;
2640 }
2641 return false;
2642 }
2643
2644 public boolean isMenuDown() {
2645 return mMenuIsDown;
2646 }
2647
Ben Murdoch8029a772010-11-16 11:58:21 +00002648 public void setupAutoFill(Message message) {
2649 // Open the settings activity at the AutoFill profile fragment so that
2650 // the user can create a new profile. When they return, we will dispatch
2651 // the message so that we can autofill the form using their new profile.
2652 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2653 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2654 AutoFillSettingsFragment.class.getName());
2655 mAutoFillSetupMessage = message;
2656 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2657 }
John Reckb3417f02011-01-14 11:01:05 -08002658
2659 @Override
2660 public void registerOptionsMenuHandler(OptionsMenuHandler handler) {
2661 mOptionsMenuHandler = handler;
2662 }
2663
2664 @Override
2665 public void unregisterOptionsMenuHandler(OptionsMenuHandler handler) {
2666 if (mOptionsMenuHandler == handler) {
2667 mOptionsMenuHandler = null;
2668 }
2669 }
2670
Narayan Kamath5119edd2011-02-23 15:49:17 +00002671 @Override
2672 public void registerDropdownChangeListener(DropdownChangeListener d) {
2673 mUi.registerDropdownChangeListener(d);
2674 }
Michael Kolbfbc579a2011-07-07 15:59:33 -07002675
2676 public boolean onSearchRequested() {
2677 mUi.editUrl(false);
2678 return true;
2679 }
2680
Michael Kolb8233fac2010-10-26 16:08:53 -07002681}