blob: f87220c652e424a37a78b1746924a9ac6838098a [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;
John Reck68234a92012-04-19 15:27:12 -070020import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070022import android.app.ProgressDialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070024import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070025import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070026import android.content.ContentValues;
27import android.content.Context;
John Reck68234a92012-04-19 15:27:12 -070028import android.content.DialogInterface;
29import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070030import android.content.Intent;
31import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
33import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070034import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050035import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.database.Cursor;
37import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020038import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070039import android.graphics.Bitmap;
40import android.graphics.Canvas;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.net.Uri;
42import android.net.http.SslError;
43import android.os.AsyncTask;
44import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070045import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070046import android.os.Handler;
47import android.os.Message;
48import android.os.PowerManager;
49import android.os.PowerManager.WakeLock;
kaiyiza8b6dbb2013-07-29 18:11:22 +080050import android.os.SystemProperties;
Ben Murdoch8029a772010-11-16 11:58:21 +000051import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070052import android.provider.Browser;
53import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.provider.BrowserContract.Images;
55import android.provider.ContactsContract;
56import android.provider.ContactsContract.Intents.Insert;
Michael Kolb0b129122012-06-04 16:31:58 -070057import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070058import android.text.TextUtils;
59import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080060import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070061import android.view.ActionMode;
62import android.view.ContextMenu;
63import android.view.ContextMenu.ContextMenuInfo;
64import android.view.Gravity;
65import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070066import android.view.Menu;
67import android.view.MenuInflater;
68import android.view.MenuItem;
69import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070070import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070071import android.view.View;
72import android.webkit.CookieManager;
73import android.webkit.CookieSyncManager;
74import android.webkit.HttpAuthHandler;
George Mount387d45d2011-10-07 15:57:53 -070075import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070076import android.webkit.SslErrorHandler;
77import android.webkit.ValueCallback;
78import android.webkit.WebChromeClient;
79import android.webkit.WebIconDatabase;
80import android.webkit.WebSettings;
81import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000082import android.webkit.WebViewClassic;
Leon Scrogginsac993842011-02-02 12:54:07 -050083import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070084
Michael Kolb4bd767d2011-05-27 11:33:55 -070085import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070086import com.android.browser.UI.ComboViews;
John Reck1cf4b792011-07-26 10:22:22 -070087import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070088import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070089
Michael Kolb8233fac2010-10-26 16:08:53 -070090import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -070091import java.io.File;
92import java.io.FileOutputStream;
93import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -070094import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -070095import java.text.DateFormat;
96import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -070097import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -070098import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -070099import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700100import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800101import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200102import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700103import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700104
105/**
106 * Controller for browser
107 */
108public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700109 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700110
111 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800112 private static final String SEND_APP_ID_EXTRA =
113 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700114 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800115
Michael Kolb8233fac2010-10-26 16:08:53 -0700116
117 // public message ids
118 public final static int LOAD_URL = 1001;
119 public final static int STOP_LOAD = 1002;
120
121 // Message Ids
122 private static final int FOCUS_NODE_HREF = 102;
123 private static final int RELEASE_WAKELOCK = 107;
124
125 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
126
127 private static final int OPEN_BOOKMARKS = 201;
128
129 private static final int EMPTY_MENU = -1;
130
Michael Kolb8233fac2010-10-26 16:08:53 -0700131 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700132 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700133 final static int PREFERENCES_PAGE = 3;
134 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000135 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700136 final static int VOICE_RESULT = 6;
Ben Murdoch8029a772010-11-16 11:58:21 +0000137
Michael Kolb8233fac2010-10-26 16:08:53 -0700138 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
139
140 // As the ids are dynamically created, we can't guarantee that they will
141 // be in sequence, so this static array maps ids to a window number.
142 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
143 { R.id.window_one_menu_id, R.id.window_two_menu_id,
144 R.id.window_three_menu_id, R.id.window_four_menu_id,
145 R.id.window_five_menu_id, R.id.window_six_menu_id,
146 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
147
148 // "source" parameter for Google search through search key
149 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
150 // "source" parameter for Google search through simplily type
151 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
152
George Mount387d45d2011-10-07 15:57:53 -0700153 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700154 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
155
John Reckd7dd9b22011-08-30 09:18:29 -0700156 // A bitmap that is re-used in createScreenshot as scratch space
157 private static Bitmap sThumbnailBitmap;
158
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 private Activity mActivity;
160 private UI mUi;
161 private TabControl mTabControl;
162 private BrowserSettings mSettings;
163 private WebViewFactory mFactory;
164
165 private WakeLock mWakeLock;
166
167 private UrlHandler mUrlHandler;
168 private UploadHandler mUploadHandler;
169 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700170 private PageDialogsHandler mPageDialogsHandler;
171 private NetworkStateHandler mNetworkHandler;
172
Ben Murdoch8029a772010-11-16 11:58:21 +0000173 private Message mAutoFillSetupMessage;
174
Michael Kolb8233fac2010-10-26 16:08:53 -0700175 private boolean mShouldShowErrorConsole;
176
177 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
178
179 // FIXME, temp address onPrepareMenu performance problem.
180 // When we move everything out of view, we should rewrite this.
181 private int mCurrentMenuState = 0;
182 private int mMenuState = R.id.MAIN_MENU;
183 private int mOldMenuState = EMPTY_MENU;
184 private Menu mCachedMenu;
185
Michael Kolb8233fac2010-10-26 16:08:53 -0700186 private boolean mMenuIsDown;
187
188 // For select and find, we keep track of the ActionMode so that
189 // finish() can be called as desired.
190 private ActionMode mActionMode;
191
192 /**
193 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
194 * of whether the configuration has changed. The first onMenuOpened call
195 * after a configuration change is simply a reopening of the same menu
196 * (i.e. mIconView did not change).
197 */
198 private boolean mConfigChanged;
199
200 /**
201 * Keeps track of whether the options menu is open. This is important in
202 * determining whether to show or hide the title bar overlay
203 */
204 private boolean mOptionsMenuOpen;
205
206 /**
207 * Whether or not the options menu is in its bigger, popup menu form. When
208 * true, we want the title bar overlay to be gone. When false, we do not.
209 * Only meaningful if mOptionsMenuOpen is true.
210 */
211 private boolean mExtendedMenuOpen;
212
Michael Kolb8233fac2010-10-26 16:08:53 -0700213 private boolean mActivityPaused = true;
214 private boolean mLoadStopped;
215
216 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500217 // Checks to see when the bookmarks database has changed, and updates the
218 // Tabs' notion of whether they represent bookmarked sites.
219 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700220 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700221
Michael Kolbc3af0672011-08-09 10:24:41 -0700222 private boolean mBlockEvents;
223
Michael Kolb0b129122012-06-04 16:31:58 -0700224 private String mVoiceResult;
225
George Mount3636d0a2011-11-21 09:08:21 -0800226 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700227 mActivity = browser;
228 mSettings = BrowserSettings.getInstance();
229 mTabControl = new TabControl(this);
230 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700231 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800232 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700233 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700234
235 mUrlHandler = new UrlHandler(this);
236 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700237 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
238
Michael Kolb8233fac2010-10-26 16:08:53 -0700239 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500240 mBookmarksObserver = new ContentObserver(mHandler) {
241 @Override
242 public void onChange(boolean selfChange) {
243 int size = mTabControl.getTabCount();
244 for (int i = 0; i < size; i++) {
245 mTabControl.getTab(i).updateBookmarkedStatus();
246 }
247 }
248
249 };
250 browser.getContentResolver().registerContentObserver(
251 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700252
253 mNetworkHandler = new NetworkStateHandler(mActivity, this);
254 // Start watching the default geolocation permissions
255 mSystemAllowGeolocationOrigins =
256 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
257 mSystemAllowGeolocationOrigins.start();
258
John Reckaf262e72011-07-25 13:55:44 -0700259 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700260 }
261
John Reck9c35b9c2012-05-30 10:08:50 -0700262 @Override
263 public void start(final Intent intent) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800264 if (BrowserWebView.isClassic()) WebViewClassic.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800265 // mCrashRecoverHandler has any previously saved state.
266 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700267 }
268
George Mount3636d0a2011-11-21 09:08:21 -0800269 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700270 // Unless the last browser usage was within 24 hours, destroy any
271 // remaining incognito tabs.
272
273 Calendar lastActiveDate = icicle != null ?
274 (Calendar) icicle.getSerializable("lastActiveDate") : null;
275 Calendar today = Calendar.getInstance();
276 Calendar yesterday = Calendar.getInstance();
277 yesterday.add(Calendar.DATE, -1);
278
Patrick Scott7d50a932011-02-04 09:27:26 -0500279 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700280 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800281 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700282
Patrick Scott7d50a932011-02-04 09:27:26 -0500283 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700284 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500285 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000286
Michael Kolbc831b632011-05-11 09:30:34 -0700287 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500288 // Not able to restore so we go ahead and clear session cookies. We
289 // must do this before trying to login the user as we don't want to
290 // clear any session cookies set during login.
291 CookieManager.getInstance().removeSessionCookie();
292 }
293
Patrick Scottd43e75a2011-03-14 14:47:23 -0400294 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500295 new Runnable() {
296 @Override public void run() {
George Mount3636d0a2011-11-21 09:08:21 -0800297 onPreloginFinished(icicle, intent, currentTabId,
298 restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500299 }
300 });
301 }
302
Michael Kolbc831b632011-05-11 09:30:34 -0700303 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
George Mount3636d0a2011-11-21 09:08:21 -0800304 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700305 if (currentTabId == -1) {
John Reck1cf4b792011-07-26 10:22:22 -0700306 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800307 if (intent == null) {
308 // This won't happen under common scenarios. The icicle is
309 // not null, but there aren't any tabs to restore.
310 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700311 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800312 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.
317 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
318 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 }
327 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 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700333 }
334 }
John Reckd8c74522011-06-14 08:45:00 -0700335 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700336 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700337 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500338 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700339 List<Tab> tabs = mTabControl.getTabs();
340 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
341 for (Tab t : tabs) {
342 restoredTabs.add(t.getId());
343 }
344 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700345 if (tabs.size() == 0) {
346 openTabToHomePage();
347 }
John Reck1cf4b792011-07-26 10:22:22 -0700348 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700349 // TabControl.restoreState() will create a new tab even if
350 // restoring the state fails.
351 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800352 // Intent is non-null when framework thinks the browser should be
353 // launching with a new intent (icicle is null).
354 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700355 mIntentHandler.onNewIntent(intent);
356 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700357 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700359 String jsFlags = getSettings().getJsEngineFlags();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800360 if (jsFlags.trim().length() != 0 && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000361 WebViewClassic.fromWebView(getCurrentWebView()).setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700362 }
George Mount3636d0a2011-11-21 09:08:21 -0800363 if (intent != null
364 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700365 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800366 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700367 }
368
John Reck1cf4b792011-07-26 10:22:22 -0700369 private static class PruneThumbnails implements Runnable {
370 private Context mContext;
371 private List<Long> mIds;
372
373 PruneThumbnails(Context context, List<Long> preserveIds) {
374 mContext = context.getApplicationContext();
375 mIds = preserveIds;
376 }
377
378 @Override
379 public void run() {
380 ContentResolver cr = mContext.getContentResolver();
381 if (mIds == null || mIds.size() == 0) {
382 cr.delete(Thumbnails.CONTENT_URI, null, null);
383 } else {
384 int length = mIds.size();
385 StringBuilder where = new StringBuilder();
386 where.append(Thumbnails._ID);
387 where.append(" not in (");
388 for (int i = 0; i < length; i++) {
389 where.append(mIds.get(i));
390 if (i < (length - 1)) {
391 where.append(",");
392 }
393 }
394 where.append(")");
395 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
396 }
397 }
398
399 }
400
Michael Kolb1514bb72010-11-22 09:11:48 -0800401 @Override
402 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700403 return mFactory;
404 }
405
406 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800407 public void onSetWebView(Tab tab, WebView view) {
408 mUi.onSetWebView(tab, view);
409 }
410
411 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800412 public void createSubWindow(Tab tab) {
413 endActionMode();
414 WebView mainView = tab.getWebView();
415 WebView subView = mFactory.createWebView((mainView == null)
416 ? false
417 : mainView.isPrivateBrowsingEnabled());
418 mUi.createSubWindow(tab, subView);
419 }
420
421 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700422 public Context getContext() {
423 return mActivity;
424 }
425
426 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700427 public Activity getActivity() {
428 return mActivity;
429 }
430
431 void setUi(UI ui) {
432 mUi = ui;
433 }
434
Michael Kolbaf63dba2012-05-16 12:58:05 -0700435 @Override
436 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700437 return mSettings;
438 }
439
440 IntentHandler getIntentHandler() {
441 return mIntentHandler;
442 }
443
444 @Override
445 public UI getUi() {
446 return mUi;
447 }
448
449 int getMaxTabs() {
450 return mActivity.getResources().getInteger(R.integer.max_tabs);
451 }
452
453 @Override
454 public TabControl getTabControl() {
455 return mTabControl;
456 }
457
Michael Kolb1bf23132010-11-19 12:55:12 -0800458 @Override
459 public List<Tab> getTabs() {
460 return mTabControl.getTabs();
461 }
462
John Reckaf262e72011-07-25 13:55:44 -0700463 // Open the icon database.
464 private void openIconDatabase() {
465 // We have to call getInstance on the UI thread
466 final WebIconDatabase instance = WebIconDatabase.getInstance();
467 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000468
John Reckaf262e72011-07-25 13:55:44 -0700469 @Override
470 public void run() {
471 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700472 }
John Reckaf262e72011-07-25 13:55:44 -0700473 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700474 }
475
476 private void startHandler() {
477 mHandler = new Handler() {
478
479 @Override
480 public void handleMessage(Message msg) {
481 switch (msg.what) {
482 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700483 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700484 break;
485 case FOCUS_NODE_HREF:
486 {
487 String url = (String) msg.getData().get("url");
488 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500489 String src = (String) msg.getData().get("src");
490 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700491 if (TextUtils.isEmpty(url)) {
492 break;
493 }
494 HashMap focusNodeMap = (HashMap) msg.obj;
495 WebView view = (WebView) focusNodeMap.get("webview");
496 // Only apply the action if the top window did not change.
497 if (getCurrentTopWebView() != view) {
498 break;
499 }
500 switch (msg.arg1) {
501 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700502 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700503 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500504 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700505 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500506 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500507 case R.id.open_newtab_context_menu_id:
508 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700509 openTab(url, parent,
510 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500511 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700512 case R.id.copy_link_context_menu_id:
513 copy(url);
514 break;
515 case R.id.save_link_context_menu_id:
516 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500517 DownloadHandler.onDownloadStartNoStream(
Andreas Sandblad8e4ce662012-06-11 11:02:49 +0200518 mActivity, url, view.getSettings().getUserAgentString(),
Jean-Baptiste Queru2584c312012-09-04 14:39:07 -0700519 null, null, null, view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700520 break;
521 }
522 break;
523 }
524
525 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700526 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700527 break;
528
529 case STOP_LOAD:
530 stopLoading();
531 break;
532
533 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700534 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700535 mWakeLock.release();
536 // if we reach here, Browser should be still in the
537 // background loading after WAKELOCK_TIMEOUT (5-min).
538 // To avoid burning the battery, stop loading.
539 mTabControl.stopAllLoading();
540 }
541 break;
542
543 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800544 Tab tab = (Tab) msg.obj;
545 if (tab != null) {
546 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700547 }
548 break;
549 }
550 }
551 };
552
553 }
554
John Reckef654f12011-07-12 16:42:08 -0700555 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200556 public Tab getCurrentTab() {
557 return mTabControl.getCurrentTab();
558 }
559
Michael Kolbba99c5d2010-11-29 14:57:41 -0800560 @Override
561 public void shareCurrentPage() {
562 shareCurrentPage(mTabControl.getCurrentTab());
563 }
564
565 private void shareCurrentPage(Tab tab) {
566 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800567 sharePage(mActivity, tab.getTitle(),
568 tab.getUrl(), tab.getFavicon(),
569 createScreenshot(tab.getWebView(),
570 getDesiredThumbnailWidth(mActivity),
571 getDesiredThumbnailHeight(mActivity)));
572 }
573 }
574
Michael Kolb8233fac2010-10-26 16:08:53 -0700575 /**
576 * Share a page, providing the title, url, favicon, and a screenshot. Uses
577 * an {@link Intent} to launch the Activity chooser.
578 * @param c Context used to launch a new Activity.
579 * @param title Title of the page. Stored in the Intent with
580 * {@link Intent#EXTRA_SUBJECT}
581 * @param url URL of the page. Stored in the Intent with
582 * {@link Intent#EXTRA_TEXT}
583 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
584 * with {@link Browser#EXTRA_SHARE_FAVICON}
585 * @param screenshot Bitmap of a screenshot of the page. Stored in the
586 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
587 */
588 static final void sharePage(Context c, String title, String url,
589 Bitmap favicon, Bitmap screenshot) {
590 Intent send = new Intent(Intent.ACTION_SEND);
591 send.setType("text/plain");
592 send.putExtra(Intent.EXTRA_TEXT, url);
593 send.putExtra(Intent.EXTRA_SUBJECT, title);
594 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
595 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
596 try {
597 c.startActivity(Intent.createChooser(send, c.getString(
598 R.string.choosertitle_sharevia)));
599 } catch(android.content.ActivityNotFoundException ex) {
600 // if no app handles it, do nothing
601 }
602 }
603
604 private void copy(CharSequence text) {
605 ClipboardManager cm = (ClipboardManager) mActivity
606 .getSystemService(Context.CLIPBOARD_SERVICE);
607 cm.setText(text);
608 }
609
610 // lifecycle
611
John Reck9c35b9c2012-05-30 10:08:50 -0700612 @Override
613 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700614 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800615 // update the menu in case of a locale change
616 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700617 if (mPageDialogsHandler != null) {
618 mPageDialogsHandler.onConfigurationChanged(config);
619 }
620 mUi.onConfigurationChanged(config);
621 }
622
623 @Override
624 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700625 if (!mUi.isWebShowing()) {
626 mUi.showWeb(false);
627 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700628 mIntentHandler.onNewIntent(intent);
629 }
630
John Reck9c35b9c2012-05-30 10:08:50 -0700631 @Override
632 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800633 if (mUi.isCustomViewShowing()) {
634 hideCustomView();
635 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700636 if (mActivityPaused) {
637 Log.e(LOGTAG, "BrowserActivity is already paused.");
638 return;
639 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700640 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800641 Tab tab = mTabControl.getCurrentTab();
642 if (tab != null) {
643 tab.pause();
644 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700645 if (mWakeLock == null) {
646 PowerManager pm = (PowerManager) mActivity
647 .getSystemService(Context.POWER_SERVICE);
648 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
649 }
Michael Kolb70976932010-11-30 11:34:01 -0800650 mWakeLock.acquire();
651 mHandler.sendMessageDelayed(mHandler
652 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
653 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700654 }
655 mUi.onPause();
656 mNetworkHandler.onPause();
657
658 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100659 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700660 if (sThumbnailBitmap != null) {
661 sThumbnailBitmap.recycle();
662 sThumbnailBitmap = null;
663 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700664 }
665
John Reck9c35b9c2012-05-30 10:08:50 -0700666 @Override
667 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700668 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800669 Bundle saveState = createSaveState();
670
671 // crash recovery manages all save & restore state
672 mCrashRecoveryHandler.writeState(saveState);
673 mSettings.setLastRunPaused(true);
674 }
675
676 /**
677 * Save the current state to outState. Does not write the state to
678 * disk.
679 * @return Bundle containing the current state of all tabs.
680 */
681 /* package */ Bundle createSaveState() {
682 Bundle saveState = new Bundle();
683 mTabControl.saveState(saveState);
684 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700685 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800686 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700687 }
George Mount3636d0a2011-11-21 09:08:21 -0800688 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700689 }
690
John Reck9c35b9c2012-05-30 10:08:50 -0700691 @Override
692 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700693 if (!mActivityPaused) {
694 Log.e(LOGTAG, "BrowserActivity is already resumed.");
695 return;
696 }
George Mount3636d0a2011-11-21 09:08:21 -0800697 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700698 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800699 Tab current = mTabControl.getCurrentTab();
700 if (current != null) {
701 current.resume();
702 resumeWebViewTimers(current);
703 }
John Reckf57c0292011-07-21 18:15:39 -0700704 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200705
Michael Kolb8233fac2010-10-26 16:08:53 -0700706 mUi.onResume();
707 mNetworkHandler.onResume();
708 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100709 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700710 if (mVoiceResult != null) {
711 mUi.onVoiceResult(mVoiceResult);
712 mVoiceResult = null;
713 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700714 }
715
John Reckf57c0292011-07-21 18:15:39 -0700716 private void releaseWakeLock() {
717 if (mWakeLock != null && mWakeLock.isHeld()) {
718 mHandler.removeMessages(RELEASE_WAKELOCK);
719 mWakeLock.release();
720 }
721 }
722
Michael Kolb70976932010-11-30 11:34:01 -0800723 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800724 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800725 * @param tab guaranteed non-null
726 */
727 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700728 boolean inLoad = tab.inPageLoad();
729 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
730 CookieSyncManager.getInstance().startSync();
731 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100732 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700733 }
734 }
735
Michael Kolb70976932010-11-30 11:34:01 -0800736 /**
737 * Pause all WebView timers using the WebView of the given tab
738 * @param tab
739 * @return true if the timers are paused or tab is null
740 */
741 private boolean pauseWebViewTimers(Tab tab) {
742 if (tab == null) {
743 return true;
744 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700745 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100746 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700747 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700748 }
Michael Kolb70976932010-11-30 11:34:01 -0800749 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700750 }
751
John Reck9c35b9c2012-05-30 10:08:50 -0700752 @Override
753 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800754 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700755 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
756 mUploadHandler = null;
757 }
758 if (mTabControl == null) return;
759 mUi.onDestroy();
760 // Remove the current tab and sub window
761 Tab t = mTabControl.getCurrentTab();
762 if (t != null) {
763 dismissSubWindow(t);
764 removeTab(t);
765 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500766 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700767 // Destroy all the tabs
768 mTabControl.destroy();
769 WebIconDatabase.getInstance().close();
770 // Stop watching the default geolocation permissions
771 mSystemAllowGeolocationOrigins.stop();
772 mSystemAllowGeolocationOrigins = null;
kaiyiza8b6dbb2013-07-29 18:11:22 +0800773 mCrashRecoveryHandler.clearState();
Michael Kolb8233fac2010-10-26 16:08:53 -0700774 }
775
776 protected boolean isActivityPaused() {
777 return mActivityPaused;
778 }
779
John Reck9c35b9c2012-05-30 10:08:50 -0700780 @Override
781 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700782 mTabControl.freeMemory();
783 }
784
785 @Override
786 public boolean shouldShowErrorConsole() {
787 return mShouldShowErrorConsole;
788 }
789
790 protected void setShouldShowErrorConsole(boolean show) {
791 if (show == mShouldShowErrorConsole) {
792 // Nothing to do.
793 return;
794 }
795 mShouldShowErrorConsole = show;
796 Tab t = mTabControl.getCurrentTab();
797 if (t == null) {
798 // There is no current tab so we cannot toggle the error console
799 return;
800 }
801 mUi.setShouldShowErrorConsole(t, show);
802 }
803
804 @Override
805 public void stopLoading() {
806 mLoadStopped = true;
807 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700808 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700809 if (w != null) {
810 w.stopLoading();
811 mUi.onPageStopped(tab);
812 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700813 }
814
815 boolean didUserStopLoading() {
816 return mLoadStopped;
817 }
818
819 // WebViewController
820
821 @Override
John Reck324d4402011-01-11 16:56:42 -0800822 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700823
824 // We've started to load a new page. If there was a pending message
825 // to save a screenshot then we will now take the new page and save
826 // an incorrect screenshot. Therefore, remove any pending thumbnail
827 // messages from the queue.
828 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800829 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700830
831 // reset sync timer to avoid sync starts during loading a page
832 CookieSyncManager.getInstance().resetSync();
833
834 if (!mNetworkHandler.isNetworkUp()) {
835 view.setNetworkAvailable(false);
836 }
837
838 // when BrowserActivity just starts, onPageStarted may be called before
839 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
840 // to start the timer. As we won't switch tabs while an activity is in
841 // pause state, we can ensure calling resume and pause in pair.
842 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800843 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700844 }
845 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700846 endActionMode();
847
John Reck30c714c2010-12-16 17:30:34 -0800848 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700849
John Reck324d4402011-01-11 16:56:42 -0800850 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700851 // update the bookmark database for favicon
852 maybeUpdateFavicon(tab, null, url, favicon);
853
854 Performance.tracePageStart(url);
855
856 // Performance probe
857 if (false) {
858 Performance.onPageStarted();
859 }
860
861 }
862
863 @Override
John Reck324d4402011-01-11 16:56:42 -0800864 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700865 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800866 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200867
Michael Kolb8233fac2010-10-26 16:08:53 -0700868 // Performance probe
869 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800870 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 }
872
873 Performance.tracePageFinished();
874 }
875
876 @Override
John Reck30c714c2010-12-16 17:30:34 -0800877 public void onProgressChanged(Tab tab) {
878 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700879
880 if (newProgress == 100) {
881 CookieSyncManager.getInstance().sync();
882 // onProgressChanged() may continue to be called after the main
883 // frame has finished loading, as any remaining sub frames continue
884 // to load. We'll only get called once though with newProgress as
885 // 100 when everything is loaded. (onPageFinished is called once
886 // when the main frame completes loading regardless of the state of
887 // any sub frames so calls to onProgressChanges may continue after
888 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800889 if (tab.inPageLoad()) {
890 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200891 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
892 // pause the WebView timer and release the wake lock if it is
893 // finished while BrowserActivity is in pause state.
894 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700895 }
John Reckd9862372012-02-21 15:04:50 -0800896 if (!tab.isPrivateBrowsingEnabled()
897 && !TextUtils.isEmpty(tab.getUrl())
898 && !tab.isSnapshot()) {
899 // Only update the bookmark screenshot if the user did not
900 // cancel the load early and there is not already
901 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700902 if (tab.shouldUpdateThumbnail() &&
903 (tab.inForeground() && !didUserStopLoading()
904 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800905 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
906 mHandler.sendMessageDelayed(mHandler.obtainMessage(
907 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
908 500);
909 }
910 }
911 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700912 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800913 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700914 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800915 // still loading
916 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -0700917 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800918 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700919 }
920 }
John Reck30c714c2010-12-16 17:30:34 -0800921 mUi.onProgressChanged(tab);
922 }
923
924 @Override
Steve Block2466eff2011-10-03 15:33:09 +0100925 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800926 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700927 }
928
929 @Override
930 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800931 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800932 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800933 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700934 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
935 return;
936 }
937 // Update the title in the history database if not in private browsing mode
938 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -0700939 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700940 }
941 }
942
943 @Override
944 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800945 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700946 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
947 }
948
949 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800950 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
951 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700952 }
953
954 @Override
955 public boolean shouldOverrideKeyEvent(KeyEvent event) {
956 if (mMenuIsDown) {
957 // only check shortcut key when MENU is held
958 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
959 event);
960 } else {
961 return false;
962 }
963 }
964
965 @Override
John Reck997b1b72012-04-19 18:08:25 -0700966 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700967 if (!isActivityPaused()) {
968 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -0700969 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700970 } else {
John Reck997b1b72012-04-19 18:08:25 -0700971 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700972 }
973 }
John Reck997b1b72012-04-19 18:08:25 -0700974 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700975 }
976
977 @Override
John Reck324d4402011-01-11 16:56:42 -0800978 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700979 // Don't save anything in private browsing mode
980 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800981 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700982
John Reck324d4402011-01-11 16:56:42 -0800983 if (TextUtils.isEmpty(url)
984 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700985 return;
986 }
John Reckf57c0292011-07-21 18:15:39 -0700987 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -0700988 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -0700989 }
990
991 @Override
992 public void getVisitedHistory(final ValueCallback<String[]> callback) {
993 AsyncTask<Void, Void, String[]> task =
994 new AsyncTask<Void, Void, String[]>() {
995 @Override
996 public String[] doInBackground(Void... unused) {
997 return Browser.getVisitedHistory(mActivity.getContentResolver());
998 }
999 @Override
1000 public void onPostExecute(String[] result) {
1001 callback.onReceiveValue(result);
1002 }
1003 };
1004 task.execute();
1005 }
1006
1007 @Override
1008 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1009 final HttpAuthHandler handler, final String host,
1010 final String realm) {
1011 String username = null;
1012 String password = null;
1013
1014 boolean reuseHttpAuthUsernamePassword
1015 = handler.useHttpAuthUsernamePassword();
1016
1017 if (reuseHttpAuthUsernamePassword && view != null) {
1018 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1019 if (credentials != null && credentials.length == 2) {
1020 username = credentials[0];
1021 password = credentials[1];
1022 }
1023 }
1024
1025 if (username != null && password != null) {
1026 handler.proceed(username, password);
1027 } else {
Ben Murdochfdd37112011-08-05 15:48:14 +01001028 if (tab.inForeground() && !handler.suppressDialog()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001029 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1030 } else {
1031 handler.cancel();
1032 }
1033 }
1034 }
1035
1036 @Override
1037 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001038 String contentDisposition, String mimetype, String referer,
1039 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001040 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001041 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001042 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled());
qqzhoua95a2e22013-04-18 17:28:31 +08001043 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 // This Tab was opened for the sole purpose of downloading a
1045 // file. Remove it.
1046 if (tab == mTabControl.getCurrentTab()) {
1047 // In this case, the Tab is still on top.
1048 goBackOnePageOrQuit();
1049 } else {
1050 // In this case, it is not.
1051 closeTab(tab);
1052 }
1053 }
1054 }
1055
1056 @Override
1057 public Bitmap getDefaultVideoPoster() {
1058 return mUi.getDefaultVideoPoster();
1059 }
1060
1061 @Override
1062 public View getVideoLoadingProgressView() {
1063 return mUi.getVideoLoadingProgressView();
1064 }
1065
1066 @Override
1067 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1068 SslError error) {
1069 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1070 }
1071
Patrick Scott92066772011-03-10 08:46:27 -05001072 @Override
1073 public void showAutoLogin(Tab tab) {
1074 assert tab.inForeground();
1075 // Update the title bar to show the auto-login request.
1076 mUi.showAutoLogin(tab);
1077 }
1078
1079 @Override
1080 public void hideAutoLogin(Tab tab) {
1081 assert tab.inForeground();
1082 mUi.hideAutoLogin(tab);
1083 }
1084
Michael Kolb8233fac2010-10-26 16:08:53 -07001085 // helper method
1086
1087 /*
1088 * Update the favorites icon if the private browsing isn't enabled and the
1089 * icon is valid.
1090 */
1091 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1092 final String url, Bitmap favicon) {
1093 if (favicon == null) {
1094 return;
1095 }
1096 if (!tab.isPrivateBrowsingEnabled()) {
1097 Bookmarks.updateFavicon(mActivity
1098 .getContentResolver(), originalUrl, url, favicon);
1099 }
1100 }
1101
Leon Scroggins4cd97792010-12-03 15:31:56 -05001102 @Override
1103 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001104 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001105 mUi.bookmarkedStatusHasChanged(tab);
1106 }
1107
Michael Kolb8233fac2010-10-26 16:08:53 -07001108 // end WebViewController
1109
1110 protected void pageUp() {
1111 getCurrentTopWebView().pageUp(false);
1112 }
1113
1114 protected void pageDown() {
1115 getCurrentTopWebView().pageDown(false);
1116 }
1117
1118 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001119 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001120 public void editUrl() {
1121 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001122 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001123 }
1124
John Reck9c35b9c2012-05-30 10:08:50 -07001125 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001126 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001127 WebChromeClient.CustomViewCallback callback) {
1128 if (tab.inForeground()) {
1129 if (mUi.isCustomViewShowing()) {
1130 callback.onCustomViewHidden();
1131 return;
1132 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001133 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001134 // Save the menu state and set it to empty while the custom
1135 // view is showing.
1136 mOldMenuState = mMenuState;
1137 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001138 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001139 }
1140 }
1141
1142 @Override
1143 public void hideCustomView() {
1144 if (mUi.isCustomViewShowing()) {
1145 mUi.onHideCustomView();
1146 // Reset the old menu state.
1147 mMenuState = mOldMenuState;
1148 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001149 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001150 }
1151 }
1152
John Reck9c35b9c2012-05-30 10:08:50 -07001153 @Override
1154 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001155 Intent intent) {
1156 if (getCurrentTopWebView() == null) return;
1157 switch (requestCode) {
1158 case PREFERENCES_PAGE:
1159 if (resultCode == Activity.RESULT_OK && intent != null) {
1160 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001161 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001162 mTabControl.removeParentChildRelationShips();
1163 }
1164 }
1165 break;
1166 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001167 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001168 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001169 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001170 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001171 case AUTOFILL_SETUP:
1172 // Determine whether a profile was actually set up or not
1173 // and if so, send the message back to the WebTextView to
1174 // fill the form with the new profile.
1175 if (getSettings().getAutoFillProfile() != null) {
1176 mAutoFillSetupMessage.sendToTarget();
1177 mAutoFillSetupMessage = null;
1178 }
1179 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001180 case COMBO_VIEW:
1181 if (intent == null || resultCode != Activity.RESULT_OK) {
1182 break;
1183 }
John Reck3ba45532011-08-11 16:26:53 -07001184 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001185 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1186 Tab t = getCurrentTab();
1187 Uri uri = intent.getData();
1188 loadUrl(t, uri.toString());
1189 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1190 String[] urls = intent.getStringArrayExtra(
1191 ComboViewActivity.EXTRA_OPEN_ALL);
1192 Tab parent = getCurrentTab();
1193 for (String url : urls) {
1194 parent = openTab(url, parent,
1195 !mSettings.openInBackground(), true);
1196 }
1197 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1198 long id = intent.getLongExtra(
1199 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1200 if (id >= 0) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08001201 if (BrowserWebView.isClassic()) {
1202 createNewSnapshotTab(id, true);
1203 } else {
1204 Toast.makeText(mActivity, "Snapshot Tab requires WebViewClassic",
1205 Toast.LENGTH_LONG).show();
1206 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001207 }
1208 }
1209 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001210 case VOICE_RESULT:
1211 if (resultCode == Activity.RESULT_OK && intent != null) {
1212 ArrayList<String> results = intent.getStringArrayListExtra(
1213 RecognizerIntent.EXTRA_RESULTS);
1214 if (results.size() >= 1) {
1215 mVoiceResult = results.get(0);
1216 }
1217 }
1218 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001219 default:
1220 break;
1221 }
1222 getCurrentTopWebView().requestFocus();
1223 }
1224
1225 /**
1226 * Open the Go page.
1227 * @param startWithHistory If true, open starting on the history tab.
1228 * Otherwise, start with the bookmarks tab.
1229 */
1230 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001231 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 if (mTabControl.getCurrentWebView() == null) {
1233 return;
1234 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001235 // clear action mode
1236 if (isInCustomActionMode()) {
1237 endActionMode();
1238 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001239 Bundle extras = new Bundle();
1240 // Disable opening in a new window if we have maxed out the windows
1241 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1242 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001243 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001244 }
1245
1246 // combo view callbacks
1247
Michael Kolb8233fac2010-10-26 16:08:53 -07001248 // key handling
1249 protected void onBackKey() {
1250 if (!mUi.onBackKey()) {
1251 WebView subwindow = mTabControl.getCurrentSubWindow();
1252 if (subwindow != null) {
1253 if (subwindow.canGoBack()) {
1254 subwindow.goBack();
1255 } else {
1256 dismissSubWindow(mTabControl.getCurrentTab());
1257 }
1258 } else {
1259 goBackOnePageOrQuit();
1260 }
1261 }
1262 }
1263
Michael Kolb4bd767d2011-05-27 11:33:55 -07001264 protected boolean onMenuKey() {
1265 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001266 }
1267
Michael Kolb8233fac2010-10-26 16:08:53 -07001268 // menu handling and state
1269 // TODO: maybe put into separate handler
1270
John Reck9c35b9c2012-05-30 10:08:50 -07001271 @Override
1272 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001273 if (mMenuState == EMPTY_MENU) {
1274 return false;
1275 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001276 MenuInflater inflater = mActivity.getMenuInflater();
1277 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001278 return true;
1279 }
1280
John Reck9c35b9c2012-05-30 10:08:50 -07001281 @Override
1282 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001283 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001284 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001285 return;
1286 }
1287 if (!(v instanceof WebView)) {
1288 return;
1289 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001290 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001291 WebView.HitTestResult result = webview.getHitTestResult();
1292 if (result == null) {
1293 return;
1294 }
1295
1296 int type = result.getType();
1297 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1298 Log.w(LOGTAG,
1299 "We should not show context menu when nothing is touched");
1300 return;
1301 }
1302 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1303 // let TextView handles context menu
1304 return;
1305 }
1306
1307 // Note, http://b/issue?id=1106666 is requesting that
1308 // an inflated menu can be used again. This is not available
1309 // yet, so inflate each time (yuk!)
1310 MenuInflater inflater = mActivity.getMenuInflater();
1311 inflater.inflate(R.menu.browsercontext, menu);
1312
1313 // Show the correct menu group
1314 final String extra = result.getExtra();
Michael Kolbc159c1a2011-12-15 16:06:38 -08001315 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001316 menu.setGroupVisible(R.id.PHONE_MENU,
1317 type == WebView.HitTestResult.PHONE_TYPE);
1318 menu.setGroupVisible(R.id.EMAIL_MENU,
1319 type == WebView.HitTestResult.EMAIL_TYPE);
1320 menu.setGroupVisible(R.id.GEO_MENU,
1321 type == WebView.HitTestResult.GEO_TYPE);
1322 menu.setGroupVisible(R.id.IMAGE_MENU,
1323 type == WebView.HitTestResult.IMAGE_TYPE
1324 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1325 menu.setGroupVisible(R.id.ANCHOR_MENU,
1326 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1327 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001328 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1329 || type == WebView.HitTestResult.PHONE_TYPE
1330 || type == WebView.HitTestResult.EMAIL_TYPE
1331 || type == WebView.HitTestResult.GEO_TYPE;
1332 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1333 if (hitText) {
1334 menu.findItem(R.id.select_text_menu_id)
1335 .setOnMenuItemClickListener(new SelectText(webview));
1336 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001337 // Setup custom handling depending on the type
1338 switch (type) {
1339 case WebView.HitTestResult.PHONE_TYPE:
1340 menu.setHeaderTitle(Uri.decode(extra));
1341 menu.findItem(R.id.dial_context_menu_id).setIntent(
1342 new Intent(Intent.ACTION_VIEW, Uri
1343 .parse(WebView.SCHEME_TEL + extra)));
1344 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1345 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1346 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1347 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1348 addIntent);
1349 menu.findItem(R.id.copy_phone_context_menu_id)
1350 .setOnMenuItemClickListener(
1351 new Copy(extra));
1352 break;
1353
1354 case WebView.HitTestResult.EMAIL_TYPE:
1355 menu.setHeaderTitle(extra);
1356 menu.findItem(R.id.email_context_menu_id).setIntent(
1357 new Intent(Intent.ACTION_VIEW, Uri
1358 .parse(WebView.SCHEME_MAILTO + extra)));
1359 menu.findItem(R.id.copy_mail_context_menu_id)
1360 .setOnMenuItemClickListener(
1361 new Copy(extra));
1362 break;
1363
1364 case WebView.HitTestResult.GEO_TYPE:
1365 menu.setHeaderTitle(extra);
1366 menu.findItem(R.id.map_context_menu_id).setIntent(
1367 new Intent(Intent.ACTION_VIEW, Uri
1368 .parse(WebView.SCHEME_GEO
1369 + URLEncoder.encode(extra))));
1370 menu.findItem(R.id.copy_geo_context_menu_id)
1371 .setOnMenuItemClickListener(
1372 new Copy(extra));
1373 break;
1374
1375 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1376 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001377 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001378 // decide whether to show the open link in new tab option
1379 boolean showNewTab = mTabControl.canCreateNewTab();
1380 MenuItem newTabItem
1381 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001382 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001383 ? R.string.contextmenu_openlink_newwindow_background
1384 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001385 newTabItem.setVisible(showNewTab);
1386 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001387 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1388 newTabItem.setOnMenuItemClickListener(
1389 new MenuItem.OnMenuItemClickListener() {
1390 @Override
1391 public boolean onMenuItemClick(MenuItem item) {
1392 final HashMap<String, WebView> hrefMap =
1393 new HashMap<String, WebView>();
1394 hrefMap.put("webview", webview);
1395 final Message msg = mHandler.obtainMessage(
1396 FOCUS_NODE_HREF,
1397 R.id.open_newtab_context_menu_id,
1398 0, hrefMap);
1399 webview.requestFocusNodeHref(msg);
1400 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001401 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001402 });
1403 } else {
1404 newTabItem.setOnMenuItemClickListener(
1405 new MenuItem.OnMenuItemClickListener() {
1406 @Override
1407 public boolean onMenuItemClick(MenuItem item) {
1408 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001409 openTab(extra, parent,
1410 !mSettings.openInBackground(),
1411 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001412 return true;
1413 }
1414 });
1415 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001416 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001417 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1418 break;
1419 }
1420 // otherwise fall through to handle image part
1421 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001422 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1423 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001424 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1425 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001426 shareItem.setOnMenuItemClickListener(
1427 new MenuItem.OnMenuItemClickListener() {
1428 @Override
1429 public boolean onMenuItemClick(MenuItem item) {
1430 sharePage(mActivity, null, extra, null,
1431 null);
1432 return true;
1433 }
1434 }
1435 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001436 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001437 menu.findItem(R.id.view_image_context_menu_id)
1438 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1439 @Override
1440 public boolean onMenuItemClick(MenuItem item) {
1441 openTab(extra, mTabControl.getCurrentTab(), true, true);
1442 return false;
1443 }
1444 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001445 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1446 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1447 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001448 menu.findItem(R.id.set_wallpaper_context_menu_id).
1449 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1450 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001451 break;
1452
1453 default:
1454 Log.w(LOGTAG, "We should not get here.");
1455 break;
1456 }
1457 //update the ui
1458 mUi.onContextMenuCreated(menu);
1459 }
1460
1461 /**
1462 * As the menu can be open when loading state changes
1463 * we must manually update the state of the stop/reload menu
1464 * item
1465 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001466 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001467 if (menu == null) {
1468 return;
1469 }
1470 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001471 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001472 menu.findItem(R.id.stop_menu_id):
1473 menu.findItem(R.id.reload_menu_id);
1474 if (src != null) {
1475 dest.setIcon(src.getIcon());
1476 dest.setTitle(src.getTitle());
1477 }
1478 }
1479
John Reck9c35b9c2012-05-30 10:08:50 -07001480 @Override
1481 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001482 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001483 // hold on to the menu reference here; it is used by the page callbacks
1484 // to update the menu based on loading state
1485 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001486 // Note: setVisible will decide whether an item is visible; while
1487 // setEnabled() will decide whether an item is enabled, which also means
1488 // whether the matching shortcut key will function.
1489 switch (mMenuState) {
1490 case EMPTY_MENU:
1491 if (mCurrentMenuState != mMenuState) {
1492 menu.setGroupVisible(R.id.MAIN_MENU, false);
1493 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1494 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1495 }
1496 break;
1497 default:
1498 if (mCurrentMenuState != mMenuState) {
1499 menu.setGroupVisible(R.id.MAIN_MENU, true);
1500 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1501 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1502 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001503 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001504 break;
1505 }
1506 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001507 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001508 }
1509
Michael Kolb4bf79712011-07-14 14:18:12 -07001510 @Override
1511 public void updateMenuState(Tab tab, Menu menu) {
1512 boolean canGoBack = false;
1513 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001514 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001515 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001516 if (tab != null) {
1517 canGoBack = tab.canGoBack();
1518 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001519 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001520 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001521 }
1522 final MenuItem back = menu.findItem(R.id.back_menu_id);
1523 back.setEnabled(canGoBack);
1524
1525 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001526
1527 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1528 forward.setEnabled(canGoForward);
1529
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001530 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1531 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001532 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001533 if (source != null && dest != null) {
1534 dest.setTitle(source.getTitle());
1535 dest.setIcon(source.getIcon());
1536 }
John Recke1a03a32011-09-14 17:04:16 -07001537 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001538
1539 // decide whether to show the share link option
1540 PackageManager pm = mActivity.getPackageManager();
1541 Intent send = new Intent(Intent.ACTION_SEND);
1542 send.setType("text/plain");
1543 ResolveInfo ri = pm.resolveActivity(send,
1544 PackageManager.MATCH_DEFAULT_ONLY);
1545 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1546
1547 boolean isNavDump = mSettings.enableNavDump();
1548 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1549 nav.setVisible(isNavDump);
1550 nav.setEnabled(isNavDump);
1551
1552 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001553 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1554 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001555 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1556 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001557
1558 // history and snapshots item are the members of COMBO menu group,
1559 // so if show history item, only make snapshots item invisible.
1560 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001561
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001562 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001563 }
1564
John Reck9c35b9c2012-05-30 10:08:50 -07001565 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001566 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001567 if (null == getCurrentTopWebView()) {
1568 return false;
1569 }
1570 if (mMenuIsDown) {
1571 // The shortcut action consumes the MENU. Even if it is still down,
1572 // it won't trigger the next shortcut action. In the case of the
1573 // shortcut action triggering a new activity, like Bookmarks, we
1574 // won't get onKeyUp for MENU. So it is important to reset it here.
1575 mMenuIsDown = false;
1576 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001577 if (mUi.onOptionsItemSelected(item)) {
1578 // ui callback handled it
1579 return true;
1580 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 switch (item.getItemId()) {
1582 // -- Main menu
1583 case R.id.new_tab_menu_id:
1584 openTabToHomePage();
1585 break;
1586
1587 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001588 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001589 break;
1590
Afzal Najamd4e33312012-04-26 01:54:01 -04001591 case R.id.close_other_tabs_id:
1592 closeOtherTabs();
1593 break;
1594
Michael Kolb8233fac2010-10-26 16:08:53 -07001595 case R.id.goto_menu_id:
1596 editUrl();
1597 break;
1598
1599 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001600 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1601 break;
1602
1603 case R.id.history_menu_id:
1604 bookmarksOrHistoryPicker(ComboViews.History);
1605 break;
1606
1607 case R.id.snapshots_menu_id:
1608 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001609 break;
1610
Michael Kolb8233fac2010-10-26 16:08:53 -07001611 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001612 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001613 break;
1614
1615 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001616 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001617 stopLoading();
1618 } else {
1619 getCurrentTopWebView().reload();
1620 }
1621 break;
1622
1623 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001624 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001625 break;
1626
1627 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001628 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001629 break;
1630
1631 case R.id.close_menu_id:
1632 // Close the subwindow if it exists.
1633 if (mTabControl.getCurrentSubWindow() != null) {
1634 dismissSubWindow(mTabControl.getCurrentTab());
1635 break;
1636 }
1637 closeCurrentTab();
1638 break;
1639
kaiyiza8b6dbb2013-07-29 18:11:22 +08001640 case R.id.exit_menu_id:
1641 String ret = SystemProperties.get("persist.debug.browsermonkeytest");
1642 if (ret != null && ret.equals("enable"))
1643 break;
1644 onPause();
1645 return false;
1646
Michael Kolb8233fac2010-10-26 16:08:53 -07001647 case R.id.homepage_menu_id:
1648 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001649 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001650 break;
1651
1652 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001653 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001654 break;
1655
1656 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001657 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001658 break;
1659
John Reck2bc80422011-06-30 15:11:49 -07001660 case R.id.save_snapshot_menu_id:
1661 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001662 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001663 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001664 break;
1665
Michael Kolb8233fac2010-10-26 16:08:53 -07001666 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001667 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001668 break;
1669
John Recke1a03a32011-09-14 17:04:16 -07001670 case R.id.snapshot_go_live:
1671 goLive();
1672 return true;
1673
Michael Kolb8233fac2010-10-26 16:08:53 -07001674 case R.id.share_page_menu_id:
1675 Tab currentTab = mTabControl.getCurrentTab();
1676 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001677 return false;
1678 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001679 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001680 break;
1681
1682 case R.id.dump_nav_menu_id:
1683 getCurrentTopWebView().debugDump();
1684 break;
1685
Michael Kolb8233fac2010-10-26 16:08:53 -07001686 case R.id.zoom_in_menu_id:
1687 getCurrentTopWebView().zoomIn();
1688 break;
1689
1690 case R.id.zoom_out_menu_id:
1691 getCurrentTopWebView().zoomOut();
1692 break;
1693
1694 case R.id.view_downloads_menu_id:
1695 viewDownloads();
1696 break;
1697
John Reck42229bc2011-08-19 13:26:43 -07001698 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001699 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07001700 break;
1701
Michael Kolb8233fac2010-10-26 16:08:53 -07001702 case R.id.window_one_menu_id:
1703 case R.id.window_two_menu_id:
1704 case R.id.window_three_menu_id:
1705 case R.id.window_four_menu_id:
1706 case R.id.window_five_menu_id:
1707 case R.id.window_six_menu_id:
1708 case R.id.window_seven_menu_id:
1709 case R.id.window_eight_menu_id:
1710 {
1711 int menuid = item.getItemId();
1712 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1713 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1714 Tab desiredTab = mTabControl.getTab(id);
1715 if (desiredTab != null &&
1716 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001717 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001718 }
1719 break;
1720 }
1721 }
1722 }
1723 break;
1724
1725 default:
1726 return false;
1727 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001728 return true;
1729 }
1730
John Reck68234a92012-04-19 15:27:12 -07001731 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
1732 implements OnCancelListener {
1733
1734 private Tab mTab;
1735 private Dialog mProgressDialog;
1736 private ContentValues mValues;
1737
1738 private SaveSnapshotTask(Tab tab) {
1739 mTab = tab;
1740 }
1741
1742 @Override
1743 protected void onPreExecute() {
1744 CharSequence message = mActivity.getText(R.string.saving_snapshot);
1745 mProgressDialog = ProgressDialog.show(mActivity, null, message,
1746 true, true, this);
1747 mValues = mTab.createSnapshotValues();
1748 }
1749
1750 @Override
1751 protected Long doInBackground(Void... params) {
1752 if (!mTab.saveViewState(mValues)) {
1753 return null;
1754 }
1755 if (isCancelled()) {
1756 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
1757 File file = mActivity.getFileStreamPath(path);
1758 if (!file.delete()) {
1759 file.deleteOnExit();
1760 }
1761 return null;
1762 }
1763 final ContentResolver cr = mActivity.getContentResolver();
1764 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
1765 if (result == null) {
1766 return null;
1767 }
1768 long id = ContentUris.parseId(result);
1769 return id;
1770 }
1771
1772 @Override
1773 protected void onPostExecute(Long id) {
1774 if (isCancelled()) {
1775 return;
1776 }
1777 mProgressDialog.dismiss();
1778 if (id == null) {
1779 Toast.makeText(mActivity, R.string.snapshot_failed,
1780 Toast.LENGTH_SHORT).show();
1781 return;
1782 }
1783 Bundle b = new Bundle();
1784 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1785 mUi.showComboView(ComboViews.Snapshots, b);
1786 }
1787
1788 @Override
1789 public void onCancel(DialogInterface dialog) {
1790 cancel(true);
1791 }
1792 }
1793
Michael Kolb80f75082012-04-10 10:50:06 -07001794 @Override
1795 public void toggleUserAgent() {
1796 WebView web = getCurrentWebView();
1797 mSettings.toggleDesktopUseragent(web);
1798 web.loadUrl(web.getOriginalUrl());
1799 }
1800
1801 @Override
1802 public void findOnPage() {
1803 getCurrentTopWebView().showFindDialog(null, true);
1804 }
1805
1806 @Override
1807 public void openPreferences() {
1808 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1809 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1810 getCurrentTopWebView().getUrl());
1811 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1812 }
1813
1814 @Override
1815 public void bookmarkCurrentPage() {
1816 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
1817 if (bookmarkIntent != null) {
1818 mActivity.startActivity(bookmarkIntent);
1819 }
1820 }
1821
John Recke1a03a32011-09-14 17:04:16 -07001822 private void goLive() {
1823 Tab t = getCurrentTab();
1824 t.loadUrl(t.getUrl(), null);
1825 }
1826
Michael Kolb315d5022011-10-13 12:47:11 -07001827 @Override
1828 public void showPageInfo() {
1829 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
1830 }
1831
John Reck9c35b9c2012-05-30 10:08:50 -07001832 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001833 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001834 // Let the History and Bookmark fragments handle menus they created.
1835 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1836 return false;
1837 }
1838
Michael Kolb8233fac2010-10-26 16:08:53 -07001839 int id = item.getItemId();
1840 boolean result = true;
1841 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001842 // -- Browser context menu
1843 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001844 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001845 case R.id.copy_link_context_menu_id:
1846 final WebView webView = getCurrentTopWebView();
1847 if (null == webView) {
1848 result = false;
1849 break;
1850 }
1851 final HashMap<String, WebView> hrefMap =
1852 new HashMap<String, WebView>();
1853 hrefMap.put("webview", webView);
1854 final Message msg = mHandler.obtainMessage(
1855 FOCUS_NODE_HREF, id, 0, hrefMap);
1856 webView.requestFocusNodeHref(msg);
1857 break;
1858
1859 default:
1860 // For other context menus
1861 result = onOptionsItemSelected(item);
1862 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001863 return result;
1864 }
1865
1866 /**
1867 * support programmatically opening the context menu
1868 */
1869 public void openContextMenu(View view) {
1870 mActivity.openContextMenu(view);
1871 }
1872
1873 /**
1874 * programmatically open the options menu
1875 */
1876 public void openOptionsMenu() {
1877 mActivity.openOptionsMenu();
1878 }
1879
John Reck9c35b9c2012-05-30 10:08:50 -07001880 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001881 public boolean onMenuOpened(int featureId, Menu menu) {
1882 if (mOptionsMenuOpen) {
1883 if (mConfigChanged) {
1884 // We do not need to make any changes to the state of the
1885 // title bar, since the only thing that happened was a
1886 // change in orientation
1887 mConfigChanged = false;
1888 } else {
1889 if (!mExtendedMenuOpen) {
1890 mExtendedMenuOpen = true;
1891 mUi.onExtendedMenuOpened();
1892 } else {
1893 // Switching the menu back to icon view, so show the
1894 // title bar once again.
1895 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001896 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001897 }
1898 }
1899 } else {
1900 // The options menu is closed, so open it, and show the title
1901 mOptionsMenuOpen = true;
1902 mConfigChanged = false;
1903 mExtendedMenuOpen = false;
1904 mUi.onOptionsMenuOpened();
1905 }
1906 return true;
1907 }
1908
John Reck9c35b9c2012-05-30 10:08:50 -07001909 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001910 public void onOptionsMenuClosed(Menu menu) {
1911 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001912 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001913 }
1914
John Reck9c35b9c2012-05-30 10:08:50 -07001915 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001916 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001917 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001918 }
1919
1920 // Helper method for getting the top window.
1921 @Override
1922 public WebView getCurrentTopWebView() {
1923 return mTabControl.getCurrentTopWebView();
1924 }
1925
1926 @Override
1927 public WebView getCurrentWebView() {
1928 return mTabControl.getCurrentWebView();
1929 }
1930
1931 /*
1932 * This method is called as a result of the user selecting the options
1933 * menu to see the download window. It shows the download window on top of
1934 * the current window.
1935 */
1936 void viewDownloads() {
1937 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1938 mActivity.startActivity(intent);
1939 }
1940
John Reck30b065e2011-07-19 10:58:05 -07001941 int getActionModeHeight() {
1942 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1943 new int[] { android.R.attr.actionBarSize });
1944 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1945 actionBarSizeTypedArray.recycle();
1946 return size;
1947 }
1948
Michael Kolb8233fac2010-10-26 16:08:53 -07001949 // action mode
1950
John Reck9c35b9c2012-05-30 10:08:50 -07001951 @Override
1952 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001953 mUi.onActionModeStarted(mode);
1954 mActionMode = mode;
1955 }
1956
1957 /*
1958 * True if a custom ActionMode (i.e. find or select) is in use.
1959 */
1960 @Override
1961 public boolean isInCustomActionMode() {
1962 return mActionMode != null;
1963 }
1964
1965 /*
1966 * End the current ActionMode.
1967 */
1968 @Override
1969 public void endActionMode() {
1970 if (mActionMode != null) {
1971 mActionMode.finish();
1972 }
1973 }
1974
1975 /*
1976 * Called by find and select when they are finished. Replace title bars
1977 * as necessary.
1978 */
John Reck9c35b9c2012-05-30 10:08:50 -07001979 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001980 public void onActionModeFinished(ActionMode mode) {
1981 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001982 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001983 mActionMode = null;
1984 }
1985
1986 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08001987 final Tab tab = getCurrentTab();
1988 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07001989 }
1990
1991 // bookmark handling
1992
1993 /**
1994 * add the current page as a bookmark to the given folder id
1995 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07001996 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001997 * bookmarked, and if it is, edit that bookmark. If false, and
1998 * the site is already bookmarked, do not attempt to edit the
1999 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002000 */
2001 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002002 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002003 WebView w = getCurrentTopWebView();
2004 if (w == null) {
2005 return null;
2006 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002007 Intent i = new Intent(mActivity,
2008 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002009 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2010 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2011 String touchIconUrl = w.getTouchIconUrl();
2012 if (touchIconUrl != null) {
2013 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2014 WebSettings settings = w.getSettings();
2015 if (settings != null) {
2016 i.putExtra(AddBookmarkPage.USER_AGENT,
2017 settings.getUserAgentString());
2018 }
2019 }
2020 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2021 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2022 getDesiredThumbnailHeight(mActivity)));
2023 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002024 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002025 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2026 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002027 // Put the dialog at the upper right of the screen, covering the
2028 // star on the title bar.
2029 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002030 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002031 }
2032
2033 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002034 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002035 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002036 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002037 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002038 }
2039
2040 // thumbnails
2041
2042 /**
2043 * Return the desired width for thumbnail screenshots, which are stored in
2044 * the database, and used on the bookmarks screen.
2045 * @param context Context for finding out the density of the screen.
2046 * @return desired width for thumbnail screenshot.
2047 */
2048 static int getDesiredThumbnailWidth(Context context) {
2049 return context.getResources().getDimensionPixelOffset(
2050 R.dimen.bookmarkThumbnailWidth);
2051 }
2052
2053 /**
2054 * Return the desired height for thumbnail screenshots, which are stored in
2055 * the database, and used on the bookmarks screen.
2056 * @param context Context for finding out the density of the screen.
2057 * @return desired height for thumbnail screenshot.
2058 */
2059 static int getDesiredThumbnailHeight(Context context) {
2060 return context.getResources().getDimensionPixelOffset(
2061 R.dimen.bookmarkThumbnailHeight);
2062 }
2063
John Reck8cc92352011-07-06 17:41:52 -07002064 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002065 if (view == null || view.getContentHeight() == 0
2066 || view.getContentWidth() == 0) {
2067 return null;
2068 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002069 // We render to a bitmap 2x the desired size so that we can then
2070 // re-scale it with filtering since canvas.scale doesn't filter
2071 // This helps reduce aliasing at the cost of being slightly blurry
2072 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002073 int scaledWidth = width * filter_scale;
2074 int scaledHeight = height * filter_scale;
2075 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2076 || sThumbnailBitmap.getHeight() != scaledHeight) {
2077 if (sThumbnailBitmap != null) {
2078 sThumbnailBitmap.recycle();
2079 sThumbnailBitmap = null;
2080 }
2081 sThumbnailBitmap =
2082 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002083 }
John Reckd7dd9b22011-08-30 09:18:29 -07002084 Canvas canvas = new Canvas(sThumbnailBitmap);
2085 int contentWidth = view.getContentWidth();
2086 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2087 if (view instanceof BrowserWebView) {
2088 int dy = -((BrowserWebView)view).getTitleHeight();
2089 canvas.translate(0, dy * overviewScale);
2090 }
2091
2092 canvas.scale(overviewScale, overviewScale);
2093
2094 if (view instanceof BrowserWebView) {
2095 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002096 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002097 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002098 }
John Reckd7dd9b22011-08-30 09:18:29 -07002099 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2100 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002101 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002102 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002103 }
2104
John Reck34ef2672011-02-10 11:30:55 -08002105 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002106 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002107 // FIXME: Would like to make sure there is actually something to
2108 // draw, but the API for that (WebViewCore.pictureReady()) is not
2109 // currently accessible here.
2110
John Reck34ef2672011-02-10 11:30:55 -08002111 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002112 if (view == null) {
2113 // Tab was destroyed
2114 return;
2115 }
John Reck34ef2672011-02-10 11:30:55 -08002116 final String url = tab.getUrl();
2117 final String originalUrl = view.getOriginalUrl();
John Reck34ef2672011-02-10 11:30:55 -08002118 if (TextUtils.isEmpty(url)) {
2119 return;
2120 }
2121
2122 // Only update thumbnails for web urls (http(s)://), not for
2123 // about:, javascript:, data:, etc...
2124 // Unless it is a bookmarked site, then always update
2125 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2126 return;
2127 }
2128
Michael Kolb8233fac2010-10-26 16:08:53 -07002129 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2130 getDesiredThumbnailHeight(mActivity));
2131 if (bm == null) {
2132 return;
2133 }
2134
2135 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002136 new AsyncTask<Void, Void, Void>() {
2137 @Override
2138 protected Void doInBackground(Void... unused) {
2139 Cursor cursor = null;
2140 try {
2141 // TODO: Clean this up
2142 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2143 if (cursor != null && cursor.moveToFirst()) {
2144 final ByteArrayOutputStream os =
2145 new ByteArrayOutputStream();
2146 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002147
John Reck34ef2672011-02-10 11:30:55 -08002148 ContentValues values = new ContentValues();
2149 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002150
John Reck34ef2672011-02-10 11:30:55 -08002151 do {
John Reck617fd832011-02-16 14:35:59 -08002152 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002153 cr.update(Images.CONTENT_URI, values, null, null);
2154 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002155 }
John Reck34ef2672011-02-10 11:30:55 -08002156 } catch (IllegalStateException e) {
2157 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002158 } catch (SQLiteException s) {
2159 // Added for possible error when user tries to remove the same bookmark
2160 // that is being updated with a screen shot
2161 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002162 } finally {
2163 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002164 }
John Reck34ef2672011-02-10 11:30:55 -08002165 return null;
2166 }
2167 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002168 }
2169
2170 private class Copy implements OnMenuItemClickListener {
2171 private CharSequence mText;
2172
John Reck9c35b9c2012-05-30 10:08:50 -07002173 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002174 public boolean onMenuItemClick(MenuItem item) {
2175 copy(mText);
2176 return true;
2177 }
2178
2179 public Copy(CharSequence toCopy) {
2180 mText = toCopy;
2181 }
2182 }
2183
Leon Scroggins63c02662010-11-18 15:16:27 -05002184 private static class Download implements OnMenuItemClickListener {
2185 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002186 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002187 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002188 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002189 private static final String FALLBACK_EXTENSION = "dat";
2190 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002191
John Reck9c35b9c2012-05-30 10:08:50 -07002192 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002193 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002194 if (DataUri.isDataUri(mText)) {
2195 saveDataUri();
2196 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002197 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07002198 null, null, null, mPrivateBrowsing);
George Mount387d45d2011-10-07 15:57:53 -07002199 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002200 return true;
2201 }
2202
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002203 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2204 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002205 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002206 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002207 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002208 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002209 }
George Mount387d45d2011-10-07 15:57:53 -07002210
2211 /**
2212 * Treats mText as a data URI and writes its contents to a file
2213 * based on the current time.
2214 */
2215 private void saveDataUri() {
2216 FileOutputStream outputStream = null;
2217 try {
2218 DataUri uri = new DataUri(mText);
2219 File target = getTarget(uri);
2220 outputStream = new FileOutputStream(target);
2221 outputStream.write(uri.getData());
2222 final DownloadManager manager =
2223 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2224 manager.addCompletedDownload(target.getName(),
2225 mActivity.getTitle().toString(), false,
2226 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002227 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002228 } catch (IOException e) {
2229 Log.e(LOGTAG, "Could not save data URL");
2230 } finally {
2231 if (outputStream != null) {
2232 try {
2233 outputStream.close();
2234 } catch (IOException e) {
2235 // ignore close errors
2236 }
2237 }
2238 }
2239 }
2240
2241 /**
2242 * Creates a File based on the current time stamp and uses
2243 * the mime type of the DataUri to get the extension.
2244 */
2245 private File getTarget(DataUri uri) throws IOException {
2246 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002247 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002248 String nameBase = format.format(new Date());
2249 String mimeType = uri.getMimeType();
2250 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2251 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2252 if (extension == null) {
2253 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2254 extension = FALLBACK_EXTENSION;
2255 }
2256 extension = "." + extension; // createTempFile needs the '.'
2257 File targetFile = File.createTempFile(nameBase, extension, dir);
2258 return targetFile;
2259 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002260 }
2261
Cary Clark8974d282010-11-22 10:46:05 -05002262 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002263 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002264
John Reck9c35b9c2012-05-30 10:08:50 -07002265 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002266 public boolean onMenuItemClick(MenuItem item) {
2267 if (mWebView != null) {
2268 return mWebView.selectText();
2269 }
2270 return false;
2271 }
2272
2273 public SelectText(WebView webView) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002274 if (BrowserWebView.isClassic()) {
2275 mWebView = WebViewClassic.fromWebView(webView);
2276 }
Cary Clark8974d282010-11-22 10:46:05 -05002277 }
2278
2279 }
2280
Michael Kolb8233fac2010-10-26 16:08:53 -07002281 /********************** TODO: UI stuff *****************************/
2282
2283 // these methods have been copied, they still need to be cleaned up
2284
2285 /****************** tabs ***************************************************/
2286
2287 // basic tab interactions:
2288
2289 // it is assumed that tabcontrol already knows about the tab
2290 protected void addTab(Tab tab) {
2291 mUi.addTab(tab);
2292 }
2293
2294 protected void removeTab(Tab tab) {
2295 mUi.removeTab(tab);
2296 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002297 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002298 }
2299
Michael Kolbf2055602011-04-09 17:20:03 -07002300 @Override
2301 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002302 // monkey protection against delayed start
2303 if (tab != null) {
2304 mTabControl.setCurrentTab(tab);
2305 // the tab is guaranteed to have a webview after setCurrentTab
2306 mUi.setActiveTab(tab);
2307 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002308 }
2309
John Reck8bcafc12011-08-29 16:43:02 -07002310 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002311 Tab current = mTabControl.getCurrentTab();
2312 if (current != null
2313 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002314 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002315 }
2316 }
2317
John Reck26b18322011-06-21 13:08:58 -07002318 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002319 // Dismiss the subwindow if applicable.
2320 dismissSubWindow(appTab);
2321 // Since we might kill the WebView, remove it from the
2322 // content view first.
2323 mUi.detachTab(appTab);
2324 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002325 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002326 // TODO: analyze why the remove and add are necessary
2327 mUi.attachTab(appTab);
2328 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002329 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002330 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002331 } else {
2332 // If the tab was the current tab, we have to attach
2333 // it to the view system again.
2334 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002335 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002336 }
2337 }
2338
2339 // Remove the sub window if it exists. Also called by TabControl when the
2340 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002341 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002342 public void dismissSubWindow(Tab tab) {
2343 removeSubWindow(tab);
2344 // dismiss the subwindow. This will destroy the WebView.
2345 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002346 WebView wv = getCurrentTopWebView();
2347 if (wv != null) {
2348 wv.requestFocus();
2349 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002350 }
2351
2352 @Override
2353 public void removeSubWindow(Tab t) {
2354 if (t.getSubWebView() != null) {
2355 mUi.removeSubWindow(t.getSubViewContainer());
2356 }
2357 }
2358
2359 @Override
2360 public void attachSubWindow(Tab tab) {
2361 if (tab.getSubWebView() != null) {
2362 mUi.attachSubWindow(tab.getSubViewContainer());
2363 getCurrentTopWebView().requestFocus();
2364 }
2365 }
2366
Mathew Inwood29721c22011-06-29 17:55:24 +01002367 private Tab showPreloadedTab(final UrlData urlData) {
2368 if (!urlData.isPreloaded()) {
2369 return null;
2370 }
2371 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2372 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2373 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002374 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002375 // Could not submit query. Fallback to regular tab creation
2376 tabControl.destroy();
2377 return null;
2378 }
2379 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002380 // check tab count and make room for new tab
2381 if (!mTabControl.canCreateNewTab()) {
2382 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2383 if (leastUsed != null) {
2384 closeTab(leastUsed);
2385 }
2386 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002387 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002388 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002389 mTabControl.addPreloadedTab(t);
2390 addTab(t);
2391 setActiveTab(t);
2392 return t;
2393 }
2394
Michael Kolb7bcafde2011-05-09 13:55:59 -07002395 // open a non inconito tab with the given url data
2396 // and set as active tab
2397 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002398 Tab tab = showPreloadedTab(urlData);
2399 if (tab == null) {
2400 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002401 if ((tab != null) && !urlData.isEmpty()) {
2402 loadUrlDataIn(tab, urlData);
2403 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002404 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002405 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002406 }
2407
Michael Kolb843510f2010-12-09 10:51:49 -08002408 @Override
2409 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002410 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002411 }
2412
Michael Kolb8233fac2010-10-26 16:08:53 -07002413 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002414 public Tab openIncognitoTab() {
2415 return openTab(INCOGNITO_URI, true, true, false);
2416 }
2417
2418 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002419 public Tab openTab(String url, boolean incognito, boolean setActive,
2420 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002421 return openTab(url, incognito, setActive, useCurrent, null);
2422 }
2423
2424 @Override
2425 public Tab openTab(String url, Tab parent, boolean setActive,
2426 boolean useCurrent) {
2427 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2428 setActive, useCurrent, parent);
2429 }
2430
2431 public Tab openTab(String url, boolean incognito, boolean setActive,
2432 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002433 Tab tab = createNewTab(incognito, setActive, useCurrent);
2434 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002435 if (parent != null && parent != tab) {
2436 parent.addChildTab(tab);
2437 }
Michael Kolb519d2282011-05-09 17:03:19 -07002438 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002439 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002440 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002441 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002442 return tab;
2443 }
2444
2445 // this method will attempt to create a new tab
2446 // incognito: private browsing tab
2447 // setActive: ste tab as current tab
2448 // useCurrent: if no new tab can be created, return current tab
2449 private Tab createNewTab(boolean incognito, boolean setActive,
2450 boolean useCurrent) {
2451 Tab tab = null;
2452 if (mTabControl.canCreateNewTab()) {
2453 tab = mTabControl.createNewTab(incognito);
2454 addTab(tab);
2455 if (setActive) {
2456 setActiveTab(tab);
2457 }
2458 } else {
2459 if (useCurrent) {
2460 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002461 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002462 } else {
2463 mUi.showMaxTabsWarning();
2464 }
2465 }
2466 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002467 }
2468
John Reck2bc80422011-06-30 15:11:49 -07002469 @Override
2470 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2471 SnapshotTab tab = null;
2472 if (mTabControl.canCreateNewTab()) {
2473 tab = mTabControl.createSnapshotTab(snapshotId);
2474 addTab(tab);
2475 if (setActive) {
2476 setActiveTab(tab);
2477 }
2478 } else {
2479 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002480 }
2481 return tab;
2482 }
2483
Michael Kolb8233fac2010-10-26 16:08:53 -07002484 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002485 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002486 * @return boolean True if we successfully switched to a different tab. If
2487 * the indexth tab is null, or if that tab is the same as
2488 * the current one, return false.
2489 */
2490 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002491 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002492 Tab currentTab = mTabControl.getCurrentTab();
2493 if (tab == null || tab == currentTab) {
2494 return false;
2495 }
2496 setActiveTab(tab);
2497 return true;
2498 }
2499
2500 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002501 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002502 closeCurrentTab(false);
2503 }
2504
2505 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002506 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002507 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002508 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002509 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002510 return;
2511 }
Michael Kolbc831b632011-05-11 09:30:34 -07002512 final Tab current = mTabControl.getCurrentTab();
2513 final int pos = mTabControl.getCurrentPosition();
2514 Tab newTab = current.getParent();
2515 if (newTab == null) {
2516 newTab = mTabControl.getTab(pos + 1);
2517 if (newTab == null) {
2518 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002519 }
2520 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002521 if (andQuit) {
2522 mTabControl.setCurrentTab(newTab);
2523 closeTab(current);
2524 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002525 // Close window
2526 closeTab(current);
2527 }
2528 }
2529
2530 /**
2531 * Close the tab, remove its associated title bar, and adjust mTabControl's
2532 * current tab to a valid value.
2533 */
2534 @Override
2535 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002536 if (tab == mTabControl.getCurrentTab()) {
2537 closeCurrentTab();
2538 } else {
2539 removeTab(tab);
2540 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002541 }
2542
Afzal Najamd4e33312012-04-26 01:54:01 -04002543 /**
2544 * Close all tabs except the current one
2545 */
2546 @Override
2547 public void closeOtherTabs() {
2548 int inactiveTabs = mTabControl.getTabCount() - 1;
2549 for (int i = inactiveTabs; i >= 0; i--) {
2550 Tab tab = mTabControl.getTab(i);
2551 if (tab != mTabControl.getCurrentTab()) {
2552 removeTab(tab);
2553 }
2554 }
2555 }
2556
Michael Kolb8233fac2010-10-26 16:08:53 -07002557 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002558 protected void loadUrlFromContext(String url) {
2559 Tab tab = getCurrentTab();
2560 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002561 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002562 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002563 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002564 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002565 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002566 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002567 }
2568 }
2569 }
2570
2571 /**
2572 * Load the URL into the given WebView and update the title bar
2573 * to reflect the new load. Call this instead of WebView.loadUrl
2574 * directly.
2575 * @param view The WebView used to load url.
2576 * @param url The URL to load.
2577 */
John Reck71e51422011-07-01 16:49:28 -07002578 @Override
2579 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002580 loadUrl(tab, url, null);
2581 }
2582
2583 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2584 if (tab != null) {
2585 dismissSubWindow(tab);
2586 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002587 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002588 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002589 }
2590
2591 /**
2592 * Load UrlData into a Tab and update the title bar to reflect the new
2593 * load. Call this instead of UrlData.loadIn directly.
2594 * @param t The Tab used to load.
2595 * @param data The UrlData being loaded.
2596 */
2597 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002598 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002599 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002600 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002601 } else {
John Reck38b39652012-06-05 09:22:59 -07002602 if (t != null && data.mDisableUrlOverride) {
2603 t.disableUrlOverridingForLoad();
2604 }
John Reck26b18322011-06-21 13:08:58 -07002605 loadUrl(t, data.mUrl, data.mHeaders);
2606 }
2607 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 }
2609
John Reck30c714c2010-12-16 17:30:34 -08002610 @Override
2611 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002612 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002613 if (tab.canGoBack()) {
2614 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002615 } else {
John Reckef654f12011-07-12 16:42:08 -07002616 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002617 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002618 }
2619
2620 void goBackOnePageOrQuit() {
2621 Tab current = mTabControl.getCurrentTab();
2622 if (current == null) {
2623 /*
2624 * Instead of finishing the activity, simply push this to the back
2625 * of the stack and let ActivityManager to choose the foreground
2626 * activity. As BrowserActivity is singleTask, it will be always the
2627 * root of the task. So we can use either true or false for
2628 * moveTaskToBack().
2629 */
2630 mActivity.moveTaskToBack(true);
2631 return;
2632 }
John Reckef654f12011-07-12 16:42:08 -07002633 if (current.canGoBack()) {
2634 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002635 } else {
2636 // Check to see if we are closing a window that was created by
2637 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002638 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002639 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002640 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002641 // Now we close the other tab
2642 closeTab(current);
2643 } else {
Michael Kolbe28b3472011-08-04 16:54:31 -07002644 if ((current.getAppId() != null) || current.closeOnBack()) {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002645 closeCurrentTab(true);
Michael Kolbe28b3472011-08-04 16:54:31 -07002646 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002647 /*
2648 * Instead of finishing the activity, simply push this to the back
2649 * of the stack and let ActivityManager to choose the foreground
2650 * activity. As BrowserActivity is singleTask, it will be always the
2651 * root of the task. So we can use either true or false for
2652 * moveTaskToBack().
2653 */
2654 mActivity.moveTaskToBack(true);
2655 }
2656 }
2657 }
2658
2659 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002660 * helper method for key handler
2661 * returns the current tab if it can't advance
2662 */
Michael Kolbc831b632011-05-11 09:30:34 -07002663 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002664 int pos = mTabControl.getCurrentPosition() + 1;
2665 if (pos >= mTabControl.getTabCount()) {
2666 pos = 0;
2667 }
2668 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002669 }
2670
2671 /**
2672 * helper method for key handler
2673 * returns the current tab if it can't advance
2674 */
Michael Kolbc831b632011-05-11 09:30:34 -07002675 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002676 int pos = mTabControl.getCurrentPosition() - 1;
2677 if ( pos < 0) {
2678 pos = mTabControl.getTabCount() - 1;
2679 }
2680 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002681 }
2682
John Reckbcef87f2012-02-03 14:58:34 -08002683 boolean isMenuOrCtrlKey(int keyCode) {
2684 return (KeyEvent.KEYCODE_MENU == keyCode)
2685 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2686 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2687 }
2688
Michael Kolb0035fad2011-03-14 13:25:28 -07002689 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002690 * handle key events in browser
2691 *
2692 * @param keyCode
2693 * @param event
2694 * @return true if handled, false to pass to super
2695 */
John Reck9c35b9c2012-05-30 10:08:50 -07002696 @Override
2697 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002698 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002699 // Even if MENU is already held down, we need to call to super to open
2700 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002701 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002702 mMenuIsDown = true;
2703 return false;
2704 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002705
Cary Clark8ff8c662010-12-29 15:03:05 -05002706 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002707 Tab tab = getCurrentTab();
2708 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002709
Cary Clark160bbb92011-01-10 11:17:07 -05002710 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2711 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002712
Michael Kolb8233fac2010-10-26 16:08:53 -07002713 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002714 case KeyEvent.KEYCODE_TAB:
2715 if (event.isCtrlPressed()) {
2716 if (event.isShiftPressed()) {
2717 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002718 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002719 } else {
2720 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002721 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002722 }
2723 return true;
2724 }
2725 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002726 case KeyEvent.KEYCODE_SPACE:
2727 // WebView/WebTextView handle the keys in the KeyDown. As
2728 // the Activity's shortcut keys are only handled when WebView
2729 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002730 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002731 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002732 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002733 pageDown();
2734 }
2735 return true;
2736 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002737 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002738 event.startTracking();
2739 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002740 case KeyEvent.KEYCODE_FORWARD:
2741 if (!noModifiers) break;
2742 tab.goForward();
2743 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002744 case KeyEvent.KEYCODE_DPAD_LEFT:
2745 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002746 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002747 return true;
2748 }
2749 break;
2750 case KeyEvent.KEYCODE_DPAD_RIGHT:
2751 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002752 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002753 return true;
2754 }
2755 break;
2756 case KeyEvent.KEYCODE_A:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002757 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002758 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002759 return true;
2760 }
2761 break;
Michael Kolba4183062011-01-16 10:43:21 -08002762// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002763 case KeyEvent.KEYCODE_C:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002764 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002765 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002766 return true;
2767 }
2768 break;
Michael Kolba4183062011-01-16 10:43:21 -08002769// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002770// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002771// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002772// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002773// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002774// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002775// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002776// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002777// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002778// case KeyEvent.KEYCODE_M: // unused
2779// case KeyEvent.KEYCODE_N: // in Chrome: new window
2780// case KeyEvent.KEYCODE_O: // in Chrome: open file
2781// case KeyEvent.KEYCODE_P: // in Chrome: print page
2782// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002783// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002784// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2785 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002786 // we can't use the ctrl/shift flags, they check for
2787 // exclusive use of a modifier
2788 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002789 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002790 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002791 } else {
2792 openTabToHomePage();
2793 }
2794 return true;
2795 }
2796 break;
2797// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2798// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002799// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002800// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2801// case KeyEvent.KEYCODE_Y: // unused
2802// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002803 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002804 // it is a regular key and webview is not null
2805 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002806 }
2807
John Reck9c35b9c2012-05-30 10:08:50 -07002808 @Override
2809 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002810 switch(keyCode) {
2811 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002812 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002813 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002814 return true;
2815 }
2816 break;
2817 }
2818 return false;
2819 }
2820
John Reck9c35b9c2012-05-30 10:08:50 -07002821 @Override
2822 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002823 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002824 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002825 if (KeyEvent.KEYCODE_MENU == keyCode
2826 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002827 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002828 }
2829 }
Cary Clark160bbb92011-01-10 11:17:07 -05002830 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002831 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002832 case KeyEvent.KEYCODE_BACK:
2833 if (event.isTracking() && !event.isCanceled()) {
2834 onBackKey();
2835 return true;
2836 }
2837 break;
2838 }
2839 return false;
2840 }
2841
2842 public boolean isMenuDown() {
2843 return mMenuIsDown;
2844 }
2845
John Reck9c35b9c2012-05-30 10:08:50 -07002846 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002847 public void setupAutoFill(Message message) {
2848 // Open the settings activity at the AutoFill profile fragment so that
2849 // the user can create a new profile. When they return, we will dispatch
2850 // the message so that we can autofill the form using their new profile.
2851 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2852 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2853 AutoFillSettingsFragment.class.getName());
2854 mAutoFillSetupMessage = message;
2855 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2856 }
John Reckb3417f02011-01-14 11:01:05 -08002857
John Reck9c35b9c2012-05-30 10:08:50 -07002858 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002859 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002860 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002861 return true;
2862 }
2863
John Reck1cf4b792011-07-26 10:22:22 -07002864 @Override
2865 public boolean shouldCaptureThumbnails() {
2866 return mUi.shouldCaptureThumbnails();
2867 }
2868
Michael Kolbc3af0672011-08-09 10:24:41 -07002869 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07002870 public boolean supportsVoice() {
2871 PackageManager pm = mActivity.getPackageManager();
2872 List activities = pm.queryIntentActivities(new Intent(
2873 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
2874 return activities.size() != 0;
2875 }
2876
2877 @Override
2878 public void startVoiceRecognizer() {
2879 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
2880 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
2881 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
2882 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
2883 mActivity.startActivityForResult(voice, VOICE_RESULT);
2884 }
2885
2886 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002887 public void setBlockEvents(boolean block) {
2888 mBlockEvents = block;
2889 }
2890
John Reck9c35b9c2012-05-30 10:08:50 -07002891 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002892 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002893 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002894 }
2895
John Reck9c35b9c2012-05-30 10:08:50 -07002896 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002897 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002898 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002899 }
2900
John Reck9c35b9c2012-05-30 10:08:50 -07002901 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002902 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002903 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002904 }
2905
John Reck9c35b9c2012-05-30 10:08:50 -07002906 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002907 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002908 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002909 }
2910
John Reck9c35b9c2012-05-30 10:08:50 -07002911 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002912 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002913 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002914 }
2915
Michael Kolb8233fac2010-10-26 16:08:53 -07002916}