blob: 4c05688ba051e8b8c6f264e88df8df5502c2ec7a [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
20import android.app.DownloadManager;
21import android.app.SearchManager;
22import android.content.ClipboardManager;
23import android.content.ContentProvider;
24import android.content.ContentProviderClient;
25import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070026import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.content.ContentValues;
28import android.content.Context;
29import android.content.Intent;
30import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
32import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070033import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050034import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070035import android.database.Cursor;
36import android.database.sqlite.SQLiteDatabase;
Michael Kolb8233fac2010-10-26 16:08:53 -070037import android.graphics.Bitmap;
38import android.graphics.Canvas;
39import android.graphics.Picture;
40import android.net.Uri;
41import android.net.http.SslError;
42import android.os.AsyncTask;
43import android.os.Bundle;
44import android.os.Handler;
45import android.os.Message;
46import android.os.PowerManager;
47import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000048import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070049import android.provider.Browser;
50import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.provider.BrowserContract.Images;
52import android.provider.ContactsContract;
53import android.provider.ContactsContract.Intents.Insert;
Michael Kolbcfa3af52010-12-14 10:36:11 -080054import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070055import android.text.TextUtils;
56import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080057import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070058import android.view.ActionMode;
59import android.view.ContextMenu;
60import android.view.ContextMenu.ContextMenuInfo;
61import android.view.Gravity;
62import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070063import android.view.Menu;
64import android.view.MenuInflater;
65import android.view.MenuItem;
66import android.view.MenuItem.OnMenuItemClickListener;
67import android.view.View;
68import android.webkit.CookieManager;
69import android.webkit.CookieSyncManager;
70import android.webkit.HttpAuthHandler;
71import android.webkit.SslErrorHandler;
72import android.webkit.ValueCallback;
73import android.webkit.WebChromeClient;
74import android.webkit.WebIconDatabase;
75import android.webkit.WebSettings;
76import android.webkit.WebView;
Leon Scrogginsac993842011-02-02 12:54:07 -050077import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070078
Michael Kolb4bd767d2011-05-27 11:33:55 -070079import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070080import com.android.browser.UI.ComboViews;
Michael Kolb4bd767d2011-05-27 11:33:55 -070081import com.android.browser.UI.DropdownChangeListener;
82import com.android.browser.provider.BrowserProvider;
John Reck8cc92352011-07-06 17:41:52 -070083import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070084import com.android.browser.search.SearchEngine;
85import com.android.common.Search;
86
Michael Kolb8233fac2010-10-26 16:08:53 -070087import java.io.ByteArrayOutputStream;
88import java.io.File;
89import java.net.URLEncoder;
90import java.util.Calendar;
91import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080092import java.util.List;
John Reck26b18322011-06-21 13:08:58 -070093import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -070094
95/**
96 * Controller for browser
97 */
98public class Controller
99 implements WebViewController, UiController {
100
101 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800102 private static final String SEND_APP_ID_EXTRA =
103 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700104 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800105
Michael Kolb8233fac2010-10-26 16:08:53 -0700106
107 // public message ids
108 public final static int LOAD_URL = 1001;
109 public final static int STOP_LOAD = 1002;
110
111 // Message Ids
112 private static final int FOCUS_NODE_HREF = 102;
113 private static final int RELEASE_WAKELOCK = 107;
114
115 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
116
117 private static final int OPEN_BOOKMARKS = 201;
118
119 private static final int EMPTY_MENU = -1;
120
Michael Kolb8233fac2010-10-26 16:08:53 -0700121 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700122 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700123 final static int PREFERENCES_PAGE = 3;
124 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000125 final static int AUTOFILL_SETUP = 5;
126
Michael Kolb8233fac2010-10-26 16:08:53 -0700127 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
128
129 // As the ids are dynamically created, we can't guarantee that they will
130 // be in sequence, so this static array maps ids to a window number.
131 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
132 { R.id.window_one_menu_id, R.id.window_two_menu_id,
133 R.id.window_three_menu_id, R.id.window_four_menu_id,
134 R.id.window_five_menu_id, R.id.window_six_menu_id,
135 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
136
137 // "source" parameter for Google search through search key
138 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
139 // "source" parameter for Google search through simplily type
140 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
141
Guang Zhu9e78f512011-05-04 11:45:11 -0700142 // "no-crash-recovery" parameter in intetnt to suppress crash recovery
143 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
144
Michael Kolb8233fac2010-10-26 16:08:53 -0700145 private Activity mActivity;
146 private UI mUi;
147 private TabControl mTabControl;
148 private BrowserSettings mSettings;
149 private WebViewFactory mFactory;
150
151 private WakeLock mWakeLock;
152
153 private UrlHandler mUrlHandler;
154 private UploadHandler mUploadHandler;
155 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 private PageDialogsHandler mPageDialogsHandler;
157 private NetworkStateHandler mNetworkHandler;
Martijn Coenenb2f93552011-06-14 10:48:35 +0200158 private NfcHandler mNfcHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700159
Ben Murdoch8029a772010-11-16 11:58:21 +0000160 private Message mAutoFillSetupMessage;
161
Michael Kolb8233fac2010-10-26 16:08:53 -0700162 private boolean mShouldShowErrorConsole;
163
164 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
165
166 // FIXME, temp address onPrepareMenu performance problem.
167 // When we move everything out of view, we should rewrite this.
168 private int mCurrentMenuState = 0;
169 private int mMenuState = R.id.MAIN_MENU;
170 private int mOldMenuState = EMPTY_MENU;
171 private Menu mCachedMenu;
172
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 private boolean mMenuIsDown;
174
175 // For select and find, we keep track of the ActionMode so that
176 // finish() can be called as desired.
177 private ActionMode mActionMode;
178
179 /**
180 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
181 * of whether the configuration has changed. The first onMenuOpened call
182 * after a configuration change is simply a reopening of the same menu
183 * (i.e. mIconView did not change).
184 */
185 private boolean mConfigChanged;
186
187 /**
188 * Keeps track of whether the options menu is open. This is important in
189 * determining whether to show or hide the title bar overlay
190 */
191 private boolean mOptionsMenuOpen;
192
193 /**
194 * Whether or not the options menu is in its bigger, popup menu form. When
195 * true, we want the title bar overlay to be gone. When false, we do not.
196 * Only meaningful if mOptionsMenuOpen is true.
197 */
198 private boolean mExtendedMenuOpen;
199
200 private boolean mInLoad;
201
202 private boolean mActivityPaused = true;
203 private boolean mLoadStopped;
204
205 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500206 // Checks to see when the bookmarks database has changed, and updates the
207 // Tabs' notion of whether they represent bookmarked sites.
208 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700209 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700210
John Reck30b065e2011-07-19 10:58:05 -0700211 private boolean mSimulateActionBarOverlayMode;
212
Michael Kolb8233fac2010-10-26 16:08:53 -0700213 private static class ClearThumbnails extends AsyncTask<File, Void, Void> {
214 @Override
215 public Void doInBackground(File... files) {
216 if (files != null) {
217 for (File f : files) {
218 if (!f.delete()) {
219 Log.e(LOGTAG, f.getPath() + " was not deleted");
220 }
221 }
222 }
223 return null;
224 }
225 }
226
John Reckbc490d22011-07-22 15:04:59 -0700227 public Controller(Activity browser, boolean preloadCrashState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700228 mActivity = browser;
229 mSettings = BrowserSettings.getInstance();
230 mTabControl = new TabControl(this);
231 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700232 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
John Reckbc490d22011-07-22 15:04:59 -0700233 if (preloadCrashState) {
234 mCrashRecoveryHandler.preloadCrashState();
235 }
Michael Kolb14612442011-06-24 13:06:29 -0700236 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700237
238 mUrlHandler = new UrlHandler(this);
239 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200241 mNfcHandler = new NfcHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700242
Michael Kolb8233fac2010-10-26 16:08:53 -0700243 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500244 mBookmarksObserver = new ContentObserver(mHandler) {
245 @Override
246 public void onChange(boolean selfChange) {
247 int size = mTabControl.getTabCount();
248 for (int i = 0; i < size; i++) {
249 mTabControl.getTab(i).updateBookmarkedStatus();
250 }
251 }
252
253 };
254 browser.getContentResolver().registerContentObserver(
255 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700256
257 mNetworkHandler = new NetworkStateHandler(mActivity, this);
258 // Start watching the default geolocation permissions
259 mSystemAllowGeolocationOrigins =
260 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
261 mSystemAllowGeolocationOrigins.start();
262
263 retainIconsOnStartup();
John Reck30b065e2011-07-19 10:58:05 -0700264 mSimulateActionBarOverlayMode = !BrowserActivity.isTablet(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -0700265 }
266
Patrick Scott7d50a932011-02-04 09:27:26 -0500267 void start(final Bundle icicle, final Intent intent) {
Guang Zhu9e78f512011-05-04 11:45:11 -0700268 boolean noCrashRecovery = intent.getBooleanExtra(NO_CRASH_RECOVERY, false);
269 if (icicle != null || noCrashRecovery) {
John Reck847b5322011-04-14 17:02:18 -0700270 doStart(icicle, intent);
271 } else {
272 mCrashRecoveryHandler.startRecovery(intent);
273 }
274 }
275
276 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 // Unless the last browser usage was within 24 hours, destroy any
278 // remaining incognito tabs.
279
280 Calendar lastActiveDate = icicle != null ?
281 (Calendar) icicle.getSerializable("lastActiveDate") : null;
282 Calendar today = Calendar.getInstance();
283 Calendar yesterday = Calendar.getInstance();
284 yesterday.add(Calendar.DATE, -1);
285
Patrick Scott7d50a932011-02-04 09:27:26 -0500286 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700287 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800288 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700289
Patrick Scott7d50a932011-02-04 09:27:26 -0500290 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700291 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500292 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000293
Michael Kolbc831b632011-05-11 09:30:34 -0700294 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500295 // Not able to restore so we go ahead and clear session cookies. We
296 // must do this before trying to login the user as we don't want to
297 // clear any session cookies set during login.
298 CookieManager.getInstance().removeSessionCookie();
299 }
300
Patrick Scottd43e75a2011-03-14 14:47:23 -0400301 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500302 new Runnable() {
303 @Override public void run() {
Michael Kolbc831b632011-05-11 09:30:34 -0700304 onPreloginFinished(icicle, intent, currentTabId, restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500305 }
306 });
307 }
308
Michael Kolbc831b632011-05-11 09:30:34 -0700309 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500310 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700311 if (currentTabId == -1) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700312 final Bundle extra = intent.getExtras();
313 // Create an initial tab.
314 // If the intent is ACTION_VIEW and data is not null, the Browser is
315 // invoked to view the content by another application. In this case,
316 // the tab will be close when exit.
Michael Kolb14612442011-06-24 13:06:29 -0700317 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
Michael Kolb7bcafde2011-05-09 13:55:59 -0700318 Tab t = null;
319 if (urlData.isEmpty()) {
320 t = openTabToHomePage();
321 } else {
322 t = openTab(urlData);
323 }
324 if (t != null) {
325 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
326 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700327 WebView webView = t.getWebView();
328 if (extra != null) {
329 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
330 if (scale > 0 && scale <= 1000) {
331 webView.setInitialScale(scale);
332 }
333 }
John Reckd8c74522011-06-14 08:45:00 -0700334 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700335 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700336 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500337 mUi.needsRestoreAllTabs());
Michael Kolb1bf23132010-11-19 12:55:12 -0800338 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700339 // TabControl.restoreState() will create a new tab even if
340 // restoring the state fails.
341 setActiveTab(mTabControl.getCurrentTab());
John Reckdb22ec42011-06-29 11:31:24 -0700342 // Handle the intent
343 mIntentHandler.onNewIntent(intent);
Michael Kolb8233fac2010-10-26 16:08:53 -0700344 }
345 // clear up the thumbnail directory, which is no longer used;
346 // ideally this should only be run once after an upgrade from
347 // a previous version of the browser
348 new ClearThumbnails().execute(mTabControl.getThumbnailDir()
349 .listFiles());
350 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700351 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700352 if (jsFlags.trim().length() != 0) {
353 getCurrentWebView().setJsFlags(jsFlags);
354 }
John Reck439c9a52010-12-14 10:04:39 -0800355 if (BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
356 bookmarksOrHistoryPicker(false);
357 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 }
359
Michael Kolb1514bb72010-11-22 09:11:48 -0800360 @Override
361 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700362 return mFactory;
363 }
364
365 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800366 public void onSetWebView(Tab tab, WebView view) {
367 mUi.onSetWebView(tab, view);
368 }
369
370 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800371 public void createSubWindow(Tab tab) {
372 endActionMode();
373 WebView mainView = tab.getWebView();
374 WebView subView = mFactory.createWebView((mainView == null)
375 ? false
376 : mainView.isPrivateBrowsingEnabled());
377 mUi.createSubWindow(tab, subView);
378 }
379
380 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700381 public Context getContext() {
382 return mActivity;
383 }
384
385 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700386 public Activity getActivity() {
387 return mActivity;
388 }
389
390 void setUi(UI ui) {
391 mUi = ui;
392 }
393
394 BrowserSettings getSettings() {
395 return mSettings;
396 }
397
398 IntentHandler getIntentHandler() {
399 return mIntentHandler;
400 }
401
402 @Override
403 public UI getUi() {
404 return mUi;
405 }
406
407 int getMaxTabs() {
408 return mActivity.getResources().getInteger(R.integer.max_tabs);
409 }
410
411 @Override
412 public TabControl getTabControl() {
413 return mTabControl;
414 }
415
Michael Kolb1bf23132010-11-19 12:55:12 -0800416 @Override
417 public List<Tab> getTabs() {
418 return mTabControl.getTabs();
419 }
420
Michael Kolb8233fac2010-10-26 16:08:53 -0700421 // Open the icon database and retain all the icons for visited sites.
Ben Murdoch9446b932010-11-25 16:20:14 +0000422 // This is done on a background thread so as not to stall startup.
Michael Kolb8233fac2010-10-26 16:08:53 -0700423 private void retainIconsOnStartup() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000424 // WebIconDatabase needs to be retrieved on the UI thread so that if
425 // it has not been created successfully yet the Handler is started on the
426 // UI thread.
John Reckcadae722011-07-25 11:36:17 -0700427 Runnable task = new RetainIconsOnStartupTask(
428 mActivity, WebIconDatabase.getInstance());
429 BackgroundHandler.execute(task);
Ben Murdoch9446b932010-11-25 16:20:14 +0000430 }
431
John Reckcadae722011-07-25 11:36:17 -0700432 private static class RetainIconsOnStartupTask implements Runnable {
Ben Murdoch9446b932010-11-25 16:20:14 +0000433 private WebIconDatabase mDb;
John Reckcadae722011-07-25 11:36:17 -0700434 private Context mContext;
Ben Murdoch9446b932010-11-25 16:20:14 +0000435
John Reckcadae722011-07-25 11:36:17 -0700436 public RetainIconsOnStartupTask(Context context, WebIconDatabase db) {
Ben Murdoch9446b932010-11-25 16:20:14 +0000437 mDb = db;
John Reckcadae722011-07-25 11:36:17 -0700438 mContext = context;
Ben Murdoch9446b932010-11-25 16:20:14 +0000439 }
440
John Recka00cbbd2010-12-16 12:38:19 -0800441 @Override
John Reckcadae722011-07-25 11:36:17 -0700442 public void run() {
443 mDb.open(mContext.getDir("icons", 0).getPath());
Ben Murdoch9446b932010-11-25 16:20:14 +0000444 Cursor c = null;
445 try {
John Reckcadae722011-07-25 11:36:17 -0700446 c = Browser.getAllBookmarks(mContext.getContentResolver());
447 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
448 while (c.moveToNext()) {
449 String url = c.getString(urlIndex);
450 mDb.retainIconForPageUrl(url);
Ben Murdoch9446b932010-11-25 16:20:14 +0000451 }
John Reckcadae722011-07-25 11:36:17 -0700452 } catch (Throwable e) {
Ben Murdoch9446b932010-11-25 16:20:14 +0000453 Log.e(LOGTAG, "retainIconsOnStartup", e);
454 } finally {
455 if (c != null) c.close();
Michael Kolb8233fac2010-10-26 16:08:53 -0700456 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700457 }
458 }
459
460 private void startHandler() {
461 mHandler = new Handler() {
462
463 @Override
464 public void handleMessage(Message msg) {
465 switch (msg.what) {
466 case OPEN_BOOKMARKS:
467 bookmarksOrHistoryPicker(false);
468 break;
469 case FOCUS_NODE_HREF:
470 {
471 String url = (String) msg.getData().get("url");
472 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500473 String src = (String) msg.getData().get("src");
474 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700475 if (TextUtils.isEmpty(url)) {
476 break;
477 }
478 HashMap focusNodeMap = (HashMap) msg.obj;
479 WebView view = (WebView) focusNodeMap.get("webview");
480 // Only apply the action if the top window did not change.
481 if (getCurrentTopWebView() != view) {
482 break;
483 }
484 switch (msg.arg1) {
485 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700486 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700487 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500488 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700489 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500490 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500491 case R.id.open_newtab_context_menu_id:
492 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700493 openTab(url, parent,
494 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500495 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700496 case R.id.copy_link_context_menu_id:
497 copy(url);
498 break;
499 case R.id.save_link_context_menu_id:
500 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500501 DownloadHandler.onDownloadStartNoStream(
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000502 mActivity, url, null, null, null,
503 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 break;
505 }
506 break;
507 }
508
509 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700510 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 break;
512
513 case STOP_LOAD:
514 stopLoading();
515 break;
516
517 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700518 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700519 mWakeLock.release();
520 // if we reach here, Browser should be still in the
521 // background loading after WAKELOCK_TIMEOUT (5-min).
522 // To avoid burning the battery, stop loading.
523 mTabControl.stopAllLoading();
524 }
525 break;
526
527 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800528 Tab tab = (Tab) msg.obj;
529 if (tab != null) {
530 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700531 }
532 break;
533 }
534 }
535 };
536
537 }
538
John Reckef654f12011-07-12 16:42:08 -0700539 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200540 public Tab getCurrentTab() {
541 return mTabControl.getCurrentTab();
542 }
543
Michael Kolbba99c5d2010-11-29 14:57:41 -0800544 @Override
545 public void shareCurrentPage() {
546 shareCurrentPage(mTabControl.getCurrentTab());
547 }
548
549 private void shareCurrentPage(Tab tab) {
550 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800551 sharePage(mActivity, tab.getTitle(),
552 tab.getUrl(), tab.getFavicon(),
553 createScreenshot(tab.getWebView(),
554 getDesiredThumbnailWidth(mActivity),
555 getDesiredThumbnailHeight(mActivity)));
556 }
557 }
558
Michael Kolb8233fac2010-10-26 16:08:53 -0700559 /**
560 * Share a page, providing the title, url, favicon, and a screenshot. Uses
561 * an {@link Intent} to launch the Activity chooser.
562 * @param c Context used to launch a new Activity.
563 * @param title Title of the page. Stored in the Intent with
564 * {@link Intent#EXTRA_SUBJECT}
565 * @param url URL of the page. Stored in the Intent with
566 * {@link Intent#EXTRA_TEXT}
567 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
568 * with {@link Browser#EXTRA_SHARE_FAVICON}
569 * @param screenshot Bitmap of a screenshot of the page. Stored in the
570 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
571 */
572 static final void sharePage(Context c, String title, String url,
573 Bitmap favicon, Bitmap screenshot) {
574 Intent send = new Intent(Intent.ACTION_SEND);
575 send.setType("text/plain");
576 send.putExtra(Intent.EXTRA_TEXT, url);
577 send.putExtra(Intent.EXTRA_SUBJECT, title);
578 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
579 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
580 try {
581 c.startActivity(Intent.createChooser(send, c.getString(
582 R.string.choosertitle_sharevia)));
583 } catch(android.content.ActivityNotFoundException ex) {
584 // if no app handles it, do nothing
585 }
586 }
587
588 private void copy(CharSequence text) {
589 ClipboardManager cm = (ClipboardManager) mActivity
590 .getSystemService(Context.CLIPBOARD_SERVICE);
591 cm.setText(text);
592 }
593
594 // lifecycle
595
596 protected void onConfgurationChanged(Configuration config) {
597 mConfigChanged = true;
598 if (mPageDialogsHandler != null) {
599 mPageDialogsHandler.onConfigurationChanged(config);
600 }
601 mUi.onConfigurationChanged(config);
602 }
603
604 @Override
605 public void handleNewIntent(Intent intent) {
606 mIntentHandler.onNewIntent(intent);
607 }
608
609 protected void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800610 if (mUi.isCustomViewShowing()) {
611 hideCustomView();
612 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700613 if (mActivityPaused) {
614 Log.e(LOGTAG, "BrowserActivity is already paused.");
615 return;
616 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700617 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800618 Tab tab = mTabControl.getCurrentTab();
619 if (tab != null) {
620 tab.pause();
621 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700622 if (mWakeLock == null) {
623 PowerManager pm = (PowerManager) mActivity
624 .getSystemService(Context.POWER_SERVICE);
625 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
626 }
Michael Kolb70976932010-11-30 11:34:01 -0800627 mWakeLock.acquire();
628 mHandler.sendMessageDelayed(mHandler
629 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
630 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700631 }
632 mUi.onPause();
633 mNetworkHandler.onPause();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200634 mNfcHandler.onPause();
Michael Kolb8233fac2010-10-26 16:08:53 -0700635
636 WebView.disablePlatformNotifications();
John Reck378a4102011-06-09 16:23:01 -0700637 mCrashRecoveryHandler.backupState();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200638
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 }
640
John Reckaed9c542011-05-27 16:08:53 -0700641 void onSaveInstanceState(Bundle outState, boolean saveImages) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700642 // the default implementation requires each view to have an id. As the
643 // browser handles the state itself and it doesn't use id for the views,
644 // don't call the default implementation. Otherwise it will trigger the
645 // warning like this, "couldn't save which view has focus because the
646 // focused view XXX has no id".
647
648 // Save all the tabs
John Reck88026f42011-07-22 16:08:42 -0700649 mTabControl.saveState(outState, false);
John Reck24f18262011-06-17 14:47:20 -0700650 if (!outState.isEmpty()) {
651 // Save time so that we know how old incognito tabs (if any) are.
652 outState.putSerializable("lastActiveDate", Calendar.getInstance());
653 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700654 }
655
656 void onResume() {
657 if (!mActivityPaused) {
658 Log.e(LOGTAG, "BrowserActivity is already resumed.");
659 return;
660 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800662 Tab current = mTabControl.getCurrentTab();
663 if (current != null) {
664 current.resume();
665 resumeWebViewTimers(current);
666 }
John Reckf57c0292011-07-21 18:15:39 -0700667 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200668
Michael Kolb8233fac2010-10-26 16:08:53 -0700669 mUi.onResume();
670 mNetworkHandler.onResume();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200671 mNfcHandler.onResume();
Michael Kolb8233fac2010-10-26 16:08:53 -0700672 WebView.enablePlatformNotifications();
673 }
674
John Reckf57c0292011-07-21 18:15:39 -0700675 private void releaseWakeLock() {
676 if (mWakeLock != null && mWakeLock.isHeld()) {
677 mHandler.removeMessages(RELEASE_WAKELOCK);
678 mWakeLock.release();
679 }
680 }
681
Michael Kolb70976932010-11-30 11:34:01 -0800682 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800683 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800684 * @param tab guaranteed non-null
685 */
686 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700687 boolean inLoad = tab.inPageLoad();
688 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
689 CookieSyncManager.getInstance().startSync();
690 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100691 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700692 }
693 }
694
Michael Kolb70976932010-11-30 11:34:01 -0800695 /**
696 * Pause all WebView timers using the WebView of the given tab
697 * @param tab
698 * @return true if the timers are paused or tab is null
699 */
700 private boolean pauseWebViewTimers(Tab tab) {
701 if (tab == null) {
702 return true;
703 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700704 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100705 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700706 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700707 }
Michael Kolb70976932010-11-30 11:34:01 -0800708 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700709 }
710
711 void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800712 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700713 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
714 mUploadHandler = null;
715 }
716 if (mTabControl == null) return;
717 mUi.onDestroy();
718 // Remove the current tab and sub window
719 Tab t = mTabControl.getCurrentTab();
720 if (t != null) {
721 dismissSubWindow(t);
722 removeTab(t);
723 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500724 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700725 // Destroy all the tabs
726 mTabControl.destroy();
727 WebIconDatabase.getInstance().close();
728 // Stop watching the default geolocation permissions
729 mSystemAllowGeolocationOrigins.stop();
730 mSystemAllowGeolocationOrigins = null;
731 }
732
733 protected boolean isActivityPaused() {
734 return mActivityPaused;
735 }
736
737 protected void onLowMemory() {
738 mTabControl.freeMemory();
739 }
740
741 @Override
742 public boolean shouldShowErrorConsole() {
743 return mShouldShowErrorConsole;
744 }
745
746 protected void setShouldShowErrorConsole(boolean show) {
747 if (show == mShouldShowErrorConsole) {
748 // Nothing to do.
749 return;
750 }
751 mShouldShowErrorConsole = show;
752 Tab t = mTabControl.getCurrentTab();
753 if (t == null) {
754 // There is no current tab so we cannot toggle the error console
755 return;
756 }
757 mUi.setShouldShowErrorConsole(t, show);
758 }
759
760 @Override
761 public void stopLoading() {
762 mLoadStopped = true;
763 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700764 WebView w = getCurrentTopWebView();
765 w.stopLoading();
Michael Kolb8233fac2010-10-26 16:08:53 -0700766 mUi.onPageStopped(tab);
767 }
768
769 boolean didUserStopLoading() {
770 return mLoadStopped;
771 }
772
773 // WebViewController
774
775 @Override
John Reck324d4402011-01-11 16:56:42 -0800776 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700777
778 // We've started to load a new page. If there was a pending message
779 // to save a screenshot then we will now take the new page and save
780 // an incorrect screenshot. Therefore, remove any pending thumbnail
781 // messages from the queue.
782 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800783 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700784
785 // reset sync timer to avoid sync starts during loading a page
786 CookieSyncManager.getInstance().resetSync();
787
788 if (!mNetworkHandler.isNetworkUp()) {
789 view.setNetworkAvailable(false);
790 }
791
792 // when BrowserActivity just starts, onPageStarted may be called before
793 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
794 // to start the timer. As we won't switch tabs while an activity is in
795 // pause state, we can ensure calling resume and pause in pair.
796 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800797 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700798 }
799 mLoadStopped = false;
800 if (!mNetworkHandler.isNetworkUp()) {
801 mNetworkHandler.createAndShowNetworkDialog();
802 }
803 endActionMode();
804
John Reck30c714c2010-12-16 17:30:34 -0800805 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700806
John Reck324d4402011-01-11 16:56:42 -0800807 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700808 // update the bookmark database for favicon
809 maybeUpdateFavicon(tab, null, url, favicon);
810
811 Performance.tracePageStart(url);
812
813 // Performance probe
814 if (false) {
815 Performance.onPageStarted();
816 }
817
818 }
819
820 @Override
John Reck324d4402011-01-11 16:56:42 -0800821 public void onPageFinished(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800822 mUi.onTabDataChanged(tab);
John Reck324d4402011-01-11 16:56:42 -0800823 if (!tab.isPrivateBrowsingEnabled()
John Reckd8c74522011-06-14 08:45:00 -0700824 && !TextUtils.isEmpty(tab.getUrl())
825 && !tab.isSnapshot()) {
John Recka1696282011-07-08 14:10:37 -0700826 // Only update the bookmark screenshot if the user did not
827 // cancel the load early and there is not already
828 // a pending update for the tab.
Michael Kolb8233fac2010-10-26 16:08:53 -0700829 if (tab.inForeground() && !didUserStopLoading()
830 || !tab.inForeground()) {
John Recka1696282011-07-08 14:10:37 -0700831 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
832 mHandler.sendMessageDelayed(mHandler.obtainMessage(
833 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
834 500);
835 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700836 }
837 }
838 // pause the WebView timer and release the wake lock if it is finished
839 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800840 if (mActivityPaused && pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700841 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200843
Michael Kolb8233fac2010-10-26 16:08:53 -0700844 // Performance probe
845 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800846 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 }
848
849 Performance.tracePageFinished();
850 }
851
852 @Override
John Reck30c714c2010-12-16 17:30:34 -0800853 public void onProgressChanged(Tab tab) {
854 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700855
856 if (newProgress == 100) {
857 CookieSyncManager.getInstance().sync();
858 // onProgressChanged() may continue to be called after the main
859 // frame has finished loading, as any remaining sub frames continue
860 // to load. We'll only get called once though with newProgress as
861 // 100 when everything is loaded. (onPageFinished is called once
862 // when the main frame completes loading regardless of the state of
863 // any sub frames so calls to onProgressChanges may continue after
864 // onPageFinished has executed)
865 if (mInLoad) {
866 mInLoad = false;
867 updateInLoadMenuItems(mCachedMenu);
868 }
869 } else {
870 if (!mInLoad) {
871 // onPageFinished may have already been called but a subframe is
872 // still loading and updating the progress. Reset mInLoad and
873 // update the menu items.
874 mInLoad = true;
875 updateInLoadMenuItems(mCachedMenu);
876 }
877 }
John Reck30c714c2010-12-16 17:30:34 -0800878 mUi.onProgressChanged(tab);
879 }
880
881 @Override
882 public void onUpdatedLockIcon(Tab tab) {
883 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700884 }
885
886 @Override
887 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800888 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800889 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800890 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700891 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
892 return;
893 }
894 // Update the title in the history database if not in private browsing mode
895 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -0700896 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700897 }
898 }
899
900 @Override
901 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800902 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700903 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
904 }
905
906 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800907 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
908 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700909 }
910
911 @Override
912 public boolean shouldOverrideKeyEvent(KeyEvent event) {
913 if (mMenuIsDown) {
914 // only check shortcut key when MENU is held
915 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
916 event);
917 } else {
918 return false;
919 }
920 }
921
922 @Override
923 public void onUnhandledKeyEvent(KeyEvent event) {
924 if (!isActivityPaused()) {
925 if (event.getAction() == KeyEvent.ACTION_DOWN) {
926 mActivity.onKeyDown(event.getKeyCode(), event);
927 } else {
928 mActivity.onKeyUp(event.getKeyCode(), event);
929 }
930 }
931 }
932
933 @Override
John Reck324d4402011-01-11 16:56:42 -0800934 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700935 // Don't save anything in private browsing mode
936 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800937 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700938
John Reck324d4402011-01-11 16:56:42 -0800939 if (TextUtils.isEmpty(url)
940 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700941 return;
942 }
John Reckf57c0292011-07-21 18:15:39 -0700943 DataController.getInstance(mActivity).updateVisitedHistory(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700944 WebIconDatabase.getInstance().retainIconForPageUrl(url);
John Reck847b5322011-04-14 17:02:18 -0700945 if (!mActivityPaused) {
946 // Since we clear the state in onPause, don't backup the current
947 // state if we are already paused
948 mCrashRecoveryHandler.backupState();
949 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700950 }
951
952 @Override
953 public void getVisitedHistory(final ValueCallback<String[]> callback) {
954 AsyncTask<Void, Void, String[]> task =
955 new AsyncTask<Void, Void, String[]>() {
956 @Override
957 public String[] doInBackground(Void... unused) {
958 return Browser.getVisitedHistory(mActivity.getContentResolver());
959 }
960 @Override
961 public void onPostExecute(String[] result) {
962 callback.onReceiveValue(result);
963 }
964 };
965 task.execute();
966 }
967
968 @Override
969 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
970 final HttpAuthHandler handler, final String host,
971 final String realm) {
972 String username = null;
973 String password = null;
974
975 boolean reuseHttpAuthUsernamePassword
976 = handler.useHttpAuthUsernamePassword();
977
978 if (reuseHttpAuthUsernamePassword && view != null) {
979 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
980 if (credentials != null && credentials.length == 2) {
981 username = credentials[0];
982 password = credentials[1];
983 }
984 }
985
986 if (username != null && password != null) {
987 handler.proceed(username, password);
988 } else {
989 if (tab.inForeground()) {
990 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
991 } else {
992 handler.cancel();
993 }
994 }
995 }
996
997 @Override
998 public void onDownloadStart(Tab tab, String url, String userAgent,
999 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001000 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -05001001 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001002 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
1003 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001004 // This Tab was opened for the sole purpose of downloading a
1005 // file. Remove it.
1006 if (tab == mTabControl.getCurrentTab()) {
1007 // In this case, the Tab is still on top.
1008 goBackOnePageOrQuit();
1009 } else {
1010 // In this case, it is not.
1011 closeTab(tab);
1012 }
1013 }
1014 }
1015
1016 @Override
1017 public Bitmap getDefaultVideoPoster() {
1018 return mUi.getDefaultVideoPoster();
1019 }
1020
1021 @Override
1022 public View getVideoLoadingProgressView() {
1023 return mUi.getVideoLoadingProgressView();
1024 }
1025
1026 @Override
1027 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1028 SslError error) {
1029 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1030 }
1031
Patrick Scott92066772011-03-10 08:46:27 -05001032 @Override
1033 public void showAutoLogin(Tab tab) {
1034 assert tab.inForeground();
1035 // Update the title bar to show the auto-login request.
1036 mUi.showAutoLogin(tab);
1037 }
1038
1039 @Override
1040 public void hideAutoLogin(Tab tab) {
1041 assert tab.inForeground();
1042 mUi.hideAutoLogin(tab);
1043 }
1044
Michael Kolb8233fac2010-10-26 16:08:53 -07001045 // helper method
1046
1047 /*
1048 * Update the favorites icon if the private browsing isn't enabled and the
1049 * icon is valid.
1050 */
1051 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1052 final String url, Bitmap favicon) {
1053 if (favicon == null) {
1054 return;
1055 }
1056 if (!tab.isPrivateBrowsingEnabled()) {
1057 Bookmarks.updateFavicon(mActivity
1058 .getContentResolver(), originalUrl, url, favicon);
1059 }
1060 }
1061
Leon Scroggins4cd97792010-12-03 15:31:56 -05001062 @Override
1063 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001064 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001065 mUi.bookmarkedStatusHasChanged(tab);
1066 }
1067
Michael Kolb8233fac2010-10-26 16:08:53 -07001068 // end WebViewController
1069
1070 protected void pageUp() {
1071 getCurrentTopWebView().pageUp(false);
1072 }
1073
1074 protected void pageDown() {
1075 getCurrentTopWebView().pageDown(false);
1076 }
1077
1078 // callback from phone title bar
1079 public void editUrl() {
1080 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08001081 mUi.editUrl(false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001082 }
1083
Michael Kolbcfa3af52010-12-14 10:36:11 -08001084 public void startVoiceSearch() {
1085 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1086 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
1087 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
1088 intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
1089 mActivity.getComponentName().flattenToString());
1090 intent.putExtra(SEND_APP_ID_EXTRA, false);
Michael Kolb17c4eba2011-01-10 13:10:07 -08001091 intent.putExtra(RecognizerIntent.EXTRA_WEB_SEARCH_ONLY, true);
Michael Kolbcfa3af52010-12-14 10:36:11 -08001092 mActivity.startActivity(intent);
1093 }
1094
Michael Kolb11d19782011-03-20 10:17:40 -07001095 @Override
1096 public void activateVoiceSearchMode(String title, List<String> results) {
1097 mUi.showVoiceTitleBar(title, results);
Michael Kolb8233fac2010-10-26 16:08:53 -07001098 }
1099
1100 public void revertVoiceSearchMode(Tab tab) {
1101 mUi.revertVoiceTitleBar(tab);
1102 }
1103
Michael Kolb736990c2011-03-20 10:01:20 -07001104 public boolean supportsVoiceSearch() {
John Reck35e9dd62011-04-25 09:01:54 -07001105 SearchEngine searchEngine = getSettings().getSearchEngine();
Michael Kolb736990c2011-03-20 10:01:20 -07001106 return (searchEngine != null && searchEngine.supportsVoiceSearch());
1107 }
1108
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001109 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001110 WebChromeClient.CustomViewCallback callback) {
1111 if (tab.inForeground()) {
1112 if (mUi.isCustomViewShowing()) {
1113 callback.onCustomViewHidden();
1114 return;
1115 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001116 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001117 // Save the menu state and set it to empty while the custom
1118 // view is showing.
1119 mOldMenuState = mMenuState;
1120 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001121 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001122 }
1123 }
1124
1125 @Override
1126 public void hideCustomView() {
1127 if (mUi.isCustomViewShowing()) {
1128 mUi.onHideCustomView();
1129 // Reset the old menu state.
1130 mMenuState = mOldMenuState;
1131 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001132 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001133 }
1134 }
1135
1136 protected void onActivityResult(int requestCode, int resultCode,
1137 Intent intent) {
1138 if (getCurrentTopWebView() == null) return;
1139 switch (requestCode) {
1140 case PREFERENCES_PAGE:
1141 if (resultCode == Activity.RESULT_OK && intent != null) {
1142 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001143 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001144 mTabControl.removeParentChildRelationShips();
1145 }
1146 }
1147 break;
1148 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001149 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001150 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001151 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001152 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001153 case AUTOFILL_SETUP:
1154 // Determine whether a profile was actually set up or not
1155 // and if so, send the message back to the WebTextView to
1156 // fill the form with the new profile.
1157 if (getSettings().getAutoFillProfile() != null) {
1158 mAutoFillSetupMessage.sendToTarget();
1159 mAutoFillSetupMessage = null;
1160 }
1161 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001162 case COMBO_VIEW:
1163 if (intent == null || resultCode != Activity.RESULT_OK) {
1164 break;
1165 }
1166 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1167 Tab t = getCurrentTab();
1168 Uri uri = intent.getData();
1169 loadUrl(t, uri.toString());
1170 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1171 String[] urls = intent.getStringArrayExtra(
1172 ComboViewActivity.EXTRA_OPEN_ALL);
1173 Tab parent = getCurrentTab();
1174 for (String url : urls) {
1175 parent = openTab(url, parent,
1176 !mSettings.openInBackground(), true);
1177 }
1178 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1179 long id = intent.getLongExtra(
1180 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1181 if (id >= 0) {
1182 createNewSnapshotTab(id, true);
1183 }
1184 }
1185 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001186 default:
1187 break;
1188 }
1189 getCurrentTopWebView().requestFocus();
1190 }
1191
1192 /**
1193 * Open the Go page.
1194 * @param startWithHistory If true, open starting on the history tab.
1195 * Otherwise, start with the bookmarks tab.
1196 */
1197 @Override
1198 public void bookmarksOrHistoryPicker(boolean startWithHistory) {
1199 if (mTabControl.getCurrentWebView() == null) {
1200 return;
1201 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001202 // clear action mode
1203 if (isInCustomActionMode()) {
1204 endActionMode();
1205 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001206 Bundle extras = new Bundle();
1207 // Disable opening in a new window if we have maxed out the windows
1208 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1209 !mTabControl.canCreateNewTab());
John Reck2bc80422011-06-30 15:11:49 -07001210 mUi.showComboView(startWithHistory
1211 ? ComboViews.History : ComboViews.Bookmarks, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001212 }
1213
1214 // combo view callbacks
1215
1216 /**
1217 * callback from ComboPage when clear history is requested
1218 */
1219 public void onRemoveParentChildRelationships() {
1220 mTabControl.removeParentChildRelationShips();
1221 }
1222
Michael Kolb8233fac2010-10-26 16:08:53 -07001223 // key handling
1224 protected void onBackKey() {
1225 if (!mUi.onBackKey()) {
1226 WebView subwindow = mTabControl.getCurrentSubWindow();
1227 if (subwindow != null) {
1228 if (subwindow.canGoBack()) {
1229 subwindow.goBack();
1230 } else {
1231 dismissSubWindow(mTabControl.getCurrentTab());
1232 }
1233 } else {
1234 goBackOnePageOrQuit();
1235 }
1236 }
1237 }
1238
Michael Kolb4bd767d2011-05-27 11:33:55 -07001239 protected boolean onMenuKey() {
1240 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001241 }
1242
Michael Kolb8233fac2010-10-26 16:08:53 -07001243 // menu handling and state
1244 // TODO: maybe put into separate handler
1245
1246 protected boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001247 if (mMenuState == EMPTY_MENU) {
1248 return false;
1249 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001250 MenuInflater inflater = mActivity.getMenuInflater();
1251 inflater.inflate(R.menu.browser, menu);
1252 updateInLoadMenuItems(menu);
1253 // hold on to the menu reference here; it is used by the page callbacks
1254 // to update the menu based on loading state
1255 mCachedMenu = menu;
1256 return true;
1257 }
1258
1259 protected void onCreateContextMenu(ContextMenu menu, View v,
1260 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001261 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001262 return;
1263 }
1264 if (!(v instanceof WebView)) {
1265 return;
1266 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001267 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001268 WebView.HitTestResult result = webview.getHitTestResult();
1269 if (result == null) {
1270 return;
1271 }
1272
1273 int type = result.getType();
1274 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1275 Log.w(LOGTAG,
1276 "We should not show context menu when nothing is touched");
1277 return;
1278 }
1279 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1280 // let TextView handles context menu
1281 return;
1282 }
1283
1284 // Note, http://b/issue?id=1106666 is requesting that
1285 // an inflated menu can be used again. This is not available
1286 // yet, so inflate each time (yuk!)
1287 MenuInflater inflater = mActivity.getMenuInflater();
1288 inflater.inflate(R.menu.browsercontext, menu);
1289
1290 // Show the correct menu group
1291 final String extra = result.getExtra();
1292 menu.setGroupVisible(R.id.PHONE_MENU,
1293 type == WebView.HitTestResult.PHONE_TYPE);
1294 menu.setGroupVisible(R.id.EMAIL_MENU,
1295 type == WebView.HitTestResult.EMAIL_TYPE);
1296 menu.setGroupVisible(R.id.GEO_MENU,
1297 type == WebView.HitTestResult.GEO_TYPE);
1298 menu.setGroupVisible(R.id.IMAGE_MENU,
1299 type == WebView.HitTestResult.IMAGE_TYPE
1300 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1301 menu.setGroupVisible(R.id.ANCHOR_MENU,
1302 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1303 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001304 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1305 || type == WebView.HitTestResult.PHONE_TYPE
1306 || type == WebView.HitTestResult.EMAIL_TYPE
1307 || type == WebView.HitTestResult.GEO_TYPE;
1308 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1309 if (hitText) {
1310 menu.findItem(R.id.select_text_menu_id)
1311 .setOnMenuItemClickListener(new SelectText(webview));
1312 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001313 // Setup custom handling depending on the type
1314 switch (type) {
1315 case WebView.HitTestResult.PHONE_TYPE:
1316 menu.setHeaderTitle(Uri.decode(extra));
1317 menu.findItem(R.id.dial_context_menu_id).setIntent(
1318 new Intent(Intent.ACTION_VIEW, Uri
1319 .parse(WebView.SCHEME_TEL + extra)));
1320 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1321 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1322 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1323 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1324 addIntent);
1325 menu.findItem(R.id.copy_phone_context_menu_id)
1326 .setOnMenuItemClickListener(
1327 new Copy(extra));
1328 break;
1329
1330 case WebView.HitTestResult.EMAIL_TYPE:
1331 menu.setHeaderTitle(extra);
1332 menu.findItem(R.id.email_context_menu_id).setIntent(
1333 new Intent(Intent.ACTION_VIEW, Uri
1334 .parse(WebView.SCHEME_MAILTO + extra)));
1335 menu.findItem(R.id.copy_mail_context_menu_id)
1336 .setOnMenuItemClickListener(
1337 new Copy(extra));
1338 break;
1339
1340 case WebView.HitTestResult.GEO_TYPE:
1341 menu.setHeaderTitle(extra);
1342 menu.findItem(R.id.map_context_menu_id).setIntent(
1343 new Intent(Intent.ACTION_VIEW, Uri
1344 .parse(WebView.SCHEME_GEO
1345 + URLEncoder.encode(extra))));
1346 menu.findItem(R.id.copy_geo_context_menu_id)
1347 .setOnMenuItemClickListener(
1348 new Copy(extra));
1349 break;
1350
1351 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1352 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001353 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001354 // decide whether to show the open link in new tab option
1355 boolean showNewTab = mTabControl.canCreateNewTab();
1356 MenuItem newTabItem
1357 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001358 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001359 ? R.string.contextmenu_openlink_newwindow_background
1360 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001361 newTabItem.setVisible(showNewTab);
1362 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001363 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1364 newTabItem.setOnMenuItemClickListener(
1365 new MenuItem.OnMenuItemClickListener() {
1366 @Override
1367 public boolean onMenuItemClick(MenuItem item) {
1368 final HashMap<String, WebView> hrefMap =
1369 new HashMap<String, WebView>();
1370 hrefMap.put("webview", webview);
1371 final Message msg = mHandler.obtainMessage(
1372 FOCUS_NODE_HREF,
1373 R.id.open_newtab_context_menu_id,
1374 0, hrefMap);
1375 webview.requestFocusNodeHref(msg);
1376 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001377 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001378 });
1379 } else {
1380 newTabItem.setOnMenuItemClickListener(
1381 new MenuItem.OnMenuItemClickListener() {
1382 @Override
1383 public boolean onMenuItemClick(MenuItem item) {
1384 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001385 openTab(extra, parent,
1386 !mSettings.openInBackground(),
1387 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001388 return true;
1389 }
1390 });
1391 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001392 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001393 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1394 break;
1395 }
1396 // otherwise fall through to handle image part
1397 case WebView.HitTestResult.IMAGE_TYPE:
1398 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1399 menu.setHeaderTitle(extra);
1400 }
1401 menu.findItem(R.id.view_image_context_menu_id).setIntent(
1402 new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));
1403 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001404 setOnMenuItemClickListener(
1405 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001406 menu.findItem(R.id.set_wallpaper_context_menu_id).
1407 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1408 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001409 break;
1410
1411 default:
1412 Log.w(LOGTAG, "We should not get here.");
1413 break;
1414 }
1415 //update the ui
1416 mUi.onContextMenuCreated(menu);
1417 }
1418
1419 /**
1420 * As the menu can be open when loading state changes
1421 * we must manually update the state of the stop/reload menu
1422 * item
1423 */
1424 private void updateInLoadMenuItems(Menu menu) {
1425 if (menu == null) {
1426 return;
1427 }
1428 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
1429 MenuItem src = mInLoad ?
1430 menu.findItem(R.id.stop_menu_id):
1431 menu.findItem(R.id.reload_menu_id);
1432 if (src != null) {
1433 dest.setIcon(src.getIcon());
1434 dest.setTitle(src.getTitle());
1435 }
1436 }
1437
John Reckb3417f02011-01-14 11:01:05 -08001438 boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001439 // Note: setVisible will decide whether an item is visible; while
1440 // setEnabled() will decide whether an item is enabled, which also means
1441 // whether the matching shortcut key will function.
1442 switch (mMenuState) {
1443 case EMPTY_MENU:
1444 if (mCurrentMenuState != mMenuState) {
1445 menu.setGroupVisible(R.id.MAIN_MENU, false);
1446 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1447 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1448 }
1449 break;
1450 default:
1451 if (mCurrentMenuState != mMenuState) {
1452 menu.setGroupVisible(R.id.MAIN_MENU, true);
1453 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1454 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1455 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001456 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001457 break;
1458 }
1459 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001460 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001461 }
1462
Michael Kolb4bf79712011-07-14 14:18:12 -07001463 @Override
1464 public void updateMenuState(Tab tab, Menu menu) {
1465 boolean canGoBack = false;
1466 boolean canGoForward = false;
1467 boolean isHome = false;
1468 if (tab != null) {
1469 canGoBack = tab.canGoBack();
1470 canGoForward = tab.canGoForward();
1471 isHome = mSettings.getHomePage().equals(tab.getUrl());
1472 }
1473 final MenuItem back = menu.findItem(R.id.back_menu_id);
1474 back.setEnabled(canGoBack);
1475
1476 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1477 home.setEnabled(!isHome);
1478
1479 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1480 forward.setEnabled(canGoForward);
1481
1482 final MenuItem source = menu.findItem(mInLoad ? R.id.stop_menu_id : R.id.reload_menu_id);
1483 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001484 if (source != null && dest != null) {
1485 dest.setTitle(source.getTitle());
1486 dest.setIcon(source.getIcon());
1487 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001488
1489 // decide whether to show the share link option
1490 PackageManager pm = mActivity.getPackageManager();
1491 Intent send = new Intent(Intent.ACTION_SEND);
1492 send.setType("text/plain");
1493 ResolveInfo ri = pm.resolveActivity(send,
1494 PackageManager.MATCH_DEFAULT_ONLY);
1495 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1496
1497 boolean isNavDump = mSettings.enableNavDump();
1498 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1499 nav.setVisible(isNavDump);
1500 nav.setEnabled(isNavDump);
1501
1502 boolean showDebugSettings = mSettings.isDebugEnabled();
1503 final MenuItem counter = menu.findItem(R.id.dump_counters_menu_id);
1504 counter.setVisible(showDebugSettings);
1505 counter.setEnabled(showDebugSettings);
1506
1507 MenuItem saveSnapshot = menu.findItem(R.id.save_snapshot_menu_id);
1508 saveSnapshot.setVisible(tab != null && !tab.isSnapshot());
1509 }
1510
Michael Kolb8233fac2010-10-26 16:08:53 -07001511 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001512 if (null == getCurrentTopWebView()) {
1513 return false;
1514 }
1515 if (mMenuIsDown) {
1516 // The shortcut action consumes the MENU. Even if it is still down,
1517 // it won't trigger the next shortcut action. In the case of the
1518 // shortcut action triggering a new activity, like Bookmarks, we
1519 // won't get onKeyUp for MENU. So it is important to reset it here.
1520 mMenuIsDown = false;
1521 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001522 if (mUi.onOptionsItemSelected(item)) {
1523 // ui callback handled it
1524 return true;
1525 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001526 switch (item.getItemId()) {
1527 // -- Main menu
1528 case R.id.new_tab_menu_id:
1529 openTabToHomePage();
1530 break;
1531
1532 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001533 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001534 break;
1535
1536 case R.id.goto_menu_id:
1537 editUrl();
1538 break;
1539
1540 case R.id.bookmarks_menu_id:
1541 bookmarksOrHistoryPicker(false);
1542 break;
1543
Michael Kolb8233fac2010-10-26 16:08:53 -07001544 case R.id.add_bookmark_menu_id:
John Reckd3e4d5b2011-07-13 15:48:43 -07001545 mActivity.startActivity(createBookmarkCurrentPageIntent(false));
Michael Kolb8233fac2010-10-26 16:08:53 -07001546 break;
1547
1548 case R.id.stop_reload_menu_id:
1549 if (mInLoad) {
1550 stopLoading();
1551 } else {
1552 getCurrentTopWebView().reload();
1553 }
1554 break;
1555
1556 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001557 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001558 break;
1559
1560 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001561 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001562 break;
1563
1564 case R.id.close_menu_id:
1565 // Close the subwindow if it exists.
1566 if (mTabControl.getCurrentSubWindow() != null) {
1567 dismissSubWindow(mTabControl.getCurrentTab());
1568 break;
1569 }
1570 closeCurrentTab();
1571 break;
1572
1573 case R.id.homepage_menu_id:
1574 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001575 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001576 break;
1577
1578 case R.id.preferences_menu_id:
1579 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1580 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1581 getCurrentTopWebView().getUrl());
1582 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1583 break;
1584
1585 case R.id.find_menu_id:
Leon Scroggins1c00d5e2011-01-04 10:45:58 -05001586 getCurrentTopWebView().showFindDialog(null, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001587 break;
1588
John Reck2bc80422011-06-30 15:11:49 -07001589 case R.id.save_snapshot_menu_id:
1590 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001591 if (source == null) break;
John Reckd8c74522011-06-14 08:45:00 -07001592 final ContentResolver cr = mActivity.getContentResolver();
1593 final ContentValues values = source.createSnapshotValues();
John Reck2bc80422011-06-30 15:11:49 -07001594 if (values != null) {
1595 new AsyncTask<Tab, Void, Long>() {
John Reckd8c74522011-06-14 08:45:00 -07001596
John Reck2bc80422011-06-30 15:11:49 -07001597 @Override
1598 protected Long doInBackground(Tab... params) {
1599 Uri result = cr.insert(Snapshots.CONTENT_URI, values);
1600 long id = ContentUris.parseId(result);
1601 return id;
John Reckd8c74522011-06-14 08:45:00 -07001602 }
John Reckf33b1632011-06-04 20:00:23 -07001603
John Reck2bc80422011-06-30 15:11:49 -07001604 @Override
1605 protected void onPostExecute(Long id) {
1606 Bundle b = new Bundle();
1607 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1608 mUi.showComboView(ComboViews.Snapshots, b);
1609 };
1610 }.execute(source);
1611 } else {
1612 Toast.makeText(mActivity, R.string.snapshot_failed,
Leon Scrogginsac993842011-02-02 12:54:07 -05001613 Toast.LENGTH_SHORT).show();
Leon Scrogginsac993842011-02-02 12:54:07 -05001614 }
Leon Scrogginsac993842011-02-02 12:54:07 -05001615 break;
1616
Michael Kolb8233fac2010-10-26 16:08:53 -07001617 case R.id.page_info_menu_id:
Huahui Wuae0c0412011-06-28 10:17:05 -07001618 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
Michael Kolb8233fac2010-10-26 16:08:53 -07001619 break;
1620
1621 case R.id.classic_history_menu_id:
1622 bookmarksOrHistoryPicker(true);
1623 break;
1624
1625 case R.id.title_bar_share_page_url:
1626 case R.id.share_page_menu_id:
1627 Tab currentTab = mTabControl.getCurrentTab();
1628 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001629 return false;
1630 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001631 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001632 break;
1633
1634 case R.id.dump_nav_menu_id:
1635 getCurrentTopWebView().debugDump();
1636 break;
1637
1638 case R.id.dump_counters_menu_id:
1639 getCurrentTopWebView().dumpV8Counters();
1640 break;
1641
1642 case R.id.zoom_in_menu_id:
1643 getCurrentTopWebView().zoomIn();
1644 break;
1645
1646 case R.id.zoom_out_menu_id:
1647 getCurrentTopWebView().zoomOut();
1648 break;
1649
1650 case R.id.view_downloads_menu_id:
1651 viewDownloads();
1652 break;
1653
1654 case R.id.window_one_menu_id:
1655 case R.id.window_two_menu_id:
1656 case R.id.window_three_menu_id:
1657 case R.id.window_four_menu_id:
1658 case R.id.window_five_menu_id:
1659 case R.id.window_six_menu_id:
1660 case R.id.window_seven_menu_id:
1661 case R.id.window_eight_menu_id:
1662 {
1663 int menuid = item.getItemId();
1664 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1665 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1666 Tab desiredTab = mTabControl.getTab(id);
1667 if (desiredTab != null &&
1668 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001669 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001670 }
1671 break;
1672 }
1673 }
1674 }
1675 break;
1676
1677 default:
1678 return false;
1679 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001680 return true;
1681 }
1682
1683 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001684 // Let the History and Bookmark fragments handle menus they created.
1685 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1686 return false;
1687 }
1688
Michael Kolb8233fac2010-10-26 16:08:53 -07001689 int id = item.getItemId();
1690 boolean result = true;
1691 switch (id) {
1692 // For the context menu from the title bar
1693 case R.id.title_bar_copy_page_url:
1694 Tab currentTab = mTabControl.getCurrentTab();
1695 if (null == currentTab) {
1696 result = false;
1697 break;
1698 }
1699 WebView mainView = currentTab.getWebView();
1700 if (null == mainView) {
1701 result = false;
1702 break;
1703 }
1704 copy(mainView.getUrl());
1705 break;
1706 // -- Browser context menu
1707 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001708 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001709 case R.id.copy_link_context_menu_id:
1710 final WebView webView = getCurrentTopWebView();
1711 if (null == webView) {
1712 result = false;
1713 break;
1714 }
1715 final HashMap<String, WebView> hrefMap =
1716 new HashMap<String, WebView>();
1717 hrefMap.put("webview", webView);
1718 final Message msg = mHandler.obtainMessage(
1719 FOCUS_NODE_HREF, id, 0, hrefMap);
1720 webView.requestFocusNodeHref(msg);
1721 break;
1722
1723 default:
1724 // For other context menus
1725 result = onOptionsItemSelected(item);
1726 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001727 return result;
1728 }
1729
1730 /**
1731 * support programmatically opening the context menu
1732 */
1733 public void openContextMenu(View view) {
1734 mActivity.openContextMenu(view);
1735 }
1736
1737 /**
1738 * programmatically open the options menu
1739 */
1740 public void openOptionsMenu() {
1741 mActivity.openOptionsMenu();
1742 }
1743
1744 public boolean onMenuOpened(int featureId, Menu menu) {
1745 if (mOptionsMenuOpen) {
1746 if (mConfigChanged) {
1747 // We do not need to make any changes to the state of the
1748 // title bar, since the only thing that happened was a
1749 // change in orientation
1750 mConfigChanged = false;
1751 } else {
1752 if (!mExtendedMenuOpen) {
1753 mExtendedMenuOpen = true;
1754 mUi.onExtendedMenuOpened();
1755 } else {
1756 // Switching the menu back to icon view, so show the
1757 // title bar once again.
1758 mExtendedMenuOpen = false;
1759 mUi.onExtendedMenuClosed(mInLoad);
Michael Kolb8233fac2010-10-26 16:08:53 -07001760 }
1761 }
1762 } else {
1763 // The options menu is closed, so open it, and show the title
1764 mOptionsMenuOpen = true;
1765 mConfigChanged = false;
1766 mExtendedMenuOpen = false;
1767 mUi.onOptionsMenuOpened();
1768 }
1769 return true;
1770 }
1771
1772 public void onOptionsMenuClosed(Menu menu) {
1773 mOptionsMenuOpen = false;
1774 mUi.onOptionsMenuClosed(mInLoad);
1775 }
1776
1777 public void onContextMenuClosed(Menu menu) {
1778 mUi.onContextMenuClosed(menu, mInLoad);
1779 }
1780
1781 // Helper method for getting the top window.
1782 @Override
1783 public WebView getCurrentTopWebView() {
1784 return mTabControl.getCurrentTopWebView();
1785 }
1786
1787 @Override
1788 public WebView getCurrentWebView() {
1789 return mTabControl.getCurrentWebView();
1790 }
1791
1792 /*
1793 * This method is called as a result of the user selecting the options
1794 * menu to see the download window. It shows the download window on top of
1795 * the current window.
1796 */
1797 void viewDownloads() {
1798 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1799 mActivity.startActivity(intent);
1800 }
1801
John Reck30b065e2011-07-19 10:58:05 -07001802 int getActionModeHeight() {
1803 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1804 new int[] { android.R.attr.actionBarSize });
1805 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1806 actionBarSizeTypedArray.recycle();
1807 return size;
1808 }
1809
Michael Kolb8233fac2010-10-26 16:08:53 -07001810 // action mode
1811
1812 void onActionModeStarted(ActionMode mode) {
1813 mUi.onActionModeStarted(mode);
1814 mActionMode = mode;
John Reck30b065e2011-07-19 10:58:05 -07001815 if (mSimulateActionBarOverlayMode) {
1816 WebView web = getCurrentWebView();
1817 // Simulate overlay mode by scrolling the webview the amount it will be
1818 // pushed down. Actual overlay mode doesn't work for us as otherwise
1819 // the CAB will, well, overlay the content, which breaks things like
1820 // find on page.
1821 int scrollBy = getActionModeHeight();
1822 web.scrollBy(0, scrollBy);
1823 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001824 }
1825
1826 /*
1827 * True if a custom ActionMode (i.e. find or select) is in use.
1828 */
1829 @Override
1830 public boolean isInCustomActionMode() {
1831 return mActionMode != null;
1832 }
1833
1834 /*
1835 * End the current ActionMode.
1836 */
1837 @Override
1838 public void endActionMode() {
1839 if (mActionMode != null) {
1840 mActionMode.finish();
1841 }
1842 }
1843
1844 /*
1845 * Called by find and select when they are finished. Replace title bars
1846 * as necessary.
1847 */
1848 public void onActionModeFinished(ActionMode mode) {
1849 if (!isInCustomActionMode()) return;
1850 mUi.onActionModeFinished(mInLoad);
1851 mActionMode = null;
John Reck30b065e2011-07-19 10:58:05 -07001852 if (mSimulateActionBarOverlayMode) {
1853 WebView web = getCurrentWebView();
1854 int scrollBy = getActionModeHeight();
1855 web.scrollBy(0, -scrollBy);
1856 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001857 }
1858
1859 boolean isInLoad() {
1860 return mInLoad;
1861 }
1862
1863 // bookmark handling
1864
1865 /**
1866 * add the current page as a bookmark to the given folder id
1867 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07001868 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001869 * bookmarked, and if it is, edit that bookmark. If false, and
1870 * the site is already bookmarked, do not attempt to edit the
1871 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001872 */
1873 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07001874 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001875 Intent i = new Intent(mActivity,
1876 AddBookmarkPage.class);
1877 WebView w = getCurrentTopWebView();
1878 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1879 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1880 String touchIconUrl = w.getTouchIconUrl();
1881 if (touchIconUrl != null) {
1882 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1883 WebSettings settings = w.getSettings();
1884 if (settings != null) {
1885 i.putExtra(AddBookmarkPage.USER_AGENT,
1886 settings.getUserAgentString());
1887 }
1888 }
1889 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1890 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1891 getDesiredThumbnailHeight(mActivity)));
1892 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07001893 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001894 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1895 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001896 // Put the dialog at the upper right of the screen, covering the
1897 // star on the title bar.
1898 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07001899 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07001900 }
1901
1902 // file chooser
1903 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
1904 mUploadHandler = new UploadHandler(this);
1905 mUploadHandler.openFileChooser(uploadMsg, acceptType);
1906 }
1907
1908 // thumbnails
1909
1910 /**
1911 * Return the desired width for thumbnail screenshots, which are stored in
1912 * the database, and used on the bookmarks screen.
1913 * @param context Context for finding out the density of the screen.
1914 * @return desired width for thumbnail screenshot.
1915 */
1916 static int getDesiredThumbnailWidth(Context context) {
1917 return context.getResources().getDimensionPixelOffset(
1918 R.dimen.bookmarkThumbnailWidth);
1919 }
1920
1921 /**
1922 * Return the desired height for thumbnail screenshots, which are stored in
1923 * the database, and used on the bookmarks screen.
1924 * @param context Context for finding out the density of the screen.
1925 * @return desired height for thumbnail screenshot.
1926 */
1927 static int getDesiredThumbnailHeight(Context context) {
1928 return context.getResources().getDimensionPixelOffset(
1929 R.dimen.bookmarkThumbnailHeight);
1930 }
1931
Michael Kolb1acef692011-03-08 14:12:06 -08001932 static Bitmap createScreenshot(Tab tab, int width, int height) {
1933 if ((tab != null) && (tab.getWebView() != null)) {
1934 return createScreenshot(tab.getWebView(), width, height);
1935 }
1936 return null;
1937 }
1938
John Reck8cc92352011-07-06 17:41:52 -07001939 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reck5c6ac2f2011-01-05 10:18:03 -08001940 // We render to a bitmap 2x the desired size so that we can then
1941 // re-scale it with filtering since canvas.scale doesn't filter
1942 // This helps reduce aliasing at the cost of being slightly blurry
1943 final int filter_scale = 2;
Michael Kolb8233fac2010-10-26 16:08:53 -07001944 Picture thumbnail = view.capturePicture();
1945 if (thumbnail == null) {
1946 return null;
1947 }
John Reck5c6ac2f2011-01-05 10:18:03 -08001948 width *= filter_scale;
1949 height *= filter_scale;
Michael Kolb8233fac2010-10-26 16:08:53 -07001950 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
1951 Canvas canvas = new Canvas(bm);
1952 // May need to tweak these values to determine what is the
1953 // best scale factor
1954 int thumbnailWidth = thumbnail.getWidth();
1955 int thumbnailHeight = thumbnail.getHeight();
John Reckfe49ab42010-11-16 17:09:37 -08001956 float scaleFactor = 1.0f;
Michael Kolbeb95db42011-03-03 10:38:40 -08001957 if (thumbnailWidth > 0 && thumbnailHeight > 0) {
John Reckfe49ab42010-11-16 17:09:37 -08001958 scaleFactor = (float) width / (float)thumbnailWidth;
Michael Kolb8233fac2010-10-26 16:08:53 -07001959 } else {
1960 return null;
1961 }
John Reckfe49ab42010-11-16 17:09:37 -08001962
Michael Kolbeb95db42011-03-03 10:38:40 -08001963 float scaleFactorY = (float) height / (float)thumbnailHeight;
1964 if (scaleFactorY > scaleFactor) {
1965 // The picture is narrower than the requested AR
1966 // Center the thumnail and crop the sides
1967 scaleFactor = scaleFactorY;
John Reckfe49ab42010-11-16 17:09:37 -08001968 float wx = (thumbnailWidth * scaleFactor) - width;
1969 canvas.translate((int) -(wx / 2), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -07001970 }
1971
John Reckfe49ab42010-11-16 17:09:37 -08001972 canvas.scale(scaleFactor, scaleFactor);
Michael Kolb8233fac2010-10-26 16:08:53 -07001973
1974 thumbnail.draw(canvas);
John Reck5c6ac2f2011-01-05 10:18:03 -08001975 Bitmap ret = Bitmap.createScaledBitmap(bm, width / filter_scale,
1976 height / filter_scale, true);
1977 bm.recycle();
1978 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07001979 }
1980
John Reck34ef2672011-02-10 11:30:55 -08001981 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001982 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07001983 // FIXME: Would like to make sure there is actually something to
1984 // draw, but the API for that (WebViewCore.pictureReady()) is not
1985 // currently accessible here.
1986
John Reck34ef2672011-02-10 11:30:55 -08001987 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08001988 if (view == null) {
1989 // Tab was destroyed
1990 return;
1991 }
John Reck34ef2672011-02-10 11:30:55 -08001992 final String url = tab.getUrl();
1993 final String originalUrl = view.getOriginalUrl();
1994
1995 if (TextUtils.isEmpty(url)) {
1996 return;
1997 }
1998
1999 // Only update thumbnails for web urls (http(s)://), not for
2000 // about:, javascript:, data:, etc...
2001 // Unless it is a bookmarked site, then always update
2002 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2003 return;
2004 }
2005
Michael Kolb8233fac2010-10-26 16:08:53 -07002006 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2007 getDesiredThumbnailHeight(mActivity));
2008 if (bm == null) {
2009 return;
2010 }
2011
2012 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002013 new AsyncTask<Void, Void, Void>() {
2014 @Override
2015 protected Void doInBackground(Void... unused) {
2016 Cursor cursor = null;
2017 try {
2018 // TODO: Clean this up
2019 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2020 if (cursor != null && cursor.moveToFirst()) {
2021 final ByteArrayOutputStream os =
2022 new ByteArrayOutputStream();
2023 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002024
John Reck34ef2672011-02-10 11:30:55 -08002025 ContentValues values = new ContentValues();
2026 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002027
John Reck34ef2672011-02-10 11:30:55 -08002028 do {
John Reck617fd832011-02-16 14:35:59 -08002029 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002030 cr.update(Images.CONTENT_URI, values, null, null);
2031 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002032 }
John Reck34ef2672011-02-10 11:30:55 -08002033 } catch (IllegalStateException e) {
2034 // Ignore
2035 } finally {
2036 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002037 }
John Reck34ef2672011-02-10 11:30:55 -08002038 return null;
2039 }
2040 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002041 }
2042
2043 private class Copy implements OnMenuItemClickListener {
2044 private CharSequence mText;
2045
2046 public boolean onMenuItemClick(MenuItem item) {
2047 copy(mText);
2048 return true;
2049 }
2050
2051 public Copy(CharSequence toCopy) {
2052 mText = toCopy;
2053 }
2054 }
2055
Leon Scroggins63c02662010-11-18 15:16:27 -05002056 private static class Download implements OnMenuItemClickListener {
2057 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002058 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002059 private boolean mPrivateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002060
2061 public boolean onMenuItemClick(MenuItem item) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002062 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002063 null, null, mPrivateBrowsing);
Michael Kolb8233fac2010-10-26 16:08:53 -07002064 return true;
2065 }
2066
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002067 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002068 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002069 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002070 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002071 }
2072 }
2073
Cary Clark8974d282010-11-22 10:46:05 -05002074 private static class SelectText implements OnMenuItemClickListener {
2075 private WebView mWebView;
2076
2077 public boolean onMenuItemClick(MenuItem item) {
2078 if (mWebView != null) {
2079 return mWebView.selectText();
2080 }
2081 return false;
2082 }
2083
2084 public SelectText(WebView webView) {
2085 mWebView = webView;
2086 }
2087
2088 }
2089
Michael Kolb8233fac2010-10-26 16:08:53 -07002090 /********************** TODO: UI stuff *****************************/
2091
2092 // these methods have been copied, they still need to be cleaned up
2093
2094 /****************** tabs ***************************************************/
2095
2096 // basic tab interactions:
2097
2098 // it is assumed that tabcontrol already knows about the tab
2099 protected void addTab(Tab tab) {
2100 mUi.addTab(tab);
2101 }
2102
2103 protected void removeTab(Tab tab) {
2104 mUi.removeTab(tab);
2105 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002106 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002107 }
2108
Michael Kolbf2055602011-04-09 17:20:03 -07002109 @Override
2110 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002111 // monkey protection against delayed start
2112 if (tab != null) {
2113 mTabControl.setCurrentTab(tab);
2114 // the tab is guaranteed to have a webview after setCurrentTab
2115 mUi.setActiveTab(tab);
2116 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002117 }
2118
2119 protected void closeEmptyChildTab() {
2120 Tab current = mTabControl.getCurrentTab();
2121 if (current != null
2122 && current.getWebView().copyBackForwardList().getSize() == 0) {
Michael Kolbc831b632011-05-11 09:30:34 -07002123 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002124 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002125 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002126 closeTab(current);
2127 }
2128 }
2129 }
2130
John Reck26b18322011-06-21 13:08:58 -07002131 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002132 // Dismiss the subwindow if applicable.
2133 dismissSubWindow(appTab);
2134 // Since we might kill the WebView, remove it from the
2135 // content view first.
2136 mUi.detachTab(appTab);
2137 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002138 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002139 // TODO: analyze why the remove and add are necessary
2140 mUi.attachTab(appTab);
2141 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002142 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002143 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002144 } else {
2145 // If the tab was the current tab, we have to attach
2146 // it to the view system again.
2147 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002148 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002149 }
2150 }
2151
2152 // Remove the sub window if it exists. Also called by TabControl when the
2153 // user clicks the 'X' to dismiss a sub window.
2154 public void dismissSubWindow(Tab tab) {
2155 removeSubWindow(tab);
2156 // dismiss the subwindow. This will destroy the WebView.
2157 tab.dismissSubWindow();
2158 getCurrentTopWebView().requestFocus();
2159 }
2160
2161 @Override
2162 public void removeSubWindow(Tab t) {
2163 if (t.getSubWebView() != null) {
2164 mUi.removeSubWindow(t.getSubViewContainer());
2165 }
2166 }
2167
2168 @Override
2169 public void attachSubWindow(Tab tab) {
2170 if (tab.getSubWebView() != null) {
2171 mUi.attachSubWindow(tab.getSubViewContainer());
2172 getCurrentTopWebView().requestFocus();
2173 }
2174 }
2175
Mathew Inwood29721c22011-06-29 17:55:24 +01002176 private Tab showPreloadedTab(final UrlData urlData) {
2177 if (!urlData.isPreloaded()) {
2178 return null;
2179 }
2180 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2181 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2182 if (sbQuery != null) {
2183 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
2184 // Could not submit query. Fallback to regular tab creation
2185 tabControl.destroy();
2186 return null;
2187 }
2188 }
2189 Tab t = tabControl.getTab();
2190 mTabControl.addPreloadedTab(t);
2191 addTab(t);
2192 setActiveTab(t);
2193 return t;
2194 }
2195
Michael Kolb7bcafde2011-05-09 13:55:59 -07002196 // open a non inconito tab with the given url data
2197 // and set as active tab
2198 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002199 Tab tab = showPreloadedTab(urlData);
2200 if (tab == null) {
2201 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002202 if ((tab != null) && !urlData.isEmpty()) {
2203 loadUrlDataIn(tab, urlData);
2204 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002205 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002206 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002207 }
2208
Michael Kolb843510f2010-12-09 10:51:49 -08002209 @Override
2210 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002211 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002212 }
2213
Michael Kolb8233fac2010-10-26 16:08:53 -07002214 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002215 public Tab openIncognitoTab() {
2216 return openTab(INCOGNITO_URI, true, true, false);
2217 }
2218
2219 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002220 public Tab openTab(String url, boolean incognito, boolean setActive,
2221 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002222 return openTab(url, incognito, setActive, useCurrent, null);
2223 }
2224
2225 @Override
2226 public Tab openTab(String url, Tab parent, boolean setActive,
2227 boolean useCurrent) {
2228 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2229 setActive, useCurrent, parent);
2230 }
2231
2232 public Tab openTab(String url, boolean incognito, boolean setActive,
2233 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002234 Tab tab = createNewTab(incognito, setActive, useCurrent);
2235 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002236 if (parent != null && parent != tab) {
2237 parent.addChildTab(tab);
2238 }
Michael Kolb519d2282011-05-09 17:03:19 -07002239 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002240 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002241 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002242 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002243 return tab;
2244 }
2245
2246 // this method will attempt to create a new tab
2247 // incognito: private browsing tab
2248 // setActive: ste tab as current tab
2249 // useCurrent: if no new tab can be created, return current tab
2250 private Tab createNewTab(boolean incognito, boolean setActive,
2251 boolean useCurrent) {
2252 Tab tab = null;
2253 if (mTabControl.canCreateNewTab()) {
2254 tab = mTabControl.createNewTab(incognito);
2255 addTab(tab);
2256 if (setActive) {
2257 setActiveTab(tab);
2258 }
2259 } else {
2260 if (useCurrent) {
2261 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002262 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002263 } else {
2264 mUi.showMaxTabsWarning();
2265 }
2266 }
2267 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002268 }
2269
John Reck2bc80422011-06-30 15:11:49 -07002270 @Override
2271 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2272 SnapshotTab tab = null;
2273 if (mTabControl.canCreateNewTab()) {
2274 tab = mTabControl.createSnapshotTab(snapshotId);
2275 addTab(tab);
2276 if (setActive) {
2277 setActiveTab(tab);
2278 }
2279 } else {
2280 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002281 }
2282 return tab;
2283 }
2284
Michael Kolb8233fac2010-10-26 16:08:53 -07002285 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002286 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002287 * @return boolean True if we successfully switched to a different tab. If
2288 * the indexth tab is null, or if that tab is the same as
2289 * the current one, return false.
2290 */
2291 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002292 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002293 Tab currentTab = mTabControl.getCurrentTab();
2294 if (tab == null || tab == currentTab) {
2295 return false;
2296 }
2297 setActiveTab(tab);
2298 return true;
2299 }
2300
2301 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002302 public void closeCurrentTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002303 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002304 mCrashRecoveryHandler.clearState();
John Reck958b2422010-12-03 17:56:17 -08002305 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002306 return;
2307 }
Michael Kolbc831b632011-05-11 09:30:34 -07002308 final Tab current = mTabControl.getCurrentTab();
2309 final int pos = mTabControl.getCurrentPosition();
2310 Tab newTab = current.getParent();
2311 if (newTab == null) {
2312 newTab = mTabControl.getTab(pos + 1);
2313 if (newTab == null) {
2314 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002315 }
2316 }
Michael Kolbc831b632011-05-11 09:30:34 -07002317 if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002318 // Close window
2319 closeTab(current);
2320 }
2321 }
2322
2323 /**
2324 * Close the tab, remove its associated title bar, and adjust mTabControl's
2325 * current tab to a valid value.
2326 */
2327 @Override
2328 public void closeTab(Tab tab) {
Michael Kolb2d59c322011-01-25 13:18:55 -08002329 removeTab(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002330 }
2331
Michael Kolb8233fac2010-10-26 16:08:53 -07002332 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002333 protected void loadUrlFromContext(String url) {
2334 Tab tab = getCurrentTab();
2335 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002336 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002337 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002338 url = UrlUtils.smartUrlFilter(url);
2339 if (!view.getWebViewClient().shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002340 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002341 }
2342 }
2343 }
2344
2345 /**
2346 * Load the URL into the given WebView and update the title bar
2347 * to reflect the new load. Call this instead of WebView.loadUrl
2348 * directly.
2349 * @param view The WebView used to load url.
2350 * @param url The URL to load.
2351 */
John Reck71e51422011-07-01 16:49:28 -07002352 @Override
2353 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002354 loadUrl(tab, url, null);
2355 }
2356
2357 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2358 if (tab != null) {
2359 dismissSubWindow(tab);
2360 tab.loadUrl(url, headers);
2361 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002362 }
2363
2364 /**
2365 * Load UrlData into a Tab and update the title bar to reflect the new
2366 * load. Call this instead of UrlData.loadIn directly.
2367 * @param t The Tab used to load.
2368 * @param data The UrlData being loaded.
2369 */
2370 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002371 if (data != null) {
2372 if (data.mVoiceIntent != null) {
2373 t.activateVoiceSearchMode(data.mVoiceIntent);
Michael Kolb14612442011-06-24 13:06:29 -07002374 } else if (data.isPreloaded()) {
2375 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002376 } else {
2377 loadUrl(t, data.mUrl, data.mHeaders);
2378 }
2379 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002380 }
2381
John Reck30c714c2010-12-16 17:30:34 -08002382 @Override
2383 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002384 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002385 if (tab.canGoBack()) {
2386 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002387 } else {
John Reckef654f12011-07-12 16:42:08 -07002388 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002389 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002390 }
2391
2392 void goBackOnePageOrQuit() {
2393 Tab current = mTabControl.getCurrentTab();
2394 if (current == null) {
2395 /*
2396 * Instead of finishing the activity, simply push this to the back
2397 * of the stack and let ActivityManager to choose the foreground
2398 * activity. As BrowserActivity is singleTask, it will be always the
2399 * root of the task. So we can use either true or false for
2400 * moveTaskToBack().
2401 */
2402 mActivity.moveTaskToBack(true);
2403 return;
2404 }
John Reckef654f12011-07-12 16:42:08 -07002405 if (current.canGoBack()) {
2406 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002407 } else {
2408 // Check to see if we are closing a window that was created by
2409 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002410 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002411 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002412 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002413 // Now we close the other tab
2414 closeTab(current);
2415 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002416 /*
2417 * Instead of finishing the activity, simply push this to the back
2418 * of the stack and let ActivityManager to choose the foreground
2419 * activity. As BrowserActivity is singleTask, it will be always the
2420 * root of the task. So we can use either true or false for
2421 * moveTaskToBack().
2422 */
2423 mActivity.moveTaskToBack(true);
2424 }
2425 }
2426 }
2427
2428 /**
2429 * Feed the previously stored results strings to the BrowserProvider so that
2430 * the SearchDialog will show them instead of the standard searches.
2431 * @param result String to show on the editable line of the SearchDialog.
2432 */
2433 @Override
2434 public void showVoiceSearchResults(String result) {
2435 ContentProviderClient client = mActivity.getContentResolver()
2436 .acquireContentProviderClient(Browser.BOOKMARKS_URI);
2437 ContentProvider prov = client.getLocalContentProvider();
2438 BrowserProvider bp = (BrowserProvider) prov;
2439 bp.setQueryResults(mTabControl.getCurrentTab().getVoiceSearchResults());
2440 client.release();
2441
2442 Bundle bundle = createGoogleSearchSourceBundle(
2443 GOOGLE_SEARCH_SOURCE_SEARCHKEY);
2444 bundle.putBoolean(SearchManager.CONTEXT_IS_VOICE, true);
2445 startSearch(result, false, bundle, false);
2446 }
2447
2448 private void startSearch(String initialQuery, boolean selectInitialQuery,
2449 Bundle appSearchData, boolean globalSearch) {
2450 if (appSearchData == null) {
2451 appSearchData = createGoogleSearchSourceBundle(
2452 GOOGLE_SEARCH_SOURCE_TYPE);
2453 }
2454
2455 SearchEngine searchEngine = mSettings.getSearchEngine();
2456 if (searchEngine != null && !searchEngine.supportsVoiceSearch()) {
2457 appSearchData.putBoolean(SearchManager.DISABLE_VOICE_SEARCH, true);
2458 }
2459 mActivity.startSearch(initialQuery, selectInitialQuery, appSearchData,
2460 globalSearch);
2461 }
2462
2463 private Bundle createGoogleSearchSourceBundle(String source) {
2464 Bundle bundle = new Bundle();
2465 bundle.putString(Search.SOURCE, source);
2466 return bundle;
2467 }
2468
2469 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002470 * helper method for key handler
2471 * returns the current tab if it can't advance
2472 */
Michael Kolbc831b632011-05-11 09:30:34 -07002473 private Tab getNextTab() {
2474 return mTabControl.getTab(Math.min(mTabControl.getTabCount() - 1,
2475 mTabControl.getCurrentPosition() + 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002476 }
2477
2478 /**
2479 * helper method for key handler
2480 * returns the current tab if it can't advance
2481 */
Michael Kolbc831b632011-05-11 09:30:34 -07002482 private Tab getPrevTab() {
2483 return mTabControl.getTab(Math.max(0,
2484 mTabControl.getCurrentPosition() - 1));
Michael Kolb0035fad2011-03-14 13:25:28 -07002485 }
2486
2487 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002488 * handle key events in browser
2489 *
2490 * @param keyCode
2491 * @param event
2492 * @return true if handled, false to pass to super
2493 */
2494 boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002495 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002496 // Even if MENU is already held down, we need to call to super to open
2497 // the IME on long press.
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002498 if (!noModifiers
2499 && ((KeyEvent.KEYCODE_MENU == keyCode)
2500 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2501 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode))) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002502 mMenuIsDown = true;
2503 return false;
2504 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002505
Cary Clark8ff8c662010-12-29 15:03:05 -05002506 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002507 Tab tab = getCurrentTab();
2508 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002509
Cary Clark160bbb92011-01-10 11:17:07 -05002510 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2511 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002512
Michael Kolb8233fac2010-10-26 16:08:53 -07002513 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002514 case KeyEvent.KEYCODE_TAB:
2515 if (event.isCtrlPressed()) {
2516 if (event.isShiftPressed()) {
2517 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002518 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002519 } else {
2520 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002521 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002522 }
2523 return true;
2524 }
2525 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002526 case KeyEvent.KEYCODE_SPACE:
2527 // WebView/WebTextView handle the keys in the KeyDown. As
2528 // the Activity's shortcut keys are only handled when WebView
2529 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002530 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002531 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002532 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002533 pageDown();
2534 }
2535 return true;
2536 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002537 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002538 event.startTracking();
2539 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002540 case KeyEvent.KEYCODE_FORWARD:
2541 if (!noModifiers) break;
2542 tab.goForward();
2543 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002544 case KeyEvent.KEYCODE_DPAD_LEFT:
2545 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002546 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002547 return true;
2548 }
2549 break;
2550 case KeyEvent.KEYCODE_DPAD_RIGHT:
2551 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002552 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002553 return true;
2554 }
2555 break;
2556 case KeyEvent.KEYCODE_A:
2557 if (ctrl) {
2558 webView.selectAll();
2559 return true;
2560 }
2561 break;
Michael Kolba4183062011-01-16 10:43:21 -08002562// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002563 case KeyEvent.KEYCODE_C:
2564 if (ctrl) {
2565 webView.copySelection();
2566 return true;
2567 }
2568 break;
Michael Kolba4183062011-01-16 10:43:21 -08002569// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002570// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002571// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002572// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002573// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002574// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002575// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002576// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002577// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002578// case KeyEvent.KEYCODE_M: // unused
2579// case KeyEvent.KEYCODE_N: // in Chrome: new window
2580// case KeyEvent.KEYCODE_O: // in Chrome: open file
2581// case KeyEvent.KEYCODE_P: // in Chrome: print page
2582// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002583// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002584// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2585 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002586 // we can't use the ctrl/shift flags, they check for
2587 // exclusive use of a modifier
2588 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002589 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002590 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002591 } else {
2592 openTabToHomePage();
2593 }
2594 return true;
2595 }
2596 break;
2597// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2598// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb1a2eba42011-03-16 16:42:49 -07002599 case KeyEvent.KEYCODE_W: // in Chrome: close tab
2600 if (ctrl) {
2601 closeCurrentTab();
2602 return true;
2603 }
2604 break;
Cary Clark8ff8c662010-12-29 15:03:05 -05002605// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2606// case KeyEvent.KEYCODE_Y: // unused
2607// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002609 // it is a regular key and webview is not null
2610 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002611 }
2612
John Recke6bf4ab2011-02-24 15:48:05 -08002613 boolean onKeyLongPress(int keyCode, KeyEvent event) {
2614 switch(keyCode) {
2615 case KeyEvent.KEYCODE_BACK:
2616 if (mUi.showsWeb()) {
2617 bookmarksOrHistoryPicker(true);
2618 return true;
2619 }
2620 break;
2621 }
2622 return false;
2623 }
2624
Michael Kolb8233fac2010-10-26 16:08:53 -07002625 boolean onKeyUp(int keyCode, KeyEvent event) {
Michael Kolb2814a362011-05-19 15:49:41 -07002626 if (KeyEvent.KEYCODE_MENU == keyCode) {
2627 mMenuIsDown = false;
2628 if (event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002629 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002630 }
2631 }
Cary Clark160bbb92011-01-10 11:17:07 -05002632 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002633 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002634 case KeyEvent.KEYCODE_BACK:
2635 if (event.isTracking() && !event.isCanceled()) {
2636 onBackKey();
2637 return true;
2638 }
2639 break;
2640 }
2641 return false;
2642 }
2643
2644 public boolean isMenuDown() {
2645 return mMenuIsDown;
2646 }
2647
Ben Murdoch8029a772010-11-16 11:58:21 +00002648 public void setupAutoFill(Message message) {
2649 // Open the settings activity at the AutoFill profile fragment so that
2650 // the user can create a new profile. When they return, we will dispatch
2651 // the message so that we can autofill the form using their new profile.
2652 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2653 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2654 AutoFillSettingsFragment.class.getName());
2655 mAutoFillSetupMessage = message;
2656 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2657 }
John Reckb3417f02011-01-14 11:01:05 -08002658
2659 @Override
Narayan Kamath5119edd2011-02-23 15:49:17 +00002660 public void registerDropdownChangeListener(DropdownChangeListener d) {
2661 mUi.registerDropdownChangeListener(d);
2662 }
Michael Kolbfbc579a2011-07-07 15:59:33 -07002663
2664 public boolean onSearchRequested() {
2665 mUi.editUrl(false);
2666 return true;
2667 }
2668
Michael Kolb8233fac2010-10-26 16:08:53 -07002669}