blob: 116f0e084fbb3b9ca427c02ec8d8e9f3baced1af [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
20import android.app.DownloadManager;
21import android.app.SearchManager;
22import android.content.ClipboardManager;
23import android.content.ContentProvider;
24import android.content.ContentProviderClient;
25import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070026import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.content.ContentValues;
28import android.content.Context;
29import android.content.Intent;
30import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
32import android.content.res.Configuration;
Leon Scroggins1961ed22010-12-07 15:22:21 -050033import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import android.database.Cursor;
35import android.database.sqlite.SQLiteDatabase;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.graphics.Bitmap;
37import android.graphics.Canvas;
38import android.graphics.Picture;
39import android.net.Uri;
40import android.net.http.SslError;
41import android.os.AsyncTask;
42import android.os.Bundle;
43import android.os.Handler;
44import android.os.Message;
45import android.os.PowerManager;
46import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000047import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.provider.Browser;
49import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.provider.BrowserContract.Images;
51import android.provider.ContactsContract;
52import android.provider.ContactsContract.Intents.Insert;
Michael Kolbcfa3af52010-12-14 10:36:11 -080053import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.text.TextUtils;
55import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080056import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070057import android.view.ActionMode;
58import android.view.ContextMenu;
59import android.view.ContextMenu.ContextMenuInfo;
60import android.view.Gravity;
61import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070062import android.view.Menu;
63import android.view.MenuInflater;
64import android.view.MenuItem;
65import android.view.MenuItem.OnMenuItemClickListener;
66import android.view.View;
67import android.webkit.CookieManager;
68import android.webkit.CookieSyncManager;
69import android.webkit.HttpAuthHandler;
Mathew Inwood29721c22011-06-29 17:55:24 +010070import android.webkit.SearchBox;
Michael Kolb8233fac2010-10-26 16:08:53 -070071import android.webkit.SslErrorHandler;
72import android.webkit.ValueCallback;
73import android.webkit.WebChromeClient;
74import android.webkit.WebIconDatabase;
75import android.webkit.WebSettings;
76import android.webkit.WebView;
Leon Scrogginsac993842011-02-02 12:54:07 -050077import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070078
Michael Kolb4bd767d2011-05-27 11:33:55 -070079import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070080import com.android.browser.UI.ComboViews;
Michael Kolb4bd767d2011-05-27 11:33:55 -070081import com.android.browser.UI.DropdownChangeListener;
82import com.android.browser.provider.BrowserProvider;
John Reck8cc92352011-07-06 17:41:52 -070083import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070084import com.android.browser.search.SearchEngine;
85import com.android.common.Search;
86
Michael Kolb8233fac2010-10-26 16:08:53 -070087import java.io.ByteArrayOutputStream;
88import java.io.File;
89import java.net.URLEncoder;
90import java.util.Calendar;
91import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080092import java.util.List;
John Reck26b18322011-06-21 13:08:58 -070093import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -070094
95/**
96 * Controller for browser
97 */
98public class Controller
99 implements WebViewController, UiController {
100
101 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800102 private static final String SEND_APP_ID_EXTRA =
103 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700104 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800105
Michael Kolb8233fac2010-10-26 16:08:53 -0700106
107 // public message ids
108 public final static int LOAD_URL = 1001;
109 public final static int STOP_LOAD = 1002;
110
111 // Message Ids
112 private static final int FOCUS_NODE_HREF = 102;
113 private static final int RELEASE_WAKELOCK = 107;
114
115 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
116
117 private static final int OPEN_BOOKMARKS = 201;
118
119 private static final int EMPTY_MENU = -1;
120
Michael Kolb8233fac2010-10-26 16:08:53 -0700121 // activity requestCode
122 final static int PREFERENCES_PAGE = 3;
123 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000124 final static int AUTOFILL_SETUP = 5;
125
Michael Kolb8233fac2010-10-26 16:08:53 -0700126 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
127
128 // As the ids are dynamically created, we can't guarantee that they will
129 // be in sequence, so this static array maps ids to a window number.
130 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
131 { R.id.window_one_menu_id, R.id.window_two_menu_id,
132 R.id.window_three_menu_id, R.id.window_four_menu_id,
133 R.id.window_five_menu_id, R.id.window_six_menu_id,
134 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
135
136 // "source" parameter for Google search through search key
137 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
138 // "source" parameter for Google search through simplily type
139 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
140
Guang Zhu9e78f512011-05-04 11:45:11 -0700141 // "no-crash-recovery" parameter in intetnt to suppress crash recovery
142 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
143
Michael Kolb8233fac2010-10-26 16:08:53 -0700144 private Activity mActivity;
145 private UI mUi;
146 private TabControl mTabControl;
147 private BrowserSettings mSettings;
148 private WebViewFactory mFactory;
John Reckb3417f02011-01-14 11:01:05 -0800149 private OptionsMenuHandler mOptionsMenuHandler = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700150
151 private WakeLock mWakeLock;
152
153 private UrlHandler mUrlHandler;
154 private UploadHandler mUploadHandler;
155 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 private PageDialogsHandler mPageDialogsHandler;
157 private NetworkStateHandler mNetworkHandler;
Martijn Coenenb2f93552011-06-14 10:48:35 +0200158 private NfcHandler mNfcHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700159
Ben Murdoch8029a772010-11-16 11:58:21 +0000160 private Message mAutoFillSetupMessage;
161
Michael Kolb8233fac2010-10-26 16:08:53 -0700162 private boolean mShouldShowErrorConsole;
163
164 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
165
166 // FIXME, temp address onPrepareMenu performance problem.
167 // When we move everything out of view, we should rewrite this.
168 private int mCurrentMenuState = 0;
169 private int mMenuState = R.id.MAIN_MENU;
170 private int mOldMenuState = EMPTY_MENU;
171 private Menu mCachedMenu;
172
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 private boolean mMenuIsDown;
174
175 // For select and find, we keep track of the ActionMode so that
176 // finish() can be called as desired.
177 private ActionMode mActionMode;
178
179 /**
180 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
181 * of whether the configuration has changed. The first onMenuOpened call
182 * after a configuration change is simply a reopening of the same menu
183 * (i.e. mIconView did not change).
184 */
185 private boolean mConfigChanged;
186
187 /**
188 * Keeps track of whether the options menu is open. This is important in
189 * determining whether to show or hide the title bar overlay
190 */
191 private boolean mOptionsMenuOpen;
192
193 /**
194 * Whether or not the options menu is in its bigger, popup menu form. When
195 * true, we want the title bar overlay to be gone. When false, we do not.
196 * Only meaningful if mOptionsMenuOpen is true.
197 */
198 private boolean mExtendedMenuOpen;
199
200 private boolean mInLoad;
201
202 private boolean mActivityPaused = true;
203 private boolean mLoadStopped;
204
205 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500206 // Checks to see when the bookmarks database has changed, and updates the
207 // Tabs' notion of whether they represent bookmarked sites.
208 private ContentObserver mBookmarksObserver;
John Reck0ebd3ac2010-12-09 11:14:04 -0800209 private DataController mDataController;
John Reck847b5322011-04-14 17:02:18 -0700210 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700211
212 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
213 @Override
214 public Void doInBackground(File... files) {
215 if (files != null) {
216 for (File f : files) {
217 if (!f.delete()) {
218 Log.e(LOGTAG, f.getPath() + " was not deleted");
219 }
220 }
221 }
222 return null;
223 }
224 }
225
226 public Controller(Activity browser) {
227 mActivity = browser;
228 mSettings = BrowserSettings.getInstance();
John Reck0ebd3ac2010-12-09 11:14:04 -0800229 mDataController = DataController.getInstance(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700230 mTabControl = new TabControl(this);
231 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700232 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
Michael Kolb14612442011-06-24 13:06:29 -0700233 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700234
235 mUrlHandler = new UrlHandler(this);
236 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700237 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200238 mNfcHandler = new NfcHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700239
240 PowerManager pm = (PowerManager) mActivity
241 .getSystemService(Context.POWER_SERVICE);
242 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
243
244 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500245 mBookmarksObserver = new ContentObserver(mHandler) {
246 @Override
247 public void onChange(boolean selfChange) {
248 int size = mTabControl.getTabCount();
249 for (int i = 0; i < size; i++) {
250 mTabControl.getTab(i).updateBookmarkedStatus();
251 }
252 }
253
254 };
255 browser.getContentResolver().registerContentObserver(
256 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700257
258 mNetworkHandler = new NetworkStateHandler(mActivity, this);
259 // Start watching the default geolocation permissions
260 mSystemAllowGeolocationOrigins =
261 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
262 mSystemAllowGeolocationOrigins.start();
263
264 retainIconsOnStartup();
265 }
266
Patrick Scott7d50a932011-02-04 09:27:26 -0500267 void start(final Bundle icicle, final Intent intent) {
Guang Zhu9e78f512011-05-04 11:45:11 -0700268 boolean noCrashRecovery = intent.getBooleanExtra(NO_CRASH_RECOVERY, false);
269 if (icicle != null || noCrashRecovery) {
John Reck847b5322011-04-14 17:02:18 -0700270 doStart(icicle, intent);
271 } else {
272 mCrashRecoveryHandler.startRecovery(intent);
273 }
274 }
275
276 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 // Unless the last browser usage was within 24 hours, destroy any
278 // remaining incognito tabs.
279
280 Calendar lastActiveDate = icicle != null ?
281 (Calendar) icicle.getSerializable("lastActiveDate") : null;
282 Calendar today = Calendar.getInstance();
283 Calendar yesterday = Calendar.getInstance();
284 yesterday.add(Calendar.DATE, -1);
285
Patrick Scott7d50a932011-02-04 09:27:26 -0500286 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700287 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800288 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700289
Patrick Scott7d50a932011-02-04 09:27:26 -0500290 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700291 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500292 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000293
Michael Kolbc831b632011-05-11 09:30:34 -0700294 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500295 // Not able to restore so we go ahead and clear session cookies. We
296 // must do this before trying to login the user as we don't want to
297 // clear any session cookies set during login.
298 CookieManager.getInstance().removeSessionCookie();
299 }
300
Patrick Scottd43e75a2011-03-14 14:47:23 -0400301 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500302 new Runnable() {
303 @Override public void run() {
Michael Kolbc831b632011-05-11 09:30:34 -0700304 onPreloginFinished(icicle, intent, currentTabId, restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500305 }
306 });
307 }
308
Michael Kolbc831b632011-05-11 09:30:34 -0700309 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500310 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700311 if (currentTabId == -1) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700312 final Bundle extra = intent.getExtras();
313 // Create an initial tab.
314 // If the intent is ACTION_VIEW and data is not null, the Browser is
315 // invoked to view the content by another application. In this case,
316 // the tab will be close when exit.
Michael Kolb14612442011-06-24 13:06:29 -0700317 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
Michael Kolb7bcafde2011-05-09 13:55:59 -0700318 Tab t = null;
319 if (urlData.isEmpty()) {
320 t = openTabToHomePage();
321 } else {
322 t = openTab(urlData);
323 }
324 if (t != null) {
325 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
326 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700327 WebView webView = t.getWebView();
328 if (extra != null) {
329 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
330 if (scale > 0 && scale <= 1000) {
331 webView.setInitialScale(scale);
332 }
333 }
John Reckd8c74522011-06-14 08:45:00 -0700334 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700335 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700336 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500337 mUi.needsRestoreAllTabs());
Michael Kolb1bf23132010-11-19 12:55:12 -0800338 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700339 // TabControl.restoreState() will create a new tab even if
340 // restoring the state fails.
341 setActiveTab(mTabControl.getCurrentTab());
John Reckdb22ec42011-06-29 11:31:24 -0700342 // Handle the intent
343 mIntentHandler.onNewIntent(intent);
Michael Kolb8233fac2010-10-26 16:08:53 -0700344 }
345 // clear up the thumbnail directory, which is no longer used;
346 // ideally this should only be run once after an upgrade from
347 // a previous version of the browser
348 new ClearThumbnails().execute(mTabControl.getThumbnailDir()
349 .listFiles());
350 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700351 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700352 if (jsFlags.trim().length() != 0) {
353 getCurrentWebView().setJsFlags(jsFlags);
354 }
John Reck439c9a52010-12-14 10:04:39 -0800355 if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
356 bookmarksOrHistoryPicker(false);
357 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 }
359
Michael Kolb1514bb72010-11-22 09:11:48 -0800360 @Override
361 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700362 return mFactory;
363 }
364
365 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800366 public void onSetWebView(Tab tab, WebView view) {
367 mUi.onSetWebView(tab, view);
368 }
369
370 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800371 public void createSubWindow(Tab tab) {
372 endActionMode();
373 WebView mainView = tab.getWebView();
374 WebView subView = mFactory.createWebView((mainView == null)
375 ? false
376 : mainView.isPrivateBrowsingEnabled());
377 mUi.createSubWindow(tab, subView);
378 }
379
380 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700381 public Context getContext() {
382 return mActivity;
383 }
384
385 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700386 public Activity getActivity() {
387 return mActivity;
388 }
389
390 void setUi(UI ui) {
391 mUi = ui;
392 }
393
394 BrowserSettings getSettings() {
395 return mSettings;
396 }
397
398 IntentHandler getIntentHandler() {
399 return mIntentHandler;
400 }
401
402 @Override
403 public UI getUi() {
404 return mUi;
405 }
406
407 int getMaxTabs() {
408 return mActivity.getResources().getInteger(R.integer.max_tabs);
409 }
410
411 @Override
412 public TabControl getTabControl() {
413 return mTabControl;
414 }
415
Michael Kolb1bf23132010-11-19 12:55:12 -0800416 @Override
417 public List<Tab> getTabs() {
418 return mTabControl.getTabs();
419 }
420
Michael Kolb8233fac2010-10-26 16:08:53 -0700421 // Open the icon database and retain all the icons for visited sites.
Ben Murdoch9446b932010-11-25 16:20:14 +0000422 // This is done on a background thread so as not to stall startup.
Michael Kolb8233fac2010-10-26 16:08:53 -0700423 private void retainIconsOnStartup() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000424 // WebIconDatabase needs to be retrieved on the UI thread so that if
425 // it has not been created successfully yet the Handler is started on the
426 // UI thread.
427 new RetainIconsOnStartupTask(WebIconDatabase.getInstance()).execute();
428 }
429
430 private class RetainIconsOnStartupTask extends AsyncTask<Void, Void, Void> {
431 private WebIconDatabase mDb;
432
433 public RetainIconsOnStartupTask(WebIconDatabase db) {
434 mDb = db;
435 }
436
John Recka00cbbd2010-12-16 12:38:19 -0800437 @Override
Ben Murdoch9446b932010-11-25 16:20:14 +0000438 protected Void doInBackground(Void... unused) {
439 mDb.open(mActivity.getDir("icons", 0).getPath());
440 Cursor c = null;
441 try {
442 c = Browser.getAllBookmarks(mActivity.getContentResolver());
443 if (c.moveToFirst()) {
444 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
445 do {
446 String url = c.getString(urlIndex);
447 mDb.retainIconForPageUrl(url);
448 } while (c.moveToNext());
449 }
450 } catch (IllegalStateException e) {
451 Log.e(LOGTAG, "retainIconsOnStartup", e);
452 } finally {
453 if (c != null) c.close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700454 }
Ben Murdoch9446b932010-11-25 16:20:14 +0000455
456 return null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700457 }
458 }
459
460 private void startHandler() {
461 mHandler = new Handler() {
462
463 @Override
464 public void handleMessage(Message msg) {
465 switch (msg.what) {
466 case OPEN_BOOKMARKS:
467 bookmarksOrHistoryPicker(false);
468 break;
469 case FOCUS_NODE_HREF:
470 {
471 String url = (String) msg.getData().get("url");
472 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500473 String src = (String) msg.getData().get("src");
474 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700475 if (TextUtils.isEmpty(url)) {
476 break;
477 }
478 HashMap focusNodeMap = (HashMap) msg.obj;
479 WebView view = (WebView) focusNodeMap.get("webview");
480 // Only apply the action if the top window did not change.
481 if (getCurrentTopWebView() != view) {
482 break;
483 }
484 switch (msg.arg1) {
485 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700486 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700487 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500488 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700489 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500490 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500491 case R.id.open_newtab_context_menu_id:
492 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700493 openTab(url, parent,
494 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500495 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700496 case R.id.copy_link_context_menu_id:
497 copy(url);
498 break;
499 case R.id.save_link_context_menu_id:
500 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500501 DownloadHandler.onDownloadStartNoStream(
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000502 mActivity, url, null, null, null,
503 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 break;
505 }
506 break;
507 }
508
509 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700510 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 break;
512
513 case STOP_LOAD:
514 stopLoading();
515 break;
516
517 case RELEASE_WAKELOCK:
518 if (mWakeLock.isHeld()) {
519 mWakeLock.release();
520 // if we reach here, Browser should be still in the
521 // background loading after WAKELOCK_TIMEOUT (5-min).
522 // To avoid burning the battery, stop loading.
523 mTabControl.stopAllLoading();
524 }
525 break;
526
527 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800528 Tab tab = (Tab) msg.obj;
529 if (tab != null) {
530 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700531 }
532 break;
533 }
534 }
535 };
536
537 }
538
Martijn Coenenb2f93552011-06-14 10:48:35 +0200539
540 public Tab getCurrentTab() {
541 return mTabControl.getCurrentTab();
542 }
543
Michael Kolbba99c5d2010-11-29 14:57:41 -0800544 @Override
545 public void shareCurrentPage() {
546 shareCurrentPage(mTabControl.getCurrentTab());
547 }
548
549 private void shareCurrentPage(Tab tab) {
550 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800551 sharePage(mActivity, tab.getTitle(),
552 tab.getUrl(), tab.getFavicon(),
553 createScreenshot(tab.getWebView(),
554 getDesiredThumbnailWidth(mActivity),
555 getDesiredThumbnailHeight(mActivity)));
556 }
557 }
558
Michael Kolb8233fac2010-10-26 16:08:53 -0700559 /**
560 * Share a page, providing the title, url, favicon, and a screenshot. Uses
561 * an {@link Intent} to launch the Activity chooser.
562 * @param c Context used to launch a new Activity.
563 * @param title Title of the page. Stored in the Intent with
564 * {@link Intent#EXTRA_SUBJECT}
565 * @param url URL of the page. Stored in the Intent with
566 * {@link Intent#EXTRA_TEXT}
567 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
568 * with {@link Browser#EXTRA_SHARE_FAVICON}
569 * @param screenshot Bitmap of a screenshot of the page. Stored in the
570 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
571 */
572 static final void sharePage(Context c, String title, String url,
573 Bitmap favicon, Bitmap screenshot) {
574 Intent send = new Intent(Intent.ACTION_SEND);
575 send.setType("text/plain");
576 send.putExtra(Intent.EXTRA_TEXT, url);
577 send.putExtra(Intent.EXTRA_SUBJECT, title);
578 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
579 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
580 try {
581 c.startActivity(Intent.createChooser(send, c.getString(
582 R.string.choosertitle_sharevia)));
583 } catch(android.content.ActivityNotFoundException ex) {
584 // if no app handles it, do nothing
585 }
586 }
587
588 private void copy(CharSequence text) {
589 ClipboardManager cm = (ClipboardManager) mActivity
590 .getSystemService(Context.CLIPBOARD_SERVICE);
591 cm.setText(text);
592 }
593
594 // lifecycle
595
596 protected void onConfgurationChanged(Configuration config) {
597 mConfigChanged = true;
598 if (mPageDialogsHandler != null) {
599 mPageDialogsHandler.onConfigurationChanged(config);
600 }
601 mUi.onConfigurationChanged(config);
602 }
603
604 @Override
605 public void handleNewIntent(Intent intent) {
606 mIntentHandler.onNewIntent(intent);
607 }
608
609 protected void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800610 if (mUi.isCustomViewShowing()) {
611 hideCustomView();
612 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700613 if (mActivityPaused) {
614 Log.e(LOGTAG, "BrowserActivity is already paused.");
615 return;
616 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700617 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800618 Tab tab = mTabControl.getCurrentTab();
619 if (tab != null) {
620 tab.pause();
621 if (!pauseWebViewTimers(tab)) {
622 mWakeLock.acquire();
623 mHandler.sendMessageDelayed(mHandler
624 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
625 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 }
627 mUi.onPause();
628 mNetworkHandler.onPause();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200629 mNfcHandler.onPause();
Michael Kolb8233fac2010-10-26 16:08:53 -0700630
631 WebView.disablePlatformNotifications();
John Reck378a4102011-06-09 16:23:01 -0700632 mCrashRecoveryHandler.backupState();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200633
Michael Kolb8233fac2010-10-26 16:08:53 -0700634 }
635
John Reckaed9c542011-05-27 16:08:53 -0700636 void onSaveInstanceState(Bundle outState, boolean saveImages) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700637 // the default implementation requires each view to have an id. As the
638 // browser handles the state itself and it doesn't use id for the views,
639 // don't call the default implementation. Otherwise it will trigger the
640 // warning like this, "couldn't save which view has focus because the
641 // focused view XXX has no id".
642
643 // Save all the tabs
John Reckaed9c542011-05-27 16:08:53 -0700644 mTabControl.saveState(outState, saveImages);
John Reck24f18262011-06-17 14:47:20 -0700645 if (!outState.isEmpty()) {
646 // Save time so that we know how old incognito tabs (if any) are.
647 outState.putSerializable("lastActiveDate", Calendar.getInstance());
648 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700649 }
650
651 void onResume() {
652 if (!mActivityPaused) {
653 Log.e(LOGTAG, "BrowserActivity is already resumed.");
654 return;
655 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700656 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800657 Tab current = mTabControl.getCurrentTab();
658 if (current != null) {
659 current.resume();
660 resumeWebViewTimers(current);
661 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700662 if (mWakeLock.isHeld()) {
663 mHandler.removeMessages(RELEASE_WAKELOCK);
664 mWakeLock.release();
665 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200666
Michael Kolb8233fac2010-10-26 16:08:53 -0700667 mUi.onResume();
668 mNetworkHandler.onResume();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200669 mNfcHandler.onResume();
Michael Kolb8233fac2010-10-26 16:08:53 -0700670 WebView.enablePlatformNotifications();
671 }
672
Michael Kolb70976932010-11-30 11:34:01 -0800673 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800674 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800675 * @param tab guaranteed non-null
676 */
677 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 boolean inLoad = tab.inPageLoad();
679 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
680 CookieSyncManager.getInstance().startSync();
681 WebView w = tab.getWebView();
682 if (w != null) {
683 w.resumeTimers();
684 }
685 }
686 }
687
Michael Kolb70976932010-11-30 11:34:01 -0800688 /**
689 * Pause all WebView timers using the WebView of the given tab
690 * @param tab
691 * @return true if the timers are paused or tab is null
692 */
693 private boolean pauseWebViewTimers(Tab tab) {
694 if (tab == null) {
695 return true;
696 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 CookieSyncManager.getInstance().stopSync();
698 WebView w = getCurrentWebView();
699 if (w != null) {
700 w.pauseTimers();
701 }
702 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 }
Michael Kolb70976932010-11-30 11:34:01 -0800704 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 }
706
707 void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800708 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700709 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
710 mUploadHandler = null;
711 }
712 if (mTabControl == null) return;
713 mUi.onDestroy();
714 // Remove the current tab and sub window
715 Tab t = mTabControl.getCurrentTab();
716 if (t != null) {
717 dismissSubWindow(t);
718 removeTab(t);
719 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500720 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700721 // Destroy all the tabs
722 mTabControl.destroy();
723 WebIconDatabase.getInstance().close();
724 // Stop watching the default geolocation permissions
725 mSystemAllowGeolocationOrigins.stop();
726 mSystemAllowGeolocationOrigins = null;
727 }
728
729 protected boolean isActivityPaused() {
730 return mActivityPaused;
731 }
732
733 protected void onLowMemory() {
734 mTabControl.freeMemory();
735 }
736
737 @Override
738 public boolean shouldShowErrorConsole() {
739 return mShouldShowErrorConsole;
740 }
741
742 protected void setShouldShowErrorConsole(boolean show) {
743 if (show == mShouldShowErrorConsole) {
744 // Nothing to do.
745 return;
746 }
747 mShouldShowErrorConsole = show;
748 Tab t = mTabControl.getCurrentTab();
749 if (t == null) {
750 // There is no current tab so we cannot toggle the error console
751 return;
752 }
753 mUi.setShouldShowErrorConsole(t, show);
754 }
755
756 @Override
757 public void stopLoading() {
758 mLoadStopped = true;
759 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700760 WebView w = getCurrentTopWebView();
761 w.stopLoading();
Michael Kolb8233fac2010-10-26 16:08:53 -0700762 mUi.onPageStopped(tab);
763 }
764
765 boolean didUserStopLoading() {
766 return mLoadStopped;
767 }
768
769 // WebViewController
770
771 @Override
John Reck324d4402011-01-11 16:56:42 -0800772 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700773
774 // We've started to load a new page. If there was a pending message
775 // to save a screenshot then we will now take the new page and save
776 // an incorrect screenshot. Therefore, remove any pending thumbnail
777 // messages from the queue.
778 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800779 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700780
781 // reset sync timer to avoid sync starts during loading a page
782 CookieSyncManager.getInstance().resetSync();
783
784 if (!mNetworkHandler.isNetworkUp()) {
785 view.setNetworkAvailable(false);
786 }
787
788 // when BrowserActivity just starts, onPageStarted may be called before
789 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
790 // to start the timer. As we won't switch tabs while an activity is in
791 // pause state, we can ensure calling resume and pause in pair.
792 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800793 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700794 }
795 mLoadStopped = false;
796 if (!mNetworkHandler.isNetworkUp()) {
797 mNetworkHandler.createAndShowNetworkDialog();
798 }
799 endActionMode();
800
John Reck30c714c2010-12-16 17:30:34 -0800801 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700802
John Reck324d4402011-01-11 16:56:42 -0800803 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700804 // update the bookmark database for favicon
805 maybeUpdateFavicon(tab, null, url, favicon);
806
807 Performance.tracePageStart(url);
808
809 // Performance probe
810 if (false) {
811 Performance.onPageStarted();
812 }
813
814 }
815
816 @Override
John Reck324d4402011-01-11 16:56:42 -0800817 public void onPageFinished(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800818 mUi.onTabDataChanged(tab);
John Reck324d4402011-01-11 16:56:42 -0800819 if (!tab.isPrivateBrowsingEnabled()
John Reckd8c74522011-06-14 08:45:00 -0700820 && !TextUtils.isEmpty(tab.getUrl())
821 && !tab.isSnapshot()) {
John Recka1696282011-07-08 14:10:37 -0700822 // Only update the bookmark screenshot if the user did not
823 // cancel the load early and there is not already
824 // a pending update for the tab.
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 if (tab.inForeground() && !didUserStopLoading()
826 || !tab.inForeground()) {
John Recka1696282011-07-08 14:10:37 -0700827 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
828 mHandler.sendMessageDelayed(mHandler.obtainMessage(
829 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
830 500);
831 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700832 }
833 }
834 // pause the WebView timer and release the wake lock if it is finished
835 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800836 if (mActivityPaused && pauseWebViewTimers(tab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700837 if (mWakeLock.isHeld()) {
838 mHandler.removeMessages(RELEASE_WAKELOCK);
839 mWakeLock.release();
840 }
841 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200842
Michael Kolb8233fac2010-10-26 16:08:53 -0700843 // Performance probe
844 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800845 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700846 }
847
848 Performance.tracePageFinished();
849 }
850
851 @Override
John Reck30c714c2010-12-16 17:30:34 -0800852 public void onProgressChanged(Tab tab) {
853 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700854
855 if (newProgress == 100) {
856 CookieSyncManager.getInstance().sync();
857 // onProgressChanged() may continue to be called after the main
858 // frame has finished loading, as any remaining sub frames continue
859 // to load. We'll only get called once though with newProgress as
860 // 100 when everything is loaded. (onPageFinished is called once
861 // when the main frame completes loading regardless of the state of
862 // any sub frames so calls to onProgressChanges may continue after
863 // onPageFinished has executed)
864 if (mInLoad) {
865 mInLoad = false;
866 updateInLoadMenuItems(mCachedMenu);
867 }
868 } else {
869 if (!mInLoad) {
870 // onPageFinished may have already been called but a subframe is
871 // still loading and updating the progress. Reset mInLoad and
872 // update the menu items.
873 mInLoad = true;
874 updateInLoadMenuItems(mCachedMenu);
875 }
876 }
John Reck30c714c2010-12-16 17:30:34 -0800877 mUi.onProgressChanged(tab);
878 }
879
880 @Override
881 public void onUpdatedLockIcon(Tab tab) {
882 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 }
884
885 @Override
886 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800887 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800888 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800889 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700890 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
891 return;
892 }
893 // Update the title in the history database if not in private browsing mode
894 if (!tab.isPrivateBrowsingEnabled()) {
John Reck0ebd3ac2010-12-09 11:14:04 -0800895 mDataController.updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700896 }
897 }
898
899 @Override
900 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800901 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700902 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
903 }
904
905 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800906 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
907 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700908 }
909
910 @Override
911 public boolean shouldOverrideKeyEvent(KeyEvent event) {
912 if (mMenuIsDown) {
913 // only check shortcut key when MENU is held
914 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
915 event);
916 } else {
917 return false;
918 }
919 }
920
921 @Override
922 public void onUnhandledKeyEvent(KeyEvent event) {
923 if (!isActivityPaused()) {
924 if (event.getAction() == KeyEvent.ACTION_DOWN) {
925 mActivity.onKeyDown(event.getKeyCode(), event);
926 } else {
927 mActivity.onKeyUp(event.getKeyCode(), event);
928 }
929 }
930 }
931
932 @Override
John Reck324d4402011-01-11 16:56:42 -0800933 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700934 // Don't save anything in private browsing mode
935 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800936 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700937
John Reck324d4402011-01-11 16:56:42 -0800938 if (TextUtils.isEmpty(url)
939 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700940 return;
941 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800942 mDataController.updateVisitedHistory(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700943 WebIconDatabase.getInstance().retainIconForPageUrl(url);
John Reck847b5322011-04-14 17:02:18 -0700944 if (!mActivityPaused) {
945 // Since we clear the state in onPause, don't backup the current
946 // state if we are already paused
947 mCrashRecoveryHandler.backupState();
948 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700949 }
950
951 @Override
952 public void getVisitedHistory(final ValueCallback<String[]> callback) {
953 AsyncTask<Void, Void, String[]> task =
954 new AsyncTask<Void, Void, String[]>() {
955 @Override
956 public String[] doInBackground(Void... unused) {
957 return Browser.getVisitedHistory(mActivity.getContentResolver());
958 }
959 @Override
960 public void onPostExecute(String[] result) {
961 callback.onReceiveValue(result);
962 }
963 };
964 task.execute();
965 }
966
967 @Override
968 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
969 final HttpAuthHandler handler, final String host,
970 final String realm) {
971 String username = null;
972 String password = null;
973
974 boolean reuseHttpAuthUsernamePassword
975 = handler.useHttpAuthUsernamePassword();
976
977 if (reuseHttpAuthUsernamePassword && view != null) {
978 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
979 if (credentials != null && credentials.length == 2) {
980 username = credentials[0];
981 password = credentials[1];
982 }
983 }
984
985 if (username != null && password != null) {
986 handler.proceed(username, password);
987 } else {
988 if (tab.inForeground()) {
989 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
990 } else {
991 handler.cancel();
992 }
993 }
994 }
995
996 @Override
997 public void onDownloadStart(Tab tab, String url, String userAgent,
998 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000999 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -05001000 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001001 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
1002 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001003 // This Tab was opened for the sole purpose of downloading a
1004 // file. Remove it.
1005 if (tab == mTabControl.getCurrentTab()) {
1006 // In this case, the Tab is still on top.
1007 goBackOnePageOrQuit();
1008 } else {
1009 // In this case, it is not.
1010 closeTab(tab);
1011 }
1012 }
1013 }
1014
1015 @Override
1016 public Bitmap getDefaultVideoPoster() {
1017 return mUi.getDefaultVideoPoster();
1018 }
1019
1020 @Override
1021 public View getVideoLoadingProgressView() {
1022 return mUi.getVideoLoadingProgressView();
1023 }
1024
1025 @Override
1026 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1027 SslError error) {
1028 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1029 }
1030
Patrick Scott92066772011-03-10 08:46:27 -05001031 @Override
1032 public void showAutoLogin(Tab tab) {
1033 assert tab.inForeground();
1034 // Update the title bar to show the auto-login request.
1035 mUi.showAutoLogin(tab);
1036 }
1037
1038 @Override
1039 public void hideAutoLogin(Tab tab) {
1040 assert tab.inForeground();
1041 mUi.hideAutoLogin(tab);
1042 }
1043
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 // helper method
1045
1046 /*
1047 * Update the favorites icon if the private browsing isn't enabled and the
1048 * icon is valid.
1049 */
1050 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1051 final String url, Bitmap favicon) {
1052 if (favicon == null) {
1053 return;
1054 }
1055 if (!tab.isPrivateBrowsingEnabled()) {
1056 Bookmarks.updateFavicon(mActivity
1057 .getContentResolver(), originalUrl, url, favicon);
1058 }
1059 }
1060
Leon Scroggins4cd97792010-12-03 15:31:56 -05001061 @Override
1062 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001063 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001064 mUi.bookmarkedStatusHasChanged(tab);
1065 }
1066
Michael Kolb8233fac2010-10-26 16:08:53 -07001067 // end WebViewController
1068
1069 protected void pageUp() {
1070 getCurrentTopWebView().pageUp(false);
1071 }
1072
1073 protected void pageDown() {
1074 getCurrentTopWebView().pageDown(false);
1075 }
1076
1077 // callback from phone title bar
1078 public void editUrl() {
1079 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001080 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001081 }
1082
Michael Kolbcfa3af52010-12-14 10:36:11 -08001083 public void startVoiceSearch() {
1084 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1085 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1086 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1087 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1088 mActivity.getComponentName().flattenToString());
1089 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001090 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001091 mActivity.startActivity(intent);
1092 }
1093
Michael Kolb11d19782011-03-20 10:17:40 -07001094 @Override
1095 public void activateVoiceSearchMode(String title, List<String> results) {
1096 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001097 }
1098
1099 public void revertVoiceSearchMode(Tab tab) {
1100 mUi.revertVoiceTitleBar(tab);
1101 }
1102
Michael Kolb736990c2011-03-20 10:01:20 -07001103 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001104 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001105 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1106 }
1107
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001108 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001109 WebChromeClient.CustomViewCallback callback) {
1110 if (tab.inForeground()) {
1111 if (mUi.isCustomViewShowing()) {
1112 callback.onCustomViewHidden();
1113 return;
1114 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001115 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001116 // Save the menu state and set it to empty while the custom
1117 // view is showing.
1118 mOldMenuState = mMenuState;
1119 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001120 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001121 }
1122 }
1123
1124 @Override
1125 public void hideCustomView() {
1126 if (mUi.isCustomViewShowing()) {
1127 mUi.onHideCustomView();
1128 // Reset the old menu state.
1129 mMenuState = mOldMenuState;
1130 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001131 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001132 }
1133 }
1134
1135 protected void onActivityResult(int requestCode, int resultCode,
1136 Intent intent) {
1137 if (getCurrentTopWebView() == null) return;
1138 switch (requestCode) {
1139 case PREFERENCES_PAGE:
1140 if (resultCode == Activity.RESULT_OK && intent != null) {
1141 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001142 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001143 mTabControl.removeParentChildRelationShips();
1144 }
1145 }
1146 break;
1147 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001148 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001149 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001150 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001151 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001152 case AUTOFILL_SETUP:
1153 // Determine whether a profile was actually set up or not
1154 // and if so, send the message back to the WebTextView to
1155 // fill the form with the new profile.
1156 if (getSettings().getAutoFillProfile() != null) {
1157 mAutoFillSetupMessage.sendToTarget();
1158 mAutoFillSetupMessage = null;
1159 }
1160 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 default:
1162 break;
1163 }
1164 getCurrentTopWebView().requestFocus();
1165 }
1166
1167 /**
1168 * Open the Go page.
1169 * @param startWithHistory If true, open starting on the history tab.
1170 * Otherwise, start with the bookmarks tab.
1171 */
1172 @Override
1173 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1174 if (mTabControl.getCurrentWebView() == null) {
1175 return;
1176 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001177 // clear action mode
1178 if (isInCustomActionMode()) {
1179 endActionMode();
1180 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001181 Bundle extras = new Bundle();
1182 // Disable opening in a new window if we have maxed out the windows
1183 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1184 !mTabControl.canCreateNewTab());
John Reck2bc80422011-06-30 15:11:49 -07001185 mUi.showComboView(startWithHistory
1186 ? ComboViews.History : ComboViews.Bookmarks, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001187 }
1188
1189 // combo view callbacks
1190
1191 /**
1192 * callback from ComboPage when clear history is requested
1193 */
1194 public void onRemoveParentChildRelationships() {
1195 mTabControl.removeParentChildRelationShips();
1196 }
1197
1198 /**
1199 * callback from ComboPage when bookmark/history selection
1200 */
1201 @Override
1202 public void onUrlSelected(String url, boolean newTab) {
1203 removeComboView();
1204 if (!TextUtils.isEmpty(url)) {
1205 if (newTab) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07001206 final Tab parent = mTabControl.getCurrentTab();
1207 openTab(url,
1208 (parent != null) && parent.isPrivateBrowsingEnabled(),
1209 !mSettings.openInBackground(),
1210 true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001211 } else {
1212 final Tab currentTab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001213 loadUrl(currentTab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001214 }
1215 }
1216 }
1217
1218 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001219 * dismiss the ComboPage
1220 */
1221 @Override
1222 public void removeComboView() {
1223 mUi.hideComboView();
1224 }
1225
1226 // active tabs page handling
1227
1228 protected void showActiveTabsPage() {
1229 mMenuState = EMPTY_MENU;
1230 mUi.showActiveTabsPage();
1231 }
1232
1233 /**
1234 * Remove the active tabs page.
1235 * @param needToAttach If true, the active tabs page did not attach a tab
1236 * to the content view, so we need to do that here.
1237 */
1238 @Override
1239 public void removeActiveTabsPage(boolean needToAttach) {
1240 mMenuState = R.id.MAIN_MENU;
1241 mUi.removeActiveTabsPage();
1242 if (needToAttach) {
1243 setActiveTab(mTabControl.getCurrentTab());
1244 }
1245 getCurrentTopWebView().requestFocus();
1246 }
1247
1248 // key handling
1249 protected void onBackKey() {
1250 if (!mUi.onBackKey()) {
1251 WebView subwindow = mTabControl.getCurrentSubWindow();
1252 if (subwindow != null) {
1253 if (subwindow.canGoBack()) {
1254 subwindow.goBack();
1255 } else {
1256 dismissSubWindow(mTabControl.getCurrentTab());
1257 }
1258 } else {
1259 goBackOnePageOrQuit();
1260 }
1261 }
1262 }
1263
Michael Kolb4bd767d2011-05-27 11:33:55 -07001264 protected boolean onMenuKey() {
1265 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001266 }
1267
Michael Kolb8233fac2010-10-26 16:08:53 -07001268 // menu handling and state
1269 // TODO: maybe put into separate handler
1270
1271 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckb3417f02011-01-14 11:01:05 -08001272 if (mOptionsMenuHandler != null) {
1273 return mOptionsMenuHandler.onCreateOptionsMenu(menu);
1274 }
1275
John Reckd73c5a22010-12-22 10:22:50 -08001276 if (mMenuState == EMPTY_MENU) {
1277 return false;
1278 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001279 MenuInflater inflater = mActivity.getMenuInflater();
1280 inflater.inflate(R.menu.browser, menu);
1281 updateInLoadMenuItems(menu);
1282 // hold on to the menu reference here; it is used by the page callbacks
1283 // to update the menu based on loading state
1284 mCachedMenu = menu;
1285 return true;
1286 }
1287
1288 protected void onCreateContextMenu(ContextMenu menu, View v,
1289 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001290 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001291 return;
1292 }
1293 if (!(v instanceof WebView)) {
1294 return;
1295 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001296 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001297 WebView.HitTestResult result = webview.getHitTestResult();
1298 if (result == null) {
1299 return;
1300 }
1301
1302 int type = result.getType();
1303 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1304 Log.w(LOGTAG,
1305 "We should not show context menu when nothing is touched");
1306 return;
1307 }
1308 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1309 // let TextView handles context menu
1310 return;
1311 }
1312
1313 // Note, http://b/issue?id=1106666 is requesting that
1314 // an inflated menu can be used again. This is not available
1315 // yet, so inflate each time (yuk!)
1316 MenuInflater inflater = mActivity.getMenuInflater();
1317 inflater.inflate(R.menu.browsercontext, menu);
1318
1319 // Show the correct menu group
1320 final String extra = result.getExtra();
1321 menu.setGroupVisible(R.id.PHONE_MENU,
1322 type == WebView.HitTestResult.PHONE_TYPE);
1323 menu.setGroupVisible(R.id.EMAIL_MENU,
1324 type == WebView.HitTestResult.EMAIL_TYPE);
1325 menu.setGroupVisible(R.id.GEO_MENU,
1326 type == WebView.HitTestResult.GEO_TYPE);
1327 menu.setGroupVisible(R.id.IMAGE_MENU,
1328 type == WebView.HitTestResult.IMAGE_TYPE
1329 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1330 menu.setGroupVisible(R.id.ANCHOR_MENU,
1331 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1332 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001333 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1334 || type == WebView.HitTestResult.PHONE_TYPE
1335 || type == WebView.HitTestResult.EMAIL_TYPE
1336 || type == WebView.HitTestResult.GEO_TYPE;
1337 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1338 if (hitText) {
1339 menu.findItem(R.id.select_text_menu_id)
1340 .setOnMenuItemClickListener(new SelectText(webview));
1341 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001342 // Setup custom handling depending on the type
1343 switch (type) {
1344 case WebView.HitTestResult.PHONE_TYPE:
1345 menu.setHeaderTitle(Uri.decode(extra));
1346 menu.findItem(R.id.dial_context_menu_id).setIntent(
1347 new Intent(Intent.ACTION_VIEW, Uri
1348 .parse(WebView.SCHEME_TEL + extra)));
1349 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1350 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1351 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1352 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1353 addIntent);
1354 menu.findItem(R.id.copy_phone_context_menu_id)
1355 .setOnMenuItemClickListener(
1356 new Copy(extra));
1357 break;
1358
1359 case WebView.HitTestResult.EMAIL_TYPE:
1360 menu.setHeaderTitle(extra);
1361 menu.findItem(R.id.email_context_menu_id).setIntent(
1362 new Intent(Intent.ACTION_VIEW, Uri
1363 .parse(WebView.SCHEME_MAILTO + extra)));
1364 menu.findItem(R.id.copy_mail_context_menu_id)
1365 .setOnMenuItemClickListener(
1366 new Copy(extra));
1367 break;
1368
1369 case WebView.HitTestResult.GEO_TYPE:
1370 menu.setHeaderTitle(extra);
1371 menu.findItem(R.id.map_context_menu_id).setIntent(
1372 new Intent(Intent.ACTION_VIEW, Uri
1373 .parse(WebView.SCHEME_GEO
1374 + URLEncoder.encode(extra))));
1375 menu.findItem(R.id.copy_geo_context_menu_id)
1376 .setOnMenuItemClickListener(
1377 new Copy(extra));
1378 break;
1379
1380 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1381 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001382 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001383 // decide whether to show the open link in new tab option
1384 boolean showNewTab = mTabControl.canCreateNewTab();
1385 MenuItem newTabItem
1386 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001387 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001388 ? R.string.contextmenu_openlink_newwindow_background
1389 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001390 newTabItem.setVisible(showNewTab);
1391 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001392 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1393 newTabItem.setOnMenuItemClickListener(
1394 new MenuItem.OnMenuItemClickListener() {
1395 @Override
1396 public boolean onMenuItemClick(MenuItem item) {
1397 final HashMap<String, WebView> hrefMap =
1398 new HashMap<String, WebView>();
1399 hrefMap.put("webview", webview);
1400 final Message msg = mHandler.obtainMessage(
1401 FOCUS_NODE_HREF,
1402 R.id.open_newtab_context_menu_id,
1403 0, hrefMap);
1404 webview.requestFocusNodeHref(msg);
1405 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001406 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001407 });
1408 } else {
1409 newTabItem.setOnMenuItemClickListener(
1410 new MenuItem.OnMenuItemClickListener() {
1411 @Override
1412 public boolean onMenuItemClick(MenuItem item) {
1413 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001414 openTab(extra, parent,
1415 !mSettings.openInBackground(),
1416 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001417 return true;
1418 }
1419 });
1420 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001421 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001422 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1423 break;
1424 }
1425 // otherwise fall through to handle image part
1426 case WebView.HitTestResult.IMAGE_TYPE:
1427 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1428 menu.setHeaderTitle(extra);
1429 }
1430 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1431 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1432 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001433 setOnMenuItemClickListener(
1434 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001435 menu.findItem(R.id.set_wallpaper_context_menu_id).
1436 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1437 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001438 break;
1439
1440 default:
1441 Log.w(LOGTAG, "We should not get here.");
1442 break;
1443 }
1444 //update the ui
1445 mUi.onContextMenuCreated(menu);
1446 }
1447
1448 /**
1449 * As the menu can be open when loading state changes
1450 * we must manually update the state of the stop/reload menu
1451 * item
1452 */
1453 private void updateInLoadMenuItems(Menu menu) {
1454 if (menu == null) {
1455 return;
1456 }
1457 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1458 MenuItem src = mInLoad ?
1459 menu.findItem(R.id.stop_menu_id):
1460 menu.findItem(R.id.reload_menu_id);
1461 if (src != null) {
1462 dest.setIcon(src.getIcon());
1463 dest.setTitle(src.getTitle());
1464 }
1465 }
1466
John Reckb3417f02011-01-14 11:01:05 -08001467 boolean onPrepareOptionsMenu(Menu menu) {
1468 if (mOptionsMenuHandler != null) {
1469 return mOptionsMenuHandler.onPrepareOptionsMenu(menu);
1470 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001471 // Note: setVisible will decide whether an item is visible; while
1472 // setEnabled() will decide whether an item is enabled, which also means
1473 // whether the matching shortcut key will function.
1474 switch (mMenuState) {
1475 case EMPTY_MENU:
1476 if (mCurrentMenuState != mMenuState) {
1477 menu.setGroupVisible(R.id.MAIN_MENU, false);
1478 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1479 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1480 }
1481 break;
1482 default:
1483 if (mCurrentMenuState != mMenuState) {
1484 menu.setGroupVisible(R.id.MAIN_MENU, true);
1485 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1486 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1487 }
1488 final WebView w = getCurrentTopWebView();
1489 boolean canGoBack = false;
1490 boolean canGoForward = false;
1491 boolean isHome = false;
1492 if (w != null) {
1493 canGoBack = w.canGoBack();
1494 canGoForward = w.canGoForward();
1495 isHome = mSettings.getHomePage().equals(w.getUrl());
1496 }
1497 final MenuItem back = menu.findItem(R.id.back_menu_id);
1498 back.setEnabled(canGoBack);
1499
1500 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1501 home.setEnabled(!isHome);
1502
1503 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1504 forward.setEnabled(canGoForward);
1505
1506 // decide whether to show the share link option
1507 PackageManager pm = mActivity.getPackageManager();
1508 Intent send = new Intent(Intent.ACTION_SEND);
1509 send.setType("text/plain");
1510 ResolveInfo ri = pm.resolveActivity(send,
1511 PackageManager.MATCH_DEFAULT_ONLY);
1512 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1513
John Reck35e9dd62011-04-25 09:01:54 -07001514 boolean isNavDump = mSettings.enableNavDump();
Michael Kolb8233fac2010-10-26 16:08:53 -07001515 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1516 nav.setVisible(isNavDump);
1517 nav.setEnabled(isNavDump);
1518
John Reck35e9dd62011-04-25 09:01:54 -07001519 boolean showDebugSettings = mSettings.isDebugEnabled();
Michael Kolb8233fac2010-10-26 16:08:53 -07001520 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1521 counter.setVisible(showDebugSettings);
1522 counter.setEnabled(showDebugSettings);
1523
John Reckb3417f02011-01-14 11:01:05 -08001524 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
1525 newtab.setEnabled(getTabControl().canCreateNewTab());
Michael Kolb8233fac2010-10-26 16:08:53 -07001526
John Reck2bc80422011-06-30 15:11:49 -07001527 MenuItem saveSnapshot = menu.findItem(R.id.save_snapshot_menu_id);
1528 Tab tab = getCurrentTab();
1529 saveSnapshot.setVisible(tab != null && !tab.isSnapshot());
Michael Kolb8233fac2010-10-26 16:08:53 -07001530 break;
1531 }
1532 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001533 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001534 }
1535
1536 public boolean onOptionsItemSelected(MenuItem item) {
John Reckb3417f02011-01-14 11:01:05 -08001537 if (mOptionsMenuHandler != null &&
1538 mOptionsMenuHandler.onOptionsItemSelected(item)) {
1539 return true;
1540 }
1541
Michael Kolb8233fac2010-10-26 16:08:53 -07001542 if (item.getGroupId() != R.id.CONTEXT_MENU) {
1543 // menu remains active, so ensure comboview is dismissed
1544 // if main menu option is selected
1545 removeComboView();
1546 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001547 if (null == getCurrentTopWebView()) {
1548 return false;
1549 }
1550 if (mMenuIsDown) {
1551 // The shortcut action consumes the MENU. Even if it is still down,
1552 // it won't trigger the next shortcut action. In the case of the
1553 // shortcut action triggering a new activity, like Bookmarks, we
1554 // won't get onKeyUp for MENU. So it is important to reset it here.
1555 mMenuIsDown = false;
1556 }
1557 switch (item.getItemId()) {
1558 // -- Main menu
1559 case R.id.new_tab_menu_id:
1560 openTabToHomePage();
1561 break;
1562
1563 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001564 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001565 break;
1566
1567 case R.id.goto_menu_id:
1568 editUrl();
1569 break;
1570
1571 case R.id.bookmarks_menu_id:
1572 bookmarksOrHistoryPicker(false);
1573 break;
1574
1575 case R.id.active_tabs_menu_id:
1576 showActiveTabsPage();
1577 break;
1578
1579 case R.id.add_bookmark_menu_id:
John Reck3ffc5ca2011-06-10 15:56:06 -07001580 bookmarkCurrentPage(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 break;
1582
1583 case R.id.stop_reload_menu_id:
1584 if (mInLoad) {
1585 stopLoading();
1586 } else {
1587 getCurrentTopWebView().reload();
1588 }
1589 break;
1590
1591 case R.id.back_menu_id:
1592 getCurrentTopWebView().goBack();
1593 break;
1594
1595 case R.id.forward_menu_id:
1596 getCurrentTopWebView().goForward();
1597 break;
1598
1599 case R.id.close_menu_id:
1600 // Close the subwindow if it exists.
1601 if (mTabControl.getCurrentSubWindow() != null) {
1602 dismissSubWindow(mTabControl.getCurrentTab());
1603 break;
1604 }
1605 closeCurrentTab();
1606 break;
1607
1608 case R.id.homepage_menu_id:
1609 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001610 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001611 break;
1612
1613 case R.id.preferences_menu_id:
1614 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1615 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1616 getCurrentTopWebView().getUrl());
1617 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1618 break;
1619
1620 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001621 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001622 break;
1623
John Reck2bc80422011-06-30 15:11:49 -07001624 case R.id.save_snapshot_menu_id:
1625 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001626 if (source == null) break;
John Reckd8c74522011-06-14 08:45:00 -07001627 final ContentResolver cr = mActivity.getContentResolver();
1628 final ContentValues values = source.createSnapshotValues();
John Reck2bc80422011-06-30 15:11:49 -07001629 if (values != null) {
1630 new AsyncTask<Tab, Void, Long>() {
John Reckd8c74522011-06-14 08:45:00 -07001631
John Reck2bc80422011-06-30 15:11:49 -07001632 @Override
1633 protected Long doInBackground(Tab... params) {
1634 Uri result = cr.insert(Snapshots.CONTENT_URI, values);
1635 long id = ContentUris.parseId(result);
1636 return id;
John Reckd8c74522011-06-14 08:45:00 -07001637 }
John Reckf33b1632011-06-04 20:00:23 -07001638
John Reck2bc80422011-06-30 15:11:49 -07001639 @Override
1640 protected void onPostExecute(Long id) {
1641 Bundle b = new Bundle();
1642 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1643 mUi.showComboView(ComboViews.Snapshots, b);
1644 };
1645 }.execute(source);
1646 } else {
1647 Toast.makeText(mActivity, R.string.snapshot_failed,
Leon Scrogginsac993842011-02-02 12:54:07 -05001648 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001649 }
Leon Scrogginsac993842011-02-02 12:54:07 -05001650 break;
1651
Michael Kolb8233fac2010-10-26 16:08:53 -07001652 case R.id.page_info_menu_id:
Huahui Wuae0c0412011-06-28 10:17:05 -07001653 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
Michael Kolb8233fac2010-10-26 16:08:53 -07001654 break;
1655
1656 case R.id.classic_history_menu_id:
1657 bookmarksOrHistoryPicker(true);
1658 break;
1659
1660 case R.id.title_bar_share_page_url:
1661 case R.id.share_page_menu_id:
1662 Tab currentTab = mTabControl.getCurrentTab();
1663 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001664 return false;
1665 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001666 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001667 break;
1668
1669 case R.id.dump_nav_menu_id:
1670 getCurrentTopWebView().debugDump();
1671 break;
1672
1673 case R.id.dump_counters_menu_id:
1674 getCurrentTopWebView().dumpV8Counters();
1675 break;
1676
1677 case R.id.zoom_in_menu_id:
1678 getCurrentTopWebView().zoomIn();
1679 break;
1680
1681 case R.id.zoom_out_menu_id:
1682 getCurrentTopWebView().zoomOut();
1683 break;
1684
1685 case R.id.view_downloads_menu_id:
1686 viewDownloads();
1687 break;
1688
1689 case R.id.window_one_menu_id:
1690 case R.id.window_two_menu_id:
1691 case R.id.window_three_menu_id:
1692 case R.id.window_four_menu_id:
1693 case R.id.window_five_menu_id:
1694 case R.id.window_six_menu_id:
1695 case R.id.window_seven_menu_id:
1696 case R.id.window_eight_menu_id:
1697 {
1698 int menuid = item.getItemId();
1699 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1700 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1701 Tab desiredTab = mTabControl.getTab(id);
1702 if (desiredTab != null &&
1703 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001704 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001705 }
1706 break;
1707 }
1708 }
1709 }
1710 break;
1711
1712 default:
1713 return false;
1714 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001715 return true;
1716 }
1717
1718 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001719 // Let the History and Bookmark fragments handle menus they created.
1720 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1721 return false;
1722 }
1723
Michael Kolb8233fac2010-10-26 16:08:53 -07001724 int id = item.getItemId();
1725 boolean result = true;
1726 switch (id) {
1727 // For the context menu from the title bar
1728 case R.id.title_bar_copy_page_url:
1729 Tab currentTab = mTabControl.getCurrentTab();
1730 if (null == currentTab) {
1731 result = false;
1732 break;
1733 }
1734 WebView mainView = currentTab.getWebView();
1735 if (null == mainView) {
1736 result = false;
1737 break;
1738 }
1739 copy(mainView.getUrl());
1740 break;
1741 // -- Browser context menu
1742 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001743 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001744 case R.id.copy_link_context_menu_id:
1745 final WebView webView = getCurrentTopWebView();
1746 if (null == webView) {
1747 result = false;
1748 break;
1749 }
1750 final HashMap<String, WebView> hrefMap =
1751 new HashMap<String, WebView>();
1752 hrefMap.put("webview", webView);
1753 final Message msg = mHandler.obtainMessage(
1754 FOCUS_NODE_HREF, id, 0, hrefMap);
1755 webView.requestFocusNodeHref(msg);
1756 break;
1757
1758 default:
1759 // For other context menus
1760 result = onOptionsItemSelected(item);
1761 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001762 return result;
1763 }
1764
1765 /**
1766 * support programmatically opening the context menu
1767 */
1768 public void openContextMenu(View view) {
1769 mActivity.openContextMenu(view);
1770 }
1771
1772 /**
1773 * programmatically open the options menu
1774 */
1775 public void openOptionsMenu() {
1776 mActivity.openOptionsMenu();
1777 }
1778
1779 public boolean onMenuOpened(int featureId, Menu menu) {
1780 if (mOptionsMenuOpen) {
1781 if (mConfigChanged) {
1782 // We do not need to make any changes to the state of the
1783 // title bar, since the only thing that happened was a
1784 // change in orientation
1785 mConfigChanged = false;
1786 } else {
1787 if (!mExtendedMenuOpen) {
1788 mExtendedMenuOpen = true;
1789 mUi.onExtendedMenuOpened();
1790 } else {
1791 // Switching the menu back to icon view, so show the
1792 // title bar once again.
1793 mExtendedMenuOpen = false;
1794 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001795 }
1796 }
1797 } else {
1798 // The options menu is closed, so open it, and show the title
1799 mOptionsMenuOpen = true;
1800 mConfigChanged = false;
1801 mExtendedMenuOpen = false;
1802 mUi.onOptionsMenuOpened();
1803 }
1804 return true;
1805 }
1806
1807 public void onOptionsMenuClosed(Menu menu) {
1808 mOptionsMenuOpen = false;
1809 mUi.onOptionsMenuClosed(mInLoad);
1810 }
1811
1812 public void onContextMenuClosed(Menu menu) {
1813 mUi.onContextMenuClosed(menu, mInLoad);
1814 }
1815
1816 // Helper method for getting the top window.
1817 @Override
1818 public WebView getCurrentTopWebView() {
1819 return mTabControl.getCurrentTopWebView();
1820 }
1821
1822 @Override
1823 public WebView getCurrentWebView() {
1824 return mTabControl.getCurrentWebView();
1825 }
1826
1827 /*
1828 * This method is called as a result of the user selecting the options
1829 * menu to see the download window. It shows the download window on top of
1830 * the current window.
1831 */
1832 void viewDownloads() {
1833 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1834 mActivity.startActivity(intent);
1835 }
1836
1837 // action mode
1838
1839 void onActionModeStarted(ActionMode mode) {
1840 mUi.onActionModeStarted(mode);
1841 mActionMode = mode;
1842 }
1843
1844 /*
1845 * True if a custom ActionMode (i.e. find or select) is in use.
1846 */
1847 @Override
1848 public boolean isInCustomActionMode() {
1849 return mActionMode != null;
1850 }
1851
1852 /*
1853 * End the current ActionMode.
1854 */
1855 @Override
1856 public void endActionMode() {
1857 if (mActionMode != null) {
1858 mActionMode.finish();
1859 }
1860 }
1861
1862 /*
1863 * Called by find and select when they are finished. Replace title bars
1864 * as necessary.
1865 */
1866 public void onActionModeFinished(ActionMode mode) {
1867 if (!isInCustomActionMode()) return;
1868 mUi.onActionModeFinished(mInLoad);
1869 mActionMode = null;
1870 }
1871
1872 boolean isInLoad() {
1873 return mInLoad;
1874 }
1875
1876 // bookmark handling
1877
1878 /**
1879 * add the current page as a bookmark to the given folder id
1880 * @param folderId use -1 for the default folder
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001881 * @param canBeAnEdit If true, check to see whether the site is already
1882 * bookmarked, and if it is, edit that bookmark. If false, and
1883 * the site is already bookmarked, do not attempt to edit the
1884 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001885 */
1886 @Override
John Reck3ffc5ca2011-06-10 15:56:06 -07001887 public void bookmarkCurrentPage(boolean canBeAnEdit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001888 Intent i = new Intent(mActivity,
1889 AddBookmarkPage.class);
1890 WebView w = getCurrentTopWebView();
1891 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1892 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1893 String touchIconUrl = w.getTouchIconUrl();
1894 if (touchIconUrl != null) {
1895 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1896 WebSettings settings = w.getSettings();
1897 if (settings != null) {
1898 i.putExtra(AddBookmarkPage.USER_AGENT,
1899 settings.getUserAgentString());
1900 }
1901 }
1902 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1903 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1904 getDesiredThumbnailHeight(mActivity)));
1905 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001906 if (canBeAnEdit) {
1907 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1908 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001909 // Put the dialog at the upper right of the screen, covering the
1910 // star on the title bar.
1911 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
1912 mActivity.startActivity(i);
1913 }
1914
1915 // file chooser
1916 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1917 mUploadHandler = new UploadHandler(this);
1918 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1919 }
1920
1921 // thumbnails
1922
1923 /**
1924 * Return the desired width for thumbnail screenshots, which are stored in
1925 * the database, and used on the bookmarks screen.
1926 * @param context Context for finding out the density of the screen.
1927 * @return desired width for thumbnail screenshot.
1928 */
1929 static int getDesiredThumbnailWidth(Context context) {
1930 return context.getResources().getDimensionPixelOffset(
1931 R.dimen.bookmarkThumbnailWidth);
1932 }
1933
1934 /**
1935 * Return the desired height for thumbnail screenshots, which are stored in
1936 * the database, and used on the bookmarks screen.
1937 * @param context Context for finding out the density of the screen.
1938 * @return desired height for thumbnail screenshot.
1939 */
1940 static int getDesiredThumbnailHeight(Context context) {
1941 return context.getResources().getDimensionPixelOffset(
1942 R.dimen.bookmarkThumbnailHeight);
1943 }
1944
Michael Kolb1acef692011-03-08 14:12:06 -08001945 static Bitmap createScreenshot(Tab tab, int width, int height) {
1946 if ((tab != null) && (tab.getWebView() != null)) {
1947 return createScreenshot(tab.getWebView(), width, height);
1948 }
1949 return null;
1950 }
1951
John Reck8cc92352011-07-06 17:41:52 -07001952 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08001953 // We render to a bitmap 2x the desired size so that we can then
1954 // re-scale it with filtering since canvas.scale doesn't filter
1955 // This helps reduce aliasing at the cost of being slightly blurry
1956 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07001957 Picture thumbnail = view.capturePicture();
1958 if (thumbnail == null) {
1959 return null;
1960 }
John Reck5c6ac2f2011-01-05 10:18:03 -08001961 width *= filter_scale;
1962 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07001963 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1964 Canvas canvas = new Canvas(bm);
1965 // May need to tweak these values to determine what is the
1966 // best scale factor
1967 int thumbnailWidth = thumbnail.getWidth();
1968 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08001969 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08001970 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08001971 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07001972 } else {
1973 return null;
1974 }
John Reckfe49ab42010-11-16 17:09:37 -08001975
Michael Kolbeb95db42011-03-03 10:38:40 -08001976 float scaleFactorY = (float) height / (float)thumbnailHeight;
1977 if (scaleFactorY > scaleFactor) {
1978 // The picture is narrower than the requested AR
1979 // Center the thumnail and crop the sides
1980 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08001981 float wx = (thumbnailWidth * scaleFactor) - width;
1982 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07001983 }
1984
John Reckfe49ab42010-11-16 17:09:37 -08001985 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07001986
1987 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08001988 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
1989 height / filter_scale, true);
1990 bm.recycle();
1991 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07001992 }
1993
John Reck34ef2672011-02-10 11:30:55 -08001994 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001995 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07001996 // FIXME: Would like to make sure there is actually something to
1997 // draw, but the API for that (WebViewCore.pictureReady()) is not
1998 // currently accessible here.
1999
John Reck34ef2672011-02-10 11:30:55 -08002000 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002001 if (view == null) {
2002 // Tab was destroyed
2003 return;
2004 }
John Reck34ef2672011-02-10 11:30:55 -08002005 final String url = tab.getUrl();
2006 final String originalUrl = view.getOriginalUrl();
2007
2008 if (TextUtils.isEmpty(url)) {
2009 return;
2010 }
2011
2012 // Only update thumbnails for web urls (http(s)://), not for
2013 // about:, javascript:, data:, etc...
2014 // Unless it is a bookmarked site, then always update
2015 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2016 return;
2017 }
2018
Michael Kolb8233fac2010-10-26 16:08:53 -07002019 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2020 getDesiredThumbnailHeight(mActivity));
2021 if (bm == null) {
2022 return;
2023 }
2024
2025 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002026 new AsyncTask<Void, Void, Void>() {
2027 @Override
2028 protected Void doInBackground(Void... unused) {
2029 Cursor cursor = null;
2030 try {
2031 // TODO: Clean this up
2032 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2033 if (cursor != null && cursor.moveToFirst()) {
2034 final ByteArrayOutputStream os =
2035 new ByteArrayOutputStream();
2036 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002037
John Reck34ef2672011-02-10 11:30:55 -08002038 ContentValues values = new ContentValues();
2039 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002040
John Reck34ef2672011-02-10 11:30:55 -08002041 do {
John Reck617fd832011-02-16 14:35:59 -08002042 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002043 cr.update(Images.CONTENT_URI, values, null, null);
2044 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002045 }
John Reck34ef2672011-02-10 11:30:55 -08002046 } catch (IllegalStateException e) {
2047 // Ignore
2048 } finally {
2049 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002050 }
John Reck34ef2672011-02-10 11:30:55 -08002051 return null;
2052 }
2053 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002054 }
2055
2056 private class Copy implements OnMenuItemClickListener {
2057 private CharSequence mText;
2058
2059 public boolean onMenuItemClick(MenuItem item) {
2060 copy(mText);
2061 return true;
2062 }
2063
2064 public Copy(CharSequence toCopy) {
2065 mText = toCopy;
2066 }
2067 }
2068
Leon Scroggins63c02662010-11-18 15:16:27 -05002069 private static class Download implements OnMenuItemClickListener {
2070 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002071 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002072 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002073
2074 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002075 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002076 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002077 return true;
2078 }
2079
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002080 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002081 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002082 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002083 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002084 }
2085 }
2086
Cary Clark8974d282010-11-22 10:46:05 -05002087 private static class SelectText implements OnMenuItemClickListener {
2088 private WebView mWebView;
2089
2090 public boolean onMenuItemClick(MenuItem item) {
2091 if (mWebView != null) {
2092 return mWebView.selectText();
2093 }
2094 return false;
2095 }
2096
2097 public SelectText(WebView webView) {
2098 mWebView = webView;
2099 }
2100
2101 }
2102
Michael Kolb8233fac2010-10-26 16:08:53 -07002103 /********************** TODO: UI stuff *****************************/
2104
2105 // these methods have been copied, they still need to be cleaned up
2106
2107 /****************** tabs ***************************************************/
2108
2109 // basic tab interactions:
2110
2111 // it is assumed that tabcontrol already knows about the tab
2112 protected void addTab(Tab tab) {
2113 mUi.addTab(tab);
2114 }
2115
2116 protected void removeTab(Tab tab) {
2117 mUi.removeTab(tab);
2118 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002119 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002120 }
2121
Michael Kolbf2055602011-04-09 17:20:03 -07002122 @Override
2123 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002124 // monkey protection against delayed start
2125 if (tab != null) {
2126 mTabControl.setCurrentTab(tab);
2127 // the tab is guaranteed to have a webview after setCurrentTab
2128 mUi.setActiveTab(tab);
2129 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002130 }
2131
2132 protected void closeEmptyChildTab() {
2133 Tab current = mTabControl.getCurrentTab();
2134 if (current != null
2135 && current.getWebView().copyBackForwardList().getSize() == 0) {
Michael Kolbc831b632011-05-11 09:30:34 -07002136 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002137 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002138 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002139 closeTab(current);
2140 }
2141 }
2142 }
2143
John Reck26b18322011-06-21 13:08:58 -07002144 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002145 // Dismiss the subwindow if applicable.
2146 dismissSubWindow(appTab);
2147 // Since we might kill the WebView, remove it from the
2148 // content view first.
2149 mUi.detachTab(appTab);
2150 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002151 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002152 // TODO: analyze why the remove and add are necessary
2153 mUi.attachTab(appTab);
2154 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002155 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002156 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002157 } else {
2158 // If the tab was the current tab, we have to attach
2159 // it to the view system again.
2160 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002161 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002162 }
2163 }
2164
2165 // Remove the sub window if it exists. Also called by TabControl when the
2166 // user clicks the 'X' to dismiss a sub window.
2167 public void dismissSubWindow(Tab tab) {
2168 removeSubWindow(tab);
2169 // dismiss the subwindow. This will destroy the WebView.
2170 tab.dismissSubWindow();
2171 getCurrentTopWebView().requestFocus();
2172 }
2173
2174 @Override
2175 public void removeSubWindow(Tab t) {
2176 if (t.getSubWebView() != null) {
2177 mUi.removeSubWindow(t.getSubViewContainer());
2178 }
2179 }
2180
2181 @Override
2182 public void attachSubWindow(Tab tab) {
2183 if (tab.getSubWebView() != null) {
2184 mUi.attachSubWindow(tab.getSubViewContainer());
2185 getCurrentTopWebView().requestFocus();
2186 }
2187 }
2188
Mathew Inwood29721c22011-06-29 17:55:24 +01002189 private Tab showPreloadedTab(final UrlData urlData) {
2190 if (!urlData.isPreloaded()) {
2191 return null;
2192 }
2193 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2194 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2195 if (sbQuery != null) {
2196 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
2197 // Could not submit query. Fallback to regular tab creation
2198 tabControl.destroy();
2199 return null;
2200 }
2201 }
2202 Tab t = tabControl.getTab();
2203 mTabControl.addPreloadedTab(t);
2204 addTab(t);
2205 setActiveTab(t);
2206 return t;
2207 }
2208
Michael Kolb7bcafde2011-05-09 13:55:59 -07002209 // open a non inconito tab with the given url data
2210 // and set as active tab
2211 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002212 Tab tab = showPreloadedTab(urlData);
2213 if (tab == null) {
2214 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002215 if ((tab != null) && !urlData.isEmpty()) {
2216 loadUrlDataIn(tab, urlData);
2217 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002218 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002219 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002220 }
2221
Michael Kolb843510f2010-12-09 10:51:49 -08002222 @Override
2223 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002224 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002225 }
2226
Michael Kolb8233fac2010-10-26 16:08:53 -07002227 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002228 public Tab openIncognitoTab() {
2229 return openTab(INCOGNITO_URI, true, true, false);
2230 }
2231
2232 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002233 public Tab openTab(String url, boolean incognito, boolean setActive,
2234 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002235 return openTab(url, incognito, setActive, useCurrent, null);
2236 }
2237
2238 @Override
2239 public Tab openTab(String url, Tab parent, boolean setActive,
2240 boolean useCurrent) {
2241 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2242 setActive, useCurrent, parent);
2243 }
2244
2245 public Tab openTab(String url, boolean incognito, boolean setActive,
2246 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002247 Tab tab = createNewTab(incognito, setActive, useCurrent);
2248 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002249 if (parent != null && parent != tab) {
2250 parent.addChildTab(tab);
2251 }
Michael Kolb519d2282011-05-09 17:03:19 -07002252 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002253 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002254 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002255 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002256 return tab;
2257 }
2258
2259 // this method will attempt to create a new tab
2260 // incognito: private browsing tab
2261 // setActive: ste tab as current tab
2262 // useCurrent: if no new tab can be created, return current tab
2263 private Tab createNewTab(boolean incognito, boolean setActive,
2264 boolean useCurrent) {
2265 Tab tab = null;
2266 if (mTabControl.canCreateNewTab()) {
2267 tab = mTabControl.createNewTab(incognito);
2268 addTab(tab);
2269 if (setActive) {
2270 setActiveTab(tab);
2271 }
2272 } else {
2273 if (useCurrent) {
2274 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002275 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002276 } else {
2277 mUi.showMaxTabsWarning();
2278 }
2279 }
2280 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002281 }
2282
John Reck2bc80422011-06-30 15:11:49 -07002283 @Override
2284 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2285 SnapshotTab tab = null;
2286 if (mTabControl.canCreateNewTab()) {
2287 tab = mTabControl.createSnapshotTab(snapshotId);
2288 addTab(tab);
2289 if (setActive) {
2290 setActiveTab(tab);
2291 }
2292 } else {
2293 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002294 }
2295 return tab;
2296 }
2297
Michael Kolb8233fac2010-10-26 16:08:53 -07002298 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002299 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002300 * @return boolean True if we successfully switched to a different tab. If
2301 * the indexth tab is null, or if that tab is the same as
2302 * the current one, return false.
2303 */
2304 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002305 public boolean switchToTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002306 // hide combo view if open
2307 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002308 Tab currentTab = mTabControl.getCurrentTab();
2309 if (tab == null || tab == currentTab) {
2310 return false;
2311 }
2312 setActiveTab(tab);
2313 return true;
2314 }
2315
2316 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002317 public void closeCurrentTab() {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002318 // hide combo view if open
2319 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002320 if (mTabControl.getTabCount() == 1) {
John Reck958b2422010-12-03 17:56:17 -08002321 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002322 return;
2323 }
Michael Kolbc831b632011-05-11 09:30:34 -07002324 final Tab current = mTabControl.getCurrentTab();
2325 final int pos = mTabControl.getCurrentPosition();
2326 Tab newTab = current.getParent();
2327 if (newTab == null) {
2328 newTab = mTabControl.getTab(pos + 1);
2329 if (newTab == null) {
2330 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002331 }
2332 }
Michael Kolbc831b632011-05-11 09:30:34 -07002333 if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002334 // Close window
2335 closeTab(current);
2336 }
2337 }
2338
2339 /**
2340 * Close the tab, remove its associated title bar, and adjust mTabControl's
2341 * current tab to a valid value.
2342 */
2343 @Override
2344 public void closeTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002345 // hide combo view if open
2346 removeComboView();
Michael Kolb2d59c322011-01-25 13:18:55 -08002347 removeTab(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002348 }
2349
Michael Kolb8233fac2010-10-26 16:08:53 -07002350 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002351 protected void loadUrlFromContext(String url) {
2352 Tab tab = getCurrentTab();
2353 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002354 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002355 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002356 url = UrlUtils.smartUrlFilter(url);
2357 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002358 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002359 }
2360 }
2361 }
2362
2363 /**
2364 * Load the URL into the given WebView and update the title bar
2365 * to reflect the new load. Call this instead of WebView.loadUrl
2366 * directly.
2367 * @param view The WebView used to load url.
2368 * @param url The URL to load.
2369 */
John Reck71e51422011-07-01 16:49:28 -07002370 @Override
2371 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002372 loadUrl(tab, url, null);
2373 }
2374
2375 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2376 if (tab != null) {
2377 dismissSubWindow(tab);
2378 tab.loadUrl(url, headers);
2379 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002380 }
2381
2382 /**
2383 * Load UrlData into a Tab and update the title bar to reflect the new
2384 * load. Call this instead of UrlData.loadIn directly.
2385 * @param t The Tab used to load.
2386 * @param data The UrlData being loaded.
2387 */
2388 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002389 if (data != null) {
2390 if (data.mVoiceIntent != null) {
2391 t.activateVoiceSearchMode(data.mVoiceIntent);
Michael Kolb14612442011-06-24 13:06:29 -07002392 } else if (data.isPreloaded()) {
2393 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002394 } else {
2395 loadUrl(t, data.mUrl, data.mHeaders);
2396 }
2397 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002398 }
2399
John Reck30c714c2010-12-16 17:30:34 -08002400 @Override
2401 public void onUserCanceledSsl(Tab tab) {
2402 WebView web = tab.getWebView();
2403 // TODO: Figure out the "right" behavior
2404 if (web.canGoBack()) {
2405 web.goBack();
2406 } else {
2407 web.loadUrl(mSettings.getHomePage());
2408 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002409 }
2410
2411 void goBackOnePageOrQuit() {
2412 Tab current = mTabControl.getCurrentTab();
2413 if (current == null) {
2414 /*
2415 * Instead of finishing the activity, simply push this to the back
2416 * of the stack and let ActivityManager to choose the foreground
2417 * activity. As BrowserActivity is singleTask, it will be always the
2418 * root of the task. So we can use either true or false for
2419 * moveTaskToBack().
2420 */
2421 mActivity.moveTaskToBack(true);
2422 return;
2423 }
2424 WebView w = current.getWebView();
2425 if (w.canGoBack()) {
2426 w.goBack();
2427 } else {
2428 // Check to see if we are closing a window that was created by
2429 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002430 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002431 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002432 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002433 // Now we close the other tab
2434 closeTab(current);
2435 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002436 /*
2437 * Instead of finishing the activity, simply push this to the back
2438 * of the stack and let ActivityManager to choose the foreground
2439 * activity. As BrowserActivity is singleTask, it will be always the
2440 * root of the task. So we can use either true or false for
2441 * moveTaskToBack().
2442 */
2443 mActivity.moveTaskToBack(true);
2444 }
2445 }
2446 }
2447
2448 /**
2449 * Feed the previously stored results strings to the BrowserProvider so that
2450 * the SearchDialog will show them instead of the standard searches.
2451 * @param result String to show on the editable line of the SearchDialog.
2452 */
2453 @Override
2454 public void showVoiceSearchResults(String result) {
2455 ContentProviderClient client = mActivity.getContentResolver()
2456 .acquireContentProviderClient(Browser.BOOKMARKS_URI);
2457 ContentProvider prov = client.getLocalContentProvider();
2458 BrowserProvider bp = (BrowserProvider) prov;
2459 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
2460 client.release();
2461
2462 Bundle bundle = createGoogleSearchSourceBundle(
2463 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
2464 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
2465 startSearch(result, false, bundle, false);
2466 }
2467
2468 private void startSearch(String initialQuery, boolean selectInitialQuery,
2469 Bundle appSearchData, boolean globalSearch) {
2470 if (appSearchData == null) {
2471 appSearchData = createGoogleSearchSourceBundle(
2472 GOOGLE_SEARCH_SOURCE_TYPE);
2473 }
2474
2475 SearchEngine searchEngine = mSettings.getSearchEngine();
2476 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
2477 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
2478 }
2479 mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData,
2480 globalSearch);
2481 }
2482
2483 private Bundle createGoogleSearchSourceBundle(String source) {
2484 Bundle bundle = new Bundle();
2485 bundle.putString(Search.SOURCE, source);
2486 return bundle;
2487 }
2488
2489 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002490 * helper method for key handler
2491 * returns the current tab if it can't advance
2492 */
Michael Kolbc831b632011-05-11 09:30:34 -07002493 private Tab getNextTab() {
2494 return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1,
2495 mTabControl.getCurrentPosition() + 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002496 }
2497
2498 /**
2499 * helper method for key handler
2500 * returns the current tab if it can't advance
2501 */
Michael Kolbc831b632011-05-11 09:30:34 -07002502 private Tab getPrevTab() {
2503 return mTabControl.getTab(Math.max(0,
2504 mTabControl.getCurrentPosition() - 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002505 }
2506
2507 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002508 * handle key events in browser
2509 *
2510 * @param keyCode
2511 * @param event
2512 * @return true if handled, false to pass to super
2513 */
2514 boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002515 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002516 // Even if MENU is already held down, we need to call to super to open
2517 // the IME on long press.
Michael Kolb2814a362011-05-19 15:49:41 -07002518 if (KeyEvent.KEYCODE_MENU == keyCode) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002519 if (mOptionsMenuHandler != null) {
2520 return false;
2521 } else {
2522 event.startTracking();
2523 return true;
2524 }
Michael Kolb2814a362011-05-19 15:49:41 -07002525 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002526 if (!noModifiers
2527 && ((KeyEvent.KEYCODE_MENU == keyCode)
2528 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2529 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002530 mMenuIsDown = true;
2531 return false;
2532 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002533
Cary Clark8ff8c662010-12-29 15:03:05 -05002534 WebView webView = getCurrentTopWebView();
2535 if (webView == null) return false;
2536
Cary Clark160bbb92011-01-10 11:17:07 -05002537 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2538 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002539
Michael Kolb8233fac2010-10-26 16:08:53 -07002540 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002541 case KeyEvent.KEYCODE_TAB:
2542 if (event.isCtrlPressed()) {
2543 if (event.isShiftPressed()) {
2544 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002545 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002546 } else {
2547 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002548 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002549 }
2550 return true;
2551 }
2552 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002553 case KeyEvent.KEYCODE_SPACE:
2554 // WebView/WebTextView handle the keys in the KeyDown. As
2555 // the Activity's shortcut keys are only handled when WebView
2556 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002557 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002558 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002559 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002560 pageDown();
2561 }
2562 return true;
2563 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002564 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002565 event.startTracking();
2566 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002567 case KeyEvent.KEYCODE_DPAD_LEFT:
2568 if (ctrl) {
2569 webView.goBack();
2570 return true;
2571 }
2572 break;
2573 case KeyEvent.KEYCODE_DPAD_RIGHT:
2574 if (ctrl) {
2575 webView.goForward();
2576 return true;
2577 }
2578 break;
2579 case KeyEvent.KEYCODE_A:
2580 if (ctrl) {
2581 webView.selectAll();
2582 return true;
2583 }
2584 break;
Michael Kolba4183062011-01-16 10:43:21 -08002585// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002586 case KeyEvent.KEYCODE_C:
2587 if (ctrl) {
2588 webView.copySelection();
2589 return true;
2590 }
2591 break;
Michael Kolba4183062011-01-16 10:43:21 -08002592// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002593// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002594// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002595// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002596// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002597// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002598// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002599// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002600// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002601// case KeyEvent.KEYCODE_M: // unused
2602// case KeyEvent.KEYCODE_N: // in Chrome: new window
2603// case KeyEvent.KEYCODE_O: // in Chrome: open file
2604// case KeyEvent.KEYCODE_P: // in Chrome: print page
2605// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002606// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002607// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2608 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002609 // we can't use the ctrl/shift flags, they check for
2610 // exclusive use of a modifier
2611 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002612 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002613 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002614 } else {
2615 openTabToHomePage();
2616 }
2617 return true;
2618 }
2619 break;
2620// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2621// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb1a2eba42011-03-16 16:42:49 -07002622 case KeyEvent.KEYCODE_W: // in Chrome: close tab
2623 if (ctrl) {
2624 closeCurrentTab();
2625 return true;
2626 }
2627 break;
Cary Clark8ff8c662010-12-29 15:03:05 -05002628// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2629// case KeyEvent.KEYCODE_Y: // unused
2630// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002631 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002632 // it is a regular key and webview is not null
2633 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002634 }
2635
John Recke6bf4ab2011-02-24 15:48:05 -08002636 boolean onKeyLongPress(int keyCode, KeyEvent event) {
2637 switch(keyCode) {
2638 case KeyEvent.KEYCODE_BACK:
2639 if (mUi.showsWeb()) {
2640 bookmarksOrHistoryPicker(true);
2641 return true;
2642 }
2643 break;
2644 }
2645 return false;
2646 }
2647
Michael Kolb8233fac2010-10-26 16:08:53 -07002648 boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb2814a362011-05-19 15:49:41 -07002649 if (KeyEvent.KEYCODE_MENU == keyCode) {
2650 mMenuIsDown = false;
2651 if (event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002652 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002653 }
2654 }
Cary Clark160bbb92011-01-10 11:17:07 -05002655 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002656 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002657 case KeyEvent.KEYCODE_BACK:
2658 if (event.isTracking() && !event.isCanceled()) {
2659 onBackKey();
2660 return true;
2661 }
2662 break;
2663 }
2664 return false;
2665 }
2666
2667 public boolean isMenuDown() {
2668 return mMenuIsDown;
2669 }
2670
Ben Murdoch8029a772010-11-16 11:58:21 +00002671 public void setupAutoFill(Message message) {
2672 // Open the settings activity at the AutoFill profile fragment so that
2673 // the user can create a new profile. When they return, we will dispatch
2674 // the message so that we can autofill the form using their new profile.
2675 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2676 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2677 AutoFillSettingsFragment.class.getName());
2678 mAutoFillSetupMessage = message;
2679 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2680 }
John Reckb3417f02011-01-14 11:01:05 -08002681
2682 @Override
2683 public void registerOptionsMenuHandler(OptionsMenuHandler handler) {
2684 mOptionsMenuHandler = handler;
2685 }
2686
2687 @Override
2688 public void unregisterOptionsMenuHandler(OptionsMenuHandler handler) {
2689 if (mOptionsMenuHandler == handler) {
2690 mOptionsMenuHandler = null;
2691 }
2692 }
2693
Narayan Kamath5119edd2011-02-23 15:49:17 +00002694 @Override
2695 public void registerDropdownChangeListener(DropdownChangeListener d) {
2696 mUi.registerDropdownChangeListener(d);
2697 }
Michael Kolbfbc579a2011-07-07 15:59:33 -07002698
2699 public boolean onSearchRequested() {
2700 mUi.editUrl(false);
2701 return true;
2702 }
2703
Michael Kolb8233fac2010-10-26 16:08:53 -07002704}