blob: bba49c21a6e462d70074e3208ab679ed9ea78c90 [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;
Mathew Inwood29721c22011-06-29 17:55:24 +010070import android.webkit.SearchBox;
Michael Kolb8233fac2010-10-26 16:08:53 -070071import android.webkit.SslErrorHandler;
72import android.webkit.ValueCallback;
73import android.webkit.WebChromeClient;
74import android.webkit.WebIconDatabase;
75import android.webkit.WebSettings;
76import android.webkit.WebView;
Leon Scrogginsac993842011-02-02 12:54:07 -050077import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070078
Michael Kolb4bd767d2011-05-27 11:33:55 -070079import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070080import com.android.browser.UI.ComboViews;
Michael Kolb4bd767d2011-05-27 11:33:55 -070081import com.android.browser.UI.DropdownChangeListener;
82import com.android.browser.provider.BrowserProvider;
John Reck8cc92352011-07-06 17:41:52 -070083import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070084import com.android.browser.search.SearchEngine;
85import com.android.common.Search;
86
Michael Kolb8233fac2010-10-26 16:08:53 -070087import java.io.ByteArrayOutputStream;
88import java.io.File;
89import java.net.URLEncoder;
90import java.util.Calendar;
91import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080092import java.util.List;
John Reck26b18322011-06-21 13:08:58 -070093import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -070094
95/**
96 * Controller for browser
97 */
98public class Controller
99 implements WebViewController, UiController {
100
101 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800102 private static final String SEND_APP_ID_EXTRA =
103 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700104 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800105
Michael Kolb8233fac2010-10-26 16:08:53 -0700106
107 // public message ids
108 public final static int LOAD_URL = 1001;
109 public final static int STOP_LOAD = 1002;
110
111 // Message Ids
112 private static final int FOCUS_NODE_HREF = 102;
113 private static final int RELEASE_WAKELOCK = 107;
114
115 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
116
117 private static final int OPEN_BOOKMARKS = 201;
118
119 private static final int EMPTY_MENU = -1;
120
Michael Kolb8233fac2010-10-26 16:08:53 -0700121 // activity requestCode
122 final static int PREFERENCES_PAGE = 3;
123 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000124 final static int AUTOFILL_SETUP = 5;
125
Michael Kolb8233fac2010-10-26 16:08:53 -0700126 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
127
128 // As the ids are dynamically created, we can't guarantee that they will
129 // be in sequence, so this static array maps ids to a window number.
130 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
131 { R.id.window_one_menu_id, R.id.window_two_menu_id,
132 R.id.window_three_menu_id, R.id.window_four_menu_id,
133 R.id.window_five_menu_id, R.id.window_six_menu_id,
134 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
135
136 // "source" parameter for Google search through search key
137 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
138 // "source" parameter for Google search through simplily type
139 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
140
Guang Zhu9e78f512011-05-04 11:45:11 -0700141 // "no-crash-recovery" parameter in intetnt to suppress crash recovery
142 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
143
Michael Kolb8233fac2010-10-26 16:08:53 -0700144 private Activity mActivity;
145 private UI mUi;
146 private TabControl mTabControl;
147 private BrowserSettings mSettings;
148 private WebViewFactory mFactory;
John Reckb3417f02011-01-14 11:01:05 -0800149 private OptionsMenuHandler mOptionsMenuHandler = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700150
151 private WakeLock mWakeLock;
152
153 private UrlHandler mUrlHandler;
154 private UploadHandler mUploadHandler;
155 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 private PageDialogsHandler mPageDialogsHandler;
157 private NetworkStateHandler mNetworkHandler;
Martijn Coenenb2f93552011-06-14 10:48:35 +0200158 private NfcHandler mNfcHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700159
Ben Murdoch8029a772010-11-16 11:58:21 +0000160 private Message mAutoFillSetupMessage;
161
Michael Kolb8233fac2010-10-26 16:08:53 -0700162 private boolean mShouldShowErrorConsole;
163
164 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
165
166 // FIXME, temp address onPrepareMenu performance problem.
167 // When we move everything out of view, we should rewrite this.
168 private int mCurrentMenuState = 0;
169 private int mMenuState = R.id.MAIN_MENU;
170 private int mOldMenuState = EMPTY_MENU;
171 private Menu mCachedMenu;
172
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 private boolean mMenuIsDown;
174
175 // For select and find, we keep track of the ActionMode so that
176 // finish() can be called as desired.
177 private ActionMode mActionMode;
178
179 /**
180 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
181 * of whether the configuration has changed. The first onMenuOpened call
182 * after a configuration change is simply a reopening of the same menu
183 * (i.e. mIconView did not change).
184 */
185 private boolean mConfigChanged;
186
187 /**
188 * Keeps track of whether the options menu is open. This is important in
189 * determining whether to show or hide the title bar overlay
190 */
191 private boolean mOptionsMenuOpen;
192
193 /**
194 * Whether or not the options menu is in its bigger, popup menu form. When
195 * true, we want the title bar overlay to be gone. When false, we do not.
196 * Only meaningful if mOptionsMenuOpen is true.
197 */
198 private boolean mExtendedMenuOpen;
199
200 private boolean mInLoad;
201
202 private boolean mActivityPaused = true;
203 private boolean mLoadStopped;
204
205 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500206 // Checks to see when the bookmarks database has changed, and updates the
207 // Tabs' notion of whether they represent bookmarked sites.
208 private ContentObserver mBookmarksObserver;
John Reck0ebd3ac2010-12-09 11:14:04 -0800209 private DataController mDataController;
John Reck847b5322011-04-14 17:02:18 -0700210 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700211
212 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
213 @Override
214 public Void doInBackground(File... files) {
215 if (files != null) {
216 for (File f : files) {
217 if (!f.delete()) {
218 Log.e(LOGTAG, f.getPath() + " was not deleted");
219 }
220 }
221 }
222 return null;
223 }
224 }
225
226 public Controller(Activity browser) {
227 mActivity = browser;
228 mSettings = BrowserSettings.getInstance();
John Reck0ebd3ac2010-12-09 11:14:04 -0800229 mDataController = DataController.getInstance(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700230 mTabControl = new TabControl(this);
231 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700232 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
Michael Kolb14612442011-06-24 13:06:29 -0700233 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700234
235 mUrlHandler = new UrlHandler(this);
236 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700237 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200238 mNfcHandler = new NfcHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700239
240 PowerManager pm = (PowerManager) mActivity
241 .getSystemService(Context.POWER_SERVICE);
242 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
243
244 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500245 mBookmarksObserver = new ContentObserver(mHandler) {
246 @Override
247 public void onChange(boolean selfChange) {
248 int size = mTabControl.getTabCount();
249 for (int i = 0; i < size; i++) {
250 mTabControl.getTab(i).updateBookmarkedStatus();
251 }
252 }
253
254 };
255 browser.getContentResolver().registerContentObserver(
256 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700257
258 mNetworkHandler = new NetworkStateHandler(mActivity, this);
259 // Start watching the default geolocation permissions
260 mSystemAllowGeolocationOrigins =
261 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
262 mSystemAllowGeolocationOrigins.start();
263
264 retainIconsOnStartup();
265 }
266
Patrick Scott7d50a932011-02-04 09:27:26 -0500267 void start(final Bundle icicle, final Intent intent) {
Guang Zhu9e78f512011-05-04 11:45:11 -0700268 boolean noCrashRecovery = intent.getBooleanExtra(NO_CRASH_RECOVERY, false);
269 if (icicle != null || noCrashRecovery) {
John Reck847b5322011-04-14 17:02:18 -0700270 doStart(icicle, intent);
271 } else {
272 mCrashRecoveryHandler.startRecovery(intent);
273 }
274 }
275
276 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 // Unless the last browser usage was within 24 hours, destroy any
278 // remaining incognito tabs.
279
280 Calendar lastActiveDate = icicle != null ?
281 (Calendar) icicle.getSerializable("lastActiveDate") : null;
282 Calendar today = Calendar.getInstance();
283 Calendar yesterday = Calendar.getInstance();
284 yesterday.add(Calendar.DATE, -1);
285
Patrick Scott7d50a932011-02-04 09:27:26 -0500286 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700287 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800288 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700289
Patrick Scott7d50a932011-02-04 09:27:26 -0500290 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700291 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500292 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000293
Michael Kolbc831b632011-05-11 09:30:34 -0700294 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500295 // Not able to restore so we go ahead and clear session cookies. We
296 // must do this before trying to login the user as we don't want to
297 // clear any session cookies set during login.
298 CookieManager.getInstance().removeSessionCookie();
299 }
300
Patrick Scottd43e75a2011-03-14 14:47:23 -0400301 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500302 new Runnable() {
303 @Override public void run() {
Michael Kolbc831b632011-05-11 09:30:34 -0700304 onPreloginFinished(icicle, intent, currentTabId, restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500305 }
306 });
307 }
308
Michael Kolbc831b632011-05-11 09:30:34 -0700309 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500310 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700311 if (currentTabId == -1) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700312 final Bundle extra = intent.getExtras();
313 // Create an initial tab.
314 // If the intent is ACTION_VIEW and data is not null, the Browser is
315 // invoked to view the content by another application. In this case,
316 // the tab will be close when exit.
Michael Kolb14612442011-06-24 13:06:29 -0700317 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
Michael Kolb7bcafde2011-05-09 13:55:59 -0700318 Tab t = null;
319 if (urlData.isEmpty()) {
320 t = openTabToHomePage();
321 } else {
322 t = openTab(urlData);
323 }
324 if (t != null) {
325 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
326 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700327 WebView webView = t.getWebView();
328 if (extra != null) {
329 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
330 if (scale > 0 && scale <= 1000) {
331 webView.setInitialScale(scale);
332 }
333 }
John Reckd8c74522011-06-14 08:45:00 -0700334 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700335 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700336 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500337 mUi.needsRestoreAllTabs());
Michael Kolb1bf23132010-11-19 12:55:12 -0800338 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700339 // TabControl.restoreState() will create a new tab even if
340 // restoring the state fails.
341 setActiveTab(mTabControl.getCurrentTab());
John Reckdb22ec42011-06-29 11:31:24 -0700342 // Handle the intent
343 mIntentHandler.onNewIntent(intent);
Michael Kolb8233fac2010-10-26 16:08:53 -0700344 }
345 // clear up the thumbnail directory, which is no longer used;
346 // ideally this should only be run once after an upgrade from
347 // a previous version of the browser
348 new ClearThumbnails().execute(mTabControl.getThumbnailDir()
349 .listFiles());
350 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700351 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700352 if (jsFlags.trim().length() != 0) {
353 getCurrentWebView().setJsFlags(jsFlags);
354 }
John Reck439c9a52010-12-14 10:04:39 -0800355 if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
356 bookmarksOrHistoryPicker(false);
357 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 }
359
Michael Kolb1514bb72010-11-22 09:11:48 -0800360 @Override
361 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700362 return mFactory;
363 }
364
365 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800366 public void onSetWebView(Tab tab, WebView view) {
367 mUi.onSetWebView(tab, view);
368 }
369
370 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800371 public void createSubWindow(Tab tab) {
372 endActionMode();
373 WebView mainView = tab.getWebView();
374 WebView subView = mFactory.createWebView((mainView == null)
375 ? false
376 : mainView.isPrivateBrowsingEnabled());
377 mUi.createSubWindow(tab, subView);
378 }
379
380 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700381 public Context getContext() {
382 return mActivity;
383 }
384
385 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700386 public Activity getActivity() {
387 return mActivity;
388 }
389
390 void setUi(UI ui) {
391 mUi = ui;
392 }
393
394 BrowserSettings getSettings() {
395 return mSettings;
396 }
397
398 IntentHandler getIntentHandler() {
399 return mIntentHandler;
400 }
401
402 @Override
403 public UI getUi() {
404 return mUi;
405 }
406
407 int getMaxTabs() {
408 return mActivity.getResources().getInteger(R.integer.max_tabs);
409 }
410
411 @Override
412 public TabControl getTabControl() {
413 return mTabControl;
414 }
415
Michael Kolb1bf23132010-11-19 12:55:12 -0800416 @Override
417 public List<Tab> getTabs() {
418 return mTabControl.getTabs();
419 }
420
Michael Kolb8233fac2010-10-26 16:08:53 -0700421 // Open the icon database and retain all the icons for visited sites.
Ben Murdoch9446b932010-11-25 16:20:14 +0000422 // This is done on a background thread so as not to stall startup.
Michael Kolb8233fac2010-10-26 16:08:53 -0700423 private void retainIconsOnStartup() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000424 // WebIconDatabase needs to be retrieved on the UI thread so that if
425 // it has not been created successfully yet the Handler is started on the
426 // UI thread.
427 new RetainIconsOnStartupTask(WebIconDatabase.getInstance()).execute();
428 }
429
430 private class RetainIconsOnStartupTask extends AsyncTask<Void, Void, Void> {
431 private WebIconDatabase mDb;
432
433 public RetainIconsOnStartupTask(WebIconDatabase db) {
434 mDb = db;
435 }
436
John Recka00cbbd2010-12-16 12:38:19 -0800437 @Override
Ben Murdoch9446b932010-11-25 16:20:14 +0000438 protected Void doInBackground(Void... unused) {
439 mDb.open(mActivity.getDir("icons", 0).getPath());
440 Cursor c = null;
441 try {
442 c = Browser.getAllBookmarks(mActivity.getContentResolver());
443 if (c.moveToFirst()) {
444 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
445 do {
446 String url = c.getString(urlIndex);
447 mDb.retainIconForPageUrl(url);
448 } while (c.moveToNext());
449 }
450 } catch (IllegalStateException e) {
451 Log.e(LOGTAG, "retainIconsOnStartup", e);
452 } finally {
453 if (c != null) c.close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700454 }
Ben Murdoch9446b932010-11-25 16:20:14 +0000455
456 return null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700457 }
458 }
459
460 private void startHandler() {
461 mHandler = new Handler() {
462
463 @Override
464 public void handleMessage(Message msg) {
465 switch (msg.what) {
466 case OPEN_BOOKMARKS:
467 bookmarksOrHistoryPicker(false);
468 break;
469 case FOCUS_NODE_HREF:
470 {
471 String url = (String) msg.getData().get("url");
472 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500473 String src = (String) msg.getData().get("src");
474 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700475 if (TextUtils.isEmpty(url)) {
476 break;
477 }
478 HashMap focusNodeMap = (HashMap) msg.obj;
479 WebView view = (WebView) focusNodeMap.get("webview");
480 // Only apply the action if the top window did not change.
481 if (getCurrentTopWebView() != view) {
482 break;
483 }
484 switch (msg.arg1) {
485 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700486 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700487 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500488 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700489 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500490 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500491 case R.id.open_newtab_context_menu_id:
492 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700493 openTab(url, parent,
494 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500495 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700496 case R.id.copy_link_context_menu_id:
497 copy(url);
498 break;
499 case R.id.save_link_context_menu_id:
500 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500501 DownloadHandler.onDownloadStartNoStream(
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000502 mActivity, url, null, null, null,
503 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 break;
505 }
506 break;
507 }
508
509 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700510 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 break;
512
513 case STOP_LOAD:
514 stopLoading();
515 break;
516
517 case RELEASE_WAKELOCK:
518 if (mWakeLock.isHeld()) {
519 mWakeLock.release();
520 // if we reach here, Browser should be still in the
521 // background loading after WAKELOCK_TIMEOUT (5-min).
522 // To avoid burning the battery, stop loading.
523 mTabControl.stopAllLoading();
524 }
525 break;
526
527 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800528 Tab tab = (Tab) msg.obj;
529 if (tab != null) {
530 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700531 }
532 break;
533 }
534 }
535 };
536
537 }
538
Martijn Coenenb2f93552011-06-14 10:48:35 +0200539
540 public Tab getCurrentTab() {
541 return mTabControl.getCurrentTab();
542 }
543
Michael Kolbba99c5d2010-11-29 14:57:41 -0800544 @Override
545 public void shareCurrentPage() {
546 shareCurrentPage(mTabControl.getCurrentTab());
547 }
548
549 private void shareCurrentPage(Tab tab) {
550 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800551 sharePage(mActivity, tab.getTitle(),
552 tab.getUrl(), tab.getFavicon(),
553 createScreenshot(tab.getWebView(),
554 getDesiredThumbnailWidth(mActivity),
555 getDesiredThumbnailHeight(mActivity)));
556 }
557 }
558
Michael Kolb8233fac2010-10-26 16:08:53 -0700559 /**
560 * Share a page, providing the title, url, favicon, and a screenshot. Uses
561 * an {@link Intent} to launch the Activity chooser.
562 * @param c Context used to launch a new Activity.
563 * @param title Title of the page. Stored in the Intent with
564 * {@link Intent#EXTRA_SUBJECT}
565 * @param url URL of the page. Stored in the Intent with
566 * {@link Intent#EXTRA_TEXT}
567 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
568 * with {@link Browser#EXTRA_SHARE_FAVICON}
569 * @param screenshot Bitmap of a screenshot of the page. Stored in the
570 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
571 */
572 static final void sharePage(Context c, String title, String url,
573 Bitmap favicon, Bitmap screenshot) {
574 Intent send = new Intent(Intent.ACTION_SEND);
575 send.setType("text/plain");
576 send.putExtra(Intent.EXTRA_TEXT, url);
577 send.putExtra(Intent.EXTRA_SUBJECT, title);
578 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
579 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
580 try {
581 c.startActivity(Intent.createChooser(send, c.getString(
582 R.string.choosertitle_sharevia)));
583 } catch(android.content.ActivityNotFoundException ex) {
584 // if no app handles it, do nothing
585 }
586 }
587
588 private void copy(CharSequence text) {
589 ClipboardManager cm = (ClipboardManager) mActivity
590 .getSystemService(Context.CLIPBOARD_SERVICE);
591 cm.setText(text);
592 }
593
594 // lifecycle
595
596 protected void onConfgurationChanged(Configuration config) {
597 mConfigChanged = true;
598 if (mPageDialogsHandler != null) {
599 mPageDialogsHandler.onConfigurationChanged(config);
600 }
601 mUi.onConfigurationChanged(config);
602 }
603
604 @Override
605 public void handleNewIntent(Intent intent) {
606 mIntentHandler.onNewIntent(intent);
607 }
608
609 protected void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800610 if (mUi.isCustomViewShowing()) {
611 hideCustomView();
612 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700613 if (mActivityPaused) {
614 Log.e(LOGTAG, "BrowserActivity is already paused.");
615 return;
616 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700617 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800618 Tab tab = mTabControl.getCurrentTab();
619 if (tab != null) {
620 tab.pause();
621 if (!pauseWebViewTimers(tab)) {
622 mWakeLock.acquire();
623 mHandler.sendMessageDelayed(mHandler
624 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
625 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 }
627 mUi.onPause();
628 mNetworkHandler.onPause();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200629 mNfcHandler.onPause();
Michael Kolb8233fac2010-10-26 16:08:53 -0700630
631 WebView.disablePlatformNotifications();
John Reck378a4102011-06-09 16:23:01 -0700632 mCrashRecoveryHandler.backupState();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200633
Michael Kolb8233fac2010-10-26 16:08:53 -0700634 }
635
John Reckaed9c542011-05-27 16:08:53 -0700636 void onSaveInstanceState(Bundle outState, boolean saveImages) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700637 // the default implementation requires each view to have an id. As the
638 // browser handles the state itself and it doesn't use id for the views,
639 // don't call the default implementation. Otherwise it will trigger the
640 // warning like this, "couldn't save which view has focus because the
641 // focused view XXX has no id".
642
643 // Save all the tabs
John Reckaed9c542011-05-27 16:08:53 -0700644 mTabControl.saveState(outState, saveImages);
John Reck24f18262011-06-17 14:47:20 -0700645 if (!outState.isEmpty()) {
646 // Save time so that we know how old incognito tabs (if any) are.
647 outState.putSerializable("lastActiveDate", Calendar.getInstance());
648 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700649 }
650
651 void onResume() {
652 if (!mActivityPaused) {
653 Log.e(LOGTAG, "BrowserActivity is already resumed.");
654 return;
655 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700656 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800657 Tab current = mTabControl.getCurrentTab();
658 if (current != null) {
659 current.resume();
660 resumeWebViewTimers(current);
661 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700662 if (mWakeLock.isHeld()) {
663 mHandler.removeMessages(RELEASE_WAKELOCK);
664 mWakeLock.release();
665 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200666
Michael Kolb8233fac2010-10-26 16:08:53 -0700667 mUi.onResume();
668 mNetworkHandler.onResume();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200669 mNfcHandler.onResume();
Michael Kolb8233fac2010-10-26 16:08:53 -0700670 WebView.enablePlatformNotifications();
671 }
672
Michael Kolb70976932010-11-30 11:34:01 -0800673 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800674 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800675 * @param tab guaranteed non-null
676 */
677 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 boolean inLoad = tab.inPageLoad();
679 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
680 CookieSyncManager.getInstance().startSync();
681 WebView w = tab.getWebView();
682 if (w != null) {
683 w.resumeTimers();
684 }
685 }
686 }
687
Michael Kolb70976932010-11-30 11:34:01 -0800688 /**
689 * Pause all WebView timers using the WebView of the given tab
690 * @param tab
691 * @return true if the timers are paused or tab is null
692 */
693 private boolean pauseWebViewTimers(Tab tab) {
694 if (tab == null) {
695 return true;
696 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 CookieSyncManager.getInstance().stopSync();
698 WebView w = getCurrentWebView();
699 if (w != null) {
700 w.pauseTimers();
701 }
702 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 }
Michael Kolb70976932010-11-30 11:34:01 -0800704 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 }
706
707 void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800708 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700709 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
710 mUploadHandler = null;
711 }
712 if (mTabControl == null) return;
713 mUi.onDestroy();
714 // Remove the current tab and sub window
715 Tab t = mTabControl.getCurrentTab();
716 if (t != null) {
717 dismissSubWindow(t);
718 removeTab(t);
719 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500720 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700721 // Destroy all the tabs
722 mTabControl.destroy();
723 WebIconDatabase.getInstance().close();
724 // Stop watching the default geolocation permissions
725 mSystemAllowGeolocationOrigins.stop();
726 mSystemAllowGeolocationOrigins = null;
727 }
728
729 protected boolean isActivityPaused() {
730 return mActivityPaused;
731 }
732
733 protected void onLowMemory() {
734 mTabControl.freeMemory();
735 }
736
737 @Override
738 public boolean shouldShowErrorConsole() {
739 return mShouldShowErrorConsole;
740 }
741
742 protected void setShouldShowErrorConsole(boolean show) {
743 if (show == mShouldShowErrorConsole) {
744 // Nothing to do.
745 return;
746 }
747 mShouldShowErrorConsole = show;
748 Tab t = mTabControl.getCurrentTab();
749 if (t == null) {
750 // There is no current tab so we cannot toggle the error console
751 return;
752 }
753 mUi.setShouldShowErrorConsole(t, show);
754 }
755
756 @Override
757 public void stopLoading() {
758 mLoadStopped = true;
759 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700760 WebView w = getCurrentTopWebView();
761 w.stopLoading();
Michael Kolb8233fac2010-10-26 16:08:53 -0700762 mUi.onPageStopped(tab);
763 }
764
765 boolean didUserStopLoading() {
766 return mLoadStopped;
767 }
768
769 // WebViewController
770
771 @Override
John Reck324d4402011-01-11 16:56:42 -0800772 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700773
774 // We've started to load a new page. If there was a pending message
775 // to save a screenshot then we will now take the new page and save
776 // an incorrect screenshot. Therefore, remove any pending thumbnail
777 // messages from the queue.
778 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800779 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700780
781 // reset sync timer to avoid sync starts during loading a page
782 CookieSyncManager.getInstance().resetSync();
783
784 if (!mNetworkHandler.isNetworkUp()) {
785 view.setNetworkAvailable(false);
786 }
787
788 // when BrowserActivity just starts, onPageStarted may be called before
789 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
790 // to start the timer. As we won't switch tabs while an activity is in
791 // pause state, we can ensure calling resume and pause in pair.
792 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800793 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700794 }
795 mLoadStopped = false;
796 if (!mNetworkHandler.isNetworkUp()) {
797 mNetworkHandler.createAndShowNetworkDialog();
798 }
799 endActionMode();
800
John Reck30c714c2010-12-16 17:30:34 -0800801 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700802
John Reck324d4402011-01-11 16:56:42 -0800803 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700804 // update the bookmark database for favicon
805 maybeUpdateFavicon(tab, null, url, favicon);
806
807 Performance.tracePageStart(url);
808
809 // Performance probe
810 if (false) {
811 Performance.onPageStarted();
812 }
813
814 }
815
816 @Override
John Reck324d4402011-01-11 16:56:42 -0800817 public void onPageFinished(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800818 mUi.onTabDataChanged(tab);
John Reck324d4402011-01-11 16:56:42 -0800819 if (!tab.isPrivateBrowsingEnabled()
John Reckd8c74522011-06-14 08:45:00 -0700820 && !TextUtils.isEmpty(tab.getUrl())
821 && !tab.isSnapshot()) {
John Recka1696282011-07-08 14:10:37 -0700822 // Only update the bookmark screenshot if the user did not
823 // cancel the load early and there is not already
824 // a pending update for the tab.
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 if (tab.inForeground() && !didUserStopLoading()
826 || !tab.inForeground()) {
John Recka1696282011-07-08 14:10:37 -0700827 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
828 mHandler.sendMessageDelayed(mHandler.obtainMessage(
829 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
830 500);
831 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700832 }
833 }
834 // pause the WebView timer and release the wake lock if it is finished
835 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800836 if (mActivityPaused && pauseWebViewTimers(tab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700837 if (mWakeLock.isHeld()) {
838 mHandler.removeMessages(RELEASE_WAKELOCK);
839 mWakeLock.release();
840 }
841 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200842
Michael Kolb8233fac2010-10-26 16:08:53 -0700843 // Performance probe
844 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800845 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700846 }
847
848 Performance.tracePageFinished();
849 }
850
851 @Override
John Reck30c714c2010-12-16 17:30:34 -0800852 public void onProgressChanged(Tab tab) {
853 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700854
855 if (newProgress == 100) {
856 CookieSyncManager.getInstance().sync();
857 // onProgressChanged() may continue to be called after the main
858 // frame has finished loading, as any remaining sub frames continue
859 // to load. We'll only get called once though with newProgress as
860 // 100 when everything is loaded. (onPageFinished is called once
861 // when the main frame completes loading regardless of the state of
862 // any sub frames so calls to onProgressChanges may continue after
863 // onPageFinished has executed)
864 if (mInLoad) {
865 mInLoad = false;
866 updateInLoadMenuItems(mCachedMenu);
867 }
868 } else {
869 if (!mInLoad) {
870 // onPageFinished may have already been called but a subframe is
871 // still loading and updating the progress. Reset mInLoad and
872 // update the menu items.
873 mInLoad = true;
874 updateInLoadMenuItems(mCachedMenu);
875 }
876 }
John Reck30c714c2010-12-16 17:30:34 -0800877 mUi.onProgressChanged(tab);
878 }
879
880 @Override
881 public void onUpdatedLockIcon(Tab tab) {
882 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 }
884
885 @Override
886 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800887 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800888 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800889 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700890 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
891 return;
892 }
893 // Update the title in the history database if not in private browsing mode
894 if (!tab.isPrivateBrowsingEnabled()) {
John Reck0ebd3ac2010-12-09 11:14:04 -0800895 mDataController.updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700896 }
897 }
898
899 @Override
900 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800901 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700902 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
903 }
904
905 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800906 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
907 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700908 }
909
910 @Override
911 public boolean shouldOverrideKeyEvent(KeyEvent event) {
912 if (mMenuIsDown) {
913 // only check shortcut key when MENU is held
914 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
915 event);
916 } else {
917 return false;
918 }
919 }
920
921 @Override
922 public void onUnhandledKeyEvent(KeyEvent event) {
923 if (!isActivityPaused()) {
924 if (event.getAction() == KeyEvent.ACTION_DOWN) {
925 mActivity.onKeyDown(event.getKeyCode(), event);
926 } else {
927 mActivity.onKeyUp(event.getKeyCode(), event);
928 }
929 }
930 }
931
932 @Override
John Reck324d4402011-01-11 16:56:42 -0800933 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700934 // Don't save anything in private browsing mode
935 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800936 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700937
John Reck324d4402011-01-11 16:56:42 -0800938 if (TextUtils.isEmpty(url)
939 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700940 return;
941 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800942 mDataController.updateVisitedHistory(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700943 WebIconDatabase.getInstance().retainIconForPageUrl(url);
John Reck847b5322011-04-14 17:02:18 -0700944 if (!mActivityPaused) {
945 // Since we clear the state in onPause, don't backup the current
946 // state if we are already paused
947 mCrashRecoveryHandler.backupState();
948 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700949 }
950
951 @Override
952 public void getVisitedHistory(final ValueCallback<String[]> callback) {
953 AsyncTask<Void, Void, String[]> task =
954 new AsyncTask<Void, Void, String[]>() {
955 @Override
956 public String[] doInBackground(Void... unused) {
957 return Browser.getVisitedHistory(mActivity.getContentResolver());
958 }
959 @Override
960 public void onPostExecute(String[] result) {
961 callback.onReceiveValue(result);
962 }
963 };
964 task.execute();
965 }
966
967 @Override
968 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
969 final HttpAuthHandler handler, final String host,
970 final String realm) {
971 String username = null;
972 String password = null;
973
974 boolean reuseHttpAuthUsernamePassword
975 = handler.useHttpAuthUsernamePassword();
976
977 if (reuseHttpAuthUsernamePassword && view != null) {
978 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
979 if (credentials != null && credentials.length == 2) {
980 username = credentials[0];
981 password = credentials[1];
982 }
983 }
984
985 if (username != null && password != null) {
986 handler.proceed(username, password);
987 } else {
988 if (tab.inForeground()) {
989 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
990 } else {
991 handler.cancel();
992 }
993 }
994 }
995
996 @Override
997 public void onDownloadStart(Tab tab, String url, String userAgent,
998 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000999 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -05001000 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001001 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
1002 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001003 // This Tab was opened for the sole purpose of downloading a
1004 // file. Remove it.
1005 if (tab == mTabControl.getCurrentTab()) {
1006 // In this case, the Tab is still on top.
1007 goBackOnePageOrQuit();
1008 } else {
1009 // In this case, it is not.
1010 closeTab(tab);
1011 }
1012 }
1013 }
1014
1015 @Override
1016 public Bitmap getDefaultVideoPoster() {
1017 return mUi.getDefaultVideoPoster();
1018 }
1019
1020 @Override
1021 public View getVideoLoadingProgressView() {
1022 return mUi.getVideoLoadingProgressView();
1023 }
1024
1025 @Override
1026 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1027 SslError error) {
1028 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1029 }
1030
Patrick Scott92066772011-03-10 08:46:27 -05001031 @Override
1032 public void showAutoLogin(Tab tab) {
1033 assert tab.inForeground();
1034 // Update the title bar to show the auto-login request.
1035 mUi.showAutoLogin(tab);
1036 }
1037
1038 @Override
1039 public void hideAutoLogin(Tab tab) {
1040 assert tab.inForeground();
1041 mUi.hideAutoLogin(tab);
1042 }
1043
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 // helper method
1045
1046 /*
1047 * Update the favorites icon if the private browsing isn't enabled and the
1048 * icon is valid.
1049 */
1050 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1051 final String url, Bitmap favicon) {
1052 if (favicon == null) {
1053 return;
1054 }
1055 if (!tab.isPrivateBrowsingEnabled()) {
1056 Bookmarks.updateFavicon(mActivity
1057 .getContentResolver(), originalUrl, url, favicon);
1058 }
1059 }
1060
Leon Scroggins4cd97792010-12-03 15:31:56 -05001061 @Override
1062 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001063 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001064 mUi.bookmarkedStatusHasChanged(tab);
1065 }
1066
Michael Kolb8233fac2010-10-26 16:08:53 -07001067 // end WebViewController
1068
1069 protected void pageUp() {
1070 getCurrentTopWebView().pageUp(false);
1071 }
1072
1073 protected void pageDown() {
1074 getCurrentTopWebView().pageDown(false);
1075 }
1076
1077 // callback from phone title bar
1078 public void editUrl() {
1079 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001080 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001081 }
1082
Michael Kolbcfa3af52010-12-14 10:36:11 -08001083 public void startVoiceSearch() {
1084 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1085 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1086 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1087 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1088 mActivity.getComponentName().flattenToString());
1089 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001090 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001091 mActivity.startActivity(intent);
1092 }
1093
Michael Kolb11d19782011-03-20 10:17:40 -07001094 @Override
1095 public void activateVoiceSearchMode(String title, List<String> results) {
1096 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001097 }
1098
1099 public void revertVoiceSearchMode(Tab tab) {
1100 mUi.revertVoiceTitleBar(tab);
1101 }
1102
Michael Kolb736990c2011-03-20 10:01:20 -07001103 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001104 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001105 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1106 }
1107
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001108 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001109 WebChromeClient.CustomViewCallback callback) {
1110 if (tab.inForeground()) {
1111 if (mUi.isCustomViewShowing()) {
1112 callback.onCustomViewHidden();
1113 return;
1114 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001115 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001116 // Save the menu state and set it to empty while the custom
1117 // view is showing.
1118 mOldMenuState = mMenuState;
1119 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001120 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001121 }
1122 }
1123
1124 @Override
1125 public void hideCustomView() {
1126 if (mUi.isCustomViewShowing()) {
1127 mUi.onHideCustomView();
1128 // Reset the old menu state.
1129 mMenuState = mOldMenuState;
1130 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001131 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001132 }
1133 }
1134
1135 protected void onActivityResult(int requestCode, int resultCode,
1136 Intent intent) {
1137 if (getCurrentTopWebView() == null) return;
1138 switch (requestCode) {
1139 case PREFERENCES_PAGE:
1140 if (resultCode == Activity.RESULT_OK && intent != null) {
1141 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001142 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001143 mTabControl.removeParentChildRelationShips();
1144 }
1145 }
1146 break;
1147 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001148 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001149 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001150 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001151 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001152 case AUTOFILL_SETUP:
1153 // Determine whether a profile was actually set up or not
1154 // and if so, send the message back to the WebTextView to
1155 // fill the form with the new profile.
1156 if (getSettings().getAutoFillProfile() != null) {
1157 mAutoFillSetupMessage.sendToTarget();
1158 mAutoFillSetupMessage = null;
1159 }
1160 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 default:
1162 break;
1163 }
1164 getCurrentTopWebView().requestFocus();
1165 }
1166
1167 /**
1168 * Open the Go page.
1169 * @param startWithHistory If true, open starting on the history tab.
1170 * Otherwise, start with the bookmarks tab.
1171 */
1172 @Override
1173 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1174 if (mTabControl.getCurrentWebView() == null) {
1175 return;
1176 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001177 // clear action mode
1178 if (isInCustomActionMode()) {
1179 endActionMode();
1180 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001181 Bundle extras = new Bundle();
1182 // Disable opening in a new window if we have maxed out the windows
1183 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1184 !mTabControl.canCreateNewTab());
John Reck2bc80422011-06-30 15:11:49 -07001185 mUi.showComboView(startWithHistory
1186 ? ComboViews.History : ComboViews.Bookmarks, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001187 }
1188
1189 // combo view callbacks
1190
1191 /**
1192 * callback from ComboPage when clear history is requested
1193 */
1194 public void onRemoveParentChildRelationships() {
1195 mTabControl.removeParentChildRelationShips();
1196 }
1197
1198 /**
1199 * callback from ComboPage when bookmark/history selection
1200 */
1201 @Override
1202 public void onUrlSelected(String url, boolean newTab) {
1203 removeComboView();
1204 if (!TextUtils.isEmpty(url)) {
1205 if (newTab) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07001206 final Tab parent = mTabControl.getCurrentTab();
1207 openTab(url,
1208 (parent != null) && parent.isPrivateBrowsingEnabled(),
1209 !mSettings.openInBackground(),
1210 true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001211 } else {
1212 final Tab currentTab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001213 loadUrl(currentTab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001214 }
1215 }
1216 }
1217
1218 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001219 * dismiss the ComboPage
1220 */
1221 @Override
1222 public void removeComboView() {
1223 mUi.hideComboView();
1224 }
1225
Michael Kolb8233fac2010-10-26 16:08:53 -07001226 // key handling
1227 protected void onBackKey() {
1228 if (!mUi.onBackKey()) {
1229 WebView subwindow = mTabControl.getCurrentSubWindow();
1230 if (subwindow != null) {
1231 if (subwindow.canGoBack()) {
1232 subwindow.goBack();
1233 } else {
1234 dismissSubWindow(mTabControl.getCurrentTab());
1235 }
1236 } else {
1237 goBackOnePageOrQuit();
1238 }
1239 }
1240 }
1241
Michael Kolb4bd767d2011-05-27 11:33:55 -07001242 protected boolean onMenuKey() {
1243 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001244 }
1245
Michael Kolb8233fac2010-10-26 16:08:53 -07001246 // menu handling and state
1247 // TODO: maybe put into separate handler
1248
1249 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckb3417f02011-01-14 11:01:05 -08001250 if (mOptionsMenuHandler != null) {
1251 return mOptionsMenuHandler.onCreateOptionsMenu(menu);
1252 }
1253
John Reckd73c5a22010-12-22 10:22:50 -08001254 if (mMenuState == EMPTY_MENU) {
1255 return false;
1256 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001257 MenuInflater inflater = mActivity.getMenuInflater();
1258 inflater.inflate(R.menu.browser, menu);
1259 updateInLoadMenuItems(menu);
1260 // hold on to the menu reference here; it is used by the page callbacks
1261 // to update the menu based on loading state
1262 mCachedMenu = menu;
1263 return true;
1264 }
1265
1266 protected void onCreateContextMenu(ContextMenu menu, View v,
1267 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001268 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001269 return;
1270 }
1271 if (!(v instanceof WebView)) {
1272 return;
1273 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001274 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001275 WebView.HitTestResult result = webview.getHitTestResult();
1276 if (result == null) {
1277 return;
1278 }
1279
1280 int type = result.getType();
1281 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1282 Log.w(LOGTAG,
1283 "We should not show context menu when nothing is touched");
1284 return;
1285 }
1286 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1287 // let TextView handles context menu
1288 return;
1289 }
1290
1291 // Note, http://b/issue?id=1106666 is requesting that
1292 // an inflated menu can be used again. This is not available
1293 // yet, so inflate each time (yuk!)
1294 MenuInflater inflater = mActivity.getMenuInflater();
1295 inflater.inflate(R.menu.browsercontext, menu);
1296
1297 // Show the correct menu group
1298 final String extra = result.getExtra();
1299 menu.setGroupVisible(R.id.PHONE_MENU,
1300 type == WebView.HitTestResult.PHONE_TYPE);
1301 menu.setGroupVisible(R.id.EMAIL_MENU,
1302 type == WebView.HitTestResult.EMAIL_TYPE);
1303 menu.setGroupVisible(R.id.GEO_MENU,
1304 type == WebView.HitTestResult.GEO_TYPE);
1305 menu.setGroupVisible(R.id.IMAGE_MENU,
1306 type == WebView.HitTestResult.IMAGE_TYPE
1307 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1308 menu.setGroupVisible(R.id.ANCHOR_MENU,
1309 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1310 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001311 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1312 || type == WebView.HitTestResult.PHONE_TYPE
1313 || type == WebView.HitTestResult.EMAIL_TYPE
1314 || type == WebView.HitTestResult.GEO_TYPE;
1315 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1316 if (hitText) {
1317 menu.findItem(R.id.select_text_menu_id)
1318 .setOnMenuItemClickListener(new SelectText(webview));
1319 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001320 // Setup custom handling depending on the type
1321 switch (type) {
1322 case WebView.HitTestResult.PHONE_TYPE:
1323 menu.setHeaderTitle(Uri.decode(extra));
1324 menu.findItem(R.id.dial_context_menu_id).setIntent(
1325 new Intent(Intent.ACTION_VIEW, Uri
1326 .parse(WebView.SCHEME_TEL + extra)));
1327 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1328 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1329 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1330 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1331 addIntent);
1332 menu.findItem(R.id.copy_phone_context_menu_id)
1333 .setOnMenuItemClickListener(
1334 new Copy(extra));
1335 break;
1336
1337 case WebView.HitTestResult.EMAIL_TYPE:
1338 menu.setHeaderTitle(extra);
1339 menu.findItem(R.id.email_context_menu_id).setIntent(
1340 new Intent(Intent.ACTION_VIEW, Uri
1341 .parse(WebView.SCHEME_MAILTO + extra)));
1342 menu.findItem(R.id.copy_mail_context_menu_id)
1343 .setOnMenuItemClickListener(
1344 new Copy(extra));
1345 break;
1346
1347 case WebView.HitTestResult.GEO_TYPE:
1348 menu.setHeaderTitle(extra);
1349 menu.findItem(R.id.map_context_menu_id).setIntent(
1350 new Intent(Intent.ACTION_VIEW, Uri
1351 .parse(WebView.SCHEME_GEO
1352 + URLEncoder.encode(extra))));
1353 menu.findItem(R.id.copy_geo_context_menu_id)
1354 .setOnMenuItemClickListener(
1355 new Copy(extra));
1356 break;
1357
1358 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1359 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001360 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001361 // decide whether to show the open link in new tab option
1362 boolean showNewTab = mTabControl.canCreateNewTab();
1363 MenuItem newTabItem
1364 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001365 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001366 ? R.string.contextmenu_openlink_newwindow_background
1367 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001368 newTabItem.setVisible(showNewTab);
1369 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001370 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1371 newTabItem.setOnMenuItemClickListener(
1372 new MenuItem.OnMenuItemClickListener() {
1373 @Override
1374 public boolean onMenuItemClick(MenuItem item) {
1375 final HashMap<String, WebView> hrefMap =
1376 new HashMap<String, WebView>();
1377 hrefMap.put("webview", webview);
1378 final Message msg = mHandler.obtainMessage(
1379 FOCUS_NODE_HREF,
1380 R.id.open_newtab_context_menu_id,
1381 0, hrefMap);
1382 webview.requestFocusNodeHref(msg);
1383 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001384 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001385 });
1386 } else {
1387 newTabItem.setOnMenuItemClickListener(
1388 new MenuItem.OnMenuItemClickListener() {
1389 @Override
1390 public boolean onMenuItemClick(MenuItem item) {
1391 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001392 openTab(extra, parent,
1393 !mSettings.openInBackground(),
1394 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001395 return true;
1396 }
1397 });
1398 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001399 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001400 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1401 break;
1402 }
1403 // otherwise fall through to handle image part
1404 case WebView.HitTestResult.IMAGE_TYPE:
1405 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1406 menu.setHeaderTitle(extra);
1407 }
1408 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1409 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1410 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001411 setOnMenuItemClickListener(
1412 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001413 menu.findItem(R.id.set_wallpaper_context_menu_id).
1414 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1415 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001416 break;
1417
1418 default:
1419 Log.w(LOGTAG, "We should not get here.");
1420 break;
1421 }
1422 //update the ui
1423 mUi.onContextMenuCreated(menu);
1424 }
1425
1426 /**
1427 * As the menu can be open when loading state changes
1428 * we must manually update the state of the stop/reload menu
1429 * item
1430 */
1431 private void updateInLoadMenuItems(Menu menu) {
1432 if (menu == null) {
1433 return;
1434 }
1435 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1436 MenuItem src = mInLoad ?
1437 menu.findItem(R.id.stop_menu_id):
1438 menu.findItem(R.id.reload_menu_id);
1439 if (src != null) {
1440 dest.setIcon(src.getIcon());
1441 dest.setTitle(src.getTitle());
1442 }
1443 }
1444
John Reckb3417f02011-01-14 11:01:05 -08001445 boolean onPrepareOptionsMenu(Menu menu) {
1446 if (mOptionsMenuHandler != null) {
1447 return mOptionsMenuHandler.onPrepareOptionsMenu(menu);
1448 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001449 // Note: setVisible will decide whether an item is visible; while
1450 // setEnabled() will decide whether an item is enabled, which also means
1451 // whether the matching shortcut key will function.
1452 switch (mMenuState) {
1453 case EMPTY_MENU:
1454 if (mCurrentMenuState != mMenuState) {
1455 menu.setGroupVisible(R.id.MAIN_MENU, false);
1456 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1457 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1458 }
1459 break;
1460 default:
1461 if (mCurrentMenuState != mMenuState) {
1462 menu.setGroupVisible(R.id.MAIN_MENU, true);
1463 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1464 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1465 }
1466 final WebView w = getCurrentTopWebView();
1467 boolean canGoBack = false;
1468 boolean canGoForward = false;
1469 boolean isHome = false;
1470 if (w != null) {
1471 canGoBack = w.canGoBack();
1472 canGoForward = w.canGoForward();
1473 isHome = mSettings.getHomePage().equals(w.getUrl());
1474 }
1475 final MenuItem back = menu.findItem(R.id.back_menu_id);
1476 back.setEnabled(canGoBack);
1477
1478 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1479 home.setEnabled(!isHome);
1480
1481 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1482 forward.setEnabled(canGoForward);
1483
1484 // decide whether to show the share link option
1485 PackageManager pm = mActivity.getPackageManager();
1486 Intent send = new Intent(Intent.ACTION_SEND);
1487 send.setType("text/plain");
1488 ResolveInfo ri = pm.resolveActivity(send,
1489 PackageManager.MATCH_DEFAULT_ONLY);
1490 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1491
John Reck35e9dd62011-04-25 09:01:54 -07001492 boolean isNavDump = mSettings.enableNavDump();
Michael Kolb8233fac2010-10-26 16:08:53 -07001493 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1494 nav.setVisible(isNavDump);
1495 nav.setEnabled(isNavDump);
1496
John Reck35e9dd62011-04-25 09:01:54 -07001497 boolean showDebugSettings = mSettings.isDebugEnabled();
Michael Kolb8233fac2010-10-26 16:08:53 -07001498 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1499 counter.setVisible(showDebugSettings);
1500 counter.setEnabled(showDebugSettings);
1501
John Reckb3417f02011-01-14 11:01:05 -08001502 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
1503 newtab.setEnabled(getTabControl().canCreateNewTab());
Michael Kolb8233fac2010-10-26 16:08:53 -07001504
John Reck2bc80422011-06-30 15:11:49 -07001505 MenuItem saveSnapshot = menu.findItem(R.id.save_snapshot_menu_id);
1506 Tab tab = getCurrentTab();
1507 saveSnapshot.setVisible(tab != null && !tab.isSnapshot());
Michael Kolb8233fac2010-10-26 16:08:53 -07001508 break;
1509 }
1510 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001511 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001512 }
1513
1514 public boolean onOptionsItemSelected(MenuItem item) {
John Reckb3417f02011-01-14 11:01:05 -08001515 if (mOptionsMenuHandler != null &&
1516 mOptionsMenuHandler.onOptionsItemSelected(item)) {
1517 return true;
1518 }
1519
Michael Kolb8233fac2010-10-26 16:08:53 -07001520 if (item.getGroupId() != R.id.CONTEXT_MENU) {
1521 // menu remains active, so ensure comboview is dismissed
1522 // if main menu option is selected
1523 removeComboView();
1524 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001525 if (null == getCurrentTopWebView()) {
1526 return false;
1527 }
1528 if (mMenuIsDown) {
1529 // The shortcut action consumes the MENU. Even if it is still down,
1530 // it won't trigger the next shortcut action. In the case of the
1531 // shortcut action triggering a new activity, like Bookmarks, we
1532 // won't get onKeyUp for MENU. So it is important to reset it here.
1533 mMenuIsDown = false;
1534 }
1535 switch (item.getItemId()) {
1536 // -- Main menu
1537 case R.id.new_tab_menu_id:
1538 openTabToHomePage();
1539 break;
1540
1541 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001542 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001543 break;
1544
1545 case R.id.goto_menu_id:
1546 editUrl();
1547 break;
1548
1549 case R.id.bookmarks_menu_id:
1550 bookmarksOrHistoryPicker(false);
1551 break;
1552
Michael Kolb8233fac2010-10-26 16:08:53 -07001553 case R.id.add_bookmark_menu_id:
John Reck3ffc5ca2011-06-10 15:56:06 -07001554 bookmarkCurrentPage(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001555 break;
1556
1557 case R.id.stop_reload_menu_id:
1558 if (mInLoad) {
1559 stopLoading();
1560 } else {
1561 getCurrentTopWebView().reload();
1562 }
1563 break;
1564
1565 case R.id.back_menu_id:
1566 getCurrentTopWebView().goBack();
1567 break;
1568
1569 case R.id.forward_menu_id:
1570 getCurrentTopWebView().goForward();
1571 break;
1572
1573 case R.id.close_menu_id:
1574 // Close the subwindow if it exists.
1575 if (mTabControl.getCurrentSubWindow() != null) {
1576 dismissSubWindow(mTabControl.getCurrentTab());
1577 break;
1578 }
1579 closeCurrentTab();
1580 break;
1581
1582 case R.id.homepage_menu_id:
1583 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001584 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001585 break;
1586
1587 case R.id.preferences_menu_id:
1588 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1589 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1590 getCurrentTopWebView().getUrl());
1591 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1592 break;
1593
1594 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001595 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001596 break;
1597
John Reck2bc80422011-06-30 15:11:49 -07001598 case R.id.save_snapshot_menu_id:
1599 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001600 if (source == null) break;
John Reckd8c74522011-06-14 08:45:00 -07001601 final ContentResolver cr = mActivity.getContentResolver();
1602 final ContentValues values = source.createSnapshotValues();
John Reck2bc80422011-06-30 15:11:49 -07001603 if (values != null) {
1604 new AsyncTask<Tab, Void, Long>() {
John Reckd8c74522011-06-14 08:45:00 -07001605
John Reck2bc80422011-06-30 15:11:49 -07001606 @Override
1607 protected Long doInBackground(Tab... params) {
1608 Uri result = cr.insert(Snapshots.CONTENT_URI, values);
1609 long id = ContentUris.parseId(result);
1610 return id;
John Reckd8c74522011-06-14 08:45:00 -07001611 }
John Reckf33b1632011-06-04 20:00:23 -07001612
John Reck2bc80422011-06-30 15:11:49 -07001613 @Override
1614 protected void onPostExecute(Long id) {
1615 Bundle b = new Bundle();
1616 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1617 mUi.showComboView(ComboViews.Snapshots, b);
1618 };
1619 }.execute(source);
1620 } else {
1621 Toast.makeText(mActivity, R.string.snapshot_failed,
Leon Scrogginsac993842011-02-02 12:54:07 -05001622 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001623 }
Leon Scrogginsac993842011-02-02 12:54:07 -05001624 break;
1625
Michael Kolb8233fac2010-10-26 16:08:53 -07001626 case R.id.page_info_menu_id:
Huahui Wuae0c0412011-06-28 10:17:05 -07001627 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
Michael Kolb8233fac2010-10-26 16:08:53 -07001628 break;
1629
1630 case R.id.classic_history_menu_id:
1631 bookmarksOrHistoryPicker(true);
1632 break;
1633
1634 case R.id.title_bar_share_page_url:
1635 case R.id.share_page_menu_id:
1636 Tab currentTab = mTabControl.getCurrentTab();
1637 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001638 return false;
1639 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001640 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001641 break;
1642
1643 case R.id.dump_nav_menu_id:
1644 getCurrentTopWebView().debugDump();
1645 break;
1646
1647 case R.id.dump_counters_menu_id:
1648 getCurrentTopWebView().dumpV8Counters();
1649 break;
1650
1651 case R.id.zoom_in_menu_id:
1652 getCurrentTopWebView().zoomIn();
1653 break;
1654
1655 case R.id.zoom_out_menu_id:
1656 getCurrentTopWebView().zoomOut();
1657 break;
1658
1659 case R.id.view_downloads_menu_id:
1660 viewDownloads();
1661 break;
1662
1663 case R.id.window_one_menu_id:
1664 case R.id.window_two_menu_id:
1665 case R.id.window_three_menu_id:
1666 case R.id.window_four_menu_id:
1667 case R.id.window_five_menu_id:
1668 case R.id.window_six_menu_id:
1669 case R.id.window_seven_menu_id:
1670 case R.id.window_eight_menu_id:
1671 {
1672 int menuid = item.getItemId();
1673 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1674 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1675 Tab desiredTab = mTabControl.getTab(id);
1676 if (desiredTab != null &&
1677 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001678 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001679 }
1680 break;
1681 }
1682 }
1683 }
1684 break;
1685
1686 default:
1687 return false;
1688 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001689 return true;
1690 }
1691
1692 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001693 // Let the History and Bookmark fragments handle menus they created.
1694 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1695 return false;
1696 }
1697
Michael Kolb8233fac2010-10-26 16:08:53 -07001698 int id = item.getItemId();
1699 boolean result = true;
1700 switch (id) {
1701 // For the context menu from the title bar
1702 case R.id.title_bar_copy_page_url:
1703 Tab currentTab = mTabControl.getCurrentTab();
1704 if (null == currentTab) {
1705 result = false;
1706 break;
1707 }
1708 WebView mainView = currentTab.getWebView();
1709 if (null == mainView) {
1710 result = false;
1711 break;
1712 }
1713 copy(mainView.getUrl());
1714 break;
1715 // -- Browser context menu
1716 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001717 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001718 case R.id.copy_link_context_menu_id:
1719 final WebView webView = getCurrentTopWebView();
1720 if (null == webView) {
1721 result = false;
1722 break;
1723 }
1724 final HashMap<String, WebView> hrefMap =
1725 new HashMap<String, WebView>();
1726 hrefMap.put("webview", webView);
1727 final Message msg = mHandler.obtainMessage(
1728 FOCUS_NODE_HREF, id, 0, hrefMap);
1729 webView.requestFocusNodeHref(msg);
1730 break;
1731
1732 default:
1733 // For other context menus
1734 result = onOptionsItemSelected(item);
1735 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001736 return result;
1737 }
1738
1739 /**
1740 * support programmatically opening the context menu
1741 */
1742 public void openContextMenu(View view) {
1743 mActivity.openContextMenu(view);
1744 }
1745
1746 /**
1747 * programmatically open the options menu
1748 */
1749 public void openOptionsMenu() {
1750 mActivity.openOptionsMenu();
1751 }
1752
1753 public boolean onMenuOpened(int featureId, Menu menu) {
1754 if (mOptionsMenuOpen) {
1755 if (mConfigChanged) {
1756 // We do not need to make any changes to the state of the
1757 // title bar, since the only thing that happened was a
1758 // change in orientation
1759 mConfigChanged = false;
1760 } else {
1761 if (!mExtendedMenuOpen) {
1762 mExtendedMenuOpen = true;
1763 mUi.onExtendedMenuOpened();
1764 } else {
1765 // Switching the menu back to icon view, so show the
1766 // title bar once again.
1767 mExtendedMenuOpen = false;
1768 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001769 }
1770 }
1771 } else {
1772 // The options menu is closed, so open it, and show the title
1773 mOptionsMenuOpen = true;
1774 mConfigChanged = false;
1775 mExtendedMenuOpen = false;
1776 mUi.onOptionsMenuOpened();
1777 }
1778 return true;
1779 }
1780
1781 public void onOptionsMenuClosed(Menu menu) {
1782 mOptionsMenuOpen = false;
1783 mUi.onOptionsMenuClosed(mInLoad);
1784 }
1785
1786 public void onContextMenuClosed(Menu menu) {
1787 mUi.onContextMenuClosed(menu, mInLoad);
1788 }
1789
1790 // Helper method for getting the top window.
1791 @Override
1792 public WebView getCurrentTopWebView() {
1793 return mTabControl.getCurrentTopWebView();
1794 }
1795
1796 @Override
1797 public WebView getCurrentWebView() {
1798 return mTabControl.getCurrentWebView();
1799 }
1800
1801 /*
1802 * This method is called as a result of the user selecting the options
1803 * menu to see the download window. It shows the download window on top of
1804 * the current window.
1805 */
1806 void viewDownloads() {
1807 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1808 mActivity.startActivity(intent);
1809 }
1810
1811 // action mode
1812
1813 void onActionModeStarted(ActionMode mode) {
1814 mUi.onActionModeStarted(mode);
1815 mActionMode = mode;
1816 }
1817
1818 /*
1819 * True if a custom ActionMode (i.e. find or select) is in use.
1820 */
1821 @Override
1822 public boolean isInCustomActionMode() {
1823 return mActionMode != null;
1824 }
1825
1826 /*
1827 * End the current ActionMode.
1828 */
1829 @Override
1830 public void endActionMode() {
1831 if (mActionMode != null) {
1832 mActionMode.finish();
1833 }
1834 }
1835
1836 /*
1837 * Called by find and select when they are finished. Replace title bars
1838 * as necessary.
1839 */
1840 public void onActionModeFinished(ActionMode mode) {
1841 if (!isInCustomActionMode()) return;
1842 mUi.onActionModeFinished(mInLoad);
1843 mActionMode = null;
1844 }
1845
1846 boolean isInLoad() {
1847 return mInLoad;
1848 }
1849
1850 // bookmark handling
1851
1852 /**
1853 * add the current page as a bookmark to the given folder id
1854 * @param folderId use -1 for the default folder
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001855 * @param canBeAnEdit If true, check to see whether the site is already
1856 * bookmarked, and if it is, edit that bookmark. If false, and
1857 * the site is already bookmarked, do not attempt to edit the
1858 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001859 */
1860 @Override
John Reck3ffc5ca2011-06-10 15:56:06 -07001861 public void bookmarkCurrentPage(boolean canBeAnEdit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001862 Intent i = new Intent(mActivity,
1863 AddBookmarkPage.class);
1864 WebView w = getCurrentTopWebView();
1865 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1866 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1867 String touchIconUrl = w.getTouchIconUrl();
1868 if (touchIconUrl != null) {
1869 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1870 WebSettings settings = w.getSettings();
1871 if (settings != null) {
1872 i.putExtra(AddBookmarkPage.USER_AGENT,
1873 settings.getUserAgentString());
1874 }
1875 }
1876 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1877 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1878 getDesiredThumbnailHeight(mActivity)));
1879 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001880 if (canBeAnEdit) {
1881 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1882 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001883 // Put the dialog at the upper right of the screen, covering the
1884 // star on the title bar.
1885 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
1886 mActivity.startActivity(i);
1887 }
1888
1889 // file chooser
1890 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1891 mUploadHandler = new UploadHandler(this);
1892 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1893 }
1894
1895 // thumbnails
1896
1897 /**
1898 * Return the desired width for thumbnail screenshots, which are stored in
1899 * the database, and used on the bookmarks screen.
1900 * @param context Context for finding out the density of the screen.
1901 * @return desired width for thumbnail screenshot.
1902 */
1903 static int getDesiredThumbnailWidth(Context context) {
1904 return context.getResources().getDimensionPixelOffset(
1905 R.dimen.bookmarkThumbnailWidth);
1906 }
1907
1908 /**
1909 * Return the desired height for thumbnail screenshots, which are stored in
1910 * the database, and used on the bookmarks screen.
1911 * @param context Context for finding out the density of the screen.
1912 * @return desired height for thumbnail screenshot.
1913 */
1914 static int getDesiredThumbnailHeight(Context context) {
1915 return context.getResources().getDimensionPixelOffset(
1916 R.dimen.bookmarkThumbnailHeight);
1917 }
1918
Michael Kolb1acef692011-03-08 14:12:06 -08001919 static Bitmap createScreenshot(Tab tab, int width, int height) {
1920 if ((tab != null) && (tab.getWebView() != null)) {
1921 return createScreenshot(tab.getWebView(), width, height);
1922 }
1923 return null;
1924 }
1925
John Reck8cc92352011-07-06 17:41:52 -07001926 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08001927 // We render to a bitmap 2x the desired size so that we can then
1928 // re-scale it with filtering since canvas.scale doesn't filter
1929 // This helps reduce aliasing at the cost of being slightly blurry
1930 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07001931 Picture thumbnail = view.capturePicture();
1932 if (thumbnail == null) {
1933 return null;
1934 }
John Reck5c6ac2f2011-01-05 10:18:03 -08001935 width *= filter_scale;
1936 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07001937 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1938 Canvas canvas = new Canvas(bm);
1939 // May need to tweak these values to determine what is the
1940 // best scale factor
1941 int thumbnailWidth = thumbnail.getWidth();
1942 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08001943 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08001944 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08001945 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07001946 } else {
1947 return null;
1948 }
John Reckfe49ab42010-11-16 17:09:37 -08001949
Michael Kolbeb95db42011-03-03 10:38:40 -08001950 float scaleFactorY = (float) height / (float)thumbnailHeight;
1951 if (scaleFactorY > scaleFactor) {
1952 // The picture is narrower than the requested AR
1953 // Center the thumnail and crop the sides
1954 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08001955 float wx = (thumbnailWidth * scaleFactor) - width;
1956 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07001957 }
1958
John Reckfe49ab42010-11-16 17:09:37 -08001959 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07001960
1961 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08001962 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
1963 height / filter_scale, true);
1964 bm.recycle();
1965 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07001966 }
1967
John Reck34ef2672011-02-10 11:30:55 -08001968 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001969 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07001970 // FIXME: Would like to make sure there is actually something to
1971 // draw, but the API for that (WebViewCore.pictureReady()) is not
1972 // currently accessible here.
1973
John Reck34ef2672011-02-10 11:30:55 -08001974 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08001975 if (view == null) {
1976 // Tab was destroyed
1977 return;
1978 }
John Reck34ef2672011-02-10 11:30:55 -08001979 final String url = tab.getUrl();
1980 final String originalUrl = view.getOriginalUrl();
1981
1982 if (TextUtils.isEmpty(url)) {
1983 return;
1984 }
1985
1986 // Only update thumbnails for web urls (http(s)://), not for
1987 // about:, javascript:, data:, etc...
1988 // Unless it is a bookmarked site, then always update
1989 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
1990 return;
1991 }
1992
Michael Kolb8233fac2010-10-26 16:08:53 -07001993 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
1994 getDesiredThumbnailHeight(mActivity));
1995 if (bm == null) {
1996 return;
1997 }
1998
1999 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002000 new AsyncTask<Void, Void, Void>() {
2001 @Override
2002 protected Void doInBackground(Void... unused) {
2003 Cursor cursor = null;
2004 try {
2005 // TODO: Clean this up
2006 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2007 if (cursor != null && cursor.moveToFirst()) {
2008 final ByteArrayOutputStream os =
2009 new ByteArrayOutputStream();
2010 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002011
John Reck34ef2672011-02-10 11:30:55 -08002012 ContentValues values = new ContentValues();
2013 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002014
John Reck34ef2672011-02-10 11:30:55 -08002015 do {
John Reck617fd832011-02-16 14:35:59 -08002016 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002017 cr.update(Images.CONTENT_URI, values, null, null);
2018 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002019 }
John Reck34ef2672011-02-10 11:30:55 -08002020 } catch (IllegalStateException e) {
2021 // Ignore
2022 } finally {
2023 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002024 }
John Reck34ef2672011-02-10 11:30:55 -08002025 return null;
2026 }
2027 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002028 }
2029
2030 private class Copy implements OnMenuItemClickListener {
2031 private CharSequence mText;
2032
2033 public boolean onMenuItemClick(MenuItem item) {
2034 copy(mText);
2035 return true;
2036 }
2037
2038 public Copy(CharSequence toCopy) {
2039 mText = toCopy;
2040 }
2041 }
2042
Leon Scroggins63c02662010-11-18 15:16:27 -05002043 private static class Download implements OnMenuItemClickListener {
2044 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002045 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002046 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002047
2048 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002049 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002050 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002051 return true;
2052 }
2053
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002054 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002055 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002056 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002057 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002058 }
2059 }
2060
Cary Clark8974d282010-11-22 10:46:05 -05002061 private static class SelectText implements OnMenuItemClickListener {
2062 private WebView mWebView;
2063
2064 public boolean onMenuItemClick(MenuItem item) {
2065 if (mWebView != null) {
2066 return mWebView.selectText();
2067 }
2068 return false;
2069 }
2070
2071 public SelectText(WebView webView) {
2072 mWebView = webView;
2073 }
2074
2075 }
2076
Michael Kolb8233fac2010-10-26 16:08:53 -07002077 /********************** TODO: UI stuff *****************************/
2078
2079 // these methods have been copied, they still need to be cleaned up
2080
2081 /****************** tabs ***************************************************/
2082
2083 // basic tab interactions:
2084
2085 // it is assumed that tabcontrol already knows about the tab
2086 protected void addTab(Tab tab) {
2087 mUi.addTab(tab);
2088 }
2089
2090 protected void removeTab(Tab tab) {
2091 mUi.removeTab(tab);
2092 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002093 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002094 }
2095
Michael Kolbf2055602011-04-09 17:20:03 -07002096 @Override
2097 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002098 // monkey protection against delayed start
2099 if (tab != null) {
2100 mTabControl.setCurrentTab(tab);
2101 // the tab is guaranteed to have a webview after setCurrentTab
2102 mUi.setActiveTab(tab);
2103 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002104 }
2105
2106 protected void closeEmptyChildTab() {
2107 Tab current = mTabControl.getCurrentTab();
2108 if (current != null
2109 && current.getWebView().copyBackForwardList().getSize() == 0) {
Michael Kolbc831b632011-05-11 09:30:34 -07002110 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002111 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002112 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002113 closeTab(current);
2114 }
2115 }
2116 }
2117
John Reck26b18322011-06-21 13:08:58 -07002118 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002119 // Dismiss the subwindow if applicable.
2120 dismissSubWindow(appTab);
2121 // Since we might kill the WebView, remove it from the
2122 // content view first.
2123 mUi.detachTab(appTab);
2124 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002125 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002126 // TODO: analyze why the remove and add are necessary
2127 mUi.attachTab(appTab);
2128 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002129 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002130 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002131 } else {
2132 // If the tab was the current tab, we have to attach
2133 // it to the view system again.
2134 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002135 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002136 }
2137 }
2138
2139 // Remove the sub window if it exists. Also called by TabControl when the
2140 // user clicks the 'X' to dismiss a sub window.
2141 public void dismissSubWindow(Tab tab) {
2142 removeSubWindow(tab);
2143 // dismiss the subwindow. This will destroy the WebView.
2144 tab.dismissSubWindow();
2145 getCurrentTopWebView().requestFocus();
2146 }
2147
2148 @Override
2149 public void removeSubWindow(Tab t) {
2150 if (t.getSubWebView() != null) {
2151 mUi.removeSubWindow(t.getSubViewContainer());
2152 }
2153 }
2154
2155 @Override
2156 public void attachSubWindow(Tab tab) {
2157 if (tab.getSubWebView() != null) {
2158 mUi.attachSubWindow(tab.getSubViewContainer());
2159 getCurrentTopWebView().requestFocus();
2160 }
2161 }
2162
Mathew Inwood29721c22011-06-29 17:55:24 +01002163 private Tab showPreloadedTab(final UrlData urlData) {
2164 if (!urlData.isPreloaded()) {
2165 return null;
2166 }
2167 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2168 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2169 if (sbQuery != null) {
2170 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
2171 // Could not submit query. Fallback to regular tab creation
2172 tabControl.destroy();
2173 return null;
2174 }
2175 }
2176 Tab t = tabControl.getTab();
2177 mTabControl.addPreloadedTab(t);
2178 addTab(t);
2179 setActiveTab(t);
2180 return t;
2181 }
2182
Michael Kolb7bcafde2011-05-09 13:55:59 -07002183 // open a non inconito tab with the given url data
2184 // and set as active tab
2185 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002186 Tab tab = showPreloadedTab(urlData);
2187 if (tab == null) {
2188 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002189 if ((tab != null) && !urlData.isEmpty()) {
2190 loadUrlDataIn(tab, urlData);
2191 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002192 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002193 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002194 }
2195
Michael Kolb843510f2010-12-09 10:51:49 -08002196 @Override
2197 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002198 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002199 }
2200
Michael Kolb8233fac2010-10-26 16:08:53 -07002201 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002202 public Tab openIncognitoTab() {
2203 return openTab(INCOGNITO_URI, true, true, false);
2204 }
2205
2206 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002207 public Tab openTab(String url, boolean incognito, boolean setActive,
2208 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002209 return openTab(url, incognito, setActive, useCurrent, null);
2210 }
2211
2212 @Override
2213 public Tab openTab(String url, Tab parent, boolean setActive,
2214 boolean useCurrent) {
2215 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2216 setActive, useCurrent, parent);
2217 }
2218
2219 public Tab openTab(String url, boolean incognito, boolean setActive,
2220 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002221 Tab tab = createNewTab(incognito, setActive, useCurrent);
2222 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002223 if (parent != null && parent != tab) {
2224 parent.addChildTab(tab);
2225 }
Michael Kolb519d2282011-05-09 17:03:19 -07002226 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002227 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002228 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002229 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002230 return tab;
2231 }
2232
2233 // this method will attempt to create a new tab
2234 // incognito: private browsing tab
2235 // setActive: ste tab as current tab
2236 // useCurrent: if no new tab can be created, return current tab
2237 private Tab createNewTab(boolean incognito, boolean setActive,
2238 boolean useCurrent) {
2239 Tab tab = null;
2240 if (mTabControl.canCreateNewTab()) {
2241 tab = mTabControl.createNewTab(incognito);
2242 addTab(tab);
2243 if (setActive) {
2244 setActiveTab(tab);
2245 }
2246 } else {
2247 if (useCurrent) {
2248 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002249 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002250 } else {
2251 mUi.showMaxTabsWarning();
2252 }
2253 }
2254 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002255 }
2256
John Reck2bc80422011-06-30 15:11:49 -07002257 @Override
2258 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2259 SnapshotTab tab = null;
2260 if (mTabControl.canCreateNewTab()) {
2261 tab = mTabControl.createSnapshotTab(snapshotId);
2262 addTab(tab);
2263 if (setActive) {
2264 setActiveTab(tab);
2265 }
2266 } else {
2267 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002268 }
2269 return tab;
2270 }
2271
Michael Kolb8233fac2010-10-26 16:08:53 -07002272 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002273 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002274 * @return boolean True if we successfully switched to a different tab. If
2275 * the indexth tab is null, or if that tab is the same as
2276 * the current one, return false.
2277 */
2278 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002279 public boolean switchToTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002280 // hide combo view if open
2281 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002282 Tab currentTab = mTabControl.getCurrentTab();
2283 if (tab == null || tab == currentTab) {
2284 return false;
2285 }
2286 setActiveTab(tab);
2287 return true;
2288 }
2289
2290 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002291 public void closeCurrentTab() {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002292 // hide combo view if open
2293 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002294 if (mTabControl.getTabCount() == 1) {
John Reck958b2422010-12-03 17:56:17 -08002295 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002296 return;
2297 }
Michael Kolbc831b632011-05-11 09:30:34 -07002298 final Tab current = mTabControl.getCurrentTab();
2299 final int pos = mTabControl.getCurrentPosition();
2300 Tab newTab = current.getParent();
2301 if (newTab == null) {
2302 newTab = mTabControl.getTab(pos + 1);
2303 if (newTab == null) {
2304 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002305 }
2306 }
Michael Kolbc831b632011-05-11 09:30:34 -07002307 if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002308 // Close window
2309 closeTab(current);
2310 }
2311 }
2312
2313 /**
2314 * Close the tab, remove its associated title bar, and adjust mTabControl's
2315 * current tab to a valid value.
2316 */
2317 @Override
2318 public void closeTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002319 // hide combo view if open
2320 removeComboView();
Michael Kolb2d59c322011-01-25 13:18:55 -08002321 removeTab(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002322 }
2323
Michael Kolb8233fac2010-10-26 16:08:53 -07002324 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002325 protected void loadUrlFromContext(String url) {
2326 Tab tab = getCurrentTab();
2327 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002328 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002329 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002330 url = UrlUtils.smartUrlFilter(url);
2331 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002332 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002333 }
2334 }
2335 }
2336
2337 /**
2338 * Load the URL into the given WebView and update the title bar
2339 * to reflect the new load. Call this instead of WebView.loadUrl
2340 * directly.
2341 * @param view The WebView used to load url.
2342 * @param url The URL to load.
2343 */
John Reck71e51422011-07-01 16:49:28 -07002344 @Override
2345 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002346 loadUrl(tab, url, null);
2347 }
2348
2349 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2350 if (tab != null) {
2351 dismissSubWindow(tab);
2352 tab.loadUrl(url, headers);
2353 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002354 }
2355
2356 /**
2357 * Load UrlData into a Tab and update the title bar to reflect the new
2358 * load. Call this instead of UrlData.loadIn directly.
2359 * @param t The Tab used to load.
2360 * @param data The UrlData being loaded.
2361 */
2362 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002363 if (data != null) {
2364 if (data.mVoiceIntent != null) {
2365 t.activateVoiceSearchMode(data.mVoiceIntent);
Michael Kolb14612442011-06-24 13:06:29 -07002366 } else if (data.isPreloaded()) {
2367 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002368 } else {
2369 loadUrl(t, data.mUrl, data.mHeaders);
2370 }
2371 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002372 }
2373
John Reck30c714c2010-12-16 17:30:34 -08002374 @Override
2375 public void onUserCanceledSsl(Tab tab) {
2376 WebView web = tab.getWebView();
2377 // TODO: Figure out the "right" behavior
2378 if (web.canGoBack()) {
2379 web.goBack();
2380 } else {
2381 web.loadUrl(mSettings.getHomePage());
2382 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002383 }
2384
2385 void goBackOnePageOrQuit() {
2386 Tab current = mTabControl.getCurrentTab();
2387 if (current == null) {
2388 /*
2389 * Instead of finishing the activity, simply push this to the back
2390 * of the stack and let ActivityManager to choose the foreground
2391 * activity. As BrowserActivity is singleTask, it will be always the
2392 * root of the task. So we can use either true or false for
2393 * moveTaskToBack().
2394 */
2395 mActivity.moveTaskToBack(true);
2396 return;
2397 }
2398 WebView w = current.getWebView();
2399 if (w.canGoBack()) {
2400 w.goBack();
2401 } else {
2402 // Check to see if we are closing a window that was created by
2403 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002404 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002405 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002406 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002407 // Now we close the other tab
2408 closeTab(current);
2409 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002410 /*
2411 * Instead of finishing the activity, simply push this to the back
2412 * of the stack and let ActivityManager to choose the foreground
2413 * activity. As BrowserActivity is singleTask, it will be always the
2414 * root of the task. So we can use either true or false for
2415 * moveTaskToBack().
2416 */
2417 mActivity.moveTaskToBack(true);
2418 }
2419 }
2420 }
2421
2422 /**
2423 * Feed the previously stored results strings to the BrowserProvider so that
2424 * the SearchDialog will show them instead of the standard searches.
2425 * @param result String to show on the editable line of the SearchDialog.
2426 */
2427 @Override
2428 public void showVoiceSearchResults(String result) {
2429 ContentProviderClient client = mActivity.getContentResolver()
2430 .acquireContentProviderClient(Browser.BOOKMARKS_URI);
2431 ContentProvider prov = client.getLocalContentProvider();
2432 BrowserProvider bp = (BrowserProvider) prov;
2433 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
2434 client.release();
2435
2436 Bundle bundle = createGoogleSearchSourceBundle(
2437 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
2438 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
2439 startSearch(result, false, bundle, false);
2440 }
2441
2442 private void startSearch(String initialQuery, boolean selectInitialQuery,
2443 Bundle appSearchData, boolean globalSearch) {
2444 if (appSearchData == null) {
2445 appSearchData = createGoogleSearchSourceBundle(
2446 GOOGLE_SEARCH_SOURCE_TYPE);
2447 }
2448
2449 SearchEngine searchEngine = mSettings.getSearchEngine();
2450 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
2451 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
2452 }
2453 mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData,
2454 globalSearch);
2455 }
2456
2457 private Bundle createGoogleSearchSourceBundle(String source) {
2458 Bundle bundle = new Bundle();
2459 bundle.putString(Search.SOURCE, source);
2460 return bundle;
2461 }
2462
2463 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002464 * helper method for key handler
2465 * returns the current tab if it can't advance
2466 */
Michael Kolbc831b632011-05-11 09:30:34 -07002467 private Tab getNextTab() {
2468 return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1,
2469 mTabControl.getCurrentPosition() + 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002470 }
2471
2472 /**
2473 * helper method for key handler
2474 * returns the current tab if it can't advance
2475 */
Michael Kolbc831b632011-05-11 09:30:34 -07002476 private Tab getPrevTab() {
2477 return mTabControl.getTab(Math.max(0,
2478 mTabControl.getCurrentPosition() - 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002479 }
2480
2481 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002482 * handle key events in browser
2483 *
2484 * @param keyCode
2485 * @param event
2486 * @return true if handled, false to pass to super
2487 */
2488 boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002489 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002490 // Even if MENU is already held down, we need to call to super to open
2491 // the IME on long press.
Michael Kolb2814a362011-05-19 15:49:41 -07002492 if (KeyEvent.KEYCODE_MENU == keyCode) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002493 if (mOptionsMenuHandler != null) {
2494 return false;
2495 } else {
2496 event.startTracking();
2497 return true;
2498 }
Michael Kolb2814a362011-05-19 15:49:41 -07002499 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002500 if (!noModifiers
2501 && ((KeyEvent.KEYCODE_MENU == keyCode)
2502 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2503 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002504 mMenuIsDown = true;
2505 return false;
2506 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002507
Cary Clark8ff8c662010-12-29 15:03:05 -05002508 WebView webView = getCurrentTopWebView();
2509 if (webView == null) return false;
2510
Cary Clark160bbb92011-01-10 11:17:07 -05002511 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2512 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002513
Michael Kolb8233fac2010-10-26 16:08:53 -07002514 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002515 case KeyEvent.KEYCODE_TAB:
2516 if (event.isCtrlPressed()) {
2517 if (event.isShiftPressed()) {
2518 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002519 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002520 } else {
2521 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002522 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002523 }
2524 return true;
2525 }
2526 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002527 case KeyEvent.KEYCODE_SPACE:
2528 // WebView/WebTextView handle the keys in the KeyDown. As
2529 // the Activity's shortcut keys are only handled when WebView
2530 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002531 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002532 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002533 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002534 pageDown();
2535 }
2536 return true;
2537 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002538 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002539 event.startTracking();
2540 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002541 case KeyEvent.KEYCODE_DPAD_LEFT:
2542 if (ctrl) {
2543 webView.goBack();
2544 return true;
2545 }
2546 break;
2547 case KeyEvent.KEYCODE_DPAD_RIGHT:
2548 if (ctrl) {
2549 webView.goForward();
2550 return true;
2551 }
2552 break;
2553 case KeyEvent.KEYCODE_A:
2554 if (ctrl) {
2555 webView.selectAll();
2556 return true;
2557 }
2558 break;
Michael Kolba4183062011-01-16 10:43:21 -08002559// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002560 case KeyEvent.KEYCODE_C:
2561 if (ctrl) {
2562 webView.copySelection();
2563 return true;
2564 }
2565 break;
Michael Kolba4183062011-01-16 10:43:21 -08002566// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002567// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002568// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002569// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002570// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002571// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002572// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002573// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002574// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002575// case KeyEvent.KEYCODE_M: // unused
2576// case KeyEvent.KEYCODE_N: // in Chrome: new window
2577// case KeyEvent.KEYCODE_O: // in Chrome: open file
2578// case KeyEvent.KEYCODE_P: // in Chrome: print page
2579// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002580// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002581// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2582 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002583 // we can't use the ctrl/shift flags, they check for
2584 // exclusive use of a modifier
2585 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002586 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002587 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002588 } else {
2589 openTabToHomePage();
2590 }
2591 return true;
2592 }
2593 break;
2594// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2595// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb1a2eba42011-03-16 16:42:49 -07002596 case KeyEvent.KEYCODE_W: // in Chrome: close tab
2597 if (ctrl) {
2598 closeCurrentTab();
2599 return true;
2600 }
2601 break;
Cary Clark8ff8c662010-12-29 15:03:05 -05002602// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2603// case KeyEvent.KEYCODE_Y: // unused
2604// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002605 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002606 // it is a regular key and webview is not null
2607 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 }
2609
John Recke6bf4ab2011-02-24 15:48:05 -08002610 boolean onKeyLongPress(int keyCode, KeyEvent event) {
2611 switch(keyCode) {
2612 case KeyEvent.KEYCODE_BACK:
2613 if (mUi.showsWeb()) {
2614 bookmarksOrHistoryPicker(true);
2615 return true;
2616 }
2617 break;
2618 }
2619 return false;
2620 }
2621
Michael Kolb8233fac2010-10-26 16:08:53 -07002622 boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb2814a362011-05-19 15:49:41 -07002623 if (KeyEvent.KEYCODE_MENU == keyCode) {
2624 mMenuIsDown = false;
2625 if (event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002626 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002627 }
2628 }
Cary Clark160bbb92011-01-10 11:17:07 -05002629 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002630 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002631 case KeyEvent.KEYCODE_BACK:
2632 if (event.isTracking() && !event.isCanceled()) {
2633 onBackKey();
2634 return true;
2635 }
2636 break;
2637 }
2638 return false;
2639 }
2640
2641 public boolean isMenuDown() {
2642 return mMenuIsDown;
2643 }
2644
Ben Murdoch8029a772010-11-16 11:58:21 +00002645 public void setupAutoFill(Message message) {
2646 // Open the settings activity at the AutoFill profile fragment so that
2647 // the user can create a new profile. When they return, we will dispatch
2648 // the message so that we can autofill the form using their new profile.
2649 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2650 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2651 AutoFillSettingsFragment.class.getName());
2652 mAutoFillSetupMessage = message;
2653 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2654 }
John Reckb3417f02011-01-14 11:01:05 -08002655
2656 @Override
2657 public void registerOptionsMenuHandler(OptionsMenuHandler handler) {
2658 mOptionsMenuHandler = handler;
2659 }
2660
2661 @Override
2662 public void unregisterOptionsMenuHandler(OptionsMenuHandler handler) {
2663 if (mOptionsMenuHandler == handler) {
2664 mOptionsMenuHandler = null;
2665 }
2666 }
2667
Narayan Kamath5119edd2011-02-23 15:49:17 +00002668 @Override
2669 public void registerDropdownChangeListener(DropdownChangeListener d) {
2670 mUi.registerDropdownChangeListener(d);
2671 }
Michael Kolbfbc579a2011-07-07 15:59:33 -07002672
2673 public boolean onSearchRequested() {
2674 mUi.editUrl(false);
2675 return true;
2676 }
2677
Michael Kolb8233fac2010-10-26 16:08:53 -07002678}