blob: 09c7cd1226f8f26e6741da307ec0c9c83eca2fe3 [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()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700822 if (tab.inForeground() && !didUserStopLoading()
823 || !tab.inForeground()) {
824 // Only update the bookmark screenshot if the user did not
825 // cancel the load early.
826 mHandler.sendMessageDelayed(mHandler.obtainMessage(
John Reck34ef2672011-02-10 11:30:55 -0800827 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Michael Kolb8233fac2010-10-26 16:08:53 -0700828 500);
829 }
830 }
831 // pause the WebView timer and release the wake lock if it is finished
832 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800833 if (mActivityPaused && pauseWebViewTimers(tab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700834 if (mWakeLock.isHeld()) {
835 mHandler.removeMessages(RELEASE_WAKELOCK);
836 mWakeLock.release();
837 }
838 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200839
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 // Performance probe
841 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800842 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700843 }
844
845 Performance.tracePageFinished();
846 }
847
848 @Override
John Reck30c714c2010-12-16 17:30:34 -0800849 public void onProgressChanged(Tab tab) {
850 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700851
852 if (newProgress == 100) {
853 CookieSyncManager.getInstance().sync();
854 // onProgressChanged() may continue to be called after the main
855 // frame has finished loading, as any remaining sub frames continue
856 // to load. We'll only get called once though with newProgress as
857 // 100 when everything is loaded. (onPageFinished is called once
858 // when the main frame completes loading regardless of the state of
859 // any sub frames so calls to onProgressChanges may continue after
860 // onPageFinished has executed)
861 if (mInLoad) {
862 mInLoad = false;
863 updateInLoadMenuItems(mCachedMenu);
864 }
865 } else {
866 if (!mInLoad) {
867 // onPageFinished may have already been called but a subframe is
868 // still loading and updating the progress. Reset mInLoad and
869 // update the menu items.
870 mInLoad = true;
871 updateInLoadMenuItems(mCachedMenu);
872 }
873 }
John Reck30c714c2010-12-16 17:30:34 -0800874 mUi.onProgressChanged(tab);
875 }
876
877 @Override
878 public void onUpdatedLockIcon(Tab tab) {
879 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700880 }
881
882 @Override
883 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800884 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800885 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800886 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700887 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
888 return;
889 }
890 // Update the title in the history database if not in private browsing mode
891 if (!tab.isPrivateBrowsingEnabled()) {
John Reck0ebd3ac2010-12-09 11:14:04 -0800892 mDataController.updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700893 }
894 }
895
896 @Override
897 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800898 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700899 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
900 }
901
902 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800903 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
904 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700905 }
906
907 @Override
908 public boolean shouldOverrideKeyEvent(KeyEvent event) {
909 if (mMenuIsDown) {
910 // only check shortcut key when MENU is held
911 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
912 event);
913 } else {
914 return false;
915 }
916 }
917
918 @Override
919 public void onUnhandledKeyEvent(KeyEvent event) {
920 if (!isActivityPaused()) {
921 if (event.getAction() == KeyEvent.ACTION_DOWN) {
922 mActivity.onKeyDown(event.getKeyCode(), event);
923 } else {
924 mActivity.onKeyUp(event.getKeyCode(), event);
925 }
926 }
927 }
928
929 @Override
John Reck324d4402011-01-11 16:56:42 -0800930 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700931 // Don't save anything in private browsing mode
932 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800933 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700934
John Reck324d4402011-01-11 16:56:42 -0800935 if (TextUtils.isEmpty(url)
936 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700937 return;
938 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800939 mDataController.updateVisitedHistory(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700940 WebIconDatabase.getInstance().retainIconForPageUrl(url);
John Reck847b5322011-04-14 17:02:18 -0700941 if (!mActivityPaused) {
942 // Since we clear the state in onPause, don't backup the current
943 // state if we are already paused
944 mCrashRecoveryHandler.backupState();
945 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700946 }
947
948 @Override
949 public void getVisitedHistory(final ValueCallback<String[]> callback) {
950 AsyncTask<Void, Void, String[]> task =
951 new AsyncTask<Void, Void, String[]>() {
952 @Override
953 public String[] doInBackground(Void... unused) {
954 return Browser.getVisitedHistory(mActivity.getContentResolver());
955 }
956 @Override
957 public void onPostExecute(String[] result) {
958 callback.onReceiveValue(result);
959 }
960 };
961 task.execute();
962 }
963
964 @Override
965 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
966 final HttpAuthHandler handler, final String host,
967 final String realm) {
968 String username = null;
969 String password = null;
970
971 boolean reuseHttpAuthUsernamePassword
972 = handler.useHttpAuthUsernamePassword();
973
974 if (reuseHttpAuthUsernamePassword && view != null) {
975 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
976 if (credentials != null && credentials.length == 2) {
977 username = credentials[0];
978 password = credentials[1];
979 }
980 }
981
982 if (username != null && password != null) {
983 handler.proceed(username, password);
984 } else {
985 if (tab.inForeground()) {
986 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
987 } else {
988 handler.cancel();
989 }
990 }
991 }
992
993 @Override
994 public void onDownloadStart(Tab tab, String url, String userAgent,
995 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000996 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -0500997 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000998 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
999 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001000 // This Tab was opened for the sole purpose of downloading a
1001 // file. Remove it.
1002 if (tab == mTabControl.getCurrentTab()) {
1003 // In this case, the Tab is still on top.
1004 goBackOnePageOrQuit();
1005 } else {
1006 // In this case, it is not.
1007 closeTab(tab);
1008 }
1009 }
1010 }
1011
1012 @Override
1013 public Bitmap getDefaultVideoPoster() {
1014 return mUi.getDefaultVideoPoster();
1015 }
1016
1017 @Override
1018 public View getVideoLoadingProgressView() {
1019 return mUi.getVideoLoadingProgressView();
1020 }
1021
1022 @Override
1023 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1024 SslError error) {
1025 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1026 }
1027
Patrick Scott92066772011-03-10 08:46:27 -05001028 @Override
1029 public void showAutoLogin(Tab tab) {
1030 assert tab.inForeground();
1031 // Update the title bar to show the auto-login request.
1032 mUi.showAutoLogin(tab);
1033 }
1034
1035 @Override
1036 public void hideAutoLogin(Tab tab) {
1037 assert tab.inForeground();
1038 mUi.hideAutoLogin(tab);
1039 }
1040
Michael Kolb8233fac2010-10-26 16:08:53 -07001041 // helper method
1042
1043 /*
1044 * Update the favorites icon if the private browsing isn't enabled and the
1045 * icon is valid.
1046 */
1047 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1048 final String url, Bitmap favicon) {
1049 if (favicon == null) {
1050 return;
1051 }
1052 if (!tab.isPrivateBrowsingEnabled()) {
1053 Bookmarks.updateFavicon(mActivity
1054 .getContentResolver(), originalUrl, url, favicon);
1055 }
1056 }
1057
Leon Scroggins4cd97792010-12-03 15:31:56 -05001058 @Override
1059 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001060 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001061 mUi.bookmarkedStatusHasChanged(tab);
1062 }
1063
Michael Kolb8233fac2010-10-26 16:08:53 -07001064 // end WebViewController
1065
1066 protected void pageUp() {
1067 getCurrentTopWebView().pageUp(false);
1068 }
1069
1070 protected void pageDown() {
1071 getCurrentTopWebView().pageDown(false);
1072 }
1073
1074 // callback from phone title bar
1075 public void editUrl() {
1076 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001077 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001078 }
1079
Michael Kolbcfa3af52010-12-14 10:36:11 -08001080 public void startVoiceSearch() {
1081 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1082 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1083 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1084 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1085 mActivity.getComponentName().flattenToString());
1086 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001087 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001088 mActivity.startActivity(intent);
1089 }
1090
Michael Kolb11d19782011-03-20 10:17:40 -07001091 @Override
1092 public void activateVoiceSearchMode(String title, List<String> results) {
1093 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001094 }
1095
1096 public void revertVoiceSearchMode(Tab tab) {
1097 mUi.revertVoiceTitleBar(tab);
1098 }
1099
Michael Kolb736990c2011-03-20 10:01:20 -07001100 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001101 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001102 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1103 }
1104
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001105 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001106 WebChromeClient.CustomViewCallback callback) {
1107 if (tab.inForeground()) {
1108 if (mUi.isCustomViewShowing()) {
1109 callback.onCustomViewHidden();
1110 return;
1111 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001112 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001113 // Save the menu state and set it to empty while the custom
1114 // view is showing.
1115 mOldMenuState = mMenuState;
1116 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001117 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001118 }
1119 }
1120
1121 @Override
1122 public void hideCustomView() {
1123 if (mUi.isCustomViewShowing()) {
1124 mUi.onHideCustomView();
1125 // Reset the old menu state.
1126 mMenuState = mOldMenuState;
1127 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001128 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001129 }
1130 }
1131
1132 protected void onActivityResult(int requestCode, int resultCode,
1133 Intent intent) {
1134 if (getCurrentTopWebView() == null) return;
1135 switch (requestCode) {
1136 case PREFERENCES_PAGE:
1137 if (resultCode == Activity.RESULT_OK && intent != null) {
1138 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001139 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001140 mTabControl.removeParentChildRelationShips();
1141 }
1142 }
1143 break;
1144 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001145 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001146 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001147 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001148 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001149 case AUTOFILL_SETUP:
1150 // Determine whether a profile was actually set up or not
1151 // and if so, send the message back to the WebTextView to
1152 // fill the form with the new profile.
1153 if (getSettings().getAutoFillProfile() != null) {
1154 mAutoFillSetupMessage.sendToTarget();
1155 mAutoFillSetupMessage = null;
1156 }
1157 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001158 default:
1159 break;
1160 }
1161 getCurrentTopWebView().requestFocus();
1162 }
1163
1164 /**
1165 * Open the Go page.
1166 * @param startWithHistory If true, open starting on the history tab.
1167 * Otherwise, start with the bookmarks tab.
1168 */
1169 @Override
1170 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1171 if (mTabControl.getCurrentWebView() == null) {
1172 return;
1173 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001174 // clear action mode
1175 if (isInCustomActionMode()) {
1176 endActionMode();
1177 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001178 Bundle extras = new Bundle();
1179 // Disable opening in a new window if we have maxed out the windows
1180 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1181 !mTabControl.canCreateNewTab());
John Reck2bc80422011-06-30 15:11:49 -07001182 mUi.showComboView(startWithHistory
1183 ? ComboViews.History : ComboViews.Bookmarks, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001184 }
1185
1186 // combo view callbacks
1187
1188 /**
1189 * callback from ComboPage when clear history is requested
1190 */
1191 public void onRemoveParentChildRelationships() {
1192 mTabControl.removeParentChildRelationShips();
1193 }
1194
1195 /**
1196 * callback from ComboPage when bookmark/history selection
1197 */
1198 @Override
1199 public void onUrlSelected(String url, boolean newTab) {
1200 removeComboView();
1201 if (!TextUtils.isEmpty(url)) {
1202 if (newTab) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07001203 final Tab parent = mTabControl.getCurrentTab();
1204 openTab(url,
1205 (parent != null) && parent.isPrivateBrowsingEnabled(),
1206 !mSettings.openInBackground(),
1207 true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001208 } else {
1209 final Tab currentTab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001210 loadUrl(currentTab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001211 }
1212 }
1213 }
1214
1215 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001216 * dismiss the ComboPage
1217 */
1218 @Override
1219 public void removeComboView() {
1220 mUi.hideComboView();
1221 }
1222
1223 // active tabs page handling
1224
1225 protected void showActiveTabsPage() {
1226 mMenuState = EMPTY_MENU;
1227 mUi.showActiveTabsPage();
1228 }
1229
1230 /**
1231 * Remove the active tabs page.
1232 * @param needToAttach If true, the active tabs page did not attach a tab
1233 * to the content view, so we need to do that here.
1234 */
1235 @Override
1236 public void removeActiveTabsPage(boolean needToAttach) {
1237 mMenuState = R.id.MAIN_MENU;
1238 mUi.removeActiveTabsPage();
1239 if (needToAttach) {
1240 setActiveTab(mTabControl.getCurrentTab());
1241 }
1242 getCurrentTopWebView().requestFocus();
1243 }
1244
1245 // key handling
1246 protected void onBackKey() {
1247 if (!mUi.onBackKey()) {
1248 WebView subwindow = mTabControl.getCurrentSubWindow();
1249 if (subwindow != null) {
1250 if (subwindow.canGoBack()) {
1251 subwindow.goBack();
1252 } else {
1253 dismissSubWindow(mTabControl.getCurrentTab());
1254 }
1255 } else {
1256 goBackOnePageOrQuit();
1257 }
1258 }
1259 }
1260
Michael Kolb4bd767d2011-05-27 11:33:55 -07001261 protected boolean onMenuKey() {
1262 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001263 }
1264
Michael Kolb8233fac2010-10-26 16:08:53 -07001265 // menu handling and state
1266 // TODO: maybe put into separate handler
1267
1268 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckb3417f02011-01-14 11:01:05 -08001269 if (mOptionsMenuHandler != null) {
1270 return mOptionsMenuHandler.onCreateOptionsMenu(menu);
1271 }
1272
John Reckd73c5a22010-12-22 10:22:50 -08001273 if (mMenuState == EMPTY_MENU) {
1274 return false;
1275 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001276 MenuInflater inflater = mActivity.getMenuInflater();
1277 inflater.inflate(R.menu.browser, menu);
1278 updateInLoadMenuItems(menu);
1279 // hold on to the menu reference here; it is used by the page callbacks
1280 // to update the menu based on loading state
1281 mCachedMenu = menu;
1282 return true;
1283 }
1284
1285 protected void onCreateContextMenu(ContextMenu menu, View v,
1286 ContextMenuInfo menuInfo) {
1287 if (v instanceof TitleBarBase) {
1288 return;
1289 }
1290 if (!(v instanceof WebView)) {
1291 return;
1292 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001293 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001294 WebView.HitTestResult result = webview.getHitTestResult();
1295 if (result == null) {
1296 return;
1297 }
1298
1299 int type = result.getType();
1300 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1301 Log.w(LOGTAG,
1302 "We should not show context menu when nothing is touched");
1303 return;
1304 }
1305 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1306 // let TextView handles context menu
1307 return;
1308 }
1309
1310 // Note, http://b/issue?id=1106666 is requesting that
1311 // an inflated menu can be used again. This is not available
1312 // yet, so inflate each time (yuk!)
1313 MenuInflater inflater = mActivity.getMenuInflater();
1314 inflater.inflate(R.menu.browsercontext, menu);
1315
1316 // Show the correct menu group
1317 final String extra = result.getExtra();
1318 menu.setGroupVisible(R.id.PHONE_MENU,
1319 type == WebView.HitTestResult.PHONE_TYPE);
1320 menu.setGroupVisible(R.id.EMAIL_MENU,
1321 type == WebView.HitTestResult.EMAIL_TYPE);
1322 menu.setGroupVisible(R.id.GEO_MENU,
1323 type == WebView.HitTestResult.GEO_TYPE);
1324 menu.setGroupVisible(R.id.IMAGE_MENU,
1325 type == WebView.HitTestResult.IMAGE_TYPE
1326 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1327 menu.setGroupVisible(R.id.ANCHOR_MENU,
1328 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1329 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001330 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1331 || type == WebView.HitTestResult.PHONE_TYPE
1332 || type == WebView.HitTestResult.EMAIL_TYPE
1333 || type == WebView.HitTestResult.GEO_TYPE;
1334 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1335 if (hitText) {
1336 menu.findItem(R.id.select_text_menu_id)
1337 .setOnMenuItemClickListener(new SelectText(webview));
1338 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001339 // Setup custom handling depending on the type
1340 switch (type) {
1341 case WebView.HitTestResult.PHONE_TYPE:
1342 menu.setHeaderTitle(Uri.decode(extra));
1343 menu.findItem(R.id.dial_context_menu_id).setIntent(
1344 new Intent(Intent.ACTION_VIEW, Uri
1345 .parse(WebView.SCHEME_TEL + extra)));
1346 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1347 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1348 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1349 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1350 addIntent);
1351 menu.findItem(R.id.copy_phone_context_menu_id)
1352 .setOnMenuItemClickListener(
1353 new Copy(extra));
1354 break;
1355
1356 case WebView.HitTestResult.EMAIL_TYPE:
1357 menu.setHeaderTitle(extra);
1358 menu.findItem(R.id.email_context_menu_id).setIntent(
1359 new Intent(Intent.ACTION_VIEW, Uri
1360 .parse(WebView.SCHEME_MAILTO + extra)));
1361 menu.findItem(R.id.copy_mail_context_menu_id)
1362 .setOnMenuItemClickListener(
1363 new Copy(extra));
1364 break;
1365
1366 case WebView.HitTestResult.GEO_TYPE:
1367 menu.setHeaderTitle(extra);
1368 menu.findItem(R.id.map_context_menu_id).setIntent(
1369 new Intent(Intent.ACTION_VIEW, Uri
1370 .parse(WebView.SCHEME_GEO
1371 + URLEncoder.encode(extra))));
1372 menu.findItem(R.id.copy_geo_context_menu_id)
1373 .setOnMenuItemClickListener(
1374 new Copy(extra));
1375 break;
1376
1377 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1378 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001379 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001380 // decide whether to show the open link in new tab option
1381 boolean showNewTab = mTabControl.canCreateNewTab();
1382 MenuItem newTabItem
1383 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001384 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001385 ? R.string.contextmenu_openlink_newwindow_background
1386 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001387 newTabItem.setVisible(showNewTab);
1388 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001389 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1390 newTabItem.setOnMenuItemClickListener(
1391 new MenuItem.OnMenuItemClickListener() {
1392 @Override
1393 public boolean onMenuItemClick(MenuItem item) {
1394 final HashMap<String, WebView> hrefMap =
1395 new HashMap<String, WebView>();
1396 hrefMap.put("webview", webview);
1397 final Message msg = mHandler.obtainMessage(
1398 FOCUS_NODE_HREF,
1399 R.id.open_newtab_context_menu_id,
1400 0, hrefMap);
1401 webview.requestFocusNodeHref(msg);
1402 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001403 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001404 });
1405 } else {
1406 newTabItem.setOnMenuItemClickListener(
1407 new MenuItem.OnMenuItemClickListener() {
1408 @Override
1409 public boolean onMenuItemClick(MenuItem item) {
1410 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001411 openTab(extra, parent,
1412 !mSettings.openInBackground(),
1413 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001414 return true;
1415 }
1416 });
1417 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001418 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001419 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1420 break;
1421 }
1422 // otherwise fall through to handle image part
1423 case WebView.HitTestResult.IMAGE_TYPE:
1424 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1425 menu.setHeaderTitle(extra);
1426 }
1427 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1428 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1429 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001430 setOnMenuItemClickListener(
1431 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001432 menu.findItem(R.id.set_wallpaper_context_menu_id).
1433 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1434 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001435 break;
1436
1437 default:
1438 Log.w(LOGTAG, "We should not get here.");
1439 break;
1440 }
1441 //update the ui
1442 mUi.onContextMenuCreated(menu);
1443 }
1444
1445 /**
1446 * As the menu can be open when loading state changes
1447 * we must manually update the state of the stop/reload menu
1448 * item
1449 */
1450 private void updateInLoadMenuItems(Menu menu) {
1451 if (menu == null) {
1452 return;
1453 }
1454 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1455 MenuItem src = mInLoad ?
1456 menu.findItem(R.id.stop_menu_id):
1457 menu.findItem(R.id.reload_menu_id);
1458 if (src != null) {
1459 dest.setIcon(src.getIcon());
1460 dest.setTitle(src.getTitle());
1461 }
1462 }
1463
John Reckb3417f02011-01-14 11:01:05 -08001464 boolean onPrepareOptionsMenu(Menu menu) {
1465 if (mOptionsMenuHandler != null) {
1466 return mOptionsMenuHandler.onPrepareOptionsMenu(menu);
1467 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001468 // Note: setVisible will decide whether an item is visible; while
1469 // setEnabled() will decide whether an item is enabled, which also means
1470 // whether the matching shortcut key will function.
1471 switch (mMenuState) {
1472 case EMPTY_MENU:
1473 if (mCurrentMenuState != mMenuState) {
1474 menu.setGroupVisible(R.id.MAIN_MENU, false);
1475 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1476 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1477 }
1478 break;
1479 default:
1480 if (mCurrentMenuState != mMenuState) {
1481 menu.setGroupVisible(R.id.MAIN_MENU, true);
1482 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1483 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1484 }
1485 final WebView w = getCurrentTopWebView();
1486 boolean canGoBack = false;
1487 boolean canGoForward = false;
1488 boolean isHome = false;
1489 if (w != null) {
1490 canGoBack = w.canGoBack();
1491 canGoForward = w.canGoForward();
1492 isHome = mSettings.getHomePage().equals(w.getUrl());
1493 }
1494 final MenuItem back = menu.findItem(R.id.back_menu_id);
1495 back.setEnabled(canGoBack);
1496
1497 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1498 home.setEnabled(!isHome);
1499
1500 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1501 forward.setEnabled(canGoForward);
1502
1503 // decide whether to show the share link option
1504 PackageManager pm = mActivity.getPackageManager();
1505 Intent send = new Intent(Intent.ACTION_SEND);
1506 send.setType("text/plain");
1507 ResolveInfo ri = pm.resolveActivity(send,
1508 PackageManager.MATCH_DEFAULT_ONLY);
1509 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1510
John Reck35e9dd62011-04-25 09:01:54 -07001511 boolean isNavDump = mSettings.enableNavDump();
Michael Kolb8233fac2010-10-26 16:08:53 -07001512 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1513 nav.setVisible(isNavDump);
1514 nav.setEnabled(isNavDump);
1515
John Reck35e9dd62011-04-25 09:01:54 -07001516 boolean showDebugSettings = mSettings.isDebugEnabled();
Michael Kolb8233fac2010-10-26 16:08:53 -07001517 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1518 counter.setVisible(showDebugSettings);
1519 counter.setEnabled(showDebugSettings);
1520
John Reckb3417f02011-01-14 11:01:05 -08001521 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
1522 newtab.setEnabled(getTabControl().canCreateNewTab());
Michael Kolb8233fac2010-10-26 16:08:53 -07001523
John Reck2bc80422011-06-30 15:11:49 -07001524 MenuItem saveSnapshot = menu.findItem(R.id.save_snapshot_menu_id);
1525 Tab tab = getCurrentTab();
1526 saveSnapshot.setVisible(tab != null && !tab.isSnapshot());
Michael Kolb8233fac2010-10-26 16:08:53 -07001527 break;
1528 }
1529 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001530 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001531 }
1532
1533 public boolean onOptionsItemSelected(MenuItem item) {
John Reckb3417f02011-01-14 11:01:05 -08001534 if (mOptionsMenuHandler != null &&
1535 mOptionsMenuHandler.onOptionsItemSelected(item)) {
1536 return true;
1537 }
1538
Michael Kolb8233fac2010-10-26 16:08:53 -07001539 if (item.getGroupId() != R.id.CONTEXT_MENU) {
1540 // menu remains active, so ensure comboview is dismissed
1541 // if main menu option is selected
1542 removeComboView();
1543 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001544 if (null == getCurrentTopWebView()) {
1545 return false;
1546 }
1547 if (mMenuIsDown) {
1548 // The shortcut action consumes the MENU. Even if it is still down,
1549 // it won't trigger the next shortcut action. In the case of the
1550 // shortcut action triggering a new activity, like Bookmarks, we
1551 // won't get onKeyUp for MENU. So it is important to reset it here.
1552 mMenuIsDown = false;
1553 }
1554 switch (item.getItemId()) {
1555 // -- Main menu
1556 case R.id.new_tab_menu_id:
1557 openTabToHomePage();
1558 break;
1559
1560 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001561 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001562 break;
1563
1564 case R.id.goto_menu_id:
1565 editUrl();
1566 break;
1567
1568 case R.id.bookmarks_menu_id:
1569 bookmarksOrHistoryPicker(false);
1570 break;
1571
1572 case R.id.active_tabs_menu_id:
1573 showActiveTabsPage();
1574 break;
1575
1576 case R.id.add_bookmark_menu_id:
John Reck3ffc5ca2011-06-10 15:56:06 -07001577 bookmarkCurrentPage(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001578 break;
1579
1580 case R.id.stop_reload_menu_id:
1581 if (mInLoad) {
1582 stopLoading();
1583 } else {
1584 getCurrentTopWebView().reload();
1585 }
1586 break;
1587
1588 case R.id.back_menu_id:
1589 getCurrentTopWebView().goBack();
1590 break;
1591
1592 case R.id.forward_menu_id:
1593 getCurrentTopWebView().goForward();
1594 break;
1595
1596 case R.id.close_menu_id:
1597 // Close the subwindow if it exists.
1598 if (mTabControl.getCurrentSubWindow() != null) {
1599 dismissSubWindow(mTabControl.getCurrentTab());
1600 break;
1601 }
1602 closeCurrentTab();
1603 break;
1604
1605 case R.id.homepage_menu_id:
1606 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001607 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001608 break;
1609
1610 case R.id.preferences_menu_id:
1611 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1612 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1613 getCurrentTopWebView().getUrl());
1614 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1615 break;
1616
1617 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001618 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001619 break;
1620
John Reck2bc80422011-06-30 15:11:49 -07001621 case R.id.save_snapshot_menu_id:
1622 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001623 if (source == null) break;
John Reckd8c74522011-06-14 08:45:00 -07001624 final ContentResolver cr = mActivity.getContentResolver();
1625 final ContentValues values = source.createSnapshotValues();
John Reck2bc80422011-06-30 15:11:49 -07001626 if (values != null) {
1627 new AsyncTask<Tab, Void, Long>() {
John Reckd8c74522011-06-14 08:45:00 -07001628
John Reck2bc80422011-06-30 15:11:49 -07001629 @Override
1630 protected Long doInBackground(Tab... params) {
1631 Uri result = cr.insert(Snapshots.CONTENT_URI, values);
1632 long id = ContentUris.parseId(result);
1633 return id;
John Reckd8c74522011-06-14 08:45:00 -07001634 }
John Reckf33b1632011-06-04 20:00:23 -07001635
John Reck2bc80422011-06-30 15:11:49 -07001636 @Override
1637 protected void onPostExecute(Long id) {
1638 Bundle b = new Bundle();
1639 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1640 mUi.showComboView(ComboViews.Snapshots, b);
1641 };
1642 }.execute(source);
1643 } else {
1644 Toast.makeText(mActivity, R.string.snapshot_failed,
Leon Scrogginsac993842011-02-02 12:54:07 -05001645 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001646 }
Leon Scrogginsac993842011-02-02 12:54:07 -05001647 break;
1648
Michael Kolb8233fac2010-10-26 16:08:53 -07001649 case R.id.page_info_menu_id:
Huahui Wuae0c0412011-06-28 10:17:05 -07001650 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
Michael Kolb8233fac2010-10-26 16:08:53 -07001651 break;
1652
1653 case R.id.classic_history_menu_id:
1654 bookmarksOrHistoryPicker(true);
1655 break;
1656
1657 case R.id.title_bar_share_page_url:
1658 case R.id.share_page_menu_id:
1659 Tab currentTab = mTabControl.getCurrentTab();
1660 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001661 return false;
1662 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001663 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001664 break;
1665
1666 case R.id.dump_nav_menu_id:
1667 getCurrentTopWebView().debugDump();
1668 break;
1669
1670 case R.id.dump_counters_menu_id:
1671 getCurrentTopWebView().dumpV8Counters();
1672 break;
1673
1674 case R.id.zoom_in_menu_id:
1675 getCurrentTopWebView().zoomIn();
1676 break;
1677
1678 case R.id.zoom_out_menu_id:
1679 getCurrentTopWebView().zoomOut();
1680 break;
1681
1682 case R.id.view_downloads_menu_id:
1683 viewDownloads();
1684 break;
1685
1686 case R.id.window_one_menu_id:
1687 case R.id.window_two_menu_id:
1688 case R.id.window_three_menu_id:
1689 case R.id.window_four_menu_id:
1690 case R.id.window_five_menu_id:
1691 case R.id.window_six_menu_id:
1692 case R.id.window_seven_menu_id:
1693 case R.id.window_eight_menu_id:
1694 {
1695 int menuid = item.getItemId();
1696 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1697 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1698 Tab desiredTab = mTabControl.getTab(id);
1699 if (desiredTab != null &&
1700 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001701 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001702 }
1703 break;
1704 }
1705 }
1706 }
1707 break;
1708
1709 default:
1710 return false;
1711 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001712 return true;
1713 }
1714
1715 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001716 // Let the History and Bookmark fragments handle menus they created.
1717 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1718 return false;
1719 }
1720
Michael Kolb8233fac2010-10-26 16:08:53 -07001721 int id = item.getItemId();
1722 boolean result = true;
1723 switch (id) {
1724 // For the context menu from the title bar
1725 case R.id.title_bar_copy_page_url:
1726 Tab currentTab = mTabControl.getCurrentTab();
1727 if (null == currentTab) {
1728 result = false;
1729 break;
1730 }
1731 WebView mainView = currentTab.getWebView();
1732 if (null == mainView) {
1733 result = false;
1734 break;
1735 }
1736 copy(mainView.getUrl());
1737 break;
1738 // -- Browser context menu
1739 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001740 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001741 case R.id.copy_link_context_menu_id:
1742 final WebView webView = getCurrentTopWebView();
1743 if (null == webView) {
1744 result = false;
1745 break;
1746 }
1747 final HashMap<String, WebView> hrefMap =
1748 new HashMap<String, WebView>();
1749 hrefMap.put("webview", webView);
1750 final Message msg = mHandler.obtainMessage(
1751 FOCUS_NODE_HREF, id, 0, hrefMap);
1752 webView.requestFocusNodeHref(msg);
1753 break;
1754
1755 default:
1756 // For other context menus
1757 result = onOptionsItemSelected(item);
1758 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001759 return result;
1760 }
1761
1762 /**
1763 * support programmatically opening the context menu
1764 */
1765 public void openContextMenu(View view) {
1766 mActivity.openContextMenu(view);
1767 }
1768
1769 /**
1770 * programmatically open the options menu
1771 */
1772 public void openOptionsMenu() {
1773 mActivity.openOptionsMenu();
1774 }
1775
1776 public boolean onMenuOpened(int featureId, Menu menu) {
1777 if (mOptionsMenuOpen) {
1778 if (mConfigChanged) {
1779 // We do not need to make any changes to the state of the
1780 // title bar, since the only thing that happened was a
1781 // change in orientation
1782 mConfigChanged = false;
1783 } else {
1784 if (!mExtendedMenuOpen) {
1785 mExtendedMenuOpen = true;
1786 mUi.onExtendedMenuOpened();
1787 } else {
1788 // Switching the menu back to icon view, so show the
1789 // title bar once again.
1790 mExtendedMenuOpen = false;
1791 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001792 }
1793 }
1794 } else {
1795 // The options menu is closed, so open it, and show the title
1796 mOptionsMenuOpen = true;
1797 mConfigChanged = false;
1798 mExtendedMenuOpen = false;
1799 mUi.onOptionsMenuOpened();
1800 }
1801 return true;
1802 }
1803
1804 public void onOptionsMenuClosed(Menu menu) {
1805 mOptionsMenuOpen = false;
1806 mUi.onOptionsMenuClosed(mInLoad);
1807 }
1808
1809 public void onContextMenuClosed(Menu menu) {
1810 mUi.onContextMenuClosed(menu, mInLoad);
1811 }
1812
1813 // Helper method for getting the top window.
1814 @Override
1815 public WebView getCurrentTopWebView() {
1816 return mTabControl.getCurrentTopWebView();
1817 }
1818
1819 @Override
1820 public WebView getCurrentWebView() {
1821 return mTabControl.getCurrentWebView();
1822 }
1823
1824 /*
1825 * This method is called as a result of the user selecting the options
1826 * menu to see the download window. It shows the download window on top of
1827 * the current window.
1828 */
1829 void viewDownloads() {
1830 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1831 mActivity.startActivity(intent);
1832 }
1833
1834 // action mode
1835
1836 void onActionModeStarted(ActionMode mode) {
1837 mUi.onActionModeStarted(mode);
1838 mActionMode = mode;
1839 }
1840
1841 /*
1842 * True if a custom ActionMode (i.e. find or select) is in use.
1843 */
1844 @Override
1845 public boolean isInCustomActionMode() {
1846 return mActionMode != null;
1847 }
1848
1849 /*
1850 * End the current ActionMode.
1851 */
1852 @Override
1853 public void endActionMode() {
1854 if (mActionMode != null) {
1855 mActionMode.finish();
1856 }
1857 }
1858
1859 /*
1860 * Called by find and select when they are finished. Replace title bars
1861 * as necessary.
1862 */
1863 public void onActionModeFinished(ActionMode mode) {
1864 if (!isInCustomActionMode()) return;
1865 mUi.onActionModeFinished(mInLoad);
1866 mActionMode = null;
1867 }
1868
1869 boolean isInLoad() {
1870 return mInLoad;
1871 }
1872
1873 // bookmark handling
1874
1875 /**
1876 * add the current page as a bookmark to the given folder id
1877 * @param folderId use -1 for the default folder
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001878 * @param canBeAnEdit If true, check to see whether the site is already
1879 * bookmarked, and if it is, edit that bookmark. If false, and
1880 * the site is already bookmarked, do not attempt to edit the
1881 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001882 */
1883 @Override
John Reck3ffc5ca2011-06-10 15:56:06 -07001884 public void bookmarkCurrentPage(boolean canBeAnEdit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001885 Intent i = new Intent(mActivity,
1886 AddBookmarkPage.class);
1887 WebView w = getCurrentTopWebView();
1888 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1889 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1890 String touchIconUrl = w.getTouchIconUrl();
1891 if (touchIconUrl != null) {
1892 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1893 WebSettings settings = w.getSettings();
1894 if (settings != null) {
1895 i.putExtra(AddBookmarkPage.USER_AGENT,
1896 settings.getUserAgentString());
1897 }
1898 }
1899 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1900 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1901 getDesiredThumbnailHeight(mActivity)));
1902 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001903 if (canBeAnEdit) {
1904 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1905 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001906 // Put the dialog at the upper right of the screen, covering the
1907 // star on the title bar.
1908 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
1909 mActivity.startActivity(i);
1910 }
1911
1912 // file chooser
1913 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1914 mUploadHandler = new UploadHandler(this);
1915 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1916 }
1917
1918 // thumbnails
1919
1920 /**
1921 * Return the desired width for thumbnail screenshots, which are stored in
1922 * the database, and used on the bookmarks screen.
1923 * @param context Context for finding out the density of the screen.
1924 * @return desired width for thumbnail screenshot.
1925 */
1926 static int getDesiredThumbnailWidth(Context context) {
1927 return context.getResources().getDimensionPixelOffset(
1928 R.dimen.bookmarkThumbnailWidth);
1929 }
1930
1931 /**
1932 * Return the desired height for thumbnail screenshots, which are stored in
1933 * the database, and used on the bookmarks screen.
1934 * @param context Context for finding out the density of the screen.
1935 * @return desired height for thumbnail screenshot.
1936 */
1937 static int getDesiredThumbnailHeight(Context context) {
1938 return context.getResources().getDimensionPixelOffset(
1939 R.dimen.bookmarkThumbnailHeight);
1940 }
1941
Michael Kolb1acef692011-03-08 14:12:06 -08001942 static Bitmap createScreenshot(Tab tab, int width, int height) {
1943 if ((tab != null) && (tab.getWebView() != null)) {
1944 return createScreenshot(tab.getWebView(), width, height);
1945 }
1946 return null;
1947 }
1948
John Reck8cc92352011-07-06 17:41:52 -07001949 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08001950 // We render to a bitmap 2x the desired size so that we can then
1951 // re-scale it with filtering since canvas.scale doesn't filter
1952 // This helps reduce aliasing at the cost of being slightly blurry
1953 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07001954 Picture thumbnail = view.capturePicture();
1955 if (thumbnail == null) {
1956 return null;
1957 }
John Reck5c6ac2f2011-01-05 10:18:03 -08001958 width *= filter_scale;
1959 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07001960 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1961 Canvas canvas = new Canvas(bm);
1962 // May need to tweak these values to determine what is the
1963 // best scale factor
1964 int thumbnailWidth = thumbnail.getWidth();
1965 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08001966 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08001967 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08001968 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07001969 } else {
1970 return null;
1971 }
John Reckfe49ab42010-11-16 17:09:37 -08001972
Michael Kolbeb95db42011-03-03 10:38:40 -08001973 float scaleFactorY = (float) height / (float)thumbnailHeight;
1974 if (scaleFactorY > scaleFactor) {
1975 // The picture is narrower than the requested AR
1976 // Center the thumnail and crop the sides
1977 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08001978 float wx = (thumbnailWidth * scaleFactor) - width;
1979 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07001980 }
1981
John Reckfe49ab42010-11-16 17:09:37 -08001982 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07001983
1984 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08001985 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
1986 height / filter_scale, true);
1987 bm.recycle();
1988 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07001989 }
1990
John Reck34ef2672011-02-10 11:30:55 -08001991 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001992 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07001993 // FIXME: Would like to make sure there is actually something to
1994 // draw, but the API for that (WebViewCore.pictureReady()) is not
1995 // currently accessible here.
1996
John Reck34ef2672011-02-10 11:30:55 -08001997 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08001998 if (view == null) {
1999 // Tab was destroyed
2000 return;
2001 }
John Reck34ef2672011-02-10 11:30:55 -08002002 final String url = tab.getUrl();
2003 final String originalUrl = view.getOriginalUrl();
2004
2005 if (TextUtils.isEmpty(url)) {
2006 return;
2007 }
2008
2009 // Only update thumbnails for web urls (http(s)://), not for
2010 // about:, javascript:, data:, etc...
2011 // Unless it is a bookmarked site, then always update
2012 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2013 return;
2014 }
2015
Michael Kolb8233fac2010-10-26 16:08:53 -07002016 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2017 getDesiredThumbnailHeight(mActivity));
2018 if (bm == null) {
2019 return;
2020 }
2021
2022 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002023 new AsyncTask<Void, Void, Void>() {
2024 @Override
2025 protected Void doInBackground(Void... unused) {
2026 Cursor cursor = null;
2027 try {
2028 // TODO: Clean this up
2029 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2030 if (cursor != null && cursor.moveToFirst()) {
2031 final ByteArrayOutputStream os =
2032 new ByteArrayOutputStream();
2033 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002034
John Reck34ef2672011-02-10 11:30:55 -08002035 ContentValues values = new ContentValues();
2036 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002037
John Reck34ef2672011-02-10 11:30:55 -08002038 do {
John Reck617fd832011-02-16 14:35:59 -08002039 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002040 cr.update(Images.CONTENT_URI, values, null, null);
2041 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002042 }
John Reck34ef2672011-02-10 11:30:55 -08002043 } catch (IllegalStateException e) {
2044 // Ignore
2045 } finally {
2046 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002047 }
John Reck34ef2672011-02-10 11:30:55 -08002048 return null;
2049 }
2050 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002051 }
2052
2053 private class Copy implements OnMenuItemClickListener {
2054 private CharSequence mText;
2055
2056 public boolean onMenuItemClick(MenuItem item) {
2057 copy(mText);
2058 return true;
2059 }
2060
2061 public Copy(CharSequence toCopy) {
2062 mText = toCopy;
2063 }
2064 }
2065
Leon Scroggins63c02662010-11-18 15:16:27 -05002066 private static class Download implements OnMenuItemClickListener {
2067 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002068 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002069 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002070
2071 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002072 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002073 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002074 return true;
2075 }
2076
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002077 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002078 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002079 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002080 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002081 }
2082 }
2083
Cary Clark8974d282010-11-22 10:46:05 -05002084 private static class SelectText implements OnMenuItemClickListener {
2085 private WebView mWebView;
2086
2087 public boolean onMenuItemClick(MenuItem item) {
2088 if (mWebView != null) {
2089 return mWebView.selectText();
2090 }
2091 return false;
2092 }
2093
2094 public SelectText(WebView webView) {
2095 mWebView = webView;
2096 }
2097
2098 }
2099
Michael Kolb8233fac2010-10-26 16:08:53 -07002100 /********************** TODO: UI stuff *****************************/
2101
2102 // these methods have been copied, they still need to be cleaned up
2103
2104 /****************** tabs ***************************************************/
2105
2106 // basic tab interactions:
2107
2108 // it is assumed that tabcontrol already knows about the tab
2109 protected void addTab(Tab tab) {
2110 mUi.addTab(tab);
2111 }
2112
2113 protected void removeTab(Tab tab) {
2114 mUi.removeTab(tab);
2115 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002116 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002117 }
2118
Michael Kolbf2055602011-04-09 17:20:03 -07002119 @Override
2120 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002121 // monkey protection against delayed start
2122 if (tab != null) {
2123 mTabControl.setCurrentTab(tab);
2124 // the tab is guaranteed to have a webview after setCurrentTab
2125 mUi.setActiveTab(tab);
2126 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002127 }
2128
2129 protected void closeEmptyChildTab() {
2130 Tab current = mTabControl.getCurrentTab();
2131 if (current != null
2132 && current.getWebView().copyBackForwardList().getSize() == 0) {
Michael Kolbc831b632011-05-11 09:30:34 -07002133 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002134 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002135 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002136 closeTab(current);
2137 }
2138 }
2139 }
2140
John Reck26b18322011-06-21 13:08:58 -07002141 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002142 // Dismiss the subwindow if applicable.
2143 dismissSubWindow(appTab);
2144 // Since we might kill the WebView, remove it from the
2145 // content view first.
2146 mUi.detachTab(appTab);
2147 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002148 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002149 // TODO: analyze why the remove and add are necessary
2150 mUi.attachTab(appTab);
2151 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002152 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002153 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002154 } else {
2155 // If the tab was the current tab, we have to attach
2156 // it to the view system again.
2157 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002158 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002159 }
2160 }
2161
2162 // Remove the sub window if it exists. Also called by TabControl when the
2163 // user clicks the 'X' to dismiss a sub window.
2164 public void dismissSubWindow(Tab tab) {
2165 removeSubWindow(tab);
2166 // dismiss the subwindow. This will destroy the WebView.
2167 tab.dismissSubWindow();
2168 getCurrentTopWebView().requestFocus();
2169 }
2170
2171 @Override
2172 public void removeSubWindow(Tab t) {
2173 if (t.getSubWebView() != null) {
2174 mUi.removeSubWindow(t.getSubViewContainer());
2175 }
2176 }
2177
2178 @Override
2179 public void attachSubWindow(Tab tab) {
2180 if (tab.getSubWebView() != null) {
2181 mUi.attachSubWindow(tab.getSubViewContainer());
2182 getCurrentTopWebView().requestFocus();
2183 }
2184 }
2185
Mathew Inwood29721c22011-06-29 17:55:24 +01002186 private Tab showPreloadedTab(final UrlData urlData) {
2187 if (!urlData.isPreloaded()) {
2188 return null;
2189 }
2190 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2191 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2192 if (sbQuery != null) {
2193 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
2194 // Could not submit query. Fallback to regular tab creation
2195 tabControl.destroy();
2196 return null;
2197 }
2198 }
2199 Tab t = tabControl.getTab();
2200 mTabControl.addPreloadedTab(t);
2201 addTab(t);
2202 setActiveTab(t);
2203 return t;
2204 }
2205
Michael Kolb7bcafde2011-05-09 13:55:59 -07002206 // open a non inconito tab with the given url data
2207 // and set as active tab
2208 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002209 Tab tab = showPreloadedTab(urlData);
2210 if (tab == null) {
2211 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002212 if ((tab != null) && !urlData.isEmpty()) {
2213 loadUrlDataIn(tab, urlData);
2214 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002215 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002216 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002217 }
2218
Michael Kolb843510f2010-12-09 10:51:49 -08002219 @Override
2220 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002221 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002222 }
2223
Michael Kolb8233fac2010-10-26 16:08:53 -07002224 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002225 public Tab openIncognitoTab() {
2226 return openTab(INCOGNITO_URI, true, true, false);
2227 }
2228
2229 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002230 public Tab openTab(String url, boolean incognito, boolean setActive,
2231 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002232 return openTab(url, incognito, setActive, useCurrent, null);
2233 }
2234
2235 @Override
2236 public Tab openTab(String url, Tab parent, boolean setActive,
2237 boolean useCurrent) {
2238 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2239 setActive, useCurrent, parent);
2240 }
2241
2242 public Tab openTab(String url, boolean incognito, boolean setActive,
2243 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002244 Tab tab = createNewTab(incognito, setActive, useCurrent);
2245 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002246 if (parent != null && parent != tab) {
2247 parent.addChildTab(tab);
2248 }
Michael Kolb519d2282011-05-09 17:03:19 -07002249 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002250 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002251 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002252 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002253 return tab;
2254 }
2255
2256 // this method will attempt to create a new tab
2257 // incognito: private browsing tab
2258 // setActive: ste tab as current tab
2259 // useCurrent: if no new tab can be created, return current tab
2260 private Tab createNewTab(boolean incognito, boolean setActive,
2261 boolean useCurrent) {
2262 Tab tab = null;
2263 if (mTabControl.canCreateNewTab()) {
2264 tab = mTabControl.createNewTab(incognito);
2265 addTab(tab);
2266 if (setActive) {
2267 setActiveTab(tab);
2268 }
2269 } else {
2270 if (useCurrent) {
2271 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002272 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002273 } else {
2274 mUi.showMaxTabsWarning();
2275 }
2276 }
2277 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002278 }
2279
John Reck2bc80422011-06-30 15:11:49 -07002280 @Override
2281 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2282 SnapshotTab tab = null;
2283 if (mTabControl.canCreateNewTab()) {
2284 tab = mTabControl.createSnapshotTab(snapshotId);
2285 addTab(tab);
2286 if (setActive) {
2287 setActiveTab(tab);
2288 }
2289 } else {
2290 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002291 }
2292 return tab;
2293 }
2294
Michael Kolb8233fac2010-10-26 16:08:53 -07002295 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002296 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002297 * @return boolean True if we successfully switched to a different tab. If
2298 * the indexth tab is null, or if that tab is the same as
2299 * the current one, return false.
2300 */
2301 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002302 public boolean switchToTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002303 // hide combo view if open
2304 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002305 Tab currentTab = mTabControl.getCurrentTab();
2306 if (tab == null || tab == currentTab) {
2307 return false;
2308 }
2309 setActiveTab(tab);
2310 return true;
2311 }
2312
2313 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002314 public void closeCurrentTab() {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002315 // hide combo view if open
2316 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002317 if (mTabControl.getTabCount() == 1) {
John Reck958b2422010-12-03 17:56:17 -08002318 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002319 return;
2320 }
Michael Kolbc831b632011-05-11 09:30:34 -07002321 final Tab current = mTabControl.getCurrentTab();
2322 final int pos = mTabControl.getCurrentPosition();
2323 Tab newTab = current.getParent();
2324 if (newTab == null) {
2325 newTab = mTabControl.getTab(pos + 1);
2326 if (newTab == null) {
2327 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002328 }
2329 }
Michael Kolbc831b632011-05-11 09:30:34 -07002330 if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002331 // Close window
2332 closeTab(current);
2333 }
2334 }
2335
2336 /**
2337 * Close the tab, remove its associated title bar, and adjust mTabControl's
2338 * current tab to a valid value.
2339 */
2340 @Override
2341 public void closeTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002342 // hide combo view if open
2343 removeComboView();
Michael Kolb2d59c322011-01-25 13:18:55 -08002344 removeTab(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002345 }
2346
Michael Kolb8233fac2010-10-26 16:08:53 -07002347 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002348 protected void loadUrlFromContext(String url) {
2349 Tab tab = getCurrentTab();
2350 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002351 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002352 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002353 url = UrlUtils.smartUrlFilter(url);
2354 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002355 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002356 }
2357 }
2358 }
2359
2360 /**
2361 * Load the URL into the given WebView and update the title bar
2362 * to reflect the new load. Call this instead of WebView.loadUrl
2363 * directly.
2364 * @param view The WebView used to load url.
2365 * @param url The URL to load.
2366 */
John Reck71e51422011-07-01 16:49:28 -07002367 @Override
2368 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002369 loadUrl(tab, url, null);
2370 }
2371
2372 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2373 if (tab != null) {
2374 dismissSubWindow(tab);
2375 tab.loadUrl(url, headers);
2376 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002377 }
2378
2379 /**
2380 * Load UrlData into a Tab and update the title bar to reflect the new
2381 * load. Call this instead of UrlData.loadIn directly.
2382 * @param t The Tab used to load.
2383 * @param data The UrlData being loaded.
2384 */
2385 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002386 if (data != null) {
2387 if (data.mVoiceIntent != null) {
2388 t.activateVoiceSearchMode(data.mVoiceIntent);
Michael Kolb14612442011-06-24 13:06:29 -07002389 } else if (data.isPreloaded()) {
2390 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002391 } else {
2392 loadUrl(t, data.mUrl, data.mHeaders);
2393 }
2394 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002395 }
2396
John Reck30c714c2010-12-16 17:30:34 -08002397 @Override
2398 public void onUserCanceledSsl(Tab tab) {
2399 WebView web = tab.getWebView();
2400 // TODO: Figure out the "right" behavior
2401 if (web.canGoBack()) {
2402 web.goBack();
2403 } else {
2404 web.loadUrl(mSettings.getHomePage());
2405 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002406 }
2407
2408 void goBackOnePageOrQuit() {
2409 Tab current = mTabControl.getCurrentTab();
2410 if (current == null) {
2411 /*
2412 * Instead of finishing the activity, simply push this to the back
2413 * of the stack and let ActivityManager to choose the foreground
2414 * activity. As BrowserActivity is singleTask, it will be always the
2415 * root of the task. So we can use either true or false for
2416 * moveTaskToBack().
2417 */
2418 mActivity.moveTaskToBack(true);
2419 return;
2420 }
2421 WebView w = current.getWebView();
2422 if (w.canGoBack()) {
2423 w.goBack();
2424 } else {
2425 // Check to see if we are closing a window that was created by
2426 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002427 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002428 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002429 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002430 // Now we close the other tab
2431 closeTab(current);
2432 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002433 /*
2434 * Instead of finishing the activity, simply push this to the back
2435 * of the stack and let ActivityManager to choose the foreground
2436 * activity. As BrowserActivity is singleTask, it will be always the
2437 * root of the task. So we can use either true or false for
2438 * moveTaskToBack().
2439 */
2440 mActivity.moveTaskToBack(true);
2441 }
2442 }
2443 }
2444
2445 /**
2446 * Feed the previously stored results strings to the BrowserProvider so that
2447 * the SearchDialog will show them instead of the standard searches.
2448 * @param result String to show on the editable line of the SearchDialog.
2449 */
2450 @Override
2451 public void showVoiceSearchResults(String result) {
2452 ContentProviderClient client = mActivity.getContentResolver()
2453 .acquireContentProviderClient(Browser.BOOKMARKS_URI);
2454 ContentProvider prov = client.getLocalContentProvider();
2455 BrowserProvider bp = (BrowserProvider) prov;
2456 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
2457 client.release();
2458
2459 Bundle bundle = createGoogleSearchSourceBundle(
2460 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
2461 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
2462 startSearch(result, false, bundle, false);
2463 }
2464
2465 private void startSearch(String initialQuery, boolean selectInitialQuery,
2466 Bundle appSearchData, boolean globalSearch) {
2467 if (appSearchData == null) {
2468 appSearchData = createGoogleSearchSourceBundle(
2469 GOOGLE_SEARCH_SOURCE_TYPE);
2470 }
2471
2472 SearchEngine searchEngine = mSettings.getSearchEngine();
2473 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
2474 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
2475 }
2476 mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData,
2477 globalSearch);
2478 }
2479
2480 private Bundle createGoogleSearchSourceBundle(String source) {
2481 Bundle bundle = new Bundle();
2482 bundle.putString(Search.SOURCE, source);
2483 return bundle;
2484 }
2485
2486 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002487 * helper method for key handler
2488 * returns the current tab if it can't advance
2489 */
Michael Kolbc831b632011-05-11 09:30:34 -07002490 private Tab getNextTab() {
2491 return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1,
2492 mTabControl.getCurrentPosition() + 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002493 }
2494
2495 /**
2496 * helper method for key handler
2497 * returns the current tab if it can't advance
2498 */
Michael Kolbc831b632011-05-11 09:30:34 -07002499 private Tab getPrevTab() {
2500 return mTabControl.getTab(Math.max(0,
2501 mTabControl.getCurrentPosition() - 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002502 }
2503
2504 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002505 * handle key events in browser
2506 *
2507 * @param keyCode
2508 * @param event
2509 * @return true if handled, false to pass to super
2510 */
2511 boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002512 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002513 // Even if MENU is already held down, we need to call to super to open
2514 // the IME on long press.
Michael Kolb2814a362011-05-19 15:49:41 -07002515 if (KeyEvent.KEYCODE_MENU == keyCode) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002516 if (mOptionsMenuHandler != null) {
2517 return false;
2518 } else {
2519 event.startTracking();
2520 return true;
2521 }
Michael Kolb2814a362011-05-19 15:49:41 -07002522 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002523 if (!noModifiers
2524 && ((KeyEvent.KEYCODE_MENU == keyCode)
2525 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2526 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002527 mMenuIsDown = true;
2528 return false;
2529 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002530
Cary Clark8ff8c662010-12-29 15:03:05 -05002531 WebView webView = getCurrentTopWebView();
2532 if (webView == null) return false;
2533
Cary Clark160bbb92011-01-10 11:17:07 -05002534 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2535 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002536
Michael Kolb8233fac2010-10-26 16:08:53 -07002537 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002538 case KeyEvent.KEYCODE_TAB:
2539 if (event.isCtrlPressed()) {
2540 if (event.isShiftPressed()) {
2541 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002542 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002543 } else {
2544 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002545 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002546 }
2547 return true;
2548 }
2549 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002550 case KeyEvent.KEYCODE_SPACE:
2551 // WebView/WebTextView handle the keys in the KeyDown. As
2552 // the Activity's shortcut keys are only handled when WebView
2553 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002554 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002555 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002556 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002557 pageDown();
2558 }
2559 return true;
2560 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002561 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002562 event.startTracking();
2563 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002564 case KeyEvent.KEYCODE_DPAD_LEFT:
2565 if (ctrl) {
2566 webView.goBack();
2567 return true;
2568 }
2569 break;
2570 case KeyEvent.KEYCODE_DPAD_RIGHT:
2571 if (ctrl) {
2572 webView.goForward();
2573 return true;
2574 }
2575 break;
2576 case KeyEvent.KEYCODE_A:
2577 if (ctrl) {
2578 webView.selectAll();
2579 return true;
2580 }
2581 break;
Michael Kolba4183062011-01-16 10:43:21 -08002582// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002583 case KeyEvent.KEYCODE_C:
2584 if (ctrl) {
2585 webView.copySelection();
2586 return true;
2587 }
2588 break;
Michael Kolba4183062011-01-16 10:43:21 -08002589// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002590// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002591// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002592// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002593// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002594// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002595// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002596// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002597// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002598// case KeyEvent.KEYCODE_M: // unused
2599// case KeyEvent.KEYCODE_N: // in Chrome: new window
2600// case KeyEvent.KEYCODE_O: // in Chrome: open file
2601// case KeyEvent.KEYCODE_P: // in Chrome: print page
2602// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002603// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002604// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2605 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002606 // we can't use the ctrl/shift flags, they check for
2607 // exclusive use of a modifier
2608 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002609 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002610 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002611 } else {
2612 openTabToHomePage();
2613 }
2614 return true;
2615 }
2616 break;
2617// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2618// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb1a2eba42011-03-16 16:42:49 -07002619 case KeyEvent.KEYCODE_W: // in Chrome: close tab
2620 if (ctrl) {
2621 closeCurrentTab();
2622 return true;
2623 }
2624 break;
Cary Clark8ff8c662010-12-29 15:03:05 -05002625// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2626// case KeyEvent.KEYCODE_Y: // unused
2627// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002628 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002629 // it is a regular key and webview is not null
2630 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002631 }
2632
John Recke6bf4ab2011-02-24 15:48:05 -08002633 boolean onKeyLongPress(int keyCode, KeyEvent event) {
2634 switch(keyCode) {
2635 case KeyEvent.KEYCODE_BACK:
2636 if (mUi.showsWeb()) {
2637 bookmarksOrHistoryPicker(true);
2638 return true;
2639 }
2640 break;
2641 }
2642 return false;
2643 }
2644
Michael Kolb8233fac2010-10-26 16:08:53 -07002645 boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb2814a362011-05-19 15:49:41 -07002646 if (KeyEvent.KEYCODE_MENU == keyCode) {
2647 mMenuIsDown = false;
2648 if (event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002649 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002650 }
2651 }
Cary Clark160bbb92011-01-10 11:17:07 -05002652 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002653 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002654 case KeyEvent.KEYCODE_BACK:
2655 if (event.isTracking() && !event.isCanceled()) {
2656 onBackKey();
2657 return true;
2658 }
2659 break;
2660 }
2661 return false;
2662 }
2663
2664 public boolean isMenuDown() {
2665 return mMenuIsDown;
2666 }
2667
Ben Murdoch8029a772010-11-16 11:58:21 +00002668 public void setupAutoFill(Message message) {
2669 // Open the settings activity at the AutoFill profile fragment so that
2670 // the user can create a new profile. When they return, we will dispatch
2671 // the message so that we can autofill the form using their new profile.
2672 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2673 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2674 AutoFillSettingsFragment.class.getName());
2675 mAutoFillSetupMessage = message;
2676 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2677 }
John Reckb3417f02011-01-14 11:01:05 -08002678
2679 @Override
2680 public void registerOptionsMenuHandler(OptionsMenuHandler handler) {
2681 mOptionsMenuHandler = handler;
2682 }
2683
2684 @Override
2685 public void unregisterOptionsMenuHandler(OptionsMenuHandler handler) {
2686 if (mOptionsMenuHandler == handler) {
2687 mOptionsMenuHandler = null;
2688 }
2689 }
2690
Narayan Kamath5119edd2011-02-23 15:49:17 +00002691 @Override
2692 public void registerDropdownChangeListener(DropdownChangeListener d) {
2693 mUi.registerDropdownChangeListener(d);
2694 }
Michael Kolbfbc579a2011-07-07 15:59:33 -07002695
2696 public boolean onSearchRequested() {
2697 mUi.editUrl(false);
2698 return true;
2699 }
2700
Michael Kolb8233fac2010-10-26 16:08:53 -07002701}