blob: 9b818706278c07e69ab66dae856429061b9a62d6 [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
20import android.app.DownloadManager;
21import android.app.SearchManager;
22import android.content.ClipboardManager;
23import android.content.ContentProvider;
24import android.content.ContentProviderClient;
25import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070026import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.content.ContentValues;
28import android.content.Context;
29import android.content.Intent;
30import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
32import android.content.res.Configuration;
Leon Scroggins1961ed22010-12-07 15:22:21 -050033import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import android.database.Cursor;
35import android.database.sqlite.SQLiteDatabase;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.graphics.Bitmap;
37import android.graphics.Canvas;
38import android.graphics.Picture;
39import android.net.Uri;
40import android.net.http.SslError;
41import android.os.AsyncTask;
42import android.os.Bundle;
43import android.os.Handler;
44import android.os.Message;
45import android.os.PowerManager;
46import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000047import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.provider.Browser;
49import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.provider.BrowserContract.Images;
51import android.provider.ContactsContract;
52import android.provider.ContactsContract.Intents.Insert;
Michael Kolbcfa3af52010-12-14 10:36:11 -080053import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.text.TextUtils;
55import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080056import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070057import android.view.ActionMode;
58import android.view.ContextMenu;
59import android.view.ContextMenu.ContextMenuInfo;
60import android.view.Gravity;
61import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070062import android.view.Menu;
63import android.view.MenuInflater;
64import android.view.MenuItem;
65import android.view.MenuItem.OnMenuItemClickListener;
66import android.view.View;
67import android.webkit.CookieManager;
68import android.webkit.CookieSyncManager;
69import android.webkit.HttpAuthHandler;
70import android.webkit.SslErrorHandler;
71import android.webkit.ValueCallback;
72import android.webkit.WebChromeClient;
73import android.webkit.WebIconDatabase;
74import android.webkit.WebSettings;
75import android.webkit.WebView;
Leon Scrogginsac993842011-02-02 12:54:07 -050076import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070077
Michael Kolb4bd767d2011-05-27 11:33:55 -070078import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070079import com.android.browser.UI.ComboViews;
Michael Kolb4bd767d2011-05-27 11:33:55 -070080import com.android.browser.UI.DropdownChangeListener;
81import com.android.browser.provider.BrowserProvider;
John Reck8cc92352011-07-06 17:41:52 -070082import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070083import com.android.browser.search.SearchEngine;
84import com.android.common.Search;
85
Michael Kolb8233fac2010-10-26 16:08:53 -070086import java.io.ByteArrayOutputStream;
87import java.io.File;
88import java.net.URLEncoder;
89import java.util.Calendar;
90import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080091import java.util.List;
John Reck26b18322011-06-21 13:08:58 -070092import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -070093
94/**
95 * Controller for browser
96 */
97public class Controller
98 implements WebViewController, UiController {
99
100 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800101 private static final String SEND_APP_ID_EXTRA =
102 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700103 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800104
Michael Kolb8233fac2010-10-26 16:08:53 -0700105
106 // public message ids
107 public final static int LOAD_URL = 1001;
108 public final static int STOP_LOAD = 1002;
109
110 // Message Ids
111 private static final int FOCUS_NODE_HREF = 102;
112 private static final int RELEASE_WAKELOCK = 107;
113
114 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
115
116 private static final int OPEN_BOOKMARKS = 201;
117
118 private static final int EMPTY_MENU = -1;
119
Michael Kolb8233fac2010-10-26 16:08:53 -0700120 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700121 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700122 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;
149
150 private WakeLock mWakeLock;
151
152 private UrlHandler mUrlHandler;
153 private UploadHandler mUploadHandler;
154 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700155 private PageDialogsHandler mPageDialogsHandler;
156 private NetworkStateHandler mNetworkHandler;
Martijn Coenenb2f93552011-06-14 10:48:35 +0200157 private NfcHandler mNfcHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700158
Ben Murdoch8029a772010-11-16 11:58:21 +0000159 private Message mAutoFillSetupMessage;
160
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 private boolean mShouldShowErrorConsole;
162
163 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
164
165 // FIXME, temp address onPrepareMenu performance problem.
166 // When we move everything out of view, we should rewrite this.
167 private int mCurrentMenuState = 0;
168 private int mMenuState = R.id.MAIN_MENU;
169 private int mOldMenuState = EMPTY_MENU;
170 private Menu mCachedMenu;
171
Michael Kolb8233fac2010-10-26 16:08:53 -0700172 private boolean mMenuIsDown;
173
174 // For select and find, we keep track of the ActionMode so that
175 // finish() can be called as desired.
176 private ActionMode mActionMode;
177
178 /**
179 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
180 * of whether the configuration has changed. The first onMenuOpened call
181 * after a configuration change is simply a reopening of the same menu
182 * (i.e. mIconView did not change).
183 */
184 private boolean mConfigChanged;
185
186 /**
187 * Keeps track of whether the options menu is open. This is important in
188 * determining whether to show or hide the title bar overlay
189 */
190 private boolean mOptionsMenuOpen;
191
192 /**
193 * Whether or not the options menu is in its bigger, popup menu form. When
194 * true, we want the title bar overlay to be gone. When false, we do not.
195 * Only meaningful if mOptionsMenuOpen is true.
196 */
197 private boolean mExtendedMenuOpen;
198
199 private boolean mInLoad;
200
201 private boolean mActivityPaused = true;
202 private boolean mLoadStopped;
203
204 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500205 // Checks to see when the bookmarks database has changed, and updates the
206 // Tabs' notion of whether they represent bookmarked sites.
207 private ContentObserver mBookmarksObserver;
John Reck0ebd3ac2010-12-09 11:14:04 -0800208 private DataController mDataController;
John Reck847b5322011-04-14 17:02:18 -0700209 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700210
211 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
212 @Override
213 public Void doInBackground(File... files) {
214 if (files != null) {
215 for (File f : files) {
216 if (!f.delete()) {
217 Log.e(LOGTAG, f.getPath() + " was not deleted");
218 }
219 }
220 }
221 return null;
222 }
223 }
224
225 public Controller(Activity browser) {
226 mActivity = browser;
227 mSettings = BrowserSettings.getInstance();
John Reck0ebd3ac2010-12-09 11:14:04 -0800228 mDataController = DataController.getInstance(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700229 mTabControl = new TabControl(this);
230 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700231 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
Michael Kolb14612442011-06-24 13:06:29 -0700232 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700233
234 mUrlHandler = new UrlHandler(this);
235 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200237 mNfcHandler = new NfcHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700238
239 PowerManager pm = (PowerManager) mActivity
240 .getSystemService(Context.POWER_SERVICE);
241 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
242
243 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500244 mBookmarksObserver = new ContentObserver(mHandler) {
245 @Override
246 public void onChange(boolean selfChange) {
247 int size = mTabControl.getTabCount();
248 for (int i = 0; i < size; i++) {
249 mTabControl.getTab(i).updateBookmarkedStatus();
250 }
251 }
252
253 };
254 browser.getContentResolver().registerContentObserver(
255 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700256
257 mNetworkHandler = new NetworkStateHandler(mActivity, this);
258 // Start watching the default geolocation permissions
259 mSystemAllowGeolocationOrigins =
260 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
261 mSystemAllowGeolocationOrigins.start();
262
263 retainIconsOnStartup();
264 }
265
Patrick Scott7d50a932011-02-04 09:27:26 -0500266 void start(final Bundle icicle, final Intent intent) {
Guang Zhu9e78f512011-05-04 11:45:11 -0700267 boolean noCrashRecovery = intent.getBooleanExtra(NO_CRASH_RECOVERY, false);
268 if (icicle != null || noCrashRecovery) {
John Reck847b5322011-04-14 17:02:18 -0700269 doStart(icicle, intent);
270 } else {
271 mCrashRecoveryHandler.startRecovery(intent);
272 }
273 }
274
275 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700276 // Unless the last browser usage was within 24 hours, destroy any
277 // remaining incognito tabs.
278
279 Calendar lastActiveDate = icicle != null ?
280 (Calendar) icicle.getSerializable("lastActiveDate") : null;
281 Calendar today = Calendar.getInstance();
282 Calendar yesterday = Calendar.getInstance();
283 yesterday.add(Calendar.DATE, -1);
284
Patrick Scott7d50a932011-02-04 09:27:26 -0500285 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700286 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800287 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700288
Patrick Scott7d50a932011-02-04 09:27:26 -0500289 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700290 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500291 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000292
Michael Kolbc831b632011-05-11 09:30:34 -0700293 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500294 // Not able to restore so we go ahead and clear session cookies. We
295 // must do this before trying to login the user as we don't want to
296 // clear any session cookies set during login.
297 CookieManager.getInstance().removeSessionCookie();
298 }
299
Patrick Scottd43e75a2011-03-14 14:47:23 -0400300 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500301 new Runnable() {
302 @Override public void run() {
Michael Kolbc831b632011-05-11 09:30:34 -0700303 onPreloginFinished(icicle, intent, currentTabId, restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500304 }
305 });
306 }
307
Michael Kolbc831b632011-05-11 09:30:34 -0700308 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500309 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700310 if (currentTabId == -1) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700311 final Bundle extra = intent.getExtras();
312 // Create an initial tab.
313 // If the intent is ACTION_VIEW and data is not null, the Browser is
314 // invoked to view the content by another application. In this case,
315 // the tab will be close when exit.
Michael Kolb14612442011-06-24 13:06:29 -0700316 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
Michael Kolb7bcafde2011-05-09 13:55:59 -0700317 Tab t = null;
318 if (urlData.isEmpty()) {
319 t = openTabToHomePage();
320 } else {
321 t = openTab(urlData);
322 }
323 if (t != null) {
324 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
325 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700326 WebView webView = t.getWebView();
327 if (extra != null) {
328 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
329 if (scale > 0 && scale <= 1000) {
330 webView.setInitialScale(scale);
331 }
332 }
John Reckd8c74522011-06-14 08:45:00 -0700333 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700334 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700335 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500336 mUi.needsRestoreAllTabs());
Michael Kolb1bf23132010-11-19 12:55:12 -0800337 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700338 // TabControl.restoreState() will create a new tab even if
339 // restoring the state fails.
340 setActiveTab(mTabControl.getCurrentTab());
John Reckdb22ec42011-06-29 11:31:24 -0700341 // Handle the intent
342 mIntentHandler.onNewIntent(intent);
Michael Kolb8233fac2010-10-26 16:08:53 -0700343 }
344 // clear up the thumbnail directory, which is no longer used;
345 // ideally this should only be run once after an upgrade from
346 // a previous version of the browser
347 new ClearThumbnails().execute(mTabControl.getThumbnailDir()
348 .listFiles());
349 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700350 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700351 if (jsFlags.trim().length() != 0) {
352 getCurrentWebView().setJsFlags(jsFlags);
353 }
John Reck439c9a52010-12-14 10:04:39 -0800354 if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
355 bookmarksOrHistoryPicker(false);
356 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700357 }
358
Michael Kolb1514bb72010-11-22 09:11:48 -0800359 @Override
360 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700361 return mFactory;
362 }
363
364 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800365 public void onSetWebView(Tab tab, WebView view) {
366 mUi.onSetWebView(tab, view);
367 }
368
369 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800370 public void createSubWindow(Tab tab) {
371 endActionMode();
372 WebView mainView = tab.getWebView();
373 WebView subView = mFactory.createWebView((mainView == null)
374 ? false
375 : mainView.isPrivateBrowsingEnabled());
376 mUi.createSubWindow(tab, subView);
377 }
378
379 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700380 public Context getContext() {
381 return mActivity;
382 }
383
384 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700385 public Activity getActivity() {
386 return mActivity;
387 }
388
389 void setUi(UI ui) {
390 mUi = ui;
391 }
392
393 BrowserSettings getSettings() {
394 return mSettings;
395 }
396
397 IntentHandler getIntentHandler() {
398 return mIntentHandler;
399 }
400
401 @Override
402 public UI getUi() {
403 return mUi;
404 }
405
406 int getMaxTabs() {
407 return mActivity.getResources().getInteger(R.integer.max_tabs);
408 }
409
410 @Override
411 public TabControl getTabControl() {
412 return mTabControl;
413 }
414
Michael Kolb1bf23132010-11-19 12:55:12 -0800415 @Override
416 public List<Tab> getTabs() {
417 return mTabControl.getTabs();
418 }
419
Michael Kolb8233fac2010-10-26 16:08:53 -0700420 // Open the icon database and retain all the icons for visited sites.
Ben Murdoch9446b932010-11-25 16:20:14 +0000421 // This is done on a background thread so as not to stall startup.
Michael Kolb8233fac2010-10-26 16:08:53 -0700422 private void retainIconsOnStartup() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000423 // WebIconDatabase needs to be retrieved on the UI thread so that if
424 // it has not been created successfully yet the Handler is started on the
425 // UI thread.
426 new RetainIconsOnStartupTask(WebIconDatabase.getInstance()).execute();
427 }
428
429 private class RetainIconsOnStartupTask extends AsyncTask<Void, Void, Void> {
430 private WebIconDatabase mDb;
431
432 public RetainIconsOnStartupTask(WebIconDatabase db) {
433 mDb = db;
434 }
435
John Recka00cbbd2010-12-16 12:38:19 -0800436 @Override
Ben Murdoch9446b932010-11-25 16:20:14 +0000437 protected Void doInBackground(Void... unused) {
438 mDb.open(mActivity.getDir("icons", 0).getPath());
439 Cursor c = null;
440 try {
441 c = Browser.getAllBookmarks(mActivity.getContentResolver());
442 if (c.moveToFirst()) {
443 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
444 do {
445 String url = c.getString(urlIndex);
446 mDb.retainIconForPageUrl(url);
447 } while (c.moveToNext());
448 }
449 } catch (IllegalStateException e) {
450 Log.e(LOGTAG, "retainIconsOnStartup", e);
451 } finally {
452 if (c != null) c.close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700453 }
Ben Murdoch9446b932010-11-25 16:20:14 +0000454
455 return null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700456 }
457 }
458
459 private void startHandler() {
460 mHandler = new Handler() {
461
462 @Override
463 public void handleMessage(Message msg) {
464 switch (msg.what) {
465 case OPEN_BOOKMARKS:
466 bookmarksOrHistoryPicker(false);
467 break;
468 case FOCUS_NODE_HREF:
469 {
470 String url = (String) msg.getData().get("url");
471 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500472 String src = (String) msg.getData().get("src");
473 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700474 if (TextUtils.isEmpty(url)) {
475 break;
476 }
477 HashMap focusNodeMap = (HashMap) msg.obj;
478 WebView view = (WebView) focusNodeMap.get("webview");
479 // Only apply the action if the top window did not change.
480 if (getCurrentTopWebView() != view) {
481 break;
482 }
483 switch (msg.arg1) {
484 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700485 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700486 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500487 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700488 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500489 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500490 case R.id.open_newtab_context_menu_id:
491 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700492 openTab(url, parent,
493 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500494 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700495 case R.id.copy_link_context_menu_id:
496 copy(url);
497 break;
498 case R.id.save_link_context_menu_id:
499 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500500 DownloadHandler.onDownloadStartNoStream(
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000501 mActivity, url, null, null, null,
502 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700503 break;
504 }
505 break;
506 }
507
508 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700509 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700510 break;
511
512 case STOP_LOAD:
513 stopLoading();
514 break;
515
516 case RELEASE_WAKELOCK:
517 if (mWakeLock.isHeld()) {
518 mWakeLock.release();
519 // if we reach here, Browser should be still in the
520 // background loading after WAKELOCK_TIMEOUT (5-min).
521 // To avoid burning the battery, stop loading.
522 mTabControl.stopAllLoading();
523 }
524 break;
525
526 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800527 Tab tab = (Tab) msg.obj;
528 if (tab != null) {
529 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700530 }
531 break;
532 }
533 }
534 };
535
536 }
537
John Reckef654f12011-07-12 16:42:08 -0700538 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200539 public Tab getCurrentTab() {
540 return mTabControl.getCurrentTab();
541 }
542
Michael Kolbba99c5d2010-11-29 14:57:41 -0800543 @Override
544 public void shareCurrentPage() {
545 shareCurrentPage(mTabControl.getCurrentTab());
546 }
547
548 private void shareCurrentPage(Tab tab) {
549 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800550 sharePage(mActivity, tab.getTitle(),
551 tab.getUrl(), tab.getFavicon(),
552 createScreenshot(tab.getWebView(),
553 getDesiredThumbnailWidth(mActivity),
554 getDesiredThumbnailHeight(mActivity)));
555 }
556 }
557
Michael Kolb8233fac2010-10-26 16:08:53 -0700558 /**
559 * Share a page, providing the title, url, favicon, and a screenshot. Uses
560 * an {@link Intent} to launch the Activity chooser.
561 * @param c Context used to launch a new Activity.
562 * @param title Title of the page. Stored in the Intent with
563 * {@link Intent#EXTRA_SUBJECT}
564 * @param url URL of the page. Stored in the Intent with
565 * {@link Intent#EXTRA_TEXT}
566 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
567 * with {@link Browser#EXTRA_SHARE_FAVICON}
568 * @param screenshot Bitmap of a screenshot of the page. Stored in the
569 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
570 */
571 static final void sharePage(Context c, String title, String url,
572 Bitmap favicon, Bitmap screenshot) {
573 Intent send = new Intent(Intent.ACTION_SEND);
574 send.setType("text/plain");
575 send.putExtra(Intent.EXTRA_TEXT, url);
576 send.putExtra(Intent.EXTRA_SUBJECT, title);
577 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
578 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
579 try {
580 c.startActivity(Intent.createChooser(send, c.getString(
581 R.string.choosertitle_sharevia)));
582 } catch(android.content.ActivityNotFoundException ex) {
583 // if no app handles it, do nothing
584 }
585 }
586
587 private void copy(CharSequence text) {
588 ClipboardManager cm = (ClipboardManager) mActivity
589 .getSystemService(Context.CLIPBOARD_SERVICE);
590 cm.setText(text);
591 }
592
593 // lifecycle
594
595 protected void onConfgurationChanged(Configuration config) {
596 mConfigChanged = true;
597 if (mPageDialogsHandler != null) {
598 mPageDialogsHandler.onConfigurationChanged(config);
599 }
600 mUi.onConfigurationChanged(config);
601 }
602
603 @Override
604 public void handleNewIntent(Intent intent) {
605 mIntentHandler.onNewIntent(intent);
606 }
607
608 protected void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800609 if (mUi.isCustomViewShowing()) {
610 hideCustomView();
611 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700612 if (mActivityPaused) {
613 Log.e(LOGTAG, "BrowserActivity is already paused.");
614 return;
615 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700616 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800617 Tab tab = mTabControl.getCurrentTab();
618 if (tab != null) {
619 tab.pause();
620 if (!pauseWebViewTimers(tab)) {
621 mWakeLock.acquire();
622 mHandler.sendMessageDelayed(mHandler
623 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
624 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 }
626 mUi.onPause();
627 mNetworkHandler.onPause();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200628 mNfcHandler.onPause();
Michael Kolb8233fac2010-10-26 16:08:53 -0700629
630 WebView.disablePlatformNotifications();
John Reck378a4102011-06-09 16:23:01 -0700631 mCrashRecoveryHandler.backupState();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200632
Michael Kolb8233fac2010-10-26 16:08:53 -0700633 }
634
John Reckaed9c542011-05-27 16:08:53 -0700635 void onSaveInstanceState(Bundle outState, boolean saveImages) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700636 // the default implementation requires each view to have an id. As the
637 // browser handles the state itself and it doesn't use id for the views,
638 // don't call the default implementation. Otherwise it will trigger the
639 // warning like this, "couldn't save which view has focus because the
640 // focused view XXX has no id".
641
642 // Save all the tabs
John Reckaed9c542011-05-27 16:08:53 -0700643 mTabControl.saveState(outState, saveImages);
John Reck24f18262011-06-17 14:47:20 -0700644 if (!outState.isEmpty()) {
645 // Save time so that we know how old incognito tabs (if any) are.
646 outState.putSerializable("lastActiveDate", Calendar.getInstance());
647 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700648 }
649
650 void onResume() {
651 if (!mActivityPaused) {
652 Log.e(LOGTAG, "BrowserActivity is already resumed.");
653 return;
654 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700655 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800656 Tab current = mTabControl.getCurrentTab();
657 if (current != null) {
658 current.resume();
659 resumeWebViewTimers(current);
660 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 if (mWakeLock.isHeld()) {
662 mHandler.removeMessages(RELEASE_WAKELOCK);
663 mWakeLock.release();
664 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200665
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 mUi.onResume();
667 mNetworkHandler.onResume();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200668 mNfcHandler.onResume();
Michael Kolb8233fac2010-10-26 16:08:53 -0700669 WebView.enablePlatformNotifications();
670 }
671
Michael Kolb70976932010-11-30 11:34:01 -0800672 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800673 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800674 * @param tab guaranteed non-null
675 */
676 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700677 boolean inLoad = tab.inPageLoad();
678 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
679 CookieSyncManager.getInstance().startSync();
680 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100681 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700682 }
683 }
684
Michael Kolb70976932010-11-30 11:34:01 -0800685 /**
686 * Pause all WebView timers using the WebView of the given tab
687 * @param tab
688 * @return true if the timers are paused or tab is null
689 */
690 private boolean pauseWebViewTimers(Tab tab) {
691 if (tab == null) {
692 return true;
693 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700694 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100695 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700696 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 }
Michael Kolb70976932010-11-30 11:34:01 -0800698 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700699 }
700
701 void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800702 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
704 mUploadHandler = null;
705 }
706 if (mTabControl == null) return;
707 mUi.onDestroy();
708 // Remove the current tab and sub window
709 Tab t = mTabControl.getCurrentTab();
710 if (t != null) {
711 dismissSubWindow(t);
712 removeTab(t);
713 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500714 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700715 // Destroy all the tabs
716 mTabControl.destroy();
717 WebIconDatabase.getInstance().close();
718 // Stop watching the default geolocation permissions
719 mSystemAllowGeolocationOrigins.stop();
720 mSystemAllowGeolocationOrigins = null;
721 }
722
723 protected boolean isActivityPaused() {
724 return mActivityPaused;
725 }
726
727 protected void onLowMemory() {
728 mTabControl.freeMemory();
729 }
730
731 @Override
732 public boolean shouldShowErrorConsole() {
733 return mShouldShowErrorConsole;
734 }
735
736 protected void setShouldShowErrorConsole(boolean show) {
737 if (show == mShouldShowErrorConsole) {
738 // Nothing to do.
739 return;
740 }
741 mShouldShowErrorConsole = show;
742 Tab t = mTabControl.getCurrentTab();
743 if (t == null) {
744 // There is no current tab so we cannot toggle the error console
745 return;
746 }
747 mUi.setShouldShowErrorConsole(t, show);
748 }
749
750 @Override
751 public void stopLoading() {
752 mLoadStopped = true;
753 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700754 WebView w = getCurrentTopWebView();
755 w.stopLoading();
Michael Kolb8233fac2010-10-26 16:08:53 -0700756 mUi.onPageStopped(tab);
757 }
758
759 boolean didUserStopLoading() {
760 return mLoadStopped;
761 }
762
763 // WebViewController
764
765 @Override
John Reck324d4402011-01-11 16:56:42 -0800766 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700767
768 // We've started to load a new page. If there was a pending message
769 // to save a screenshot then we will now take the new page and save
770 // an incorrect screenshot. Therefore, remove any pending thumbnail
771 // messages from the queue.
772 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800773 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700774
775 // reset sync timer to avoid sync starts during loading a page
776 CookieSyncManager.getInstance().resetSync();
777
778 if (!mNetworkHandler.isNetworkUp()) {
779 view.setNetworkAvailable(false);
780 }
781
782 // when BrowserActivity just starts, onPageStarted may be called before
783 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
784 // to start the timer. As we won't switch tabs while an activity is in
785 // pause state, we can ensure calling resume and pause in pair.
786 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800787 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700788 }
789 mLoadStopped = false;
790 if (!mNetworkHandler.isNetworkUp()) {
791 mNetworkHandler.createAndShowNetworkDialog();
792 }
793 endActionMode();
794
John Reck30c714c2010-12-16 17:30:34 -0800795 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700796
John Reck324d4402011-01-11 16:56:42 -0800797 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700798 // update the bookmark database for favicon
799 maybeUpdateFavicon(tab, null, url, favicon);
800
801 Performance.tracePageStart(url);
802
803 // Performance probe
804 if (false) {
805 Performance.onPageStarted();
806 }
807
808 }
809
810 @Override
John Reck324d4402011-01-11 16:56:42 -0800811 public void onPageFinished(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800812 mUi.onTabDataChanged(tab);
John Reck324d4402011-01-11 16:56:42 -0800813 if (!tab.isPrivateBrowsingEnabled()
John Reckd8c74522011-06-14 08:45:00 -0700814 && !TextUtils.isEmpty(tab.getUrl())
815 && !tab.isSnapshot()) {
John Recka1696282011-07-08 14:10:37 -0700816 // Only update the bookmark screenshot if the user did not
817 // cancel the load early and there is not already
818 // a pending update for the tab.
Michael Kolb8233fac2010-10-26 16:08:53 -0700819 if (tab.inForeground() && !didUserStopLoading()
820 || !tab.inForeground()) {
John Recka1696282011-07-08 14:10:37 -0700821 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
822 mHandler.sendMessageDelayed(mHandler.obtainMessage(
823 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
824 500);
825 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700826 }
827 }
828 // pause the WebView timer and release the wake lock if it is finished
829 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800830 if (mActivityPaused && pauseWebViewTimers(tab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700831 if (mWakeLock.isHeld()) {
832 mHandler.removeMessages(RELEASE_WAKELOCK);
833 mWakeLock.release();
834 }
835 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200836
Michael Kolb8233fac2010-10-26 16:08:53 -0700837 // Performance probe
838 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800839 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 }
841
842 Performance.tracePageFinished();
843 }
844
845 @Override
John Reck30c714c2010-12-16 17:30:34 -0800846 public void onProgressChanged(Tab tab) {
847 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700848
849 if (newProgress == 100) {
850 CookieSyncManager.getInstance().sync();
851 // onProgressChanged() may continue to be called after the main
852 // frame has finished loading, as any remaining sub frames continue
853 // to load. We'll only get called once though with newProgress as
854 // 100 when everything is loaded. (onPageFinished is called once
855 // when the main frame completes loading regardless of the state of
856 // any sub frames so calls to onProgressChanges may continue after
857 // onPageFinished has executed)
858 if (mInLoad) {
859 mInLoad = false;
860 updateInLoadMenuItems(mCachedMenu);
861 }
862 } else {
863 if (!mInLoad) {
864 // onPageFinished may have already been called but a subframe is
865 // still loading and updating the progress. Reset mInLoad and
866 // update the menu items.
867 mInLoad = true;
868 updateInLoadMenuItems(mCachedMenu);
869 }
870 }
John Reck30c714c2010-12-16 17:30:34 -0800871 mUi.onProgressChanged(tab);
872 }
873
874 @Override
875 public void onUpdatedLockIcon(Tab tab) {
876 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700877 }
878
879 @Override
880 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800881 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800882 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800883 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700884 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
885 return;
886 }
887 // Update the title in the history database if not in private browsing mode
888 if (!tab.isPrivateBrowsingEnabled()) {
John Reck0ebd3ac2010-12-09 11:14:04 -0800889 mDataController.updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700890 }
891 }
892
893 @Override
894 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800895 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700896 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
897 }
898
899 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800900 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
901 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700902 }
903
904 @Override
905 public boolean shouldOverrideKeyEvent(KeyEvent event) {
906 if (mMenuIsDown) {
907 // only check shortcut key when MENU is held
908 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
909 event);
910 } else {
911 return false;
912 }
913 }
914
915 @Override
916 public void onUnhandledKeyEvent(KeyEvent event) {
917 if (!isActivityPaused()) {
918 if (event.getAction() == KeyEvent.ACTION_DOWN) {
919 mActivity.onKeyDown(event.getKeyCode(), event);
920 } else {
921 mActivity.onKeyUp(event.getKeyCode(), event);
922 }
923 }
924 }
925
926 @Override
John Reck324d4402011-01-11 16:56:42 -0800927 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700928 // Don't save anything in private browsing mode
929 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800930 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700931
John Reck324d4402011-01-11 16:56:42 -0800932 if (TextUtils.isEmpty(url)
933 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700934 return;
935 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800936 mDataController.updateVisitedHistory(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700937 WebIconDatabase.getInstance().retainIconForPageUrl(url);
John Reck847b5322011-04-14 17:02:18 -0700938 if (!mActivityPaused) {
939 // Since we clear the state in onPause, don't backup the current
940 // state if we are already paused
941 mCrashRecoveryHandler.backupState();
942 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700943 }
944
945 @Override
946 public void getVisitedHistory(final ValueCallback<String[]> callback) {
947 AsyncTask<Void, Void, String[]> task =
948 new AsyncTask<Void, Void, String[]>() {
949 @Override
950 public String[] doInBackground(Void... unused) {
951 return Browser.getVisitedHistory(mActivity.getContentResolver());
952 }
953 @Override
954 public void onPostExecute(String[] result) {
955 callback.onReceiveValue(result);
956 }
957 };
958 task.execute();
959 }
960
961 @Override
962 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
963 final HttpAuthHandler handler, final String host,
964 final String realm) {
965 String username = null;
966 String password = null;
967
968 boolean reuseHttpAuthUsernamePassword
969 = handler.useHttpAuthUsernamePassword();
970
971 if (reuseHttpAuthUsernamePassword && view != null) {
972 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
973 if (credentials != null && credentials.length == 2) {
974 username = credentials[0];
975 password = credentials[1];
976 }
977 }
978
979 if (username != null && password != null) {
980 handler.proceed(username, password);
981 } else {
982 if (tab.inForeground()) {
983 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
984 } else {
985 handler.cancel();
986 }
987 }
988 }
989
990 @Override
991 public void onDownloadStart(Tab tab, String url, String userAgent,
992 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000993 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -0500994 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000995 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
996 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700997 // This Tab was opened for the sole purpose of downloading a
998 // file. Remove it.
999 if (tab == mTabControl.getCurrentTab()) {
1000 // In this case, the Tab is still on top.
1001 goBackOnePageOrQuit();
1002 } else {
1003 // In this case, it is not.
1004 closeTab(tab);
1005 }
1006 }
1007 }
1008
1009 @Override
1010 public Bitmap getDefaultVideoPoster() {
1011 return mUi.getDefaultVideoPoster();
1012 }
1013
1014 @Override
1015 public View getVideoLoadingProgressView() {
1016 return mUi.getVideoLoadingProgressView();
1017 }
1018
1019 @Override
1020 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1021 SslError error) {
1022 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1023 }
1024
Patrick Scott92066772011-03-10 08:46:27 -05001025 @Override
1026 public void showAutoLogin(Tab tab) {
1027 assert tab.inForeground();
1028 // Update the title bar to show the auto-login request.
1029 mUi.showAutoLogin(tab);
1030 }
1031
1032 @Override
1033 public void hideAutoLogin(Tab tab) {
1034 assert tab.inForeground();
1035 mUi.hideAutoLogin(tab);
1036 }
1037
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 // helper method
1039
1040 /*
1041 * Update the favorites icon if the private browsing isn't enabled and the
1042 * icon is valid.
1043 */
1044 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1045 final String url, Bitmap favicon) {
1046 if (favicon == null) {
1047 return;
1048 }
1049 if (!tab.isPrivateBrowsingEnabled()) {
1050 Bookmarks.updateFavicon(mActivity
1051 .getContentResolver(), originalUrl, url, favicon);
1052 }
1053 }
1054
Leon Scroggins4cd97792010-12-03 15:31:56 -05001055 @Override
1056 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001057 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001058 mUi.bookmarkedStatusHasChanged(tab);
1059 }
1060
Michael Kolb8233fac2010-10-26 16:08:53 -07001061 // end WebViewController
1062
1063 protected void pageUp() {
1064 getCurrentTopWebView().pageUp(false);
1065 }
1066
1067 protected void pageDown() {
1068 getCurrentTopWebView().pageDown(false);
1069 }
1070
1071 // callback from phone title bar
1072 public void editUrl() {
1073 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001074 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001075 }
1076
Michael Kolbcfa3af52010-12-14 10:36:11 -08001077 public void startVoiceSearch() {
1078 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1079 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1080 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1081 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1082 mActivity.getComponentName().flattenToString());
1083 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001084 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001085 mActivity.startActivity(intent);
1086 }
1087
Michael Kolb11d19782011-03-20 10:17:40 -07001088 @Override
1089 public void activateVoiceSearchMode(String title, List<String> results) {
1090 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001091 }
1092
1093 public void revertVoiceSearchMode(Tab tab) {
1094 mUi.revertVoiceTitleBar(tab);
1095 }
1096
Michael Kolb736990c2011-03-20 10:01:20 -07001097 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001098 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001099 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1100 }
1101
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001102 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 WebChromeClient.CustomViewCallback callback) {
1104 if (tab.inForeground()) {
1105 if (mUi.isCustomViewShowing()) {
1106 callback.onCustomViewHidden();
1107 return;
1108 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001109 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001110 // Save the menu state and set it to empty while the custom
1111 // view is showing.
1112 mOldMenuState = mMenuState;
1113 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001114 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001115 }
1116 }
1117
1118 @Override
1119 public void hideCustomView() {
1120 if (mUi.isCustomViewShowing()) {
1121 mUi.onHideCustomView();
1122 // Reset the old menu state.
1123 mMenuState = mOldMenuState;
1124 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001125 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001126 }
1127 }
1128
1129 protected void onActivityResult(int requestCode, int resultCode,
1130 Intent intent) {
1131 if (getCurrentTopWebView() == null) return;
1132 switch (requestCode) {
1133 case PREFERENCES_PAGE:
1134 if (resultCode == Activity.RESULT_OK && intent != null) {
1135 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001136 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001137 mTabControl.removeParentChildRelationShips();
1138 }
1139 }
1140 break;
1141 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001142 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001143 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001144 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001145 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001146 case AUTOFILL_SETUP:
1147 // Determine whether a profile was actually set up or not
1148 // and if so, send the message back to the WebTextView to
1149 // fill the form with the new profile.
1150 if (getSettings().getAutoFillProfile() != null) {
1151 mAutoFillSetupMessage.sendToTarget();
1152 mAutoFillSetupMessage = null;
1153 }
1154 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001155 case COMBO_VIEW:
1156 if (intent == null || resultCode != Activity.RESULT_OK) {
1157 break;
1158 }
1159 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1160 Tab t = getCurrentTab();
1161 Uri uri = intent.getData();
1162 loadUrl(t, uri.toString());
1163 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1164 String[] urls = intent.getStringArrayExtra(
1165 ComboViewActivity.EXTRA_OPEN_ALL);
1166 Tab parent = getCurrentTab();
1167 for (String url : urls) {
1168 parent = openTab(url, parent,
1169 !mSettings.openInBackground(), true);
1170 }
1171 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1172 long id = intent.getLongExtra(
1173 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1174 if (id >= 0) {
1175 createNewSnapshotTab(id, true);
1176 }
1177 }
1178 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001179 default:
1180 break;
1181 }
1182 getCurrentTopWebView().requestFocus();
1183 }
1184
1185 /**
1186 * Open the Go page.
1187 * @param startWithHistory If true, open starting on the history tab.
1188 * Otherwise, start with the bookmarks tab.
1189 */
1190 @Override
1191 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1192 if (mTabControl.getCurrentWebView() == null) {
1193 return;
1194 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001195 // clear action mode
1196 if (isInCustomActionMode()) {
1197 endActionMode();
1198 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001199 Bundle extras = new Bundle();
1200 // Disable opening in a new window if we have maxed out the windows
1201 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1202 !mTabControl.canCreateNewTab());
John Reck2bc80422011-06-30 15:11:49 -07001203 mUi.showComboView(startWithHistory
1204 ? ComboViews.History : ComboViews.Bookmarks, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001205 }
1206
1207 // combo view callbacks
1208
1209 /**
1210 * callback from ComboPage when clear history is requested
1211 */
1212 public void onRemoveParentChildRelationships() {
1213 mTabControl.removeParentChildRelationShips();
1214 }
1215
Michael Kolb8233fac2010-10-26 16:08:53 -07001216 // key handling
1217 protected void onBackKey() {
1218 if (!mUi.onBackKey()) {
1219 WebView subwindow = mTabControl.getCurrentSubWindow();
1220 if (subwindow != null) {
1221 if (subwindow.canGoBack()) {
1222 subwindow.goBack();
1223 } else {
1224 dismissSubWindow(mTabControl.getCurrentTab());
1225 }
1226 } else {
1227 goBackOnePageOrQuit();
1228 }
1229 }
1230 }
1231
Michael Kolb4bd767d2011-05-27 11:33:55 -07001232 protected boolean onMenuKey() {
1233 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001234 }
1235
Michael Kolb8233fac2010-10-26 16:08:53 -07001236 // menu handling and state
1237 // TODO: maybe put into separate handler
1238
1239 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001240 if (mMenuState == EMPTY_MENU) {
1241 return false;
1242 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001243 MenuInflater inflater = mActivity.getMenuInflater();
1244 inflater.inflate(R.menu.browser, menu);
1245 updateInLoadMenuItems(menu);
1246 // hold on to the menu reference here; it is used by the page callbacks
1247 // to update the menu based on loading state
1248 mCachedMenu = menu;
1249 return true;
1250 }
1251
1252 protected void onCreateContextMenu(ContextMenu menu, View v,
1253 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001254 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001255 return;
1256 }
1257 if (!(v instanceof WebView)) {
1258 return;
1259 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001260 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001261 WebView.HitTestResult result = webview.getHitTestResult();
1262 if (result == null) {
1263 return;
1264 }
1265
1266 int type = result.getType();
1267 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1268 Log.w(LOGTAG,
1269 "We should not show context menu when nothing is touched");
1270 return;
1271 }
1272 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1273 // let TextView handles context menu
1274 return;
1275 }
1276
1277 // Note, http://b/issue?id=1106666 is requesting that
1278 // an inflated menu can be used again. This is not available
1279 // yet, so inflate each time (yuk!)
1280 MenuInflater inflater = mActivity.getMenuInflater();
1281 inflater.inflate(R.menu.browsercontext, menu);
1282
1283 // Show the correct menu group
1284 final String extra = result.getExtra();
1285 menu.setGroupVisible(R.id.PHONE_MENU,
1286 type == WebView.HitTestResult.PHONE_TYPE);
1287 menu.setGroupVisible(R.id.EMAIL_MENU,
1288 type == WebView.HitTestResult.EMAIL_TYPE);
1289 menu.setGroupVisible(R.id.GEO_MENU,
1290 type == WebView.HitTestResult.GEO_TYPE);
1291 menu.setGroupVisible(R.id.IMAGE_MENU,
1292 type == WebView.HitTestResult.IMAGE_TYPE
1293 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1294 menu.setGroupVisible(R.id.ANCHOR_MENU,
1295 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1296 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001297 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1298 || type == WebView.HitTestResult.PHONE_TYPE
1299 || type == WebView.HitTestResult.EMAIL_TYPE
1300 || type == WebView.HitTestResult.GEO_TYPE;
1301 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1302 if (hitText) {
1303 menu.findItem(R.id.select_text_menu_id)
1304 .setOnMenuItemClickListener(new SelectText(webview));
1305 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001306 // Setup custom handling depending on the type
1307 switch (type) {
1308 case WebView.HitTestResult.PHONE_TYPE:
1309 menu.setHeaderTitle(Uri.decode(extra));
1310 menu.findItem(R.id.dial_context_menu_id).setIntent(
1311 new Intent(Intent.ACTION_VIEW, Uri
1312 .parse(WebView.SCHEME_TEL + extra)));
1313 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1314 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1315 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1316 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1317 addIntent);
1318 menu.findItem(R.id.copy_phone_context_menu_id)
1319 .setOnMenuItemClickListener(
1320 new Copy(extra));
1321 break;
1322
1323 case WebView.HitTestResult.EMAIL_TYPE:
1324 menu.setHeaderTitle(extra);
1325 menu.findItem(R.id.email_context_menu_id).setIntent(
1326 new Intent(Intent.ACTION_VIEW, Uri
1327 .parse(WebView.SCHEME_MAILTO + extra)));
1328 menu.findItem(R.id.copy_mail_context_menu_id)
1329 .setOnMenuItemClickListener(
1330 new Copy(extra));
1331 break;
1332
1333 case WebView.HitTestResult.GEO_TYPE:
1334 menu.setHeaderTitle(extra);
1335 menu.findItem(R.id.map_context_menu_id).setIntent(
1336 new Intent(Intent.ACTION_VIEW, Uri
1337 .parse(WebView.SCHEME_GEO
1338 + URLEncoder.encode(extra))));
1339 menu.findItem(R.id.copy_geo_context_menu_id)
1340 .setOnMenuItemClickListener(
1341 new Copy(extra));
1342 break;
1343
1344 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1345 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001346 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001347 // decide whether to show the open link in new tab option
1348 boolean showNewTab = mTabControl.canCreateNewTab();
1349 MenuItem newTabItem
1350 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001351 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001352 ? R.string.contextmenu_openlink_newwindow_background
1353 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001354 newTabItem.setVisible(showNewTab);
1355 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001356 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1357 newTabItem.setOnMenuItemClickListener(
1358 new MenuItem.OnMenuItemClickListener() {
1359 @Override
1360 public boolean onMenuItemClick(MenuItem item) {
1361 final HashMap<String, WebView> hrefMap =
1362 new HashMap<String, WebView>();
1363 hrefMap.put("webview", webview);
1364 final Message msg = mHandler.obtainMessage(
1365 FOCUS_NODE_HREF,
1366 R.id.open_newtab_context_menu_id,
1367 0, hrefMap);
1368 webview.requestFocusNodeHref(msg);
1369 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001370 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001371 });
1372 } else {
1373 newTabItem.setOnMenuItemClickListener(
1374 new MenuItem.OnMenuItemClickListener() {
1375 @Override
1376 public boolean onMenuItemClick(MenuItem item) {
1377 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001378 openTab(extra, parent,
1379 !mSettings.openInBackground(),
1380 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001381 return true;
1382 }
1383 });
1384 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001385 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001386 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1387 break;
1388 }
1389 // otherwise fall through to handle image part
1390 case WebView.HitTestResult.IMAGE_TYPE:
1391 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1392 menu.setHeaderTitle(extra);
1393 }
1394 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1395 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1396 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001397 setOnMenuItemClickListener(
1398 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001399 menu.findItem(R.id.set_wallpaper_context_menu_id).
1400 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1401 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001402 break;
1403
1404 default:
1405 Log.w(LOGTAG, "We should not get here.");
1406 break;
1407 }
1408 //update the ui
1409 mUi.onContextMenuCreated(menu);
1410 }
1411
1412 /**
1413 * As the menu can be open when loading state changes
1414 * we must manually update the state of the stop/reload menu
1415 * item
1416 */
1417 private void updateInLoadMenuItems(Menu menu) {
1418 if (menu == null) {
1419 return;
1420 }
1421 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1422 MenuItem src = mInLoad ?
1423 menu.findItem(R.id.stop_menu_id):
1424 menu.findItem(R.id.reload_menu_id);
1425 if (src != null) {
1426 dest.setIcon(src.getIcon());
1427 dest.setTitle(src.getTitle());
1428 }
1429 }
1430
John Reckb3417f02011-01-14 11:01:05 -08001431 boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001432 // Note: setVisible will decide whether an item is visible; while
1433 // setEnabled() will decide whether an item is enabled, which also means
1434 // whether the matching shortcut key will function.
1435 switch (mMenuState) {
1436 case EMPTY_MENU:
1437 if (mCurrentMenuState != mMenuState) {
1438 menu.setGroupVisible(R.id.MAIN_MENU, false);
1439 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1440 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1441 }
1442 break;
1443 default:
1444 if (mCurrentMenuState != mMenuState) {
1445 menu.setGroupVisible(R.id.MAIN_MENU, true);
1446 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1447 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1448 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001449 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001450 break;
1451 }
1452 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001453 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001454 }
1455
Michael Kolb4bf79712011-07-14 14:18:12 -07001456 @Override
1457 public void updateMenuState(Tab tab, Menu menu) {
1458 boolean canGoBack = false;
1459 boolean canGoForward = false;
1460 boolean isHome = false;
1461 if (tab != null) {
1462 canGoBack = tab.canGoBack();
1463 canGoForward = tab.canGoForward();
1464 isHome = mSettings.getHomePage().equals(tab.getUrl());
1465 }
1466 final MenuItem back = menu.findItem(R.id.back_menu_id);
1467 back.setEnabled(canGoBack);
1468
1469 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1470 home.setEnabled(!isHome);
1471
1472 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1473 forward.setEnabled(canGoForward);
1474
1475 final MenuItem source = menu.findItem(mInLoad ? R.id.stop_menu_id : R.id.reload_menu_id);
1476 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001477 if (source != null && dest != null) {
1478 dest.setTitle(source.getTitle());
1479 dest.setIcon(source.getIcon());
1480 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001481
1482 // decide whether to show the share link option
1483 PackageManager pm = mActivity.getPackageManager();
1484 Intent send = new Intent(Intent.ACTION_SEND);
1485 send.setType("text/plain");
1486 ResolveInfo ri = pm.resolveActivity(send,
1487 PackageManager.MATCH_DEFAULT_ONLY);
1488 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1489
1490 boolean isNavDump = mSettings.enableNavDump();
1491 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1492 nav.setVisible(isNavDump);
1493 nav.setEnabled(isNavDump);
1494
1495 boolean showDebugSettings = mSettings.isDebugEnabled();
1496 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1497 counter.setVisible(showDebugSettings);
1498 counter.setEnabled(showDebugSettings);
1499
1500 MenuItem saveSnapshot = menu.findItem(R.id.save_snapshot_menu_id);
1501 saveSnapshot.setVisible(tab != null && !tab.isSnapshot());
1502 }
1503
Michael Kolb8233fac2010-10-26 16:08:53 -07001504 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001505 if (null == getCurrentTopWebView()) {
1506 return false;
1507 }
1508 if (mMenuIsDown) {
1509 // The shortcut action consumes the MENU. Even if it is still down,
1510 // it won't trigger the next shortcut action. In the case of the
1511 // shortcut action triggering a new activity, like Bookmarks, we
1512 // won't get onKeyUp for MENU. So it is important to reset it here.
1513 mMenuIsDown = false;
1514 }
1515 switch (item.getItemId()) {
1516 // -- Main menu
1517 case R.id.new_tab_menu_id:
1518 openTabToHomePage();
1519 break;
1520
1521 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001522 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001523 break;
1524
1525 case R.id.goto_menu_id:
1526 editUrl();
1527 break;
1528
1529 case R.id.bookmarks_menu_id:
1530 bookmarksOrHistoryPicker(false);
1531 break;
1532
Michael Kolb8233fac2010-10-26 16:08:53 -07001533 case R.id.add_bookmark_menu_id:
John Reckd3e4d5b2011-07-13 15:48:43 -07001534 mActivity.startActivity(createBookmarkCurrentPageIntent(false));
Michael Kolb8233fac2010-10-26 16:08:53 -07001535 break;
1536
1537 case R.id.stop_reload_menu_id:
1538 if (mInLoad) {
1539 stopLoading();
1540 } else {
1541 getCurrentTopWebView().reload();
1542 }
1543 break;
1544
1545 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001546 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001547 break;
1548
1549 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001550 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001551 break;
1552
1553 case R.id.close_menu_id:
1554 // Close the subwindow if it exists.
1555 if (mTabControl.getCurrentSubWindow() != null) {
1556 dismissSubWindow(mTabControl.getCurrentTab());
1557 break;
1558 }
1559 closeCurrentTab();
1560 break;
1561
1562 case R.id.homepage_menu_id:
1563 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001564 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001565 break;
1566
1567 case R.id.preferences_menu_id:
1568 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1569 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1570 getCurrentTopWebView().getUrl());
1571 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1572 break;
1573
1574 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001575 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001576 break;
1577
John Reck2bc80422011-06-30 15:11:49 -07001578 case R.id.save_snapshot_menu_id:
1579 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001580 if (source == null) break;
John Reckd8c74522011-06-14 08:45:00 -07001581 final ContentResolver cr = mActivity.getContentResolver();
1582 final ContentValues values = source.createSnapshotValues();
John Reck2bc80422011-06-30 15:11:49 -07001583 if (values != null) {
1584 new AsyncTask<Tab, Void, Long>() {
John Reckd8c74522011-06-14 08:45:00 -07001585
John Reck2bc80422011-06-30 15:11:49 -07001586 @Override
1587 protected Long doInBackground(Tab... params) {
1588 Uri result = cr.insert(Snapshots.CONTENT_URI, values);
1589 long id = ContentUris.parseId(result);
1590 return id;
John Reckd8c74522011-06-14 08:45:00 -07001591 }
John Reckf33b1632011-06-04 20:00:23 -07001592
John Reck2bc80422011-06-30 15:11:49 -07001593 @Override
1594 protected void onPostExecute(Long id) {
1595 Bundle b = new Bundle();
1596 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1597 mUi.showComboView(ComboViews.Snapshots, b);
1598 };
1599 }.execute(source);
1600 } else {
1601 Toast.makeText(mActivity, R.string.snapshot_failed,
Leon Scrogginsac993842011-02-02 12:54:07 -05001602 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001603 }
Leon Scrogginsac993842011-02-02 12:54:07 -05001604 break;
1605
Michael Kolb8233fac2010-10-26 16:08:53 -07001606 case R.id.page_info_menu_id:
Huahui Wuae0c0412011-06-28 10:17:05 -07001607 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
Michael Kolb8233fac2010-10-26 16:08:53 -07001608 break;
1609
1610 case R.id.classic_history_menu_id:
1611 bookmarksOrHistoryPicker(true);
1612 break;
1613
1614 case R.id.title_bar_share_page_url:
1615 case R.id.share_page_menu_id:
1616 Tab currentTab = mTabControl.getCurrentTab();
1617 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001618 return false;
1619 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001620 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001621 break;
1622
1623 case R.id.dump_nav_menu_id:
1624 getCurrentTopWebView().debugDump();
1625 break;
1626
1627 case R.id.dump_counters_menu_id:
1628 getCurrentTopWebView().dumpV8Counters();
1629 break;
1630
1631 case R.id.zoom_in_menu_id:
1632 getCurrentTopWebView().zoomIn();
1633 break;
1634
1635 case R.id.zoom_out_menu_id:
1636 getCurrentTopWebView().zoomOut();
1637 break;
1638
1639 case R.id.view_downloads_menu_id:
1640 viewDownloads();
1641 break;
1642
1643 case R.id.window_one_menu_id:
1644 case R.id.window_two_menu_id:
1645 case R.id.window_three_menu_id:
1646 case R.id.window_four_menu_id:
1647 case R.id.window_five_menu_id:
1648 case R.id.window_six_menu_id:
1649 case R.id.window_seven_menu_id:
1650 case R.id.window_eight_menu_id:
1651 {
1652 int menuid = item.getItemId();
1653 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1654 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1655 Tab desiredTab = mTabControl.getTab(id);
1656 if (desiredTab != null &&
1657 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001658 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001659 }
1660 break;
1661 }
1662 }
1663 }
1664 break;
1665
1666 default:
1667 return false;
1668 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001669 return true;
1670 }
1671
1672 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001673 // Let the History and Bookmark fragments handle menus they created.
1674 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1675 return false;
1676 }
1677
Michael Kolb8233fac2010-10-26 16:08:53 -07001678 int id = item.getItemId();
1679 boolean result = true;
1680 switch (id) {
1681 // For the context menu from the title bar
1682 case R.id.title_bar_copy_page_url:
1683 Tab currentTab = mTabControl.getCurrentTab();
1684 if (null == currentTab) {
1685 result = false;
1686 break;
1687 }
1688 WebView mainView = currentTab.getWebView();
1689 if (null == mainView) {
1690 result = false;
1691 break;
1692 }
1693 copy(mainView.getUrl());
1694 break;
1695 // -- Browser context menu
1696 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001697 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001698 case R.id.copy_link_context_menu_id:
1699 final WebView webView = getCurrentTopWebView();
1700 if (null == webView) {
1701 result = false;
1702 break;
1703 }
1704 final HashMap<String, WebView> hrefMap =
1705 new HashMap<String, WebView>();
1706 hrefMap.put("webview", webView);
1707 final Message msg = mHandler.obtainMessage(
1708 FOCUS_NODE_HREF, id, 0, hrefMap);
1709 webView.requestFocusNodeHref(msg);
1710 break;
1711
1712 default:
1713 // For other context menus
1714 result = onOptionsItemSelected(item);
1715 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001716 return result;
1717 }
1718
1719 /**
1720 * support programmatically opening the context menu
1721 */
1722 public void openContextMenu(View view) {
1723 mActivity.openContextMenu(view);
1724 }
1725
1726 /**
1727 * programmatically open the options menu
1728 */
1729 public void openOptionsMenu() {
1730 mActivity.openOptionsMenu();
1731 }
1732
1733 public boolean onMenuOpened(int featureId, Menu menu) {
1734 if (mOptionsMenuOpen) {
1735 if (mConfigChanged) {
1736 // We do not need to make any changes to the state of the
1737 // title bar, since the only thing that happened was a
1738 // change in orientation
1739 mConfigChanged = false;
1740 } else {
1741 if (!mExtendedMenuOpen) {
1742 mExtendedMenuOpen = true;
1743 mUi.onExtendedMenuOpened();
1744 } else {
1745 // Switching the menu back to icon view, so show the
1746 // title bar once again.
1747 mExtendedMenuOpen = false;
1748 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001749 }
1750 }
1751 } else {
1752 // The options menu is closed, so open it, and show the title
1753 mOptionsMenuOpen = true;
1754 mConfigChanged = false;
1755 mExtendedMenuOpen = false;
1756 mUi.onOptionsMenuOpened();
1757 }
1758 return true;
1759 }
1760
1761 public void onOptionsMenuClosed(Menu menu) {
1762 mOptionsMenuOpen = false;
1763 mUi.onOptionsMenuClosed(mInLoad);
1764 }
1765
1766 public void onContextMenuClosed(Menu menu) {
1767 mUi.onContextMenuClosed(menu, mInLoad);
1768 }
1769
1770 // Helper method for getting the top window.
1771 @Override
1772 public WebView getCurrentTopWebView() {
1773 return mTabControl.getCurrentTopWebView();
1774 }
1775
1776 @Override
1777 public WebView getCurrentWebView() {
1778 return mTabControl.getCurrentWebView();
1779 }
1780
1781 /*
1782 * This method is called as a result of the user selecting the options
1783 * menu to see the download window. It shows the download window on top of
1784 * the current window.
1785 */
1786 void viewDownloads() {
1787 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1788 mActivity.startActivity(intent);
1789 }
1790
1791 // action mode
1792
1793 void onActionModeStarted(ActionMode mode) {
1794 mUi.onActionModeStarted(mode);
1795 mActionMode = mode;
1796 }
1797
1798 /*
1799 * True if a custom ActionMode (i.e. find or select) is in use.
1800 */
1801 @Override
1802 public boolean isInCustomActionMode() {
1803 return mActionMode != null;
1804 }
1805
1806 /*
1807 * End the current ActionMode.
1808 */
1809 @Override
1810 public void endActionMode() {
1811 if (mActionMode != null) {
1812 mActionMode.finish();
1813 }
1814 }
1815
1816 /*
1817 * Called by find and select when they are finished. Replace title bars
1818 * as necessary.
1819 */
1820 public void onActionModeFinished(ActionMode mode) {
1821 if (!isInCustomActionMode()) return;
1822 mUi.onActionModeFinished(mInLoad);
1823 mActionMode = null;
1824 }
1825
1826 boolean isInLoad() {
1827 return mInLoad;
1828 }
1829
1830 // bookmark handling
1831
1832 /**
1833 * add the current page as a bookmark to the given folder id
1834 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07001835 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001836 * bookmarked, and if it is, edit that bookmark. If false, and
1837 * the site is already bookmarked, do not attempt to edit the
1838 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001839 */
1840 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07001841 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001842 Intent i = new Intent(mActivity,
1843 AddBookmarkPage.class);
1844 WebView w = getCurrentTopWebView();
1845 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1846 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1847 String touchIconUrl = w.getTouchIconUrl();
1848 if (touchIconUrl != null) {
1849 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1850 WebSettings settings = w.getSettings();
1851 if (settings != null) {
1852 i.putExtra(AddBookmarkPage.USER_AGENT,
1853 settings.getUserAgentString());
1854 }
1855 }
1856 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1857 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1858 getDesiredThumbnailHeight(mActivity)));
1859 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07001860 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001861 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1862 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001863 // Put the dialog at the upper right of the screen, covering the
1864 // star on the title bar.
1865 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07001866 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07001867 }
1868
1869 // file chooser
1870 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1871 mUploadHandler = new UploadHandler(this);
1872 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1873 }
1874
1875 // thumbnails
1876
1877 /**
1878 * Return the desired width for thumbnail screenshots, which are stored in
1879 * the database, and used on the bookmarks screen.
1880 * @param context Context for finding out the density of the screen.
1881 * @return desired width for thumbnail screenshot.
1882 */
1883 static int getDesiredThumbnailWidth(Context context) {
1884 return context.getResources().getDimensionPixelOffset(
1885 R.dimen.bookmarkThumbnailWidth);
1886 }
1887
1888 /**
1889 * Return the desired height for thumbnail screenshots, which are stored in
1890 * the database, and used on the bookmarks screen.
1891 * @param context Context for finding out the density of the screen.
1892 * @return desired height for thumbnail screenshot.
1893 */
1894 static int getDesiredThumbnailHeight(Context context) {
1895 return context.getResources().getDimensionPixelOffset(
1896 R.dimen.bookmarkThumbnailHeight);
1897 }
1898
Michael Kolb1acef692011-03-08 14:12:06 -08001899 static Bitmap createScreenshot(Tab tab, int width, int height) {
1900 if ((tab != null) && (tab.getWebView() != null)) {
1901 return createScreenshot(tab.getWebView(), width, height);
1902 }
1903 return null;
1904 }
1905
John Reck8cc92352011-07-06 17:41:52 -07001906 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08001907 // We render to a bitmap 2x the desired size so that we can then
1908 // re-scale it with filtering since canvas.scale doesn't filter
1909 // This helps reduce aliasing at the cost of being slightly blurry
1910 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07001911 Picture thumbnail = view.capturePicture();
1912 if (thumbnail == null) {
1913 return null;
1914 }
John Reck5c6ac2f2011-01-05 10:18:03 -08001915 width *= filter_scale;
1916 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07001917 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1918 Canvas canvas = new Canvas(bm);
1919 // May need to tweak these values to determine what is the
1920 // best scale factor
1921 int thumbnailWidth = thumbnail.getWidth();
1922 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08001923 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08001924 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08001925 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07001926 } else {
1927 return null;
1928 }
John Reckfe49ab42010-11-16 17:09:37 -08001929
Michael Kolbeb95db42011-03-03 10:38:40 -08001930 float scaleFactorY = (float) height / (float)thumbnailHeight;
1931 if (scaleFactorY > scaleFactor) {
1932 // The picture is narrower than the requested AR
1933 // Center the thumnail and crop the sides
1934 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08001935 float wx = (thumbnailWidth * scaleFactor) - width;
1936 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07001937 }
1938
John Reckfe49ab42010-11-16 17:09:37 -08001939 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07001940
1941 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08001942 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
1943 height / filter_scale, true);
1944 bm.recycle();
1945 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07001946 }
1947
John Reck34ef2672011-02-10 11:30:55 -08001948 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001949 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07001950 // FIXME: Would like to make sure there is actually something to
1951 // draw, but the API for that (WebViewCore.pictureReady()) is not
1952 // currently accessible here.
1953
John Reck34ef2672011-02-10 11:30:55 -08001954 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08001955 if (view == null) {
1956 // Tab was destroyed
1957 return;
1958 }
John Reck34ef2672011-02-10 11:30:55 -08001959 final String url = tab.getUrl();
1960 final String originalUrl = view.getOriginalUrl();
1961
1962 if (TextUtils.isEmpty(url)) {
1963 return;
1964 }
1965
1966 // Only update thumbnails for web urls (http(s)://), not for
1967 // about:, javascript:, data:, etc...
1968 // Unless it is a bookmarked site, then always update
1969 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
1970 return;
1971 }
1972
Michael Kolb8233fac2010-10-26 16:08:53 -07001973 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
1974 getDesiredThumbnailHeight(mActivity));
1975 if (bm == null) {
1976 return;
1977 }
1978
1979 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08001980 new AsyncTask<Void, Void, Void>() {
1981 @Override
1982 protected Void doInBackground(Void... unused) {
1983 Cursor cursor = null;
1984 try {
1985 // TODO: Clean this up
1986 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
1987 if (cursor != null && cursor.moveToFirst()) {
1988 final ByteArrayOutputStream os =
1989 new ByteArrayOutputStream();
1990 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07001991
John Reck34ef2672011-02-10 11:30:55 -08001992 ContentValues values = new ContentValues();
1993 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07001994
John Reck34ef2672011-02-10 11:30:55 -08001995 do {
John Reck617fd832011-02-16 14:35:59 -08001996 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08001997 cr.update(Images.CONTENT_URI, values, null, null);
1998 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07001999 }
John Reck34ef2672011-02-10 11:30:55 -08002000 } catch (IllegalStateException e) {
2001 // Ignore
2002 } finally {
2003 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002004 }
John Reck34ef2672011-02-10 11:30:55 -08002005 return null;
2006 }
2007 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002008 }
2009
2010 private class Copy implements OnMenuItemClickListener {
2011 private CharSequence mText;
2012
2013 public boolean onMenuItemClick(MenuItem item) {
2014 copy(mText);
2015 return true;
2016 }
2017
2018 public Copy(CharSequence toCopy) {
2019 mText = toCopy;
2020 }
2021 }
2022
Leon Scroggins63c02662010-11-18 15:16:27 -05002023 private static class Download implements OnMenuItemClickListener {
2024 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002025 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002026 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002027
2028 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002029 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002030 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002031 return true;
2032 }
2033
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002034 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002035 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002036 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002037 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002038 }
2039 }
2040
Cary Clark8974d282010-11-22 10:46:05 -05002041 private static class SelectText implements OnMenuItemClickListener {
2042 private WebView mWebView;
2043
2044 public boolean onMenuItemClick(MenuItem item) {
2045 if (mWebView != null) {
2046 return mWebView.selectText();
2047 }
2048 return false;
2049 }
2050
2051 public SelectText(WebView webView) {
2052 mWebView = webView;
2053 }
2054
2055 }
2056
Michael Kolb8233fac2010-10-26 16:08:53 -07002057 /********************** TODO: UI stuff *****************************/
2058
2059 // these methods have been copied, they still need to be cleaned up
2060
2061 /****************** tabs ***************************************************/
2062
2063 // basic tab interactions:
2064
2065 // it is assumed that tabcontrol already knows about the tab
2066 protected void addTab(Tab tab) {
2067 mUi.addTab(tab);
2068 }
2069
2070 protected void removeTab(Tab tab) {
2071 mUi.removeTab(tab);
2072 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002073 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002074 }
2075
Michael Kolbf2055602011-04-09 17:20:03 -07002076 @Override
2077 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002078 // monkey protection against delayed start
2079 if (tab != null) {
2080 mTabControl.setCurrentTab(tab);
2081 // the tab is guaranteed to have a webview after setCurrentTab
2082 mUi.setActiveTab(tab);
2083 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002084 }
2085
2086 protected void closeEmptyChildTab() {
2087 Tab current = mTabControl.getCurrentTab();
2088 if (current != null
2089 && current.getWebView().copyBackForwardList().getSize() == 0) {
Michael Kolbc831b632011-05-11 09:30:34 -07002090 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002091 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002092 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002093 closeTab(current);
2094 }
2095 }
2096 }
2097
John Reck26b18322011-06-21 13:08:58 -07002098 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002099 // Dismiss the subwindow if applicable.
2100 dismissSubWindow(appTab);
2101 // Since we might kill the WebView, remove it from the
2102 // content view first.
2103 mUi.detachTab(appTab);
2104 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002105 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002106 // TODO: analyze why the remove and add are necessary
2107 mUi.attachTab(appTab);
2108 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002109 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002110 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002111 } else {
2112 // If the tab was the current tab, we have to attach
2113 // it to the view system again.
2114 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002115 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002116 }
2117 }
2118
2119 // Remove the sub window if it exists. Also called by TabControl when the
2120 // user clicks the 'X' to dismiss a sub window.
2121 public void dismissSubWindow(Tab tab) {
2122 removeSubWindow(tab);
2123 // dismiss the subwindow. This will destroy the WebView.
2124 tab.dismissSubWindow();
2125 getCurrentTopWebView().requestFocus();
2126 }
2127
2128 @Override
2129 public void removeSubWindow(Tab t) {
2130 if (t.getSubWebView() != null) {
2131 mUi.removeSubWindow(t.getSubViewContainer());
2132 }
2133 }
2134
2135 @Override
2136 public void attachSubWindow(Tab tab) {
2137 if (tab.getSubWebView() != null) {
2138 mUi.attachSubWindow(tab.getSubViewContainer());
2139 getCurrentTopWebView().requestFocus();
2140 }
2141 }
2142
Mathew Inwood29721c22011-06-29 17:55:24 +01002143 private Tab showPreloadedTab(final UrlData urlData) {
2144 if (!urlData.isPreloaded()) {
2145 return null;
2146 }
2147 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2148 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2149 if (sbQuery != null) {
2150 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
2151 // Could not submit query. Fallback to regular tab creation
2152 tabControl.destroy();
2153 return null;
2154 }
2155 }
2156 Tab t = tabControl.getTab();
2157 mTabControl.addPreloadedTab(t);
2158 addTab(t);
2159 setActiveTab(t);
2160 return t;
2161 }
2162
Michael Kolb7bcafde2011-05-09 13:55:59 -07002163 // open a non inconito tab with the given url data
2164 // and set as active tab
2165 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002166 Tab tab = showPreloadedTab(urlData);
2167 if (tab == null) {
2168 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002169 if ((tab != null) && !urlData.isEmpty()) {
2170 loadUrlDataIn(tab, urlData);
2171 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002172 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002173 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002174 }
2175
Michael Kolb843510f2010-12-09 10:51:49 -08002176 @Override
2177 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002178 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002179 }
2180
Michael Kolb8233fac2010-10-26 16:08:53 -07002181 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002182 public Tab openIncognitoTab() {
2183 return openTab(INCOGNITO_URI, true, true, false);
2184 }
2185
2186 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002187 public Tab openTab(String url, boolean incognito, boolean setActive,
2188 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002189 return openTab(url, incognito, setActive, useCurrent, null);
2190 }
2191
2192 @Override
2193 public Tab openTab(String url, Tab parent, boolean setActive,
2194 boolean useCurrent) {
2195 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2196 setActive, useCurrent, parent);
2197 }
2198
2199 public Tab openTab(String url, boolean incognito, boolean setActive,
2200 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002201 Tab tab = createNewTab(incognito, setActive, useCurrent);
2202 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002203 if (parent != null && parent != tab) {
2204 parent.addChildTab(tab);
2205 }
Michael Kolb519d2282011-05-09 17:03:19 -07002206 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002207 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002208 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002209 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002210 return tab;
2211 }
2212
2213 // this method will attempt to create a new tab
2214 // incognito: private browsing tab
2215 // setActive: ste tab as current tab
2216 // useCurrent: if no new tab can be created, return current tab
2217 private Tab createNewTab(boolean incognito, boolean setActive,
2218 boolean useCurrent) {
2219 Tab tab = null;
2220 if (mTabControl.canCreateNewTab()) {
2221 tab = mTabControl.createNewTab(incognito);
2222 addTab(tab);
2223 if (setActive) {
2224 setActiveTab(tab);
2225 }
2226 } else {
2227 if (useCurrent) {
2228 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002229 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002230 } else {
2231 mUi.showMaxTabsWarning();
2232 }
2233 }
2234 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002235 }
2236
John Reck2bc80422011-06-30 15:11:49 -07002237 @Override
2238 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2239 SnapshotTab tab = null;
2240 if (mTabControl.canCreateNewTab()) {
2241 tab = mTabControl.createSnapshotTab(snapshotId);
2242 addTab(tab);
2243 if (setActive) {
2244 setActiveTab(tab);
2245 }
2246 } else {
2247 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002248 }
2249 return tab;
2250 }
2251
Michael Kolb8233fac2010-10-26 16:08:53 -07002252 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002253 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002254 * @return boolean True if we successfully switched to a different tab. If
2255 * the indexth tab is null, or if that tab is the same as
2256 * the current one, return false.
2257 */
2258 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002259 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002260 Tab currentTab = mTabControl.getCurrentTab();
2261 if (tab == null || tab == currentTab) {
2262 return false;
2263 }
2264 setActiveTab(tab);
2265 return true;
2266 }
2267
2268 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002269 public void closeCurrentTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002270 if (mTabControl.getTabCount() == 1) {
John Reckf630b552011-07-17 14:18:51 -07002271 CrashRecoveryHandler.clearState(mActivity);
John Reck958b2422010-12-03 17:56:17 -08002272 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002273 return;
2274 }
Michael Kolbc831b632011-05-11 09:30:34 -07002275 final Tab current = mTabControl.getCurrentTab();
2276 final int pos = mTabControl.getCurrentPosition();
2277 Tab newTab = current.getParent();
2278 if (newTab == null) {
2279 newTab = mTabControl.getTab(pos + 1);
2280 if (newTab == null) {
2281 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002282 }
2283 }
Michael Kolbc831b632011-05-11 09:30:34 -07002284 if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002285 // Close window
2286 closeTab(current);
2287 }
2288 }
2289
2290 /**
2291 * Close the tab, remove its associated title bar, and adjust mTabControl's
2292 * current tab to a valid value.
2293 */
2294 @Override
2295 public void closeTab(Tab tab) {
Michael Kolb2d59c322011-01-25 13:18:55 -08002296 removeTab(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002297 }
2298
Michael Kolb8233fac2010-10-26 16:08:53 -07002299 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002300 protected void loadUrlFromContext(String url) {
2301 Tab tab = getCurrentTab();
2302 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002303 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002304 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002305 url = UrlUtils.smartUrlFilter(url);
2306 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002307 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002308 }
2309 }
2310 }
2311
2312 /**
2313 * Load the URL into the given WebView and update the title bar
2314 * to reflect the new load. Call this instead of WebView.loadUrl
2315 * directly.
2316 * @param view The WebView used to load url.
2317 * @param url The URL to load.
2318 */
John Reck71e51422011-07-01 16:49:28 -07002319 @Override
2320 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002321 loadUrl(tab, url, null);
2322 }
2323
2324 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2325 if (tab != null) {
2326 dismissSubWindow(tab);
2327 tab.loadUrl(url, headers);
2328 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002329 }
2330
2331 /**
2332 * Load UrlData into a Tab and update the title bar to reflect the new
2333 * load. Call this instead of UrlData.loadIn directly.
2334 * @param t The Tab used to load.
2335 * @param data The UrlData being loaded.
2336 */
2337 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002338 if (data != null) {
2339 if (data.mVoiceIntent != null) {
2340 t.activateVoiceSearchMode(data.mVoiceIntent);
Michael Kolb14612442011-06-24 13:06:29 -07002341 } else if (data.isPreloaded()) {
2342 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002343 } else {
2344 loadUrl(t, data.mUrl, data.mHeaders);
2345 }
2346 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002347 }
2348
John Reck30c714c2010-12-16 17:30:34 -08002349 @Override
2350 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002351 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002352 if (tab.canGoBack()) {
2353 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002354 } else {
John Reckef654f12011-07-12 16:42:08 -07002355 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002356 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002357 }
2358
2359 void goBackOnePageOrQuit() {
2360 Tab current = mTabControl.getCurrentTab();
2361 if (current == null) {
2362 /*
2363 * Instead of finishing the activity, simply push this to the back
2364 * of the stack and let ActivityManager to choose the foreground
2365 * activity. As BrowserActivity is singleTask, it will be always the
2366 * root of the task. So we can use either true or false for
2367 * moveTaskToBack().
2368 */
2369 mActivity.moveTaskToBack(true);
2370 return;
2371 }
John Reckef654f12011-07-12 16:42:08 -07002372 if (current.canGoBack()) {
2373 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002374 } else {
2375 // Check to see if we are closing a window that was created by
2376 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002377 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002378 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002379 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002380 // Now we close the other tab
2381 closeTab(current);
2382 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002383 /*
2384 * Instead of finishing the activity, simply push this to the back
2385 * of the stack and let ActivityManager to choose the foreground
2386 * activity. As BrowserActivity is singleTask, it will be always the
2387 * root of the task. So we can use either true or false for
2388 * moveTaskToBack().
2389 */
2390 mActivity.moveTaskToBack(true);
2391 }
2392 }
2393 }
2394
2395 /**
2396 * Feed the previously stored results strings to the BrowserProvider so that
2397 * the SearchDialog will show them instead of the standard searches.
2398 * @param result String to show on the editable line of the SearchDialog.
2399 */
2400 @Override
2401 public void showVoiceSearchResults(String result) {
2402 ContentProviderClient client = mActivity.getContentResolver()
2403 .acquireContentProviderClient(Browser.BOOKMARKS_URI);
2404 ContentProvider prov = client.getLocalContentProvider();
2405 BrowserProvider bp = (BrowserProvider) prov;
2406 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
2407 client.release();
2408
2409 Bundle bundle = createGoogleSearchSourceBundle(
2410 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
2411 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
2412 startSearch(result, false, bundle, false);
2413 }
2414
2415 private void startSearch(String initialQuery, boolean selectInitialQuery,
2416 Bundle appSearchData, boolean globalSearch) {
2417 if (appSearchData == null) {
2418 appSearchData = createGoogleSearchSourceBundle(
2419 GOOGLE_SEARCH_SOURCE_TYPE);
2420 }
2421
2422 SearchEngine searchEngine = mSettings.getSearchEngine();
2423 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
2424 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
2425 }
2426 mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData,
2427 globalSearch);
2428 }
2429
2430 private Bundle createGoogleSearchSourceBundle(String source) {
2431 Bundle bundle = new Bundle();
2432 bundle.putString(Search.SOURCE, source);
2433 return bundle;
2434 }
2435
2436 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002437 * helper method for key handler
2438 * returns the current tab if it can't advance
2439 */
Michael Kolbc831b632011-05-11 09:30:34 -07002440 private Tab getNextTab() {
2441 return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1,
2442 mTabControl.getCurrentPosition() + 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002443 }
2444
2445 /**
2446 * helper method for key handler
2447 * returns the current tab if it can't advance
2448 */
Michael Kolbc831b632011-05-11 09:30:34 -07002449 private Tab getPrevTab() {
2450 return mTabControl.getTab(Math.max(0,
2451 mTabControl.getCurrentPosition() - 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002452 }
2453
2454 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002455 * handle key events in browser
2456 *
2457 * @param keyCode
2458 * @param event
2459 * @return true if handled, false to pass to super
2460 */
2461 boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002462 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002463 // Even if MENU is already held down, we need to call to super to open
2464 // the IME on long press.
Michael Kolb2814a362011-05-19 15:49:41 -07002465 if (KeyEvent.KEYCODE_MENU == keyCode) {
John Reckd3e4d5b2011-07-13 15:48:43 -07002466 event.startTracking();
2467 return true;
Michael Kolb2814a362011-05-19 15:49:41 -07002468 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002469 if (!noModifiers
2470 && ((KeyEvent.KEYCODE_MENU == keyCode)
2471 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2472 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002473 mMenuIsDown = true;
2474 return false;
2475 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002476
Cary Clark8ff8c662010-12-29 15:03:05 -05002477 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002478 Tab tab = getCurrentTab();
2479 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002480
Cary Clark160bbb92011-01-10 11:17:07 -05002481 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2482 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002483
Michael Kolb8233fac2010-10-26 16:08:53 -07002484 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002485 case KeyEvent.KEYCODE_TAB:
2486 if (event.isCtrlPressed()) {
2487 if (event.isShiftPressed()) {
2488 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002489 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002490 } else {
2491 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002492 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002493 }
2494 return true;
2495 }
2496 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002497 case KeyEvent.KEYCODE_SPACE:
2498 // WebView/WebTextView handle the keys in the KeyDown. As
2499 // the Activity's shortcut keys are only handled when WebView
2500 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002501 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002502 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002503 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002504 pageDown();
2505 }
2506 return true;
2507 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002508 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002509 event.startTracking();
2510 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002511 case KeyEvent.KEYCODE_FORWARD:
2512 if (!noModifiers) break;
2513 tab.goForward();
2514 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002515 case KeyEvent.KEYCODE_DPAD_LEFT:
2516 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002517 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002518 return true;
2519 }
2520 break;
2521 case KeyEvent.KEYCODE_DPAD_RIGHT:
2522 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002523 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002524 return true;
2525 }
2526 break;
2527 case KeyEvent.KEYCODE_A:
2528 if (ctrl) {
2529 webView.selectAll();
2530 return true;
2531 }
2532 break;
Michael Kolba4183062011-01-16 10:43:21 -08002533// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002534 case KeyEvent.KEYCODE_C:
2535 if (ctrl) {
2536 webView.copySelection();
2537 return true;
2538 }
2539 break;
Michael Kolba4183062011-01-16 10:43:21 -08002540// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002541// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002542// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002543// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002544// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002545// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002546// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002547// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002548// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002549// case KeyEvent.KEYCODE_M: // unused
2550// case KeyEvent.KEYCODE_N: // in Chrome: new window
2551// case KeyEvent.KEYCODE_O: // in Chrome: open file
2552// case KeyEvent.KEYCODE_P: // in Chrome: print page
2553// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002554// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002555// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2556 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002557 // we can't use the ctrl/shift flags, they check for
2558 // exclusive use of a modifier
2559 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002560 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002561 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002562 } else {
2563 openTabToHomePage();
2564 }
2565 return true;
2566 }
2567 break;
2568// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2569// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb1a2eba42011-03-16 16:42:49 -07002570 case KeyEvent.KEYCODE_W: // in Chrome: close tab
2571 if (ctrl) {
2572 closeCurrentTab();
2573 return true;
2574 }
2575 break;
Cary Clark8ff8c662010-12-29 15:03:05 -05002576// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2577// case KeyEvent.KEYCODE_Y: // unused
2578// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002579 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002580 // it is a regular key and webview is not null
2581 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002582 }
2583
John Recke6bf4ab2011-02-24 15:48:05 -08002584 boolean onKeyLongPress(int keyCode, KeyEvent event) {
2585 switch(keyCode) {
2586 case KeyEvent.KEYCODE_BACK:
2587 if (mUi.showsWeb()) {
2588 bookmarksOrHistoryPicker(true);
2589 return true;
2590 }
2591 break;
2592 }
2593 return false;
2594 }
2595
Michael Kolb8233fac2010-10-26 16:08:53 -07002596 boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb2814a362011-05-19 15:49:41 -07002597 if (KeyEvent.KEYCODE_MENU == keyCode) {
2598 mMenuIsDown = false;
2599 if (event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002600 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002601 }
2602 }
Cary Clark160bbb92011-01-10 11:17:07 -05002603 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002604 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002605 case KeyEvent.KEYCODE_BACK:
2606 if (event.isTracking() && !event.isCanceled()) {
2607 onBackKey();
2608 return true;
2609 }
2610 break;
2611 }
2612 return false;
2613 }
2614
2615 public boolean isMenuDown() {
2616 return mMenuIsDown;
2617 }
2618
Ben Murdoch8029a772010-11-16 11:58:21 +00002619 public void setupAutoFill(Message message) {
2620 // Open the settings activity at the AutoFill profile fragment so that
2621 // the user can create a new profile. When they return, we will dispatch
2622 // the message so that we can autofill the form using their new profile.
2623 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2624 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2625 AutoFillSettingsFragment.class.getName());
2626 mAutoFillSetupMessage = message;
2627 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2628 }
John Reckb3417f02011-01-14 11:01:05 -08002629
2630 @Override
Narayan Kamath5119edd2011-02-23 15:49:17 +00002631 public void registerDropdownChangeListener(DropdownChangeListener d) {
2632 mUi.registerDropdownChangeListener(d);
2633 }
Michael Kolbfbc579a2011-07-07 15:59:33 -07002634
2635 public boolean onSearchRequested() {
2636 mUi.editUrl(false);
2637 return true;
2638 }
2639
Michael Kolb8233fac2010-10-26 16:08:53 -07002640}