blob: 3974a9c4087282197309ff44144b66cb2d65a700 [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
Michael Kolb8233fac2010-10-26 16:08:53 -070084import java.io.ByteArrayOutputStream;
85import java.io.File;
86import java.net.URLEncoder;
87import java.util.Calendar;
88import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080089import java.util.List;
Michael Kolb8233fac2010-10-26 16:08:53 -070090
91/**
92 * Controller for browser
93 */
94public class Controller
95 implements WebViewController, UiController {
96
97 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -080098 private static final String SEND_APP_ID_EXTRA =
99 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700100 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800101
Michael Kolb8233fac2010-10-26 16:08:53 -0700102
103 // public message ids
104 public final static int LOAD_URL = 1001;
105 public final static int STOP_LOAD = 1002;
106
107 // Message Ids
108 private static final int FOCUS_NODE_HREF = 102;
109 private static final int RELEASE_WAKELOCK = 107;
110
111 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
112
113 private static final int OPEN_BOOKMARKS = 201;
114
115 private static final int EMPTY_MENU = -1;
116
Michael Kolb8233fac2010-10-26 16:08:53 -0700117 // activity requestCode
118 final static int PREFERENCES_PAGE = 3;
119 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000120 final static int AUTOFILL_SETUP = 5;
121
Michael Kolb8233fac2010-10-26 16:08:53 -0700122 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
123
124 // As the ids are dynamically created, we can't guarantee that they will
125 // be in sequence, so this static array maps ids to a window number.
126 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
127 { R.id.window_one_menu_id, R.id.window_two_menu_id,
128 R.id.window_three_menu_id, R.id.window_four_menu_id,
129 R.id.window_five_menu_id, R.id.window_six_menu_id,
130 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
131
132 // "source" parameter for Google search through search key
133 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
134 // "source" parameter for Google search through simplily type
135 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
136
Guang Zhu9e78f512011-05-04 11:45:11 -0700137 // "no-crash-recovery" parameter in intetnt to suppress crash recovery
138 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
139
Michael Kolb8233fac2010-10-26 16:08:53 -0700140 private Activity mActivity;
141 private UI mUi;
142 private TabControl mTabControl;
143 private BrowserSettings mSettings;
144 private WebViewFactory mFactory;
John Reckb3417f02011-01-14 11:01:05 -0800145 private OptionsMenuHandler mOptionsMenuHandler = null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700146
147 private WakeLock mWakeLock;
148
149 private UrlHandler mUrlHandler;
150 private UploadHandler mUploadHandler;
151 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700152 private PageDialogsHandler mPageDialogsHandler;
153 private NetworkStateHandler mNetworkHandler;
154
Ben Murdoch8029a772010-11-16 11:58:21 +0000155 private Message mAutoFillSetupMessage;
156
Michael Kolb8233fac2010-10-26 16:08:53 -0700157 private boolean mShouldShowErrorConsole;
158
159 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
160
161 // FIXME, temp address onPrepareMenu performance problem.
162 // When we move everything out of view, we should rewrite this.
163 private int mCurrentMenuState = 0;
164 private int mMenuState = R.id.MAIN_MENU;
165 private int mOldMenuState = EMPTY_MENU;
166 private Menu mCachedMenu;
167
Michael Kolb8233fac2010-10-26 16:08:53 -0700168 private boolean mMenuIsDown;
169
170 // For select and find, we keep track of the ActionMode so that
171 // finish() can be called as desired.
172 private ActionMode mActionMode;
173
174 /**
175 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
176 * of whether the configuration has changed. The first onMenuOpened call
177 * after a configuration change is simply a reopening of the same menu
178 * (i.e. mIconView did not change).
179 */
180 private boolean mConfigChanged;
181
182 /**
183 * Keeps track of whether the options menu is open. This is important in
184 * determining whether to show or hide the title bar overlay
185 */
186 private boolean mOptionsMenuOpen;
187
188 /**
189 * Whether or not the options menu is in its bigger, popup menu form. When
190 * true, we want the title bar overlay to be gone. When false, we do not.
191 * Only meaningful if mOptionsMenuOpen is true.
192 */
193 private boolean mExtendedMenuOpen;
194
195 private boolean mInLoad;
196
197 private boolean mActivityPaused = true;
198 private boolean mLoadStopped;
199
200 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500201 // Checks to see when the bookmarks database has changed, and updates the
202 // Tabs' notion of whether they represent bookmarked sites.
203 private ContentObserver mBookmarksObserver;
John Reck0ebd3ac2010-12-09 11:14:04 -0800204 private DataController mDataController;
John Reck847b5322011-04-14 17:02:18 -0700205 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700206
207 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
208 @Override
209 public Void doInBackground(File... files) {
210 if (files != null) {
211 for (File f : files) {
212 if (!f.delete()) {
213 Log.e(LOGTAG, f.getPath() + " was not deleted");
214 }
215 }
216 }
217 return null;
218 }
219 }
220
221 public Controller(Activity browser) {
222 mActivity = browser;
223 mSettings = BrowserSettings.getInstance();
John Reck0ebd3ac2010-12-09 11:14:04 -0800224 mDataController = DataController.getInstance(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700225 mTabControl = new TabControl(this);
226 mSettings.setController(this);
John Reck847b5322011-04-14 17:02:18 -0700227 mCrashRecoveryHandler = new CrashRecoveryHandler(this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700228
229 mUrlHandler = new UrlHandler(this);
230 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700231 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
232
233 PowerManager pm = (PowerManager) mActivity
234 .getSystemService(Context.POWER_SERVICE);
235 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
236
237 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500238 mBookmarksObserver = new ContentObserver(mHandler) {
239 @Override
240 public void onChange(boolean selfChange) {
241 int size = mTabControl.getTabCount();
242 for (int i = 0; i < size; i++) {
243 mTabControl.getTab(i).updateBookmarkedStatus();
244 }
245 }
246
247 };
248 browser.getContentResolver().registerContentObserver(
249 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700250
251 mNetworkHandler = new NetworkStateHandler(mActivity, this);
252 // Start watching the default geolocation permissions
253 mSystemAllowGeolocationOrigins =
254 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
255 mSystemAllowGeolocationOrigins.start();
256
257 retainIconsOnStartup();
258 }
259
Patrick Scott7d50a932011-02-04 09:27:26 -0500260 void start(final Bundle icicle, final Intent intent) {
Guang Zhu9e78f512011-05-04 11:45:11 -0700261 boolean noCrashRecovery = intent.getBooleanExtra(NO_CRASH_RECOVERY, false);
262 if (icicle != null || noCrashRecovery) {
John Reck847b5322011-04-14 17:02:18 -0700263 mCrashRecoveryHandler.clearState();
264 doStart(icicle, intent);
265 } else {
266 mCrashRecoveryHandler.startRecovery(intent);
267 }
268 }
269
270 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700271 // Unless the last browser usage was within 24 hours, destroy any
272 // remaining incognito tabs.
273
274 Calendar lastActiveDate = icicle != null ?
275 (Calendar) icicle.getSerializable("lastActiveDate") : null;
276 Calendar today = Calendar.getInstance();
277 Calendar yesterday = Calendar.getInstance();
278 yesterday.add(Calendar.DATE, -1);
279
Patrick Scott7d50a932011-02-04 09:27:26 -0500280 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700281 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800282 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700283
Patrick Scott7d50a932011-02-04 09:27:26 -0500284 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700285 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500286 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000287
Michael Kolbc831b632011-05-11 09:30:34 -0700288 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500289 // Not able to restore so we go ahead and clear session cookies. We
290 // must do this before trying to login the user as we don't want to
291 // clear any session cookies set during login.
292 CookieManager.getInstance().removeSessionCookie();
293 }
294
Patrick Scottd43e75a2011-03-14 14:47:23 -0400295 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500296 new Runnable() {
297 @Override public void run() {
Michael Kolbc831b632011-05-11 09:30:34 -0700298 onPreloginFinished(icicle, intent, currentTabId, restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500299 }
300 });
301 }
302
Michael Kolbc831b632011-05-11 09:30:34 -0700303 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500304 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700305 if (currentTabId == -1) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700306 final Bundle extra = intent.getExtras();
307 // Create an initial tab.
308 // If the intent is ACTION_VIEW and data is not null, the Browser is
309 // invoked to view the content by another application. In this case,
310 // the tab will be close when exit.
311 UrlData urlData = mIntentHandler.getUrlDataFromIntent(intent);
Michael Kolb7bcafde2011-05-09 13:55:59 -0700312 Tab t = null;
313 if (urlData.isEmpty()) {
314 t = openTabToHomePage();
315 } else {
316 t = openTab(urlData);
317 }
318 if (t != null) {
319 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
320 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700321 WebView webView = t.getWebView();
322 if (extra != null) {
323 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
324 if (scale > 0 && scale <= 1000) {
325 webView.setInitialScale(scale);
326 }
327 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700328 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700329 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500330 mUi.needsRestoreAllTabs());
Michael Kolb1bf23132010-11-19 12:55:12 -0800331 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700332 // TabControl.restoreState() will create a new tab even if
333 // restoring the state fails.
334 setActiveTab(mTabControl.getCurrentTab());
335 }
336 // clear up the thumbnail directory, which is no longer used;
337 // ideally this should only be run once after an upgrade from
338 // a previous version of the browser
339 new ClearThumbnails().execute(mTabControl.getThumbnailDir()
340 .listFiles());
341 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700342 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700343 if (jsFlags.trim().length() != 0) {
344 getCurrentWebView().setJsFlags(jsFlags);
345 }
John Reck439c9a52010-12-14 10:04:39 -0800346 if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
347 bookmarksOrHistoryPicker(false);
348 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700349 }
350
351 void setWebViewFactory(WebViewFactory factory) {
352 mFactory = factory;
353 }
354
Michael Kolb1514bb72010-11-22 09:11:48 -0800355 @Override
356 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700357 return mFactory;
358 }
359
360 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800361 public void onSetWebView(Tab tab, WebView view) {
362 mUi.onSetWebView(tab, view);
363 }
364
365 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800366 public void createSubWindow(Tab tab) {
367 endActionMode();
368 WebView mainView = tab.getWebView();
369 WebView subView = mFactory.createWebView((mainView == null)
370 ? false
371 : mainView.isPrivateBrowsingEnabled());
372 mUi.createSubWindow(tab, subView);
373 }
374
375 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700376 public Activity getActivity() {
377 return mActivity;
378 }
379
380 void setUi(UI ui) {
381 mUi = ui;
382 }
383
384 BrowserSettings getSettings() {
385 return mSettings;
386 }
387
388 IntentHandler getIntentHandler() {
389 return mIntentHandler;
390 }
391
392 @Override
393 public UI getUi() {
394 return mUi;
395 }
396
397 int getMaxTabs() {
398 return mActivity.getResources().getInteger(R.integer.max_tabs);
399 }
400
401 @Override
402 public TabControl getTabControl() {
403 return mTabControl;
404 }
405
Michael Kolb1bf23132010-11-19 12:55:12 -0800406 @Override
407 public List<Tab> getTabs() {
408 return mTabControl.getTabs();
409 }
410
Michael Kolb8233fac2010-10-26 16:08:53 -0700411 // Open the icon database and retain all the icons for visited sites.
Ben Murdoch9446b932010-11-25 16:20:14 +0000412 // This is done on a background thread so as not to stall startup.
Michael Kolb8233fac2010-10-26 16:08:53 -0700413 private void retainIconsOnStartup() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000414 // WebIconDatabase needs to be retrieved on the UI thread so that if
415 // it has not been created successfully yet the Handler is started on the
416 // UI thread.
417 new RetainIconsOnStartupTask(WebIconDatabase.getInstance()).execute();
418 }
419
420 private class RetainIconsOnStartupTask extends AsyncTask<Void, Void, Void> {
421 private WebIconDatabase mDb;
422
423 public RetainIconsOnStartupTask(WebIconDatabase db) {
424 mDb = db;
425 }
426
John Recka00cbbd2010-12-16 12:38:19 -0800427 @Override
Ben Murdoch9446b932010-11-25 16:20:14 +0000428 protected Void doInBackground(Void... unused) {
429 mDb.open(mActivity.getDir("icons", 0).getPath());
430 Cursor c = null;
431 try {
432 c = Browser.getAllBookmarks(mActivity.getContentResolver());
433 if (c.moveToFirst()) {
434 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
435 do {
436 String url = c.getString(urlIndex);
437 mDb.retainIconForPageUrl(url);
438 } while (c.moveToNext());
439 }
440 } catch (IllegalStateException e) {
441 Log.e(LOGTAG, "retainIconsOnStartup", e);
442 } finally {
443 if (c != null) c.close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700444 }
Ben Murdoch9446b932010-11-25 16:20:14 +0000445
446 return null;
Michael Kolb8233fac2010-10-26 16:08:53 -0700447 }
448 }
449
450 private void startHandler() {
451 mHandler = new Handler() {
452
453 @Override
454 public void handleMessage(Message msg) {
455 switch (msg.what) {
456 case OPEN_BOOKMARKS:
457 bookmarksOrHistoryPicker(false);
458 break;
459 case FOCUS_NODE_HREF:
460 {
461 String url = (String) msg.getData().get("url");
462 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500463 String src = (String) msg.getData().get("src");
464 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700465 if (TextUtils.isEmpty(url)) {
466 break;
467 }
468 HashMap focusNodeMap = (HashMap) msg.obj;
469 WebView view = (WebView) focusNodeMap.get("webview");
470 // Only apply the action if the top window did not change.
471 if (getCurrentTopWebView() != view) {
472 break;
473 }
474 switch (msg.arg1) {
475 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -0700476 loadUrlFromContext(getCurrentTopWebView(), url);
477 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500478 case R.id.view_image_context_menu_id:
479 loadUrlFromContext(getCurrentTopWebView(), src);
480 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500481 case R.id.open_newtab_context_menu_id:
482 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700483 openTab(url, parent,
484 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500485 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700486 case R.id.copy_link_context_menu_id:
487 copy(url);
488 break;
489 case R.id.save_link_context_menu_id:
490 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500491 DownloadHandler.onDownloadStartNoStream(
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000492 mActivity, url, null, null, null,
493 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700494 break;
495 }
496 break;
497 }
498
499 case LOAD_URL:
500 loadUrlFromContext(getCurrentTopWebView(), (String) msg.obj);
501 break;
502
503 case STOP_LOAD:
504 stopLoading();
505 break;
506
507 case RELEASE_WAKELOCK:
508 if (mWakeLock.isHeld()) {
509 mWakeLock.release();
510 // if we reach here, Browser should be still in the
511 // background loading after WAKELOCK_TIMEOUT (5-min).
512 // To avoid burning the battery, stop loading.
513 mTabControl.stopAllLoading();
514 }
515 break;
516
517 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800518 Tab tab = (Tab) msg.obj;
519 if (tab != null) {
520 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700521 }
522 break;
523 }
524 }
525 };
526
527 }
528
Michael Kolbba99c5d2010-11-29 14:57:41 -0800529 @Override
530 public void shareCurrentPage() {
531 shareCurrentPage(mTabControl.getCurrentTab());
532 }
533
534 private void shareCurrentPage(Tab tab) {
535 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800536 sharePage(mActivity, tab.getTitle(),
537 tab.getUrl(), tab.getFavicon(),
538 createScreenshot(tab.getWebView(),
539 getDesiredThumbnailWidth(mActivity),
540 getDesiredThumbnailHeight(mActivity)));
541 }
542 }
543
Michael Kolb8233fac2010-10-26 16:08:53 -0700544 /**
545 * Share a page, providing the title, url, favicon, and a screenshot. Uses
546 * an {@link Intent} to launch the Activity chooser.
547 * @param c Context used to launch a new Activity.
548 * @param title Title of the page. Stored in the Intent with
549 * {@link Intent#EXTRA_SUBJECT}
550 * @param url URL of the page. Stored in the Intent with
551 * {@link Intent#EXTRA_TEXT}
552 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
553 * with {@link Browser#EXTRA_SHARE_FAVICON}
554 * @param screenshot Bitmap of a screenshot of the page. Stored in the
555 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
556 */
557 static final void sharePage(Context c, String title, String url,
558 Bitmap favicon, Bitmap screenshot) {
559 Intent send = new Intent(Intent.ACTION_SEND);
560 send.setType("text/plain");
561 send.putExtra(Intent.EXTRA_TEXT, url);
562 send.putExtra(Intent.EXTRA_SUBJECT, title);
563 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
564 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
565 try {
566 c.startActivity(Intent.createChooser(send, c.getString(
567 R.string.choosertitle_sharevia)));
568 } catch(android.content.ActivityNotFoundException ex) {
569 // if no app handles it, do nothing
570 }
571 }
572
573 private void copy(CharSequence text) {
574 ClipboardManager cm = (ClipboardManager) mActivity
575 .getSystemService(Context.CLIPBOARD_SERVICE);
576 cm.setText(text);
577 }
578
579 // lifecycle
580
581 protected void onConfgurationChanged(Configuration config) {
582 mConfigChanged = true;
583 if (mPageDialogsHandler != null) {
584 mPageDialogsHandler.onConfigurationChanged(config);
585 }
586 mUi.onConfigurationChanged(config);
587 }
588
589 @Override
590 public void handleNewIntent(Intent intent) {
591 mIntentHandler.onNewIntent(intent);
592 }
593
594 protected void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800595 if (mUi.isCustomViewShowing()) {
596 hideCustomView();
597 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700598 if (mActivityPaused) {
599 Log.e(LOGTAG, "BrowserActivity is already paused.");
600 return;
601 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700602 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800603 Tab tab = mTabControl.getCurrentTab();
604 if (tab != null) {
605 tab.pause();
606 if (!pauseWebViewTimers(tab)) {
607 mWakeLock.acquire();
608 mHandler.sendMessageDelayed(mHandler
609 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
610 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700611 }
612 mUi.onPause();
613 mNetworkHandler.onPause();
614
615 WebView.disablePlatformNotifications();
John Reck847b5322011-04-14 17:02:18 -0700616 mCrashRecoveryHandler.clearState();
Michael Kolb8233fac2010-10-26 16:08:53 -0700617 }
618
John Reckaed9c542011-05-27 16:08:53 -0700619 void onSaveInstanceState(Bundle outState, boolean saveImages) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700620 // the default implementation requires each view to have an id. As the
621 // browser handles the state itself and it doesn't use id for the views,
622 // don't call the default implementation. Otherwise it will trigger the
623 // warning like this, "couldn't save which view has focus because the
624 // focused view XXX has no id".
625
626 // Save all the tabs
John Reckaed9c542011-05-27 16:08:53 -0700627 mTabControl.saveState(outState, saveImages);
Michael Kolb8233fac2010-10-26 16:08:53 -0700628 // Save time so that we know how old incognito tabs (if any) are.
629 outState.putSerializable("lastActiveDate", Calendar.getInstance());
630 }
631
632 void onResume() {
633 if (!mActivityPaused) {
634 Log.e(LOGTAG, "BrowserActivity is already resumed.");
635 return;
636 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700637 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800638 Tab current = mTabControl.getCurrentTab();
639 if (current != null) {
640 current.resume();
641 resumeWebViewTimers(current);
642 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700643 if (mWakeLock.isHeld()) {
644 mHandler.removeMessages(RELEASE_WAKELOCK);
645 mWakeLock.release();
646 }
647 mUi.onResume();
648 mNetworkHandler.onResume();
649 WebView.enablePlatformNotifications();
650 }
651
Michael Kolb70976932010-11-30 11:34:01 -0800652 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800653 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800654 * @param tab guaranteed non-null
655 */
656 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700657 boolean inLoad = tab.inPageLoad();
658 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
659 CookieSyncManager.getInstance().startSync();
660 WebView w = tab.getWebView();
661 if (w != null) {
662 w.resumeTimers();
663 }
664 }
665 }
666
Michael Kolb70976932010-11-30 11:34:01 -0800667 /**
668 * Pause all WebView timers using the WebView of the given tab
669 * @param tab
670 * @return true if the timers are paused or tab is null
671 */
672 private boolean pauseWebViewTimers(Tab tab) {
673 if (tab == null) {
674 return true;
675 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700676 CookieSyncManager.getInstance().stopSync();
677 WebView w = getCurrentWebView();
678 if (w != null) {
679 w.pauseTimers();
680 }
681 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700682 }
Michael Kolb70976932010-11-30 11:34:01 -0800683 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700684 }
685
686 void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800687 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700688 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
689 mUploadHandler = null;
690 }
691 if (mTabControl == null) return;
692 mUi.onDestroy();
693 // Remove the current tab and sub window
694 Tab t = mTabControl.getCurrentTab();
695 if (t != null) {
696 dismissSubWindow(t);
697 removeTab(t);
698 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500699 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700700 // Destroy all the tabs
701 mTabControl.destroy();
702 WebIconDatabase.getInstance().close();
703 // Stop watching the default geolocation permissions
704 mSystemAllowGeolocationOrigins.stop();
705 mSystemAllowGeolocationOrigins = null;
706 }
707
708 protected boolean isActivityPaused() {
709 return mActivityPaused;
710 }
711
712 protected void onLowMemory() {
713 mTabControl.freeMemory();
714 }
715
716 @Override
717 public boolean shouldShowErrorConsole() {
718 return mShouldShowErrorConsole;
719 }
720
721 protected void setShouldShowErrorConsole(boolean show) {
722 if (show == mShouldShowErrorConsole) {
723 // Nothing to do.
724 return;
725 }
726 mShouldShowErrorConsole = show;
727 Tab t = mTabControl.getCurrentTab();
728 if (t == null) {
729 // There is no current tab so we cannot toggle the error console
730 return;
731 }
732 mUi.setShouldShowErrorConsole(t, show);
733 }
734
735 @Override
736 public void stopLoading() {
737 mLoadStopped = true;
738 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700739 WebView w = getCurrentTopWebView();
740 w.stopLoading();
Michael Kolb8233fac2010-10-26 16:08:53 -0700741 mUi.onPageStopped(tab);
742 }
743
744 boolean didUserStopLoading() {
745 return mLoadStopped;
746 }
747
748 // WebViewController
749
750 @Override
John Reck324d4402011-01-11 16:56:42 -0800751 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700752
753 // We've started to load a new page. If there was a pending message
754 // to save a screenshot then we will now take the new page and save
755 // an incorrect screenshot. Therefore, remove any pending thumbnail
756 // messages from the queue.
757 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800758 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700759
760 // reset sync timer to avoid sync starts during loading a page
761 CookieSyncManager.getInstance().resetSync();
762
763 if (!mNetworkHandler.isNetworkUp()) {
764 view.setNetworkAvailable(false);
765 }
766
767 // when BrowserActivity just starts, onPageStarted may be called before
768 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
769 // to start the timer. As we won't switch tabs while an activity is in
770 // pause state, we can ensure calling resume and pause in pair.
771 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800772 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700773 }
774 mLoadStopped = false;
775 if (!mNetworkHandler.isNetworkUp()) {
776 mNetworkHandler.createAndShowNetworkDialog();
777 }
778 endActionMode();
779
John Reck30c714c2010-12-16 17:30:34 -0800780 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700781
John Reck324d4402011-01-11 16:56:42 -0800782 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700783 // update the bookmark database for favicon
784 maybeUpdateFavicon(tab, null, url, favicon);
785
786 Performance.tracePageStart(url);
787
788 // Performance probe
789 if (false) {
790 Performance.onPageStarted();
791 }
792
793 }
794
795 @Override
John Reck324d4402011-01-11 16:56:42 -0800796 public void onPageFinished(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800797 mUi.onTabDataChanged(tab);
John Reck324d4402011-01-11 16:56:42 -0800798 if (!tab.isPrivateBrowsingEnabled()
799 && !TextUtils.isEmpty(tab.getUrl())) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700800 if (tab.inForeground() && !didUserStopLoading()
801 || !tab.inForeground()) {
802 // Only update the bookmark screenshot if the user did not
803 // cancel the load early.
804 mHandler.sendMessageDelayed(mHandler.obtainMessage(
John Reck34ef2672011-02-10 11:30:55 -0800805 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 500);
807 }
808 }
809 // pause the WebView timer and release the wake lock if it is finished
810 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800811 if (mActivityPaused && pauseWebViewTimers(tab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700812 if (mWakeLock.isHeld()) {
813 mHandler.removeMessages(RELEASE_WAKELOCK);
814 mWakeLock.release();
815 }
816 }
817 // Performance probe
818 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800819 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700820 }
821
822 Performance.tracePageFinished();
823 }
824
825 @Override
John Reck30c714c2010-12-16 17:30:34 -0800826 public void onProgressChanged(Tab tab) {
827 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700828
829 if (newProgress == 100) {
830 CookieSyncManager.getInstance().sync();
831 // onProgressChanged() may continue to be called after the main
832 // frame has finished loading, as any remaining sub frames continue
833 // to load. We'll only get called once though with newProgress as
834 // 100 when everything is loaded. (onPageFinished is called once
835 // when the main frame completes loading regardless of the state of
836 // any sub frames so calls to onProgressChanges may continue after
837 // onPageFinished has executed)
838 if (mInLoad) {
839 mInLoad = false;
840 updateInLoadMenuItems(mCachedMenu);
841 }
842 } else {
843 if (!mInLoad) {
844 // onPageFinished may have already been called but a subframe is
845 // still loading and updating the progress. Reset mInLoad and
846 // update the menu items.
847 mInLoad = true;
848 updateInLoadMenuItems(mCachedMenu);
849 }
850 }
John Reck30c714c2010-12-16 17:30:34 -0800851 mUi.onProgressChanged(tab);
852 }
853
854 @Override
855 public void onUpdatedLockIcon(Tab tab) {
856 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700857 }
858
859 @Override
860 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800861 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800862 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800863 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
865 return;
866 }
867 // Update the title in the history database if not in private browsing mode
868 if (!tab.isPrivateBrowsingEnabled()) {
John Reck0ebd3ac2010-12-09 11:14:04 -0800869 mDataController.updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700870 }
871 }
872
873 @Override
874 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800875 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700876 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
877 }
878
879 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800880 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
881 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700882 }
883
884 @Override
885 public boolean shouldOverrideKeyEvent(KeyEvent event) {
886 if (mMenuIsDown) {
887 // only check shortcut key when MENU is held
888 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
889 event);
890 } else {
891 return false;
892 }
893 }
894
895 @Override
896 public void onUnhandledKeyEvent(KeyEvent event) {
897 if (!isActivityPaused()) {
898 if (event.getAction() == KeyEvent.ACTION_DOWN) {
899 mActivity.onKeyDown(event.getKeyCode(), event);
900 } else {
901 mActivity.onKeyUp(event.getKeyCode(), event);
902 }
903 }
904 }
905
906 @Override
John Reck324d4402011-01-11 16:56:42 -0800907 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700908 // Don't save anything in private browsing mode
909 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800910 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700911
John Reck324d4402011-01-11 16:56:42 -0800912 if (TextUtils.isEmpty(url)
913 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700914 return;
915 }
John Reck0ebd3ac2010-12-09 11:14:04 -0800916 mDataController.updateVisitedHistory(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700917 WebIconDatabase.getInstance().retainIconForPageUrl(url);
John Reck847b5322011-04-14 17:02:18 -0700918 if (!mActivityPaused) {
919 // Since we clear the state in onPause, don't backup the current
920 // state if we are already paused
921 mCrashRecoveryHandler.backupState();
922 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700923 }
924
925 @Override
926 public void getVisitedHistory(final ValueCallback<String[]> callback) {
927 AsyncTask<Void, Void, String[]> task =
928 new AsyncTask<Void, Void, String[]>() {
929 @Override
930 public String[] doInBackground(Void... unused) {
931 return Browser.getVisitedHistory(mActivity.getContentResolver());
932 }
933 @Override
934 public void onPostExecute(String[] result) {
935 callback.onReceiveValue(result);
936 }
937 };
938 task.execute();
939 }
940
941 @Override
942 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
943 final HttpAuthHandler handler, final String host,
944 final String realm) {
945 String username = null;
946 String password = null;
947
948 boolean reuseHttpAuthUsernamePassword
949 = handler.useHttpAuthUsernamePassword();
950
951 if (reuseHttpAuthUsernamePassword && view != null) {
952 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
953 if (credentials != null && credentials.length == 2) {
954 username = credentials[0];
955 password = credentials[1];
956 }
957 }
958
959 if (username != null && password != null) {
960 handler.proceed(username, password);
961 } else {
962 if (tab.inForeground()) {
963 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
964 } else {
965 handler.cancel();
966 }
967 }
968 }
969
970 @Override
971 public void onDownloadStart(Tab tab, String url, String userAgent,
972 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000973 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -0500974 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000975 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
976 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 // This Tab was opened for the sole purpose of downloading a
978 // file. Remove it.
979 if (tab == mTabControl.getCurrentTab()) {
980 // In this case, the Tab is still on top.
981 goBackOnePageOrQuit();
982 } else {
983 // In this case, it is not.
984 closeTab(tab);
985 }
986 }
987 }
988
989 @Override
990 public Bitmap getDefaultVideoPoster() {
991 return mUi.getDefaultVideoPoster();
992 }
993
994 @Override
995 public View getVideoLoadingProgressView() {
996 return mUi.getVideoLoadingProgressView();
997 }
998
999 @Override
1000 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1001 SslError error) {
1002 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1003 }
1004
Patrick Scott92066772011-03-10 08:46:27 -05001005 @Override
1006 public void showAutoLogin(Tab tab) {
1007 assert tab.inForeground();
1008 // Update the title bar to show the auto-login request.
1009 mUi.showAutoLogin(tab);
1010 }
1011
1012 @Override
1013 public void hideAutoLogin(Tab tab) {
1014 assert tab.inForeground();
1015 mUi.hideAutoLogin(tab);
1016 }
1017
Michael Kolb8233fac2010-10-26 16:08:53 -07001018 // helper method
1019
1020 /*
1021 * Update the favorites icon if the private browsing isn't enabled and the
1022 * icon is valid.
1023 */
1024 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1025 final String url, Bitmap favicon) {
1026 if (favicon == null) {
1027 return;
1028 }
1029 if (!tab.isPrivateBrowsingEnabled()) {
1030 Bookmarks.updateFavicon(mActivity
1031 .getContentResolver(), originalUrl, url, favicon);
1032 }
1033 }
1034
Leon Scroggins4cd97792010-12-03 15:31:56 -05001035 @Override
1036 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001037 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001038 mUi.bookmarkedStatusHasChanged(tab);
1039 }
1040
Michael Kolb8233fac2010-10-26 16:08:53 -07001041 // end WebViewController
1042
1043 protected void pageUp() {
1044 getCurrentTopWebView().pageUp(false);
1045 }
1046
1047 protected void pageDown() {
1048 getCurrentTopWebView().pageDown(false);
1049 }
1050
1051 // callback from phone title bar
1052 public void editUrl() {
1053 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001054 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001055 }
1056
Michael Kolbcfa3af52010-12-14 10:36:11 -08001057 public void startVoiceSearch() {
1058 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1059 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1060 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1061 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1062 mActivity.getComponentName().flattenToString());
1063 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001064 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001065 mActivity.startActivity(intent);
1066 }
1067
Michael Kolb11d19782011-03-20 10:17:40 -07001068 @Override
1069 public void activateVoiceSearchMode(String title, List<String> results) {
1070 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001071 }
1072
1073 public void revertVoiceSearchMode(Tab tab) {
1074 mUi.revertVoiceTitleBar(tab);
1075 }
1076
Michael Kolb736990c2011-03-20 10:01:20 -07001077 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001078 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001079 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1080 }
1081
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001082 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001083 WebChromeClient.CustomViewCallback callback) {
1084 if (tab.inForeground()) {
1085 if (mUi.isCustomViewShowing()) {
1086 callback.onCustomViewHidden();
1087 return;
1088 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001089 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001090 // Save the menu state and set it to empty while the custom
1091 // view is showing.
1092 mOldMenuState = mMenuState;
1093 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001094 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001095 }
1096 }
1097
1098 @Override
1099 public void hideCustomView() {
1100 if (mUi.isCustomViewShowing()) {
1101 mUi.onHideCustomView();
1102 // Reset the old menu state.
1103 mMenuState = mOldMenuState;
1104 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001105 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001106 }
1107 }
1108
1109 protected void onActivityResult(int requestCode, int resultCode,
1110 Intent intent) {
1111 if (getCurrentTopWebView() == null) return;
1112 switch (requestCode) {
1113 case PREFERENCES_PAGE:
1114 if (resultCode == Activity.RESULT_OK && intent != null) {
1115 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001116 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001117 mTabControl.removeParentChildRelationShips();
1118 }
1119 }
1120 break;
1121 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001122 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001123 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001124 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001125 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001126 case AUTOFILL_SETUP:
1127 // Determine whether a profile was actually set up or not
1128 // and if so, send the message back to the WebTextView to
1129 // fill the form with the new profile.
1130 if (getSettings().getAutoFillProfile() != null) {
1131 mAutoFillSetupMessage.sendToTarget();
1132 mAutoFillSetupMessage = null;
1133 }
1134 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001135 default:
1136 break;
1137 }
1138 getCurrentTopWebView().requestFocus();
1139 }
1140
1141 /**
1142 * Open the Go page.
1143 * @param startWithHistory If true, open starting on the history tab.
1144 * Otherwise, start with the bookmarks tab.
1145 */
1146 @Override
1147 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1148 if (mTabControl.getCurrentWebView() == null) {
1149 return;
1150 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001151 // clear action mode
1152 if (isInCustomActionMode()) {
1153 endActionMode();
1154 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001155 Bundle extras = new Bundle();
1156 // Disable opening in a new window if we have maxed out the windows
1157 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1158 !mTabControl.canCreateNewTab());
1159 mUi.showComboView(startWithHistory, extras);
1160 }
1161
1162 // combo view callbacks
1163
1164 /**
1165 * callback from ComboPage when clear history is requested
1166 */
1167 public void onRemoveParentChildRelationships() {
1168 mTabControl.removeParentChildRelationShips();
1169 }
1170
1171 /**
1172 * callback from ComboPage when bookmark/history selection
1173 */
1174 @Override
1175 public void onUrlSelected(String url, boolean newTab) {
1176 removeComboView();
1177 if (!TextUtils.isEmpty(url)) {
1178 if (newTab) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07001179 final Tab parent = mTabControl.getCurrentTab();
1180 openTab(url,
1181 (parent != null) && parent.isPrivateBrowsingEnabled(),
1182 !mSettings.openInBackground(),
1183 true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001184 } else {
1185 final Tab currentTab = mTabControl.getCurrentTab();
1186 dismissSubWindow(currentTab);
1187 loadUrl(getCurrentTopWebView(), url);
1188 }
1189 }
1190 }
1191
1192 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001193 * dismiss the ComboPage
1194 */
1195 @Override
1196 public void removeComboView() {
1197 mUi.hideComboView();
1198 }
1199
1200 // active tabs page handling
1201
1202 protected void showActiveTabsPage() {
1203 mMenuState = EMPTY_MENU;
1204 mUi.showActiveTabsPage();
1205 }
1206
1207 /**
1208 * Remove the active tabs page.
1209 * @param needToAttach If true, the active tabs page did not attach a tab
1210 * to the content view, so we need to do that here.
1211 */
1212 @Override
1213 public void removeActiveTabsPage(boolean needToAttach) {
1214 mMenuState = R.id.MAIN_MENU;
1215 mUi.removeActiveTabsPage();
1216 if (needToAttach) {
1217 setActiveTab(mTabControl.getCurrentTab());
1218 }
1219 getCurrentTopWebView().requestFocus();
1220 }
1221
1222 // key handling
1223 protected void onBackKey() {
1224 if (!mUi.onBackKey()) {
1225 WebView subwindow = mTabControl.getCurrentSubWindow();
1226 if (subwindow != null) {
1227 if (subwindow.canGoBack()) {
1228 subwindow.goBack();
1229 } else {
1230 dismissSubWindow(mTabControl.getCurrentTab());
1231 }
1232 } else {
1233 goBackOnePageOrQuit();
1234 }
1235 }
1236 }
1237
Michael Kolb4bd767d2011-05-27 11:33:55 -07001238 protected boolean onMenuKey() {
1239 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001240 }
1241
Michael Kolb8233fac2010-10-26 16:08:53 -07001242 // menu handling and state
1243 // TODO: maybe put into separate handler
1244
1245 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckb3417f02011-01-14 11:01:05 -08001246 if (mOptionsMenuHandler != null) {
1247 return mOptionsMenuHandler.onCreateOptionsMenu(menu);
1248 }
1249
John Reckd73c5a22010-12-22 10:22:50 -08001250 if (mMenuState == EMPTY_MENU) {
1251 return false;
1252 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001253 MenuInflater inflater = mActivity.getMenuInflater();
1254 inflater.inflate(R.menu.browser, menu);
1255 updateInLoadMenuItems(menu);
1256 // hold on to the menu reference here; it is used by the page callbacks
1257 // to update the menu based on loading state
1258 mCachedMenu = menu;
1259 return true;
1260 }
1261
1262 protected void onCreateContextMenu(ContextMenu menu, View v,
1263 ContextMenuInfo menuInfo) {
1264 if (v instanceof TitleBarBase) {
1265 return;
1266 }
1267 if (!(v instanceof WebView)) {
1268 return;
1269 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001270 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001271 WebView.HitTestResult result = webview.getHitTestResult();
1272 if (result == null) {
1273 return;
1274 }
1275
1276 int type = result.getType();
1277 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1278 Log.w(LOGTAG,
1279 "We should not show context menu when nothing is touched");
1280 return;
1281 }
1282 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1283 // let TextView handles context menu
1284 return;
1285 }
1286
1287 // Note, http://b/issue?id=1106666 is requesting that
1288 // an inflated menu can be used again. This is not available
1289 // yet, so inflate each time (yuk!)
1290 MenuInflater inflater = mActivity.getMenuInflater();
1291 inflater.inflate(R.menu.browsercontext, menu);
1292
1293 // Show the correct menu group
1294 final String extra = result.getExtra();
1295 menu.setGroupVisible(R.id.PHONE_MENU,
1296 type == WebView.HitTestResult.PHONE_TYPE);
1297 menu.setGroupVisible(R.id.EMAIL_MENU,
1298 type == WebView.HitTestResult.EMAIL_TYPE);
1299 menu.setGroupVisible(R.id.GEO_MENU,
1300 type == WebView.HitTestResult.GEO_TYPE);
1301 menu.setGroupVisible(R.id.IMAGE_MENU,
1302 type == WebView.HitTestResult.IMAGE_TYPE
1303 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1304 menu.setGroupVisible(R.id.ANCHOR_MENU,
1305 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1306 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001307 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1308 || type == WebView.HitTestResult.PHONE_TYPE
1309 || type == WebView.HitTestResult.EMAIL_TYPE
1310 || type == WebView.HitTestResult.GEO_TYPE;
1311 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1312 if (hitText) {
1313 menu.findItem(R.id.select_text_menu_id)
1314 .setOnMenuItemClickListener(new SelectText(webview));
1315 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001316 // Setup custom handling depending on the type
1317 switch (type) {
1318 case WebView.HitTestResult.PHONE_TYPE:
1319 menu.setHeaderTitle(Uri.decode(extra));
1320 menu.findItem(R.id.dial_context_menu_id).setIntent(
1321 new Intent(Intent.ACTION_VIEW, Uri
1322 .parse(WebView.SCHEME_TEL + extra)));
1323 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1324 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1325 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1326 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1327 addIntent);
1328 menu.findItem(R.id.copy_phone_context_menu_id)
1329 .setOnMenuItemClickListener(
1330 new Copy(extra));
1331 break;
1332
1333 case WebView.HitTestResult.EMAIL_TYPE:
1334 menu.setHeaderTitle(extra);
1335 menu.findItem(R.id.email_context_menu_id).setIntent(
1336 new Intent(Intent.ACTION_VIEW, Uri
1337 .parse(WebView.SCHEME_MAILTO + extra)));
1338 menu.findItem(R.id.copy_mail_context_menu_id)
1339 .setOnMenuItemClickListener(
1340 new Copy(extra));
1341 break;
1342
1343 case WebView.HitTestResult.GEO_TYPE:
1344 menu.setHeaderTitle(extra);
1345 menu.findItem(R.id.map_context_menu_id).setIntent(
1346 new Intent(Intent.ACTION_VIEW, Uri
1347 .parse(WebView.SCHEME_GEO
1348 + URLEncoder.encode(extra))));
1349 menu.findItem(R.id.copy_geo_context_menu_id)
1350 .setOnMenuItemClickListener(
1351 new Copy(extra));
1352 break;
1353
1354 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1355 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001356 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001357 // decide whether to show the open link in new tab option
1358 boolean showNewTab = mTabControl.canCreateNewTab();
1359 MenuItem newTabItem
1360 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001361 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001362 ? R.string.contextmenu_openlink_newwindow_background
1363 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001364 newTabItem.setVisible(showNewTab);
1365 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001366 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1367 newTabItem.setOnMenuItemClickListener(
1368 new MenuItem.OnMenuItemClickListener() {
1369 @Override
1370 public boolean onMenuItemClick(MenuItem item) {
1371 final HashMap<String, WebView> hrefMap =
1372 new HashMap<String, WebView>();
1373 hrefMap.put("webview", webview);
1374 final Message msg = mHandler.obtainMessage(
1375 FOCUS_NODE_HREF,
1376 R.id.open_newtab_context_menu_id,
1377 0, hrefMap);
1378 webview.requestFocusNodeHref(msg);
1379 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001380 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001381 });
1382 } else {
1383 newTabItem.setOnMenuItemClickListener(
1384 new MenuItem.OnMenuItemClickListener() {
1385 @Override
1386 public boolean onMenuItemClick(MenuItem item) {
1387 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001388 openTab(extra, parent,
1389 !mSettings.openInBackground(),
1390 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001391 return true;
1392 }
1393 });
1394 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001395 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1397 break;
1398 }
1399 // otherwise fall through to handle image part
1400 case WebView.HitTestResult.IMAGE_TYPE:
1401 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1402 menu.setHeaderTitle(extra);
1403 }
1404 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1405 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1406 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001407 setOnMenuItemClickListener(
1408 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001409 menu.findItem(R.id.set_wallpaper_context_menu_id).
1410 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1411 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001412 break;
1413
1414 default:
1415 Log.w(LOGTAG, "We should not get here.");
1416 break;
1417 }
1418 //update the ui
1419 mUi.onContextMenuCreated(menu);
1420 }
1421
1422 /**
1423 * As the menu can be open when loading state changes
1424 * we must manually update the state of the stop/reload menu
1425 * item
1426 */
1427 private void updateInLoadMenuItems(Menu menu) {
1428 if (menu == null) {
1429 return;
1430 }
1431 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1432 MenuItem src = mInLoad ?
1433 menu.findItem(R.id.stop_menu_id):
1434 menu.findItem(R.id.reload_menu_id);
1435 if (src != null) {
1436 dest.setIcon(src.getIcon());
1437 dest.setTitle(src.getTitle());
1438 }
1439 }
1440
John Reckb3417f02011-01-14 11:01:05 -08001441 boolean onPrepareOptionsMenu(Menu menu) {
1442 if (mOptionsMenuHandler != null) {
1443 return mOptionsMenuHandler.onPrepareOptionsMenu(menu);
1444 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001445 // Note: setVisible will decide whether an item is visible; while
1446 // setEnabled() will decide whether an item is enabled, which also means
1447 // whether the matching shortcut key will function.
1448 switch (mMenuState) {
1449 case EMPTY_MENU:
1450 if (mCurrentMenuState != mMenuState) {
1451 menu.setGroupVisible(R.id.MAIN_MENU, false);
1452 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1453 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1454 }
1455 break;
1456 default:
1457 if (mCurrentMenuState != mMenuState) {
1458 menu.setGroupVisible(R.id.MAIN_MENU, true);
1459 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1460 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1461 }
1462 final WebView w = getCurrentTopWebView();
1463 boolean canGoBack = false;
1464 boolean canGoForward = false;
1465 boolean isHome = false;
1466 if (w != null) {
1467 canGoBack = w.canGoBack();
1468 canGoForward = w.canGoForward();
1469 isHome = mSettings.getHomePage().equals(w.getUrl());
1470 }
1471 final MenuItem back = menu.findItem(R.id.back_menu_id);
1472 back.setEnabled(canGoBack);
1473
1474 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1475 home.setEnabled(!isHome);
1476
1477 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1478 forward.setEnabled(canGoForward);
1479
1480 // decide whether to show the share link option
1481 PackageManager pm = mActivity.getPackageManager();
1482 Intent send = new Intent(Intent.ACTION_SEND);
1483 send.setType("text/plain");
1484 ResolveInfo ri = pm.resolveActivity(send,
1485 PackageManager.MATCH_DEFAULT_ONLY);
1486 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1487
John Reck35e9dd62011-04-25 09:01:54 -07001488 boolean isNavDump = mSettings.enableNavDump();
Michael Kolb8233fac2010-10-26 16:08:53 -07001489 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1490 nav.setVisible(isNavDump);
1491 nav.setEnabled(isNavDump);
1492
John Reck35e9dd62011-04-25 09:01:54 -07001493 boolean showDebugSettings = mSettings.isDebugEnabled();
Michael Kolb8233fac2010-10-26 16:08:53 -07001494 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1495 counter.setVisible(showDebugSettings);
1496 counter.setEnabled(showDebugSettings);
1497
John Reckb3417f02011-01-14 11:01:05 -08001498 final MenuItem newtab = menu.findItem(R.id.new_tab_menu_id);
1499 newtab.setEnabled(getTabControl().canCreateNewTab());
Michael Kolb8233fac2010-10-26 16:08:53 -07001500
Leon Scroggins81983762011-02-11 15:17:36 -05001501 MenuItem archive = menu.findItem(R.id.save_webarchive_menu_id);
John Reck51d8bad2011-02-28 15:47:47 -08001502 Tab tab = getTabControl().getCurrentTab();
1503 String url = tab != null ? tab.getUrl() : null;
1504 archive.setVisible(!TextUtils.isEmpty(url)
1505 && !url.endsWith(".webarchivexml"));
Michael Kolb8233fac2010-10-26 16:08:53 -07001506 break;
1507 }
1508 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001509 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001510 }
1511
1512 public boolean onOptionsItemSelected(MenuItem item) {
John Reckb3417f02011-01-14 11:01:05 -08001513 if (mOptionsMenuHandler != null &&
1514 mOptionsMenuHandler.onOptionsItemSelected(item)) {
1515 return true;
1516 }
1517
Michael Kolb8233fac2010-10-26 16:08:53 -07001518 if (item.getGroupId() != R.id.CONTEXT_MENU) {
1519 // menu remains active, so ensure comboview is dismissed
1520 // if main menu option is selected
1521 removeComboView();
1522 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001523 if (null == getCurrentTopWebView()) {
1524 return false;
1525 }
1526 if (mMenuIsDown) {
1527 // The shortcut action consumes the MENU. Even if it is still down,
1528 // it won't trigger the next shortcut action. In the case of the
1529 // shortcut action triggering a new activity, like Bookmarks, we
1530 // won't get onKeyUp for MENU. So it is important to reset it here.
1531 mMenuIsDown = false;
1532 }
1533 switch (item.getItemId()) {
1534 // -- Main menu
1535 case R.id.new_tab_menu_id:
1536 openTabToHomePage();
1537 break;
1538
1539 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001540 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001541 break;
1542
1543 case R.id.goto_menu_id:
1544 editUrl();
1545 break;
1546
1547 case R.id.bookmarks_menu_id:
1548 bookmarksOrHistoryPicker(false);
1549 break;
1550
1551 case R.id.active_tabs_menu_id:
1552 showActiveTabsPage();
1553 break;
1554
1555 case R.id.add_bookmark_menu_id:
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001556 bookmarkCurrentPage(AddBookmarkPage.DEFAULT_FOLDER_ID, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001557 break;
1558
1559 case R.id.stop_reload_menu_id:
1560 if (mInLoad) {
1561 stopLoading();
1562 } else {
1563 getCurrentTopWebView().reload();
1564 }
1565 break;
1566
1567 case R.id.back_menu_id:
1568 getCurrentTopWebView().goBack();
1569 break;
1570
1571 case R.id.forward_menu_id:
1572 getCurrentTopWebView().goForward();
1573 break;
1574
1575 case R.id.close_menu_id:
1576 // Close the subwindow if it exists.
1577 if (mTabControl.getCurrentSubWindow() != null) {
1578 dismissSubWindow(mTabControl.getCurrentTab());
1579 break;
1580 }
1581 closeCurrentTab();
1582 break;
1583
1584 case R.id.homepage_menu_id:
1585 Tab current = mTabControl.getCurrentTab();
1586 if (current != null) {
1587 dismissSubWindow(current);
1588 loadUrl(current.getWebView(), mSettings.getHomePage());
1589 }
1590 break;
1591
1592 case R.id.preferences_menu_id:
1593 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1594 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1595 getCurrentTopWebView().getUrl());
1596 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1597 break;
1598
1599 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001600 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001601 break;
1602
Leon Scrogginsac993842011-02-02 12:54:07 -05001603 case R.id.save_webarchive_menu_id:
1604 String state = Environment.getExternalStorageState();
1605 if (!Environment.MEDIA_MOUNTED.equals(state)) {
1606 Log.e(LOGTAG, "External storage not mounted");
1607 Toast.makeText(mActivity, R.string.webarchive_failed,
1608 Toast.LENGTH_SHORT).show();
1609 break;
1610 }
1611 final String directory = Environment.getExternalStoragePublicDirectory(
1612 Environment.DIRECTORY_DOWNLOADS) + File.separator;
1613 File dir = new File(directory);
1614 if (!dir.exists() && !dir.mkdirs()) {
1615 Log.e(LOGTAG, "Save as Web Archive: mkdirs for " + directory + " failed!");
1616 Toast.makeText(mActivity, R.string.webarchive_failed,
1617 Toast.LENGTH_SHORT).show();
1618 break;
1619 }
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001620 final WebView topWebView = getCurrentTopWebView();
Leon Scrogginsac993842011-02-02 12:54:07 -05001621 final String title = topWebView.getTitle();
John Reck51d8bad2011-02-28 15:47:47 -08001622 final String url = topWebView.getUrl();
Leon Scrogginsac993842011-02-02 12:54:07 -05001623 topWebView.saveWebArchive(directory, true,
1624 new ValueCallback<String>() {
1625 @Override
1626 public void onReceiveValue(final String value) {
1627 if (value != null) {
1628 File file = new File(value);
1629 final long length = file.length();
1630 if (file.exists() && length > 0) {
Leon Scroggins1cb96552011-02-11 14:22:57 -05001631 Toast.makeText(mActivity, R.string.webarchive_saved,
1632 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001633 final DownloadManager manager = (DownloadManager) mActivity
1634 .getSystemService(Context.DOWNLOAD_SERVICE);
1635 new Thread("Add WebArchive to download manager") {
1636 @Override
1637 public void run() {
Vasu Noria9e30a72011-03-07 11:37:34 -08001638 manager.addCompletedDownload(
1639 null == title ? value : title,
Leon Scrogginsac993842011-02-02 12:54:07 -05001640 value, true, "application/x-webarchive-xml",
1641 value, length, true);
1642 }
1643 }.start();
1644 return;
1645 }
1646 }
John Reck51d8bad2011-02-28 15:47:47 -08001647 DownloadHandler.onDownloadStartNoStream(mActivity,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001648 url, null, null, null, topWebView.isPrivateBrowsingEnabled());
Leon Scrogginsac993842011-02-02 12:54:07 -05001649 }
1650 });
1651 break;
1652
Michael Kolb8233fac2010-10-26 16:08:53 -07001653 case R.id.page_info_menu_id:
1654 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(),
1655 false);
1656 break;
1657
1658 case R.id.classic_history_menu_id:
1659 bookmarksOrHistoryPicker(true);
1660 break;
1661
1662 case R.id.title_bar_share_page_url:
1663 case R.id.share_page_menu_id:
1664 Tab currentTab = mTabControl.getCurrentTab();
1665 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001666 return false;
1667 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001668 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001669 break;
1670
1671 case R.id.dump_nav_menu_id:
1672 getCurrentTopWebView().debugDump();
1673 break;
1674
1675 case R.id.dump_counters_menu_id:
1676 getCurrentTopWebView().dumpV8Counters();
1677 break;
1678
1679 case R.id.zoom_in_menu_id:
1680 getCurrentTopWebView().zoomIn();
1681 break;
1682
1683 case R.id.zoom_out_menu_id:
1684 getCurrentTopWebView().zoomOut();
1685 break;
1686
1687 case R.id.view_downloads_menu_id:
1688 viewDownloads();
1689 break;
1690
1691 case R.id.window_one_menu_id:
1692 case R.id.window_two_menu_id:
1693 case R.id.window_three_menu_id:
1694 case R.id.window_four_menu_id:
1695 case R.id.window_five_menu_id:
1696 case R.id.window_six_menu_id:
1697 case R.id.window_seven_menu_id:
1698 case R.id.window_eight_menu_id:
1699 {
1700 int menuid = item.getItemId();
1701 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1702 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1703 Tab desiredTab = mTabControl.getTab(id);
1704 if (desiredTab != null &&
1705 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001706 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001707 }
1708 break;
1709 }
1710 }
1711 }
1712 break;
1713
1714 default:
1715 return false;
1716 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001717 return true;
1718 }
1719
1720 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001721 // Let the History and Bookmark fragments handle menus they created.
1722 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1723 return false;
1724 }
1725
Michael Kolb8233fac2010-10-26 16:08:53 -07001726 int id = item.getItemId();
1727 boolean result = true;
1728 switch (id) {
1729 // For the context menu from the title bar
1730 case R.id.title_bar_copy_page_url:
1731 Tab currentTab = mTabControl.getCurrentTab();
1732 if (null == currentTab) {
1733 result = false;
1734 break;
1735 }
1736 WebView mainView = currentTab.getWebView();
1737 if (null == mainView) {
1738 result = false;
1739 break;
1740 }
1741 copy(mainView.getUrl());
1742 break;
1743 // -- Browser context menu
1744 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001745 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001746 case R.id.copy_link_context_menu_id:
1747 final WebView webView = getCurrentTopWebView();
1748 if (null == webView) {
1749 result = false;
1750 break;
1751 }
1752 final HashMap<String, WebView> hrefMap =
1753 new HashMap<String, WebView>();
1754 hrefMap.put("webview", webView);
1755 final Message msg = mHandler.obtainMessage(
1756 FOCUS_NODE_HREF, id, 0, hrefMap);
1757 webView.requestFocusNodeHref(msg);
1758 break;
1759
1760 default:
1761 // For other context menus
1762 result = onOptionsItemSelected(item);
1763 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001764 return result;
1765 }
1766
1767 /**
1768 * support programmatically opening the context menu
1769 */
1770 public void openContextMenu(View view) {
1771 mActivity.openContextMenu(view);
1772 }
1773
1774 /**
1775 * programmatically open the options menu
1776 */
1777 public void openOptionsMenu() {
1778 mActivity.openOptionsMenu();
1779 }
1780
1781 public boolean onMenuOpened(int featureId, Menu menu) {
1782 if (mOptionsMenuOpen) {
1783 if (mConfigChanged) {
1784 // We do not need to make any changes to the state of the
1785 // title bar, since the only thing that happened was a
1786 // change in orientation
1787 mConfigChanged = false;
1788 } else {
1789 if (!mExtendedMenuOpen) {
1790 mExtendedMenuOpen = true;
1791 mUi.onExtendedMenuOpened();
1792 } else {
1793 // Switching the menu back to icon view, so show the
1794 // title bar once again.
1795 mExtendedMenuOpen = false;
1796 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001797 }
1798 }
1799 } else {
1800 // The options menu is closed, so open it, and show the title
1801 mOptionsMenuOpen = true;
1802 mConfigChanged = false;
1803 mExtendedMenuOpen = false;
1804 mUi.onOptionsMenuOpened();
1805 }
1806 return true;
1807 }
1808
1809 public void onOptionsMenuClosed(Menu menu) {
1810 mOptionsMenuOpen = false;
1811 mUi.onOptionsMenuClosed(mInLoad);
1812 }
1813
1814 public void onContextMenuClosed(Menu menu) {
1815 mUi.onContextMenuClosed(menu, mInLoad);
1816 }
1817
1818 // Helper method for getting the top window.
1819 @Override
1820 public WebView getCurrentTopWebView() {
1821 return mTabControl.getCurrentTopWebView();
1822 }
1823
1824 @Override
1825 public WebView getCurrentWebView() {
1826 return mTabControl.getCurrentWebView();
1827 }
1828
1829 /*
1830 * This method is called as a result of the user selecting the options
1831 * menu to see the download window. It shows the download window on top of
1832 * the current window.
1833 */
1834 void viewDownloads() {
1835 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1836 mActivity.startActivity(intent);
1837 }
1838
1839 // action mode
1840
1841 void onActionModeStarted(ActionMode mode) {
1842 mUi.onActionModeStarted(mode);
1843 mActionMode = mode;
1844 }
1845
1846 /*
1847 * True if a custom ActionMode (i.e. find or select) is in use.
1848 */
1849 @Override
1850 public boolean isInCustomActionMode() {
1851 return mActionMode != null;
1852 }
1853
1854 /*
1855 * End the current ActionMode.
1856 */
1857 @Override
1858 public void endActionMode() {
1859 if (mActionMode != null) {
1860 mActionMode.finish();
1861 }
1862 }
1863
1864 /*
1865 * Called by find and select when they are finished. Replace title bars
1866 * as necessary.
1867 */
1868 public void onActionModeFinished(ActionMode mode) {
1869 if (!isInCustomActionMode()) return;
1870 mUi.onActionModeFinished(mInLoad);
1871 mActionMode = null;
1872 }
1873
1874 boolean isInLoad() {
1875 return mInLoad;
1876 }
1877
1878 // bookmark handling
1879
1880 /**
1881 * add the current page as a bookmark to the given folder id
1882 * @param folderId use -1 for the default folder
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001883 * @param canBeAnEdit If true, check to see whether the site is already
1884 * bookmarked, and if it is, edit that bookmark. If false, and
1885 * the site is already bookmarked, do not attempt to edit the
1886 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001887 */
1888 @Override
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001889 public void bookmarkCurrentPage(long folderId, boolean canBeAnEdit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001890 Intent i = new Intent(mActivity,
1891 AddBookmarkPage.class);
1892 WebView w = getCurrentTopWebView();
1893 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1894 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1895 String touchIconUrl = w.getTouchIconUrl();
1896 if (touchIconUrl != null) {
1897 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1898 WebSettings settings = w.getSettings();
1899 if (settings != null) {
1900 i.putExtra(AddBookmarkPage.USER_AGENT,
1901 settings.getUserAgentString());
1902 }
1903 }
1904 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1905 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1906 getDesiredThumbnailHeight(mActivity)));
1907 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
1908 i.putExtra(BrowserContract.Bookmarks.PARENT,
1909 folderId);
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001910 if (canBeAnEdit) {
1911 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1912 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001913 // Put the dialog at the upper right of the screen, covering the
1914 // star on the title bar.
1915 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
1916 mActivity.startActivity(i);
1917 }
1918
1919 // file chooser
1920 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1921 mUploadHandler = new UploadHandler(this);
1922 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1923 }
1924
1925 // thumbnails
1926
1927 /**
1928 * Return the desired width for thumbnail screenshots, which are stored in
1929 * the database, and used on the bookmarks screen.
1930 * @param context Context for finding out the density of the screen.
1931 * @return desired width for thumbnail screenshot.
1932 */
1933 static int getDesiredThumbnailWidth(Context context) {
1934 return context.getResources().getDimensionPixelOffset(
1935 R.dimen.bookmarkThumbnailWidth);
1936 }
1937
1938 /**
1939 * Return the desired height for thumbnail screenshots, which are stored in
1940 * the database, and used on the bookmarks screen.
1941 * @param context Context for finding out the density of the screen.
1942 * @return desired height for thumbnail screenshot.
1943 */
1944 static int getDesiredThumbnailHeight(Context context) {
1945 return context.getResources().getDimensionPixelOffset(
1946 R.dimen.bookmarkThumbnailHeight);
1947 }
1948
Michael Kolb1acef692011-03-08 14:12:06 -08001949 static Bitmap createScreenshot(Tab tab, int width, int height) {
1950 if ((tab != null) && (tab.getWebView() != null)) {
1951 return createScreenshot(tab.getWebView(), width, height);
1952 }
1953 return null;
1954 }
1955
Michael Kolb8233fac2010-10-26 16:08:53 -07001956 private static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08001957 // We render to a bitmap 2x the desired size so that we can then
1958 // re-scale it with filtering since canvas.scale doesn't filter
1959 // This helps reduce aliasing at the cost of being slightly blurry
1960 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07001961 Picture thumbnail = view.capturePicture();
1962 if (thumbnail == null) {
1963 return null;
1964 }
John Reck5c6ac2f2011-01-05 10:18:03 -08001965 width *= filter_scale;
1966 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07001967 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1968 Canvas canvas = new Canvas(bm);
1969 // May need to tweak these values to determine what is the
1970 // best scale factor
1971 int thumbnailWidth = thumbnail.getWidth();
1972 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08001973 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08001974 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08001975 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07001976 } else {
1977 return null;
1978 }
John Reckfe49ab42010-11-16 17:09:37 -08001979
Michael Kolbeb95db42011-03-03 10:38:40 -08001980 float scaleFactorY = (float) height / (float)thumbnailHeight;
1981 if (scaleFactorY > scaleFactor) {
1982 // The picture is narrower than the requested AR
1983 // Center the thumnail and crop the sides
1984 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08001985 float wx = (thumbnailWidth * scaleFactor) - width;
1986 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07001987 }
1988
John Reckfe49ab42010-11-16 17:09:37 -08001989 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07001990
1991 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08001992 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
1993 height / filter_scale, true);
1994 bm.recycle();
1995 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07001996 }
1997
John Reck34ef2672011-02-10 11:30:55 -08001998 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001999 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002000 // FIXME: Would like to make sure there is actually something to
2001 // draw, but the API for that (WebViewCore.pictureReady()) is not
2002 // currently accessible here.
2003
John Reck34ef2672011-02-10 11:30:55 -08002004 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002005 if (view == null) {
2006 // Tab was destroyed
2007 return;
2008 }
John Reck34ef2672011-02-10 11:30:55 -08002009 final String url = tab.getUrl();
2010 final String originalUrl = view.getOriginalUrl();
2011
2012 if (TextUtils.isEmpty(url)) {
2013 return;
2014 }
2015
2016 // Only update thumbnails for web urls (http(s)://), not for
2017 // about:, javascript:, data:, etc...
2018 // Unless it is a bookmarked site, then always update
2019 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2020 return;
2021 }
2022
Michael Kolb8233fac2010-10-26 16:08:53 -07002023 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2024 getDesiredThumbnailHeight(mActivity));
2025 if (bm == null) {
2026 return;
2027 }
2028
2029 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002030 new AsyncTask<Void, Void, Void>() {
2031 @Override
2032 protected Void doInBackground(Void... unused) {
2033 Cursor cursor = null;
2034 try {
2035 // TODO: Clean this up
2036 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2037 if (cursor != null && cursor.moveToFirst()) {
2038 final ByteArrayOutputStream os =
2039 new ByteArrayOutputStream();
2040 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002041
John Reck34ef2672011-02-10 11:30:55 -08002042 ContentValues values = new ContentValues();
2043 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002044
John Reck34ef2672011-02-10 11:30:55 -08002045 do {
John Reck617fd832011-02-16 14:35:59 -08002046 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002047 cr.update(Images.CONTENT_URI, values, null, null);
2048 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002049 }
John Reck34ef2672011-02-10 11:30:55 -08002050 } catch (IllegalStateException e) {
2051 // Ignore
2052 } finally {
2053 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002054 }
John Reck34ef2672011-02-10 11:30:55 -08002055 return null;
2056 }
2057 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002058 }
2059
2060 private class Copy implements OnMenuItemClickListener {
2061 private CharSequence mText;
2062
2063 public boolean onMenuItemClick(MenuItem item) {
2064 copy(mText);
2065 return true;
2066 }
2067
2068 public Copy(CharSequence toCopy) {
2069 mText = toCopy;
2070 }
2071 }
2072
Leon Scroggins63c02662010-11-18 15:16:27 -05002073 private static class Download implements OnMenuItemClickListener {
2074 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002075 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002076 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002077
2078 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002079 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002080 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002081 return true;
2082 }
2083
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002084 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002085 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002086 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002087 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002088 }
2089 }
2090
Cary Clark8974d282010-11-22 10:46:05 -05002091 private static class SelectText implements OnMenuItemClickListener {
2092 private WebView mWebView;
2093
2094 public boolean onMenuItemClick(MenuItem item) {
2095 if (mWebView != null) {
2096 return mWebView.selectText();
2097 }
2098 return false;
2099 }
2100
2101 public SelectText(WebView webView) {
2102 mWebView = webView;
2103 }
2104
2105 }
2106
Michael Kolb8233fac2010-10-26 16:08:53 -07002107 /********************** TODO: UI stuff *****************************/
2108
2109 // these methods have been copied, they still need to be cleaned up
2110
2111 /****************** tabs ***************************************************/
2112
2113 // basic tab interactions:
2114
2115 // it is assumed that tabcontrol already knows about the tab
2116 protected void addTab(Tab tab) {
2117 mUi.addTab(tab);
2118 }
2119
2120 protected void removeTab(Tab tab) {
2121 mUi.removeTab(tab);
2122 mTabControl.removeTab(tab);
2123 }
2124
Michael Kolbf2055602011-04-09 17:20:03 -07002125 @Override
2126 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002127 // monkey protection against delayed start
2128 if (tab != null) {
2129 mTabControl.setCurrentTab(tab);
2130 // the tab is guaranteed to have a webview after setCurrentTab
2131 mUi.setActiveTab(tab);
2132 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002133 }
2134
2135 protected void closeEmptyChildTab() {
2136 Tab current = mTabControl.getCurrentTab();
2137 if (current != null
2138 && current.getWebView().copyBackForwardList().getSize() == 0) {
Michael Kolbc831b632011-05-11 09:30:34 -07002139 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002140 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002141 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002142 closeTab(current);
2143 }
2144 }
2145 }
2146
2147 protected void reuseTab(Tab appTab, String appId, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002148 // Dismiss the subwindow if applicable.
2149 dismissSubWindow(appTab);
2150 // Since we might kill the WebView, remove it from the
2151 // content view first.
2152 mUi.detachTab(appTab);
2153 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002154 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002155 // TODO: analyze why the remove and add are necessary
2156 mUi.attachTab(appTab);
2157 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002158 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002159 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002160 } else {
2161 // If the tab was the current tab, we have to attach
2162 // it to the view system again.
2163 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002164 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002165 }
2166 }
2167
2168 // Remove the sub window if it exists. Also called by TabControl when the
2169 // user clicks the 'X' to dismiss a sub window.
2170 public void dismissSubWindow(Tab tab) {
2171 removeSubWindow(tab);
2172 // dismiss the subwindow. This will destroy the WebView.
2173 tab.dismissSubWindow();
2174 getCurrentTopWebView().requestFocus();
2175 }
2176
2177 @Override
2178 public void removeSubWindow(Tab t) {
2179 if (t.getSubWebView() != null) {
2180 mUi.removeSubWindow(t.getSubViewContainer());
2181 }
2182 }
2183
2184 @Override
2185 public void attachSubWindow(Tab tab) {
2186 if (tab.getSubWebView() != null) {
2187 mUi.attachSubWindow(tab.getSubViewContainer());
2188 getCurrentTopWebView().requestFocus();
2189 }
2190 }
2191
Michael Kolb7bcafde2011-05-09 13:55:59 -07002192 // open a non inconito tab with the given url data
2193 // and set as active tab
2194 public Tab openTab(UrlData urlData) {
2195 Tab tab = createNewTab(false, true, true);
2196 if ((tab != null) && !urlData.isEmpty()) {
2197 loadUrlDataIn(tab, urlData);
2198 }
2199 return tab;
2200 }
2201
Michael Kolb843510f2010-12-09 10:51:49 -08002202 @Override
2203 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002204 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002205 }
2206
Michael Kolb8233fac2010-10-26 16:08:53 -07002207 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002208 public Tab openIncognitoTab() {
2209 return openTab(INCOGNITO_URI, true, true, false);
2210 }
2211
2212 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002213 public Tab openTab(String url, boolean incognito, boolean setActive,
2214 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002215 return openTab(url, incognito, setActive, useCurrent, null);
2216 }
2217
2218 @Override
2219 public Tab openTab(String url, Tab parent, boolean setActive,
2220 boolean useCurrent) {
2221 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2222 setActive, useCurrent, parent);
2223 }
2224
2225 public Tab openTab(String url, boolean incognito, boolean setActive,
2226 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002227 Tab tab = createNewTab(incognito, setActive, useCurrent);
2228 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002229 if (parent != null && parent != tab) {
2230 parent.addChildTab(tab);
2231 }
Michael Kolba4261fd2011-05-05 11:27:37 -07002232 WebView w = tab.getWebView();
Michael Kolb519d2282011-05-09 17:03:19 -07002233 if (url != null) {
2234 loadUrl(w, url);
2235 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002236 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002237 return tab;
2238 }
2239
2240 // this method will attempt to create a new tab
2241 // incognito: private browsing tab
2242 // setActive: ste tab as current tab
2243 // useCurrent: if no new tab can be created, return current tab
2244 private Tab createNewTab(boolean incognito, boolean setActive,
2245 boolean useCurrent) {
2246 Tab tab = null;
2247 if (mTabControl.canCreateNewTab()) {
2248 tab = mTabControl.createNewTab(incognito);
2249 addTab(tab);
2250 if (setActive) {
2251 setActiveTab(tab);
2252 }
2253 } else {
2254 if (useCurrent) {
2255 tab = mTabControl.getCurrentTab();
2256 // Get rid of the subwindow if it exists
2257 dismissSubWindow(tab);
2258 } else {
2259 mUi.showMaxTabsWarning();
2260 }
2261 }
2262 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002263 }
2264
2265 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002266 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002267 * @return boolean True if we successfully switched to a different tab. If
2268 * the indexth tab is null, or if that tab is the same as
2269 * the current one, return false.
2270 */
2271 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002272 public boolean switchToTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002273 // hide combo view if open
2274 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002275 Tab currentTab = mTabControl.getCurrentTab();
2276 if (tab == null || tab == currentTab) {
2277 return false;
2278 }
2279 setActiveTab(tab);
2280 return true;
2281 }
2282
2283 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002284 public void closeCurrentTab() {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002285 // hide combo view if open
2286 removeComboView();
Michael Kolb8233fac2010-10-26 16:08:53 -07002287 if (mTabControl.getTabCount() == 1) {
John Reck958b2422010-12-03 17:56:17 -08002288 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002289 return;
2290 }
Michael Kolbc831b632011-05-11 09:30:34 -07002291 final Tab current = mTabControl.getCurrentTab();
2292 final int pos = mTabControl.getCurrentPosition();
2293 Tab newTab = current.getParent();
2294 if (newTab == null) {
2295 newTab = mTabControl.getTab(pos + 1);
2296 if (newTab == null) {
2297 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002298 }
2299 }
Michael Kolbc831b632011-05-11 09:30:34 -07002300 if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002301 // Close window
2302 closeTab(current);
2303 }
2304 }
2305
2306 /**
2307 * Close the tab, remove its associated title bar, and adjust mTabControl's
2308 * current tab to a valid value.
2309 */
2310 @Override
2311 public void closeTab(Tab tab) {
Michael Kolb14ee8fb2010-12-09 09:08:20 -08002312 // hide combo view if open
2313 removeComboView();
Michael Kolb2d59c322011-01-25 13:18:55 -08002314 removeTab(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002315 }
2316
2317 /**************** TODO: Url loading clean up *******************************/
2318
2319 // Called when loading from context menu or LOAD_URL message
2320 protected void loadUrlFromContext(WebView view, String url) {
2321 // In case the user enters nothing.
2322 if (url != null && url.length() != 0 && view != null) {
2323 url = UrlUtils.smartUrlFilter(url);
2324 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
2325 loadUrl(view, url);
2326 }
2327 }
2328 }
2329
2330 /**
2331 * Load the URL into the given WebView and update the title bar
2332 * to reflect the new load. Call this instead of WebView.loadUrl
2333 * directly.
2334 * @param view The WebView used to load url.
2335 * @param url The URL to load.
2336 */
2337 protected void loadUrl(WebView view, String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002338 view.loadUrl(url);
2339 }
2340
2341 /**
2342 * Load UrlData into a Tab and update the title bar to reflect the new
2343 * load. Call this instead of UrlData.loadIn directly.
2344 * @param t The Tab used to load.
2345 * @param data The UrlData being loaded.
2346 */
2347 protected void loadUrlDataIn(Tab t, UrlData data) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002348 data.loadIn(t);
2349 }
2350
John Reck30c714c2010-12-16 17:30:34 -08002351 @Override
2352 public void onUserCanceledSsl(Tab tab) {
2353 WebView web = tab.getWebView();
2354 // TODO: Figure out the "right" behavior
2355 if (web.canGoBack()) {
2356 web.goBack();
2357 } else {
2358 web.loadUrl(mSettings.getHomePage());
2359 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002360 }
2361
2362 void goBackOnePageOrQuit() {
2363 Tab current = mTabControl.getCurrentTab();
2364 if (current == null) {
2365 /*
2366 * Instead of finishing the activity, simply push this to the back
2367 * of the stack and let ActivityManager to choose the foreground
2368 * activity. As BrowserActivity is singleTask, it will be always the
2369 * root of the task. So we can use either true or false for
2370 * moveTaskToBack().
2371 */
2372 mActivity.moveTaskToBack(true);
2373 return;
2374 }
2375 WebView w = current.getWebView();
2376 if (w.canGoBack()) {
2377 w.goBack();
2378 } else {
2379 // Check to see if we are closing a window that was created by
2380 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002381 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002382 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002383 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002384 // Now we close the other tab
2385 closeTab(current);
2386 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002387 /*
2388 * Instead of finishing the activity, simply push this to the back
2389 * of the stack and let ActivityManager to choose the foreground
2390 * activity. As BrowserActivity is singleTask, it will be always the
2391 * root of the task. So we can use either true or false for
2392 * moveTaskToBack().
2393 */
2394 mActivity.moveTaskToBack(true);
2395 }
2396 }
2397 }
2398
2399 /**
2400 * Feed the previously stored results strings to the BrowserProvider so that
2401 * the SearchDialog will show them instead of the standard searches.
2402 * @param result String to show on the editable line of the SearchDialog.
2403 */
2404 @Override
2405 public void showVoiceSearchResults(String result) {
2406 ContentProviderClient client = mActivity.getContentResolver()
2407 .acquireContentProviderClient(Browser.BOOKMARKS_URI);
2408 ContentProvider prov = client.getLocalContentProvider();
2409 BrowserProvider bp = (BrowserProvider) prov;
2410 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
2411 client.release();
2412
2413 Bundle bundle = createGoogleSearchSourceBundle(
2414 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
2415 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
2416 startSearch(result, false, bundle, false);
2417 }
2418
2419 private void startSearch(String initialQuery, boolean selectInitialQuery,
2420 Bundle appSearchData, boolean globalSearch) {
2421 if (appSearchData == null) {
2422 appSearchData = createGoogleSearchSourceBundle(
2423 GOOGLE_SEARCH_SOURCE_TYPE);
2424 }
2425
2426 SearchEngine searchEngine = mSettings.getSearchEngine();
2427 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
2428 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
2429 }
2430 mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData,
2431 globalSearch);
2432 }
2433
2434 private Bundle createGoogleSearchSourceBundle(String source) {
2435 Bundle bundle = new Bundle();
2436 bundle.putString(Search.SOURCE, source);
2437 return bundle;
2438 }
2439
2440 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002441 * helper method for key handler
2442 * returns the current tab if it can't advance
2443 */
Michael Kolbc831b632011-05-11 09:30:34 -07002444 private Tab getNextTab() {
2445 return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1,
2446 mTabControl.getCurrentPosition() + 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002447 }
2448
2449 /**
2450 * helper method for key handler
2451 * returns the current tab if it can't advance
2452 */
Michael Kolbc831b632011-05-11 09:30:34 -07002453 private Tab getPrevTab() {
2454 return mTabControl.getTab(Math.max(0,
2455 mTabControl.getCurrentPosition() - 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002456 }
2457
2458 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002459 * handle key events in browser
2460 *
2461 * @param keyCode
2462 * @param event
2463 * @return true if handled, false to pass to super
2464 */
2465 boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002466 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002467 // Even if MENU is already held down, we need to call to super to open
2468 // the IME on long press.
Michael Kolb2814a362011-05-19 15:49:41 -07002469 if (KeyEvent.KEYCODE_MENU == keyCode) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002470 if (mOptionsMenuHandler != null) {
2471 return false;
2472 } else {
2473 event.startTracking();
2474 return true;
2475 }
Michael Kolb2814a362011-05-19 15:49:41 -07002476 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002477 if (!noModifiers
2478 && ((KeyEvent.KEYCODE_MENU == keyCode)
2479 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2480 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002481 mMenuIsDown = true;
2482 return false;
2483 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002484
Cary Clark8ff8c662010-12-29 15:03:05 -05002485 WebView webView = getCurrentTopWebView();
2486 if (webView == null) return false;
2487
Cary Clark160bbb92011-01-10 11:17:07 -05002488 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2489 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002490
Michael Kolb8233fac2010-10-26 16:08:53 -07002491 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002492 case KeyEvent.KEYCODE_TAB:
2493 if (event.isCtrlPressed()) {
2494 if (event.isShiftPressed()) {
2495 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002496 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002497 } else {
2498 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002499 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002500 }
2501 return true;
2502 }
2503 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002504 case KeyEvent.KEYCODE_SPACE:
2505 // WebView/WebTextView handle the keys in the KeyDown. As
2506 // the Activity's shortcut keys are only handled when WebView
2507 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002508 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002509 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002510 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002511 pageDown();
2512 }
2513 return true;
2514 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002515 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002516 event.startTracking();
2517 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002518 case KeyEvent.KEYCODE_DPAD_LEFT:
2519 if (ctrl) {
2520 webView.goBack();
2521 return true;
2522 }
2523 break;
2524 case KeyEvent.KEYCODE_DPAD_RIGHT:
2525 if (ctrl) {
2526 webView.goForward();
2527 return true;
2528 }
2529 break;
2530 case KeyEvent.KEYCODE_A:
2531 if (ctrl) {
2532 webView.selectAll();
2533 return true;
2534 }
2535 break;
Michael Kolba4183062011-01-16 10:43:21 -08002536// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002537 case KeyEvent.KEYCODE_C:
2538 if (ctrl) {
2539 webView.copySelection();
2540 return true;
2541 }
2542 break;
Michael Kolba4183062011-01-16 10:43:21 -08002543// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002544// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002545// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002546// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002547// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002548// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002549// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002550// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002551// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002552// case KeyEvent.KEYCODE_M: // unused
2553// case KeyEvent.KEYCODE_N: // in Chrome: new window
2554// case KeyEvent.KEYCODE_O: // in Chrome: open file
2555// case KeyEvent.KEYCODE_P: // in Chrome: print page
2556// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002557// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002558// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2559 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002560 // we can't use the ctrl/shift flags, they check for
2561 // exclusive use of a modifier
2562 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002563 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002564 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002565 } else {
2566 openTabToHomePage();
2567 }
2568 return true;
2569 }
2570 break;
2571// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2572// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb1a2eba42011-03-16 16:42:49 -07002573 case KeyEvent.KEYCODE_W: // in Chrome: close tab
2574 if (ctrl) {
2575 closeCurrentTab();
2576 return true;
2577 }
2578 break;
Cary Clark8ff8c662010-12-29 15:03:05 -05002579// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2580// case KeyEvent.KEYCODE_Y: // unused
2581// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002582 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002583 // it is a regular key and webview is not null
2584 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002585 }
2586
John Recke6bf4ab2011-02-24 15:48:05 -08002587 boolean onKeyLongPress(int keyCode, KeyEvent event) {
2588 switch(keyCode) {
2589 case KeyEvent.KEYCODE_BACK:
2590 if (mUi.showsWeb()) {
2591 bookmarksOrHistoryPicker(true);
2592 return true;
2593 }
2594 break;
2595 }
2596 return false;
2597 }
2598
Michael Kolb8233fac2010-10-26 16:08:53 -07002599 boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb2814a362011-05-19 15:49:41 -07002600 if (KeyEvent.KEYCODE_MENU == keyCode) {
2601 mMenuIsDown = false;
2602 if (event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002603 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002604 }
2605 }
Cary Clark160bbb92011-01-10 11:17:07 -05002606 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002607 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 case KeyEvent.KEYCODE_BACK:
2609 if (event.isTracking() && !event.isCanceled()) {
2610 onBackKey();
2611 return true;
2612 }
2613 break;
2614 }
2615 return false;
2616 }
2617
2618 public boolean isMenuDown() {
2619 return mMenuIsDown;
2620 }
2621
Ben Murdoch8029a772010-11-16 11:58:21 +00002622 public void setupAutoFill(Message message) {
2623 // Open the settings activity at the AutoFill profile fragment so that
2624 // the user can create a new profile. When they return, we will dispatch
2625 // the message so that we can autofill the form using their new profile.
2626 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2627 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2628 AutoFillSettingsFragment.class.getName());
2629 mAutoFillSetupMessage = message;
2630 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2631 }
John Reckb3417f02011-01-14 11:01:05 -08002632
2633 @Override
2634 public void registerOptionsMenuHandler(OptionsMenuHandler handler) {
2635 mOptionsMenuHandler = handler;
2636 }
2637
2638 @Override
2639 public void unregisterOptionsMenuHandler(OptionsMenuHandler handler) {
2640 if (mOptionsMenuHandler == handler) {
2641 mOptionsMenuHandler = null;
2642 }
2643 }
2644
Narayan Kamath5119edd2011-02-23 15:49:17 +00002645 @Override
2646 public void registerDropdownChangeListener(DropdownChangeListener d) {
2647 mUi.registerDropdownChangeListener(d);
2648 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002649}