blob: 986b6174cce4c4ba1758b6e627474af4944dd672 [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;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
29import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
31import android.content.res.Configuration;
Leon Scroggins1961ed22010-12-07 15:22:21 -050032import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070033import android.database.Cursor;
34import android.database.sqlite.SQLiteDatabase;
Michael Kolb8233fac2010-10-26 16:08:53 -070035import android.graphics.Bitmap;
36import android.graphics.Canvas;
37import android.graphics.Picture;
38import android.net.Uri;
39import android.net.http.SslError;
40import android.os.AsyncTask;
41import android.os.Bundle;
Leon Scrogginsac993842011-02-02 12:54:07 -050042import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.os.Handler;
44import android.os.Message;
45import android.os.PowerManager;
46import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000047import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070048import android.provider.Browser;
49import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.provider.BrowserContract.Images;
51import android.provider.ContactsContract;
52import android.provider.ContactsContract.Intents.Insert;
Michael Kolbcfa3af52010-12-14 10:36:11 -080053import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.text.TextUtils;
55import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080056import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070057import android.view.ActionMode;
58import android.view.ContextMenu;
59import android.view.ContextMenu.ContextMenuInfo;
60import android.view.Gravity;
61import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070062import android.view.Menu;
63import android.view.MenuInflater;
64import android.view.MenuItem;
65import android.view.MenuItem.OnMenuItemClickListener;
66import android.view.View;
67import android.webkit.CookieManager;
68import android.webkit.CookieSyncManager;
69import android.webkit.HttpAuthHandler;
70import android.webkit.SslErrorHandler;
71import android.webkit.ValueCallback;
72import android.webkit.WebChromeClient;
73import android.webkit.WebIconDatabase;
74import android.webkit.WebSettings;
75import android.webkit.WebView;
Leon Scrogginsac993842011-02-02 12:54:07 -050076import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070077
Michael Kolb4bd767d2011-05-27 11:33:55 -070078import com.android.browser.IntentHandler.UrlData;
79import com.android.browser.UI.DropdownChangeListener;
80import com.android.browser.provider.BrowserProvider;
81import com.android.browser.search.SearchEngine;
82import com.android.common.Search;
83
John Reckf33b1632011-06-04 20:00:23 -070084import java.io.ByteArrayInputStream;
Michael Kolb8233fac2010-10-26 16:08:53 -070085import java.io.ByteArrayOutputStream;
86import java.io.File;
John Reckf33b1632011-06-04 20:00:23 -070087import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -070088import java.net.URLEncoder;
89import java.util.Calendar;
90import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080091import java.util.List;
Michael Kolb8233fac2010-10-26 16:08:53 -070092
93/**
94 * Controller for browser
95 */
96public class Controller
97 implements WebViewController, UiController {
98
99 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800100 private static final String SEND_APP_ID_EXTRA =
101 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700102 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800103
Michael Kolb8233fac2010-10-26 16:08:53 -0700104
105 // public message ids
106 public final static int LOAD_URL = 1001;
107 public final static int STOP_LOAD = 1002;
108
109 // Message Ids
110 private static final int FOCUS_NODE_HREF = 102;
111 private static final int RELEASE_WAKELOCK = 107;
112
113 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
114
115 private static final int OPEN_BOOKMARKS = 201;
116
117 private static final int EMPTY_MENU = -1;
118
Michael Kolb8233fac2010-10-26 16:08:53 -0700119 // activity requestCode
120 final static int PREFERENCES_PAGE = 3;
121 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000122 final static int AUTOFILL_SETUP = 5;
123
Michael Kolb8233fac2010-10-26 16:08:53 -0700124 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
125
126 // As the ids are dynamically created, we can't guarantee that they will
127 // be in sequence, so this static array maps ids to a window number.
128 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
129 { R.id.window_one_menu_id, R.id.window_two_menu_id,
130 R.id.window_three_menu_id, R.id.window_four_menu_id,
131 R.id.window_five_menu_id, R.id.window_six_menu_id,
132 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
133
134 // "source" parameter for Google search through search key
135 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
136 // "source" parameter for Google search through simplily type
137 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
138
Guang Zhu9e78f512011-05-04 11:45:11 -0700139 // "no-crash-recovery" parameter in intetnt to suppress crash recovery
140 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
141
Michael Kolb8233fac2010-10-26 16:08:53 -0700142 private Activity mActivity;
143 private UI mUi;
144 private TabControl mTabControl;
145 private BrowserSettings mSettings;
146 private WebViewFactory mFactory;
John Reckb3417f02011-01-14 11:01:05 -0800147 private OptionsMenuHandler mOptionsMenuHandler = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700148
149 private WakeLock mWakeLock;
150
151 private UrlHandler mUrlHandler;
152 private UploadHandler mUploadHandler;
153 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700154 private PageDialogsHandler mPageDialogsHandler;
155 private NetworkStateHandler mNetworkHandler;
156
Ben Murdoch8029a772010-11-16 11:58:21 +0000157 private Message mAutoFillSetupMessage;
158
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 private boolean mShouldShowErrorConsole;
160
161 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
162
163 // FIXME, temp address onPrepareMenu performance problem.
164 // When we move everything out of view, we should rewrite this.
165 private int mCurrentMenuState = 0;
166 private int mMenuState = R.id.MAIN_MENU;
167 private int mOldMenuState = EMPTY_MENU;
168 private Menu mCachedMenu;
169
Michael Kolb8233fac2010-10-26 16:08:53 -0700170 private boolean mMenuIsDown;
171
172 // For select and find, we keep track of the ActionMode so that
173 // finish() can be called as desired.
174 private ActionMode mActionMode;
175
176 /**
177 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
178 * of whether the configuration has changed. The first onMenuOpened call
179 * after a configuration change is simply a reopening of the same menu
180 * (i.e. mIconView did not change).
181 */
182 private boolean mConfigChanged;
183
184 /**
185 * Keeps track of whether the options menu is open. This is important in
186 * determining whether to show or hide the title bar overlay
187 */
188 private boolean mOptionsMenuOpen;
189
190 /**
191 * Whether or not the options menu is in its bigger, popup menu form. When
192 * true, we want the title bar overlay to be gone. When false, we do not.
193 * Only meaningful if mOptionsMenuOpen is true.
194 */
195 private boolean mExtendedMenuOpen;
196
197 private boolean mInLoad;
198
199 private boolean mActivityPaused = true;
200 private boolean mLoadStopped;
201
202 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500203 // Checks to see when the bookmarks database has changed, and updates the
204 // Tabs' notion of whether they represent bookmarked sites.
205 private ContentObserver mBookmarksObserver;
John Reck0ebd3ac2010-12-09 11:14:04 -0800206 private DataController mDataController;
John Reck847b5322011-04-14 17:02:18 -0700207 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700208
209 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
210 @Override
211 public Void doInBackground(File... files) {
212 if (files != null) {
213 for (File f : files) {
214 if (!f.delete()) {
215 Log.e(LOGTAG, f.getPath() + " was not deleted");
216 }
217 }
218 }
219 return null;
220 }
221 }
222
223 public Controller(Activity browser) {
224 mActivity = browser;
225 mSettings = BrowserSettings.getInstance();
John Reck0ebd3ac2010-12-09 11:14:04 -0800226 mDataController = DataController.getInstance(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700227 mTabControl = new TabControl(this);
228 mSettings.setController(this);
John Reck847b5322011-04-14 17:02:18 -0700229 mCrashRecoveryHandler = new CrashRecoveryHandler(this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700230
231 mUrlHandler = new UrlHandler(this);
232 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700233 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
234
235 PowerManager pm = (PowerManager) mActivity
236 .getSystemService(Context.POWER_SERVICE);
237 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
238
239 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500240 mBookmarksObserver = new ContentObserver(mHandler) {
241 @Override
242 public void onChange(boolean selfChange) {
243 int size = mTabControl.getTabCount();
244 for (int i = 0; i < size; i++) {
245 mTabControl.getTab(i).updateBookmarkedStatus();
246 }
247 }
248
249 };
250 browser.getContentResolver().registerContentObserver(
251 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700252
253 mNetworkHandler = new NetworkStateHandler(mActivity, this);
254 // Start watching the default geolocation permissions
255 mSystemAllowGeolocationOrigins =
256 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
257 mSystemAllowGeolocationOrigins.start();
258
259 retainIconsOnStartup();
260 }
261
Patrick Scott7d50a932011-02-04 09:27:26 -0500262 void start(final Bundle icicle, final Intent intent) {
Guang Zhu9e78f512011-05-04 11:45:11 -0700263 boolean noCrashRecovery = intent.getBooleanExtra(NO_CRASH_RECOVERY, false);
264 if (icicle != null || noCrashRecovery) {
John Reck847b5322011-04-14 17:02:18 -0700265 mCrashRecoveryHandler.clearState();
266 doStart(icicle, intent);
267 } else {
268 mCrashRecoveryHandler.startRecovery(intent);
269 }
270 }
271
272 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700273 // Unless the last browser usage was within 24 hours, destroy any
274 // remaining incognito tabs.
275
276 Calendar lastActiveDate = icicle != null ?
277 (Calendar) icicle.getSerializable("lastActiveDate") : null;
278 Calendar today = Calendar.getInstance();
279 Calendar yesterday = Calendar.getInstance();
280 yesterday.add(Calendar.DATE, -1);
281
Patrick Scott7d50a932011-02-04 09:27:26 -0500282 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700283 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800284 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700285
Patrick Scott7d50a932011-02-04 09:27:26 -0500286 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700287 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500288 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000289
Michael Kolbc831b632011-05-11 09:30:34 -0700290 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500291 // Not able to restore so we go ahead and clear session cookies. We
292 // must do this before trying to login the user as we don't want to
293 // clear any session cookies set during login.
294 CookieManager.getInstance().removeSessionCookie();
295 }
296
Patrick Scottd43e75a2011-03-14 14:47:23 -0400297 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500298 new Runnable() {
299 @Override public void run() {
Michael Kolbc831b632011-05-11 09:30:34 -0700300 onPreloginFinished(icicle, intent, currentTabId, restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500301 }
302 });
303 }
304
Michael Kolbc831b632011-05-11 09:30:34 -0700305 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500306 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700307 if (currentTabId == -1) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700308 final Bundle extra = intent.getExtras();
309 // Create an initial tab.
310 // If the intent is ACTION_VIEW and data is not null, the Browser is
311 // invoked to view the content by another application. In this case,
312 // the tab will be close when exit.
313 UrlData urlData = mIntentHandler.getUrlDataFromIntent(intent);
Michael Kolb7bcafde2011-05-09 13:55:59 -0700314 Tab t = null;
315 if (urlData.isEmpty()) {
316 t = openTabToHomePage();
317 } else {
318 t = openTab(urlData);
319 }
320 if (t != null) {
321 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
322 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700323 WebView webView = t.getWebView();
324 if (extra != null) {
325 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
326 if (scale > 0 && scale <= 1000) {
327 webView.setInitialScale(scale);
328 }
329 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700330 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700331 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500332 mUi.needsRestoreAllTabs());
Michael Kolb1bf23132010-11-19 12:55:12 -0800333 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700334 // TabControl.restoreState() will create a new tab even if
335 // restoring the state fails.
336 setActiveTab(mTabControl.getCurrentTab());
337 }
338 // clear up the thumbnail directory, which is no longer used;
339 // ideally this should only be run once after an upgrade from
340 // a previous version of the browser
341 new ClearThumbnails().execute(mTabControl.getThumbnailDir()
342 .listFiles());
343 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700344 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700345 if (jsFlags.trim().length() != 0) {
346 getCurrentWebView().setJsFlags(jsFlags);
347 }
John Reck439c9a52010-12-14 10:04:39 -0800348 if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
349 bookmarksOrHistoryPicker(false);
350 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700351 }
352
353 void setWebViewFactory(WebViewFactory factory) {
354 mFactory = factory;
355 }
356
Michael Kolb1514bb72010-11-22 09:11:48 -0800357 @Override
358 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700359 return mFactory;
360 }
361
362 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800363 public void onSetWebView(Tab tab, WebView view) {
364 mUi.onSetWebView(tab, view);
365 }
366
367 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800368 public void createSubWindow(Tab tab) {
369 endActionMode();
370 WebView mainView = tab.getWebView();
371 WebView subView = mFactory.createWebView((mainView == null)
372 ? false
373 : mainView.isPrivateBrowsingEnabled());
374 mUi.createSubWindow(tab, subView);
375 }
376
377 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700378 public Activity getActivity() {
379 return mActivity;
380 }
381
382 void setUi(UI ui) {
383 mUi = ui;
384 }
385
386 BrowserSettings getSettings() {
387 return mSettings;
388 }
389
390 IntentHandler getIntentHandler() {
391 return mIntentHandler;
392 }
393
394 @Override
395 public UI getUi() {
396 return mUi;
397 }
398
399 int getMaxTabs() {
400 return mActivity.getResources().getInteger(R.integer.max_tabs);
401 }
402
403 @Override
404 public TabControl getTabControl() {
405 return mTabControl;
406 }
407
Michael Kolb1bf23132010-11-19 12:55:12 -0800408 @Override
409 public List<Tab> getTabs() {
410 return mTabControl.getTabs();
411 }
412
Michael Kolb8233fac2010-10-26 16:08:53 -0700413 // Open the icon database and retain all the icons for visited sites.
Ben Murdoch9446b932010-11-25 16:20:14 +0000414 // This is done on a background thread so as not to stall startup.
Michael Kolb8233fac2010-10-26 16:08:53 -0700415 private void retainIconsOnStartup() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000416 // WebIconDatabase needs to be retrieved on the UI thread so that if
417 // it has not been created successfully yet the Handler is started on the
418 // UI thread.
419 new RetainIconsOnStartupTask(WebIconDatabase.getInstance()).execute();
420 }
421
422 private class RetainIconsOnStartupTask extends AsyncTask<Void, Void, Void> {
423 private WebIconDatabase mDb;
424
425 public RetainIconsOnStartupTask(WebIconDatabase db) {
426 mDb = db;
427 }
428
John Recka00cbbd2010-12-16 12:38:19 -0800429 @Override
Ben Murdoch9446b932010-11-25 16:20:14 +0000430 protected Void doInBackground(Void... unused) {
431 mDb.open(mActivity.getDir("icons", 0).getPath());
432 Cursor c = null;
433 try {
434 c = Browser.getAllBookmarks(mActivity.getContentResolver());
435 if (c.moveToFirst()) {
436 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
437 do {
438 String url = c.getString(urlIndex);
439 mDb.retainIconForPageUrl(url);
440 } while (c.moveToNext());
441 }
442 } catch (IllegalStateException e) {
443 Log.e(LOGTAG, "retainIconsOnStartup", e);
444 } finally {
445 if (c != null) c.close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700446 }
Ben Murdoch9446b932010-11-25 16:20:14 +0000447
448 return null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700449 }
450 }
451
452 private void startHandler() {
453 mHandler = new Handler() {
454
455 @Override
456 public void handleMessage(Message msg) {
457 switch (msg.what) {
458 case OPEN_BOOKMARKS:
459 bookmarksOrHistoryPicker(false);
460 break;
461 case FOCUS_NODE_HREF:
462 {
463 String url = (String) msg.getData().get("url");
464 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500465 String src = (String) msg.getData().get("src");
466 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700467 if (TextUtils.isEmpty(url)) {
468 break;
469 }
470 HashMap focusNodeMap = (HashMap) msg.obj;
471 WebView view = (WebView) focusNodeMap.get("webview");
472 // Only apply the action if the top window did not change.
473 if (getCurrentTopWebView() != view) {
474 break;
475 }
476 switch (msg.arg1) {
477 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -0700478 loadUrlFromContext(getCurrentTopWebView(), url);
479 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500480 case R.id.view_image_context_menu_id:
481 loadUrlFromContext(getCurrentTopWebView(), src);
482 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500483 case R.id.open_newtab_context_menu_id:
484 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700485 openTab(url, parent,
486 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500487 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700488 case R.id.copy_link_context_menu_id:
489 copy(url);
490 break;
491 case R.id.save_link_context_menu_id:
492 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500493 DownloadHandler.onDownloadStartNoStream(
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000494 mActivity, url, null, null, null,
495 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700496 break;
497 }
498 break;
499 }
500
501 case LOAD_URL:
502 loadUrlFromContext(getCurrentTopWebView(), (String) msg.obj);
503 break;
504
505 case STOP_LOAD:
506 stopLoading();
507 break;
508
509 case RELEASE_WAKELOCK:
510 if (mWakeLock.isHeld()) {
511 mWakeLock.release();
512 // if we reach here, Browser should be still in the
513 // background loading after WAKELOCK_TIMEOUT (5-min).
514 // To avoid burning the battery, stop loading.
515 mTabControl.stopAllLoading();
516 }
517 break;
518
519 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800520 Tab tab = (Tab) msg.obj;
521 if (tab != null) {
522 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 }
524 break;
525 }
526 }
527 };
528
529 }
530
Michael Kolbba99c5d2010-11-29 14:57:41 -0800531 @Override
532 public void shareCurrentPage() {
533 shareCurrentPage(mTabControl.getCurrentTab());
534 }
535
536 private void shareCurrentPage(Tab tab) {
537 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800538 sharePage(mActivity, tab.getTitle(),
539 tab.getUrl(), tab.getFavicon(),
540 createScreenshot(tab.getWebView(),
541 getDesiredThumbnailWidth(mActivity),
542 getDesiredThumbnailHeight(mActivity)));
543 }
544 }
545
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 /**
547 * Share a page, providing the title, url, favicon, and a screenshot. Uses
548 * an {@link Intent} to launch the Activity chooser.
549 * @param c Context used to launch a new Activity.
550 * @param title Title of the page. Stored in the Intent with
551 * {@link Intent#EXTRA_SUBJECT}
552 * @param url URL of the page. Stored in the Intent with
553 * {@link Intent#EXTRA_TEXT}
554 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
555 * with {@link Browser#EXTRA_SHARE_FAVICON}
556 * @param screenshot Bitmap of a screenshot of the page. Stored in the
557 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
558 */
559 static final void sharePage(Context c, String title, String url,
560 Bitmap favicon, Bitmap screenshot) {
561 Intent send = new Intent(Intent.ACTION_SEND);
562 send.setType("text/plain");
563 send.putExtra(Intent.EXTRA_TEXT, url);
564 send.putExtra(Intent.EXTRA_SUBJECT, title);
565 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
566 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
567 try {
568 c.startActivity(Intent.createChooser(send, c.getString(
569 R.string.choosertitle_sharevia)));
570 } catch(android.content.ActivityNotFoundException ex) {
571 // if no app handles it, do nothing
572 }
573 }
574
575 private void copy(CharSequence text) {
576 ClipboardManager cm = (ClipboardManager) mActivity
577 .getSystemService(Context.CLIPBOARD_SERVICE);
578 cm.setText(text);
579 }
580
581 // lifecycle
582
583 protected void onConfgurationChanged(Configuration config) {
584 mConfigChanged = true;
585 if (mPageDialogsHandler != null) {
586 mPageDialogsHandler.onConfigurationChanged(config);
587 }
588 mUi.onConfigurationChanged(config);
589 }
590
591 @Override
592 public void handleNewIntent(Intent intent) {
593 mIntentHandler.onNewIntent(intent);
594 }
595
596 protected void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800597 if (mUi.isCustomViewShowing()) {
598 hideCustomView();
599 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700600 if (mActivityPaused) {
601 Log.e(LOGTAG, "BrowserActivity is already paused.");
602 return;
603 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700604 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800605 Tab tab = mTabControl.getCurrentTab();
606 if (tab != null) {
607 tab.pause();
608 if (!pauseWebViewTimers(tab)) {
609 mWakeLock.acquire();
610 mHandler.sendMessageDelayed(mHandler
611 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
612 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700613 }
614 mUi.onPause();
615 mNetworkHandler.onPause();
616
617 WebView.disablePlatformNotifications();
John Reck847b5322011-04-14 17:02:18 -0700618 mCrashRecoveryHandler.clearState();
Michael Kolb8233fac2010-10-26 16:08:53 -0700619 }
620
John Reckaed9c542011-05-27 16:08:53 -0700621 void onSaveInstanceState(Bundle outState, boolean saveImages) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700622 // the default implementation requires each view to have an id. As the
623 // browser handles the state itself and it doesn't use id for the views,
624 // don't call the default implementation. Otherwise it will trigger the
625 // warning like this, "couldn't save which view has focus because the
626 // focused view XXX has no id".
627
628 // Save all the tabs
John Reckaed9c542011-05-27 16:08:53 -0700629 mTabControl.saveState(outState, saveImages);
Michael Kolb8233fac2010-10-26 16:08:53 -0700630 // Save time so that we know how old incognito tabs (if any) are.
631 outState.putSerializable("lastActiveDate", Calendar.getInstance());
632 }
633
634 void onResume() {
635 if (!mActivityPaused) {
636 Log.e(LOGTAG, "BrowserActivity is already resumed.");
637 return;
638 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800640 Tab current = mTabControl.getCurrentTab();
641 if (current != null) {
642 current.resume();
643 resumeWebViewTimers(current);
644 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700645 if (mWakeLock.isHeld()) {
646 mHandler.removeMessages(RELEASE_WAKELOCK);
647 mWakeLock.release();
648 }
649 mUi.onResume();
650 mNetworkHandler.onResume();
651 WebView.enablePlatformNotifications();
652 }
653
Michael Kolb70976932010-11-30 11:34:01 -0800654 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800655 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800656 * @param tab guaranteed non-null
657 */
658 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700659 boolean inLoad = tab.inPageLoad();
660 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
661 CookieSyncManager.getInstance().startSync();
662 WebView w = tab.getWebView();
663 if (w != null) {
664 w.resumeTimers();
665 }
666 }
667 }
668
Michael Kolb70976932010-11-30 11:34:01 -0800669 /**
670 * Pause all WebView timers using the WebView of the given tab
671 * @param tab
672 * @return true if the timers are paused or tab is null
673 */
674 private boolean pauseWebViewTimers(Tab tab) {
675 if (tab == null) {
676 return true;
677 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700678 CookieSyncManager.getInstance().stopSync();
679 WebView w = getCurrentWebView();
680 if (w != null) {
681 w.pauseTimers();
682 }
683 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700684 }
Michael Kolb70976932010-11-30 11:34:01 -0800685 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700686 }
687
688 void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800689 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700690 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
691 mUploadHandler = null;
692 }
693 if (mTabControl == null) return;
694 mUi.onDestroy();
695 // Remove the current tab and sub window
696 Tab t = mTabControl.getCurrentTab();
697 if (t != null) {
698 dismissSubWindow(t);
699 removeTab(t);
700 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500701 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700702 // Destroy all the tabs
703 mTabControl.destroy();
704 WebIconDatabase.getInstance().close();
705 // Stop watching the default geolocation permissions
706 mSystemAllowGeolocationOrigins.stop();
707 mSystemAllowGeolocationOrigins = null;
708 }
709
710 protected boolean isActivityPaused() {
711 return mActivityPaused;
712 }
713
714 protected void onLowMemory() {
715 mTabControl.freeMemory();
716 }
717
718 @Override
719 public boolean shouldShowErrorConsole() {
720 return mShouldShowErrorConsole;
721 }
722
723 protected void setShouldShowErrorConsole(boolean show) {
724 if (show == mShouldShowErrorConsole) {
725 // Nothing to do.
726 return;
727 }
728 mShouldShowErrorConsole = show;
729 Tab t = mTabControl.getCurrentTab();
730 if (t == null) {
731 // There is no current tab so we cannot toggle the error console
732 return;
733 }
734 mUi.setShouldShowErrorConsole(t, show);
735 }
736
737 @Override
738 public void stopLoading() {
739 mLoadStopped = true;
740 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700741 WebView w = getCurrentTopWebView();
742 w.stopLoading();
Michael Kolb8233fac2010-10-26 16:08:53 -0700743 mUi.onPageStopped(tab);
744 }
745
746 boolean didUserStopLoading() {
747 return mLoadStopped;
748 }
749
750 // WebViewController
751
752 @Override
John Reck324d4402011-01-11 16:56:42 -0800753 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700754
755 // We've started to load a new page. If there was a pending message
756 // to save a screenshot then we will now take the new page and save
757 // an incorrect screenshot. Therefore, remove any pending thumbnail
758 // messages from the queue.
759 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800760 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700761
762 // reset sync timer to avoid sync starts during loading a page
763 CookieSyncManager.getInstance().resetSync();
764
765 if (!mNetworkHandler.isNetworkUp()) {
766 view.setNetworkAvailable(false);
767 }
768
769 // when BrowserActivity just starts, onPageStarted may be called before
770 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
771 // to start the timer. As we won't switch tabs while an activity is in
772 // pause state, we can ensure calling resume and pause in pair.
773 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800774 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700775 }
776 mLoadStopped = false;
777 if (!mNetworkHandler.isNetworkUp()) {
778 mNetworkHandler.createAndShowNetworkDialog();
779 }
780 endActionMode();
781
John Reck30c714c2010-12-16 17:30:34 -0800782 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700783
John Reck324d4402011-01-11 16:56:42 -0800784 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700785 // update the bookmark database for favicon
786 maybeUpdateFavicon(tab, null, url, favicon);
787
788 Performance.tracePageStart(url);
789
790 // Performance probe
791 if (false) {
792 Performance.onPageStarted();
793 }
794
795 }
796
797 @Override
John Reck324d4402011-01-11 16:56:42 -0800798 public void onPageFinished(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800799 mUi.onTabDataChanged(tab);
John Reck324d4402011-01-11 16:56:42 -0800800 if (!tab.isPrivateBrowsingEnabled()
801 && !TextUtils.isEmpty(tab.getUrl())) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700802 if (tab.inForeground() && !didUserStopLoading()
803 || !tab.inForeground()) {
804 // Only update the bookmark screenshot if the user did not
805 // cancel the load early.
806 mHandler.sendMessageDelayed(mHandler.obtainMessage(
John Reck34ef2672011-02-10 11:30:55 -0800807 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Michael Kolb8233fac2010-10-26 16:08:53 -0700808 500);
809 }
810 }
811 // pause the WebView timer and release the wake lock if it is finished
812 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800813 if (mActivityPaused && pauseWebViewTimers(tab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700814 if (mWakeLock.isHeld()) {
815 mHandler.removeMessages(RELEASE_WAKELOCK);
816 mWakeLock.release();
817 }
818 }
819 // Performance probe
820 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800821 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700822 }
823
824 Performance.tracePageFinished();
825 }
826
827 @Override
John Reck30c714c2010-12-16 17:30:34 -0800828 public void onProgressChanged(Tab tab) {
829 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700830
831 if (newProgress == 100) {
832 CookieSyncManager.getInstance().sync();
833 // onProgressChanged() may continue to be called after the main
834 // frame has finished loading, as any remaining sub frames continue
835 // to load. We'll only get called once though with newProgress as
836 // 100 when everything is loaded. (onPageFinished is called once
837 // when the main frame completes loading regardless of the state of
838 // any sub frames so calls to onProgressChanges may continue after
839 // onPageFinished has executed)
840 if (mInLoad) {
841 mInLoad = false;
842 updateInLoadMenuItems(mCachedMenu);
843 }
844 } else {
845 if (!mInLoad) {
846 // onPageFinished may have already been called but a subframe is
847 // still loading and updating the progress. Reset mInLoad and
848 // update the menu items.
849 mInLoad = true;
850 updateInLoadMenuItems(mCachedMenu);
851 }
852 }
John Reck30c714c2010-12-16 17:30:34 -0800853 mUi.onProgressChanged(tab);
854 }
855
856 @Override
857 public void onUpdatedLockIcon(Tab tab) {
858 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700859 }
860
861 @Override
862 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800863 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800864 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800865 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700866 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
867 return;
868 }
869 // Update the title in the history database if not in private browsing mode
870 if (!tab.isPrivateBrowsingEnabled()) {
John Reck0ebd3ac2010-12-09 11:14:04 -0800871 mDataController.updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700872 }
873 }
874
875 @Override
876 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800877 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
879 }
880
881 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800882 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
883 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700884 }
885
886 @Override
887 public boolean shouldOverrideKeyEvent(KeyEvent event) {
888 if (mMenuIsDown) {
889 // only check shortcut key when MENU is held
890 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
891 event);
892 } else {
893 return false;
894 }
895 }
896
897 @Override
898 public void onUnhandledKeyEvent(KeyEvent event) {
899 if (!isActivityPaused()) {
900 if (event.getAction() == KeyEvent.ACTION_DOWN) {
901 mActivity.onKeyDown(event.getKeyCode(), event);
902 } else {
903 mActivity.onKeyUp(event.getKeyCode(), event);
904 }
905 }
906 }
907
908 @Override
John Reck324d4402011-01-11 16:56:42 -0800909 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700910 // Don't save anything in private browsing mode
911 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800912 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700913
John Reck324d4402011-01-11 16:56:42 -0800914 if (TextUtils.isEmpty(url)
915 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700916 return;
917 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800918 mDataController.updateVisitedHistory(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700919 WebIconDatabase.getInstance().retainIconForPageUrl(url);
John Reck847b5322011-04-14 17:02:18 -0700920 if (!mActivityPaused) {
921 // Since we clear the state in onPause, don't backup the current
922 // state if we are already paused
923 mCrashRecoveryHandler.backupState();
924 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700925 }
926
927 @Override
928 public void getVisitedHistory(final ValueCallback<String[]> callback) {
929 AsyncTask<Void, Void, String[]> task =
930 new AsyncTask<Void, Void, String[]>() {
931 @Override
932 public String[] doInBackground(Void... unused) {
933 return Browser.getVisitedHistory(mActivity.getContentResolver());
934 }
935 @Override
936 public void onPostExecute(String[] result) {
937 callback.onReceiveValue(result);
938 }
939 };
940 task.execute();
941 }
942
943 @Override
944 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
945 final HttpAuthHandler handler, final String host,
946 final String realm) {
947 String username = null;
948 String password = null;
949
950 boolean reuseHttpAuthUsernamePassword
951 = handler.useHttpAuthUsernamePassword();
952
953 if (reuseHttpAuthUsernamePassword && view != null) {
954 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
955 if (credentials != null && credentials.length == 2) {
956 username = credentials[0];
957 password = credentials[1];
958 }
959 }
960
961 if (username != null && password != null) {
962 handler.proceed(username, password);
963 } else {
964 if (tab.inForeground()) {
965 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
966 } else {
967 handler.cancel();
968 }
969 }
970 }
971
972 @Override
973 public void onDownloadStart(Tab tab, String url, String userAgent,
974 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000975 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -0500976 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000977 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
978 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700979 // This Tab was opened for the sole purpose of downloading a
980 // file. Remove it.
981 if (tab == mTabControl.getCurrentTab()) {
982 // In this case, the Tab is still on top.
983 goBackOnePageOrQuit();
984 } else {
985 // In this case, it is not.
986 closeTab(tab);
987 }
988 }
989 }
990
991 @Override
992 public Bitmap getDefaultVideoPoster() {
993 return mUi.getDefaultVideoPoster();
994 }
995
996 @Override
997 public View getVideoLoadingProgressView() {
998 return mUi.getVideoLoadingProgressView();
999 }
1000
1001 @Override
1002 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1003 SslError error) {
1004 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1005 }
1006
Patrick Scott92066772011-03-10 08:46:27 -05001007 @Override
1008 public void showAutoLogin(Tab tab) {
1009 assert tab.inForeground();
1010 // Update the title bar to show the auto-login request.
1011 mUi.showAutoLogin(tab);
1012 }
1013
1014 @Override
1015 public void hideAutoLogin(Tab tab) {
1016 assert tab.inForeground();
1017 mUi.hideAutoLogin(tab);
1018 }
1019
Michael Kolb8233fac2010-10-26 16:08:53 -07001020 // helper method
1021
1022 /*
1023 * Update the favorites icon if the private browsing isn't enabled and the
1024 * icon is valid.
1025 */
1026 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1027 final String url, Bitmap favicon) {
1028 if (favicon == null) {
1029 return;
1030 }
1031 if (!tab.isPrivateBrowsingEnabled()) {
1032 Bookmarks.updateFavicon(mActivity
1033 .getContentResolver(), originalUrl, url, favicon);
1034 }
1035 }
1036
Leon Scroggins4cd97792010-12-03 15:31:56 -05001037 @Override
1038 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001039 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001040 mUi.bookmarkedStatusHasChanged(tab);
1041 }
1042
Michael Kolb8233fac2010-10-26 16:08:53 -07001043 // end WebViewController
1044
1045 protected void pageUp() {
1046 getCurrentTopWebView().pageUp(false);
1047 }
1048
1049 protected void pageDown() {
1050 getCurrentTopWebView().pageDown(false);
1051 }
1052
1053 // callback from phone title bar
1054 public void editUrl() {
1055 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001056 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001057 }
1058
Michael Kolbcfa3af52010-12-14 10:36:11 -08001059 public void startVoiceSearch() {
1060 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1061 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1062 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1063 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1064 mActivity.getComponentName().flattenToString());
1065 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001066 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001067 mActivity.startActivity(intent);
1068 }
1069
Michael Kolb11d19782011-03-20 10:17:40 -07001070 @Override
1071 public void activateVoiceSearchMode(String title, List<String> results) {
1072 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001073 }
1074
1075 public void revertVoiceSearchMode(Tab tab) {
1076 mUi.revertVoiceTitleBar(tab);
1077 }
1078
Michael Kolb736990c2011-03-20 10:01:20 -07001079 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001080 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001081 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1082 }
1083
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001084 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001085 WebChromeClient.CustomViewCallback callback) {
1086 if (tab.inForeground()) {
1087 if (mUi.isCustomViewShowing()) {
1088 callback.onCustomViewHidden();
1089 return;
1090 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001091 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001092 // Save the menu state and set it to empty while the custom
1093 // view is showing.
1094 mOldMenuState = mMenuState;
1095 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001096 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001097 }
1098 }
1099
1100 @Override
1101 public void hideCustomView() {
1102 if (mUi.isCustomViewShowing()) {
1103 mUi.onHideCustomView();
1104 // Reset the old menu state.
1105 mMenuState = mOldMenuState;
1106 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001107 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001108 }
1109 }
1110
1111 protected void onActivityResult(int requestCode, int resultCode,
1112 Intent intent) {
1113 if (getCurrentTopWebView() == null) return;
1114 switch (requestCode) {
1115 case PREFERENCES_PAGE:
1116 if (resultCode == Activity.RESULT_OK && intent != null) {
1117 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001118 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001119 mTabControl.removeParentChildRelationShips();
1120 }
1121 }
1122 break;
1123 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001124 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001125 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001126 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001127 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001128 case AUTOFILL_SETUP:
1129 // Determine whether a profile was actually set up or not
1130 // and if so, send the message back to the WebTextView to
1131 // fill the form with the new profile.
1132 if (getSettings().getAutoFillProfile() != null) {
1133 mAutoFillSetupMessage.sendToTarget();
1134 mAutoFillSetupMessage = null;
1135 }
1136 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001137 default:
1138 break;
1139 }
1140 getCurrentTopWebView().requestFocus();
1141 }
1142
1143 /**
1144 * Open the Go page.
1145 * @param startWithHistory If true, open starting on the history tab.
1146 * Otherwise, start with the bookmarks tab.
1147 */
1148 @Override
1149 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1150 if (mTabControl.getCurrentWebView() == null) {
1151 return;
1152 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001153 // clear action mode
1154 if (isInCustomActionMode()) {
1155 endActionMode();
1156 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001157 Bundle extras = new Bundle();
1158 // Disable opening in a new window if we have maxed out the windows
1159 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1160 !mTabControl.canCreateNewTab());
1161 mUi.showComboView(startWithHistory, extras);
1162 }
1163
1164 // combo view callbacks
1165
1166 /**
1167 * callback from ComboPage when clear history is requested
1168 */
1169 public void onRemoveParentChildRelationships() {
1170 mTabControl.removeParentChildRelationShips();
1171 }
1172
1173 /**
1174 * callback from ComboPage when bookmark/history selection
1175 */
1176 @Override
1177 public void onUrlSelected(String url, boolean newTab) {
1178 removeComboView();
1179 if (!TextUtils.isEmpty(url)) {
1180 if (newTab) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07001181 final Tab parent = mTabControl.getCurrentTab();
1182 openTab(url,
1183 (parent != null) && parent.isPrivateBrowsingEnabled(),
1184 !mSettings.openInBackground(),
1185 true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001186 } else {
1187 final Tab currentTab = mTabControl.getCurrentTab();
1188 dismissSubWindow(currentTab);
1189 loadUrl(getCurrentTopWebView(), url);
1190 }
1191 }
1192 }
1193
1194 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001195 * dismiss the ComboPage
1196 */
1197 @Override
1198 public void removeComboView() {
1199 mUi.hideComboView();
1200 }
1201
1202 // active tabs page handling
1203
1204 protected void showActiveTabsPage() {
1205 mMenuState = EMPTY_MENU;
1206 mUi.showActiveTabsPage();
1207 }
1208
1209 /**
1210 * Remove the active tabs page.
1211 * @param needToAttach If true, the active tabs page did not attach a tab
1212 * to the content view, so we need to do that here.
1213 */
1214 @Override
1215 public void removeActiveTabsPage(boolean needToAttach) {
1216 mMenuState = R.id.MAIN_MENU;
1217 mUi.removeActiveTabsPage();
1218 if (needToAttach) {
1219 setActiveTab(mTabControl.getCurrentTab());
1220 }
1221 getCurrentTopWebView().requestFocus();
1222 }
1223
1224 // key handling
1225 protected void onBackKey() {
1226 if (!mUi.onBackKey()) {
1227 WebView subwindow = mTabControl.getCurrentSubWindow();
1228 if (subwindow != null) {
1229 if (subwindow.canGoBack()) {
1230 subwindow.goBack();
1231 } else {
1232 dismissSubWindow(mTabControl.getCurrentTab());
1233 }
1234 } else {
1235 goBackOnePageOrQuit();
1236 }
1237 }
1238 }
1239
Michael Kolb4bd767d2011-05-27 11:33:55 -07001240 protected boolean onMenuKey() {
1241 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001242 }
1243
Michael Kolb8233fac2010-10-26 16:08:53 -07001244 // menu handling and state
1245 // TODO: maybe put into separate handler
1246
1247 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckb3417f02011-01-14 11:01:05 -08001248 if (mOptionsMenuHandler != null) {
1249 return mOptionsMenuHandler.onCreateOptionsMenu(menu);
1250 }
1251
John Reckd73c5a22010-12-22 10:22:50 -08001252 if (mMenuState == EMPTY_MENU) {
1253 return false;
1254 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001255 MenuInflater inflater = mActivity.getMenuInflater();
1256 inflater.inflate(R.menu.browser, menu);
1257 updateInLoadMenuItems(menu);
1258 // hold on to the menu reference here; it is used by the page callbacks
1259 // to update the menu based on loading state
1260 mCachedMenu = menu;
1261 return true;
1262 }
1263
1264 protected void onCreateContextMenu(ContextMenu menu, View v,
1265 ContextMenuInfo menuInfo) {
1266 if (v instanceof TitleBarBase) {
1267 return;
1268 }
1269 if (!(v instanceof WebView)) {
1270 return;
1271 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001272 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001273 WebView.HitTestResult result = webview.getHitTestResult();
1274 if (result == null) {
1275 return;
1276 }
1277
1278 int type = result.getType();
1279 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1280 Log.w(LOGTAG,
1281 "We should not show context menu when nothing is touched");
1282 return;
1283 }
1284 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1285 // let TextView handles context menu
1286 return;
1287 }
1288
1289 // Note, http://b/issue?id=1106666 is requesting that
1290 // an inflated menu can be used again. This is not available
1291 // yet, so inflate each time (yuk!)
1292 MenuInflater inflater = mActivity.getMenuInflater();
1293 inflater.inflate(R.menu.browsercontext, menu);
1294
1295 // Show the correct menu group
1296 final String extra = result.getExtra();
1297 menu.setGroupVisible(R.id.PHONE_MENU,
1298 type == WebView.HitTestResult.PHONE_TYPE);
1299 menu.setGroupVisible(R.id.EMAIL_MENU,
1300 type == WebView.HitTestResult.EMAIL_TYPE);
1301 menu.setGroupVisible(R.id.GEO_MENU,
1302 type == WebView.HitTestResult.GEO_TYPE);
1303 menu.setGroupVisible(R.id.IMAGE_MENU,
1304 type == WebView.HitTestResult.IMAGE_TYPE
1305 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1306 menu.setGroupVisible(R.id.ANCHOR_MENU,
1307 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1308 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001309 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1310 || type == WebView.HitTestResult.PHONE_TYPE
1311 || type == WebView.HitTestResult.EMAIL_TYPE
1312 || type == WebView.HitTestResult.GEO_TYPE;
1313 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1314 if (hitText) {
1315 menu.findItem(R.id.select_text_menu_id)
1316 .setOnMenuItemClickListener(new SelectText(webview));
1317 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001318 // Setup custom handling depending on the type
1319 switch (type) {
1320 case WebView.HitTestResult.PHONE_TYPE:
1321 menu.setHeaderTitle(Uri.decode(extra));
1322 menu.findItem(R.id.dial_context_menu_id).setIntent(
1323 new Intent(Intent.ACTION_VIEW, Uri
1324 .parse(WebView.SCHEME_TEL + extra)));
1325 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1326 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1327 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1328 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1329 addIntent);
1330 menu.findItem(R.id.copy_phone_context_menu_id)
1331 .setOnMenuItemClickListener(
1332 new Copy(extra));
1333 break;
1334
1335 case WebView.HitTestResult.EMAIL_TYPE:
1336 menu.setHeaderTitle(extra);
1337 menu.findItem(R.id.email_context_menu_id).setIntent(
1338 new Intent(Intent.ACTION_VIEW, Uri
1339 .parse(WebView.SCHEME_MAILTO + extra)));
1340 menu.findItem(R.id.copy_mail_context_menu_id)
1341 .setOnMenuItemClickListener(
1342 new Copy(extra));
1343 break;
1344
1345 case WebView.HitTestResult.GEO_TYPE:
1346 menu.setHeaderTitle(extra);
1347 menu.findItem(R.id.map_context_menu_id).setIntent(
1348 new Intent(Intent.ACTION_VIEW, Uri
1349 .parse(WebView.SCHEME_GEO
1350 + URLEncoder.encode(extra))));
1351 menu.findItem(R.id.copy_geo_context_menu_id)
1352 .setOnMenuItemClickListener(
1353 new Copy(extra));
1354 break;
1355
1356 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1357 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001358 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001359 // decide whether to show the open link in new tab option
1360 boolean showNewTab = mTabControl.canCreateNewTab();
1361 MenuItem newTabItem
1362 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001363 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001364 ? R.string.contextmenu_openlink_newwindow_background
1365 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001366 newTabItem.setVisible(showNewTab);
1367 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001368 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1369 newTabItem.setOnMenuItemClickListener(
1370 new MenuItem.OnMenuItemClickListener() {
1371 @Override
1372 public boolean onMenuItemClick(MenuItem item) {
1373 final HashMap<String, WebView> hrefMap =
1374 new HashMap<String, WebView>();
1375 hrefMap.put("webview", webview);
1376 final Message msg = mHandler.obtainMessage(
1377 FOCUS_NODE_HREF,
1378 R.id.open_newtab_context_menu_id,
1379 0, hrefMap);
1380 webview.requestFocusNodeHref(msg);
1381 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001382 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001383 });
1384 } else {
1385 newTabItem.setOnMenuItemClickListener(
1386 new MenuItem.OnMenuItemClickListener() {
1387 @Override
1388 public boolean onMenuItemClick(MenuItem item) {
1389 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001390 openTab(extra, parent,
1391 !mSettings.openInBackground(),
1392 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001393 return true;
1394 }
1395 });
1396 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001397 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001398 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1399 break;
1400 }
1401 // otherwise fall through to handle image part
1402 case WebView.HitTestResult.IMAGE_TYPE:
1403 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1404 menu.setHeaderTitle(extra);
1405 }
1406 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1407 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1408 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001409 setOnMenuItemClickListener(
1410 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001411 menu.findItem(R.id.set_wallpaper_context_menu_id).
1412 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1413 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001414 break;
1415
1416 default:
1417 Log.w(LOGTAG, "We should not get here.");
1418 break;
1419 }
1420 //update the ui
1421 mUi.onContextMenuCreated(menu);
1422 }
1423
1424 /**
1425 * As the menu can be open when loading state changes
1426 * we must manually update the state of the stop/reload menu
1427 * item
1428 */
1429 private void updateInLoadMenuItems(Menu menu) {
1430 if (menu == null) {
1431 return;
1432 }
1433 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1434 MenuItem src = mInLoad ?
1435 menu.findItem(R.id.stop_menu_id):
1436 menu.findItem(R.id.reload_menu_id);
1437 if (src != null) {
1438 dest.setIcon(src.getIcon());
1439 dest.setTitle(src.getTitle());
1440 }
1441 }
1442
John Reckb3417f02011-01-14 11:01:05 -08001443 boolean onPrepareOptionsMenu(Menu menu) {
1444 if (mOptionsMenuHandler != null) {
1445 return mOptionsMenuHandler.onPrepareOptionsMenu(menu);
1446 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001447 // Note: setVisible will decide whether an item is visible; while
1448 // setEnabled() will decide whether an item is enabled, which also means
1449 // whether the matching shortcut key will function.
1450 switch (mMenuState) {
1451 case EMPTY_MENU:
1452 if (mCurrentMenuState != mMenuState) {
1453 menu.setGroupVisible(R.id.MAIN_MENU, false);
1454 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1455 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1456 }
1457 break;
1458 default:
1459 if (mCurrentMenuState != mMenuState) {
1460 menu.setGroupVisible(R.id.MAIN_MENU, true);
1461 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1462 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1463 }
1464 final WebView w = getCurrentTopWebView();
1465 boolean canGoBack = false;
1466 boolean canGoForward = false;
1467 boolean isHome = false;
1468 if (w != null) {
1469 canGoBack = w.canGoBack();
1470 canGoForward = w.canGoForward();
1471 isHome = mSettings.getHomePage().equals(w.getUrl());
1472 }
1473 final MenuItem back = menu.findItem(R.id.back_menu_id);
1474 back.setEnabled(canGoBack);
1475
1476 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1477 home.setEnabled(!isHome);
1478
1479 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1480 forward.setEnabled(canGoForward);
1481
1482 // decide whether to show the share link option
1483 PackageManager pm = mActivity.getPackageManager();
1484 Intent send = new Intent(Intent.ACTION_SEND);
1485 send.setType("text/plain");
1486 ResolveInfo ri = pm.resolveActivity(send,
1487 PackageManager.MATCH_DEFAULT_ONLY);
1488 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1489
John Reck35e9dd62011-04-25 09:01:54 -07001490 boolean isNavDump = mSettings.enableNavDump();
Michael Kolb8233fac2010-10-26 16:08:53 -07001491 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1492 nav.setVisible(isNavDump);
1493 nav.setEnabled(isNavDump);
1494
John Reck35e9dd62011-04-25 09:01:54 -07001495 boolean showDebugSettings = mSettings.isDebugEnabled();
Michael Kolb8233fac2010-10-26 16:08:53 -07001496 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1497 counter.setVisible(showDebugSettings);
1498 counter.setEnabled(showDebugSettings);
1499
John Reckb3417f02011-01-14 11:01:05 -08001500 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
1501 newtab.setEnabled(getTabControl().canCreateNewTab());
Michael Kolb8233fac2010-10-26 16:08:53 -07001502
Leon Scroggins81983762011-02-11 15:17:36 -05001503 MenuItem archive = menu.findItem(R.id.save_webarchive_menu_id);
John Reck51d8bad2011-02-28 15:47:47 -08001504 Tab tab = getTabControl().getCurrentTab();
1505 String url = tab != null ? tab.getUrl() : null;
1506 archive.setVisible(!TextUtils.isEmpty(url)
1507 && !url.endsWith(".webarchivexml"));
Michael Kolb8233fac2010-10-26 16:08:53 -07001508 break;
1509 }
1510 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001511 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001512 }
1513
1514 public boolean onOptionsItemSelected(MenuItem item) {
John Reckb3417f02011-01-14 11:01:05 -08001515 if (mOptionsMenuHandler != null &&
1516 mOptionsMenuHandler.onOptionsItemSelected(item)) {
1517 return true;
1518 }
1519
Michael Kolb8233fac2010-10-26 16:08:53 -07001520 if (item.getGroupId() != R.id.CONTEXT_MENU) {
1521 // menu remains active, so ensure comboview is dismissed
1522 // if main menu option is selected
1523 removeComboView();
1524 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001525 if (null == getCurrentTopWebView()) {
1526 return false;
1527 }
1528 if (mMenuIsDown) {
1529 // The shortcut action consumes the MENU. Even if it is still down,
1530 // it won't trigger the next shortcut action. In the case of the
1531 // shortcut action triggering a new activity, like Bookmarks, we
1532 // won't get onKeyUp for MENU. So it is important to reset it here.
1533 mMenuIsDown = false;
1534 }
1535 switch (item.getItemId()) {
1536 // -- Main menu
1537 case R.id.new_tab_menu_id:
1538 openTabToHomePage();
1539 break;
1540
1541 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001542 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001543 break;
1544
1545 case R.id.goto_menu_id:
1546 editUrl();
1547 break;
1548
1549 case R.id.bookmarks_menu_id:
1550 bookmarksOrHistoryPicker(false);
1551 break;
1552
1553 case R.id.active_tabs_menu_id:
1554 showActiveTabsPage();
1555 break;
1556
1557 case R.id.add_bookmark_menu_id:
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001558 bookmarkCurrentPage(AddBookmarkPage.DEFAULT_FOLDER_ID, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001559 break;
1560
1561 case R.id.stop_reload_menu_id:
1562 if (mInLoad) {
1563 stopLoading();
1564 } else {
1565 getCurrentTopWebView().reload();
1566 }
1567 break;
1568
1569 case R.id.back_menu_id:
1570 getCurrentTopWebView().goBack();
1571 break;
1572
1573 case R.id.forward_menu_id:
1574 getCurrentTopWebView().goForward();
1575 break;
1576
1577 case R.id.close_menu_id:
1578 // Close the subwindow if it exists.
1579 if (mTabControl.getCurrentSubWindow() != null) {
1580 dismissSubWindow(mTabControl.getCurrentTab());
1581 break;
1582 }
1583 closeCurrentTab();
1584 break;
1585
1586 case R.id.homepage_menu_id:
1587 Tab current = mTabControl.getCurrentTab();
1588 if (current != null) {
1589 dismissSubWindow(current);
1590 loadUrl(current.getWebView(), mSettings.getHomePage());
1591 }
1592 break;
1593
1594 case R.id.preferences_menu_id:
1595 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1596 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1597 getCurrentTopWebView().getUrl());
1598 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1599 break;
1600
1601 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001602 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001603 break;
1604
John Reckf33b1632011-06-04 20:00:23 -07001605 case R.id.freeze_tab_menu_id:
1606 // TODO: Show error messages
John Reck541f55a2011-06-07 16:34:43 -07001607 Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001608 if (source == null) break;
John Reck541f55a2011-06-07 16:34:43 -07001609 Tab snapshot = createNewTab(false, false, false);
1610 if (snapshot == null) break;
John Reckf33b1632011-06-04 20:00:23 -07001611 try {
1612 ByteArrayOutputStream bos = new ByteArrayOutputStream();
John Reck541f55a2011-06-07 16:34:43 -07001613 source.saveSnapshot(bos);
John Reckf33b1632011-06-04 20:00:23 -07001614 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
John Reck541f55a2011-06-07 16:34:43 -07001615 snapshot.loadSnapshot(bis);
1616 mUi.onTabDataChanged(snapshot);
John Reckf33b1632011-06-04 20:00:23 -07001617 bis.close();
1618 bos.close();
John Reck541f55a2011-06-07 16:34:43 -07001619 setActiveTab(snapshot);
John Reckf33b1632011-06-04 20:00:23 -07001620 } catch (IOException e) {
John Reckf33b1632011-06-04 20:00:23 -07001621 }
1622 break;
1623
Leon Scrogginsac993842011-02-02 12:54:07 -05001624 case R.id.save_webarchive_menu_id:
1625 String state = Environment.getExternalStorageState();
1626 if (!Environment.MEDIA_MOUNTED.equals(state)) {
1627 Log.e(LOGTAG, "External storage not mounted");
1628 Toast.makeText(mActivity, R.string.webarchive_failed,
1629 Toast.LENGTH_SHORT).show();
1630 break;
1631 }
1632 final String directory = Environment.getExternalStoragePublicDirectory(
1633 Environment.DIRECTORY_DOWNLOADS) + File.separator;
1634 File dir = new File(directory);
1635 if (!dir.exists() && !dir.mkdirs()) {
1636 Log.e(LOGTAG, "Save as Web Archive: mkdirs for " + directory + " failed!");
1637 Toast.makeText(mActivity, R.string.webarchive_failed,
1638 Toast.LENGTH_SHORT).show();
1639 break;
1640 }
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001641 final WebView topWebView = getCurrentTopWebView();
Leon Scrogginsac993842011-02-02 12:54:07 -05001642 final String title = topWebView.getTitle();
John Reck51d8bad2011-02-28 15:47:47 -08001643 final String url = topWebView.getUrl();
Leon Scrogginsac993842011-02-02 12:54:07 -05001644 topWebView.saveWebArchive(directory, true,
1645 new ValueCallback<String>() {
1646 @Override
1647 public void onReceiveValue(final String value) {
1648 if (value != null) {
1649 File file = new File(value);
1650 final long length = file.length();
1651 if (file.exists() && length > 0) {
Leon Scroggins1cb96552011-02-11 14:22:57 -05001652 Toast.makeText(mActivity, R.string.webarchive_saved,
1653 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001654 final DownloadManager manager = (DownloadManager) mActivity
1655 .getSystemService(Context.DOWNLOAD_SERVICE);
1656 new Thread("Add WebArchive to download manager") {
1657 @Override
1658 public void run() {
Vasu Noria9e30a72011-03-07 11:37:34 -08001659 manager.addCompletedDownload(
1660 null == title ? value : title,
Leon Scrogginsac993842011-02-02 12:54:07 -05001661 value, true, "application/x-webarchive-xml",
1662 value, length, true);
1663 }
1664 }.start();
1665 return;
1666 }
1667 }
John Reck51d8bad2011-02-28 15:47:47 -08001668 DownloadHandler.onDownloadStartNoStream(mActivity,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001669 url, null, null, null, topWebView.isPrivateBrowsingEnabled());
Leon Scrogginsac993842011-02-02 12:54:07 -05001670 }
1671 });
1672 break;
1673
Michael Kolb8233fac2010-10-26 16:08:53 -07001674 case R.id.page_info_menu_id:
1675 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(),
1676 false);
1677 break;
1678
1679 case R.id.classic_history_menu_id:
1680 bookmarksOrHistoryPicker(true);
1681 break;
1682
1683 case R.id.title_bar_share_page_url:
1684 case R.id.share_page_menu_id:
1685 Tab currentTab = mTabControl.getCurrentTab();
1686 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001687 return false;
1688 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001689 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001690 break;
1691
1692 case R.id.dump_nav_menu_id:
1693 getCurrentTopWebView().debugDump();
1694 break;
1695
1696 case R.id.dump_counters_menu_id:
1697 getCurrentTopWebView().dumpV8Counters();
1698 break;
1699
1700 case R.id.zoom_in_menu_id:
1701 getCurrentTopWebView().zoomIn();
1702 break;
1703
1704 case R.id.zoom_out_menu_id:
1705 getCurrentTopWebView().zoomOut();
1706 break;
1707
1708 case R.id.view_downloads_menu_id:
1709 viewDownloads();
1710 break;
1711
1712 case R.id.window_one_menu_id:
1713 case R.id.window_two_menu_id:
1714 case R.id.window_three_menu_id:
1715 case R.id.window_four_menu_id:
1716 case R.id.window_five_menu_id:
1717 case R.id.window_six_menu_id:
1718 case R.id.window_seven_menu_id:
1719 case R.id.window_eight_menu_id:
1720 {
1721 int menuid = item.getItemId();
1722 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1723 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1724 Tab desiredTab = mTabControl.getTab(id);
1725 if (desiredTab != null &&
1726 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001727 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001728 }
1729 break;
1730 }
1731 }
1732 }
1733 break;
1734
1735 default:
1736 return false;
1737 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001738 return true;
1739 }
1740
1741 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001742 // Let the History and Bookmark fragments handle menus they created.
1743 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1744 return false;
1745 }
1746
Michael Kolb8233fac2010-10-26 16:08:53 -07001747 int id = item.getItemId();
1748 boolean result = true;
1749 switch (id) {
1750 // For the context menu from the title bar
1751 case R.id.title_bar_copy_page_url:
1752 Tab currentTab = mTabControl.getCurrentTab();
1753 if (null == currentTab) {
1754 result = false;
1755 break;
1756 }
1757 WebView mainView = currentTab.getWebView();
1758 if (null == mainView) {
1759 result = false;
1760 break;
1761 }
1762 copy(mainView.getUrl());
1763 break;
1764 // -- Browser context menu
1765 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001766 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001767 case R.id.copy_link_context_menu_id:
1768 final WebView webView = getCurrentTopWebView();
1769 if (null == webView) {
1770 result = false;
1771 break;
1772 }
1773 final HashMap<String, WebView> hrefMap =
1774 new HashMap<String, WebView>();
1775 hrefMap.put("webview", webView);
1776 final Message msg = mHandler.obtainMessage(
1777 FOCUS_NODE_HREF, id, 0, hrefMap);
1778 webView.requestFocusNodeHref(msg);
1779 break;
1780
1781 default:
1782 // For other context menus
1783 result = onOptionsItemSelected(item);
1784 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001785 return result;
1786 }
1787
1788 /**
1789 * support programmatically opening the context menu
1790 */
1791 public void openContextMenu(View view) {
1792 mActivity.openContextMenu(view);
1793 }
1794
1795 /**
1796 * programmatically open the options menu
1797 */
1798 public void openOptionsMenu() {
1799 mActivity.openOptionsMenu();
1800 }
1801
1802 public boolean onMenuOpened(int featureId, Menu menu) {
1803 if (mOptionsMenuOpen) {
1804 if (mConfigChanged) {
1805 // We do not need to make any changes to the state of the
1806 // title bar, since the only thing that happened was a
1807 // change in orientation
1808 mConfigChanged = false;
1809 } else {
1810 if (!mExtendedMenuOpen) {
1811 mExtendedMenuOpen = true;
1812 mUi.onExtendedMenuOpened();
1813 } else {
1814 // Switching the menu back to icon view, so show the
1815 // title bar once again.
1816 mExtendedMenuOpen = false;
1817 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001818 }
1819 }
1820 } else {
1821 // The options menu is closed, so open it, and show the title
1822 mOptionsMenuOpen = true;
1823 mConfigChanged = false;
1824 mExtendedMenuOpen = false;
1825 mUi.onOptionsMenuOpened();
1826 }
1827 return true;
1828 }
1829
1830 public void onOptionsMenuClosed(Menu menu) {
1831 mOptionsMenuOpen = false;
1832 mUi.onOptionsMenuClosed(mInLoad);
1833 }
1834
1835 public void onContextMenuClosed(Menu menu) {
1836 mUi.onContextMenuClosed(menu, mInLoad);
1837 }
1838
1839 // Helper method for getting the top window.
1840 @Override
1841 public WebView getCurrentTopWebView() {
1842 return mTabControl.getCurrentTopWebView();
1843 }
1844
1845 @Override
1846 public WebView getCurrentWebView() {
1847 return mTabControl.getCurrentWebView();
1848 }
1849
1850 /*
1851 * This method is called as a result of the user selecting the options
1852 * menu to see the download window. It shows the download window on top of
1853 * the current window.
1854 */
1855 void viewDownloads() {
1856 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1857 mActivity.startActivity(intent);
1858 }
1859
1860 // action mode
1861
1862 void onActionModeStarted(ActionMode mode) {
1863 mUi.onActionModeStarted(mode);
1864 mActionMode = mode;
1865 }
1866
1867 /*
1868 * True if a custom ActionMode (i.e. find or select) is in use.
1869 */
1870 @Override
1871 public boolean isInCustomActionMode() {
1872 return mActionMode != null;
1873 }
1874
1875 /*
1876 * End the current ActionMode.
1877 */
1878 @Override
1879 public void endActionMode() {
1880 if (mActionMode != null) {
1881 mActionMode.finish();
1882 }
1883 }
1884
1885 /*
1886 * Called by find and select when they are finished. Replace title bars
1887 * as necessary.
1888 */
1889 public void onActionModeFinished(ActionMode mode) {
1890 if (!isInCustomActionMode()) return;
1891 mUi.onActionModeFinished(mInLoad);
1892 mActionMode = null;
1893 }
1894
1895 boolean isInLoad() {
1896 return mInLoad;
1897 }
1898
1899 // bookmark handling
1900
1901 /**
1902 * add the current page as a bookmark to the given folder id
1903 * @param folderId use -1 for the default folder
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001904 * @param canBeAnEdit If true, check to see whether the site is already
1905 * bookmarked, and if it is, edit that bookmark. If false, and
1906 * the site is already bookmarked, do not attempt to edit the
1907 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001908 */
1909 @Override
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001910 public void bookmarkCurrentPage(long folderId, boolean canBeAnEdit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001911 Intent i = new Intent(mActivity,
1912 AddBookmarkPage.class);
1913 WebView w = getCurrentTopWebView();
1914 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1915 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1916 String touchIconUrl = w.getTouchIconUrl();
1917 if (touchIconUrl != null) {
1918 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1919 WebSettings settings = w.getSettings();
1920 if (settings != null) {
1921 i.putExtra(AddBookmarkPage.USER_AGENT,
1922 settings.getUserAgentString());
1923 }
1924 }
1925 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1926 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1927 getDesiredThumbnailHeight(mActivity)));
1928 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
1929 i.putExtra(BrowserContract.Bookmarks.PARENT,
1930 folderId);
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001931 if (canBeAnEdit) {
1932 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1933 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001934 // Put the dialog at the upper right of the screen, covering the
1935 // star on the title bar.
1936 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
1937 mActivity.startActivity(i);
1938 }
1939
1940 // file chooser
1941 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1942 mUploadHandler = new UploadHandler(this);
1943 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1944 }
1945
1946 // thumbnails
1947
1948 /**
1949 * Return the desired width for thumbnail screenshots, which are stored in
1950 * the database, and used on the bookmarks screen.
1951 * @param context Context for finding out the density of the screen.
1952 * @return desired width for thumbnail screenshot.
1953 */
1954 static int getDesiredThumbnailWidth(Context context) {
1955 return context.getResources().getDimensionPixelOffset(
1956 R.dimen.bookmarkThumbnailWidth);
1957 }
1958
1959 /**
1960 * Return the desired height for thumbnail screenshots, which are stored in
1961 * the database, and used on the bookmarks screen.
1962 * @param context Context for finding out the density of the screen.
1963 * @return desired height for thumbnail screenshot.
1964 */
1965 static int getDesiredThumbnailHeight(Context context) {
1966 return context.getResources().getDimensionPixelOffset(
1967 R.dimen.bookmarkThumbnailHeight);
1968 }
1969
Michael Kolb1acef692011-03-08 14:12:06 -08001970 static Bitmap createScreenshot(Tab tab, int width, int height) {
1971 if ((tab != null) && (tab.getWebView() != null)) {
1972 return createScreenshot(tab.getWebView(), width, height);
1973 }
1974 return null;
1975 }
1976
Michael Kolb8233fac2010-10-26 16:08:53 -07001977 private static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08001978 // We render to a bitmap 2x the desired size so that we can then
1979 // re-scale it with filtering since canvas.scale doesn't filter
1980 // This helps reduce aliasing at the cost of being slightly blurry
1981 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07001982 Picture thumbnail = view.capturePicture();
1983 if (thumbnail == null) {
1984 return null;
1985 }
John Reck5c6ac2f2011-01-05 10:18:03 -08001986 width *= filter_scale;
1987 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07001988 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1989 Canvas canvas = new Canvas(bm);
1990 // May need to tweak these values to determine what is the
1991 // best scale factor
1992 int thumbnailWidth = thumbnail.getWidth();
1993 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08001994 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08001995 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08001996 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07001997 } else {
1998 return null;
1999 }
John Reckfe49ab42010-11-16 17:09:37 -08002000
Michael Kolbeb95db42011-03-03 10:38:40 -08002001 float scaleFactorY = (float) height / (float)thumbnailHeight;
2002 if (scaleFactorY > scaleFactor) {
2003 // The picture is narrower than the requested AR
2004 // Center the thumnail and crop the sides
2005 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08002006 float wx = (thumbnailWidth * scaleFactor) - width;
2007 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07002008 }
2009
John Reckfe49ab42010-11-16 17:09:37 -08002010 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07002011
2012 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08002013 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
2014 height / filter_scale, true);
2015 bm.recycle();
2016 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002017 }
2018
John Reck34ef2672011-02-10 11:30:55 -08002019 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002020 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002021 // FIXME: Would like to make sure there is actually something to
2022 // draw, but the API for that (WebViewCore.pictureReady()) is not
2023 // currently accessible here.
2024
John Reck34ef2672011-02-10 11:30:55 -08002025 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002026 if (view == null) {
2027 // Tab was destroyed
2028 return;
2029 }
John Reck34ef2672011-02-10 11:30:55 -08002030 final String url = tab.getUrl();
2031 final String originalUrl = view.getOriginalUrl();
2032
2033 if (TextUtils.isEmpty(url)) {
2034 return;
2035 }
2036
2037 // Only update thumbnails for web urls (http(s)://), not for
2038 // about:, javascript:, data:, etc...
2039 // Unless it is a bookmarked site, then always update
2040 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2041 return;
2042 }
2043
Michael Kolb8233fac2010-10-26 16:08:53 -07002044 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2045 getDesiredThumbnailHeight(mActivity));
2046 if (bm == null) {
2047 return;
2048 }
2049
2050 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002051 new AsyncTask<Void, Void, Void>() {
2052 @Override
2053 protected Void doInBackground(Void... unused) {
2054 Cursor cursor = null;
2055 try {
2056 // TODO: Clean this up
2057 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2058 if (cursor != null && cursor.moveToFirst()) {
2059 final ByteArrayOutputStream os =
2060 new ByteArrayOutputStream();
2061 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002062
John Reck34ef2672011-02-10 11:30:55 -08002063 ContentValues values = new ContentValues();
2064 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002065
John Reck34ef2672011-02-10 11:30:55 -08002066 do {
John Reck617fd832011-02-16 14:35:59 -08002067 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002068 cr.update(Images.CONTENT_URI, values, null, null);
2069 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002070 }
John Reck34ef2672011-02-10 11:30:55 -08002071 } catch (IllegalStateException e) {
2072 // Ignore
2073 } finally {
2074 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002075 }
John Reck34ef2672011-02-10 11:30:55 -08002076 return null;
2077 }
2078 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002079 }
2080
2081 private class Copy implements OnMenuItemClickListener {
2082 private CharSequence mText;
2083
2084 public boolean onMenuItemClick(MenuItem item) {
2085 copy(mText);
2086 return true;
2087 }
2088
2089 public Copy(CharSequence toCopy) {
2090 mText = toCopy;
2091 }
2092 }
2093
Leon Scroggins63c02662010-11-18 15:16:27 -05002094 private static class Download implements OnMenuItemClickListener {
2095 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002096 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002097 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002098
2099 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002100 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002101 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002102 return true;
2103 }
2104
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002105 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002106 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002107 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002108 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002109 }
2110 }
2111
Cary Clark8974d282010-11-22 10:46:05 -05002112 private static class SelectText implements OnMenuItemClickListener {
2113 private WebView mWebView;
2114
2115 public boolean onMenuItemClick(MenuItem item) {
2116 if (mWebView != null) {
2117 return mWebView.selectText();
2118 }
2119 return false;
2120 }
2121
2122 public SelectText(WebView webView) {
2123 mWebView = webView;
2124 }
2125
2126 }
2127
Michael Kolb8233fac2010-10-26 16:08:53 -07002128 /********************** TODO: UI stuff *****************************/
2129
2130 // these methods have been copied, they still need to be cleaned up
2131
2132 /****************** tabs ***************************************************/
2133
2134 // basic tab interactions:
2135
2136 // it is assumed that tabcontrol already knows about the tab
2137 protected void addTab(Tab tab) {
2138 mUi.addTab(tab);
2139 }
2140
2141 protected void removeTab(Tab tab) {
2142 mUi.removeTab(tab);
2143 mTabControl.removeTab(tab);
2144 }
2145
Michael Kolbf2055602011-04-09 17:20:03 -07002146 @Override
2147 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002148 // monkey protection against delayed start
2149 if (tab != null) {
2150 mTabControl.setCurrentTab(tab);
2151 // the tab is guaranteed to have a webview after setCurrentTab
2152 mUi.setActiveTab(tab);
2153 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002154 }
2155
2156 protected void closeEmptyChildTab() {
2157 Tab current = mTabControl.getCurrentTab();
2158 if (current != null
2159 && current.getWebView().copyBackForwardList().getSize() == 0) {
Michael Kolbc831b632011-05-11 09:30:34 -07002160 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002161 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002162 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002163 closeTab(current);
2164 }
2165 }
2166 }
2167
2168 protected void reuseTab(Tab appTab, String appId, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002169 // Dismiss the subwindow if applicable.
2170 dismissSubWindow(appTab);
2171 // Since we might kill the WebView, remove it from the
2172 // content view first.
2173 mUi.detachTab(appTab);
2174 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002175 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002176 // TODO: analyze why the remove and add are necessary
2177 mUi.attachTab(appTab);
2178 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002179 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002180 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002181 } else {
2182 // If the tab was the current tab, we have to attach
2183 // it to the view system again.
2184 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002185 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002186 }
2187 }
2188
2189 // Remove the sub window if it exists. Also called by TabControl when the
2190 // user clicks the 'X' to dismiss a sub window.
2191 public void dismissSubWindow(Tab tab) {
2192 removeSubWindow(tab);
2193 // dismiss the subwindow. This will destroy the WebView.
2194 tab.dismissSubWindow();
2195 getCurrentTopWebView().requestFocus();
2196 }
2197
2198 @Override
2199 public void removeSubWindow(Tab t) {
2200 if (t.getSubWebView() != null) {
2201 mUi.removeSubWindow(t.getSubViewContainer());
2202 }
2203 }
2204
2205 @Override
2206 public void attachSubWindow(Tab tab) {
2207 if (tab.getSubWebView() != null) {
2208 mUi.attachSubWindow(tab.getSubViewContainer());
2209 getCurrentTopWebView().requestFocus();
2210 }
2211 }
2212
Michael Kolb7bcafde2011-05-09 13:55:59 -07002213 // open a non inconito tab with the given url data
2214 // and set as active tab
2215 public Tab openTab(UrlData urlData) {
2216 Tab tab = createNewTab(false, true, true);
2217 if ((tab != null) && !urlData.isEmpty()) {
2218 loadUrlDataIn(tab, urlData);
2219 }
2220 return tab;
2221 }
2222
Michael Kolb843510f2010-12-09 10:51:49 -08002223 @Override
2224 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002225 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002226 }
2227
Michael Kolb8233fac2010-10-26 16:08:53 -07002228 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002229 public Tab openIncognitoTab() {
2230 return openTab(INCOGNITO_URI, true, true, false);
2231 }
2232
2233 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002234 public Tab openTab(String url, boolean incognito, boolean setActive,
2235 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002236 return openTab(url, incognito, setActive, useCurrent, null);
2237 }
2238
2239 @Override
2240 public Tab openTab(String url, Tab parent, boolean setActive,
2241 boolean useCurrent) {
2242 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2243 setActive, useCurrent, parent);
2244 }
2245
2246 public Tab openTab(String url, boolean incognito, boolean setActive,
2247 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002248 Tab tab = createNewTab(incognito, setActive, useCurrent);
2249 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002250 if (parent != null && parent != tab) {
2251 parent.addChildTab(tab);
2252 }
Michael Kolba4261fd2011-05-05 11:27:37 -07002253 WebView w = tab.getWebView();
Michael Kolb519d2282011-05-09 17:03:19 -07002254 if (url != null) {
2255 loadUrl(w, url);
2256 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002257 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002258 return tab;
2259 }
2260
2261 // this method will attempt to create a new tab
2262 // incognito: private browsing tab
2263 // setActive: ste tab as current tab
2264 // useCurrent: if no new tab can be created, return current tab
2265 private Tab createNewTab(boolean incognito, boolean setActive,
2266 boolean useCurrent) {
2267 Tab tab = null;
2268 if (mTabControl.canCreateNewTab()) {
2269 tab = mTabControl.createNewTab(incognito);
2270 addTab(tab);
2271 if (setActive) {
2272 setActiveTab(tab);
2273 }
2274 } else {
2275 if (useCurrent) {
2276 tab = mTabControl.getCurrentTab();
2277 // Get rid of the subwindow if it exists
2278 dismissSubWindow(tab);
2279 } else {
2280 mUi.showMaxTabsWarning();
2281 }
2282 }
2283 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002284 }
2285
2286 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002287 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002288 * @return boolean True if we successfully switched to a different tab. If
2289 * the indexth tab is null, or if that tab is the same as
2290 * the current one, return false.
2291 */
2292 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002293 public boolean switchToTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002294 // hide combo view if open
2295 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002296 Tab currentTab = mTabControl.getCurrentTab();
2297 if (tab == null || tab == currentTab) {
2298 return false;
2299 }
2300 setActiveTab(tab);
2301 return true;
2302 }
2303
2304 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002305 public void closeCurrentTab() {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002306 // hide combo view if open
2307 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002308 if (mTabControl.getTabCount() == 1) {
John Reck958b2422010-12-03 17:56:17 -08002309 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002310 return;
2311 }
Michael Kolbc831b632011-05-11 09:30:34 -07002312 final Tab current = mTabControl.getCurrentTab();
2313 final int pos = mTabControl.getCurrentPosition();
2314 Tab newTab = current.getParent();
2315 if (newTab == null) {
2316 newTab = mTabControl.getTab(pos + 1);
2317 if (newTab == null) {
2318 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002319 }
2320 }
Michael Kolbc831b632011-05-11 09:30:34 -07002321 if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002322 // Close window
2323 closeTab(current);
2324 }
2325 }
2326
2327 /**
2328 * Close the tab, remove its associated title bar, and adjust mTabControl's
2329 * current tab to a valid value.
2330 */
2331 @Override
2332 public void closeTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002333 // hide combo view if open
2334 removeComboView();
Michael Kolb2d59c322011-01-25 13:18:55 -08002335 removeTab(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002336 }
2337
2338 /**************** TODO: Url loading clean up *******************************/
2339
2340 // Called when loading from context menu or LOAD_URL message
2341 protected void loadUrlFromContext(WebView view, String url) {
2342 // In case the user enters nothing.
2343 if (url != null && url.length() != 0 && view != null) {
2344 url = UrlUtils.smartUrlFilter(url);
2345 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
2346 loadUrl(view, url);
2347 }
2348 }
2349 }
2350
2351 /**
2352 * Load the URL into the given WebView and update the title bar
2353 * to reflect the new load. Call this instead of WebView.loadUrl
2354 * directly.
2355 * @param view The WebView used to load url.
2356 * @param url The URL to load.
2357 */
2358 protected void loadUrl(WebView view, String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002359 view.loadUrl(url);
2360 }
2361
2362 /**
2363 * Load UrlData into a Tab and update the title bar to reflect the new
2364 * load. Call this instead of UrlData.loadIn directly.
2365 * @param t The Tab used to load.
2366 * @param data The UrlData being loaded.
2367 */
2368 protected void loadUrlDataIn(Tab t, UrlData data) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002369 data.loadIn(t);
2370 }
2371
John Reck30c714c2010-12-16 17:30:34 -08002372 @Override
2373 public void onUserCanceledSsl(Tab tab) {
2374 WebView web = tab.getWebView();
2375 // TODO: Figure out the "right" behavior
2376 if (web.canGoBack()) {
2377 web.goBack();
2378 } else {
2379 web.loadUrl(mSettings.getHomePage());
2380 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002381 }
2382
2383 void goBackOnePageOrQuit() {
2384 Tab current = mTabControl.getCurrentTab();
2385 if (current == null) {
2386 /*
2387 * Instead of finishing the activity, simply push this to the back
2388 * of the stack and let ActivityManager to choose the foreground
2389 * activity. As BrowserActivity is singleTask, it will be always the
2390 * root of the task. So we can use either true or false for
2391 * moveTaskToBack().
2392 */
2393 mActivity.moveTaskToBack(true);
2394 return;
2395 }
2396 WebView w = current.getWebView();
2397 if (w.canGoBack()) {
2398 w.goBack();
2399 } else {
2400 // Check to see if we are closing a window that was created by
2401 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002402 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002403 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002404 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002405 // Now we close the other tab
2406 closeTab(current);
2407 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002408 /*
2409 * Instead of finishing the activity, simply push this to the back
2410 * of the stack and let ActivityManager to choose the foreground
2411 * activity. As BrowserActivity is singleTask, it will be always the
2412 * root of the task. So we can use either true or false for
2413 * moveTaskToBack().
2414 */
2415 mActivity.moveTaskToBack(true);
2416 }
2417 }
2418 }
2419
2420 /**
2421 * Feed the previously stored results strings to the BrowserProvider so that
2422 * the SearchDialog will show them instead of the standard searches.
2423 * @param result String to show on the editable line of the SearchDialog.
2424 */
2425 @Override
2426 public void showVoiceSearchResults(String result) {
2427 ContentProviderClient client = mActivity.getContentResolver()
2428 .acquireContentProviderClient(Browser.BOOKMARKS_URI);
2429 ContentProvider prov = client.getLocalContentProvider();
2430 BrowserProvider bp = (BrowserProvider) prov;
2431 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
2432 client.release();
2433
2434 Bundle bundle = createGoogleSearchSourceBundle(
2435 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
2436 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
2437 startSearch(result, false, bundle, false);
2438 }
2439
2440 private void startSearch(String initialQuery, boolean selectInitialQuery,
2441 Bundle appSearchData, boolean globalSearch) {
2442 if (appSearchData == null) {
2443 appSearchData = createGoogleSearchSourceBundle(
2444 GOOGLE_SEARCH_SOURCE_TYPE);
2445 }
2446
2447 SearchEngine searchEngine = mSettings.getSearchEngine();
2448 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
2449 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
2450 }
2451 mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData,
2452 globalSearch);
2453 }
2454
2455 private Bundle createGoogleSearchSourceBundle(String source) {
2456 Bundle bundle = new Bundle();
2457 bundle.putString(Search.SOURCE, source);
2458 return bundle;
2459 }
2460
2461 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002462 * helper method for key handler
2463 * returns the current tab if it can't advance
2464 */
Michael Kolbc831b632011-05-11 09:30:34 -07002465 private Tab getNextTab() {
2466 return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1,
2467 mTabControl.getCurrentPosition() + 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002468 }
2469
2470 /**
2471 * helper method for key handler
2472 * returns the current tab if it can't advance
2473 */
Michael Kolbc831b632011-05-11 09:30:34 -07002474 private Tab getPrevTab() {
2475 return mTabControl.getTab(Math.max(0,
2476 mTabControl.getCurrentPosition() - 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002477 }
2478
2479 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002480 * handle key events in browser
2481 *
2482 * @param keyCode
2483 * @param event
2484 * @return true if handled, false to pass to super
2485 */
2486 boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002487 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002488 // Even if MENU is already held down, we need to call to super to open
2489 // the IME on long press.
Michael Kolb2814a362011-05-19 15:49:41 -07002490 if (KeyEvent.KEYCODE_MENU == keyCode) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002491 if (mOptionsMenuHandler != null) {
2492 return false;
2493 } else {
2494 event.startTracking();
2495 return true;
2496 }
Michael Kolb2814a362011-05-19 15:49:41 -07002497 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002498 if (!noModifiers
2499 && ((KeyEvent.KEYCODE_MENU == keyCode)
2500 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2501 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002502 mMenuIsDown = true;
2503 return false;
2504 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002505
Cary Clark8ff8c662010-12-29 15:03:05 -05002506 WebView webView = getCurrentTopWebView();
2507 if (webView == null) return false;
2508
Cary Clark160bbb92011-01-10 11:17:07 -05002509 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2510 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002511
Michael Kolb8233fac2010-10-26 16:08:53 -07002512 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002513 case KeyEvent.KEYCODE_TAB:
2514 if (event.isCtrlPressed()) {
2515 if (event.isShiftPressed()) {
2516 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002517 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002518 } else {
2519 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002520 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002521 }
2522 return true;
2523 }
2524 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002525 case KeyEvent.KEYCODE_SPACE:
2526 // WebView/WebTextView handle the keys in the KeyDown. As
2527 // the Activity's shortcut keys are only handled when WebView
2528 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002529 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002530 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002531 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002532 pageDown();
2533 }
2534 return true;
2535 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002536 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002537 event.startTracking();
2538 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002539 case KeyEvent.KEYCODE_DPAD_LEFT:
2540 if (ctrl) {
2541 webView.goBack();
2542 return true;
2543 }
2544 break;
2545 case KeyEvent.KEYCODE_DPAD_RIGHT:
2546 if (ctrl) {
2547 webView.goForward();
2548 return true;
2549 }
2550 break;
2551 case KeyEvent.KEYCODE_A:
2552 if (ctrl) {
2553 webView.selectAll();
2554 return true;
2555 }
2556 break;
Michael Kolba4183062011-01-16 10:43:21 -08002557// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002558 case KeyEvent.KEYCODE_C:
2559 if (ctrl) {
2560 webView.copySelection();
2561 return true;
2562 }
2563 break;
Michael Kolba4183062011-01-16 10:43:21 -08002564// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002565// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002566// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002567// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002568// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002569// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002570// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002571// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002572// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002573// case KeyEvent.KEYCODE_M: // unused
2574// case KeyEvent.KEYCODE_N: // in Chrome: new window
2575// case KeyEvent.KEYCODE_O: // in Chrome: open file
2576// case KeyEvent.KEYCODE_P: // in Chrome: print page
2577// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002578// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002579// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2580 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002581 // we can't use the ctrl/shift flags, they check for
2582 // exclusive use of a modifier
2583 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002584 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002585 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002586 } else {
2587 openTabToHomePage();
2588 }
2589 return true;
2590 }
2591 break;
2592// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2593// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb1a2eba42011-03-16 16:42:49 -07002594 case KeyEvent.KEYCODE_W: // in Chrome: close tab
2595 if (ctrl) {
2596 closeCurrentTab();
2597 return true;
2598 }
2599 break;
Cary Clark8ff8c662010-12-29 15:03:05 -05002600// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2601// case KeyEvent.KEYCODE_Y: // unused
2602// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002603 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002604 // it is a regular key and webview is not null
2605 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002606 }
2607
John Recke6bf4ab2011-02-24 15:48:05 -08002608 boolean onKeyLongPress(int keyCode, KeyEvent event) {
2609 switch(keyCode) {
2610 case KeyEvent.KEYCODE_BACK:
2611 if (mUi.showsWeb()) {
2612 bookmarksOrHistoryPicker(true);
2613 return true;
2614 }
2615 break;
2616 }
2617 return false;
2618 }
2619
Michael Kolb8233fac2010-10-26 16:08:53 -07002620 boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb2814a362011-05-19 15:49:41 -07002621 if (KeyEvent.KEYCODE_MENU == keyCode) {
2622 mMenuIsDown = false;
2623 if (event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002624 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002625 }
2626 }
Cary Clark160bbb92011-01-10 11:17:07 -05002627 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002628 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002629 case KeyEvent.KEYCODE_BACK:
2630 if (event.isTracking() && !event.isCanceled()) {
2631 onBackKey();
2632 return true;
2633 }
2634 break;
2635 }
2636 return false;
2637 }
2638
2639 public boolean isMenuDown() {
2640 return mMenuIsDown;
2641 }
2642
Ben Murdoch8029a772010-11-16 11:58:21 +00002643 public void setupAutoFill(Message message) {
2644 // Open the settings activity at the AutoFill profile fragment so that
2645 // the user can create a new profile. When they return, we will dispatch
2646 // the message so that we can autofill the form using their new profile.
2647 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2648 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2649 AutoFillSettingsFragment.class.getName());
2650 mAutoFillSetupMessage = message;
2651 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2652 }
John Reckb3417f02011-01-14 11:01:05 -08002653
2654 @Override
2655 public void registerOptionsMenuHandler(OptionsMenuHandler handler) {
2656 mOptionsMenuHandler = handler;
2657 }
2658
2659 @Override
2660 public void unregisterOptionsMenuHandler(OptionsMenuHandler handler) {
2661 if (mOptionsMenuHandler == handler) {
2662 mOptionsMenuHandler = null;
2663 }
2664 }
2665
Narayan Kamath5119edd2011-02-23 15:49:17 +00002666 @Override
2667 public void registerDropdownChangeListener(DropdownChangeListener d) {
2668 mUi.registerDropdownChangeListener(d);
2669 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002670}