blob: 9a015b2db18eb404e69cb4920d23c9e1a019a91b [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
20import android.app.DownloadManager;
21import android.app.SearchManager;
22import android.content.ClipboardManager;
23import android.content.ContentProvider;
24import android.content.ContentProviderClient;
25import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070026import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.content.ContentValues;
28import android.content.Context;
29import android.content.Intent;
30import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
32import android.content.res.Configuration;
Leon Scroggins1961ed22010-12-07 15:22:21 -050033import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070034import android.database.Cursor;
35import android.database.sqlite.SQLiteDatabase;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.graphics.Bitmap;
37import android.graphics.Canvas;
38import android.graphics.Picture;
39import android.net.Uri;
40import android.net.http.SslError;
41import android.os.AsyncTask;
42import android.os.Bundle;
Leon Scrogginsac993842011-02-02 12:54:07 -050043import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070044import 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;
80import com.android.browser.UI.DropdownChangeListener;
81import com.android.browser.provider.BrowserProvider;
John Reckd8c74522011-06-14 08:45:00 -070082import com.android.browser.provider.BrowserProvider2.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070083import com.android.browser.search.SearchEngine;
84import com.android.common.Search;
85
Michael Kolb8233fac2010-10-26 16:08:53 -070086import java.io.ByteArrayOutputStream;
87import java.io.File;
88import java.net.URLEncoder;
89import java.util.Calendar;
90import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080091import java.util.List;
John Reck26b18322011-06-21 13:08:58 -070092import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -070093
94/**
95 * Controller for browser
96 */
97public class Controller
98 implements WebViewController, UiController {
99
100 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800101 private static final String SEND_APP_ID_EXTRA =
102 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700103 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800104
Michael Kolb8233fac2010-10-26 16:08:53 -0700105
106 // public message ids
107 public final static int LOAD_URL = 1001;
108 public final static int STOP_LOAD = 1002;
109
110 // Message Ids
111 private static final int FOCUS_NODE_HREF = 102;
112 private static final int RELEASE_WAKELOCK = 107;
113
114 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
115
116 private static final int OPEN_BOOKMARKS = 201;
117
118 private static final int EMPTY_MENU = -1;
119
Michael Kolb8233fac2010-10-26 16:08:53 -0700120 // activity requestCode
121 final static int PREFERENCES_PAGE = 3;
122 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000123 final static int AUTOFILL_SETUP = 5;
124
Michael Kolb8233fac2010-10-26 16:08:53 -0700125 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
126
127 // As the ids are dynamically created, we can't guarantee that they will
128 // be in sequence, so this static array maps ids to a window number.
129 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
130 { R.id.window_one_menu_id, R.id.window_two_menu_id,
131 R.id.window_three_menu_id, R.id.window_four_menu_id,
132 R.id.window_five_menu_id, R.id.window_six_menu_id,
133 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
134
135 // "source" parameter for Google search through search key
136 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
137 // "source" parameter for Google search through simplily type
138 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
139
Guang Zhu9e78f512011-05-04 11:45:11 -0700140 // "no-crash-recovery" parameter in intetnt to suppress crash recovery
141 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
142
Michael Kolb8233fac2010-10-26 16:08:53 -0700143 private Activity mActivity;
144 private UI mUi;
145 private TabControl mTabControl;
146 private BrowserSettings mSettings;
147 private WebViewFactory mFactory;
John Reckb3417f02011-01-14 11:01:05 -0800148 private OptionsMenuHandler mOptionsMenuHandler = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700149
150 private WakeLock mWakeLock;
151
152 private UrlHandler mUrlHandler;
153 private UploadHandler mUploadHandler;
154 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700155 private PageDialogsHandler mPageDialogsHandler;
156 private NetworkStateHandler mNetworkHandler;
Martijn Coenenb2f93552011-06-14 10:48:35 +0200157 private NfcHandler mNfcHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700158
Ben Murdoch8029a772010-11-16 11:58:21 +0000159 private Message mAutoFillSetupMessage;
160
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 private boolean mShouldShowErrorConsole;
162
163 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
164
165 // FIXME, temp address onPrepareMenu performance problem.
166 // When we move everything out of view, we should rewrite this.
167 private int mCurrentMenuState = 0;
168 private int mMenuState = R.id.MAIN_MENU;
169 private int mOldMenuState = EMPTY_MENU;
170 private Menu mCachedMenu;
171
Michael Kolb8233fac2010-10-26 16:08:53 -0700172 private boolean mMenuIsDown;
173
174 // For select and find, we keep track of the ActionMode so that
175 // finish() can be called as desired.
176 private ActionMode mActionMode;
177
178 /**
179 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
180 * of whether the configuration has changed. The first onMenuOpened call
181 * after a configuration change is simply a reopening of the same menu
182 * (i.e. mIconView did not change).
183 */
184 private boolean mConfigChanged;
185
186 /**
187 * Keeps track of whether the options menu is open. This is important in
188 * determining whether to show or hide the title bar overlay
189 */
190 private boolean mOptionsMenuOpen;
191
192 /**
193 * Whether or not the options menu is in its bigger, popup menu form. When
194 * true, we want the title bar overlay to be gone. When false, we do not.
195 * Only meaningful if mOptionsMenuOpen is true.
196 */
197 private boolean mExtendedMenuOpen;
198
199 private boolean mInLoad;
200
201 private boolean mActivityPaused = true;
202 private boolean mLoadStopped;
203
204 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500205 // Checks to see when the bookmarks database has changed, and updates the
206 // Tabs' notion of whether they represent bookmarked sites.
207 private ContentObserver mBookmarksObserver;
John Reck0ebd3ac2010-12-09 11:14:04 -0800208 private DataController mDataController;
John Reck847b5322011-04-14 17:02:18 -0700209 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700210
211 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
212 @Override
213 public Void doInBackground(File... files) {
214 if (files != null) {
215 for (File f : files) {
216 if (!f.delete()) {
217 Log.e(LOGTAG, f.getPath() + " was not deleted");
218 }
219 }
220 }
221 return null;
222 }
223 }
224
225 public Controller(Activity browser) {
226 mActivity = browser;
227 mSettings = BrowserSettings.getInstance();
John Reck0ebd3ac2010-12-09 11:14:04 -0800228 mDataController = DataController.getInstance(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700229 mTabControl = new TabControl(this);
230 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700231 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700232
233 mUrlHandler = new UrlHandler(this);
234 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700235 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200236 mNfcHandler = new NfcHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700237
238 PowerManager pm = (PowerManager) mActivity
239 .getSystemService(Context.POWER_SERVICE);
240 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
241
242 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500243 mBookmarksObserver = new ContentObserver(mHandler) {
244 @Override
245 public void onChange(boolean selfChange) {
246 int size = mTabControl.getTabCount();
247 for (int i = 0; i < size; i++) {
248 mTabControl.getTab(i).updateBookmarkedStatus();
249 }
250 }
251
252 };
253 browser.getContentResolver().registerContentObserver(
254 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700255
256 mNetworkHandler = new NetworkStateHandler(mActivity, this);
257 // Start watching the default geolocation permissions
258 mSystemAllowGeolocationOrigins =
259 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
260 mSystemAllowGeolocationOrigins.start();
261
262 retainIconsOnStartup();
263 }
264
Patrick Scott7d50a932011-02-04 09:27:26 -0500265 void start(final Bundle icicle, final Intent intent) {
Guang Zhu9e78f512011-05-04 11:45:11 -0700266 boolean noCrashRecovery = intent.getBooleanExtra(NO_CRASH_RECOVERY, false);
267 if (icicle != null || noCrashRecovery) {
John Reck847b5322011-04-14 17:02:18 -0700268 doStart(icicle, intent);
269 } else {
270 mCrashRecoveryHandler.startRecovery(intent);
271 }
272 }
273
274 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 // Unless the last browser usage was within 24 hours, destroy any
276 // remaining incognito tabs.
277
278 Calendar lastActiveDate = icicle != null ?
279 (Calendar) icicle.getSerializable("lastActiveDate") : null;
280 Calendar today = Calendar.getInstance();
281 Calendar yesterday = Calendar.getInstance();
282 yesterday.add(Calendar.DATE, -1);
283
Patrick Scott7d50a932011-02-04 09:27:26 -0500284 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700285 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800286 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700287
Patrick Scott7d50a932011-02-04 09:27:26 -0500288 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700289 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500290 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000291
Michael Kolbc831b632011-05-11 09:30:34 -0700292 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500293 // Not able to restore so we go ahead and clear session cookies. We
294 // must do this before trying to login the user as we don't want to
295 // clear any session cookies set during login.
296 CookieManager.getInstance().removeSessionCookie();
297 }
298
Patrick Scottd43e75a2011-03-14 14:47:23 -0400299 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500300 new Runnable() {
301 @Override public void run() {
Michael Kolbc831b632011-05-11 09:30:34 -0700302 onPreloginFinished(icicle, intent, currentTabId, restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500303 }
304 });
305 }
306
Michael Kolbc831b632011-05-11 09:30:34 -0700307 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500308 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700309 if (currentTabId == -1) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700310 final Bundle extra = intent.getExtras();
311 // Create an initial tab.
312 // If the intent is ACTION_VIEW and data is not null, the Browser is
313 // invoked to view the content by another application. In this case,
314 // the tab will be close when exit.
315 UrlData urlData = mIntentHandler.getUrlDataFromIntent(intent);
Michael Kolb7bcafde2011-05-09 13:55:59 -0700316 Tab t = null;
317 if (urlData.isEmpty()) {
318 t = openTabToHomePage();
319 } else {
320 t = openTab(urlData);
321 }
322 if (t != null) {
323 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
324 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700325 WebView webView = t.getWebView();
326 if (extra != null) {
327 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
328 if (scale > 0 && scale <= 1000) {
329 webView.setInitialScale(scale);
330 }
331 }
John Reckd8c74522011-06-14 08:45:00 -0700332 mTabControl.loadSnapshotTabs();
333 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700334 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700335 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500336 mUi.needsRestoreAllTabs());
Michael Kolb1bf23132010-11-19 12:55:12 -0800337 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700338 // TabControl.restoreState() will create a new tab even if
339 // restoring the state fails.
340 setActiveTab(mTabControl.getCurrentTab());
341 }
342 // clear up the thumbnail directory, which is no longer used;
343 // ideally this should only be run once after an upgrade from
344 // a previous version of the browser
345 new ClearThumbnails().execute(mTabControl.getThumbnailDir()
346 .listFiles());
347 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700348 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700349 if (jsFlags.trim().length() != 0) {
350 getCurrentWebView().setJsFlags(jsFlags);
351 }
John Reck439c9a52010-12-14 10:04:39 -0800352 if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
353 bookmarksOrHistoryPicker(false);
354 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700355 }
356
357 void setWebViewFactory(WebViewFactory factory) {
358 mFactory = factory;
359 }
360
Michael Kolb1514bb72010-11-22 09:11:48 -0800361 @Override
362 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700363 return mFactory;
364 }
365
366 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800367 public void onSetWebView(Tab tab, WebView view) {
368 mUi.onSetWebView(tab, view);
369 }
370
371 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800372 public void createSubWindow(Tab tab) {
373 endActionMode();
374 WebView mainView = tab.getWebView();
375 WebView subView = mFactory.createWebView((mainView == null)
376 ? false
377 : mainView.isPrivateBrowsingEnabled());
378 mUi.createSubWindow(tab, subView);
379 }
380
381 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700382 public Activity getActivity() {
383 return mActivity;
384 }
385
386 void setUi(UI ui) {
387 mUi = ui;
388 }
389
390 BrowserSettings getSettings() {
391 return mSettings;
392 }
393
394 IntentHandler getIntentHandler() {
395 return mIntentHandler;
396 }
397
398 @Override
399 public UI getUi() {
400 return mUi;
401 }
402
403 int getMaxTabs() {
404 return mActivity.getResources().getInteger(R.integer.max_tabs);
405 }
406
407 @Override
408 public TabControl getTabControl() {
409 return mTabControl;
410 }
411
Michael Kolb1bf23132010-11-19 12:55:12 -0800412 @Override
413 public List<Tab> getTabs() {
414 return mTabControl.getTabs();
415 }
416
Michael Kolb8233fac2010-10-26 16:08:53 -0700417 // Open the icon database and retain all the icons for visited sites.
Ben Murdoch9446b932010-11-25 16:20:14 +0000418 // This is done on a background thread so as not to stall startup.
Michael Kolb8233fac2010-10-26 16:08:53 -0700419 private void retainIconsOnStartup() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000420 // WebIconDatabase needs to be retrieved on the UI thread so that if
421 // it has not been created successfully yet the Handler is started on the
422 // UI thread.
423 new RetainIconsOnStartupTask(WebIconDatabase.getInstance()).execute();
424 }
425
426 private class RetainIconsOnStartupTask extends AsyncTask<Void, Void, Void> {
427 private WebIconDatabase mDb;
428
429 public RetainIconsOnStartupTask(WebIconDatabase db) {
430 mDb = db;
431 }
432
John Recka00cbbd2010-12-16 12:38:19 -0800433 @Override
Ben Murdoch9446b932010-11-25 16:20:14 +0000434 protected Void doInBackground(Void... unused) {
435 mDb.open(mActivity.getDir("icons", 0).getPath());
436 Cursor c = null;
437 try {
438 c = Browser.getAllBookmarks(mActivity.getContentResolver());
439 if (c.moveToFirst()) {
440 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
441 do {
442 String url = c.getString(urlIndex);
443 mDb.retainIconForPageUrl(url);
444 } while (c.moveToNext());
445 }
446 } catch (IllegalStateException e) {
447 Log.e(LOGTAG, "retainIconsOnStartup", e);
448 } finally {
449 if (c != null) c.close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700450 }
Ben Murdoch9446b932010-11-25 16:20:14 +0000451
452 return null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700453 }
454 }
455
456 private void startHandler() {
457 mHandler = new Handler() {
458
459 @Override
460 public void handleMessage(Message msg) {
461 switch (msg.what) {
462 case OPEN_BOOKMARKS:
463 bookmarksOrHistoryPicker(false);
464 break;
465 case FOCUS_NODE_HREF:
466 {
467 String url = (String) msg.getData().get("url");
468 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500469 String src = (String) msg.getData().get("src");
470 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700471 if (TextUtils.isEmpty(url)) {
472 break;
473 }
474 HashMap focusNodeMap = (HashMap) msg.obj;
475 WebView view = (WebView) focusNodeMap.get("webview");
476 // Only apply the action if the top window did not change.
477 if (getCurrentTopWebView() != view) {
478 break;
479 }
480 switch (msg.arg1) {
481 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700482 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700483 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500484 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700485 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500486 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500487 case R.id.open_newtab_context_menu_id:
488 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700489 openTab(url, parent,
490 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500491 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700492 case R.id.copy_link_context_menu_id:
493 copy(url);
494 break;
495 case R.id.save_link_context_menu_id:
496 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500497 DownloadHandler.onDownloadStartNoStream(
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000498 mActivity, url, null, null, null,
499 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700500 break;
501 }
502 break;
503 }
504
505 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700506 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700507 break;
508
509 case STOP_LOAD:
510 stopLoading();
511 break;
512
513 case RELEASE_WAKELOCK:
514 if (mWakeLock.isHeld()) {
515 mWakeLock.release();
516 // if we reach here, Browser should be still in the
517 // background loading after WAKELOCK_TIMEOUT (5-min).
518 // To avoid burning the battery, stop loading.
519 mTabControl.stopAllLoading();
520 }
521 break;
522
523 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800524 Tab tab = (Tab) msg.obj;
525 if (tab != null) {
526 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700527 }
528 break;
529 }
530 }
531 };
532
533 }
534
Martijn Coenenb2f93552011-06-14 10:48:35 +0200535
536 public Tab getCurrentTab() {
537 return mTabControl.getCurrentTab();
538 }
539
Michael Kolbba99c5d2010-11-29 14:57:41 -0800540 @Override
541 public void shareCurrentPage() {
542 shareCurrentPage(mTabControl.getCurrentTab());
543 }
544
545 private void shareCurrentPage(Tab tab) {
546 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800547 sharePage(mActivity, tab.getTitle(),
548 tab.getUrl(), tab.getFavicon(),
549 createScreenshot(tab.getWebView(),
550 getDesiredThumbnailWidth(mActivity),
551 getDesiredThumbnailHeight(mActivity)));
552 }
553 }
554
Michael Kolb8233fac2010-10-26 16:08:53 -0700555 /**
556 * Share a page, providing the title, url, favicon, and a screenshot. Uses
557 * an {@link Intent} to launch the Activity chooser.
558 * @param c Context used to launch a new Activity.
559 * @param title Title of the page. Stored in the Intent with
560 * {@link Intent#EXTRA_SUBJECT}
561 * @param url URL of the page. Stored in the Intent with
562 * {@link Intent#EXTRA_TEXT}
563 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
564 * with {@link Browser#EXTRA_SHARE_FAVICON}
565 * @param screenshot Bitmap of a screenshot of the page. Stored in the
566 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
567 */
568 static final void sharePage(Context c, String title, String url,
569 Bitmap favicon, Bitmap screenshot) {
570 Intent send = new Intent(Intent.ACTION_SEND);
571 send.setType("text/plain");
572 send.putExtra(Intent.EXTRA_TEXT, url);
573 send.putExtra(Intent.EXTRA_SUBJECT, title);
574 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
575 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
576 try {
577 c.startActivity(Intent.createChooser(send, c.getString(
578 R.string.choosertitle_sharevia)));
579 } catch(android.content.ActivityNotFoundException ex) {
580 // if no app handles it, do nothing
581 }
582 }
583
584 private void copy(CharSequence text) {
585 ClipboardManager cm = (ClipboardManager) mActivity
586 .getSystemService(Context.CLIPBOARD_SERVICE);
587 cm.setText(text);
588 }
589
590 // lifecycle
591
592 protected void onConfgurationChanged(Configuration config) {
593 mConfigChanged = true;
594 if (mPageDialogsHandler != null) {
595 mPageDialogsHandler.onConfigurationChanged(config);
596 }
597 mUi.onConfigurationChanged(config);
598 }
599
600 @Override
601 public void handleNewIntent(Intent intent) {
602 mIntentHandler.onNewIntent(intent);
603 }
604
605 protected void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800606 if (mUi.isCustomViewShowing()) {
607 hideCustomView();
608 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700609 if (mActivityPaused) {
610 Log.e(LOGTAG, "BrowserActivity is already paused.");
611 return;
612 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700613 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800614 Tab tab = mTabControl.getCurrentTab();
615 if (tab != null) {
616 tab.pause();
617 if (!pauseWebViewTimers(tab)) {
618 mWakeLock.acquire();
619 mHandler.sendMessageDelayed(mHandler
620 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
621 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700622 }
623 mUi.onPause();
624 mNetworkHandler.onPause();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200625 mNfcHandler.onPause();
Michael Kolb8233fac2010-10-26 16:08:53 -0700626
627 WebView.disablePlatformNotifications();
John Reck378a4102011-06-09 16:23:01 -0700628 mCrashRecoveryHandler.backupState();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200629
Michael Kolb8233fac2010-10-26 16:08:53 -0700630 }
631
John Reckaed9c542011-05-27 16:08:53 -0700632 void onSaveInstanceState(Bundle outState, boolean saveImages) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700633 // the default implementation requires each view to have an id. As the
634 // browser handles the state itself and it doesn't use id for the views,
635 // don't call the default implementation. Otherwise it will trigger the
636 // warning like this, "couldn't save which view has focus because the
637 // focused view XXX has no id".
638
639 // Save all the tabs
John Reckaed9c542011-05-27 16:08:53 -0700640 mTabControl.saveState(outState, saveImages);
John Reck24f18262011-06-17 14:47:20 -0700641 if (!outState.isEmpty()) {
642 // Save time so that we know how old incognito tabs (if any) are.
643 outState.putSerializable("lastActiveDate", Calendar.getInstance());
644 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700645 }
646
647 void onResume() {
648 if (!mActivityPaused) {
649 Log.e(LOGTAG, "BrowserActivity is already resumed.");
650 return;
651 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800653 Tab current = mTabControl.getCurrentTab();
654 if (current != null) {
655 current.resume();
656 resumeWebViewTimers(current);
657 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700658 if (mWakeLock.isHeld()) {
659 mHandler.removeMessages(RELEASE_WAKELOCK);
660 mWakeLock.release();
661 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200662
Michael Kolb8233fac2010-10-26 16:08:53 -0700663 mUi.onResume();
664 mNetworkHandler.onResume();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200665 mNfcHandler.onResume();
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 WebView.enablePlatformNotifications();
667 }
668
Michael Kolb70976932010-11-30 11:34:01 -0800669 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800670 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800671 * @param tab guaranteed non-null
672 */
673 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700674 boolean inLoad = tab.inPageLoad();
675 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
676 CookieSyncManager.getInstance().startSync();
677 WebView w = tab.getWebView();
678 if (w != null) {
679 w.resumeTimers();
680 }
681 }
682 }
683
Michael Kolb70976932010-11-30 11:34:01 -0800684 /**
685 * Pause all WebView timers using the WebView of the given tab
686 * @param tab
687 * @return true if the timers are paused or tab is null
688 */
689 private boolean pauseWebViewTimers(Tab tab) {
690 if (tab == null) {
691 return true;
692 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700693 CookieSyncManager.getInstance().stopSync();
694 WebView w = getCurrentWebView();
695 if (w != null) {
696 w.pauseTimers();
697 }
698 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700699 }
Michael Kolb70976932010-11-30 11:34:01 -0800700 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700701 }
702
703 void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800704 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
706 mUploadHandler = null;
707 }
708 if (mTabControl == null) return;
709 mUi.onDestroy();
710 // Remove the current tab and sub window
711 Tab t = mTabControl.getCurrentTab();
712 if (t != null) {
713 dismissSubWindow(t);
714 removeTab(t);
715 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500716 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700717 // Destroy all the tabs
718 mTabControl.destroy();
719 WebIconDatabase.getInstance().close();
720 // Stop watching the default geolocation permissions
721 mSystemAllowGeolocationOrigins.stop();
722 mSystemAllowGeolocationOrigins = null;
723 }
724
725 protected boolean isActivityPaused() {
726 return mActivityPaused;
727 }
728
729 protected void onLowMemory() {
730 mTabControl.freeMemory();
731 }
732
733 @Override
734 public boolean shouldShowErrorConsole() {
735 return mShouldShowErrorConsole;
736 }
737
738 protected void setShouldShowErrorConsole(boolean show) {
739 if (show == mShouldShowErrorConsole) {
740 // Nothing to do.
741 return;
742 }
743 mShouldShowErrorConsole = show;
744 Tab t = mTabControl.getCurrentTab();
745 if (t == null) {
746 // There is no current tab so we cannot toggle the error console
747 return;
748 }
749 mUi.setShouldShowErrorConsole(t, show);
750 }
751
752 @Override
753 public void stopLoading() {
754 mLoadStopped = true;
755 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700756 WebView w = getCurrentTopWebView();
757 w.stopLoading();
Michael Kolb8233fac2010-10-26 16:08:53 -0700758 mUi.onPageStopped(tab);
759 }
760
761 boolean didUserStopLoading() {
762 return mLoadStopped;
763 }
764
765 // WebViewController
766
767 @Override
John Reck324d4402011-01-11 16:56:42 -0800768 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700769
770 // We've started to load a new page. If there was a pending message
771 // to save a screenshot then we will now take the new page and save
772 // an incorrect screenshot. Therefore, remove any pending thumbnail
773 // messages from the queue.
774 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800775 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700776
777 // reset sync timer to avoid sync starts during loading a page
778 CookieSyncManager.getInstance().resetSync();
779
780 if (!mNetworkHandler.isNetworkUp()) {
781 view.setNetworkAvailable(false);
782 }
783
784 // when BrowserActivity just starts, onPageStarted may be called before
785 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
786 // to start the timer. As we won't switch tabs while an activity is in
787 // pause state, we can ensure calling resume and pause in pair.
788 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800789 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700790 }
791 mLoadStopped = false;
792 if (!mNetworkHandler.isNetworkUp()) {
793 mNetworkHandler.createAndShowNetworkDialog();
794 }
795 endActionMode();
796
John Reck30c714c2010-12-16 17:30:34 -0800797 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700798
John Reck324d4402011-01-11 16:56:42 -0800799 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700800 // update the bookmark database for favicon
801 maybeUpdateFavicon(tab, null, url, favicon);
802
803 Performance.tracePageStart(url);
804
805 // Performance probe
806 if (false) {
807 Performance.onPageStarted();
808 }
809
810 }
811
812 @Override
John Reck324d4402011-01-11 16:56:42 -0800813 public void onPageFinished(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800814 mUi.onTabDataChanged(tab);
John Reck324d4402011-01-11 16:56:42 -0800815 if (!tab.isPrivateBrowsingEnabled()
John Reckd8c74522011-06-14 08:45:00 -0700816 && !TextUtils.isEmpty(tab.getUrl())
817 && !tab.isSnapshot()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700818 if (tab.inForeground() && !didUserStopLoading()
819 || !tab.inForeground()) {
820 // Only update the bookmark screenshot if the user did not
821 // cancel the load early.
822 mHandler.sendMessageDelayed(mHandler.obtainMessage(
John Reck34ef2672011-02-10 11:30:55 -0800823 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Michael Kolb8233fac2010-10-26 16:08:53 -0700824 500);
825 }
826 }
827 // pause the WebView timer and release the wake lock if it is finished
828 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800829 if (mActivityPaused && pauseWebViewTimers(tab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700830 if (mWakeLock.isHeld()) {
831 mHandler.removeMessages(RELEASE_WAKELOCK);
832 mWakeLock.release();
833 }
834 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200835
Michael Kolb8233fac2010-10-26 16:08:53 -0700836 // Performance probe
837 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800838 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 }
840
841 Performance.tracePageFinished();
842 }
843
844 @Override
John Reck30c714c2010-12-16 17:30:34 -0800845 public void onProgressChanged(Tab tab) {
846 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700847
848 if (newProgress == 100) {
849 CookieSyncManager.getInstance().sync();
850 // onProgressChanged() may continue to be called after the main
851 // frame has finished loading, as any remaining sub frames continue
852 // to load. We'll only get called once though with newProgress as
853 // 100 when everything is loaded. (onPageFinished is called once
854 // when the main frame completes loading regardless of the state of
855 // any sub frames so calls to onProgressChanges may continue after
856 // onPageFinished has executed)
857 if (mInLoad) {
858 mInLoad = false;
859 updateInLoadMenuItems(mCachedMenu);
860 }
861 } else {
862 if (!mInLoad) {
863 // onPageFinished may have already been called but a subframe is
864 // still loading and updating the progress. Reset mInLoad and
865 // update the menu items.
866 mInLoad = true;
867 updateInLoadMenuItems(mCachedMenu);
868 }
869 }
John Reck30c714c2010-12-16 17:30:34 -0800870 mUi.onProgressChanged(tab);
871 }
872
873 @Override
874 public void onUpdatedLockIcon(Tab tab) {
875 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700876 }
877
878 @Override
879 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800880 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800881 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800882 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
884 return;
885 }
886 // Update the title in the history database if not in private browsing mode
887 if (!tab.isPrivateBrowsingEnabled()) {
John Reck0ebd3ac2010-12-09 11:14:04 -0800888 mDataController.updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700889 }
890 }
891
892 @Override
893 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800894 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700895 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
896 }
897
898 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800899 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
900 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700901 }
902
903 @Override
904 public boolean shouldOverrideKeyEvent(KeyEvent event) {
905 if (mMenuIsDown) {
906 // only check shortcut key when MENU is held
907 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
908 event);
909 } else {
910 return false;
911 }
912 }
913
914 @Override
915 public void onUnhandledKeyEvent(KeyEvent event) {
916 if (!isActivityPaused()) {
917 if (event.getAction() == KeyEvent.ACTION_DOWN) {
918 mActivity.onKeyDown(event.getKeyCode(), event);
919 } else {
920 mActivity.onKeyUp(event.getKeyCode(), event);
921 }
922 }
923 }
924
925 @Override
John Reck324d4402011-01-11 16:56:42 -0800926 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700927 // Don't save anything in private browsing mode
928 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800929 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700930
John Reck324d4402011-01-11 16:56:42 -0800931 if (TextUtils.isEmpty(url)
932 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700933 return;
934 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800935 mDataController.updateVisitedHistory(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700936 WebIconDatabase.getInstance().retainIconForPageUrl(url);
John Reck847b5322011-04-14 17:02:18 -0700937 if (!mActivityPaused) {
938 // Since we clear the state in onPause, don't backup the current
939 // state if we are already paused
940 mCrashRecoveryHandler.backupState();
941 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700942 }
943
944 @Override
945 public void getVisitedHistory(final ValueCallback<String[]> callback) {
946 AsyncTask<Void, Void, String[]> task =
947 new AsyncTask<Void, Void, String[]>() {
948 @Override
949 public String[] doInBackground(Void... unused) {
950 return Browser.getVisitedHistory(mActivity.getContentResolver());
951 }
952 @Override
953 public void onPostExecute(String[] result) {
954 callback.onReceiveValue(result);
955 }
956 };
957 task.execute();
958 }
959
960 @Override
961 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
962 final HttpAuthHandler handler, final String host,
963 final String realm) {
964 String username = null;
965 String password = null;
966
967 boolean reuseHttpAuthUsernamePassword
968 = handler.useHttpAuthUsernamePassword();
969
970 if (reuseHttpAuthUsernamePassword && view != null) {
971 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
972 if (credentials != null && credentials.length == 2) {
973 username = credentials[0];
974 password = credentials[1];
975 }
976 }
977
978 if (username != null && password != null) {
979 handler.proceed(username, password);
980 } else {
981 if (tab.inForeground()) {
982 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
983 } else {
984 handler.cancel();
985 }
986 }
987 }
988
989 @Override
990 public void onDownloadStart(Tab tab, String url, String userAgent,
991 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000992 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -0500993 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000994 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
995 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700996 // This Tab was opened for the sole purpose of downloading a
997 // file. Remove it.
998 if (tab == mTabControl.getCurrentTab()) {
999 // In this case, the Tab is still on top.
1000 goBackOnePageOrQuit();
1001 } else {
1002 // In this case, it is not.
1003 closeTab(tab);
1004 }
1005 }
1006 }
1007
1008 @Override
1009 public Bitmap getDefaultVideoPoster() {
1010 return mUi.getDefaultVideoPoster();
1011 }
1012
1013 @Override
1014 public View getVideoLoadingProgressView() {
1015 return mUi.getVideoLoadingProgressView();
1016 }
1017
1018 @Override
1019 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1020 SslError error) {
1021 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1022 }
1023
Patrick Scott92066772011-03-10 08:46:27 -05001024 @Override
1025 public void showAutoLogin(Tab tab) {
1026 assert tab.inForeground();
1027 // Update the title bar to show the auto-login request.
1028 mUi.showAutoLogin(tab);
1029 }
1030
1031 @Override
1032 public void hideAutoLogin(Tab tab) {
1033 assert tab.inForeground();
1034 mUi.hideAutoLogin(tab);
1035 }
1036
Michael Kolb8233fac2010-10-26 16:08:53 -07001037 // helper method
1038
1039 /*
1040 * Update the favorites icon if the private browsing isn't enabled and the
1041 * icon is valid.
1042 */
1043 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1044 final String url, Bitmap favicon) {
1045 if (favicon == null) {
1046 return;
1047 }
1048 if (!tab.isPrivateBrowsingEnabled()) {
1049 Bookmarks.updateFavicon(mActivity
1050 .getContentResolver(), originalUrl, url, favicon);
1051 }
1052 }
1053
Leon Scroggins4cd97792010-12-03 15:31:56 -05001054 @Override
1055 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001056 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001057 mUi.bookmarkedStatusHasChanged(tab);
1058 }
1059
Michael Kolb8233fac2010-10-26 16:08:53 -07001060 // end WebViewController
1061
1062 protected void pageUp() {
1063 getCurrentTopWebView().pageUp(false);
1064 }
1065
1066 protected void pageDown() {
1067 getCurrentTopWebView().pageDown(false);
1068 }
1069
1070 // callback from phone title bar
1071 public void editUrl() {
1072 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001073 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001074 }
1075
Michael Kolbcfa3af52010-12-14 10:36:11 -08001076 public void startVoiceSearch() {
1077 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1078 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1079 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1080 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1081 mActivity.getComponentName().flattenToString());
1082 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001083 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001084 mActivity.startActivity(intent);
1085 }
1086
Michael Kolb11d19782011-03-20 10:17:40 -07001087 @Override
1088 public void activateVoiceSearchMode(String title, List<String> results) {
1089 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001090 }
1091
1092 public void revertVoiceSearchMode(Tab tab) {
1093 mUi.revertVoiceTitleBar(tab);
1094 }
1095
Michael Kolb736990c2011-03-20 10:01:20 -07001096 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001097 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001098 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1099 }
1100
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001101 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001102 WebChromeClient.CustomViewCallback callback) {
1103 if (tab.inForeground()) {
1104 if (mUi.isCustomViewShowing()) {
1105 callback.onCustomViewHidden();
1106 return;
1107 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001108 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001109 // Save the menu state and set it to empty while the custom
1110 // view is showing.
1111 mOldMenuState = mMenuState;
1112 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001113 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001114 }
1115 }
1116
1117 @Override
1118 public void hideCustomView() {
1119 if (mUi.isCustomViewShowing()) {
1120 mUi.onHideCustomView();
1121 // Reset the old menu state.
1122 mMenuState = mOldMenuState;
1123 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001124 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001125 }
1126 }
1127
1128 protected void onActivityResult(int requestCode, int resultCode,
1129 Intent intent) {
1130 if (getCurrentTopWebView() == null) return;
1131 switch (requestCode) {
1132 case PREFERENCES_PAGE:
1133 if (resultCode == Activity.RESULT_OK && intent != null) {
1134 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001135 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001136 mTabControl.removeParentChildRelationShips();
1137 }
1138 }
1139 break;
1140 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001141 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001142 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001143 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001144 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001145 case AUTOFILL_SETUP:
1146 // Determine whether a profile was actually set up or not
1147 // and if so, send the message back to the WebTextView to
1148 // fill the form with the new profile.
1149 if (getSettings().getAutoFillProfile() != null) {
1150 mAutoFillSetupMessage.sendToTarget();
1151 mAutoFillSetupMessage = null;
1152 }
1153 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001154 default:
1155 break;
1156 }
1157 getCurrentTopWebView().requestFocus();
1158 }
1159
1160 /**
1161 * Open the Go page.
1162 * @param startWithHistory If true, open starting on the history tab.
1163 * Otherwise, start with the bookmarks tab.
1164 */
1165 @Override
1166 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1167 if (mTabControl.getCurrentWebView() == null) {
1168 return;
1169 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001170 // clear action mode
1171 if (isInCustomActionMode()) {
1172 endActionMode();
1173 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001174 Bundle extras = new Bundle();
1175 // Disable opening in a new window if we have maxed out the windows
1176 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1177 !mTabControl.canCreateNewTab());
1178 mUi.showComboView(startWithHistory, extras);
1179 }
1180
1181 // combo view callbacks
1182
1183 /**
1184 * callback from ComboPage when clear history is requested
1185 */
1186 public void onRemoveParentChildRelationships() {
1187 mTabControl.removeParentChildRelationShips();
1188 }
1189
1190 /**
1191 * callback from ComboPage when bookmark/history selection
1192 */
1193 @Override
1194 public void onUrlSelected(String url, boolean newTab) {
1195 removeComboView();
1196 if (!TextUtils.isEmpty(url)) {
1197 if (newTab) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07001198 final Tab parent = mTabControl.getCurrentTab();
1199 openTab(url,
1200 (parent != null) && parent.isPrivateBrowsingEnabled(),
1201 !mSettings.openInBackground(),
1202 true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001203 } else {
1204 final Tab currentTab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001205 loadUrl(currentTab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001206 }
1207 }
1208 }
1209
1210 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001211 * dismiss the ComboPage
1212 */
1213 @Override
1214 public void removeComboView() {
1215 mUi.hideComboView();
1216 }
1217
1218 // active tabs page handling
1219
1220 protected void showActiveTabsPage() {
1221 mMenuState = EMPTY_MENU;
1222 mUi.showActiveTabsPage();
1223 }
1224
1225 /**
1226 * Remove the active tabs page.
1227 * @param needToAttach If true, the active tabs page did not attach a tab
1228 * to the content view, so we need to do that here.
1229 */
1230 @Override
1231 public void removeActiveTabsPage(boolean needToAttach) {
1232 mMenuState = R.id.MAIN_MENU;
1233 mUi.removeActiveTabsPage();
1234 if (needToAttach) {
1235 setActiveTab(mTabControl.getCurrentTab());
1236 }
1237 getCurrentTopWebView().requestFocus();
1238 }
1239
1240 // key handling
1241 protected void onBackKey() {
1242 if (!mUi.onBackKey()) {
1243 WebView subwindow = mTabControl.getCurrentSubWindow();
1244 if (subwindow != null) {
1245 if (subwindow.canGoBack()) {
1246 subwindow.goBack();
1247 } else {
1248 dismissSubWindow(mTabControl.getCurrentTab());
1249 }
1250 } else {
1251 goBackOnePageOrQuit();
1252 }
1253 }
1254 }
1255
Michael Kolb4bd767d2011-05-27 11:33:55 -07001256 protected boolean onMenuKey() {
1257 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001258 }
1259
Michael Kolb8233fac2010-10-26 16:08:53 -07001260 // menu handling and state
1261 // TODO: maybe put into separate handler
1262
1263 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckb3417f02011-01-14 11:01:05 -08001264 if (mOptionsMenuHandler != null) {
1265 return mOptionsMenuHandler.onCreateOptionsMenu(menu);
1266 }
1267
John Reckd73c5a22010-12-22 10:22:50 -08001268 if (mMenuState == EMPTY_MENU) {
1269 return false;
1270 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001271 MenuInflater inflater = mActivity.getMenuInflater();
1272 inflater.inflate(R.menu.browser, menu);
1273 updateInLoadMenuItems(menu);
1274 // hold on to the menu reference here; it is used by the page callbacks
1275 // to update the menu based on loading state
1276 mCachedMenu = menu;
1277 return true;
1278 }
1279
1280 protected void onCreateContextMenu(ContextMenu menu, View v,
1281 ContextMenuInfo menuInfo) {
1282 if (v instanceof TitleBarBase) {
1283 return;
1284 }
1285 if (!(v instanceof WebView)) {
1286 return;
1287 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001288 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001289 WebView.HitTestResult result = webview.getHitTestResult();
1290 if (result == null) {
1291 return;
1292 }
1293
1294 int type = result.getType();
1295 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1296 Log.w(LOGTAG,
1297 "We should not show context menu when nothing is touched");
1298 return;
1299 }
1300 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1301 // let TextView handles context menu
1302 return;
1303 }
1304
1305 // Note, http://b/issue?id=1106666 is requesting that
1306 // an inflated menu can be used again. This is not available
1307 // yet, so inflate each time (yuk!)
1308 MenuInflater inflater = mActivity.getMenuInflater();
1309 inflater.inflate(R.menu.browsercontext, menu);
1310
1311 // Show the correct menu group
1312 final String extra = result.getExtra();
1313 menu.setGroupVisible(R.id.PHONE_MENU,
1314 type == WebView.HitTestResult.PHONE_TYPE);
1315 menu.setGroupVisible(R.id.EMAIL_MENU,
1316 type == WebView.HitTestResult.EMAIL_TYPE);
1317 menu.setGroupVisible(R.id.GEO_MENU,
1318 type == WebView.HitTestResult.GEO_TYPE);
1319 menu.setGroupVisible(R.id.IMAGE_MENU,
1320 type == WebView.HitTestResult.IMAGE_TYPE
1321 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1322 menu.setGroupVisible(R.id.ANCHOR_MENU,
1323 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1324 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001325 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1326 || type == WebView.HitTestResult.PHONE_TYPE
1327 || type == WebView.HitTestResult.EMAIL_TYPE
1328 || type == WebView.HitTestResult.GEO_TYPE;
1329 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1330 if (hitText) {
1331 menu.findItem(R.id.select_text_menu_id)
1332 .setOnMenuItemClickListener(new SelectText(webview));
1333 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001334 // Setup custom handling depending on the type
1335 switch (type) {
1336 case WebView.HitTestResult.PHONE_TYPE:
1337 menu.setHeaderTitle(Uri.decode(extra));
1338 menu.findItem(R.id.dial_context_menu_id).setIntent(
1339 new Intent(Intent.ACTION_VIEW, Uri
1340 .parse(WebView.SCHEME_TEL + extra)));
1341 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1342 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1343 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1344 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1345 addIntent);
1346 menu.findItem(R.id.copy_phone_context_menu_id)
1347 .setOnMenuItemClickListener(
1348 new Copy(extra));
1349 break;
1350
1351 case WebView.HitTestResult.EMAIL_TYPE:
1352 menu.setHeaderTitle(extra);
1353 menu.findItem(R.id.email_context_menu_id).setIntent(
1354 new Intent(Intent.ACTION_VIEW, Uri
1355 .parse(WebView.SCHEME_MAILTO + extra)));
1356 menu.findItem(R.id.copy_mail_context_menu_id)
1357 .setOnMenuItemClickListener(
1358 new Copy(extra));
1359 break;
1360
1361 case WebView.HitTestResult.GEO_TYPE:
1362 menu.setHeaderTitle(extra);
1363 menu.findItem(R.id.map_context_menu_id).setIntent(
1364 new Intent(Intent.ACTION_VIEW, Uri
1365 .parse(WebView.SCHEME_GEO
1366 + URLEncoder.encode(extra))));
1367 menu.findItem(R.id.copy_geo_context_menu_id)
1368 .setOnMenuItemClickListener(
1369 new Copy(extra));
1370 break;
1371
1372 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1373 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001374 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001375 // decide whether to show the open link in new tab option
1376 boolean showNewTab = mTabControl.canCreateNewTab();
1377 MenuItem newTabItem
1378 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001379 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001380 ? R.string.contextmenu_openlink_newwindow_background
1381 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001382 newTabItem.setVisible(showNewTab);
1383 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001384 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1385 newTabItem.setOnMenuItemClickListener(
1386 new MenuItem.OnMenuItemClickListener() {
1387 @Override
1388 public boolean onMenuItemClick(MenuItem item) {
1389 final HashMap<String, WebView> hrefMap =
1390 new HashMap<String, WebView>();
1391 hrefMap.put("webview", webview);
1392 final Message msg = mHandler.obtainMessage(
1393 FOCUS_NODE_HREF,
1394 R.id.open_newtab_context_menu_id,
1395 0, hrefMap);
1396 webview.requestFocusNodeHref(msg);
1397 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001398 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001399 });
1400 } else {
1401 newTabItem.setOnMenuItemClickListener(
1402 new MenuItem.OnMenuItemClickListener() {
1403 @Override
1404 public boolean onMenuItemClick(MenuItem item) {
1405 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001406 openTab(extra, parent,
1407 !mSettings.openInBackground(),
1408 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001409 return true;
1410 }
1411 });
1412 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001413 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001414 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1415 break;
1416 }
1417 // otherwise fall through to handle image part
1418 case WebView.HitTestResult.IMAGE_TYPE:
1419 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1420 menu.setHeaderTitle(extra);
1421 }
1422 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1423 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1424 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001425 setOnMenuItemClickListener(
1426 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001427 menu.findItem(R.id.set_wallpaper_context_menu_id).
1428 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1429 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001430 break;
1431
1432 default:
1433 Log.w(LOGTAG, "We should not get here.");
1434 break;
1435 }
1436 //update the ui
1437 mUi.onContextMenuCreated(menu);
1438 }
1439
1440 /**
1441 * As the menu can be open when loading state changes
1442 * we must manually update the state of the stop/reload menu
1443 * item
1444 */
1445 private void updateInLoadMenuItems(Menu menu) {
1446 if (menu == null) {
1447 return;
1448 }
1449 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1450 MenuItem src = mInLoad ?
1451 menu.findItem(R.id.stop_menu_id):
1452 menu.findItem(R.id.reload_menu_id);
1453 if (src != null) {
1454 dest.setIcon(src.getIcon());
1455 dest.setTitle(src.getTitle());
1456 }
1457 }
1458
John Reckb3417f02011-01-14 11:01:05 -08001459 boolean onPrepareOptionsMenu(Menu menu) {
1460 if (mOptionsMenuHandler != null) {
1461 return mOptionsMenuHandler.onPrepareOptionsMenu(menu);
1462 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001463 // Note: setVisible will decide whether an item is visible; while
1464 // setEnabled() will decide whether an item is enabled, which also means
1465 // whether the matching shortcut key will function.
1466 switch (mMenuState) {
1467 case EMPTY_MENU:
1468 if (mCurrentMenuState != mMenuState) {
1469 menu.setGroupVisible(R.id.MAIN_MENU, false);
1470 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1471 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1472 }
1473 break;
1474 default:
1475 if (mCurrentMenuState != mMenuState) {
1476 menu.setGroupVisible(R.id.MAIN_MENU, true);
1477 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1478 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1479 }
1480 final WebView w = getCurrentTopWebView();
1481 boolean canGoBack = false;
1482 boolean canGoForward = false;
1483 boolean isHome = false;
1484 if (w != null) {
1485 canGoBack = w.canGoBack();
1486 canGoForward = w.canGoForward();
1487 isHome = mSettings.getHomePage().equals(w.getUrl());
1488 }
1489 final MenuItem back = menu.findItem(R.id.back_menu_id);
1490 back.setEnabled(canGoBack);
1491
1492 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1493 home.setEnabled(!isHome);
1494
1495 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1496 forward.setEnabled(canGoForward);
1497
1498 // decide whether to show the share link option
1499 PackageManager pm = mActivity.getPackageManager();
1500 Intent send = new Intent(Intent.ACTION_SEND);
1501 send.setType("text/plain");
1502 ResolveInfo ri = pm.resolveActivity(send,
1503 PackageManager.MATCH_DEFAULT_ONLY);
1504 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1505
John Reck35e9dd62011-04-25 09:01:54 -07001506 boolean isNavDump = mSettings.enableNavDump();
Michael Kolb8233fac2010-10-26 16:08:53 -07001507 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1508 nav.setVisible(isNavDump);
1509 nav.setEnabled(isNavDump);
1510
John Reck35e9dd62011-04-25 09:01:54 -07001511 boolean showDebugSettings = mSettings.isDebugEnabled();
Michael Kolb8233fac2010-10-26 16:08:53 -07001512 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1513 counter.setVisible(showDebugSettings);
1514 counter.setEnabled(showDebugSettings);
1515
John Reckb3417f02011-01-14 11:01:05 -08001516 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
1517 newtab.setEnabled(getTabControl().canCreateNewTab());
Michael Kolb8233fac2010-10-26 16:08:53 -07001518
Leon Scroggins81983762011-02-11 15:17:36 -05001519 MenuItem archive = menu.findItem(R.id.save_webarchive_menu_id);
John Reck51d8bad2011-02-28 15:47:47 -08001520 Tab tab = getTabControl().getCurrentTab();
1521 String url = tab != null ? tab.getUrl() : null;
1522 archive.setVisible(!TextUtils.isEmpty(url)
1523 && !url.endsWith(".webarchivexml"));
Michael Kolb8233fac2010-10-26 16:08:53 -07001524 break;
1525 }
1526 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001527 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001528 }
1529
1530 public boolean onOptionsItemSelected(MenuItem item) {
John Reckb3417f02011-01-14 11:01:05 -08001531 if (mOptionsMenuHandler != null &&
1532 mOptionsMenuHandler.onOptionsItemSelected(item)) {
1533 return true;
1534 }
1535
Michael Kolb8233fac2010-10-26 16:08:53 -07001536 if (item.getGroupId() != R.id.CONTEXT_MENU) {
1537 // menu remains active, so ensure comboview is dismissed
1538 // if main menu option is selected
1539 removeComboView();
1540 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001541 if (null == getCurrentTopWebView()) {
1542 return false;
1543 }
1544 if (mMenuIsDown) {
1545 // The shortcut action consumes the MENU. Even if it is still down,
1546 // it won't trigger the next shortcut action. In the case of the
1547 // shortcut action triggering a new activity, like Bookmarks, we
1548 // won't get onKeyUp for MENU. So it is important to reset it here.
1549 mMenuIsDown = false;
1550 }
1551 switch (item.getItemId()) {
1552 // -- Main menu
1553 case R.id.new_tab_menu_id:
1554 openTabToHomePage();
1555 break;
1556
1557 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001558 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001559 break;
1560
1561 case R.id.goto_menu_id:
1562 editUrl();
1563 break;
1564
1565 case R.id.bookmarks_menu_id:
1566 bookmarksOrHistoryPicker(false);
1567 break;
1568
1569 case R.id.active_tabs_menu_id:
1570 showActiveTabsPage();
1571 break;
1572
1573 case R.id.add_bookmark_menu_id:
John Reck3ffc5ca2011-06-10 15:56:06 -07001574 bookmarkCurrentPage(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001575 break;
1576
1577 case R.id.stop_reload_menu_id:
1578 if (mInLoad) {
1579 stopLoading();
1580 } else {
1581 getCurrentTopWebView().reload();
1582 }
1583 break;
1584
1585 case R.id.back_menu_id:
1586 getCurrentTopWebView().goBack();
1587 break;
1588
1589 case R.id.forward_menu_id:
1590 getCurrentTopWebView().goForward();
1591 break;
1592
1593 case R.id.close_menu_id:
1594 // Close the subwindow if it exists.
1595 if (mTabControl.getCurrentSubWindow() != null) {
1596 dismissSubWindow(mTabControl.getCurrentTab());
1597 break;
1598 }
1599 closeCurrentTab();
1600 break;
1601
1602 case R.id.homepage_menu_id:
1603 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001604 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001605 break;
1606
1607 case R.id.preferences_menu_id:
1608 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1609 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1610 getCurrentTopWebView().getUrl());
1611 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1612 break;
1613
1614 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001615 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001616 break;
1617
John Reckf33b1632011-06-04 20:00:23 -07001618 case R.id.freeze_tab_menu_id:
1619 // TODO: Show error messages
John Reck541f55a2011-06-07 16:34:43 -07001620 Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001621 if (source == null) break;
John Reckd8c74522011-06-14 08:45:00 -07001622 final ContentResolver cr = mActivity.getContentResolver();
1623 final ContentValues values = source.createSnapshotValues();
1624 new AsyncTask<Tab, Void, Long>() {
1625 @Override
1626 protected Long doInBackground(Tab... params) {
1627 Tab t = params[0];
1628 if (values == null) {
1629 return t.isSnapshot()
1630 ? ((SnapshotTab)t).getSnapshotId()
1631 : -1;
1632 }
1633 Uri result = cr.insert(Snapshots.CONTENT_URI, values);
1634 long id = ContentUris.parseId(result);
1635 return id;
1636 }
1637
1638 protected void onPostExecute(Long id) {
1639 if (id > 0) {
1640 createNewSnapshotTab(id, true);
1641 }
1642 };
1643 }.execute(source);
John Reckf33b1632011-06-04 20:00:23 -07001644 break;
1645
Leon Scrogginsac993842011-02-02 12:54:07 -05001646 case R.id.save_webarchive_menu_id:
1647 String state = Environment.getExternalStorageState();
1648 if (!Environment.MEDIA_MOUNTED.equals(state)) {
1649 Log.e(LOGTAG, "External storage not mounted");
1650 Toast.makeText(mActivity, R.string.webarchive_failed,
1651 Toast.LENGTH_SHORT).show();
1652 break;
1653 }
1654 final String directory = Environment.getExternalStoragePublicDirectory(
1655 Environment.DIRECTORY_DOWNLOADS) + File.separator;
1656 File dir = new File(directory);
1657 if (!dir.exists() && !dir.mkdirs()) {
1658 Log.e(LOGTAG, "Save as Web Archive: mkdirs for " + directory + " failed!");
1659 Toast.makeText(mActivity, R.string.webarchive_failed,
1660 Toast.LENGTH_SHORT).show();
1661 break;
1662 }
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001663 final WebView topWebView = getCurrentTopWebView();
Leon Scrogginsac993842011-02-02 12:54:07 -05001664 final String title = topWebView.getTitle();
John Reck51d8bad2011-02-28 15:47:47 -08001665 final String url = topWebView.getUrl();
Leon Scrogginsac993842011-02-02 12:54:07 -05001666 topWebView.saveWebArchive(directory, true,
1667 new ValueCallback<String>() {
1668 @Override
1669 public void onReceiveValue(final String value) {
1670 if (value != null) {
1671 File file = new File(value);
1672 final long length = file.length();
1673 if (file.exists() && length > 0) {
Leon Scroggins1cb96552011-02-11 14:22:57 -05001674 Toast.makeText(mActivity, R.string.webarchive_saved,
1675 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001676 final DownloadManager manager = (DownloadManager) mActivity
1677 .getSystemService(Context.DOWNLOAD_SERVICE);
1678 new Thread("Add WebArchive to download manager") {
1679 @Override
1680 public void run() {
Vasu Noria9e30a72011-03-07 11:37:34 -08001681 manager.addCompletedDownload(
1682 null == title ? value : title,
Leon Scrogginsac993842011-02-02 12:54:07 -05001683 value, true, "application/x-webarchive-xml",
1684 value, length, true);
1685 }
1686 }.start();
1687 return;
1688 }
1689 }
John Reck51d8bad2011-02-28 15:47:47 -08001690 DownloadHandler.onDownloadStartNoStream(mActivity,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001691 url, null, null, null, topWebView.isPrivateBrowsingEnabled());
Leon Scrogginsac993842011-02-02 12:54:07 -05001692 }
1693 });
1694 break;
1695
Michael Kolb8233fac2010-10-26 16:08:53 -07001696 case R.id.page_info_menu_id:
1697 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(),
1698 false);
1699 break;
1700
1701 case R.id.classic_history_menu_id:
1702 bookmarksOrHistoryPicker(true);
1703 break;
1704
1705 case R.id.title_bar_share_page_url:
1706 case R.id.share_page_menu_id:
1707 Tab currentTab = mTabControl.getCurrentTab();
1708 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001709 return false;
1710 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001711 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001712 break;
1713
1714 case R.id.dump_nav_menu_id:
1715 getCurrentTopWebView().debugDump();
1716 break;
1717
1718 case R.id.dump_counters_menu_id:
1719 getCurrentTopWebView().dumpV8Counters();
1720 break;
1721
1722 case R.id.zoom_in_menu_id:
1723 getCurrentTopWebView().zoomIn();
1724 break;
1725
1726 case R.id.zoom_out_menu_id:
1727 getCurrentTopWebView().zoomOut();
1728 break;
1729
1730 case R.id.view_downloads_menu_id:
1731 viewDownloads();
1732 break;
1733
1734 case R.id.window_one_menu_id:
1735 case R.id.window_two_menu_id:
1736 case R.id.window_three_menu_id:
1737 case R.id.window_four_menu_id:
1738 case R.id.window_five_menu_id:
1739 case R.id.window_six_menu_id:
1740 case R.id.window_seven_menu_id:
1741 case R.id.window_eight_menu_id:
1742 {
1743 int menuid = item.getItemId();
1744 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1745 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1746 Tab desiredTab = mTabControl.getTab(id);
1747 if (desiredTab != null &&
1748 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001749 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001750 }
1751 break;
1752 }
1753 }
1754 }
1755 break;
1756
1757 default:
1758 return false;
1759 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001760 return true;
1761 }
1762
1763 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001764 // Let the History and Bookmark fragments handle menus they created.
1765 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1766 return false;
1767 }
1768
Michael Kolb8233fac2010-10-26 16:08:53 -07001769 int id = item.getItemId();
1770 boolean result = true;
1771 switch (id) {
1772 // For the context menu from the title bar
1773 case R.id.title_bar_copy_page_url:
1774 Tab currentTab = mTabControl.getCurrentTab();
1775 if (null == currentTab) {
1776 result = false;
1777 break;
1778 }
1779 WebView mainView = currentTab.getWebView();
1780 if (null == mainView) {
1781 result = false;
1782 break;
1783 }
1784 copy(mainView.getUrl());
1785 break;
1786 // -- Browser context menu
1787 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001788 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001789 case R.id.copy_link_context_menu_id:
1790 final WebView webView = getCurrentTopWebView();
1791 if (null == webView) {
1792 result = false;
1793 break;
1794 }
1795 final HashMap<String, WebView> hrefMap =
1796 new HashMap<String, WebView>();
1797 hrefMap.put("webview", webView);
1798 final Message msg = mHandler.obtainMessage(
1799 FOCUS_NODE_HREF, id, 0, hrefMap);
1800 webView.requestFocusNodeHref(msg);
1801 break;
1802
1803 default:
1804 // For other context menus
1805 result = onOptionsItemSelected(item);
1806 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001807 return result;
1808 }
1809
1810 /**
1811 * support programmatically opening the context menu
1812 */
1813 public void openContextMenu(View view) {
1814 mActivity.openContextMenu(view);
1815 }
1816
1817 /**
1818 * programmatically open the options menu
1819 */
1820 public void openOptionsMenu() {
1821 mActivity.openOptionsMenu();
1822 }
1823
1824 public boolean onMenuOpened(int featureId, Menu menu) {
1825 if (mOptionsMenuOpen) {
1826 if (mConfigChanged) {
1827 // We do not need to make any changes to the state of the
1828 // title bar, since the only thing that happened was a
1829 // change in orientation
1830 mConfigChanged = false;
1831 } else {
1832 if (!mExtendedMenuOpen) {
1833 mExtendedMenuOpen = true;
1834 mUi.onExtendedMenuOpened();
1835 } else {
1836 // Switching the menu back to icon view, so show the
1837 // title bar once again.
1838 mExtendedMenuOpen = false;
1839 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001840 }
1841 }
1842 } else {
1843 // The options menu is closed, so open it, and show the title
1844 mOptionsMenuOpen = true;
1845 mConfigChanged = false;
1846 mExtendedMenuOpen = false;
1847 mUi.onOptionsMenuOpened();
1848 }
1849 return true;
1850 }
1851
1852 public void onOptionsMenuClosed(Menu menu) {
1853 mOptionsMenuOpen = false;
1854 mUi.onOptionsMenuClosed(mInLoad);
1855 }
1856
1857 public void onContextMenuClosed(Menu menu) {
1858 mUi.onContextMenuClosed(menu, mInLoad);
1859 }
1860
1861 // Helper method for getting the top window.
1862 @Override
1863 public WebView getCurrentTopWebView() {
1864 return mTabControl.getCurrentTopWebView();
1865 }
1866
1867 @Override
1868 public WebView getCurrentWebView() {
1869 return mTabControl.getCurrentWebView();
1870 }
1871
1872 /*
1873 * This method is called as a result of the user selecting the options
1874 * menu to see the download window. It shows the download window on top of
1875 * the current window.
1876 */
1877 void viewDownloads() {
1878 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1879 mActivity.startActivity(intent);
1880 }
1881
1882 // action mode
1883
1884 void onActionModeStarted(ActionMode mode) {
1885 mUi.onActionModeStarted(mode);
1886 mActionMode = mode;
1887 }
1888
1889 /*
1890 * True if a custom ActionMode (i.e. find or select) is in use.
1891 */
1892 @Override
1893 public boolean isInCustomActionMode() {
1894 return mActionMode != null;
1895 }
1896
1897 /*
1898 * End the current ActionMode.
1899 */
1900 @Override
1901 public void endActionMode() {
1902 if (mActionMode != null) {
1903 mActionMode.finish();
1904 }
1905 }
1906
1907 /*
1908 * Called by find and select when they are finished. Replace title bars
1909 * as necessary.
1910 */
1911 public void onActionModeFinished(ActionMode mode) {
1912 if (!isInCustomActionMode()) return;
1913 mUi.onActionModeFinished(mInLoad);
1914 mActionMode = null;
1915 }
1916
1917 boolean isInLoad() {
1918 return mInLoad;
1919 }
1920
1921 // bookmark handling
1922
1923 /**
1924 * add the current page as a bookmark to the given folder id
1925 * @param folderId use -1 for the default folder
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001926 * @param canBeAnEdit If true, check to see whether the site is already
1927 * bookmarked, and if it is, edit that bookmark. If false, and
1928 * the site is already bookmarked, do not attempt to edit the
1929 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001930 */
1931 @Override
John Reck3ffc5ca2011-06-10 15:56:06 -07001932 public void bookmarkCurrentPage(boolean canBeAnEdit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001933 Intent i = new Intent(mActivity,
1934 AddBookmarkPage.class);
1935 WebView w = getCurrentTopWebView();
1936 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1937 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1938 String touchIconUrl = w.getTouchIconUrl();
1939 if (touchIconUrl != null) {
1940 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1941 WebSettings settings = w.getSettings();
1942 if (settings != null) {
1943 i.putExtra(AddBookmarkPage.USER_AGENT,
1944 settings.getUserAgentString());
1945 }
1946 }
1947 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1948 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1949 getDesiredThumbnailHeight(mActivity)));
1950 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001951 if (canBeAnEdit) {
1952 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1953 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001954 // Put the dialog at the upper right of the screen, covering the
1955 // star on the title bar.
1956 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
1957 mActivity.startActivity(i);
1958 }
1959
1960 // file chooser
1961 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1962 mUploadHandler = new UploadHandler(this);
1963 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1964 }
1965
1966 // thumbnails
1967
1968 /**
1969 * Return the desired width for thumbnail screenshots, which are stored in
1970 * the database, and used on the bookmarks screen.
1971 * @param context Context for finding out the density of the screen.
1972 * @return desired width for thumbnail screenshot.
1973 */
1974 static int getDesiredThumbnailWidth(Context context) {
1975 return context.getResources().getDimensionPixelOffset(
1976 R.dimen.bookmarkThumbnailWidth);
1977 }
1978
1979 /**
1980 * Return the desired height for thumbnail screenshots, which are stored in
1981 * the database, and used on the bookmarks screen.
1982 * @param context Context for finding out the density of the screen.
1983 * @return desired height for thumbnail screenshot.
1984 */
1985 static int getDesiredThumbnailHeight(Context context) {
1986 return context.getResources().getDimensionPixelOffset(
1987 R.dimen.bookmarkThumbnailHeight);
1988 }
1989
Michael Kolb1acef692011-03-08 14:12:06 -08001990 static Bitmap createScreenshot(Tab tab, int width, int height) {
1991 if ((tab != null) && (tab.getWebView() != null)) {
1992 return createScreenshot(tab.getWebView(), width, height);
1993 }
1994 return null;
1995 }
1996
Michael Kolb8233fac2010-10-26 16:08:53 -07001997 private static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08001998 // We render to a bitmap 2x the desired size so that we can then
1999 // re-scale it with filtering since canvas.scale doesn't filter
2000 // This helps reduce aliasing at the cost of being slightly blurry
2001 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07002002 Picture thumbnail = view.capturePicture();
2003 if (thumbnail == null) {
2004 return null;
2005 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002006 width *= filter_scale;
2007 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07002008 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
2009 Canvas canvas = new Canvas(bm);
2010 // May need to tweak these values to determine what is the
2011 // best scale factor
2012 int thumbnailWidth = thumbnail.getWidth();
2013 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08002014 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08002015 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08002016 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07002017 } else {
2018 return null;
2019 }
John Reckfe49ab42010-11-16 17:09:37 -08002020
Michael Kolbeb95db42011-03-03 10:38:40 -08002021 float scaleFactorY = (float) height / (float)thumbnailHeight;
2022 if (scaleFactorY > scaleFactor) {
2023 // The picture is narrower than the requested AR
2024 // Center the thumnail and crop the sides
2025 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08002026 float wx = (thumbnailWidth * scaleFactor) - width;
2027 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07002028 }
2029
John Reckfe49ab42010-11-16 17:09:37 -08002030 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07002031
2032 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08002033 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
2034 height / filter_scale, true);
2035 bm.recycle();
2036 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002037 }
2038
John Reck34ef2672011-02-10 11:30:55 -08002039 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002040 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002041 // FIXME: Would like to make sure there is actually something to
2042 // draw, but the API for that (WebViewCore.pictureReady()) is not
2043 // currently accessible here.
2044
John Reck34ef2672011-02-10 11:30:55 -08002045 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002046 if (view == null) {
2047 // Tab was destroyed
2048 return;
2049 }
John Reck34ef2672011-02-10 11:30:55 -08002050 final String url = tab.getUrl();
2051 final String originalUrl = view.getOriginalUrl();
2052
2053 if (TextUtils.isEmpty(url)) {
2054 return;
2055 }
2056
2057 // Only update thumbnails for web urls (http(s)://), not for
2058 // about:, javascript:, data:, etc...
2059 // Unless it is a bookmarked site, then always update
2060 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2061 return;
2062 }
2063
Michael Kolb8233fac2010-10-26 16:08:53 -07002064 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2065 getDesiredThumbnailHeight(mActivity));
2066 if (bm == null) {
2067 return;
2068 }
2069
2070 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002071 new AsyncTask<Void, Void, Void>() {
2072 @Override
2073 protected Void doInBackground(Void... unused) {
2074 Cursor cursor = null;
2075 try {
2076 // TODO: Clean this up
2077 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2078 if (cursor != null && cursor.moveToFirst()) {
2079 final ByteArrayOutputStream os =
2080 new ByteArrayOutputStream();
2081 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002082
John Reck34ef2672011-02-10 11:30:55 -08002083 ContentValues values = new ContentValues();
2084 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002085
John Reck34ef2672011-02-10 11:30:55 -08002086 do {
John Reck617fd832011-02-16 14:35:59 -08002087 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002088 cr.update(Images.CONTENT_URI, values, null, null);
2089 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002090 }
John Reck34ef2672011-02-10 11:30:55 -08002091 } catch (IllegalStateException e) {
2092 // Ignore
2093 } finally {
2094 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002095 }
John Reck34ef2672011-02-10 11:30:55 -08002096 return null;
2097 }
2098 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002099 }
2100
2101 private class Copy implements OnMenuItemClickListener {
2102 private CharSequence mText;
2103
2104 public boolean onMenuItemClick(MenuItem item) {
2105 copy(mText);
2106 return true;
2107 }
2108
2109 public Copy(CharSequence toCopy) {
2110 mText = toCopy;
2111 }
2112 }
2113
Leon Scroggins63c02662010-11-18 15:16:27 -05002114 private static class Download implements OnMenuItemClickListener {
2115 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002116 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002117 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002118
2119 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002120 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002121 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002122 return true;
2123 }
2124
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002125 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002126 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002127 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002128 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002129 }
2130 }
2131
Cary Clark8974d282010-11-22 10:46:05 -05002132 private static class SelectText implements OnMenuItemClickListener {
2133 private WebView mWebView;
2134
2135 public boolean onMenuItemClick(MenuItem item) {
2136 if (mWebView != null) {
2137 return mWebView.selectText();
2138 }
2139 return false;
2140 }
2141
2142 public SelectText(WebView webView) {
2143 mWebView = webView;
2144 }
2145
2146 }
2147
Michael Kolb8233fac2010-10-26 16:08:53 -07002148 /********************** TODO: UI stuff *****************************/
2149
2150 // these methods have been copied, they still need to be cleaned up
2151
2152 /****************** tabs ***************************************************/
2153
2154 // basic tab interactions:
2155
2156 // it is assumed that tabcontrol already knows about the tab
2157 protected void addTab(Tab tab) {
2158 mUi.addTab(tab);
2159 }
2160
2161 protected void removeTab(Tab tab) {
2162 mUi.removeTab(tab);
2163 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002164 mCrashRecoveryHandler.backupState();
John Reckd8c74522011-06-14 08:45:00 -07002165 if (tab.isSnapshot()) {
2166 SnapshotTab st = (SnapshotTab) tab;
2167 final Uri uri = ContentUris.withAppendedId(
2168 Snapshots.CONTENT_URI, st.getSnapshotId());
2169 final ContentResolver cr = mActivity.getContentResolver();
2170 new Thread() {
2171 @Override
2172 public void run() {
2173 cr.delete(uri, null, null);
2174 }
2175 }.start();
2176 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002177 }
2178
Michael Kolbf2055602011-04-09 17:20:03 -07002179 @Override
2180 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002181 // monkey protection against delayed start
2182 if (tab != null) {
2183 mTabControl.setCurrentTab(tab);
2184 // the tab is guaranteed to have a webview after setCurrentTab
2185 mUi.setActiveTab(tab);
2186 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002187 }
2188
2189 protected void closeEmptyChildTab() {
2190 Tab current = mTabControl.getCurrentTab();
2191 if (current != null
2192 && current.getWebView().copyBackForwardList().getSize() == 0) {
Michael Kolbc831b632011-05-11 09:30:34 -07002193 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002194 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002195 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002196 closeTab(current);
2197 }
2198 }
2199 }
2200
John Reck26b18322011-06-21 13:08:58 -07002201 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002202 // Dismiss the subwindow if applicable.
2203 dismissSubWindow(appTab);
2204 // Since we might kill the WebView, remove it from the
2205 // content view first.
2206 mUi.detachTab(appTab);
2207 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002208 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002209 // TODO: analyze why the remove and add are necessary
2210 mUi.attachTab(appTab);
2211 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002212 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002213 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002214 } else {
2215 // If the tab was the current tab, we have to attach
2216 // it to the view system again.
2217 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002218 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002219 }
2220 }
2221
2222 // Remove the sub window if it exists. Also called by TabControl when the
2223 // user clicks the 'X' to dismiss a sub window.
2224 public void dismissSubWindow(Tab tab) {
2225 removeSubWindow(tab);
2226 // dismiss the subwindow. This will destroy the WebView.
2227 tab.dismissSubWindow();
2228 getCurrentTopWebView().requestFocus();
2229 }
2230
2231 @Override
2232 public void removeSubWindow(Tab t) {
2233 if (t.getSubWebView() != null) {
2234 mUi.removeSubWindow(t.getSubViewContainer());
2235 }
2236 }
2237
2238 @Override
2239 public void attachSubWindow(Tab tab) {
2240 if (tab.getSubWebView() != null) {
2241 mUi.attachSubWindow(tab.getSubViewContainer());
2242 getCurrentTopWebView().requestFocus();
2243 }
2244 }
2245
Michael Kolb7bcafde2011-05-09 13:55:59 -07002246 // open a non inconito tab with the given url data
2247 // and set as active tab
2248 public Tab openTab(UrlData urlData) {
2249 Tab tab = createNewTab(false, true, true);
2250 if ((tab != null) && !urlData.isEmpty()) {
2251 loadUrlDataIn(tab, urlData);
2252 }
2253 return tab;
2254 }
2255
Michael Kolb843510f2010-12-09 10:51:49 -08002256 @Override
2257 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002258 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002259 }
2260
Michael Kolb8233fac2010-10-26 16:08:53 -07002261 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002262 public Tab openIncognitoTab() {
2263 return openTab(INCOGNITO_URI, true, true, false);
2264 }
2265
2266 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002267 public Tab openTab(String url, boolean incognito, boolean setActive,
2268 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002269 return openTab(url, incognito, setActive, useCurrent, null);
2270 }
2271
2272 @Override
2273 public Tab openTab(String url, Tab parent, boolean setActive,
2274 boolean useCurrent) {
2275 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2276 setActive, useCurrent, parent);
2277 }
2278
2279 public Tab openTab(String url, boolean incognito, boolean setActive,
2280 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002281 Tab tab = createNewTab(incognito, setActive, useCurrent);
2282 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002283 if (parent != null && parent != tab) {
2284 parent.addChildTab(tab);
2285 }
Michael Kolb519d2282011-05-09 17:03:19 -07002286 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002287 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002288 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002289 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002290 return tab;
2291 }
2292
2293 // this method will attempt to create a new tab
2294 // incognito: private browsing tab
2295 // setActive: ste tab as current tab
2296 // useCurrent: if no new tab can be created, return current tab
2297 private Tab createNewTab(boolean incognito, boolean setActive,
2298 boolean useCurrent) {
2299 Tab tab = null;
2300 if (mTabControl.canCreateNewTab()) {
2301 tab = mTabControl.createNewTab(incognito);
2302 addTab(tab);
2303 if (setActive) {
2304 setActiveTab(tab);
2305 }
2306 } else {
2307 if (useCurrent) {
2308 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002309 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002310 } else {
2311 mUi.showMaxTabsWarning();
2312 }
2313 }
2314 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002315 }
2316
John Reckd8c74522011-06-14 08:45:00 -07002317 private SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2318 SnapshotTab tab = mTabControl.createSnapshotTab(snapshotId);
2319 addTab(tab);
2320 if (setActive) {
2321 setActiveTab(tab);
2322 }
2323 return tab;
2324 }
2325
Michael Kolb8233fac2010-10-26 16:08:53 -07002326 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002327 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002328 * @return boolean True if we successfully switched to a different tab. If
2329 * the indexth tab is null, or if that tab is the same as
2330 * the current one, return false.
2331 */
2332 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002333 public boolean switchToTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002334 // hide combo view if open
2335 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002336 Tab currentTab = mTabControl.getCurrentTab();
2337 if (tab == null || tab == currentTab) {
2338 return false;
2339 }
2340 setActiveTab(tab);
2341 return true;
2342 }
2343
2344 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002345 public void closeCurrentTab() {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002346 // hide combo view if open
2347 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002348 if (mTabControl.getTabCount() == 1) {
John Reck958b2422010-12-03 17:56:17 -08002349 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002350 return;
2351 }
Michael Kolbc831b632011-05-11 09:30:34 -07002352 final Tab current = mTabControl.getCurrentTab();
2353 final int pos = mTabControl.getCurrentPosition();
2354 Tab newTab = current.getParent();
2355 if (newTab == null) {
2356 newTab = mTabControl.getTab(pos + 1);
2357 if (newTab == null) {
2358 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002359 }
2360 }
Michael Kolbc831b632011-05-11 09:30:34 -07002361 if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002362 // Close window
2363 closeTab(current);
2364 }
2365 }
2366
2367 /**
2368 * Close the tab, remove its associated title bar, and adjust mTabControl's
2369 * current tab to a valid value.
2370 */
2371 @Override
2372 public void closeTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002373 // hide combo view if open
2374 removeComboView();
Michael Kolb2d59c322011-01-25 13:18:55 -08002375 removeTab(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002376 }
2377
Michael Kolb8233fac2010-10-26 16:08:53 -07002378 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002379 protected void loadUrlFromContext(String url) {
2380 Tab tab = getCurrentTab();
2381 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002382 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002383 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002384 url = UrlUtils.smartUrlFilter(url);
2385 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002386 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002387 }
2388 }
2389 }
2390
2391 /**
2392 * Load the URL into the given WebView and update the title bar
2393 * to reflect the new load. Call this instead of WebView.loadUrl
2394 * directly.
2395 * @param view The WebView used to load url.
2396 * @param url The URL to load.
2397 */
John Reck26b18322011-06-21 13:08:58 -07002398 protected void loadUrl(Tab tab, String url) {
2399 loadUrl(tab, url, null);
2400 }
2401
2402 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2403 if (tab != null) {
2404 dismissSubWindow(tab);
2405 tab.loadUrl(url, headers);
2406 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002407 }
2408
2409 /**
2410 * Load UrlData into a Tab and update the title bar to reflect the new
2411 * load. Call this instead of UrlData.loadIn directly.
2412 * @param t The Tab used to load.
2413 * @param data The UrlData being loaded.
2414 */
2415 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002416 if (data != null) {
2417 if (data.mVoiceIntent != null) {
2418 t.activateVoiceSearchMode(data.mVoiceIntent);
2419 } else {
2420 loadUrl(t, data.mUrl, data.mHeaders);
2421 }
2422 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002423 }
2424
John Reck30c714c2010-12-16 17:30:34 -08002425 @Override
2426 public void onUserCanceledSsl(Tab tab) {
2427 WebView web = tab.getWebView();
2428 // TODO: Figure out the "right" behavior
2429 if (web.canGoBack()) {
2430 web.goBack();
2431 } else {
2432 web.loadUrl(mSettings.getHomePage());
2433 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002434 }
2435
2436 void goBackOnePageOrQuit() {
2437 Tab current = mTabControl.getCurrentTab();
2438 if (current == null) {
2439 /*
2440 * Instead of finishing the activity, simply push this to the back
2441 * of the stack and let ActivityManager to choose the foreground
2442 * activity. As BrowserActivity is singleTask, it will be always the
2443 * root of the task. So we can use either true or false for
2444 * moveTaskToBack().
2445 */
2446 mActivity.moveTaskToBack(true);
2447 return;
2448 }
2449 WebView w = current.getWebView();
2450 if (w.canGoBack()) {
2451 w.goBack();
2452 } else {
2453 // Check to see if we are closing a window that was created by
2454 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002455 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002456 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002457 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002458 // Now we close the other tab
2459 closeTab(current);
2460 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002461 /*
2462 * Instead of finishing the activity, simply push this to the back
2463 * of the stack and let ActivityManager to choose the foreground
2464 * activity. As BrowserActivity is singleTask, it will be always the
2465 * root of the task. So we can use either true or false for
2466 * moveTaskToBack().
2467 */
2468 mActivity.moveTaskToBack(true);
2469 }
2470 }
2471 }
2472
2473 /**
2474 * Feed the previously stored results strings to the BrowserProvider so that
2475 * the SearchDialog will show them instead of the standard searches.
2476 * @param result String to show on the editable line of the SearchDialog.
2477 */
2478 @Override
2479 public void showVoiceSearchResults(String result) {
2480 ContentProviderClient client = mActivity.getContentResolver()
2481 .acquireContentProviderClient(Browser.BOOKMARKS_URI);
2482 ContentProvider prov = client.getLocalContentProvider();
2483 BrowserProvider bp = (BrowserProvider) prov;
2484 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
2485 client.release();
2486
2487 Bundle bundle = createGoogleSearchSourceBundle(
2488 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
2489 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
2490 startSearch(result, false, bundle, false);
2491 }
2492
2493 private void startSearch(String initialQuery, boolean selectInitialQuery,
2494 Bundle appSearchData, boolean globalSearch) {
2495 if (appSearchData == null) {
2496 appSearchData = createGoogleSearchSourceBundle(
2497 GOOGLE_SEARCH_SOURCE_TYPE);
2498 }
2499
2500 SearchEngine searchEngine = mSettings.getSearchEngine();
2501 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
2502 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
2503 }
2504 mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData,
2505 globalSearch);
2506 }
2507
2508 private Bundle createGoogleSearchSourceBundle(String source) {
2509 Bundle bundle = new Bundle();
2510 bundle.putString(Search.SOURCE, source);
2511 return bundle;
2512 }
2513
2514 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002515 * helper method for key handler
2516 * returns the current tab if it can't advance
2517 */
Michael Kolbc831b632011-05-11 09:30:34 -07002518 private Tab getNextTab() {
2519 return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1,
2520 mTabControl.getCurrentPosition() + 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002521 }
2522
2523 /**
2524 * helper method for key handler
2525 * returns the current tab if it can't advance
2526 */
Michael Kolbc831b632011-05-11 09:30:34 -07002527 private Tab getPrevTab() {
2528 return mTabControl.getTab(Math.max(0,
2529 mTabControl.getCurrentPosition() - 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002530 }
2531
2532 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002533 * handle key events in browser
2534 *
2535 * @param keyCode
2536 * @param event
2537 * @return true if handled, false to pass to super
2538 */
2539 boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002540 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002541 // Even if MENU is already held down, we need to call to super to open
2542 // the IME on long press.
Michael Kolb2814a362011-05-19 15:49:41 -07002543 if (KeyEvent.KEYCODE_MENU == keyCode) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002544 if (mOptionsMenuHandler != null) {
2545 return false;
2546 } else {
2547 event.startTracking();
2548 return true;
2549 }
Michael Kolb2814a362011-05-19 15:49:41 -07002550 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002551 if (!noModifiers
2552 && ((KeyEvent.KEYCODE_MENU == keyCode)
2553 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2554 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002555 mMenuIsDown = true;
2556 return false;
2557 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002558
Cary Clark8ff8c662010-12-29 15:03:05 -05002559 WebView webView = getCurrentTopWebView();
2560 if (webView == null) return false;
2561
Cary Clark160bbb92011-01-10 11:17:07 -05002562 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2563 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002564
Michael Kolb8233fac2010-10-26 16:08:53 -07002565 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002566 case KeyEvent.KEYCODE_TAB:
2567 if (event.isCtrlPressed()) {
2568 if (event.isShiftPressed()) {
2569 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002570 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002571 } else {
2572 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002573 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002574 }
2575 return true;
2576 }
2577 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002578 case KeyEvent.KEYCODE_SPACE:
2579 // WebView/WebTextView handle the keys in the KeyDown. As
2580 // the Activity's shortcut keys are only handled when WebView
2581 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002582 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002583 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002584 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002585 pageDown();
2586 }
2587 return true;
2588 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002589 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002590 event.startTracking();
2591 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002592 case KeyEvent.KEYCODE_DPAD_LEFT:
2593 if (ctrl) {
2594 webView.goBack();
2595 return true;
2596 }
2597 break;
2598 case KeyEvent.KEYCODE_DPAD_RIGHT:
2599 if (ctrl) {
2600 webView.goForward();
2601 return true;
2602 }
2603 break;
2604 case KeyEvent.KEYCODE_A:
2605 if (ctrl) {
2606 webView.selectAll();
2607 return true;
2608 }
2609 break;
Michael Kolba4183062011-01-16 10:43:21 -08002610// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002611 case KeyEvent.KEYCODE_C:
2612 if (ctrl) {
2613 webView.copySelection();
2614 return true;
2615 }
2616 break;
Michael Kolba4183062011-01-16 10:43:21 -08002617// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002618// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002619// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002620// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002621// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002622// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002623// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002624// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002625// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002626// case KeyEvent.KEYCODE_M: // unused
2627// case KeyEvent.KEYCODE_N: // in Chrome: new window
2628// case KeyEvent.KEYCODE_O: // in Chrome: open file
2629// case KeyEvent.KEYCODE_P: // in Chrome: print page
2630// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002631// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002632// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2633 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002634 // we can't use the ctrl/shift flags, they check for
2635 // exclusive use of a modifier
2636 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002637 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002638 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002639 } else {
2640 openTabToHomePage();
2641 }
2642 return true;
2643 }
2644 break;
2645// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2646// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb1a2eba42011-03-16 16:42:49 -07002647 case KeyEvent.KEYCODE_W: // in Chrome: close tab
2648 if (ctrl) {
2649 closeCurrentTab();
2650 return true;
2651 }
2652 break;
Cary Clark8ff8c662010-12-29 15:03:05 -05002653// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2654// case KeyEvent.KEYCODE_Y: // unused
2655// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002656 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002657 // it is a regular key and webview is not null
2658 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002659 }
2660
John Recke6bf4ab2011-02-24 15:48:05 -08002661 boolean onKeyLongPress(int keyCode, KeyEvent event) {
2662 switch(keyCode) {
2663 case KeyEvent.KEYCODE_BACK:
2664 if (mUi.showsWeb()) {
2665 bookmarksOrHistoryPicker(true);
2666 return true;
2667 }
2668 break;
2669 }
2670 return false;
2671 }
2672
Michael Kolb8233fac2010-10-26 16:08:53 -07002673 boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb2814a362011-05-19 15:49:41 -07002674 if (KeyEvent.KEYCODE_MENU == keyCode) {
2675 mMenuIsDown = false;
2676 if (event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002677 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002678 }
2679 }
Cary Clark160bbb92011-01-10 11:17:07 -05002680 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002681 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002682 case KeyEvent.KEYCODE_BACK:
2683 if (event.isTracking() && !event.isCanceled()) {
2684 onBackKey();
2685 return true;
2686 }
2687 break;
2688 }
2689 return false;
2690 }
2691
2692 public boolean isMenuDown() {
2693 return mMenuIsDown;
2694 }
2695
Ben Murdoch8029a772010-11-16 11:58:21 +00002696 public void setupAutoFill(Message message) {
2697 // Open the settings activity at the AutoFill profile fragment so that
2698 // the user can create a new profile. When they return, we will dispatch
2699 // the message so that we can autofill the form using their new profile.
2700 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2701 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2702 AutoFillSettingsFragment.class.getName());
2703 mAutoFillSetupMessage = message;
2704 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2705 }
John Reckb3417f02011-01-14 11:01:05 -08002706
2707 @Override
2708 public void registerOptionsMenuHandler(OptionsMenuHandler handler) {
2709 mOptionsMenuHandler = handler;
2710 }
2711
2712 @Override
2713 public void unregisterOptionsMenuHandler(OptionsMenuHandler handler) {
2714 if (mOptionsMenuHandler == handler) {
2715 mOptionsMenuHandler = null;
2716 }
2717 }
2718
Narayan Kamath5119edd2011-02-23 15:49:17 +00002719 @Override
2720 public void registerDropdownChangeListener(DropdownChangeListener d) {
2721 mUi.registerDropdownChangeListener(d);
2722 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002723}