blob: 5fb413bb6653ebad451f12476f041ba172a55140 [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 Reck378a4102011-06-09 16:23:01 -0700229 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(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 doStart(icicle, intent);
266 } else {
267 mCrashRecoveryHandler.startRecovery(intent);
268 }
269 }
270
271 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700272 // Unless the last browser usage was within 24 hours, destroy any
273 // remaining incognito tabs.
274
275 Calendar lastActiveDate = icicle != null ?
276 (Calendar) icicle.getSerializable("lastActiveDate") : null;
277 Calendar today = Calendar.getInstance();
278 Calendar yesterday = Calendar.getInstance();
279 yesterday.add(Calendar.DATE, -1);
280
Patrick Scott7d50a932011-02-04 09:27:26 -0500281 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700282 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800283 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700284
Patrick Scott7d50a932011-02-04 09:27:26 -0500285 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700286 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500287 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000288
Michael Kolbc831b632011-05-11 09:30:34 -0700289 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500290 // Not able to restore so we go ahead and clear session cookies. We
291 // must do this before trying to login the user as we don't want to
292 // clear any session cookies set during login.
293 CookieManager.getInstance().removeSessionCookie();
294 }
295
Patrick Scottd43e75a2011-03-14 14:47:23 -0400296 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500297 new Runnable() {
298 @Override public void run() {
Michael Kolbc831b632011-05-11 09:30:34 -0700299 onPreloginFinished(icicle, intent, currentTabId, restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500300 }
301 });
302 }
303
Michael Kolbc831b632011-05-11 09:30:34 -0700304 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500305 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700306 if (currentTabId == -1) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700307 final Bundle extra = intent.getExtras();
308 // Create an initial tab.
309 // If the intent is ACTION_VIEW and data is not null, the Browser is
310 // invoked to view the content by another application. In this case,
311 // the tab will be close when exit.
312 UrlData urlData = mIntentHandler.getUrlDataFromIntent(intent);
Michael Kolb7bcafde2011-05-09 13:55:59 -0700313 Tab t = null;
314 if (urlData.isEmpty()) {
315 t = openTabToHomePage();
316 } else {
317 t = openTab(urlData);
318 }
319 if (t != null) {
320 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
321 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700322 WebView webView = t.getWebView();
323 if (extra != null) {
324 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
325 if (scale > 0 && scale <= 1000) {
326 webView.setInitialScale(scale);
327 }
328 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700329 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700330 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500331 mUi.needsRestoreAllTabs());
Michael Kolb1bf23132010-11-19 12:55:12 -0800332 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700333 // TabControl.restoreState() will create a new tab even if
334 // restoring the state fails.
335 setActiveTab(mTabControl.getCurrentTab());
336 }
337 // clear up the thumbnail directory, which is no longer used;
338 // ideally this should only be run once after an upgrade from
339 // a previous version of the browser
340 new ClearThumbnails().execute(mTabControl.getThumbnailDir()
341 .listFiles());
342 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700343 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700344 if (jsFlags.trim().length() != 0) {
345 getCurrentWebView().setJsFlags(jsFlags);
346 }
John Reck439c9a52010-12-14 10:04:39 -0800347 if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
348 bookmarksOrHistoryPicker(false);
349 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700350 }
351
352 void setWebViewFactory(WebViewFactory factory) {
353 mFactory = factory;
354 }
355
Michael Kolb1514bb72010-11-22 09:11:48 -0800356 @Override
357 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 return mFactory;
359 }
360
361 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800362 public void onSetWebView(Tab tab, WebView view) {
363 mUi.onSetWebView(tab, view);
364 }
365
366 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800367 public void createSubWindow(Tab tab) {
368 endActionMode();
369 WebView mainView = tab.getWebView();
370 WebView subView = mFactory.createWebView((mainView == null)
371 ? false
372 : mainView.isPrivateBrowsingEnabled());
373 mUi.createSubWindow(tab, subView);
374 }
375
376 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700377 public Activity getActivity() {
378 return mActivity;
379 }
380
381 void setUi(UI ui) {
382 mUi = ui;
383 }
384
385 BrowserSettings getSettings() {
386 return mSettings;
387 }
388
389 IntentHandler getIntentHandler() {
390 return mIntentHandler;
391 }
392
393 @Override
394 public UI getUi() {
395 return mUi;
396 }
397
398 int getMaxTabs() {
399 return mActivity.getResources().getInteger(R.integer.max_tabs);
400 }
401
402 @Override
403 public TabControl getTabControl() {
404 return mTabControl;
405 }
406
Michael Kolb1bf23132010-11-19 12:55:12 -0800407 @Override
408 public List<Tab> getTabs() {
409 return mTabControl.getTabs();
410 }
411
Michael Kolb8233fac2010-10-26 16:08:53 -0700412 // Open the icon database and retain all the icons for visited sites.
Ben Murdoch9446b932010-11-25 16:20:14 +0000413 // This is done on a background thread so as not to stall startup.
Michael Kolb8233fac2010-10-26 16:08:53 -0700414 private void retainIconsOnStartup() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000415 // WebIconDatabase needs to be retrieved on the UI thread so that if
416 // it has not been created successfully yet the Handler is started on the
417 // UI thread.
418 new RetainIconsOnStartupTask(WebIconDatabase.getInstance()).execute();
419 }
420
421 private class RetainIconsOnStartupTask extends AsyncTask<Void, Void, Void> {
422 private WebIconDatabase mDb;
423
424 public RetainIconsOnStartupTask(WebIconDatabase db) {
425 mDb = db;
426 }
427
John Recka00cbbd2010-12-16 12:38:19 -0800428 @Override
Ben Murdoch9446b932010-11-25 16:20:14 +0000429 protected Void doInBackground(Void... unused) {
430 mDb.open(mActivity.getDir("icons", 0).getPath());
431 Cursor c = null;
432 try {
433 c = Browser.getAllBookmarks(mActivity.getContentResolver());
434 if (c.moveToFirst()) {
435 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
436 do {
437 String url = c.getString(urlIndex);
438 mDb.retainIconForPageUrl(url);
439 } while (c.moveToNext());
440 }
441 } catch (IllegalStateException e) {
442 Log.e(LOGTAG, "retainIconsOnStartup", e);
443 } finally {
444 if (c != null) c.close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700445 }
Ben Murdoch9446b932010-11-25 16:20:14 +0000446
447 return null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700448 }
449 }
450
451 private void startHandler() {
452 mHandler = new Handler() {
453
454 @Override
455 public void handleMessage(Message msg) {
456 switch (msg.what) {
457 case OPEN_BOOKMARKS:
458 bookmarksOrHistoryPicker(false);
459 break;
460 case FOCUS_NODE_HREF:
461 {
462 String url = (String) msg.getData().get("url");
463 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500464 String src = (String) msg.getData().get("src");
465 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700466 if (TextUtils.isEmpty(url)) {
467 break;
468 }
469 HashMap focusNodeMap = (HashMap) msg.obj;
470 WebView view = (WebView) focusNodeMap.get("webview");
471 // Only apply the action if the top window did not change.
472 if (getCurrentTopWebView() != view) {
473 break;
474 }
475 switch (msg.arg1) {
476 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -0700477 loadUrlFromContext(getCurrentTopWebView(), url);
478 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500479 case R.id.view_image_context_menu_id:
480 loadUrlFromContext(getCurrentTopWebView(), src);
481 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500482 case R.id.open_newtab_context_menu_id:
483 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700484 openTab(url, parent,
485 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500486 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700487 case R.id.copy_link_context_menu_id:
488 copy(url);
489 break;
490 case R.id.save_link_context_menu_id:
491 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500492 DownloadHandler.onDownloadStartNoStream(
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000493 mActivity, url, null, null, null,
494 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700495 break;
496 }
497 break;
498 }
499
500 case LOAD_URL:
501 loadUrlFromContext(getCurrentTopWebView(), (String) msg.obj);
502 break;
503
504 case STOP_LOAD:
505 stopLoading();
506 break;
507
508 case RELEASE_WAKELOCK:
509 if (mWakeLock.isHeld()) {
510 mWakeLock.release();
511 // if we reach here, Browser should be still in the
512 // background loading after WAKELOCK_TIMEOUT (5-min).
513 // To avoid burning the battery, stop loading.
514 mTabControl.stopAllLoading();
515 }
516 break;
517
518 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800519 Tab tab = (Tab) msg.obj;
520 if (tab != null) {
521 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700522 }
523 break;
524 }
525 }
526 };
527
528 }
529
Michael Kolbba99c5d2010-11-29 14:57:41 -0800530 @Override
531 public void shareCurrentPage() {
532 shareCurrentPage(mTabControl.getCurrentTab());
533 }
534
535 private void shareCurrentPage(Tab tab) {
536 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800537 sharePage(mActivity, tab.getTitle(),
538 tab.getUrl(), tab.getFavicon(),
539 createScreenshot(tab.getWebView(),
540 getDesiredThumbnailWidth(mActivity),
541 getDesiredThumbnailHeight(mActivity)));
542 }
543 }
544
Michael Kolb8233fac2010-10-26 16:08:53 -0700545 /**
546 * Share a page, providing the title, url, favicon, and a screenshot. Uses
547 * an {@link Intent} to launch the Activity chooser.
548 * @param c Context used to launch a new Activity.
549 * @param title Title of the page. Stored in the Intent with
550 * {@link Intent#EXTRA_SUBJECT}
551 * @param url URL of the page. Stored in the Intent with
552 * {@link Intent#EXTRA_TEXT}
553 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
554 * with {@link Browser#EXTRA_SHARE_FAVICON}
555 * @param screenshot Bitmap of a screenshot of the page. Stored in the
556 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
557 */
558 static final void sharePage(Context c, String title, String url,
559 Bitmap favicon, Bitmap screenshot) {
560 Intent send = new Intent(Intent.ACTION_SEND);
561 send.setType("text/plain");
562 send.putExtra(Intent.EXTRA_TEXT, url);
563 send.putExtra(Intent.EXTRA_SUBJECT, title);
564 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
565 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
566 try {
567 c.startActivity(Intent.createChooser(send, c.getString(
568 R.string.choosertitle_sharevia)));
569 } catch(android.content.ActivityNotFoundException ex) {
570 // if no app handles it, do nothing
571 }
572 }
573
574 private void copy(CharSequence text) {
575 ClipboardManager cm = (ClipboardManager) mActivity
576 .getSystemService(Context.CLIPBOARD_SERVICE);
577 cm.setText(text);
578 }
579
580 // lifecycle
581
582 protected void onConfgurationChanged(Configuration config) {
583 mConfigChanged = true;
584 if (mPageDialogsHandler != null) {
585 mPageDialogsHandler.onConfigurationChanged(config);
586 }
587 mUi.onConfigurationChanged(config);
588 }
589
590 @Override
591 public void handleNewIntent(Intent intent) {
592 mIntentHandler.onNewIntent(intent);
593 }
594
595 protected void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800596 if (mUi.isCustomViewShowing()) {
597 hideCustomView();
598 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700599 if (mActivityPaused) {
600 Log.e(LOGTAG, "BrowserActivity is already paused.");
601 return;
602 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700603 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800604 Tab tab = mTabControl.getCurrentTab();
605 if (tab != null) {
606 tab.pause();
607 if (!pauseWebViewTimers(tab)) {
608 mWakeLock.acquire();
609 mHandler.sendMessageDelayed(mHandler
610 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
611 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700612 }
613 mUi.onPause();
614 mNetworkHandler.onPause();
615
616 WebView.disablePlatformNotifications();
John Reck378a4102011-06-09 16:23:01 -0700617 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -0700618 }
619
John Reckaed9c542011-05-27 16:08:53 -0700620 void onSaveInstanceState(Bundle outState, boolean saveImages) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 // the default implementation requires each view to have an id. As the
622 // browser handles the state itself and it doesn't use id for the views,
623 // don't call the default implementation. Otherwise it will trigger the
624 // warning like this, "couldn't save which view has focus because the
625 // focused view XXX has no id".
626
627 // Save all the tabs
John Reckaed9c542011-05-27 16:08:53 -0700628 mTabControl.saveState(outState, saveImages);
Michael Kolb8233fac2010-10-26 16:08:53 -0700629 // Save time so that we know how old incognito tabs (if any) are.
630 outState.putSerializable("lastActiveDate", Calendar.getInstance());
631 }
632
633 void onResume() {
634 if (!mActivityPaused) {
635 Log.e(LOGTAG, "BrowserActivity is already resumed.");
636 return;
637 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700638 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800639 Tab current = mTabControl.getCurrentTab();
640 if (current != null) {
641 current.resume();
642 resumeWebViewTimers(current);
643 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700644 if (mWakeLock.isHeld()) {
645 mHandler.removeMessages(RELEASE_WAKELOCK);
646 mWakeLock.release();
647 }
648 mUi.onResume();
649 mNetworkHandler.onResume();
650 WebView.enablePlatformNotifications();
651 }
652
Michael Kolb70976932010-11-30 11:34:01 -0800653 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800654 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800655 * @param tab guaranteed non-null
656 */
657 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700658 boolean inLoad = tab.inPageLoad();
659 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
660 CookieSyncManager.getInstance().startSync();
661 WebView w = tab.getWebView();
662 if (w != null) {
663 w.resumeTimers();
664 }
665 }
666 }
667
Michael Kolb70976932010-11-30 11:34:01 -0800668 /**
669 * Pause all WebView timers using the WebView of the given tab
670 * @param tab
671 * @return true if the timers are paused or tab is null
672 */
673 private boolean pauseWebViewTimers(Tab tab) {
674 if (tab == null) {
675 return true;
676 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700677 CookieSyncManager.getInstance().stopSync();
678 WebView w = getCurrentWebView();
679 if (w != null) {
680 w.pauseTimers();
681 }
682 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700683 }
Michael Kolb70976932010-11-30 11:34:01 -0800684 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700685 }
686
687 void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800688 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700689 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
690 mUploadHandler = null;
691 }
692 if (mTabControl == null) return;
693 mUi.onDestroy();
694 // Remove the current tab and sub window
695 Tab t = mTabControl.getCurrentTab();
696 if (t != null) {
697 dismissSubWindow(t);
698 removeTab(t);
699 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500700 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700701 // Destroy all the tabs
702 mTabControl.destroy();
703 WebIconDatabase.getInstance().close();
704 // Stop watching the default geolocation permissions
705 mSystemAllowGeolocationOrigins.stop();
706 mSystemAllowGeolocationOrigins = null;
707 }
708
709 protected boolean isActivityPaused() {
710 return mActivityPaused;
711 }
712
713 protected void onLowMemory() {
714 mTabControl.freeMemory();
715 }
716
717 @Override
718 public boolean shouldShowErrorConsole() {
719 return mShouldShowErrorConsole;
720 }
721
722 protected void setShouldShowErrorConsole(boolean show) {
723 if (show == mShouldShowErrorConsole) {
724 // Nothing to do.
725 return;
726 }
727 mShouldShowErrorConsole = show;
728 Tab t = mTabControl.getCurrentTab();
729 if (t == null) {
730 // There is no current tab so we cannot toggle the error console
731 return;
732 }
733 mUi.setShouldShowErrorConsole(t, show);
734 }
735
736 @Override
737 public void stopLoading() {
738 mLoadStopped = true;
739 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700740 WebView w = getCurrentTopWebView();
741 w.stopLoading();
Michael Kolb8233fac2010-10-26 16:08:53 -0700742 mUi.onPageStopped(tab);
743 }
744
745 boolean didUserStopLoading() {
746 return mLoadStopped;
747 }
748
749 // WebViewController
750
751 @Override
John Reck324d4402011-01-11 16:56:42 -0800752 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700753
754 // We've started to load a new page. If there was a pending message
755 // to save a screenshot then we will now take the new page and save
756 // an incorrect screenshot. Therefore, remove any pending thumbnail
757 // messages from the queue.
758 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800759 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700760
761 // reset sync timer to avoid sync starts during loading a page
762 CookieSyncManager.getInstance().resetSync();
763
764 if (!mNetworkHandler.isNetworkUp()) {
765 view.setNetworkAvailable(false);
766 }
767
768 // when BrowserActivity just starts, onPageStarted may be called before
769 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
770 // to start the timer. As we won't switch tabs while an activity is in
771 // pause state, we can ensure calling resume and pause in pair.
772 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800773 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700774 }
775 mLoadStopped = false;
776 if (!mNetworkHandler.isNetworkUp()) {
777 mNetworkHandler.createAndShowNetworkDialog();
778 }
779 endActionMode();
780
John Reck30c714c2010-12-16 17:30:34 -0800781 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700782
John Reck324d4402011-01-11 16:56:42 -0800783 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700784 // update the bookmark database for favicon
785 maybeUpdateFavicon(tab, null, url, favicon);
786
787 Performance.tracePageStart(url);
788
789 // Performance probe
790 if (false) {
791 Performance.onPageStarted();
792 }
793
794 }
795
796 @Override
John Reck324d4402011-01-11 16:56:42 -0800797 public void onPageFinished(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800798 mUi.onTabDataChanged(tab);
John Reck324d4402011-01-11 16:56:42 -0800799 if (!tab.isPrivateBrowsingEnabled()
800 && !TextUtils.isEmpty(tab.getUrl())) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700801 if (tab.inForeground() && !didUserStopLoading()
802 || !tab.inForeground()) {
803 // Only update the bookmark screenshot if the user did not
804 // cancel the load early.
805 mHandler.sendMessageDelayed(mHandler.obtainMessage(
John Reck34ef2672011-02-10 11:30:55 -0800806 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Michael Kolb8233fac2010-10-26 16:08:53 -0700807 500);
808 }
809 }
810 // pause the WebView timer and release the wake lock if it is finished
811 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800812 if (mActivityPaused && pauseWebViewTimers(tab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700813 if (mWakeLock.isHeld()) {
814 mHandler.removeMessages(RELEASE_WAKELOCK);
815 mWakeLock.release();
816 }
817 }
818 // Performance probe
819 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800820 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700821 }
822
823 Performance.tracePageFinished();
824 }
825
826 @Override
John Reck30c714c2010-12-16 17:30:34 -0800827 public void onProgressChanged(Tab tab) {
828 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700829
830 if (newProgress == 100) {
831 CookieSyncManager.getInstance().sync();
832 // onProgressChanged() may continue to be called after the main
833 // frame has finished loading, as any remaining sub frames continue
834 // to load. We'll only get called once though with newProgress as
835 // 100 when everything is loaded. (onPageFinished is called once
836 // when the main frame completes loading regardless of the state of
837 // any sub frames so calls to onProgressChanges may continue after
838 // onPageFinished has executed)
839 if (mInLoad) {
840 mInLoad = false;
841 updateInLoadMenuItems(mCachedMenu);
842 }
843 } else {
844 if (!mInLoad) {
845 // onPageFinished may have already been called but a subframe is
846 // still loading and updating the progress. Reset mInLoad and
847 // update the menu items.
848 mInLoad = true;
849 updateInLoadMenuItems(mCachedMenu);
850 }
851 }
John Reck30c714c2010-12-16 17:30:34 -0800852 mUi.onProgressChanged(tab);
853 }
854
855 @Override
856 public void onUpdatedLockIcon(Tab tab) {
857 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700858 }
859
860 @Override
861 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800862 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800863 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800864 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700865 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
866 return;
867 }
868 // Update the title in the history database if not in private browsing mode
869 if (!tab.isPrivateBrowsingEnabled()) {
John Reck0ebd3ac2010-12-09 11:14:04 -0800870 mDataController.updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 }
872 }
873
874 @Override
875 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800876 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700877 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
878 }
879
880 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800881 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
882 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 }
884
885 @Override
886 public boolean shouldOverrideKeyEvent(KeyEvent event) {
887 if (mMenuIsDown) {
888 // only check shortcut key when MENU is held
889 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
890 event);
891 } else {
892 return false;
893 }
894 }
895
896 @Override
897 public void onUnhandledKeyEvent(KeyEvent event) {
898 if (!isActivityPaused()) {
899 if (event.getAction() == KeyEvent.ACTION_DOWN) {
900 mActivity.onKeyDown(event.getKeyCode(), event);
901 } else {
902 mActivity.onKeyUp(event.getKeyCode(), event);
903 }
904 }
905 }
906
907 @Override
John Reck324d4402011-01-11 16:56:42 -0800908 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700909 // Don't save anything in private browsing mode
910 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800911 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700912
John Reck324d4402011-01-11 16:56:42 -0800913 if (TextUtils.isEmpty(url)
914 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700915 return;
916 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800917 mDataController.updateVisitedHistory(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700918 WebIconDatabase.getInstance().retainIconForPageUrl(url);
John Reck847b5322011-04-14 17:02:18 -0700919 if (!mActivityPaused) {
920 // Since we clear the state in onPause, don't backup the current
921 // state if we are already paused
922 mCrashRecoveryHandler.backupState();
923 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700924 }
925
926 @Override
927 public void getVisitedHistory(final ValueCallback<String[]> callback) {
928 AsyncTask<Void, Void, String[]> task =
929 new AsyncTask<Void, Void, String[]>() {
930 @Override
931 public String[] doInBackground(Void... unused) {
932 return Browser.getVisitedHistory(mActivity.getContentResolver());
933 }
934 @Override
935 public void onPostExecute(String[] result) {
936 callback.onReceiveValue(result);
937 }
938 };
939 task.execute();
940 }
941
942 @Override
943 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
944 final HttpAuthHandler handler, final String host,
945 final String realm) {
946 String username = null;
947 String password = null;
948
949 boolean reuseHttpAuthUsernamePassword
950 = handler.useHttpAuthUsernamePassword();
951
952 if (reuseHttpAuthUsernamePassword && view != null) {
953 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
954 if (credentials != null && credentials.length == 2) {
955 username = credentials[0];
956 password = credentials[1];
957 }
958 }
959
960 if (username != null && password != null) {
961 handler.proceed(username, password);
962 } else {
963 if (tab.inForeground()) {
964 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
965 } else {
966 handler.cancel();
967 }
968 }
969 }
970
971 @Override
972 public void onDownloadStart(Tab tab, String url, String userAgent,
973 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000974 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -0500975 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000976 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
977 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700978 // This Tab was opened for the sole purpose of downloading a
979 // file. Remove it.
980 if (tab == mTabControl.getCurrentTab()) {
981 // In this case, the Tab is still on top.
982 goBackOnePageOrQuit();
983 } else {
984 // In this case, it is not.
985 closeTab(tab);
986 }
987 }
988 }
989
990 @Override
991 public Bitmap getDefaultVideoPoster() {
992 return mUi.getDefaultVideoPoster();
993 }
994
995 @Override
996 public View getVideoLoadingProgressView() {
997 return mUi.getVideoLoadingProgressView();
998 }
999
1000 @Override
1001 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1002 SslError error) {
1003 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1004 }
1005
Patrick Scott92066772011-03-10 08:46:27 -05001006 @Override
1007 public void showAutoLogin(Tab tab) {
1008 assert tab.inForeground();
1009 // Update the title bar to show the auto-login request.
1010 mUi.showAutoLogin(tab);
1011 }
1012
1013 @Override
1014 public void hideAutoLogin(Tab tab) {
1015 assert tab.inForeground();
1016 mUi.hideAutoLogin(tab);
1017 }
1018
Michael Kolb8233fac2010-10-26 16:08:53 -07001019 // helper method
1020
1021 /*
1022 * Update the favorites icon if the private browsing isn't enabled and the
1023 * icon is valid.
1024 */
1025 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1026 final String url, Bitmap favicon) {
1027 if (favicon == null) {
1028 return;
1029 }
1030 if (!tab.isPrivateBrowsingEnabled()) {
1031 Bookmarks.updateFavicon(mActivity
1032 .getContentResolver(), originalUrl, url, favicon);
1033 }
1034 }
1035
Leon Scroggins4cd97792010-12-03 15:31:56 -05001036 @Override
1037 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001038 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001039 mUi.bookmarkedStatusHasChanged(tab);
1040 }
1041
Michael Kolb8233fac2010-10-26 16:08:53 -07001042 // end WebViewController
1043
1044 protected void pageUp() {
1045 getCurrentTopWebView().pageUp(false);
1046 }
1047
1048 protected void pageDown() {
1049 getCurrentTopWebView().pageDown(false);
1050 }
1051
1052 // callback from phone title bar
1053 public void editUrl() {
1054 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001055 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001056 }
1057
Michael Kolbcfa3af52010-12-14 10:36:11 -08001058 public void startVoiceSearch() {
1059 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1060 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1061 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1062 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1063 mActivity.getComponentName().flattenToString());
1064 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001065 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001066 mActivity.startActivity(intent);
1067 }
1068
Michael Kolb11d19782011-03-20 10:17:40 -07001069 @Override
1070 public void activateVoiceSearchMode(String title, List<String> results) {
1071 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001072 }
1073
1074 public void revertVoiceSearchMode(Tab tab) {
1075 mUi.revertVoiceTitleBar(tab);
1076 }
1077
Michael Kolb736990c2011-03-20 10:01:20 -07001078 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001079 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001080 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1081 }
1082
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001083 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001084 WebChromeClient.CustomViewCallback callback) {
1085 if (tab.inForeground()) {
1086 if (mUi.isCustomViewShowing()) {
1087 callback.onCustomViewHidden();
1088 return;
1089 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001090 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001091 // Save the menu state and set it to empty while the custom
1092 // view is showing.
1093 mOldMenuState = mMenuState;
1094 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001095 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001096 }
1097 }
1098
1099 @Override
1100 public void hideCustomView() {
1101 if (mUi.isCustomViewShowing()) {
1102 mUi.onHideCustomView();
1103 // Reset the old menu state.
1104 mMenuState = mOldMenuState;
1105 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001106 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001107 }
1108 }
1109
1110 protected void onActivityResult(int requestCode, int resultCode,
1111 Intent intent) {
1112 if (getCurrentTopWebView() == null) return;
1113 switch (requestCode) {
1114 case PREFERENCES_PAGE:
1115 if (resultCode == Activity.RESULT_OK && intent != null) {
1116 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001117 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001118 mTabControl.removeParentChildRelationShips();
1119 }
1120 }
1121 break;
1122 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001123 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001124 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001125 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001126 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001127 case AUTOFILL_SETUP:
1128 // Determine whether a profile was actually set up or not
1129 // and if so, send the message back to the WebTextView to
1130 // fill the form with the new profile.
1131 if (getSettings().getAutoFillProfile() != null) {
1132 mAutoFillSetupMessage.sendToTarget();
1133 mAutoFillSetupMessage = null;
1134 }
1135 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001136 default:
1137 break;
1138 }
1139 getCurrentTopWebView().requestFocus();
1140 }
1141
1142 /**
1143 * Open the Go page.
1144 * @param startWithHistory If true, open starting on the history tab.
1145 * Otherwise, start with the bookmarks tab.
1146 */
1147 @Override
1148 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1149 if (mTabControl.getCurrentWebView() == null) {
1150 return;
1151 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001152 // clear action mode
1153 if (isInCustomActionMode()) {
1154 endActionMode();
1155 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001156 Bundle extras = new Bundle();
1157 // Disable opening in a new window if we have maxed out the windows
1158 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1159 !mTabControl.canCreateNewTab());
1160 mUi.showComboView(startWithHistory, extras);
1161 }
1162
1163 // combo view callbacks
1164
1165 /**
1166 * callback from ComboPage when clear history is requested
1167 */
1168 public void onRemoveParentChildRelationships() {
1169 mTabControl.removeParentChildRelationShips();
1170 }
1171
1172 /**
1173 * callback from ComboPage when bookmark/history selection
1174 */
1175 @Override
1176 public void onUrlSelected(String url, boolean newTab) {
1177 removeComboView();
1178 if (!TextUtils.isEmpty(url)) {
1179 if (newTab) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07001180 final Tab parent = mTabControl.getCurrentTab();
1181 openTab(url,
1182 (parent != null) && parent.isPrivateBrowsingEnabled(),
1183 !mSettings.openInBackground(),
1184 true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001185 } else {
1186 final Tab currentTab = mTabControl.getCurrentTab();
1187 dismissSubWindow(currentTab);
1188 loadUrl(getCurrentTopWebView(), url);
1189 }
1190 }
1191 }
1192
1193 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001194 * dismiss the ComboPage
1195 */
1196 @Override
1197 public void removeComboView() {
1198 mUi.hideComboView();
1199 }
1200
1201 // active tabs page handling
1202
1203 protected void showActiveTabsPage() {
1204 mMenuState = EMPTY_MENU;
1205 mUi.showActiveTabsPage();
1206 }
1207
1208 /**
1209 * Remove the active tabs page.
1210 * @param needToAttach If true, the active tabs page did not attach a tab
1211 * to the content view, so we need to do that here.
1212 */
1213 @Override
1214 public void removeActiveTabsPage(boolean needToAttach) {
1215 mMenuState = R.id.MAIN_MENU;
1216 mUi.removeActiveTabsPage();
1217 if (needToAttach) {
1218 setActiveTab(mTabControl.getCurrentTab());
1219 }
1220 getCurrentTopWebView().requestFocus();
1221 }
1222
1223 // key handling
1224 protected void onBackKey() {
1225 if (!mUi.onBackKey()) {
1226 WebView subwindow = mTabControl.getCurrentSubWindow();
1227 if (subwindow != null) {
1228 if (subwindow.canGoBack()) {
1229 subwindow.goBack();
1230 } else {
1231 dismissSubWindow(mTabControl.getCurrentTab());
1232 }
1233 } else {
1234 goBackOnePageOrQuit();
1235 }
1236 }
1237 }
1238
Michael Kolb4bd767d2011-05-27 11:33:55 -07001239 protected boolean onMenuKey() {
1240 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001241 }
1242
Michael Kolb8233fac2010-10-26 16:08:53 -07001243 // menu handling and state
1244 // TODO: maybe put into separate handler
1245
1246 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckb3417f02011-01-14 11:01:05 -08001247 if (mOptionsMenuHandler != null) {
1248 return mOptionsMenuHandler.onCreateOptionsMenu(menu);
1249 }
1250
John Reckd73c5a22010-12-22 10:22:50 -08001251 if (mMenuState == EMPTY_MENU) {
1252 return false;
1253 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001254 MenuInflater inflater = mActivity.getMenuInflater();
1255 inflater.inflate(R.menu.browser, menu);
1256 updateInLoadMenuItems(menu);
1257 // hold on to the menu reference here; it is used by the page callbacks
1258 // to update the menu based on loading state
1259 mCachedMenu = menu;
1260 return true;
1261 }
1262
1263 protected void onCreateContextMenu(ContextMenu menu, View v,
1264 ContextMenuInfo menuInfo) {
1265 if (v instanceof TitleBarBase) {
1266 return;
1267 }
1268 if (!(v instanceof WebView)) {
1269 return;
1270 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001271 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001272 WebView.HitTestResult result = webview.getHitTestResult();
1273 if (result == null) {
1274 return;
1275 }
1276
1277 int type = result.getType();
1278 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1279 Log.w(LOGTAG,
1280 "We should not show context menu when nothing is touched");
1281 return;
1282 }
1283 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1284 // let TextView handles context menu
1285 return;
1286 }
1287
1288 // Note, http://b/issue?id=1106666 is requesting that
1289 // an inflated menu can be used again. This is not available
1290 // yet, so inflate each time (yuk!)
1291 MenuInflater inflater = mActivity.getMenuInflater();
1292 inflater.inflate(R.menu.browsercontext, menu);
1293
1294 // Show the correct menu group
1295 final String extra = result.getExtra();
1296 menu.setGroupVisible(R.id.PHONE_MENU,
1297 type == WebView.HitTestResult.PHONE_TYPE);
1298 menu.setGroupVisible(R.id.EMAIL_MENU,
1299 type == WebView.HitTestResult.EMAIL_TYPE);
1300 menu.setGroupVisible(R.id.GEO_MENU,
1301 type == WebView.HitTestResult.GEO_TYPE);
1302 menu.setGroupVisible(R.id.IMAGE_MENU,
1303 type == WebView.HitTestResult.IMAGE_TYPE
1304 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1305 menu.setGroupVisible(R.id.ANCHOR_MENU,
1306 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1307 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001308 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1309 || type == WebView.HitTestResult.PHONE_TYPE
1310 || type == WebView.HitTestResult.EMAIL_TYPE
1311 || type == WebView.HitTestResult.GEO_TYPE;
1312 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1313 if (hitText) {
1314 menu.findItem(R.id.select_text_menu_id)
1315 .setOnMenuItemClickListener(new SelectText(webview));
1316 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001317 // Setup custom handling depending on the type
1318 switch (type) {
1319 case WebView.HitTestResult.PHONE_TYPE:
1320 menu.setHeaderTitle(Uri.decode(extra));
1321 menu.findItem(R.id.dial_context_menu_id).setIntent(
1322 new Intent(Intent.ACTION_VIEW, Uri
1323 .parse(WebView.SCHEME_TEL + extra)));
1324 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1325 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1326 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1327 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1328 addIntent);
1329 menu.findItem(R.id.copy_phone_context_menu_id)
1330 .setOnMenuItemClickListener(
1331 new Copy(extra));
1332 break;
1333
1334 case WebView.HitTestResult.EMAIL_TYPE:
1335 menu.setHeaderTitle(extra);
1336 menu.findItem(R.id.email_context_menu_id).setIntent(
1337 new Intent(Intent.ACTION_VIEW, Uri
1338 .parse(WebView.SCHEME_MAILTO + extra)));
1339 menu.findItem(R.id.copy_mail_context_menu_id)
1340 .setOnMenuItemClickListener(
1341 new Copy(extra));
1342 break;
1343
1344 case WebView.HitTestResult.GEO_TYPE:
1345 menu.setHeaderTitle(extra);
1346 menu.findItem(R.id.map_context_menu_id).setIntent(
1347 new Intent(Intent.ACTION_VIEW, Uri
1348 .parse(WebView.SCHEME_GEO
1349 + URLEncoder.encode(extra))));
1350 menu.findItem(R.id.copy_geo_context_menu_id)
1351 .setOnMenuItemClickListener(
1352 new Copy(extra));
1353 break;
1354
1355 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1356 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001357 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001358 // decide whether to show the open link in new tab option
1359 boolean showNewTab = mTabControl.canCreateNewTab();
1360 MenuItem newTabItem
1361 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001362 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001363 ? R.string.contextmenu_openlink_newwindow_background
1364 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001365 newTabItem.setVisible(showNewTab);
1366 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001367 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1368 newTabItem.setOnMenuItemClickListener(
1369 new MenuItem.OnMenuItemClickListener() {
1370 @Override
1371 public boolean onMenuItemClick(MenuItem item) {
1372 final HashMap<String, WebView> hrefMap =
1373 new HashMap<String, WebView>();
1374 hrefMap.put("webview", webview);
1375 final Message msg = mHandler.obtainMessage(
1376 FOCUS_NODE_HREF,
1377 R.id.open_newtab_context_menu_id,
1378 0, hrefMap);
1379 webview.requestFocusNodeHref(msg);
1380 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001381 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001382 });
1383 } else {
1384 newTabItem.setOnMenuItemClickListener(
1385 new MenuItem.OnMenuItemClickListener() {
1386 @Override
1387 public boolean onMenuItemClick(MenuItem item) {
1388 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001389 openTab(extra, parent,
1390 !mSettings.openInBackground(),
1391 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001392 return true;
1393 }
1394 });
1395 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001397 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1398 break;
1399 }
1400 // otherwise fall through to handle image part
1401 case WebView.HitTestResult.IMAGE_TYPE:
1402 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1403 menu.setHeaderTitle(extra);
1404 }
1405 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1406 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1407 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001408 setOnMenuItemClickListener(
1409 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001410 menu.findItem(R.id.set_wallpaper_context_menu_id).
1411 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1412 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001413 break;
1414
1415 default:
1416 Log.w(LOGTAG, "We should not get here.");
1417 break;
1418 }
1419 //update the ui
1420 mUi.onContextMenuCreated(menu);
1421 }
1422
1423 /**
1424 * As the menu can be open when loading state changes
1425 * we must manually update the state of the stop/reload menu
1426 * item
1427 */
1428 private void updateInLoadMenuItems(Menu menu) {
1429 if (menu == null) {
1430 return;
1431 }
1432 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1433 MenuItem src = mInLoad ?
1434 menu.findItem(R.id.stop_menu_id):
1435 menu.findItem(R.id.reload_menu_id);
1436 if (src != null) {
1437 dest.setIcon(src.getIcon());
1438 dest.setTitle(src.getTitle());
1439 }
1440 }
1441
John Reckb3417f02011-01-14 11:01:05 -08001442 boolean onPrepareOptionsMenu(Menu menu) {
1443 if (mOptionsMenuHandler != null) {
1444 return mOptionsMenuHandler.onPrepareOptionsMenu(menu);
1445 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001446 // Note: setVisible will decide whether an item is visible; while
1447 // setEnabled() will decide whether an item is enabled, which also means
1448 // whether the matching shortcut key will function.
1449 switch (mMenuState) {
1450 case EMPTY_MENU:
1451 if (mCurrentMenuState != mMenuState) {
1452 menu.setGroupVisible(R.id.MAIN_MENU, false);
1453 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1454 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1455 }
1456 break;
1457 default:
1458 if (mCurrentMenuState != mMenuState) {
1459 menu.setGroupVisible(R.id.MAIN_MENU, true);
1460 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1461 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1462 }
1463 final WebView w = getCurrentTopWebView();
1464 boolean canGoBack = false;
1465 boolean canGoForward = false;
1466 boolean isHome = false;
1467 if (w != null) {
1468 canGoBack = w.canGoBack();
1469 canGoForward = w.canGoForward();
1470 isHome = mSettings.getHomePage().equals(w.getUrl());
1471 }
1472 final MenuItem back = menu.findItem(R.id.back_menu_id);
1473 back.setEnabled(canGoBack);
1474
1475 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1476 home.setEnabled(!isHome);
1477
1478 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1479 forward.setEnabled(canGoForward);
1480
1481 // decide whether to show the share link option
1482 PackageManager pm = mActivity.getPackageManager();
1483 Intent send = new Intent(Intent.ACTION_SEND);
1484 send.setType("text/plain");
1485 ResolveInfo ri = pm.resolveActivity(send,
1486 PackageManager.MATCH_DEFAULT_ONLY);
1487 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1488
John Reck35e9dd62011-04-25 09:01:54 -07001489 boolean isNavDump = mSettings.enableNavDump();
Michael Kolb8233fac2010-10-26 16:08:53 -07001490 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1491 nav.setVisible(isNavDump);
1492 nav.setEnabled(isNavDump);
1493
John Reck35e9dd62011-04-25 09:01:54 -07001494 boolean showDebugSettings = mSettings.isDebugEnabled();
Michael Kolb8233fac2010-10-26 16:08:53 -07001495 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1496 counter.setVisible(showDebugSettings);
1497 counter.setEnabled(showDebugSettings);
1498
John Reckb3417f02011-01-14 11:01:05 -08001499 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
1500 newtab.setEnabled(getTabControl().canCreateNewTab());
Michael Kolb8233fac2010-10-26 16:08:53 -07001501
Leon Scroggins81983762011-02-11 15:17:36 -05001502 MenuItem archive = menu.findItem(R.id.save_webarchive_menu_id);
John Reck51d8bad2011-02-28 15:47:47 -08001503 Tab tab = getTabControl().getCurrentTab();
1504 String url = tab != null ? tab.getUrl() : null;
1505 archive.setVisible(!TextUtils.isEmpty(url)
1506 && !url.endsWith(".webarchivexml"));
Michael Kolb8233fac2010-10-26 16:08:53 -07001507 break;
1508 }
1509 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001510 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001511 }
1512
1513 public boolean onOptionsItemSelected(MenuItem item) {
John Reckb3417f02011-01-14 11:01:05 -08001514 if (mOptionsMenuHandler != null &&
1515 mOptionsMenuHandler.onOptionsItemSelected(item)) {
1516 return true;
1517 }
1518
Michael Kolb8233fac2010-10-26 16:08:53 -07001519 if (item.getGroupId() != R.id.CONTEXT_MENU) {
1520 // menu remains active, so ensure comboview is dismissed
1521 // if main menu option is selected
1522 removeComboView();
1523 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001524 if (null == getCurrentTopWebView()) {
1525 return false;
1526 }
1527 if (mMenuIsDown) {
1528 // The shortcut action consumes the MENU. Even if it is still down,
1529 // it won't trigger the next shortcut action. In the case of the
1530 // shortcut action triggering a new activity, like Bookmarks, we
1531 // won't get onKeyUp for MENU. So it is important to reset it here.
1532 mMenuIsDown = false;
1533 }
1534 switch (item.getItemId()) {
1535 // -- Main menu
1536 case R.id.new_tab_menu_id:
1537 openTabToHomePage();
1538 break;
1539
1540 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001541 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001542 break;
1543
1544 case R.id.goto_menu_id:
1545 editUrl();
1546 break;
1547
1548 case R.id.bookmarks_menu_id:
1549 bookmarksOrHistoryPicker(false);
1550 break;
1551
1552 case R.id.active_tabs_menu_id:
1553 showActiveTabsPage();
1554 break;
1555
1556 case R.id.add_bookmark_menu_id:
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001557 bookmarkCurrentPage(AddBookmarkPage.DEFAULT_FOLDER_ID, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001558 break;
1559
1560 case R.id.stop_reload_menu_id:
1561 if (mInLoad) {
1562 stopLoading();
1563 } else {
1564 getCurrentTopWebView().reload();
1565 }
1566 break;
1567
1568 case R.id.back_menu_id:
1569 getCurrentTopWebView().goBack();
1570 break;
1571
1572 case R.id.forward_menu_id:
1573 getCurrentTopWebView().goForward();
1574 break;
1575
1576 case R.id.close_menu_id:
1577 // Close the subwindow if it exists.
1578 if (mTabControl.getCurrentSubWindow() != null) {
1579 dismissSubWindow(mTabControl.getCurrentTab());
1580 break;
1581 }
1582 closeCurrentTab();
1583 break;
1584
1585 case R.id.homepage_menu_id:
1586 Tab current = mTabControl.getCurrentTab();
1587 if (current != null) {
1588 dismissSubWindow(current);
1589 loadUrl(current.getWebView(), mSettings.getHomePage());
1590 }
1591 break;
1592
1593 case R.id.preferences_menu_id:
1594 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1595 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1596 getCurrentTopWebView().getUrl());
1597 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1598 break;
1599
1600 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001601 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001602 break;
1603
John Reckf33b1632011-06-04 20:00:23 -07001604 case R.id.freeze_tab_menu_id:
1605 // TODO: Show error messages
John Reck541f55a2011-06-07 16:34:43 -07001606 Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001607 if (source == null) break;
John Reck541f55a2011-06-07 16:34:43 -07001608 Tab snapshot = createNewTab(false, false, false);
1609 if (snapshot == null) break;
John Reckf33b1632011-06-04 20:00:23 -07001610 try {
1611 ByteArrayOutputStream bos = new ByteArrayOutputStream();
John Reck541f55a2011-06-07 16:34:43 -07001612 source.saveSnapshot(bos);
John Reckf33b1632011-06-04 20:00:23 -07001613 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
John Reck541f55a2011-06-07 16:34:43 -07001614 snapshot.loadSnapshot(bis);
1615 mUi.onTabDataChanged(snapshot);
John Reckf33b1632011-06-04 20:00:23 -07001616 bis.close();
1617 bos.close();
John Reck541f55a2011-06-07 16:34:43 -07001618 setActiveTab(snapshot);
John Reckf33b1632011-06-04 20:00:23 -07001619 } catch (IOException e) {
John Reckf33b1632011-06-04 20:00:23 -07001620 }
1621 break;
1622
Leon Scrogginsac993842011-02-02 12:54:07 -05001623 case R.id.save_webarchive_menu_id:
1624 String state = Environment.getExternalStorageState();
1625 if (!Environment.MEDIA_MOUNTED.equals(state)) {
1626 Log.e(LOGTAG, "External storage not mounted");
1627 Toast.makeText(mActivity, R.string.webarchive_failed,
1628 Toast.LENGTH_SHORT).show();
1629 break;
1630 }
1631 final String directory = Environment.getExternalStoragePublicDirectory(
1632 Environment.DIRECTORY_DOWNLOADS) + File.separator;
1633 File dir = new File(directory);
1634 if (!dir.exists() && !dir.mkdirs()) {
1635 Log.e(LOGTAG, "Save as Web Archive: mkdirs for " + directory + " failed!");
1636 Toast.makeText(mActivity, R.string.webarchive_failed,
1637 Toast.LENGTH_SHORT).show();
1638 break;
1639 }
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001640 final WebView topWebView = getCurrentTopWebView();
Leon Scrogginsac993842011-02-02 12:54:07 -05001641 final String title = topWebView.getTitle();
John Reck51d8bad2011-02-28 15:47:47 -08001642 final String url = topWebView.getUrl();
Leon Scrogginsac993842011-02-02 12:54:07 -05001643 topWebView.saveWebArchive(directory, true,
1644 new ValueCallback<String>() {
1645 @Override
1646 public void onReceiveValue(final String value) {
1647 if (value != null) {
1648 File file = new File(value);
1649 final long length = file.length();
1650 if (file.exists() && length > 0) {
Leon Scroggins1cb96552011-02-11 14:22:57 -05001651 Toast.makeText(mActivity, R.string.webarchive_saved,
1652 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001653 final DownloadManager manager = (DownloadManager) mActivity
1654 .getSystemService(Context.DOWNLOAD_SERVICE);
1655 new Thread("Add WebArchive to download manager") {
1656 @Override
1657 public void run() {
Vasu Noria9e30a72011-03-07 11:37:34 -08001658 manager.addCompletedDownload(
1659 null == title ? value : title,
Leon Scrogginsac993842011-02-02 12:54:07 -05001660 value, true, "application/x-webarchive-xml",
1661 value, length, true);
1662 }
1663 }.start();
1664 return;
1665 }
1666 }
John Reck51d8bad2011-02-28 15:47:47 -08001667 DownloadHandler.onDownloadStartNoStream(mActivity,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001668 url, null, null, null, topWebView.isPrivateBrowsingEnabled());
Leon Scrogginsac993842011-02-02 12:54:07 -05001669 }
1670 });
1671 break;
1672
Michael Kolb8233fac2010-10-26 16:08:53 -07001673 case R.id.page_info_menu_id:
1674 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(),
1675 false);
1676 break;
1677
1678 case R.id.classic_history_menu_id:
1679 bookmarksOrHistoryPicker(true);
1680 break;
1681
1682 case R.id.title_bar_share_page_url:
1683 case R.id.share_page_menu_id:
1684 Tab currentTab = mTabControl.getCurrentTab();
1685 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001686 return false;
1687 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001688 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001689 break;
1690
1691 case R.id.dump_nav_menu_id:
1692 getCurrentTopWebView().debugDump();
1693 break;
1694
1695 case R.id.dump_counters_menu_id:
1696 getCurrentTopWebView().dumpV8Counters();
1697 break;
1698
1699 case R.id.zoom_in_menu_id:
1700 getCurrentTopWebView().zoomIn();
1701 break;
1702
1703 case R.id.zoom_out_menu_id:
1704 getCurrentTopWebView().zoomOut();
1705 break;
1706
1707 case R.id.view_downloads_menu_id:
1708 viewDownloads();
1709 break;
1710
1711 case R.id.window_one_menu_id:
1712 case R.id.window_two_menu_id:
1713 case R.id.window_three_menu_id:
1714 case R.id.window_four_menu_id:
1715 case R.id.window_five_menu_id:
1716 case R.id.window_six_menu_id:
1717 case R.id.window_seven_menu_id:
1718 case R.id.window_eight_menu_id:
1719 {
1720 int menuid = item.getItemId();
1721 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1722 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1723 Tab desiredTab = mTabControl.getTab(id);
1724 if (desiredTab != null &&
1725 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001726 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001727 }
1728 break;
1729 }
1730 }
1731 }
1732 break;
1733
1734 default:
1735 return false;
1736 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001737 return true;
1738 }
1739
1740 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001741 // Let the History and Bookmark fragments handle menus they created.
1742 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1743 return false;
1744 }
1745
Michael Kolb8233fac2010-10-26 16:08:53 -07001746 int id = item.getItemId();
1747 boolean result = true;
1748 switch (id) {
1749 // For the context menu from the title bar
1750 case R.id.title_bar_copy_page_url:
1751 Tab currentTab = mTabControl.getCurrentTab();
1752 if (null == currentTab) {
1753 result = false;
1754 break;
1755 }
1756 WebView mainView = currentTab.getWebView();
1757 if (null == mainView) {
1758 result = false;
1759 break;
1760 }
1761 copy(mainView.getUrl());
1762 break;
1763 // -- Browser context menu
1764 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001765 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001766 case R.id.copy_link_context_menu_id:
1767 final WebView webView = getCurrentTopWebView();
1768 if (null == webView) {
1769 result = false;
1770 break;
1771 }
1772 final HashMap<String, WebView> hrefMap =
1773 new HashMap<String, WebView>();
1774 hrefMap.put("webview", webView);
1775 final Message msg = mHandler.obtainMessage(
1776 FOCUS_NODE_HREF, id, 0, hrefMap);
1777 webView.requestFocusNodeHref(msg);
1778 break;
1779
1780 default:
1781 // For other context menus
1782 result = onOptionsItemSelected(item);
1783 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001784 return result;
1785 }
1786
1787 /**
1788 * support programmatically opening the context menu
1789 */
1790 public void openContextMenu(View view) {
1791 mActivity.openContextMenu(view);
1792 }
1793
1794 /**
1795 * programmatically open the options menu
1796 */
1797 public void openOptionsMenu() {
1798 mActivity.openOptionsMenu();
1799 }
1800
1801 public boolean onMenuOpened(int featureId, Menu menu) {
1802 if (mOptionsMenuOpen) {
1803 if (mConfigChanged) {
1804 // We do not need to make any changes to the state of the
1805 // title bar, since the only thing that happened was a
1806 // change in orientation
1807 mConfigChanged = false;
1808 } else {
1809 if (!mExtendedMenuOpen) {
1810 mExtendedMenuOpen = true;
1811 mUi.onExtendedMenuOpened();
1812 } else {
1813 // Switching the menu back to icon view, so show the
1814 // title bar once again.
1815 mExtendedMenuOpen = false;
1816 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001817 }
1818 }
1819 } else {
1820 // The options menu is closed, so open it, and show the title
1821 mOptionsMenuOpen = true;
1822 mConfigChanged = false;
1823 mExtendedMenuOpen = false;
1824 mUi.onOptionsMenuOpened();
1825 }
1826 return true;
1827 }
1828
1829 public void onOptionsMenuClosed(Menu menu) {
1830 mOptionsMenuOpen = false;
1831 mUi.onOptionsMenuClosed(mInLoad);
1832 }
1833
1834 public void onContextMenuClosed(Menu menu) {
1835 mUi.onContextMenuClosed(menu, mInLoad);
1836 }
1837
1838 // Helper method for getting the top window.
1839 @Override
1840 public WebView getCurrentTopWebView() {
1841 return mTabControl.getCurrentTopWebView();
1842 }
1843
1844 @Override
1845 public WebView getCurrentWebView() {
1846 return mTabControl.getCurrentWebView();
1847 }
1848
1849 /*
1850 * This method is called as a result of the user selecting the options
1851 * menu to see the download window. It shows the download window on top of
1852 * the current window.
1853 */
1854 void viewDownloads() {
1855 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1856 mActivity.startActivity(intent);
1857 }
1858
1859 // action mode
1860
1861 void onActionModeStarted(ActionMode mode) {
1862 mUi.onActionModeStarted(mode);
1863 mActionMode = mode;
1864 }
1865
1866 /*
1867 * True if a custom ActionMode (i.e. find or select) is in use.
1868 */
1869 @Override
1870 public boolean isInCustomActionMode() {
1871 return mActionMode != null;
1872 }
1873
1874 /*
1875 * End the current ActionMode.
1876 */
1877 @Override
1878 public void endActionMode() {
1879 if (mActionMode != null) {
1880 mActionMode.finish();
1881 }
1882 }
1883
1884 /*
1885 * Called by find and select when they are finished. Replace title bars
1886 * as necessary.
1887 */
1888 public void onActionModeFinished(ActionMode mode) {
1889 if (!isInCustomActionMode()) return;
1890 mUi.onActionModeFinished(mInLoad);
1891 mActionMode = null;
1892 }
1893
1894 boolean isInLoad() {
1895 return mInLoad;
1896 }
1897
1898 // bookmark handling
1899
1900 /**
1901 * add the current page as a bookmark to the given folder id
1902 * @param folderId use -1 for the default folder
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001903 * @param canBeAnEdit If true, check to see whether the site is already
1904 * bookmarked, and if it is, edit that bookmark. If false, and
1905 * the site is already bookmarked, do not attempt to edit the
1906 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001907 */
1908 @Override
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001909 public void bookmarkCurrentPage(long folderId, boolean canBeAnEdit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001910 Intent i = new Intent(mActivity,
1911 AddBookmarkPage.class);
1912 WebView w = getCurrentTopWebView();
1913 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1914 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1915 String touchIconUrl = w.getTouchIconUrl();
1916 if (touchIconUrl != null) {
1917 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1918 WebSettings settings = w.getSettings();
1919 if (settings != null) {
1920 i.putExtra(AddBookmarkPage.USER_AGENT,
1921 settings.getUserAgentString());
1922 }
1923 }
1924 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1925 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1926 getDesiredThumbnailHeight(mActivity)));
1927 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
1928 i.putExtra(BrowserContract.Bookmarks.PARENT,
1929 folderId);
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001930 if (canBeAnEdit) {
1931 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1932 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001933 // Put the dialog at the upper right of the screen, covering the
1934 // star on the title bar.
1935 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
1936 mActivity.startActivity(i);
1937 }
1938
1939 // file chooser
1940 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1941 mUploadHandler = new UploadHandler(this);
1942 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1943 }
1944
1945 // thumbnails
1946
1947 /**
1948 * Return the desired width for thumbnail screenshots, which are stored in
1949 * the database, and used on the bookmarks screen.
1950 * @param context Context for finding out the density of the screen.
1951 * @return desired width for thumbnail screenshot.
1952 */
1953 static int getDesiredThumbnailWidth(Context context) {
1954 return context.getResources().getDimensionPixelOffset(
1955 R.dimen.bookmarkThumbnailWidth);
1956 }
1957
1958 /**
1959 * Return the desired height for thumbnail screenshots, which are stored in
1960 * the database, and used on the bookmarks screen.
1961 * @param context Context for finding out the density of the screen.
1962 * @return desired height for thumbnail screenshot.
1963 */
1964 static int getDesiredThumbnailHeight(Context context) {
1965 return context.getResources().getDimensionPixelOffset(
1966 R.dimen.bookmarkThumbnailHeight);
1967 }
1968
Michael Kolb1acef692011-03-08 14:12:06 -08001969 static Bitmap createScreenshot(Tab tab, int width, int height) {
1970 if ((tab != null) && (tab.getWebView() != null)) {
1971 return createScreenshot(tab.getWebView(), width, height);
1972 }
1973 return null;
1974 }
1975
Michael Kolb8233fac2010-10-26 16:08:53 -07001976 private static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08001977 // We render to a bitmap 2x the desired size so that we can then
1978 // re-scale it with filtering since canvas.scale doesn't filter
1979 // This helps reduce aliasing at the cost of being slightly blurry
1980 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07001981 Picture thumbnail = view.capturePicture();
1982 if (thumbnail == null) {
1983 return null;
1984 }
John Reck5c6ac2f2011-01-05 10:18:03 -08001985 width *= filter_scale;
1986 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07001987 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1988 Canvas canvas = new Canvas(bm);
1989 // May need to tweak these values to determine what is the
1990 // best scale factor
1991 int thumbnailWidth = thumbnail.getWidth();
1992 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08001993 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08001994 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08001995 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07001996 } else {
1997 return null;
1998 }
John Reckfe49ab42010-11-16 17:09:37 -08001999
Michael Kolbeb95db42011-03-03 10:38:40 -08002000 float scaleFactorY = (float) height / (float)thumbnailHeight;
2001 if (scaleFactorY > scaleFactor) {
2002 // The picture is narrower than the requested AR
2003 // Center the thumnail and crop the sides
2004 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08002005 float wx = (thumbnailWidth * scaleFactor) - width;
2006 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07002007 }
2008
John Reckfe49ab42010-11-16 17:09:37 -08002009 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07002010
2011 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08002012 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
2013 height / filter_scale, true);
2014 bm.recycle();
2015 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002016 }
2017
John Reck34ef2672011-02-10 11:30:55 -08002018 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002019 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002020 // FIXME: Would like to make sure there is actually something to
2021 // draw, but the API for that (WebViewCore.pictureReady()) is not
2022 // currently accessible here.
2023
John Reck34ef2672011-02-10 11:30:55 -08002024 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002025 if (view == null) {
2026 // Tab was destroyed
2027 return;
2028 }
John Reck34ef2672011-02-10 11:30:55 -08002029 final String url = tab.getUrl();
2030 final String originalUrl = view.getOriginalUrl();
2031
2032 if (TextUtils.isEmpty(url)) {
2033 return;
2034 }
2035
2036 // Only update thumbnails for web urls (http(s)://), not for
2037 // about:, javascript:, data:, etc...
2038 // Unless it is a bookmarked site, then always update
2039 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2040 return;
2041 }
2042
Michael Kolb8233fac2010-10-26 16:08:53 -07002043 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2044 getDesiredThumbnailHeight(mActivity));
2045 if (bm == null) {
2046 return;
2047 }
2048
2049 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002050 new AsyncTask<Void, Void, Void>() {
2051 @Override
2052 protected Void doInBackground(Void... unused) {
2053 Cursor cursor = null;
2054 try {
2055 // TODO: Clean this up
2056 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2057 if (cursor != null && cursor.moveToFirst()) {
2058 final ByteArrayOutputStream os =
2059 new ByteArrayOutputStream();
2060 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002061
John Reck34ef2672011-02-10 11:30:55 -08002062 ContentValues values = new ContentValues();
2063 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002064
John Reck34ef2672011-02-10 11:30:55 -08002065 do {
John Reck617fd832011-02-16 14:35:59 -08002066 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002067 cr.update(Images.CONTENT_URI, values, null, null);
2068 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002069 }
John Reck34ef2672011-02-10 11:30:55 -08002070 } catch (IllegalStateException e) {
2071 // Ignore
2072 } finally {
2073 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002074 }
John Reck34ef2672011-02-10 11:30:55 -08002075 return null;
2076 }
2077 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002078 }
2079
2080 private class Copy implements OnMenuItemClickListener {
2081 private CharSequence mText;
2082
2083 public boolean onMenuItemClick(MenuItem item) {
2084 copy(mText);
2085 return true;
2086 }
2087
2088 public Copy(CharSequence toCopy) {
2089 mText = toCopy;
2090 }
2091 }
2092
Leon Scroggins63c02662010-11-18 15:16:27 -05002093 private static class Download implements OnMenuItemClickListener {
2094 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002095 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002096 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002097
2098 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002099 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002100 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002101 return true;
2102 }
2103
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002104 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002105 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002106 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002107 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002108 }
2109 }
2110
Cary Clark8974d282010-11-22 10:46:05 -05002111 private static class SelectText implements OnMenuItemClickListener {
2112 private WebView mWebView;
2113
2114 public boolean onMenuItemClick(MenuItem item) {
2115 if (mWebView != null) {
2116 return mWebView.selectText();
2117 }
2118 return false;
2119 }
2120
2121 public SelectText(WebView webView) {
2122 mWebView = webView;
2123 }
2124
2125 }
2126
Michael Kolb8233fac2010-10-26 16:08:53 -07002127 /********************** TODO: UI stuff *****************************/
2128
2129 // these methods have been copied, they still need to be cleaned up
2130
2131 /****************** tabs ***************************************************/
2132
2133 // basic tab interactions:
2134
2135 // it is assumed that tabcontrol already knows about the tab
2136 protected void addTab(Tab tab) {
2137 mUi.addTab(tab);
2138 }
2139
2140 protected void removeTab(Tab tab) {
2141 mUi.removeTab(tab);
2142 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002143 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002144 }
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}