blob: b3be61873a1ed55d10728e8acdfd4d6eb4ad9b71 [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;
John Reck30b065e2011-07-19 10:58:05 -070033import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050034import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070035import android.database.Cursor;
36import android.database.sqlite.SQLiteDatabase;
Michael Kolb8233fac2010-10-26 16:08:53 -070037import android.graphics.Bitmap;
38import android.graphics.Canvas;
39import android.graphics.Picture;
40import android.net.Uri;
41import android.net.http.SslError;
42import android.os.AsyncTask;
43import android.os.Bundle;
44import android.os.Handler;
45import android.os.Message;
46import android.os.PowerManager;
47import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000048import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070049import android.provider.Browser;
50import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.provider.BrowserContract.Images;
52import android.provider.ContactsContract;
53import android.provider.ContactsContract.Intents.Insert;
Michael Kolbcfa3af52010-12-14 10:36:11 -080054import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070055import android.text.TextUtils;
56import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080057import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070058import android.view.ActionMode;
59import android.view.ContextMenu;
60import android.view.ContextMenu.ContextMenuInfo;
61import android.view.Gravity;
62import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070063import android.view.Menu;
64import android.view.MenuInflater;
65import android.view.MenuItem;
66import android.view.MenuItem.OnMenuItemClickListener;
67import android.view.View;
68import android.webkit.CookieManager;
69import android.webkit.CookieSyncManager;
70import android.webkit.HttpAuthHandler;
71import android.webkit.SslErrorHandler;
72import android.webkit.ValueCallback;
73import android.webkit.WebChromeClient;
74import android.webkit.WebIconDatabase;
75import android.webkit.WebSettings;
76import android.webkit.WebView;
Leon Scrogginsac993842011-02-02 12:54:07 -050077import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070078
Michael Kolb4bd767d2011-05-27 11:33:55 -070079import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070080import com.android.browser.UI.ComboViews;
Michael Kolb4bd767d2011-05-27 11:33:55 -070081import com.android.browser.UI.DropdownChangeListener;
82import com.android.browser.provider.BrowserProvider;
John Reck8cc92352011-07-06 17:41:52 -070083import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070084import com.android.browser.search.SearchEngine;
85import com.android.common.Search;
86
Michael Kolb8233fac2010-10-26 16:08:53 -070087import java.io.ByteArrayOutputStream;
88import java.io.File;
89import java.net.URLEncoder;
90import java.util.Calendar;
91import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080092import java.util.List;
John Reck26b18322011-06-21 13:08:58 -070093import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -070094
95/**
96 * Controller for browser
97 */
98public class Controller
99 implements WebViewController, UiController {
100
101 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800102 private static final String SEND_APP_ID_EXTRA =
103 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700104 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800105
Michael Kolb8233fac2010-10-26 16:08:53 -0700106
107 // public message ids
108 public final static int LOAD_URL = 1001;
109 public final static int STOP_LOAD = 1002;
110
111 // Message Ids
112 private static final int FOCUS_NODE_HREF = 102;
113 private static final int RELEASE_WAKELOCK = 107;
114
115 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
116
117 private static final int OPEN_BOOKMARKS = 201;
118
119 private static final int EMPTY_MENU = -1;
120
Michael Kolb8233fac2010-10-26 16:08:53 -0700121 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700122 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700123 final static int PREFERENCES_PAGE = 3;
124 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000125 final static int AUTOFILL_SETUP = 5;
126
Michael Kolb8233fac2010-10-26 16:08:53 -0700127 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
128
129 // As the ids are dynamically created, we can't guarantee that they will
130 // be in sequence, so this static array maps ids to a window number.
131 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
132 { R.id.window_one_menu_id, R.id.window_two_menu_id,
133 R.id.window_three_menu_id, R.id.window_four_menu_id,
134 R.id.window_five_menu_id, R.id.window_six_menu_id,
135 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
136
137 // "source" parameter for Google search through search key
138 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
139 // "source" parameter for Google search through simplily type
140 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
141
Guang Zhu9e78f512011-05-04 11:45:11 -0700142 // "no-crash-recovery" parameter in intetnt to suppress crash recovery
143 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
144
Michael Kolb8233fac2010-10-26 16:08:53 -0700145 private Activity mActivity;
146 private UI mUi;
147 private TabControl mTabControl;
148 private BrowserSettings mSettings;
149 private WebViewFactory mFactory;
150
151 private WakeLock mWakeLock;
152
153 private UrlHandler mUrlHandler;
154 private UploadHandler mUploadHandler;
155 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 private PageDialogsHandler mPageDialogsHandler;
157 private NetworkStateHandler mNetworkHandler;
Martijn Coenenb2f93552011-06-14 10:48:35 +0200158 private NfcHandler mNfcHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700159
Ben Murdoch8029a772010-11-16 11:58:21 +0000160 private Message mAutoFillSetupMessage;
161
Michael Kolb8233fac2010-10-26 16:08:53 -0700162 private boolean mShouldShowErrorConsole;
163
164 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
165
166 // FIXME, temp address onPrepareMenu performance problem.
167 // When we move everything out of view, we should rewrite this.
168 private int mCurrentMenuState = 0;
169 private int mMenuState = R.id.MAIN_MENU;
170 private int mOldMenuState = EMPTY_MENU;
171 private Menu mCachedMenu;
172
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 private boolean mMenuIsDown;
174
175 // For select and find, we keep track of the ActionMode so that
176 // finish() can be called as desired.
177 private ActionMode mActionMode;
178
179 /**
180 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
181 * of whether the configuration has changed. The first onMenuOpened call
182 * after a configuration change is simply a reopening of the same menu
183 * (i.e. mIconView did not change).
184 */
185 private boolean mConfigChanged;
186
187 /**
188 * Keeps track of whether the options menu is open. This is important in
189 * determining whether to show or hide the title bar overlay
190 */
191 private boolean mOptionsMenuOpen;
192
193 /**
194 * Whether or not the options menu is in its bigger, popup menu form. When
195 * true, we want the title bar overlay to be gone. When false, we do not.
196 * Only meaningful if mOptionsMenuOpen is true.
197 */
198 private boolean mExtendedMenuOpen;
199
200 private boolean mInLoad;
201
202 private boolean mActivityPaused = true;
203 private boolean mLoadStopped;
204
205 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500206 // Checks to see when the bookmarks database has changed, and updates the
207 // Tabs' notion of whether they represent bookmarked sites.
208 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700209 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700210
John Reck30b065e2011-07-19 10:58:05 -0700211 private boolean mSimulateActionBarOverlayMode;
212
Michael Kolb8233fac2010-10-26 16:08:53 -0700213 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
214 @Override
215 public Void doInBackground(File... files) {
216 if (files != null) {
217 for (File f : files) {
218 if (!f.delete()) {
219 Log.e(LOGTAG, f.getPath() + " was not deleted");
220 }
221 }
222 }
223 return null;
224 }
225 }
226
John Reckbc490d22011-07-22 15:04:59 -0700227 public Controller(Activity browser, boolean preloadCrashState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700228 mActivity = browser;
229 mSettings = BrowserSettings.getInstance();
230 mTabControl = new TabControl(this);
231 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700232 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
John Reckbc490d22011-07-22 15:04:59 -0700233 if (preloadCrashState) {
234 mCrashRecoveryHandler.preloadCrashState();
235 }
Michael Kolb14612442011-06-24 13:06:29 -0700236 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700237
238 mUrlHandler = new UrlHandler(this);
239 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200241 mNfcHandler = new NfcHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700242
Michael Kolb8233fac2010-10-26 16:08:53 -0700243 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
John Reckaf262e72011-07-25 13:55:44 -0700263 openIconDatabase();
John Reck30b065e2011-07-19 10:58:05 -0700264 mSimulateActionBarOverlayMode = !BrowserActivity.isTablet(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700265 }
266
Patrick Scott7d50a932011-02-04 09:27:26 -0500267 void start(final Bundle icicle, final Intent intent) {
Guang Zhu9e78f512011-05-04 11:45:11 -0700268 boolean noCrashRecovery = intent.getBooleanExtra(NO_CRASH_RECOVERY, false);
269 if (icicle != null || noCrashRecovery) {
John Reck847b5322011-04-14 17:02:18 -0700270 doStart(icicle, intent);
271 } else {
272 mCrashRecoveryHandler.startRecovery(intent);
273 }
274 }
275
276 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 // Unless the last browser usage was within 24 hours, destroy any
278 // remaining incognito tabs.
279
280 Calendar lastActiveDate = icicle != null ?
281 (Calendar) icicle.getSerializable("lastActiveDate") : null;
282 Calendar today = Calendar.getInstance();
283 Calendar yesterday = Calendar.getInstance();
284 yesterday.add(Calendar.DATE, -1);
285
Patrick Scott7d50a932011-02-04 09:27:26 -0500286 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700287 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800288 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700289
Patrick Scott7d50a932011-02-04 09:27:26 -0500290 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700291 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500292 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000293
Michael Kolbc831b632011-05-11 09:30:34 -0700294 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500295 // Not able to restore so we go ahead and clear session cookies. We
296 // must do this before trying to login the user as we don't want to
297 // clear any session cookies set during login.
298 CookieManager.getInstance().removeSessionCookie();
299 }
300
Patrick Scottd43e75a2011-03-14 14:47:23 -0400301 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500302 new Runnable() {
303 @Override public void run() {
Michael Kolbc831b632011-05-11 09:30:34 -0700304 onPreloginFinished(icicle, intent, currentTabId, restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500305 }
306 });
307 }
308
Michael Kolbc831b632011-05-11 09:30:34 -0700309 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500310 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700311 if (currentTabId == -1) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700312 final Bundle extra = intent.getExtras();
313 // Create an initial tab.
314 // If the intent is ACTION_VIEW and data is not null, the Browser is
315 // invoked to view the content by another application. In this case,
316 // the tab will be close when exit.
Michael Kolb14612442011-06-24 13:06:29 -0700317 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
Michael Kolb7bcafde2011-05-09 13:55:59 -0700318 Tab t = null;
319 if (urlData.isEmpty()) {
320 t = openTabToHomePage();
321 } else {
322 t = openTab(urlData);
323 }
324 if (t != null) {
325 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
326 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700327 WebView webView = t.getWebView();
328 if (extra != null) {
329 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
330 if (scale > 0 && scale <= 1000) {
331 webView.setInitialScale(scale);
332 }
333 }
John Reckd8c74522011-06-14 08:45:00 -0700334 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700335 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700336 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500337 mUi.needsRestoreAllTabs());
Michael Kolb1bf23132010-11-19 12:55:12 -0800338 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700339 // TabControl.restoreState() will create a new tab even if
340 // restoring the state fails.
341 setActiveTab(mTabControl.getCurrentTab());
John Reckdb22ec42011-06-29 11:31:24 -0700342 // Handle the intent
343 mIntentHandler.onNewIntent(intent);
Michael Kolb8233fac2010-10-26 16:08:53 -0700344 }
345 // clear up the thumbnail directory, which is no longer used;
346 // ideally this should only be run once after an upgrade from
347 // a previous version of the browser
348 new ClearThumbnails().execute(mTabControl.getThumbnailDir()
349 .listFiles());
350 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700351 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700352 if (jsFlags.trim().length() != 0) {
353 getCurrentWebView().setJsFlags(jsFlags);
354 }
John Reck439c9a52010-12-14 10:04:39 -0800355 if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
356 bookmarksOrHistoryPicker(false);
357 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 }
359
Michael Kolb1514bb72010-11-22 09:11:48 -0800360 @Override
361 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700362 return mFactory;
363 }
364
365 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800366 public void onSetWebView(Tab tab, WebView view) {
367 mUi.onSetWebView(tab, view);
368 }
369
370 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800371 public void createSubWindow(Tab tab) {
372 endActionMode();
373 WebView mainView = tab.getWebView();
374 WebView subView = mFactory.createWebView((mainView == null)
375 ? false
376 : mainView.isPrivateBrowsingEnabled());
377 mUi.createSubWindow(tab, subView);
378 }
379
380 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700381 public Context getContext() {
382 return mActivity;
383 }
384
385 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700386 public Activity getActivity() {
387 return mActivity;
388 }
389
390 void setUi(UI ui) {
391 mUi = ui;
392 }
393
394 BrowserSettings getSettings() {
395 return mSettings;
396 }
397
398 IntentHandler getIntentHandler() {
399 return mIntentHandler;
400 }
401
402 @Override
403 public UI getUi() {
404 return mUi;
405 }
406
407 int getMaxTabs() {
408 return mActivity.getResources().getInteger(R.integer.max_tabs);
409 }
410
411 @Override
412 public TabControl getTabControl() {
413 return mTabControl;
414 }
415
Michael Kolb1bf23132010-11-19 12:55:12 -0800416 @Override
417 public List<Tab> getTabs() {
418 return mTabControl.getTabs();
419 }
420
John Reckaf262e72011-07-25 13:55:44 -0700421 // Open the icon database.
422 private void openIconDatabase() {
423 // We have to call getInstance on the UI thread
424 final WebIconDatabase instance = WebIconDatabase.getInstance();
425 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000426
John Reckaf262e72011-07-25 13:55:44 -0700427 @Override
428 public void run() {
429 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700430 }
John Reckaf262e72011-07-25 13:55:44 -0700431 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700432 }
433
434 private void startHandler() {
435 mHandler = new Handler() {
436
437 @Override
438 public void handleMessage(Message msg) {
439 switch (msg.what) {
440 case OPEN_BOOKMARKS:
441 bookmarksOrHistoryPicker(false);
442 break;
443 case FOCUS_NODE_HREF:
444 {
445 String url = (String) msg.getData().get("url");
446 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500447 String src = (String) msg.getData().get("src");
448 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700449 if (TextUtils.isEmpty(url)) {
450 break;
451 }
452 HashMap focusNodeMap = (HashMap) msg.obj;
453 WebView view = (WebView) focusNodeMap.get("webview");
454 // Only apply the action if the top window did not change.
455 if (getCurrentTopWebView() != view) {
456 break;
457 }
458 switch (msg.arg1) {
459 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700460 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700461 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500462 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700463 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500464 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500465 case R.id.open_newtab_context_menu_id:
466 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700467 openTab(url, parent,
468 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500469 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700470 case R.id.copy_link_context_menu_id:
471 copy(url);
472 break;
473 case R.id.save_link_context_menu_id:
474 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500475 DownloadHandler.onDownloadStartNoStream(
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000476 mActivity, url, null, null, null,
477 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700478 break;
479 }
480 break;
481 }
482
483 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700484 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700485 break;
486
487 case STOP_LOAD:
488 stopLoading();
489 break;
490
491 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700492 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700493 mWakeLock.release();
494 // if we reach here, Browser should be still in the
495 // background loading after WAKELOCK_TIMEOUT (5-min).
496 // To avoid burning the battery, stop loading.
497 mTabControl.stopAllLoading();
498 }
499 break;
500
501 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800502 Tab tab = (Tab) msg.obj;
503 if (tab != null) {
504 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700505 }
506 break;
507 }
508 }
509 };
510
511 }
512
John Reckef654f12011-07-12 16:42:08 -0700513 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200514 public Tab getCurrentTab() {
515 return mTabControl.getCurrentTab();
516 }
517
Michael Kolbba99c5d2010-11-29 14:57:41 -0800518 @Override
519 public void shareCurrentPage() {
520 shareCurrentPage(mTabControl.getCurrentTab());
521 }
522
523 private void shareCurrentPage(Tab tab) {
524 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800525 sharePage(mActivity, tab.getTitle(),
526 tab.getUrl(), tab.getFavicon(),
527 createScreenshot(tab.getWebView(),
528 getDesiredThumbnailWidth(mActivity),
529 getDesiredThumbnailHeight(mActivity)));
530 }
531 }
532
Michael Kolb8233fac2010-10-26 16:08:53 -0700533 /**
534 * Share a page, providing the title, url, favicon, and a screenshot. Uses
535 * an {@link Intent} to launch the Activity chooser.
536 * @param c Context used to launch a new Activity.
537 * @param title Title of the page. Stored in the Intent with
538 * {@link Intent#EXTRA_SUBJECT}
539 * @param url URL of the page. Stored in the Intent with
540 * {@link Intent#EXTRA_TEXT}
541 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
542 * with {@link Browser#EXTRA_SHARE_FAVICON}
543 * @param screenshot Bitmap of a screenshot of the page. Stored in the
544 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
545 */
546 static final void sharePage(Context c, String title, String url,
547 Bitmap favicon, Bitmap screenshot) {
548 Intent send = new Intent(Intent.ACTION_SEND);
549 send.setType("text/plain");
550 send.putExtra(Intent.EXTRA_TEXT, url);
551 send.putExtra(Intent.EXTRA_SUBJECT, title);
552 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
553 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
554 try {
555 c.startActivity(Intent.createChooser(send, c.getString(
556 R.string.choosertitle_sharevia)));
557 } catch(android.content.ActivityNotFoundException ex) {
558 // if no app handles it, do nothing
559 }
560 }
561
562 private void copy(CharSequence text) {
563 ClipboardManager cm = (ClipboardManager) mActivity
564 .getSystemService(Context.CLIPBOARD_SERVICE);
565 cm.setText(text);
566 }
567
568 // lifecycle
569
570 protected void onConfgurationChanged(Configuration config) {
571 mConfigChanged = true;
572 if (mPageDialogsHandler != null) {
573 mPageDialogsHandler.onConfigurationChanged(config);
574 }
575 mUi.onConfigurationChanged(config);
576 }
577
578 @Override
579 public void handleNewIntent(Intent intent) {
580 mIntentHandler.onNewIntent(intent);
581 }
582
583 protected void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800584 if (mUi.isCustomViewShowing()) {
585 hideCustomView();
586 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700587 if (mActivityPaused) {
588 Log.e(LOGTAG, "BrowserActivity is already paused.");
589 return;
590 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700591 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800592 Tab tab = mTabControl.getCurrentTab();
593 if (tab != null) {
594 tab.pause();
595 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700596 if (mWakeLock == null) {
597 PowerManager pm = (PowerManager) mActivity
598 .getSystemService(Context.POWER_SERVICE);
599 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
600 }
Michael Kolb70976932010-11-30 11:34:01 -0800601 mWakeLock.acquire();
602 mHandler.sendMessageDelayed(mHandler
603 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
604 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700605 }
606 mUi.onPause();
607 mNetworkHandler.onPause();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200608 mNfcHandler.onPause();
Michael Kolb8233fac2010-10-26 16:08:53 -0700609
610 WebView.disablePlatformNotifications();
John Reck378a4102011-06-09 16:23:01 -0700611 mCrashRecoveryHandler.backupState();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200612
Michael Kolb8233fac2010-10-26 16:08:53 -0700613 }
614
John Reckaed9c542011-05-27 16:08:53 -0700615 void onSaveInstanceState(Bundle outState, boolean saveImages) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700616 // the default implementation requires each view to have an id. As the
617 // browser handles the state itself and it doesn't use id for the views,
618 // don't call the default implementation. Otherwise it will trigger the
619 // warning like this, "couldn't save which view has focus because the
620 // focused view XXX has no id".
621
622 // Save all the tabs
John Reck88026f42011-07-22 16:08:42 -0700623 mTabControl.saveState(outState, false);
John Reck24f18262011-06-17 14:47:20 -0700624 if (!outState.isEmpty()) {
625 // Save time so that we know how old incognito tabs (if any) are.
626 outState.putSerializable("lastActiveDate", Calendar.getInstance());
627 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700628 }
629
630 void onResume() {
631 if (!mActivityPaused) {
632 Log.e(LOGTAG, "BrowserActivity is already resumed.");
633 return;
634 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800636 Tab current = mTabControl.getCurrentTab();
637 if (current != null) {
638 current.resume();
639 resumeWebViewTimers(current);
640 }
John Reckf57c0292011-07-21 18:15:39 -0700641 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200642
Michael Kolb8233fac2010-10-26 16:08:53 -0700643 mUi.onResume();
644 mNetworkHandler.onResume();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200645 mNfcHandler.onResume();
Michael Kolb8233fac2010-10-26 16:08:53 -0700646 WebView.enablePlatformNotifications();
647 }
648
John Reckf57c0292011-07-21 18:15:39 -0700649 private void releaseWakeLock() {
650 if (mWakeLock != null && mWakeLock.isHeld()) {
651 mHandler.removeMessages(RELEASE_WAKELOCK);
652 mWakeLock.release();
653 }
654 }
655
Michael Kolb70976932010-11-30 11:34:01 -0800656 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800657 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800658 * @param tab guaranteed non-null
659 */
660 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 boolean inLoad = tab.inPageLoad();
662 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
663 CookieSyncManager.getInstance().startSync();
664 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100665 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 }
667 }
668
Michael Kolb70976932010-11-30 11:34:01 -0800669 /**
670 * Pause all WebView timers using the WebView of the given tab
671 * @param tab
672 * @return true if the timers are paused or tab is null
673 */
674 private boolean pauseWebViewTimers(Tab tab) {
675 if (tab == null) {
676 return true;
677 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100679 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700680 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700681 }
Michael Kolb70976932010-11-30 11:34:01 -0800682 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700683 }
684
685 void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800686 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700687 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
688 mUploadHandler = null;
689 }
690 if (mTabControl == null) return;
691 mUi.onDestroy();
692 // Remove the current tab and sub window
693 Tab t = mTabControl.getCurrentTab();
694 if (t != null) {
695 dismissSubWindow(t);
696 removeTab(t);
697 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500698 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700699 // Destroy all the tabs
700 mTabControl.destroy();
701 WebIconDatabase.getInstance().close();
702 // Stop watching the default geolocation permissions
703 mSystemAllowGeolocationOrigins.stop();
704 mSystemAllowGeolocationOrigins = null;
705 }
706
707 protected boolean isActivityPaused() {
708 return mActivityPaused;
709 }
710
711 protected void onLowMemory() {
712 mTabControl.freeMemory();
713 }
714
715 @Override
716 public boolean shouldShowErrorConsole() {
717 return mShouldShowErrorConsole;
718 }
719
720 protected void setShouldShowErrorConsole(boolean show) {
721 if (show == mShouldShowErrorConsole) {
722 // Nothing to do.
723 return;
724 }
725 mShouldShowErrorConsole = show;
726 Tab t = mTabControl.getCurrentTab();
727 if (t == null) {
728 // There is no current tab so we cannot toggle the error console
729 return;
730 }
731 mUi.setShouldShowErrorConsole(t, show);
732 }
733
734 @Override
735 public void stopLoading() {
736 mLoadStopped = true;
737 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700738 WebView w = getCurrentTopWebView();
739 w.stopLoading();
Michael Kolb8233fac2010-10-26 16:08:53 -0700740 mUi.onPageStopped(tab);
741 }
742
743 boolean didUserStopLoading() {
744 return mLoadStopped;
745 }
746
747 // WebViewController
748
749 @Override
John Reck324d4402011-01-11 16:56:42 -0800750 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700751
752 // We've started to load a new page. If there was a pending message
753 // to save a screenshot then we will now take the new page and save
754 // an incorrect screenshot. Therefore, remove any pending thumbnail
755 // messages from the queue.
756 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800757 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700758
759 // reset sync timer to avoid sync starts during loading a page
760 CookieSyncManager.getInstance().resetSync();
761
762 if (!mNetworkHandler.isNetworkUp()) {
763 view.setNetworkAvailable(false);
764 }
765
766 // when BrowserActivity just starts, onPageStarted may be called before
767 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
768 // to start the timer. As we won't switch tabs while an activity is in
769 // pause state, we can ensure calling resume and pause in pair.
770 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800771 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700772 }
773 mLoadStopped = false;
774 if (!mNetworkHandler.isNetworkUp()) {
775 mNetworkHandler.createAndShowNetworkDialog();
776 }
777 endActionMode();
778
John Reck30c714c2010-12-16 17:30:34 -0800779 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700780
John Reck324d4402011-01-11 16:56:42 -0800781 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700782 // update the bookmark database for favicon
783 maybeUpdateFavicon(tab, null, url, favicon);
784
785 Performance.tracePageStart(url);
786
787 // Performance probe
788 if (false) {
789 Performance.onPageStarted();
790 }
791
792 }
793
794 @Override
John Reck324d4402011-01-11 16:56:42 -0800795 public void onPageFinished(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800796 mUi.onTabDataChanged(tab);
John Reck324d4402011-01-11 16:56:42 -0800797 if (!tab.isPrivateBrowsingEnabled()
John Reckd8c74522011-06-14 08:45:00 -0700798 && !TextUtils.isEmpty(tab.getUrl())
799 && !tab.isSnapshot()) {
John Recka1696282011-07-08 14:10:37 -0700800 // Only update the bookmark screenshot if the user did not
801 // cancel the load early and there is not already
802 // a pending update for the tab.
Michael Kolb8233fac2010-10-26 16:08:53 -0700803 if (tab.inForeground() && !didUserStopLoading()
804 || !tab.inForeground()) {
John Recka1696282011-07-08 14:10:37 -0700805 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
806 mHandler.sendMessageDelayed(mHandler.obtainMessage(
807 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
808 500);
809 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700810 }
811 }
812 // pause the WebView timer and release the wake lock if it is finished
813 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800814 if (mActivityPaused && pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700815 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700816 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200817
Michael Kolb8233fac2010-10-26 16:08:53 -0700818 // Performance probe
819 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800820 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700821 }
822
823 Performance.tracePageFinished();
824 }
825
826 @Override
John Reck30c714c2010-12-16 17:30:34 -0800827 public void onProgressChanged(Tab tab) {
828 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700829
830 if (newProgress == 100) {
831 CookieSyncManager.getInstance().sync();
832 // onProgressChanged() may continue to be called after the main
833 // frame has finished loading, as any remaining sub frames continue
834 // to load. We'll only get called once though with newProgress as
835 // 100 when everything is loaded. (onPageFinished is called once
836 // when the main frame completes loading regardless of the state of
837 // any sub frames so calls to onProgressChanges may continue after
838 // onPageFinished has executed)
839 if (mInLoad) {
840 mInLoad = false;
841 updateInLoadMenuItems(mCachedMenu);
842 }
843 } else {
844 if (!mInLoad) {
845 // onPageFinished may have already been called but a subframe is
846 // still loading and updating the progress. Reset mInLoad and
847 // update the menu items.
848 mInLoad = true;
849 updateInLoadMenuItems(mCachedMenu);
850 }
851 }
John Reck30c714c2010-12-16 17:30:34 -0800852 mUi.onProgressChanged(tab);
853 }
854
855 @Override
856 public void onUpdatedLockIcon(Tab tab) {
857 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700858 }
859
860 @Override
861 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800862 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800863 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800864 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700865 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
866 return;
867 }
868 // Update the title in the history database if not in private browsing mode
869 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -0700870 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 }
872 }
873
874 @Override
875 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800876 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700877 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
878 }
879
880 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800881 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
882 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 }
884
885 @Override
886 public boolean shouldOverrideKeyEvent(KeyEvent event) {
887 if (mMenuIsDown) {
888 // only check shortcut key when MENU is held
889 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
890 event);
891 } else {
892 return false;
893 }
894 }
895
896 @Override
897 public void onUnhandledKeyEvent(KeyEvent event) {
898 if (!isActivityPaused()) {
899 if (event.getAction() == KeyEvent.ACTION_DOWN) {
900 mActivity.onKeyDown(event.getKeyCode(), event);
901 } else {
902 mActivity.onKeyUp(event.getKeyCode(), event);
903 }
904 }
905 }
906
907 @Override
John Reck324d4402011-01-11 16:56:42 -0800908 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700909 // Don't save anything in private browsing mode
910 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800911 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700912
John Reck324d4402011-01-11 16:56:42 -0800913 if (TextUtils.isEmpty(url)
914 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700915 return;
916 }
John Reckf57c0292011-07-21 18:15:39 -0700917 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck847b5322011-04-14 17:02:18 -0700918 if (!mActivityPaused) {
919 // Since we clear the state in onPause, don't backup the current
920 // state if we are already paused
921 mCrashRecoveryHandler.backupState();
922 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700923 }
924
925 @Override
926 public void getVisitedHistory(final ValueCallback<String[]> callback) {
927 AsyncTask<Void, Void, String[]> task =
928 new AsyncTask<Void, Void, String[]>() {
929 @Override
930 public String[] doInBackground(Void... unused) {
931 return Browser.getVisitedHistory(mActivity.getContentResolver());
932 }
933 @Override
934 public void onPostExecute(String[] result) {
935 callback.onReceiveValue(result);
936 }
937 };
938 task.execute();
939 }
940
941 @Override
942 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
943 final HttpAuthHandler handler, final String host,
944 final String realm) {
945 String username = null;
946 String password = null;
947
948 boolean reuseHttpAuthUsernamePassword
949 = handler.useHttpAuthUsernamePassword();
950
951 if (reuseHttpAuthUsernamePassword && view != null) {
952 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
953 if (credentials != null && credentials.length == 2) {
954 username = credentials[0];
955 password = credentials[1];
956 }
957 }
958
959 if (username != null && password != null) {
960 handler.proceed(username, password);
961 } else {
962 if (tab.inForeground()) {
963 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
964 } else {
965 handler.cancel();
966 }
967 }
968 }
969
970 @Override
971 public void onDownloadStart(Tab tab, String url, String userAgent,
972 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000973 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -0500974 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000975 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
976 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 // This Tab was opened for the sole purpose of downloading a
978 // file. Remove it.
979 if (tab == mTabControl.getCurrentTab()) {
980 // In this case, the Tab is still on top.
981 goBackOnePageOrQuit();
982 } else {
983 // In this case, it is not.
984 closeTab(tab);
985 }
986 }
987 }
988
989 @Override
990 public Bitmap getDefaultVideoPoster() {
991 return mUi.getDefaultVideoPoster();
992 }
993
994 @Override
995 public View getVideoLoadingProgressView() {
996 return mUi.getVideoLoadingProgressView();
997 }
998
999 @Override
1000 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1001 SslError error) {
1002 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1003 }
1004
Patrick Scott92066772011-03-10 08:46:27 -05001005 @Override
1006 public void showAutoLogin(Tab tab) {
1007 assert tab.inForeground();
1008 // Update the title bar to show the auto-login request.
1009 mUi.showAutoLogin(tab);
1010 }
1011
1012 @Override
1013 public void hideAutoLogin(Tab tab) {
1014 assert tab.inForeground();
1015 mUi.hideAutoLogin(tab);
1016 }
1017
Michael Kolb8233fac2010-10-26 16:08:53 -07001018 // helper method
1019
1020 /*
1021 * Update the favorites icon if the private browsing isn't enabled and the
1022 * icon is valid.
1023 */
1024 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1025 final String url, Bitmap favicon) {
1026 if (favicon == null) {
1027 return;
1028 }
1029 if (!tab.isPrivateBrowsingEnabled()) {
1030 Bookmarks.updateFavicon(mActivity
1031 .getContentResolver(), originalUrl, url, favicon);
1032 }
1033 }
1034
Leon Scroggins4cd97792010-12-03 15:31:56 -05001035 @Override
1036 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001037 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001038 mUi.bookmarkedStatusHasChanged(tab);
1039 }
1040
Michael Kolb8233fac2010-10-26 16:08:53 -07001041 // end WebViewController
1042
1043 protected void pageUp() {
1044 getCurrentTopWebView().pageUp(false);
1045 }
1046
1047 protected void pageDown() {
1048 getCurrentTopWebView().pageDown(false);
1049 }
1050
1051 // callback from phone title bar
1052 public void editUrl() {
1053 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001054 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001055 }
1056
Michael Kolbcfa3af52010-12-14 10:36:11 -08001057 public void startVoiceSearch() {
1058 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1059 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1060 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1061 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1062 mActivity.getComponentName().flattenToString());
1063 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001064 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001065 mActivity.startActivity(intent);
1066 }
1067
Michael Kolb11d19782011-03-20 10:17:40 -07001068 @Override
1069 public void activateVoiceSearchMode(String title, List<String> results) {
1070 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001071 }
1072
1073 public void revertVoiceSearchMode(Tab tab) {
1074 mUi.revertVoiceTitleBar(tab);
1075 }
1076
Michael Kolb736990c2011-03-20 10:01:20 -07001077 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001078 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001079 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1080 }
1081
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001082 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001083 WebChromeClient.CustomViewCallback callback) {
1084 if (tab.inForeground()) {
1085 if (mUi.isCustomViewShowing()) {
1086 callback.onCustomViewHidden();
1087 return;
1088 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001089 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001090 // Save the menu state and set it to empty while the custom
1091 // view is showing.
1092 mOldMenuState = mMenuState;
1093 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001094 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001095 }
1096 }
1097
1098 @Override
1099 public void hideCustomView() {
1100 if (mUi.isCustomViewShowing()) {
1101 mUi.onHideCustomView();
1102 // Reset the old menu state.
1103 mMenuState = mOldMenuState;
1104 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001105 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001106 }
1107 }
1108
1109 protected void onActivityResult(int requestCode, int resultCode,
1110 Intent intent) {
1111 if (getCurrentTopWebView() == null) return;
1112 switch (requestCode) {
1113 case PREFERENCES_PAGE:
1114 if (resultCode == Activity.RESULT_OK && intent != null) {
1115 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001116 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001117 mTabControl.removeParentChildRelationShips();
1118 }
1119 }
1120 break;
1121 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001122 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001123 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001124 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001125 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001126 case AUTOFILL_SETUP:
1127 // Determine whether a profile was actually set up or not
1128 // and if so, send the message back to the WebTextView to
1129 // fill the form with the new profile.
1130 if (getSettings().getAutoFillProfile() != null) {
1131 mAutoFillSetupMessage.sendToTarget();
1132 mAutoFillSetupMessage = null;
1133 }
1134 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001135 case COMBO_VIEW:
1136 if (intent == null || resultCode != Activity.RESULT_OK) {
1137 break;
1138 }
1139 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1140 Tab t = getCurrentTab();
1141 Uri uri = intent.getData();
1142 loadUrl(t, uri.toString());
1143 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1144 String[] urls = intent.getStringArrayExtra(
1145 ComboViewActivity.EXTRA_OPEN_ALL);
1146 Tab parent = getCurrentTab();
1147 for (String url : urls) {
1148 parent = openTab(url, parent,
1149 !mSettings.openInBackground(), true);
1150 }
1151 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1152 long id = intent.getLongExtra(
1153 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1154 if (id >= 0) {
1155 createNewSnapshotTab(id, true);
1156 }
1157 }
1158 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001159 default:
1160 break;
1161 }
1162 getCurrentTopWebView().requestFocus();
1163 }
1164
1165 /**
1166 * Open the Go page.
1167 * @param startWithHistory If true, open starting on the history tab.
1168 * Otherwise, start with the bookmarks tab.
1169 */
1170 @Override
1171 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1172 if (mTabControl.getCurrentWebView() == null) {
1173 return;
1174 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001175 // clear action mode
1176 if (isInCustomActionMode()) {
1177 endActionMode();
1178 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001179 Bundle extras = new Bundle();
1180 // Disable opening in a new window if we have maxed out the windows
1181 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1182 !mTabControl.canCreateNewTab());
John Reck2bc80422011-06-30 15:11:49 -07001183 mUi.showComboView(startWithHistory
1184 ? ComboViews.History : ComboViews.Bookmarks, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001185 }
1186
1187 // combo view callbacks
1188
1189 /**
1190 * callback from ComboPage when clear history is requested
1191 */
1192 public void onRemoveParentChildRelationships() {
1193 mTabControl.removeParentChildRelationShips();
1194 }
1195
Michael Kolb8233fac2010-10-26 16:08:53 -07001196 // key handling
1197 protected void onBackKey() {
1198 if (!mUi.onBackKey()) {
1199 WebView subwindow = mTabControl.getCurrentSubWindow();
1200 if (subwindow != null) {
1201 if (subwindow.canGoBack()) {
1202 subwindow.goBack();
1203 } else {
1204 dismissSubWindow(mTabControl.getCurrentTab());
1205 }
1206 } else {
1207 goBackOnePageOrQuit();
1208 }
1209 }
1210 }
1211
Michael Kolb4bd767d2011-05-27 11:33:55 -07001212 protected boolean onMenuKey() {
1213 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001214 }
1215
Michael Kolb8233fac2010-10-26 16:08:53 -07001216 // menu handling and state
1217 // TODO: maybe put into separate handler
1218
1219 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001220 if (mMenuState == EMPTY_MENU) {
1221 return false;
1222 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001223 MenuInflater inflater = mActivity.getMenuInflater();
1224 inflater.inflate(R.menu.browser, menu);
1225 updateInLoadMenuItems(menu);
1226 // hold on to the menu reference here; it is used by the page callbacks
1227 // to update the menu based on loading state
1228 mCachedMenu = menu;
1229 return true;
1230 }
1231
1232 protected void onCreateContextMenu(ContextMenu menu, View v,
1233 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001234 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001235 return;
1236 }
1237 if (!(v instanceof WebView)) {
1238 return;
1239 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001240 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001241 WebView.HitTestResult result = webview.getHitTestResult();
1242 if (result == null) {
1243 return;
1244 }
1245
1246 int type = result.getType();
1247 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1248 Log.w(LOGTAG,
1249 "We should not show context menu when nothing is touched");
1250 return;
1251 }
1252 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1253 // let TextView handles context menu
1254 return;
1255 }
1256
1257 // Note, http://b/issue?id=1106666 is requesting that
1258 // an inflated menu can be used again. This is not available
1259 // yet, so inflate each time (yuk!)
1260 MenuInflater inflater = mActivity.getMenuInflater();
1261 inflater.inflate(R.menu.browsercontext, menu);
1262
1263 // Show the correct menu group
1264 final String extra = result.getExtra();
1265 menu.setGroupVisible(R.id.PHONE_MENU,
1266 type == WebView.HitTestResult.PHONE_TYPE);
1267 menu.setGroupVisible(R.id.EMAIL_MENU,
1268 type == WebView.HitTestResult.EMAIL_TYPE);
1269 menu.setGroupVisible(R.id.GEO_MENU,
1270 type == WebView.HitTestResult.GEO_TYPE);
1271 menu.setGroupVisible(R.id.IMAGE_MENU,
1272 type == WebView.HitTestResult.IMAGE_TYPE
1273 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1274 menu.setGroupVisible(R.id.ANCHOR_MENU,
1275 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1276 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001277 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1278 || type == WebView.HitTestResult.PHONE_TYPE
1279 || type == WebView.HitTestResult.EMAIL_TYPE
1280 || type == WebView.HitTestResult.GEO_TYPE;
1281 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1282 if (hitText) {
1283 menu.findItem(R.id.select_text_menu_id)
1284 .setOnMenuItemClickListener(new SelectText(webview));
1285 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001286 // Setup custom handling depending on the type
1287 switch (type) {
1288 case WebView.HitTestResult.PHONE_TYPE:
1289 menu.setHeaderTitle(Uri.decode(extra));
1290 menu.findItem(R.id.dial_context_menu_id).setIntent(
1291 new Intent(Intent.ACTION_VIEW, Uri
1292 .parse(WebView.SCHEME_TEL + extra)));
1293 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1294 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1295 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1296 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1297 addIntent);
1298 menu.findItem(R.id.copy_phone_context_menu_id)
1299 .setOnMenuItemClickListener(
1300 new Copy(extra));
1301 break;
1302
1303 case WebView.HitTestResult.EMAIL_TYPE:
1304 menu.setHeaderTitle(extra);
1305 menu.findItem(R.id.email_context_menu_id).setIntent(
1306 new Intent(Intent.ACTION_VIEW, Uri
1307 .parse(WebView.SCHEME_MAILTO + extra)));
1308 menu.findItem(R.id.copy_mail_context_menu_id)
1309 .setOnMenuItemClickListener(
1310 new Copy(extra));
1311 break;
1312
1313 case WebView.HitTestResult.GEO_TYPE:
1314 menu.setHeaderTitle(extra);
1315 menu.findItem(R.id.map_context_menu_id).setIntent(
1316 new Intent(Intent.ACTION_VIEW, Uri
1317 .parse(WebView.SCHEME_GEO
1318 + URLEncoder.encode(extra))));
1319 menu.findItem(R.id.copy_geo_context_menu_id)
1320 .setOnMenuItemClickListener(
1321 new Copy(extra));
1322 break;
1323
1324 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1325 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001326 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001327 // decide whether to show the open link in new tab option
1328 boolean showNewTab = mTabControl.canCreateNewTab();
1329 MenuItem newTabItem
1330 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001331 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001332 ? R.string.contextmenu_openlink_newwindow_background
1333 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001334 newTabItem.setVisible(showNewTab);
1335 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001336 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1337 newTabItem.setOnMenuItemClickListener(
1338 new MenuItem.OnMenuItemClickListener() {
1339 @Override
1340 public boolean onMenuItemClick(MenuItem item) {
1341 final HashMap<String, WebView> hrefMap =
1342 new HashMap<String, WebView>();
1343 hrefMap.put("webview", webview);
1344 final Message msg = mHandler.obtainMessage(
1345 FOCUS_NODE_HREF,
1346 R.id.open_newtab_context_menu_id,
1347 0, hrefMap);
1348 webview.requestFocusNodeHref(msg);
1349 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001350 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001351 });
1352 } else {
1353 newTabItem.setOnMenuItemClickListener(
1354 new MenuItem.OnMenuItemClickListener() {
1355 @Override
1356 public boolean onMenuItemClick(MenuItem item) {
1357 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001358 openTab(extra, parent,
1359 !mSettings.openInBackground(),
1360 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001361 return true;
1362 }
1363 });
1364 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001365 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001366 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1367 break;
1368 }
1369 // otherwise fall through to handle image part
1370 case WebView.HitTestResult.IMAGE_TYPE:
1371 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1372 menu.setHeaderTitle(extra);
1373 }
1374 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1375 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1376 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001377 setOnMenuItemClickListener(
1378 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001379 menu.findItem(R.id.set_wallpaper_context_menu_id).
1380 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1381 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001382 break;
1383
1384 default:
1385 Log.w(LOGTAG, "We should not get here.");
1386 break;
1387 }
1388 //update the ui
1389 mUi.onContextMenuCreated(menu);
1390 }
1391
1392 /**
1393 * As the menu can be open when loading state changes
1394 * we must manually update the state of the stop/reload menu
1395 * item
1396 */
1397 private void updateInLoadMenuItems(Menu menu) {
1398 if (menu == null) {
1399 return;
1400 }
1401 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1402 MenuItem src = mInLoad ?
1403 menu.findItem(R.id.stop_menu_id):
1404 menu.findItem(R.id.reload_menu_id);
1405 if (src != null) {
1406 dest.setIcon(src.getIcon());
1407 dest.setTitle(src.getTitle());
1408 }
1409 }
1410
John Reckb3417f02011-01-14 11:01:05 -08001411 boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001412 // Note: setVisible will decide whether an item is visible; while
1413 // setEnabled() will decide whether an item is enabled, which also means
1414 // whether the matching shortcut key will function.
1415 switch (mMenuState) {
1416 case EMPTY_MENU:
1417 if (mCurrentMenuState != mMenuState) {
1418 menu.setGroupVisible(R.id.MAIN_MENU, false);
1419 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1420 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1421 }
1422 break;
1423 default:
1424 if (mCurrentMenuState != mMenuState) {
1425 menu.setGroupVisible(R.id.MAIN_MENU, true);
1426 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1427 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1428 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001429 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001430 break;
1431 }
1432 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001433 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001434 }
1435
Michael Kolb4bf79712011-07-14 14:18:12 -07001436 @Override
1437 public void updateMenuState(Tab tab, Menu menu) {
1438 boolean canGoBack = false;
1439 boolean canGoForward = false;
1440 boolean isHome = false;
1441 if (tab != null) {
1442 canGoBack = tab.canGoBack();
1443 canGoForward = tab.canGoForward();
1444 isHome = mSettings.getHomePage().equals(tab.getUrl());
1445 }
1446 final MenuItem back = menu.findItem(R.id.back_menu_id);
1447 back.setEnabled(canGoBack);
1448
1449 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1450 home.setEnabled(!isHome);
1451
1452 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1453 forward.setEnabled(canGoForward);
1454
1455 final MenuItem source = menu.findItem(mInLoad ? R.id.stop_menu_id : R.id.reload_menu_id);
1456 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001457 if (source != null && dest != null) {
1458 dest.setTitle(source.getTitle());
1459 dest.setIcon(source.getIcon());
1460 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001461
1462 // decide whether to show the share link option
1463 PackageManager pm = mActivity.getPackageManager();
1464 Intent send = new Intent(Intent.ACTION_SEND);
1465 send.setType("text/plain");
1466 ResolveInfo ri = pm.resolveActivity(send,
1467 PackageManager.MATCH_DEFAULT_ONLY);
1468 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1469
1470 boolean isNavDump = mSettings.enableNavDump();
1471 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1472 nav.setVisible(isNavDump);
1473 nav.setEnabled(isNavDump);
1474
1475 boolean showDebugSettings = mSettings.isDebugEnabled();
1476 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1477 counter.setVisible(showDebugSettings);
1478 counter.setEnabled(showDebugSettings);
1479
1480 MenuItem saveSnapshot = menu.findItem(R.id.save_snapshot_menu_id);
1481 saveSnapshot.setVisible(tab != null && !tab.isSnapshot());
1482 }
1483
Michael Kolb8233fac2010-10-26 16:08:53 -07001484 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001485 if (null == getCurrentTopWebView()) {
1486 return false;
1487 }
1488 if (mMenuIsDown) {
1489 // The shortcut action consumes the MENU. Even if it is still down,
1490 // it won't trigger the next shortcut action. In the case of the
1491 // shortcut action triggering a new activity, like Bookmarks, we
1492 // won't get onKeyUp for MENU. So it is important to reset it here.
1493 mMenuIsDown = false;
1494 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001495 if (mUi.onOptionsItemSelected(item)) {
1496 // ui callback handled it
1497 return true;
1498 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001499 switch (item.getItemId()) {
1500 // -- Main menu
1501 case R.id.new_tab_menu_id:
1502 openTabToHomePage();
1503 break;
1504
1505 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001506 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001507 break;
1508
1509 case R.id.goto_menu_id:
1510 editUrl();
1511 break;
1512
1513 case R.id.bookmarks_menu_id:
1514 bookmarksOrHistoryPicker(false);
1515 break;
1516
Michael Kolb8233fac2010-10-26 16:08:53 -07001517 case R.id.add_bookmark_menu_id:
John Reckd3e4d5b2011-07-13 15:48:43 -07001518 mActivity.startActivity(createBookmarkCurrentPageIntent(false));
Michael Kolb8233fac2010-10-26 16:08:53 -07001519 break;
1520
1521 case R.id.stop_reload_menu_id:
1522 if (mInLoad) {
1523 stopLoading();
1524 } else {
1525 getCurrentTopWebView().reload();
1526 }
1527 break;
1528
1529 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001530 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001531 break;
1532
1533 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001534 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001535 break;
1536
1537 case R.id.close_menu_id:
1538 // Close the subwindow if it exists.
1539 if (mTabControl.getCurrentSubWindow() != null) {
1540 dismissSubWindow(mTabControl.getCurrentTab());
1541 break;
1542 }
1543 closeCurrentTab();
1544 break;
1545
1546 case R.id.homepage_menu_id:
1547 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001548 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001549 break;
1550
1551 case R.id.preferences_menu_id:
1552 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1553 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1554 getCurrentTopWebView().getUrl());
1555 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1556 break;
1557
1558 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001559 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001560 break;
1561
John Reck2bc80422011-06-30 15:11:49 -07001562 case R.id.save_snapshot_menu_id:
1563 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001564 if (source == null) break;
John Reckd8c74522011-06-14 08:45:00 -07001565 final ContentResolver cr = mActivity.getContentResolver();
1566 final ContentValues values = source.createSnapshotValues();
John Reck2bc80422011-06-30 15:11:49 -07001567 if (values != null) {
1568 new AsyncTask<Tab, Void, Long>() {
John Reckd8c74522011-06-14 08:45:00 -07001569
John Reck2bc80422011-06-30 15:11:49 -07001570 @Override
1571 protected Long doInBackground(Tab... params) {
1572 Uri result = cr.insert(Snapshots.CONTENT_URI, values);
1573 long id = ContentUris.parseId(result);
1574 return id;
John Reckd8c74522011-06-14 08:45:00 -07001575 }
John Reckf33b1632011-06-04 20:00:23 -07001576
John Reck2bc80422011-06-30 15:11:49 -07001577 @Override
1578 protected void onPostExecute(Long id) {
1579 Bundle b = new Bundle();
1580 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1581 mUi.showComboView(ComboViews.Snapshots, b);
1582 };
1583 }.execute(source);
1584 } else {
1585 Toast.makeText(mActivity, R.string.snapshot_failed,
Leon Scrogginsac993842011-02-02 12:54:07 -05001586 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001587 }
Leon Scrogginsac993842011-02-02 12:54:07 -05001588 break;
1589
Michael Kolb8233fac2010-10-26 16:08:53 -07001590 case R.id.page_info_menu_id:
Huahui Wuae0c0412011-06-28 10:17:05 -07001591 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
Michael Kolb8233fac2010-10-26 16:08:53 -07001592 break;
1593
1594 case R.id.classic_history_menu_id:
1595 bookmarksOrHistoryPicker(true);
1596 break;
1597
1598 case R.id.title_bar_share_page_url:
1599 case R.id.share_page_menu_id:
1600 Tab currentTab = mTabControl.getCurrentTab();
1601 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001602 return false;
1603 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001604 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001605 break;
1606
1607 case R.id.dump_nav_menu_id:
1608 getCurrentTopWebView().debugDump();
1609 break;
1610
1611 case R.id.dump_counters_menu_id:
1612 getCurrentTopWebView().dumpV8Counters();
1613 break;
1614
1615 case R.id.zoom_in_menu_id:
1616 getCurrentTopWebView().zoomIn();
1617 break;
1618
1619 case R.id.zoom_out_menu_id:
1620 getCurrentTopWebView().zoomOut();
1621 break;
1622
1623 case R.id.view_downloads_menu_id:
1624 viewDownloads();
1625 break;
1626
1627 case R.id.window_one_menu_id:
1628 case R.id.window_two_menu_id:
1629 case R.id.window_three_menu_id:
1630 case R.id.window_four_menu_id:
1631 case R.id.window_five_menu_id:
1632 case R.id.window_six_menu_id:
1633 case R.id.window_seven_menu_id:
1634 case R.id.window_eight_menu_id:
1635 {
1636 int menuid = item.getItemId();
1637 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1638 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1639 Tab desiredTab = mTabControl.getTab(id);
1640 if (desiredTab != null &&
1641 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001642 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001643 }
1644 break;
1645 }
1646 }
1647 }
1648 break;
1649
1650 default:
1651 return false;
1652 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001653 return true;
1654 }
1655
1656 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001657 // Let the History and Bookmark fragments handle menus they created.
1658 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1659 return false;
1660 }
1661
Michael Kolb8233fac2010-10-26 16:08:53 -07001662 int id = item.getItemId();
1663 boolean result = true;
1664 switch (id) {
1665 // For the context menu from the title bar
1666 case R.id.title_bar_copy_page_url:
1667 Tab currentTab = mTabControl.getCurrentTab();
1668 if (null == currentTab) {
1669 result = false;
1670 break;
1671 }
1672 WebView mainView = currentTab.getWebView();
1673 if (null == mainView) {
1674 result = false;
1675 break;
1676 }
1677 copy(mainView.getUrl());
1678 break;
1679 // -- Browser context menu
1680 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001681 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001682 case R.id.copy_link_context_menu_id:
1683 final WebView webView = getCurrentTopWebView();
1684 if (null == webView) {
1685 result = false;
1686 break;
1687 }
1688 final HashMap<String, WebView> hrefMap =
1689 new HashMap<String, WebView>();
1690 hrefMap.put("webview", webView);
1691 final Message msg = mHandler.obtainMessage(
1692 FOCUS_NODE_HREF, id, 0, hrefMap);
1693 webView.requestFocusNodeHref(msg);
1694 break;
1695
1696 default:
1697 // For other context menus
1698 result = onOptionsItemSelected(item);
1699 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001700 return result;
1701 }
1702
1703 /**
1704 * support programmatically opening the context menu
1705 */
1706 public void openContextMenu(View view) {
1707 mActivity.openContextMenu(view);
1708 }
1709
1710 /**
1711 * programmatically open the options menu
1712 */
1713 public void openOptionsMenu() {
1714 mActivity.openOptionsMenu();
1715 }
1716
1717 public boolean onMenuOpened(int featureId, Menu menu) {
1718 if (mOptionsMenuOpen) {
1719 if (mConfigChanged) {
1720 // We do not need to make any changes to the state of the
1721 // title bar, since the only thing that happened was a
1722 // change in orientation
1723 mConfigChanged = false;
1724 } else {
1725 if (!mExtendedMenuOpen) {
1726 mExtendedMenuOpen = true;
1727 mUi.onExtendedMenuOpened();
1728 } else {
1729 // Switching the menu back to icon view, so show the
1730 // title bar once again.
1731 mExtendedMenuOpen = false;
1732 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001733 }
1734 }
1735 } else {
1736 // The options menu is closed, so open it, and show the title
1737 mOptionsMenuOpen = true;
1738 mConfigChanged = false;
1739 mExtendedMenuOpen = false;
1740 mUi.onOptionsMenuOpened();
1741 }
1742 return true;
1743 }
1744
1745 public void onOptionsMenuClosed(Menu menu) {
1746 mOptionsMenuOpen = false;
1747 mUi.onOptionsMenuClosed(mInLoad);
1748 }
1749
1750 public void onContextMenuClosed(Menu menu) {
1751 mUi.onContextMenuClosed(menu, mInLoad);
1752 }
1753
1754 // Helper method for getting the top window.
1755 @Override
1756 public WebView getCurrentTopWebView() {
1757 return mTabControl.getCurrentTopWebView();
1758 }
1759
1760 @Override
1761 public WebView getCurrentWebView() {
1762 return mTabControl.getCurrentWebView();
1763 }
1764
1765 /*
1766 * This method is called as a result of the user selecting the options
1767 * menu to see the download window. It shows the download window on top of
1768 * the current window.
1769 */
1770 void viewDownloads() {
1771 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1772 mActivity.startActivity(intent);
1773 }
1774
John Reck30b065e2011-07-19 10:58:05 -07001775 int getActionModeHeight() {
1776 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1777 new int[] { android.R.attr.actionBarSize });
1778 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1779 actionBarSizeTypedArray.recycle();
1780 return size;
1781 }
1782
Michael Kolb8233fac2010-10-26 16:08:53 -07001783 // action mode
1784
1785 void onActionModeStarted(ActionMode mode) {
1786 mUi.onActionModeStarted(mode);
1787 mActionMode = mode;
John Reck30b065e2011-07-19 10:58:05 -07001788 if (mSimulateActionBarOverlayMode) {
1789 WebView web = getCurrentWebView();
1790 // Simulate overlay mode by scrolling the webview the amount it will be
1791 // pushed down. Actual overlay mode doesn't work for us as otherwise
1792 // the CAB will, well, overlay the content, which breaks things like
1793 // find on page.
1794 int scrollBy = getActionModeHeight();
1795 web.scrollBy(0, scrollBy);
1796 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001797 }
1798
1799 /*
1800 * True if a custom ActionMode (i.e. find or select) is in use.
1801 */
1802 @Override
1803 public boolean isInCustomActionMode() {
1804 return mActionMode != null;
1805 }
1806
1807 /*
1808 * End the current ActionMode.
1809 */
1810 @Override
1811 public void endActionMode() {
1812 if (mActionMode != null) {
1813 mActionMode.finish();
1814 }
1815 }
1816
1817 /*
1818 * Called by find and select when they are finished. Replace title bars
1819 * as necessary.
1820 */
1821 public void onActionModeFinished(ActionMode mode) {
1822 if (!isInCustomActionMode()) return;
1823 mUi.onActionModeFinished(mInLoad);
1824 mActionMode = null;
John Reck30b065e2011-07-19 10:58:05 -07001825 if (mSimulateActionBarOverlayMode) {
1826 WebView web = getCurrentWebView();
1827 int scrollBy = getActionModeHeight();
1828 web.scrollBy(0, -scrollBy);
1829 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001830 }
1831
1832 boolean isInLoad() {
1833 return mInLoad;
1834 }
1835
1836 // bookmark handling
1837
1838 /**
1839 * add the current page as a bookmark to the given folder id
1840 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07001841 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001842 * bookmarked, and if it is, edit that bookmark. If false, and
1843 * the site is already bookmarked, do not attempt to edit the
1844 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001845 */
1846 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07001847 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001848 Intent i = new Intent(mActivity,
1849 AddBookmarkPage.class);
1850 WebView w = getCurrentTopWebView();
1851 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1852 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1853 String touchIconUrl = w.getTouchIconUrl();
1854 if (touchIconUrl != null) {
1855 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1856 WebSettings settings = w.getSettings();
1857 if (settings != null) {
1858 i.putExtra(AddBookmarkPage.USER_AGENT,
1859 settings.getUserAgentString());
1860 }
1861 }
1862 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1863 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1864 getDesiredThumbnailHeight(mActivity)));
1865 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07001866 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001867 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1868 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001869 // Put the dialog at the upper right of the screen, covering the
1870 // star on the title bar.
1871 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07001872 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07001873 }
1874
1875 // file chooser
1876 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1877 mUploadHandler = new UploadHandler(this);
1878 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1879 }
1880
1881 // thumbnails
1882
1883 /**
1884 * Return the desired width for thumbnail screenshots, which are stored in
1885 * the database, and used on the bookmarks screen.
1886 * @param context Context for finding out the density of the screen.
1887 * @return desired width for thumbnail screenshot.
1888 */
1889 static int getDesiredThumbnailWidth(Context context) {
1890 return context.getResources().getDimensionPixelOffset(
1891 R.dimen.bookmarkThumbnailWidth);
1892 }
1893
1894 /**
1895 * Return the desired height for thumbnail screenshots, which are stored in
1896 * the database, and used on the bookmarks screen.
1897 * @param context Context for finding out the density of the screen.
1898 * @return desired height for thumbnail screenshot.
1899 */
1900 static int getDesiredThumbnailHeight(Context context) {
1901 return context.getResources().getDimensionPixelOffset(
1902 R.dimen.bookmarkThumbnailHeight);
1903 }
1904
Michael Kolb1acef692011-03-08 14:12:06 -08001905 static Bitmap createScreenshot(Tab tab, int width, int height) {
1906 if ((tab != null) && (tab.getWebView() != null)) {
1907 return createScreenshot(tab.getWebView(), width, height);
1908 }
1909 return null;
1910 }
1911
John Reck8cc92352011-07-06 17:41:52 -07001912 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08001913 // We render to a bitmap 2x the desired size so that we can then
1914 // re-scale it with filtering since canvas.scale doesn't filter
1915 // This helps reduce aliasing at the cost of being slightly blurry
1916 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07001917 Picture thumbnail = view.capturePicture();
1918 if (thumbnail == null) {
1919 return null;
1920 }
John Reck5c6ac2f2011-01-05 10:18:03 -08001921 width *= filter_scale;
1922 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07001923 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1924 Canvas canvas = new Canvas(bm);
1925 // May need to tweak these values to determine what is the
1926 // best scale factor
1927 int thumbnailWidth = thumbnail.getWidth();
1928 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08001929 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08001930 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08001931 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07001932 } else {
1933 return null;
1934 }
John Reckfe49ab42010-11-16 17:09:37 -08001935
Michael Kolbeb95db42011-03-03 10:38:40 -08001936 float scaleFactorY = (float) height / (float)thumbnailHeight;
1937 if (scaleFactorY > scaleFactor) {
1938 // The picture is narrower than the requested AR
1939 // Center the thumnail and crop the sides
1940 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08001941 float wx = (thumbnailWidth * scaleFactor) - width;
1942 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07001943 }
1944
John Reckfe49ab42010-11-16 17:09:37 -08001945 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07001946
1947 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08001948 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
1949 height / filter_scale, true);
1950 bm.recycle();
1951 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07001952 }
1953
John Reck34ef2672011-02-10 11:30:55 -08001954 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001955 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07001956 // FIXME: Would like to make sure there is actually something to
1957 // draw, but the API for that (WebViewCore.pictureReady()) is not
1958 // currently accessible here.
1959
John Reck34ef2672011-02-10 11:30:55 -08001960 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08001961 if (view == null) {
1962 // Tab was destroyed
1963 return;
1964 }
John Reck34ef2672011-02-10 11:30:55 -08001965 final String url = tab.getUrl();
1966 final String originalUrl = view.getOriginalUrl();
1967
1968 if (TextUtils.isEmpty(url)) {
1969 return;
1970 }
1971
1972 // Only update thumbnails for web urls (http(s)://), not for
1973 // about:, javascript:, data:, etc...
1974 // Unless it is a bookmarked site, then always update
1975 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
1976 return;
1977 }
1978
Michael Kolb8233fac2010-10-26 16:08:53 -07001979 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
1980 getDesiredThumbnailHeight(mActivity));
1981 if (bm == null) {
1982 return;
1983 }
1984
1985 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08001986 new AsyncTask<Void, Void, Void>() {
1987 @Override
1988 protected Void doInBackground(Void... unused) {
1989 Cursor cursor = null;
1990 try {
1991 // TODO: Clean this up
1992 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
1993 if (cursor != null && cursor.moveToFirst()) {
1994 final ByteArrayOutputStream os =
1995 new ByteArrayOutputStream();
1996 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07001997
John Reck34ef2672011-02-10 11:30:55 -08001998 ContentValues values = new ContentValues();
1999 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002000
John Reck34ef2672011-02-10 11:30:55 -08002001 do {
John Reck617fd832011-02-16 14:35:59 -08002002 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002003 cr.update(Images.CONTENT_URI, values, null, null);
2004 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002005 }
John Reck34ef2672011-02-10 11:30:55 -08002006 } catch (IllegalStateException e) {
2007 // Ignore
2008 } finally {
2009 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002010 }
John Reck34ef2672011-02-10 11:30:55 -08002011 return null;
2012 }
2013 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002014 }
2015
2016 private class Copy implements OnMenuItemClickListener {
2017 private CharSequence mText;
2018
2019 public boolean onMenuItemClick(MenuItem item) {
2020 copy(mText);
2021 return true;
2022 }
2023
2024 public Copy(CharSequence toCopy) {
2025 mText = toCopy;
2026 }
2027 }
2028
Leon Scroggins63c02662010-11-18 15:16:27 -05002029 private static class Download implements OnMenuItemClickListener {
2030 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002031 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002032 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002033
2034 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002035 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002036 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002037 return true;
2038 }
2039
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002040 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002041 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002042 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002043 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002044 }
2045 }
2046
Cary Clark8974d282010-11-22 10:46:05 -05002047 private static class SelectText implements OnMenuItemClickListener {
2048 private WebView mWebView;
2049
2050 public boolean onMenuItemClick(MenuItem item) {
2051 if (mWebView != null) {
2052 return mWebView.selectText();
2053 }
2054 return false;
2055 }
2056
2057 public SelectText(WebView webView) {
2058 mWebView = webView;
2059 }
2060
2061 }
2062
Michael Kolb8233fac2010-10-26 16:08:53 -07002063 /********************** TODO: UI stuff *****************************/
2064
2065 // these methods have been copied, they still need to be cleaned up
2066
2067 /****************** tabs ***************************************************/
2068
2069 // basic tab interactions:
2070
2071 // it is assumed that tabcontrol already knows about the tab
2072 protected void addTab(Tab tab) {
2073 mUi.addTab(tab);
2074 }
2075
2076 protected void removeTab(Tab tab) {
2077 mUi.removeTab(tab);
2078 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002079 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002080 }
2081
Michael Kolbf2055602011-04-09 17:20:03 -07002082 @Override
2083 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002084 // monkey protection against delayed start
2085 if (tab != null) {
2086 mTabControl.setCurrentTab(tab);
2087 // the tab is guaranteed to have a webview after setCurrentTab
2088 mUi.setActiveTab(tab);
2089 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002090 }
2091
2092 protected void closeEmptyChildTab() {
2093 Tab current = mTabControl.getCurrentTab();
2094 if (current != null
2095 && current.getWebView().copyBackForwardList().getSize() == 0) {
Michael Kolbc831b632011-05-11 09:30:34 -07002096 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002097 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002098 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002099 closeTab(current);
2100 }
2101 }
2102 }
2103
John Reck26b18322011-06-21 13:08:58 -07002104 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002105 // Dismiss the subwindow if applicable.
2106 dismissSubWindow(appTab);
2107 // Since we might kill the WebView, remove it from the
2108 // content view first.
2109 mUi.detachTab(appTab);
2110 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002111 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002112 // TODO: analyze why the remove and add are necessary
2113 mUi.attachTab(appTab);
2114 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002115 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002116 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002117 } else {
2118 // If the tab was the current tab, we have to attach
2119 // it to the view system again.
2120 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002121 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002122 }
2123 }
2124
2125 // Remove the sub window if it exists. Also called by TabControl when the
2126 // user clicks the 'X' to dismiss a sub window.
2127 public void dismissSubWindow(Tab tab) {
2128 removeSubWindow(tab);
2129 // dismiss the subwindow. This will destroy the WebView.
2130 tab.dismissSubWindow();
2131 getCurrentTopWebView().requestFocus();
2132 }
2133
2134 @Override
2135 public void removeSubWindow(Tab t) {
2136 if (t.getSubWebView() != null) {
2137 mUi.removeSubWindow(t.getSubViewContainer());
2138 }
2139 }
2140
2141 @Override
2142 public void attachSubWindow(Tab tab) {
2143 if (tab.getSubWebView() != null) {
2144 mUi.attachSubWindow(tab.getSubViewContainer());
2145 getCurrentTopWebView().requestFocus();
2146 }
2147 }
2148
Mathew Inwood29721c22011-06-29 17:55:24 +01002149 private Tab showPreloadedTab(final UrlData urlData) {
2150 if (!urlData.isPreloaded()) {
2151 return null;
2152 }
2153 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2154 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2155 if (sbQuery != null) {
2156 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
2157 // Could not submit query. Fallback to regular tab creation
2158 tabControl.destroy();
2159 return null;
2160 }
2161 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002162 // check tab count and make room for new tab
2163 if (!mTabControl.canCreateNewTab()) {
2164 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2165 if (leastUsed != null) {
2166 closeTab(leastUsed);
2167 }
2168 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002169 Tab t = tabControl.getTab();
2170 mTabControl.addPreloadedTab(t);
2171 addTab(t);
2172 setActiveTab(t);
2173 return t;
2174 }
2175
Michael Kolb7bcafde2011-05-09 13:55:59 -07002176 // open a non inconito tab with the given url data
2177 // and set as active tab
2178 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002179 Tab tab = showPreloadedTab(urlData);
2180 if (tab == null) {
2181 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002182 if ((tab != null) && !urlData.isEmpty()) {
2183 loadUrlDataIn(tab, urlData);
2184 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002185 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002186 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002187 }
2188
Michael Kolb843510f2010-12-09 10:51:49 -08002189 @Override
2190 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002191 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002192 }
2193
Michael Kolb8233fac2010-10-26 16:08:53 -07002194 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002195 public Tab openIncognitoTab() {
2196 return openTab(INCOGNITO_URI, true, true, false);
2197 }
2198
2199 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002200 public Tab openTab(String url, boolean incognito, boolean setActive,
2201 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002202 return openTab(url, incognito, setActive, useCurrent, null);
2203 }
2204
2205 @Override
2206 public Tab openTab(String url, Tab parent, boolean setActive,
2207 boolean useCurrent) {
2208 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2209 setActive, useCurrent, parent);
2210 }
2211
2212 public Tab openTab(String url, boolean incognito, boolean setActive,
2213 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002214 Tab tab = createNewTab(incognito, setActive, useCurrent);
2215 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002216 if (parent != null && parent != tab) {
2217 parent.addChildTab(tab);
2218 }
Michael Kolb519d2282011-05-09 17:03:19 -07002219 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002220 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002221 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002222 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002223 return tab;
2224 }
2225
2226 // this method will attempt to create a new tab
2227 // incognito: private browsing tab
2228 // setActive: ste tab as current tab
2229 // useCurrent: if no new tab can be created, return current tab
2230 private Tab createNewTab(boolean incognito, boolean setActive,
2231 boolean useCurrent) {
2232 Tab tab = null;
2233 if (mTabControl.canCreateNewTab()) {
2234 tab = mTabControl.createNewTab(incognito);
2235 addTab(tab);
2236 if (setActive) {
2237 setActiveTab(tab);
2238 }
2239 } else {
2240 if (useCurrent) {
2241 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002242 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002243 } else {
2244 mUi.showMaxTabsWarning();
2245 }
2246 }
2247 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002248 }
2249
John Reck2bc80422011-06-30 15:11:49 -07002250 @Override
2251 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2252 SnapshotTab tab = null;
2253 if (mTabControl.canCreateNewTab()) {
2254 tab = mTabControl.createSnapshotTab(snapshotId);
2255 addTab(tab);
2256 if (setActive) {
2257 setActiveTab(tab);
2258 }
2259 } else {
2260 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002261 }
2262 return tab;
2263 }
2264
Michael Kolb8233fac2010-10-26 16:08:53 -07002265 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002266 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002267 * @return boolean True if we successfully switched to a different tab. If
2268 * the indexth tab is null, or if that tab is the same as
2269 * the current one, return false.
2270 */
2271 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002272 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002273 Tab currentTab = mTabControl.getCurrentTab();
2274 if (tab == null || tab == currentTab) {
2275 return false;
2276 }
2277 setActiveTab(tab);
2278 return true;
2279 }
2280
2281 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002282 public void closeCurrentTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002283 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002284 mCrashRecoveryHandler.clearState();
John Reck958b2422010-12-03 17:56:17 -08002285 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002286 return;
2287 }
Michael Kolbc831b632011-05-11 09:30:34 -07002288 final Tab current = mTabControl.getCurrentTab();
2289 final int pos = mTabControl.getCurrentPosition();
2290 Tab newTab = current.getParent();
2291 if (newTab == null) {
2292 newTab = mTabControl.getTab(pos + 1);
2293 if (newTab == null) {
2294 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002295 }
2296 }
Michael Kolbc831b632011-05-11 09:30:34 -07002297 if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002298 // Close window
2299 closeTab(current);
2300 }
2301 }
2302
2303 /**
2304 * Close the tab, remove its associated title bar, and adjust mTabControl's
2305 * current tab to a valid value.
2306 */
2307 @Override
2308 public void closeTab(Tab tab) {
Michael Kolb2d59c322011-01-25 13:18:55 -08002309 removeTab(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002310 }
2311
Michael Kolb8233fac2010-10-26 16:08:53 -07002312 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002313 protected void loadUrlFromContext(String url) {
2314 Tab tab = getCurrentTab();
2315 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002316 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002317 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002318 url = UrlUtils.smartUrlFilter(url);
2319 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002320 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002321 }
2322 }
2323 }
2324
2325 /**
2326 * Load the URL into the given WebView and update the title bar
2327 * to reflect the new load. Call this instead of WebView.loadUrl
2328 * directly.
2329 * @param view The WebView used to load url.
2330 * @param url The URL to load.
2331 */
John Reck71e51422011-07-01 16:49:28 -07002332 @Override
2333 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002334 loadUrl(tab, url, null);
2335 }
2336
2337 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2338 if (tab != null) {
2339 dismissSubWindow(tab);
2340 tab.loadUrl(url, headers);
2341 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002342 }
2343
2344 /**
2345 * Load UrlData into a Tab and update the title bar to reflect the new
2346 * load. Call this instead of UrlData.loadIn directly.
2347 * @param t The Tab used to load.
2348 * @param data The UrlData being loaded.
2349 */
2350 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002351 if (data != null) {
2352 if (data.mVoiceIntent != null) {
2353 t.activateVoiceSearchMode(data.mVoiceIntent);
Michael Kolb14612442011-06-24 13:06:29 -07002354 } else if (data.isPreloaded()) {
2355 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002356 } else {
2357 loadUrl(t, data.mUrl, data.mHeaders);
2358 }
2359 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002360 }
2361
John Reck30c714c2010-12-16 17:30:34 -08002362 @Override
2363 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002364 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002365 if (tab.canGoBack()) {
2366 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002367 } else {
John Reckef654f12011-07-12 16:42:08 -07002368 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002369 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002370 }
2371
2372 void goBackOnePageOrQuit() {
2373 Tab current = mTabControl.getCurrentTab();
2374 if (current == null) {
2375 /*
2376 * Instead of finishing the activity, simply push this to the back
2377 * of the stack and let ActivityManager to choose the foreground
2378 * activity. As BrowserActivity is singleTask, it will be always the
2379 * root of the task. So we can use either true or false for
2380 * moveTaskToBack().
2381 */
2382 mActivity.moveTaskToBack(true);
2383 return;
2384 }
John Reckef654f12011-07-12 16:42:08 -07002385 if (current.canGoBack()) {
2386 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002387 } else {
2388 // Check to see if we are closing a window that was created by
2389 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002390 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002391 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002392 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002393 // Now we close the other tab
2394 closeTab(current);
2395 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002396 /*
2397 * Instead of finishing the activity, simply push this to the back
2398 * of the stack and let ActivityManager to choose the foreground
2399 * activity. As BrowserActivity is singleTask, it will be always the
2400 * root of the task. So we can use either true or false for
2401 * moveTaskToBack().
2402 */
2403 mActivity.moveTaskToBack(true);
2404 }
2405 }
2406 }
2407
2408 /**
2409 * Feed the previously stored results strings to the BrowserProvider so that
2410 * the SearchDialog will show them instead of the standard searches.
2411 * @param result String to show on the editable line of the SearchDialog.
2412 */
2413 @Override
2414 public void showVoiceSearchResults(String result) {
2415 ContentProviderClient client = mActivity.getContentResolver()
2416 .acquireContentProviderClient(Browser.BOOKMARKS_URI);
2417 ContentProvider prov = client.getLocalContentProvider();
2418 BrowserProvider bp = (BrowserProvider) prov;
2419 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
2420 client.release();
2421
2422 Bundle bundle = createGoogleSearchSourceBundle(
2423 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
2424 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
2425 startSearch(result, false, bundle, false);
2426 }
2427
2428 private void startSearch(String initialQuery, boolean selectInitialQuery,
2429 Bundle appSearchData, boolean globalSearch) {
2430 if (appSearchData == null) {
2431 appSearchData = createGoogleSearchSourceBundle(
2432 GOOGLE_SEARCH_SOURCE_TYPE);
2433 }
2434
2435 SearchEngine searchEngine = mSettings.getSearchEngine();
2436 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
2437 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
2438 }
2439 mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData,
2440 globalSearch);
2441 }
2442
2443 private Bundle createGoogleSearchSourceBundle(String source) {
2444 Bundle bundle = new Bundle();
2445 bundle.putString(Search.SOURCE, source);
2446 return bundle;
2447 }
2448
2449 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002450 * helper method for key handler
2451 * returns the current tab if it can't advance
2452 */
Michael Kolbc831b632011-05-11 09:30:34 -07002453 private Tab getNextTab() {
2454 return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1,
2455 mTabControl.getCurrentPosition() + 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002456 }
2457
2458 /**
2459 * helper method for key handler
2460 * returns the current tab if it can't advance
2461 */
Michael Kolbc831b632011-05-11 09:30:34 -07002462 private Tab getPrevTab() {
2463 return mTabControl.getTab(Math.max(0,
2464 mTabControl.getCurrentPosition() - 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002465 }
2466
2467 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002468 * handle key events in browser
2469 *
2470 * @param keyCode
2471 * @param event
2472 * @return true if handled, false to pass to super
2473 */
2474 boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002475 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002476 // Even if MENU is already held down, we need to call to super to open
2477 // the IME on long press.
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002478 if (!noModifiers
2479 && ((KeyEvent.KEYCODE_MENU == keyCode)
2480 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2481 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002482 mMenuIsDown = true;
2483 return false;
2484 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002485
Cary Clark8ff8c662010-12-29 15:03:05 -05002486 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002487 Tab tab = getCurrentTab();
2488 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002489
Cary Clark160bbb92011-01-10 11:17:07 -05002490 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2491 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002492
Michael Kolb8233fac2010-10-26 16:08:53 -07002493 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002494 case KeyEvent.KEYCODE_TAB:
2495 if (event.isCtrlPressed()) {
2496 if (event.isShiftPressed()) {
2497 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002498 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002499 } else {
2500 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002501 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002502 }
2503 return true;
2504 }
2505 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002506 case KeyEvent.KEYCODE_SPACE:
2507 // WebView/WebTextView handle the keys in the KeyDown. As
2508 // the Activity's shortcut keys are only handled when WebView
2509 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002510 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002511 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002512 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002513 pageDown();
2514 }
2515 return true;
2516 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002517 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002518 event.startTracking();
2519 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002520 case KeyEvent.KEYCODE_FORWARD:
2521 if (!noModifiers) break;
2522 tab.goForward();
2523 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002524 case KeyEvent.KEYCODE_DPAD_LEFT:
2525 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002526 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002527 return true;
2528 }
2529 break;
2530 case KeyEvent.KEYCODE_DPAD_RIGHT:
2531 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002532 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002533 return true;
2534 }
2535 break;
2536 case KeyEvent.KEYCODE_A:
2537 if (ctrl) {
2538 webView.selectAll();
2539 return true;
2540 }
2541 break;
Michael Kolba4183062011-01-16 10:43:21 -08002542// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002543 case KeyEvent.KEYCODE_C:
2544 if (ctrl) {
2545 webView.copySelection();
2546 return true;
2547 }
2548 break;
Michael Kolba4183062011-01-16 10:43:21 -08002549// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002550// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002551// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002552// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002553// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002554// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002555// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002556// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002557// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002558// case KeyEvent.KEYCODE_M: // unused
2559// case KeyEvent.KEYCODE_N: // in Chrome: new window
2560// case KeyEvent.KEYCODE_O: // in Chrome: open file
2561// case KeyEvent.KEYCODE_P: // in Chrome: print page
2562// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002563// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002564// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2565 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002566 // we can't use the ctrl/shift flags, they check for
2567 // exclusive use of a modifier
2568 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002569 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002570 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002571 } else {
2572 openTabToHomePage();
2573 }
2574 return true;
2575 }
2576 break;
2577// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2578// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb1a2eba42011-03-16 16:42:49 -07002579 case KeyEvent.KEYCODE_W: // in Chrome: close tab
2580 if (ctrl) {
2581 closeCurrentTab();
2582 return true;
2583 }
2584 break;
Cary Clark8ff8c662010-12-29 15:03:05 -05002585// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2586// case KeyEvent.KEYCODE_Y: // unused
2587// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002588 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002589 // it is a regular key and webview is not null
2590 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002591 }
2592
John Recke6bf4ab2011-02-24 15:48:05 -08002593 boolean onKeyLongPress(int keyCode, KeyEvent event) {
2594 switch(keyCode) {
2595 case KeyEvent.KEYCODE_BACK:
2596 if (mUi.showsWeb()) {
2597 bookmarksOrHistoryPicker(true);
2598 return true;
2599 }
2600 break;
2601 }
2602 return false;
2603 }
2604
Michael Kolb8233fac2010-10-26 16:08:53 -07002605 boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb2814a362011-05-19 15:49:41 -07002606 if (KeyEvent.KEYCODE_MENU == keyCode) {
2607 mMenuIsDown = false;
2608 if (event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002609 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002610 }
2611 }
Cary Clark160bbb92011-01-10 11:17:07 -05002612 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002613 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002614 case KeyEvent.KEYCODE_BACK:
2615 if (event.isTracking() && !event.isCanceled()) {
2616 onBackKey();
2617 return true;
2618 }
2619 break;
2620 }
2621 return false;
2622 }
2623
2624 public boolean isMenuDown() {
2625 return mMenuIsDown;
2626 }
2627
Ben Murdoch8029a772010-11-16 11:58:21 +00002628 public void setupAutoFill(Message message) {
2629 // Open the settings activity at the AutoFill profile fragment so that
2630 // the user can create a new profile. When they return, we will dispatch
2631 // the message so that we can autofill the form using their new profile.
2632 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2633 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2634 AutoFillSettingsFragment.class.getName());
2635 mAutoFillSetupMessage = message;
2636 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2637 }
John Reckb3417f02011-01-14 11:01:05 -08002638
2639 @Override
Narayan Kamath5119edd2011-02-23 15:49:17 +00002640 public void registerDropdownChangeListener(DropdownChangeListener d) {
2641 mUi.registerDropdownChangeListener(d);
2642 }
Michael Kolbfbc579a2011-07-07 15:59:33 -07002643
2644 public boolean onSearchRequested() {
2645 mUi.editUrl(false);
2646 return true;
2647 }
2648
Michael Kolb8233fac2010-10-26 16:08:53 -07002649}