blob: 7b7fcd48f74cafe2f78ed1ce2fd41a68903381c0 [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;
Ben Murdoch8029a772010-11-16 11:58:21 +000050import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.provider.Browser;
52import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070053import android.provider.BrowserContract.Images;
54import android.provider.ContactsContract;
55import android.provider.ContactsContract.Intents.Insert;
Michael Kolb0b129122012-06-04 16:31:58 -070056import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070057import android.text.TextUtils;
58import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080059import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070060import android.view.ActionMode;
61import android.view.ContextMenu;
62import android.view.ContextMenu.ContextMenuInfo;
63import android.view.Gravity;
64import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070065import android.view.Menu;
66import android.view.MenuInflater;
67import android.view.MenuItem;
68import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070069import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070070import android.view.View;
71import android.webkit.CookieManager;
72import android.webkit.CookieSyncManager;
73import android.webkit.HttpAuthHandler;
George Mount387d45d2011-10-07 15:57:53 -070074import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070075import android.webkit.SslErrorHandler;
76import android.webkit.ValueCallback;
77import android.webkit.WebChromeClient;
78import android.webkit.WebIconDatabase;
79import android.webkit.WebSettings;
80import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000081import android.webkit.WebViewClassic;
Leon Scrogginsac993842011-02-02 12:54:07 -050082import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070083
Michael Kolb4bd767d2011-05-27 11:33:55 -070084import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070085import com.android.browser.UI.ComboViews;
John Reck1cf4b792011-07-26 10:22:22 -070086import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070087import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070088
Michael Kolb8233fac2010-10-26 16:08:53 -070089import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -070090import java.io.File;
91import java.io.FileOutputStream;
92import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -070093import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -070094import java.text.DateFormat;
95import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -070096import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -070097import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -070098import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -070099import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800100import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200101import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700102import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700103
104/**
105 * Controller for browser
106 */
107public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700108 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700109
110 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800111 private static final String SEND_APP_ID_EXTRA =
112 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700113 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800114
Michael Kolb8233fac2010-10-26 16:08:53 -0700115
116 // public message ids
117 public final static int LOAD_URL = 1001;
118 public final static int STOP_LOAD = 1002;
119
120 // Message Ids
121 private static final int FOCUS_NODE_HREF = 102;
122 private static final int RELEASE_WAKELOCK = 107;
123
124 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
125
126 private static final int OPEN_BOOKMARKS = 201;
127
128 private static final int EMPTY_MENU = -1;
129
Michael Kolb8233fac2010-10-26 16:08:53 -0700130 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700131 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700132 final static int PREFERENCES_PAGE = 3;
133 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000134 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700135 final static int VOICE_RESULT = 6;
Ben Murdoch8029a772010-11-16 11:58:21 +0000136
Michael Kolb8233fac2010-10-26 16:08:53 -0700137 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
138
139 // As the ids are dynamically created, we can't guarantee that they will
140 // be in sequence, so this static array maps ids to a window number.
141 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
142 { R.id.window_one_menu_id, R.id.window_two_menu_id,
143 R.id.window_three_menu_id, R.id.window_four_menu_id,
144 R.id.window_five_menu_id, R.id.window_six_menu_id,
145 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
146
147 // "source" parameter for Google search through search key
148 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
149 // "source" parameter for Google search through simplily type
150 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
151
George Mount387d45d2011-10-07 15:57:53 -0700152 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700153 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
154
John Reckd7dd9b22011-08-30 09:18:29 -0700155 // A bitmap that is re-used in createScreenshot as scratch space
156 private static Bitmap sThumbnailBitmap;
157
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 private Activity mActivity;
159 private UI mUi;
160 private TabControl mTabControl;
161 private BrowserSettings mSettings;
162 private WebViewFactory mFactory;
163
164 private WakeLock mWakeLock;
165
166 private UrlHandler mUrlHandler;
167 private UploadHandler mUploadHandler;
168 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700169 private PageDialogsHandler mPageDialogsHandler;
170 private NetworkStateHandler mNetworkHandler;
171
Ben Murdoch8029a772010-11-16 11:58:21 +0000172 private Message mAutoFillSetupMessage;
173
Michael Kolb8233fac2010-10-26 16:08:53 -0700174 private boolean mShouldShowErrorConsole;
175
176 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
177
178 // FIXME, temp address onPrepareMenu performance problem.
179 // When we move everything out of view, we should rewrite this.
180 private int mCurrentMenuState = 0;
181 private int mMenuState = R.id.MAIN_MENU;
182 private int mOldMenuState = EMPTY_MENU;
183 private Menu mCachedMenu;
184
Michael Kolb8233fac2010-10-26 16:08:53 -0700185 private boolean mMenuIsDown;
186
187 // For select and find, we keep track of the ActionMode so that
188 // finish() can be called as desired.
189 private ActionMode mActionMode;
190
191 /**
192 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
193 * of whether the configuration has changed. The first onMenuOpened call
194 * after a configuration change is simply a reopening of the same menu
195 * (i.e. mIconView did not change).
196 */
197 private boolean mConfigChanged;
198
199 /**
200 * Keeps track of whether the options menu is open. This is important in
201 * determining whether to show or hide the title bar overlay
202 */
203 private boolean mOptionsMenuOpen;
204
205 /**
206 * Whether or not the options menu is in its bigger, popup menu form. When
207 * true, we want the title bar overlay to be gone. When false, we do not.
208 * Only meaningful if mOptionsMenuOpen is true.
209 */
210 private boolean mExtendedMenuOpen;
211
Michael Kolb8233fac2010-10-26 16:08:53 -0700212 private boolean mActivityPaused = true;
213 private boolean mLoadStopped;
214
215 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500216 // Checks to see when the bookmarks database has changed, and updates the
217 // Tabs' notion of whether they represent bookmarked sites.
218 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700219 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700220
Michael Kolbc3af0672011-08-09 10:24:41 -0700221 private boolean mBlockEvents;
222
Michael Kolb0b129122012-06-04 16:31:58 -0700223 private String mVoiceResult;
224
George Mount3636d0a2011-11-21 09:08:21 -0800225 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700226 mActivity = browser;
227 mSettings = BrowserSettings.getInstance();
228 mTabControl = new TabControl(this);
229 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700230 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800231 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700232 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700233
234 mUrlHandler = new UrlHandler(this);
235 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
237
Michael Kolb8233fac2010-10-26 16:08:53 -0700238 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500239 mBookmarksObserver = new ContentObserver(mHandler) {
240 @Override
241 public void onChange(boolean selfChange) {
242 int size = mTabControl.getTabCount();
243 for (int i = 0; i < size; i++) {
244 mTabControl.getTab(i).updateBookmarkedStatus();
245 }
246 }
247
248 };
249 browser.getContentResolver().registerContentObserver(
250 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700251
252 mNetworkHandler = new NetworkStateHandler(mActivity, this);
253 // Start watching the default geolocation permissions
254 mSystemAllowGeolocationOrigins =
255 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
256 mSystemAllowGeolocationOrigins.start();
257
John Reckaf262e72011-07-25 13:55:44 -0700258 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700259 }
260
John Reck9c35b9c2012-05-30 10:08:50 -0700261 @Override
262 public void start(final Intent intent) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800263 if (BrowserWebView.isClassic()) WebViewClassic.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800264 // mCrashRecoverHandler has any previously saved state.
265 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700266 }
267
George Mount3636d0a2011-11-21 09:08:21 -0800268 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700269 // Unless the last browser usage was within 24 hours, destroy any
270 // remaining incognito tabs.
271
272 Calendar lastActiveDate = icicle != null ?
273 (Calendar) icicle.getSerializable("lastActiveDate") : null;
274 Calendar today = Calendar.getInstance();
275 Calendar yesterday = Calendar.getInstance();
276 yesterday.add(Calendar.DATE, -1);
277
Patrick Scott7d50a932011-02-04 09:27:26 -0500278 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700279 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800280 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700281
Patrick Scott7d50a932011-02-04 09:27:26 -0500282 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700283 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500284 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000285
Michael Kolbc831b632011-05-11 09:30:34 -0700286 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500287 // Not able to restore so we go ahead and clear session cookies. We
288 // must do this before trying to login the user as we don't want to
289 // clear any session cookies set during login.
290 CookieManager.getInstance().removeSessionCookie();
291 }
292
Patrick Scottd43e75a2011-03-14 14:47:23 -0400293 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500294 new Runnable() {
295 @Override public void run() {
George Mount3636d0a2011-11-21 09:08:21 -0800296 onPreloginFinished(icicle, intent, currentTabId,
297 restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500298 }
299 });
300 }
301
Michael Kolbc831b632011-05-11 09:30:34 -0700302 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
George Mount3636d0a2011-11-21 09:08:21 -0800303 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700304 if (currentTabId == -1) {
John Reck1cf4b792011-07-26 10:22:22 -0700305 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800306 if (intent == null) {
307 // This won't happen under common scenarios. The icicle is
308 // not null, but there aren't any tabs to restore.
309 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700310 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800311 final Bundle extra = intent.getExtras();
312 // Create an initial tab.
313 // If the intent is ACTION_VIEW and data is not null, the Browser is
314 // invoked to view the content by another application. In this case,
315 // the tab will be close when exit.
316 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
317 Tab t = null;
318 if (urlData.isEmpty()) {
319 t = openTabToHomePage();
320 } else {
321 t = openTab(urlData);
322 }
323 if (t != null) {
324 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
325 }
326 WebView webView = t.getWebView();
327 if (extra != null) {
328 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
329 if (scale > 0 && scale <= 1000) {
330 webView.setInitialScale(scale);
331 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700332 }
333 }
John Reckd8c74522011-06-14 08:45:00 -0700334 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700335 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700336 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500337 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700338 List<Tab> tabs = mTabControl.getTabs();
339 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
340 for (Tab t : tabs) {
341 restoredTabs.add(t.getId());
342 }
343 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700344 if (tabs.size() == 0) {
345 openTabToHomePage();
346 }
John Reck1cf4b792011-07-26 10:22:22 -0700347 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700348 // TabControl.restoreState() will create a new tab even if
349 // restoring the state fails.
350 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800351 // Intent is non-null when framework thinks the browser should be
352 // launching with a new intent (icicle is null).
353 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700354 mIntentHandler.onNewIntent(intent);
355 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700356 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700357 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700358 String jsFlags = getSettings().getJsEngineFlags();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800359 if (jsFlags.trim().length() != 0 && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000360 WebViewClassic.fromWebView(getCurrentWebView()).setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700361 }
George Mount3636d0a2011-11-21 09:08:21 -0800362 if (intent != null
363 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700364 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800365 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700366 }
367
John Reck1cf4b792011-07-26 10:22:22 -0700368 private static class PruneThumbnails implements Runnable {
369 private Context mContext;
370 private List<Long> mIds;
371
372 PruneThumbnails(Context context, List<Long> preserveIds) {
373 mContext = context.getApplicationContext();
374 mIds = preserveIds;
375 }
376
377 @Override
378 public void run() {
379 ContentResolver cr = mContext.getContentResolver();
380 if (mIds == null || mIds.size() == 0) {
381 cr.delete(Thumbnails.CONTENT_URI, null, null);
382 } else {
383 int length = mIds.size();
384 StringBuilder where = new StringBuilder();
385 where.append(Thumbnails._ID);
386 where.append(" not in (");
387 for (int i = 0; i < length; i++) {
388 where.append(mIds.get(i));
389 if (i < (length - 1)) {
390 where.append(",");
391 }
392 }
393 where.append(")");
394 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
395 }
396 }
397
398 }
399
Michael Kolb1514bb72010-11-22 09:11:48 -0800400 @Override
401 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700402 return mFactory;
403 }
404
405 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800406 public void onSetWebView(Tab tab, WebView view) {
407 mUi.onSetWebView(tab, view);
408 }
409
410 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800411 public void createSubWindow(Tab tab) {
412 endActionMode();
413 WebView mainView = tab.getWebView();
414 WebView subView = mFactory.createWebView((mainView == null)
415 ? false
416 : mainView.isPrivateBrowsingEnabled());
417 mUi.createSubWindow(tab, subView);
418 }
419
420 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700421 public Context getContext() {
422 return mActivity;
423 }
424
425 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700426 public Activity getActivity() {
427 return mActivity;
428 }
429
430 void setUi(UI ui) {
431 mUi = ui;
432 }
433
Michael Kolbaf63dba2012-05-16 12:58:05 -0700434 @Override
435 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700436 return mSettings;
437 }
438
439 IntentHandler getIntentHandler() {
440 return mIntentHandler;
441 }
442
443 @Override
444 public UI getUi() {
445 return mUi;
446 }
447
448 int getMaxTabs() {
449 return mActivity.getResources().getInteger(R.integer.max_tabs);
450 }
451
452 @Override
453 public TabControl getTabControl() {
454 return mTabControl;
455 }
456
Michael Kolb1bf23132010-11-19 12:55:12 -0800457 @Override
458 public List<Tab> getTabs() {
459 return mTabControl.getTabs();
460 }
461
John Reckaf262e72011-07-25 13:55:44 -0700462 // Open the icon database.
463 private void openIconDatabase() {
464 // We have to call getInstance on the UI thread
465 final WebIconDatabase instance = WebIconDatabase.getInstance();
466 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000467
John Reckaf262e72011-07-25 13:55:44 -0700468 @Override
469 public void run() {
470 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700471 }
John Reckaf262e72011-07-25 13:55:44 -0700472 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700473 }
474
475 private void startHandler() {
476 mHandler = new Handler() {
477
478 @Override
479 public void handleMessage(Message msg) {
480 switch (msg.what) {
481 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700482 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700483 break;
484 case FOCUS_NODE_HREF:
485 {
486 String url = (String) msg.getData().get("url");
487 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500488 String src = (String) msg.getData().get("src");
489 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700490 if (TextUtils.isEmpty(url)) {
491 break;
492 }
493 HashMap focusNodeMap = (HashMap) msg.obj;
494 WebView view = (WebView) focusNodeMap.get("webview");
495 // Only apply the action if the top window did not change.
496 if (getCurrentTopWebView() != view) {
497 break;
498 }
499 switch (msg.arg1) {
500 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700501 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700502 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500503 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700504 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500505 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500506 case R.id.open_newtab_context_menu_id:
507 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700508 openTab(url, parent,
509 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500510 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700511 case R.id.copy_link_context_menu_id:
512 copy(url);
513 break;
514 case R.id.save_link_context_menu_id:
515 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500516 DownloadHandler.onDownloadStartNoStream(
Andreas Sandblad8e4ce662012-06-11 11:02:49 +0200517 mActivity, url, view.getSettings().getUserAgentString(),
Jean-Baptiste Queru2584c312012-09-04 14:39:07 -0700518 null, null, null, view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700519 break;
520 }
521 break;
522 }
523
524 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700525 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700526 break;
527
528 case STOP_LOAD:
529 stopLoading();
530 break;
531
532 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700533 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700534 mWakeLock.release();
535 // if we reach here, Browser should be still in the
536 // background loading after WAKELOCK_TIMEOUT (5-min).
537 // To avoid burning the battery, stop loading.
538 mTabControl.stopAllLoading();
539 }
540 break;
541
542 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800543 Tab tab = (Tab) msg.obj;
544 if (tab != null) {
545 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 }
547 break;
548 }
549 }
550 };
551
552 }
553
John Reckef654f12011-07-12 16:42:08 -0700554 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200555 public Tab getCurrentTab() {
556 return mTabControl.getCurrentTab();
557 }
558
Michael Kolbba99c5d2010-11-29 14:57:41 -0800559 @Override
560 public void shareCurrentPage() {
561 shareCurrentPage(mTabControl.getCurrentTab());
562 }
563
564 private void shareCurrentPage(Tab tab) {
565 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800566 sharePage(mActivity, tab.getTitle(),
567 tab.getUrl(), tab.getFavicon(),
568 createScreenshot(tab.getWebView(),
569 getDesiredThumbnailWidth(mActivity),
570 getDesiredThumbnailHeight(mActivity)));
571 }
572 }
573
Michael Kolb8233fac2010-10-26 16:08:53 -0700574 /**
575 * Share a page, providing the title, url, favicon, and a screenshot. Uses
576 * an {@link Intent} to launch the Activity chooser.
577 * @param c Context used to launch a new Activity.
578 * @param title Title of the page. Stored in the Intent with
579 * {@link Intent#EXTRA_SUBJECT}
580 * @param url URL of the page. Stored in the Intent with
581 * {@link Intent#EXTRA_TEXT}
582 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
583 * with {@link Browser#EXTRA_SHARE_FAVICON}
584 * @param screenshot Bitmap of a screenshot of the page. Stored in the
585 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
586 */
587 static final void sharePage(Context c, String title, String url,
588 Bitmap favicon, Bitmap screenshot) {
589 Intent send = new Intent(Intent.ACTION_SEND);
590 send.setType("text/plain");
591 send.putExtra(Intent.EXTRA_TEXT, url);
592 send.putExtra(Intent.EXTRA_SUBJECT, title);
593 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
594 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
595 try {
596 c.startActivity(Intent.createChooser(send, c.getString(
597 R.string.choosertitle_sharevia)));
598 } catch(android.content.ActivityNotFoundException ex) {
599 // if no app handles it, do nothing
600 }
601 }
602
603 private void copy(CharSequence text) {
604 ClipboardManager cm = (ClipboardManager) mActivity
605 .getSystemService(Context.CLIPBOARD_SERVICE);
606 cm.setText(text);
607 }
608
609 // lifecycle
610
John Reck9c35b9c2012-05-30 10:08:50 -0700611 @Override
612 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700613 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800614 // update the menu in case of a locale change
615 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700616 if (mPageDialogsHandler != null) {
617 mPageDialogsHandler.onConfigurationChanged(config);
618 }
619 mUi.onConfigurationChanged(config);
620 }
621
622 @Override
623 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700624 if (!mUi.isWebShowing()) {
625 mUi.showWeb(false);
626 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700627 mIntentHandler.onNewIntent(intent);
628 }
629
John Reck9c35b9c2012-05-30 10:08:50 -0700630 @Override
631 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800632 if (mUi.isCustomViewShowing()) {
633 hideCustomView();
634 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 if (mActivityPaused) {
636 Log.e(LOGTAG, "BrowserActivity is already paused.");
637 return;
638 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800640 Tab tab = mTabControl.getCurrentTab();
641 if (tab != null) {
642 tab.pause();
643 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700644 if (mWakeLock == null) {
645 PowerManager pm = (PowerManager) mActivity
646 .getSystemService(Context.POWER_SERVICE);
647 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
648 }
Michael Kolb70976932010-11-30 11:34:01 -0800649 mWakeLock.acquire();
650 mHandler.sendMessageDelayed(mHandler
651 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
652 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700653 }
654 mUi.onPause();
655 mNetworkHandler.onPause();
656
657 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100658 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700659 if (sThumbnailBitmap != null) {
660 sThumbnailBitmap.recycle();
661 sThumbnailBitmap = null;
662 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700663 }
664
John Reck9c35b9c2012-05-30 10:08:50 -0700665 @Override
666 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700667 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800668 Bundle saveState = createSaveState();
669
670 // crash recovery manages all save & restore state
671 mCrashRecoveryHandler.writeState(saveState);
672 mSettings.setLastRunPaused(true);
673 }
674
675 /**
676 * Save the current state to outState. Does not write the state to
677 * disk.
678 * @return Bundle containing the current state of all tabs.
679 */
680 /* package */ Bundle createSaveState() {
681 Bundle saveState = new Bundle();
682 mTabControl.saveState(saveState);
683 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700684 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800685 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700686 }
George Mount3636d0a2011-11-21 09:08:21 -0800687 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700688 }
689
John Reck9c35b9c2012-05-30 10:08:50 -0700690 @Override
691 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700692 if (!mActivityPaused) {
693 Log.e(LOGTAG, "BrowserActivity is already resumed.");
694 return;
695 }
George Mount3636d0a2011-11-21 09:08:21 -0800696 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800698 Tab current = mTabControl.getCurrentTab();
699 if (current != null) {
700 current.resume();
701 resumeWebViewTimers(current);
702 }
John Reckf57c0292011-07-21 18:15:39 -0700703 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200704
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 mUi.onResume();
706 mNetworkHandler.onResume();
707 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100708 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700709 if (mVoiceResult != null) {
710 mUi.onVoiceResult(mVoiceResult);
711 mVoiceResult = null;
712 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700713 }
714
John Reckf57c0292011-07-21 18:15:39 -0700715 private void releaseWakeLock() {
716 if (mWakeLock != null && mWakeLock.isHeld()) {
717 mHandler.removeMessages(RELEASE_WAKELOCK);
718 mWakeLock.release();
719 }
720 }
721
Michael Kolb70976932010-11-30 11:34:01 -0800722 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800723 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800724 * @param tab guaranteed non-null
725 */
726 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700727 boolean inLoad = tab.inPageLoad();
728 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
729 CookieSyncManager.getInstance().startSync();
730 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100731 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700732 }
733 }
734
Michael Kolb70976932010-11-30 11:34:01 -0800735 /**
736 * Pause all WebView timers using the WebView of the given tab
737 * @param tab
738 * @return true if the timers are paused or tab is null
739 */
740 private boolean pauseWebViewTimers(Tab tab) {
741 if (tab == null) {
742 return true;
743 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700744 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100745 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700746 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700747 }
Michael Kolb70976932010-11-30 11:34:01 -0800748 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700749 }
750
John Reck9c35b9c2012-05-30 10:08:50 -0700751 @Override
752 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800753 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700754 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
755 mUploadHandler = null;
756 }
757 if (mTabControl == null) return;
758 mUi.onDestroy();
759 // Remove the current tab and sub window
760 Tab t = mTabControl.getCurrentTab();
761 if (t != null) {
762 dismissSubWindow(t);
763 removeTab(t);
764 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500765 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700766 // Destroy all the tabs
767 mTabControl.destroy();
768 WebIconDatabase.getInstance().close();
769 // Stop watching the default geolocation permissions
770 mSystemAllowGeolocationOrigins.stop();
771 mSystemAllowGeolocationOrigins = null;
772 }
773
774 protected boolean isActivityPaused() {
775 return mActivityPaused;
776 }
777
John Reck9c35b9c2012-05-30 10:08:50 -0700778 @Override
779 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700780 mTabControl.freeMemory();
781 }
782
783 @Override
784 public boolean shouldShowErrorConsole() {
785 return mShouldShowErrorConsole;
786 }
787
788 protected void setShouldShowErrorConsole(boolean show) {
789 if (show == mShouldShowErrorConsole) {
790 // Nothing to do.
791 return;
792 }
793 mShouldShowErrorConsole = show;
794 Tab t = mTabControl.getCurrentTab();
795 if (t == null) {
796 // There is no current tab so we cannot toggle the error console
797 return;
798 }
799 mUi.setShouldShowErrorConsole(t, show);
800 }
801
802 @Override
803 public void stopLoading() {
804 mLoadStopped = true;
805 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700807 if (w != null) {
808 w.stopLoading();
809 mUi.onPageStopped(tab);
810 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700811 }
812
813 boolean didUserStopLoading() {
814 return mLoadStopped;
815 }
816
817 // WebViewController
818
819 @Override
John Reck324d4402011-01-11 16:56:42 -0800820 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700821
822 // We've started to load a new page. If there was a pending message
823 // to save a screenshot then we will now take the new page and save
824 // an incorrect screenshot. Therefore, remove any pending thumbnail
825 // messages from the queue.
826 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800827 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700828
829 // reset sync timer to avoid sync starts during loading a page
830 CookieSyncManager.getInstance().resetSync();
831
832 if (!mNetworkHandler.isNetworkUp()) {
833 view.setNetworkAvailable(false);
834 }
835
836 // when BrowserActivity just starts, onPageStarted may be called before
837 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
838 // to start the timer. As we won't switch tabs while an activity is in
839 // pause state, we can ensure calling resume and pause in pair.
840 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800841 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 }
843 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700844 endActionMode();
845
John Reck30c714c2010-12-16 17:30:34 -0800846 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700847
John Reck324d4402011-01-11 16:56:42 -0800848 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700849 // update the bookmark database for favicon
850 maybeUpdateFavicon(tab, null, url, favicon);
851
852 Performance.tracePageStart(url);
853
854 // Performance probe
855 if (false) {
856 Performance.onPageStarted();
857 }
858
859 }
860
861 @Override
John Reck324d4402011-01-11 16:56:42 -0800862 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700863 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800864 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200865
Michael Kolb8233fac2010-10-26 16:08:53 -0700866 // Performance probe
867 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800868 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700869 }
870
871 Performance.tracePageFinished();
872 }
873
874 @Override
John Reck30c714c2010-12-16 17:30:34 -0800875 public void onProgressChanged(Tab tab) {
876 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700877
878 if (newProgress == 100) {
879 CookieSyncManager.getInstance().sync();
880 // onProgressChanged() may continue to be called after the main
881 // frame has finished loading, as any remaining sub frames continue
882 // to load. We'll only get called once though with newProgress as
883 // 100 when everything is loaded. (onPageFinished is called once
884 // when the main frame completes loading regardless of the state of
885 // any sub frames so calls to onProgressChanges may continue after
886 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800887 if (tab.inPageLoad()) {
888 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200889 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
890 // pause the WebView timer and release the wake lock if it is
891 // finished while BrowserActivity is in pause state.
892 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700893 }
John Reckd9862372012-02-21 15:04:50 -0800894 if (!tab.isPrivateBrowsingEnabled()
895 && !TextUtils.isEmpty(tab.getUrl())
896 && !tab.isSnapshot()) {
897 // Only update the bookmark screenshot if the user did not
898 // cancel the load early and there is not already
899 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700900 if (tab.shouldUpdateThumbnail() &&
901 (tab.inForeground() && !didUserStopLoading()
902 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800903 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
904 mHandler.sendMessageDelayed(mHandler.obtainMessage(
905 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
906 500);
907 }
908 }
909 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700910 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800911 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700912 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800913 // still loading
914 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -0700915 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800916 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700917 }
918 }
John Reck30c714c2010-12-16 17:30:34 -0800919 mUi.onProgressChanged(tab);
920 }
921
922 @Override
Steve Block2466eff2011-10-03 15:33:09 +0100923 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800924 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700925 }
926
927 @Override
928 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800929 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800930 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800931 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700932 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
933 return;
934 }
935 // Update the title in the history database if not in private browsing mode
936 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -0700937 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 }
939 }
940
941 @Override
942 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800943 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700944 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
945 }
946
947 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800948 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
949 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700950 }
951
952 @Override
953 public boolean shouldOverrideKeyEvent(KeyEvent event) {
954 if (mMenuIsDown) {
955 // only check shortcut key when MENU is held
956 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
957 event);
958 } else {
959 return false;
960 }
961 }
962
963 @Override
John Reck997b1b72012-04-19 18:08:25 -0700964 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700965 if (!isActivityPaused()) {
966 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -0700967 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700968 } else {
John Reck997b1b72012-04-19 18:08:25 -0700969 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700970 }
971 }
John Reck997b1b72012-04-19 18:08:25 -0700972 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700973 }
974
975 @Override
John Reck324d4402011-01-11 16:56:42 -0800976 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 // Don't save anything in private browsing mode
978 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800979 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700980
John Reck324d4402011-01-11 16:56:42 -0800981 if (TextUtils.isEmpty(url)
982 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700983 return;
984 }
John Reckf57c0292011-07-21 18:15:39 -0700985 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -0700986 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -0700987 }
988
989 @Override
990 public void getVisitedHistory(final ValueCallback<String[]> callback) {
991 AsyncTask<Void, Void, String[]> task =
992 new AsyncTask<Void, Void, String[]>() {
993 @Override
994 public String[] doInBackground(Void... unused) {
995 return Browser.getVisitedHistory(mActivity.getContentResolver());
996 }
997 @Override
998 public void onPostExecute(String[] result) {
999 callback.onReceiveValue(result);
1000 }
1001 };
1002 task.execute();
1003 }
1004
1005 @Override
1006 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1007 final HttpAuthHandler handler, final String host,
1008 final String realm) {
1009 String username = null;
1010 String password = null;
1011
1012 boolean reuseHttpAuthUsernamePassword
1013 = handler.useHttpAuthUsernamePassword();
1014
1015 if (reuseHttpAuthUsernamePassword && view != null) {
1016 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1017 if (credentials != null && credentials.length == 2) {
1018 username = credentials[0];
1019 password = credentials[1];
1020 }
1021 }
1022
1023 if (username != null && password != null) {
1024 handler.proceed(username, password);
1025 } else {
Ben Murdochfdd37112011-08-05 15:48:14 +01001026 if (tab.inForeground() && !handler.suppressDialog()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001027 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1028 } else {
1029 handler.cancel();
1030 }
1031 }
1032 }
1033
1034 @Override
1035 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001036 String contentDisposition, String mimetype, String referer,
1037 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001038 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -05001039 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001040 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled());
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001041 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001042 // This Tab was opened for the sole purpose of downloading a
1043 // file. Remove it.
1044 if (tab == mTabControl.getCurrentTab()) {
1045 // In this case, the Tab is still on top.
1046 goBackOnePageOrQuit();
1047 } else {
1048 // In this case, it is not.
1049 closeTab(tab);
1050 }
1051 }
1052 }
1053
1054 @Override
1055 public Bitmap getDefaultVideoPoster() {
1056 return mUi.getDefaultVideoPoster();
1057 }
1058
1059 @Override
1060 public View getVideoLoadingProgressView() {
1061 return mUi.getVideoLoadingProgressView();
1062 }
1063
1064 @Override
1065 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1066 SslError error) {
1067 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1068 }
1069
Patrick Scott92066772011-03-10 08:46:27 -05001070 @Override
1071 public void showAutoLogin(Tab tab) {
1072 assert tab.inForeground();
1073 // Update the title bar to show the auto-login request.
1074 mUi.showAutoLogin(tab);
1075 }
1076
1077 @Override
1078 public void hideAutoLogin(Tab tab) {
1079 assert tab.inForeground();
1080 mUi.hideAutoLogin(tab);
1081 }
1082
Michael Kolb8233fac2010-10-26 16:08:53 -07001083 // helper method
1084
1085 /*
1086 * Update the favorites icon if the private browsing isn't enabled and the
1087 * icon is valid.
1088 */
1089 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1090 final String url, Bitmap favicon) {
1091 if (favicon == null) {
1092 return;
1093 }
1094 if (!tab.isPrivateBrowsingEnabled()) {
1095 Bookmarks.updateFavicon(mActivity
1096 .getContentResolver(), originalUrl, url, favicon);
1097 }
1098 }
1099
Leon Scroggins4cd97792010-12-03 15:31:56 -05001100 @Override
1101 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001102 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001103 mUi.bookmarkedStatusHasChanged(tab);
1104 }
1105
Michael Kolb8233fac2010-10-26 16:08:53 -07001106 // end WebViewController
1107
1108 protected void pageUp() {
1109 getCurrentTopWebView().pageUp(false);
1110 }
1111
1112 protected void pageDown() {
1113 getCurrentTopWebView().pageDown(false);
1114 }
1115
1116 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001117 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001118 public void editUrl() {
1119 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001120 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001121 }
1122
John Reck9c35b9c2012-05-30 10:08:50 -07001123 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001124 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001125 WebChromeClient.CustomViewCallback callback) {
1126 if (tab.inForeground()) {
1127 if (mUi.isCustomViewShowing()) {
1128 callback.onCustomViewHidden();
1129 return;
1130 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001131 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001132 // Save the menu state and set it to empty while the custom
1133 // view is showing.
1134 mOldMenuState = mMenuState;
1135 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001136 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001137 }
1138 }
1139
1140 @Override
1141 public void hideCustomView() {
1142 if (mUi.isCustomViewShowing()) {
1143 mUi.onHideCustomView();
1144 // Reset the old menu state.
1145 mMenuState = mOldMenuState;
1146 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001147 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001148 }
1149 }
1150
John Reck9c35b9c2012-05-30 10:08:50 -07001151 @Override
1152 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001153 Intent intent) {
1154 if (getCurrentTopWebView() == null) return;
1155 switch (requestCode) {
1156 case PREFERENCES_PAGE:
1157 if (resultCode == Activity.RESULT_OK && intent != null) {
1158 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001159 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001160 mTabControl.removeParentChildRelationShips();
1161 }
1162 }
1163 break;
1164 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001165 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001166 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001167 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001168 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001169 case AUTOFILL_SETUP:
1170 // Determine whether a profile was actually set up or not
1171 // and if so, send the message back to the WebTextView to
1172 // fill the form with the new profile.
1173 if (getSettings().getAutoFillProfile() != null) {
1174 mAutoFillSetupMessage.sendToTarget();
1175 mAutoFillSetupMessage = null;
1176 }
1177 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001178 case COMBO_VIEW:
1179 if (intent == null || resultCode != Activity.RESULT_OK) {
1180 break;
1181 }
John Reck3ba45532011-08-11 16:26:53 -07001182 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001183 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1184 Tab t = getCurrentTab();
1185 Uri uri = intent.getData();
1186 loadUrl(t, uri.toString());
1187 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1188 String[] urls = intent.getStringArrayExtra(
1189 ComboViewActivity.EXTRA_OPEN_ALL);
1190 Tab parent = getCurrentTab();
1191 for (String url : urls) {
1192 parent = openTab(url, parent,
1193 !mSettings.openInBackground(), true);
1194 }
1195 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1196 long id = intent.getLongExtra(
1197 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1198 if (id >= 0) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08001199 if (BrowserWebView.isClassic()) {
1200 createNewSnapshotTab(id, true);
1201 } else {
1202 Toast.makeText(mActivity, "Snapshot Tab requires WebViewClassic",
1203 Toast.LENGTH_LONG).show();
1204 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001205 }
1206 }
1207 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001208 case VOICE_RESULT:
1209 if (resultCode == Activity.RESULT_OK && intent != null) {
1210 ArrayList<String> results = intent.getStringArrayListExtra(
1211 RecognizerIntent.EXTRA_RESULTS);
1212 if (results.size() >= 1) {
1213 mVoiceResult = results.get(0);
1214 }
1215 }
1216 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001217 default:
1218 break;
1219 }
1220 getCurrentTopWebView().requestFocus();
1221 }
1222
1223 /**
1224 * Open the Go page.
1225 * @param startWithHistory If true, open starting on the history tab.
1226 * Otherwise, start with the bookmarks tab.
1227 */
1228 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001229 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001230 if (mTabControl.getCurrentWebView() == null) {
1231 return;
1232 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001233 // clear action mode
1234 if (isInCustomActionMode()) {
1235 endActionMode();
1236 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001237 Bundle extras = new Bundle();
1238 // Disable opening in a new window if we have maxed out the windows
1239 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1240 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001241 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001242 }
1243
1244 // combo view callbacks
1245
Michael Kolb8233fac2010-10-26 16:08:53 -07001246 // key handling
1247 protected void onBackKey() {
1248 if (!mUi.onBackKey()) {
1249 WebView subwindow = mTabControl.getCurrentSubWindow();
1250 if (subwindow != null) {
1251 if (subwindow.canGoBack()) {
1252 subwindow.goBack();
1253 } else {
1254 dismissSubWindow(mTabControl.getCurrentTab());
1255 }
1256 } else {
1257 goBackOnePageOrQuit();
1258 }
1259 }
1260 }
1261
Michael Kolb4bd767d2011-05-27 11:33:55 -07001262 protected boolean onMenuKey() {
1263 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001264 }
1265
Michael Kolb8233fac2010-10-26 16:08:53 -07001266 // menu handling and state
1267 // TODO: maybe put into separate handler
1268
John Reck9c35b9c2012-05-30 10:08:50 -07001269 @Override
1270 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001271 if (mMenuState == EMPTY_MENU) {
1272 return false;
1273 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001274 MenuInflater inflater = mActivity.getMenuInflater();
1275 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001276 return true;
1277 }
1278
John Reck9c35b9c2012-05-30 10:08:50 -07001279 @Override
1280 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001281 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001282 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001283 return;
1284 }
1285 if (!(v instanceof WebView)) {
1286 return;
1287 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001288 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001289 WebView.HitTestResult result = webview.getHitTestResult();
1290 if (result == null) {
1291 return;
1292 }
1293
1294 int type = result.getType();
1295 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1296 Log.w(LOGTAG,
1297 "We should not show context menu when nothing is touched");
1298 return;
1299 }
1300 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1301 // let TextView handles context menu
1302 return;
1303 }
1304
1305 // Note, http://b/issue?id=1106666 is requesting that
1306 // an inflated menu can be used again. This is not available
1307 // yet, so inflate each time (yuk!)
1308 MenuInflater inflater = mActivity.getMenuInflater();
1309 inflater.inflate(R.menu.browsercontext, menu);
1310
1311 // Show the correct menu group
1312 final String extra = result.getExtra();
Michael Kolbc159c1a2011-12-15 16:06:38 -08001313 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001314 menu.setGroupVisible(R.id.PHONE_MENU,
1315 type == WebView.HitTestResult.PHONE_TYPE);
1316 menu.setGroupVisible(R.id.EMAIL_MENU,
1317 type == WebView.HitTestResult.EMAIL_TYPE);
1318 menu.setGroupVisible(R.id.GEO_MENU,
1319 type == WebView.HitTestResult.GEO_TYPE);
1320 menu.setGroupVisible(R.id.IMAGE_MENU,
1321 type == WebView.HitTestResult.IMAGE_TYPE
1322 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1323 menu.setGroupVisible(R.id.ANCHOR_MENU,
1324 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1325 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001326 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1327 || type == WebView.HitTestResult.PHONE_TYPE
1328 || type == WebView.HitTestResult.EMAIL_TYPE
1329 || type == WebView.HitTestResult.GEO_TYPE;
1330 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1331 if (hitText) {
1332 menu.findItem(R.id.select_text_menu_id)
1333 .setOnMenuItemClickListener(new SelectText(webview));
1334 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001335 // Setup custom handling depending on the type
1336 switch (type) {
1337 case WebView.HitTestResult.PHONE_TYPE:
1338 menu.setHeaderTitle(Uri.decode(extra));
1339 menu.findItem(R.id.dial_context_menu_id).setIntent(
1340 new Intent(Intent.ACTION_VIEW, Uri
1341 .parse(WebView.SCHEME_TEL + extra)));
1342 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1343 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1344 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1345 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1346 addIntent);
1347 menu.findItem(R.id.copy_phone_context_menu_id)
1348 .setOnMenuItemClickListener(
1349 new Copy(extra));
1350 break;
1351
1352 case WebView.HitTestResult.EMAIL_TYPE:
1353 menu.setHeaderTitle(extra);
1354 menu.findItem(R.id.email_context_menu_id).setIntent(
1355 new Intent(Intent.ACTION_VIEW, Uri
1356 .parse(WebView.SCHEME_MAILTO + extra)));
1357 menu.findItem(R.id.copy_mail_context_menu_id)
1358 .setOnMenuItemClickListener(
1359 new Copy(extra));
1360 break;
1361
1362 case WebView.HitTestResult.GEO_TYPE:
1363 menu.setHeaderTitle(extra);
1364 menu.findItem(R.id.map_context_menu_id).setIntent(
1365 new Intent(Intent.ACTION_VIEW, Uri
1366 .parse(WebView.SCHEME_GEO
1367 + URLEncoder.encode(extra))));
1368 menu.findItem(R.id.copy_geo_context_menu_id)
1369 .setOnMenuItemClickListener(
1370 new Copy(extra));
1371 break;
1372
1373 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1374 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001375 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001376 // decide whether to show the open link in new tab option
1377 boolean showNewTab = mTabControl.canCreateNewTab();
1378 MenuItem newTabItem
1379 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001380 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001381 ? R.string.contextmenu_openlink_newwindow_background
1382 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001383 newTabItem.setVisible(showNewTab);
1384 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001385 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1386 newTabItem.setOnMenuItemClickListener(
1387 new MenuItem.OnMenuItemClickListener() {
1388 @Override
1389 public boolean onMenuItemClick(MenuItem item) {
1390 final HashMap<String, WebView> hrefMap =
1391 new HashMap<String, WebView>();
1392 hrefMap.put("webview", webview);
1393 final Message msg = mHandler.obtainMessage(
1394 FOCUS_NODE_HREF,
1395 R.id.open_newtab_context_menu_id,
1396 0, hrefMap);
1397 webview.requestFocusNodeHref(msg);
1398 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001399 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001400 });
1401 } else {
1402 newTabItem.setOnMenuItemClickListener(
1403 new MenuItem.OnMenuItemClickListener() {
1404 @Override
1405 public boolean onMenuItemClick(MenuItem item) {
1406 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001407 openTab(extra, parent,
1408 !mSettings.openInBackground(),
1409 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001410 return true;
1411 }
1412 });
1413 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001414 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001415 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1416 break;
1417 }
1418 // otherwise fall through to handle image part
1419 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001420 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1421 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001422 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1423 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001424 shareItem.setOnMenuItemClickListener(
1425 new MenuItem.OnMenuItemClickListener() {
1426 @Override
1427 public boolean onMenuItemClick(MenuItem item) {
1428 sharePage(mActivity, null, extra, null,
1429 null);
1430 return true;
1431 }
1432 }
1433 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001434 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001435 menu.findItem(R.id.view_image_context_menu_id)
1436 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1437 @Override
1438 public boolean onMenuItemClick(MenuItem item) {
1439 openTab(extra, mTabControl.getCurrentTab(), true, true);
1440 return false;
1441 }
1442 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001443 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1444 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1445 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001446 menu.findItem(R.id.set_wallpaper_context_menu_id).
1447 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1448 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001449 break;
1450
1451 default:
1452 Log.w(LOGTAG, "We should not get here.");
1453 break;
1454 }
1455 //update the ui
1456 mUi.onContextMenuCreated(menu);
1457 }
1458
1459 /**
1460 * As the menu can be open when loading state changes
1461 * we must manually update the state of the stop/reload menu
1462 * item
1463 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001464 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001465 if (menu == null) {
1466 return;
1467 }
1468 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001469 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001470 menu.findItem(R.id.stop_menu_id):
1471 menu.findItem(R.id.reload_menu_id);
1472 if (src != null) {
1473 dest.setIcon(src.getIcon());
1474 dest.setTitle(src.getTitle());
1475 }
1476 }
1477
John Reck9c35b9c2012-05-30 10:08:50 -07001478 @Override
1479 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001480 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001481 // hold on to the menu reference here; it is used by the page callbacks
1482 // to update the menu based on loading state
1483 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001484 // Note: setVisible will decide whether an item is visible; while
1485 // setEnabled() will decide whether an item is enabled, which also means
1486 // whether the matching shortcut key will function.
1487 switch (mMenuState) {
1488 case EMPTY_MENU:
1489 if (mCurrentMenuState != mMenuState) {
1490 menu.setGroupVisible(R.id.MAIN_MENU, false);
1491 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1492 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1493 }
1494 break;
1495 default:
1496 if (mCurrentMenuState != mMenuState) {
1497 menu.setGroupVisible(R.id.MAIN_MENU, true);
1498 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1499 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1500 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001501 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001502 break;
1503 }
1504 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001505 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001506 }
1507
Michael Kolb4bf79712011-07-14 14:18:12 -07001508 @Override
1509 public void updateMenuState(Tab tab, Menu menu) {
1510 boolean canGoBack = false;
1511 boolean canGoForward = false;
1512 boolean isHome = false;
John Reck42229bc2011-08-19 13:26:43 -07001513 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001514 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001515 if (tab != null) {
1516 canGoBack = tab.canGoBack();
1517 canGoForward = tab.canGoForward();
1518 isHome = mSettings.getHomePage().equals(tab.getUrl());
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);
1526 home.setEnabled(!isHome);
1527
1528 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1529 forward.setEnabled(canGoForward);
1530
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001531 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1532 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001533 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001534 if (source != null && dest != null) {
1535 dest.setTitle(source.getTitle());
1536 dest.setIcon(source.getIcon());
1537 }
John Recke1a03a32011-09-14 17:04:16 -07001538 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001539
1540 // decide whether to show the share link option
1541 PackageManager pm = mActivity.getPackageManager();
1542 Intent send = new Intent(Intent.ACTION_SEND);
1543 send.setType("text/plain");
1544 ResolveInfo ri = pm.resolveActivity(send,
1545 PackageManager.MATCH_DEFAULT_ONLY);
1546 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1547
1548 boolean isNavDump = mSettings.enableNavDump();
1549 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1550 nav.setVisible(isNavDump);
1551 nav.setEnabled(isNavDump);
1552
1553 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001554 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1555 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001556 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1557 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Michael Kolb315d5022011-10-13 12:47:11 -07001558 menu.setGroupVisible(R.id.COMBO_MENU, false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001559
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001560 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001561 }
1562
John Reck9c35b9c2012-05-30 10:08:50 -07001563 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001564 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001565 if (null == getCurrentTopWebView()) {
1566 return false;
1567 }
1568 if (mMenuIsDown) {
1569 // The shortcut action consumes the MENU. Even if it is still down,
1570 // it won't trigger the next shortcut action. In the case of the
1571 // shortcut action triggering a new activity, like Bookmarks, we
1572 // won't get onKeyUp for MENU. So it is important to reset it here.
1573 mMenuIsDown = false;
1574 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001575 if (mUi.onOptionsItemSelected(item)) {
1576 // ui callback handled it
1577 return true;
1578 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001579 switch (item.getItemId()) {
1580 // -- Main menu
1581 case R.id.new_tab_menu_id:
1582 openTabToHomePage();
1583 break;
1584
1585 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001586 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001587 break;
1588
Afzal Najamd4e33312012-04-26 01:54:01 -04001589 case R.id.close_other_tabs_id:
1590 closeOtherTabs();
1591 break;
1592
Michael Kolb8233fac2010-10-26 16:08:53 -07001593 case R.id.goto_menu_id:
1594 editUrl();
1595 break;
1596
1597 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001598 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1599 break;
1600
1601 case R.id.history_menu_id:
1602 bookmarksOrHistoryPicker(ComboViews.History);
1603 break;
1604
1605 case R.id.snapshots_menu_id:
1606 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001607 break;
1608
Michael Kolb8233fac2010-10-26 16:08:53 -07001609 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001610 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001611 break;
1612
1613 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001614 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001615 stopLoading();
1616 } else {
1617 getCurrentTopWebView().reload();
1618 }
1619 break;
1620
1621 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001622 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001623 break;
1624
1625 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001626 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001627 break;
1628
1629 case R.id.close_menu_id:
1630 // Close the subwindow if it exists.
1631 if (mTabControl.getCurrentSubWindow() != null) {
1632 dismissSubWindow(mTabControl.getCurrentTab());
1633 break;
1634 }
1635 closeCurrentTab();
1636 break;
1637
1638 case R.id.homepage_menu_id:
1639 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001640 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001641 break;
1642
1643 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001644 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001645 break;
1646
1647 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001648 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001649 break;
1650
John Reck2bc80422011-06-30 15:11:49 -07001651 case R.id.save_snapshot_menu_id:
1652 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001653 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001654 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001655 break;
1656
Michael Kolb8233fac2010-10-26 16:08:53 -07001657 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001658 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001659 break;
1660
John Recke1a03a32011-09-14 17:04:16 -07001661 case R.id.snapshot_go_live:
1662 goLive();
1663 return true;
1664
Michael Kolb8233fac2010-10-26 16:08:53 -07001665 case R.id.share_page_menu_id:
1666 Tab currentTab = mTabControl.getCurrentTab();
1667 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001668 return false;
1669 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001670 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001671 break;
1672
1673 case R.id.dump_nav_menu_id:
1674 getCurrentTopWebView().debugDump();
1675 break;
1676
Michael Kolb8233fac2010-10-26 16:08:53 -07001677 case R.id.zoom_in_menu_id:
1678 getCurrentTopWebView().zoomIn();
1679 break;
1680
1681 case R.id.zoom_out_menu_id:
1682 getCurrentTopWebView().zoomOut();
1683 break;
1684
1685 case R.id.view_downloads_menu_id:
1686 viewDownloads();
1687 break;
1688
John Reck42229bc2011-08-19 13:26:43 -07001689 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001690 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07001691 break;
1692
Michael Kolb8233fac2010-10-26 16:08:53 -07001693 case R.id.window_one_menu_id:
1694 case R.id.window_two_menu_id:
1695 case R.id.window_three_menu_id:
1696 case R.id.window_four_menu_id:
1697 case R.id.window_five_menu_id:
1698 case R.id.window_six_menu_id:
1699 case R.id.window_seven_menu_id:
1700 case R.id.window_eight_menu_id:
1701 {
1702 int menuid = item.getItemId();
1703 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1704 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1705 Tab desiredTab = mTabControl.getTab(id);
1706 if (desiredTab != null &&
1707 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001708 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001709 }
1710 break;
1711 }
1712 }
1713 }
1714 break;
1715
1716 default:
1717 return false;
1718 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001719 return true;
1720 }
1721
John Reck68234a92012-04-19 15:27:12 -07001722 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
1723 implements OnCancelListener {
1724
1725 private Tab mTab;
1726 private Dialog mProgressDialog;
1727 private ContentValues mValues;
1728
1729 private SaveSnapshotTask(Tab tab) {
1730 mTab = tab;
1731 }
1732
1733 @Override
1734 protected void onPreExecute() {
1735 CharSequence message = mActivity.getText(R.string.saving_snapshot);
1736 mProgressDialog = ProgressDialog.show(mActivity, null, message,
1737 true, true, this);
1738 mValues = mTab.createSnapshotValues();
1739 }
1740
1741 @Override
1742 protected Long doInBackground(Void... params) {
1743 if (!mTab.saveViewState(mValues)) {
1744 return null;
1745 }
1746 if (isCancelled()) {
1747 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
1748 File file = mActivity.getFileStreamPath(path);
1749 if (!file.delete()) {
1750 file.deleteOnExit();
1751 }
1752 return null;
1753 }
1754 final ContentResolver cr = mActivity.getContentResolver();
1755 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
1756 if (result == null) {
1757 return null;
1758 }
1759 long id = ContentUris.parseId(result);
1760 return id;
1761 }
1762
1763 @Override
1764 protected void onPostExecute(Long id) {
1765 if (isCancelled()) {
1766 return;
1767 }
1768 mProgressDialog.dismiss();
1769 if (id == null) {
1770 Toast.makeText(mActivity, R.string.snapshot_failed,
1771 Toast.LENGTH_SHORT).show();
1772 return;
1773 }
1774 Bundle b = new Bundle();
1775 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1776 mUi.showComboView(ComboViews.Snapshots, b);
1777 }
1778
1779 @Override
1780 public void onCancel(DialogInterface dialog) {
1781 cancel(true);
1782 }
1783 }
1784
Michael Kolb80f75082012-04-10 10:50:06 -07001785 @Override
1786 public void toggleUserAgent() {
1787 WebView web = getCurrentWebView();
1788 mSettings.toggleDesktopUseragent(web);
1789 web.loadUrl(web.getOriginalUrl());
1790 }
1791
1792 @Override
1793 public void findOnPage() {
1794 getCurrentTopWebView().showFindDialog(null, true);
1795 }
1796
1797 @Override
1798 public void openPreferences() {
1799 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1800 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1801 getCurrentTopWebView().getUrl());
1802 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1803 }
1804
1805 @Override
1806 public void bookmarkCurrentPage() {
1807 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
1808 if (bookmarkIntent != null) {
1809 mActivity.startActivity(bookmarkIntent);
1810 }
1811 }
1812
John Recke1a03a32011-09-14 17:04:16 -07001813 private void goLive() {
1814 Tab t = getCurrentTab();
1815 t.loadUrl(t.getUrl(), null);
1816 }
1817
Michael Kolb315d5022011-10-13 12:47:11 -07001818 @Override
1819 public void showPageInfo() {
1820 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
1821 }
1822
John Reck9c35b9c2012-05-30 10:08:50 -07001823 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001824 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001825 // Let the History and Bookmark fragments handle menus they created.
1826 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1827 return false;
1828 }
1829
Michael Kolb8233fac2010-10-26 16:08:53 -07001830 int id = item.getItemId();
1831 boolean result = true;
1832 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001833 // -- Browser context menu
1834 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001835 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001836 case R.id.copy_link_context_menu_id:
1837 final WebView webView = getCurrentTopWebView();
1838 if (null == webView) {
1839 result = false;
1840 break;
1841 }
1842 final HashMap<String, WebView> hrefMap =
1843 new HashMap<String, WebView>();
1844 hrefMap.put("webview", webView);
1845 final Message msg = mHandler.obtainMessage(
1846 FOCUS_NODE_HREF, id, 0, hrefMap);
1847 webView.requestFocusNodeHref(msg);
1848 break;
1849
1850 default:
1851 // For other context menus
1852 result = onOptionsItemSelected(item);
1853 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001854 return result;
1855 }
1856
1857 /**
1858 * support programmatically opening the context menu
1859 */
1860 public void openContextMenu(View view) {
1861 mActivity.openContextMenu(view);
1862 }
1863
1864 /**
1865 * programmatically open the options menu
1866 */
1867 public void openOptionsMenu() {
1868 mActivity.openOptionsMenu();
1869 }
1870
John Reck9c35b9c2012-05-30 10:08:50 -07001871 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001872 public boolean onMenuOpened(int featureId, Menu menu) {
1873 if (mOptionsMenuOpen) {
1874 if (mConfigChanged) {
1875 // We do not need to make any changes to the state of the
1876 // title bar, since the only thing that happened was a
1877 // change in orientation
1878 mConfigChanged = false;
1879 } else {
1880 if (!mExtendedMenuOpen) {
1881 mExtendedMenuOpen = true;
1882 mUi.onExtendedMenuOpened();
1883 } else {
1884 // Switching the menu back to icon view, so show the
1885 // title bar once again.
1886 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001887 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001888 }
1889 }
1890 } else {
1891 // The options menu is closed, so open it, and show the title
1892 mOptionsMenuOpen = true;
1893 mConfigChanged = false;
1894 mExtendedMenuOpen = false;
1895 mUi.onOptionsMenuOpened();
1896 }
1897 return true;
1898 }
1899
John Reck9c35b9c2012-05-30 10:08:50 -07001900 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001901 public void onOptionsMenuClosed(Menu menu) {
1902 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001903 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001904 }
1905
John Reck9c35b9c2012-05-30 10:08:50 -07001906 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001907 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001908 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001909 }
1910
1911 // Helper method for getting the top window.
1912 @Override
1913 public WebView getCurrentTopWebView() {
1914 return mTabControl.getCurrentTopWebView();
1915 }
1916
1917 @Override
1918 public WebView getCurrentWebView() {
1919 return mTabControl.getCurrentWebView();
1920 }
1921
1922 /*
1923 * This method is called as a result of the user selecting the options
1924 * menu to see the download window. It shows the download window on top of
1925 * the current window.
1926 */
1927 void viewDownloads() {
1928 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1929 mActivity.startActivity(intent);
1930 }
1931
John Reck30b065e2011-07-19 10:58:05 -07001932 int getActionModeHeight() {
1933 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1934 new int[] { android.R.attr.actionBarSize });
1935 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1936 actionBarSizeTypedArray.recycle();
1937 return size;
1938 }
1939
Michael Kolb8233fac2010-10-26 16:08:53 -07001940 // action mode
1941
John Reck9c35b9c2012-05-30 10:08:50 -07001942 @Override
1943 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001944 mUi.onActionModeStarted(mode);
1945 mActionMode = mode;
1946 }
1947
1948 /*
1949 * True if a custom ActionMode (i.e. find or select) is in use.
1950 */
1951 @Override
1952 public boolean isInCustomActionMode() {
1953 return mActionMode != null;
1954 }
1955
1956 /*
1957 * End the current ActionMode.
1958 */
1959 @Override
1960 public void endActionMode() {
1961 if (mActionMode != null) {
1962 mActionMode.finish();
1963 }
1964 }
1965
1966 /*
1967 * Called by find and select when they are finished. Replace title bars
1968 * as necessary.
1969 */
John Reck9c35b9c2012-05-30 10:08:50 -07001970 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001971 public void onActionModeFinished(ActionMode mode) {
1972 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001973 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001974 mActionMode = null;
1975 }
1976
1977 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08001978 final Tab tab = getCurrentTab();
1979 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07001980 }
1981
1982 // bookmark handling
1983
1984 /**
1985 * add the current page as a bookmark to the given folder id
1986 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07001987 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001988 * bookmarked, and if it is, edit that bookmark. If false, and
1989 * the site is already bookmarked, do not attempt to edit the
1990 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001991 */
1992 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07001993 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07001994 WebView w = getCurrentTopWebView();
1995 if (w == null) {
1996 return null;
1997 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001998 Intent i = new Intent(mActivity,
1999 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002000 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2001 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2002 String touchIconUrl = w.getTouchIconUrl();
2003 if (touchIconUrl != null) {
2004 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2005 WebSettings settings = w.getSettings();
2006 if (settings != null) {
2007 i.putExtra(AddBookmarkPage.USER_AGENT,
2008 settings.getUserAgentString());
2009 }
2010 }
2011 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2012 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2013 getDesiredThumbnailHeight(mActivity)));
2014 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002015 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002016 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2017 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002018 // Put the dialog at the upper right of the screen, covering the
2019 // star on the title bar.
2020 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002021 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002022 }
2023
2024 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002025 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002026 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002027 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002028 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002029 }
2030
2031 // thumbnails
2032
2033 /**
2034 * Return the desired width for thumbnail screenshots, which are stored in
2035 * the database, and used on the bookmarks screen.
2036 * @param context Context for finding out the density of the screen.
2037 * @return desired width for thumbnail screenshot.
2038 */
2039 static int getDesiredThumbnailWidth(Context context) {
2040 return context.getResources().getDimensionPixelOffset(
2041 R.dimen.bookmarkThumbnailWidth);
2042 }
2043
2044 /**
2045 * Return the desired height for thumbnail screenshots, which are stored in
2046 * the database, and used on the bookmarks screen.
2047 * @param context Context for finding out the density of the screen.
2048 * @return desired height for thumbnail screenshot.
2049 */
2050 static int getDesiredThumbnailHeight(Context context) {
2051 return context.getResources().getDimensionPixelOffset(
2052 R.dimen.bookmarkThumbnailHeight);
2053 }
2054
John Reck8cc92352011-07-06 17:41:52 -07002055 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002056 if (view == null || view.getContentHeight() == 0
2057 || view.getContentWidth() == 0) {
2058 return null;
2059 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002060 // We render to a bitmap 2x the desired size so that we can then
2061 // re-scale it with filtering since canvas.scale doesn't filter
2062 // This helps reduce aliasing at the cost of being slightly blurry
2063 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002064 int scaledWidth = width * filter_scale;
2065 int scaledHeight = height * filter_scale;
2066 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2067 || sThumbnailBitmap.getHeight() != scaledHeight) {
2068 if (sThumbnailBitmap != null) {
2069 sThumbnailBitmap.recycle();
2070 sThumbnailBitmap = null;
2071 }
2072 sThumbnailBitmap =
2073 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002074 }
John Reckd7dd9b22011-08-30 09:18:29 -07002075 Canvas canvas = new Canvas(sThumbnailBitmap);
2076 int contentWidth = view.getContentWidth();
2077 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2078 if (view instanceof BrowserWebView) {
2079 int dy = -((BrowserWebView)view).getTitleHeight();
2080 canvas.translate(0, dy * overviewScale);
2081 }
2082
2083 canvas.scale(overviewScale, overviewScale);
2084
2085 if (view instanceof BrowserWebView) {
2086 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002087 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002088 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002089 }
John Reckd7dd9b22011-08-30 09:18:29 -07002090 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2091 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002092 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002093 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002094 }
2095
John Reck34ef2672011-02-10 11:30:55 -08002096 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002097 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002098 // FIXME: Would like to make sure there is actually something to
2099 // draw, but the API for that (WebViewCore.pictureReady()) is not
2100 // currently accessible here.
2101
John Reck34ef2672011-02-10 11:30:55 -08002102 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002103 if (view == null) {
2104 // Tab was destroyed
2105 return;
2106 }
John Reck34ef2672011-02-10 11:30:55 -08002107 final String url = tab.getUrl();
2108 final String originalUrl = view.getOriginalUrl();
John Reck34ef2672011-02-10 11:30:55 -08002109 if (TextUtils.isEmpty(url)) {
2110 return;
2111 }
2112
2113 // Only update thumbnails for web urls (http(s)://), not for
2114 // about:, javascript:, data:, etc...
2115 // Unless it is a bookmarked site, then always update
2116 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2117 return;
2118 }
2119
Michael Kolb8233fac2010-10-26 16:08:53 -07002120 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2121 getDesiredThumbnailHeight(mActivity));
2122 if (bm == null) {
2123 return;
2124 }
2125
2126 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002127 new AsyncTask<Void, Void, Void>() {
2128 @Override
2129 protected Void doInBackground(Void... unused) {
2130 Cursor cursor = null;
2131 try {
2132 // TODO: Clean this up
2133 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2134 if (cursor != null && cursor.moveToFirst()) {
2135 final ByteArrayOutputStream os =
2136 new ByteArrayOutputStream();
2137 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002138
John Reck34ef2672011-02-10 11:30:55 -08002139 ContentValues values = new ContentValues();
2140 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002141
John Reck34ef2672011-02-10 11:30:55 -08002142 do {
John Reck617fd832011-02-16 14:35:59 -08002143 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002144 cr.update(Images.CONTENT_URI, values, null, null);
2145 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002146 }
John Reck34ef2672011-02-10 11:30:55 -08002147 } catch (IllegalStateException e) {
2148 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002149 } catch (SQLiteException s) {
2150 // Added for possible error when user tries to remove the same bookmark
2151 // that is being updated with a screen shot
2152 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002153 } finally {
2154 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002155 }
John Reck34ef2672011-02-10 11:30:55 -08002156 return null;
2157 }
2158 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002159 }
2160
2161 private class Copy implements OnMenuItemClickListener {
2162 private CharSequence mText;
2163
John Reck9c35b9c2012-05-30 10:08:50 -07002164 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002165 public boolean onMenuItemClick(MenuItem item) {
2166 copy(mText);
2167 return true;
2168 }
2169
2170 public Copy(CharSequence toCopy) {
2171 mText = toCopy;
2172 }
2173 }
2174
Leon Scroggins63c02662010-11-18 15:16:27 -05002175 private static class Download implements OnMenuItemClickListener {
2176 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002177 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002178 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002179 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002180 private static final String FALLBACK_EXTENSION = "dat";
2181 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002182
John Reck9c35b9c2012-05-30 10:08:50 -07002183 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002184 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002185 if (DataUri.isDataUri(mText)) {
2186 saveDataUri();
2187 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002188 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07002189 null, null, null, mPrivateBrowsing);
George Mount387d45d2011-10-07 15:57:53 -07002190 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002191 return true;
2192 }
2193
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002194 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2195 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002196 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002197 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002198 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002199 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002200 }
George Mount387d45d2011-10-07 15:57:53 -07002201
2202 /**
2203 * Treats mText as a data URI and writes its contents to a file
2204 * based on the current time.
2205 */
2206 private void saveDataUri() {
2207 FileOutputStream outputStream = null;
2208 try {
2209 DataUri uri = new DataUri(mText);
2210 File target = getTarget(uri);
2211 outputStream = new FileOutputStream(target);
2212 outputStream.write(uri.getData());
2213 final DownloadManager manager =
2214 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2215 manager.addCompletedDownload(target.getName(),
2216 mActivity.getTitle().toString(), false,
2217 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002218 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002219 } catch (IOException e) {
2220 Log.e(LOGTAG, "Could not save data URL");
2221 } finally {
2222 if (outputStream != null) {
2223 try {
2224 outputStream.close();
2225 } catch (IOException e) {
2226 // ignore close errors
2227 }
2228 }
2229 }
2230 }
2231
2232 /**
2233 * Creates a File based on the current time stamp and uses
2234 * the mime type of the DataUri to get the extension.
2235 */
2236 private File getTarget(DataUri uri) throws IOException {
2237 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002238 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002239 String nameBase = format.format(new Date());
2240 String mimeType = uri.getMimeType();
2241 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2242 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2243 if (extension == null) {
2244 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2245 extension = FALLBACK_EXTENSION;
2246 }
2247 extension = "." + extension; // createTempFile needs the '.'
2248 File targetFile = File.createTempFile(nameBase, extension, dir);
2249 return targetFile;
2250 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002251 }
2252
Cary Clark8974d282010-11-22 10:46:05 -05002253 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002254 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002255
John Reck9c35b9c2012-05-30 10:08:50 -07002256 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002257 public boolean onMenuItemClick(MenuItem item) {
2258 if (mWebView != null) {
2259 return mWebView.selectText();
2260 }
2261 return false;
2262 }
2263
2264 public SelectText(WebView webView) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002265 if (BrowserWebView.isClassic()) {
2266 mWebView = WebViewClassic.fromWebView(webView);
2267 }
Cary Clark8974d282010-11-22 10:46:05 -05002268 }
2269
2270 }
2271
Michael Kolb8233fac2010-10-26 16:08:53 -07002272 /********************** TODO: UI stuff *****************************/
2273
2274 // these methods have been copied, they still need to be cleaned up
2275
2276 /****************** tabs ***************************************************/
2277
2278 // basic tab interactions:
2279
2280 // it is assumed that tabcontrol already knows about the tab
2281 protected void addTab(Tab tab) {
2282 mUi.addTab(tab);
2283 }
2284
2285 protected void removeTab(Tab tab) {
2286 mUi.removeTab(tab);
2287 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002288 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002289 }
2290
Michael Kolbf2055602011-04-09 17:20:03 -07002291 @Override
2292 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002293 // monkey protection against delayed start
2294 if (tab != null) {
2295 mTabControl.setCurrentTab(tab);
2296 // the tab is guaranteed to have a webview after setCurrentTab
2297 mUi.setActiveTab(tab);
2298 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002299 }
2300
John Reck8bcafc12011-08-29 16:43:02 -07002301 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002302 Tab current = mTabControl.getCurrentTab();
2303 if (current != null
2304 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002305 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002306 }
2307 }
2308
John Reck26b18322011-06-21 13:08:58 -07002309 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002310 // Dismiss the subwindow if applicable.
2311 dismissSubWindow(appTab);
2312 // Since we might kill the WebView, remove it from the
2313 // content view first.
2314 mUi.detachTab(appTab);
2315 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002316 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002317 // TODO: analyze why the remove and add are necessary
2318 mUi.attachTab(appTab);
2319 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002320 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002321 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002322 } else {
2323 // If the tab was the current tab, we have to attach
2324 // it to the view system again.
2325 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002326 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002327 }
2328 }
2329
2330 // Remove the sub window if it exists. Also called by TabControl when the
2331 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002332 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002333 public void dismissSubWindow(Tab tab) {
2334 removeSubWindow(tab);
2335 // dismiss the subwindow. This will destroy the WebView.
2336 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002337 WebView wv = getCurrentTopWebView();
2338 if (wv != null) {
2339 wv.requestFocus();
2340 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002341 }
2342
2343 @Override
2344 public void removeSubWindow(Tab t) {
2345 if (t.getSubWebView() != null) {
2346 mUi.removeSubWindow(t.getSubViewContainer());
2347 }
2348 }
2349
2350 @Override
2351 public void attachSubWindow(Tab tab) {
2352 if (tab.getSubWebView() != null) {
2353 mUi.attachSubWindow(tab.getSubViewContainer());
2354 getCurrentTopWebView().requestFocus();
2355 }
2356 }
2357
Mathew Inwood29721c22011-06-29 17:55:24 +01002358 private Tab showPreloadedTab(final UrlData urlData) {
2359 if (!urlData.isPreloaded()) {
2360 return null;
2361 }
2362 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2363 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2364 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002365 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002366 // Could not submit query. Fallback to regular tab creation
2367 tabControl.destroy();
2368 return null;
2369 }
2370 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002371 // check tab count and make room for new tab
2372 if (!mTabControl.canCreateNewTab()) {
2373 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2374 if (leastUsed != null) {
2375 closeTab(leastUsed);
2376 }
2377 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002378 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002379 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002380 mTabControl.addPreloadedTab(t);
2381 addTab(t);
2382 setActiveTab(t);
2383 return t;
2384 }
2385
Michael Kolb7bcafde2011-05-09 13:55:59 -07002386 // open a non inconito tab with the given url data
2387 // and set as active tab
2388 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002389 Tab tab = showPreloadedTab(urlData);
2390 if (tab == null) {
2391 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002392 if ((tab != null) && !urlData.isEmpty()) {
2393 loadUrlDataIn(tab, urlData);
2394 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002395 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002396 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002397 }
2398
Michael Kolb843510f2010-12-09 10:51:49 -08002399 @Override
2400 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002401 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002402 }
2403
Michael Kolb8233fac2010-10-26 16:08:53 -07002404 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002405 public Tab openIncognitoTab() {
2406 return openTab(INCOGNITO_URI, true, true, false);
2407 }
2408
2409 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002410 public Tab openTab(String url, boolean incognito, boolean setActive,
2411 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002412 return openTab(url, incognito, setActive, useCurrent, null);
2413 }
2414
2415 @Override
2416 public Tab openTab(String url, Tab parent, boolean setActive,
2417 boolean useCurrent) {
2418 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2419 setActive, useCurrent, parent);
2420 }
2421
2422 public Tab openTab(String url, boolean incognito, boolean setActive,
2423 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002424 Tab tab = createNewTab(incognito, setActive, useCurrent);
2425 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002426 if (parent != null && parent != tab) {
2427 parent.addChildTab(tab);
2428 }
Michael Kolb519d2282011-05-09 17:03:19 -07002429 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002430 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002431 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002432 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002433 return tab;
2434 }
2435
2436 // this method will attempt to create a new tab
2437 // incognito: private browsing tab
2438 // setActive: ste tab as current tab
2439 // useCurrent: if no new tab can be created, return current tab
2440 private Tab createNewTab(boolean incognito, boolean setActive,
2441 boolean useCurrent) {
2442 Tab tab = null;
2443 if (mTabControl.canCreateNewTab()) {
2444 tab = mTabControl.createNewTab(incognito);
2445 addTab(tab);
2446 if (setActive) {
2447 setActiveTab(tab);
2448 }
2449 } else {
2450 if (useCurrent) {
2451 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002452 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002453 } else {
2454 mUi.showMaxTabsWarning();
2455 }
2456 }
2457 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002458 }
2459
John Reck2bc80422011-06-30 15:11:49 -07002460 @Override
2461 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2462 SnapshotTab tab = null;
2463 if (mTabControl.canCreateNewTab()) {
2464 tab = mTabControl.createSnapshotTab(snapshotId);
2465 addTab(tab);
2466 if (setActive) {
2467 setActiveTab(tab);
2468 }
2469 } else {
2470 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002471 }
2472 return tab;
2473 }
2474
Michael Kolb8233fac2010-10-26 16:08:53 -07002475 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002476 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002477 * @return boolean True if we successfully switched to a different tab. If
2478 * the indexth tab is null, or if that tab is the same as
2479 * the current one, return false.
2480 */
2481 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002482 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002483 Tab currentTab = mTabControl.getCurrentTab();
2484 if (tab == null || tab == currentTab) {
2485 return false;
2486 }
2487 setActiveTab(tab);
2488 return true;
2489 }
2490
2491 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002492 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002493 closeCurrentTab(false);
2494 }
2495
2496 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002497 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002498 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002499 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002500 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002501 return;
2502 }
Michael Kolbc831b632011-05-11 09:30:34 -07002503 final Tab current = mTabControl.getCurrentTab();
2504 final int pos = mTabControl.getCurrentPosition();
2505 Tab newTab = current.getParent();
2506 if (newTab == null) {
2507 newTab = mTabControl.getTab(pos + 1);
2508 if (newTab == null) {
2509 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002510 }
2511 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002512 if (andQuit) {
2513 mTabControl.setCurrentTab(newTab);
2514 closeTab(current);
2515 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002516 // Close window
2517 closeTab(current);
2518 }
2519 }
2520
2521 /**
2522 * Close the tab, remove its associated title bar, and adjust mTabControl's
2523 * current tab to a valid value.
2524 */
2525 @Override
2526 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002527 if (tab == mTabControl.getCurrentTab()) {
2528 closeCurrentTab();
2529 } else {
2530 removeTab(tab);
2531 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002532 }
2533
Afzal Najamd4e33312012-04-26 01:54:01 -04002534 /**
2535 * Close all tabs except the current one
2536 */
2537 @Override
2538 public void closeOtherTabs() {
2539 int inactiveTabs = mTabControl.getTabCount() - 1;
2540 for (int i = inactiveTabs; i >= 0; i--) {
2541 Tab tab = mTabControl.getTab(i);
2542 if (tab != mTabControl.getCurrentTab()) {
2543 removeTab(tab);
2544 }
2545 }
2546 }
2547
Michael Kolb8233fac2010-10-26 16:08:53 -07002548 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002549 protected void loadUrlFromContext(String url) {
2550 Tab tab = getCurrentTab();
2551 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002552 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002553 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002554 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002555 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002556 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002557 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002558 }
2559 }
2560 }
2561
2562 /**
2563 * Load the URL into the given WebView and update the title bar
2564 * to reflect the new load. Call this instead of WebView.loadUrl
2565 * directly.
2566 * @param view The WebView used to load url.
2567 * @param url The URL to load.
2568 */
John Reck71e51422011-07-01 16:49:28 -07002569 @Override
2570 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002571 loadUrl(tab, url, null);
2572 }
2573
2574 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2575 if (tab != null) {
2576 dismissSubWindow(tab);
2577 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002578 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002579 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002580 }
2581
2582 /**
2583 * Load UrlData into a Tab and update the title bar to reflect the new
2584 * load. Call this instead of UrlData.loadIn directly.
2585 * @param t The Tab used to load.
2586 * @param data The UrlData being loaded.
2587 */
2588 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002589 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002590 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002591 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002592 } else {
John Reck38b39652012-06-05 09:22:59 -07002593 if (t != null && data.mDisableUrlOverride) {
2594 t.disableUrlOverridingForLoad();
2595 }
John Reck26b18322011-06-21 13:08:58 -07002596 loadUrl(t, data.mUrl, data.mHeaders);
2597 }
2598 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002599 }
2600
John Reck30c714c2010-12-16 17:30:34 -08002601 @Override
2602 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002603 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002604 if (tab.canGoBack()) {
2605 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002606 } else {
John Reckef654f12011-07-12 16:42:08 -07002607 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002608 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002609 }
2610
2611 void goBackOnePageOrQuit() {
2612 Tab current = mTabControl.getCurrentTab();
2613 if (current == null) {
2614 /*
2615 * Instead of finishing the activity, simply push this to the back
2616 * of the stack and let ActivityManager to choose the foreground
2617 * activity. As BrowserActivity is singleTask, it will be always the
2618 * root of the task. So we can use either true or false for
2619 * moveTaskToBack().
2620 */
2621 mActivity.moveTaskToBack(true);
2622 return;
2623 }
John Reckef654f12011-07-12 16:42:08 -07002624 if (current.canGoBack()) {
2625 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002626 } else {
2627 // Check to see if we are closing a window that was created by
2628 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002629 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002630 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002631 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002632 // Now we close the other tab
2633 closeTab(current);
2634 } else {
Michael Kolbe28b3472011-08-04 16:54:31 -07002635 if ((current.getAppId() != null) || current.closeOnBack()) {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002636 closeCurrentTab(true);
Michael Kolbe28b3472011-08-04 16:54:31 -07002637 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002638 /*
2639 * Instead of finishing the activity, simply push this to the back
2640 * of the stack and let ActivityManager to choose the foreground
2641 * activity. As BrowserActivity is singleTask, it will be always the
2642 * root of the task. So we can use either true or false for
2643 * moveTaskToBack().
2644 */
2645 mActivity.moveTaskToBack(true);
2646 }
2647 }
2648 }
2649
2650 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002651 * helper method for key handler
2652 * returns the current tab if it can't advance
2653 */
Michael Kolbc831b632011-05-11 09:30:34 -07002654 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002655 int pos = mTabControl.getCurrentPosition() + 1;
2656 if (pos >= mTabControl.getTabCount()) {
2657 pos = 0;
2658 }
2659 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002660 }
2661
2662 /**
2663 * helper method for key handler
2664 * returns the current tab if it can't advance
2665 */
Michael Kolbc831b632011-05-11 09:30:34 -07002666 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002667 int pos = mTabControl.getCurrentPosition() - 1;
2668 if ( pos < 0) {
2669 pos = mTabControl.getTabCount() - 1;
2670 }
2671 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002672 }
2673
John Reckbcef87f2012-02-03 14:58:34 -08002674 boolean isMenuOrCtrlKey(int keyCode) {
2675 return (KeyEvent.KEYCODE_MENU == keyCode)
2676 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2677 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2678 }
2679
Michael Kolb0035fad2011-03-14 13:25:28 -07002680 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002681 * handle key events in browser
2682 *
2683 * @param keyCode
2684 * @param event
2685 * @return true if handled, false to pass to super
2686 */
John Reck9c35b9c2012-05-30 10:08:50 -07002687 @Override
2688 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002689 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002690 // Even if MENU is already held down, we need to call to super to open
2691 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002692 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002693 mMenuIsDown = true;
2694 return false;
2695 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002696
Cary Clark8ff8c662010-12-29 15:03:05 -05002697 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002698 Tab tab = getCurrentTab();
2699 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002700
Cary Clark160bbb92011-01-10 11:17:07 -05002701 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2702 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002703
Michael Kolb8233fac2010-10-26 16:08:53 -07002704 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002705 case KeyEvent.KEYCODE_TAB:
2706 if (event.isCtrlPressed()) {
2707 if (event.isShiftPressed()) {
2708 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002709 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002710 } else {
2711 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002712 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002713 }
2714 return true;
2715 }
2716 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002717 case KeyEvent.KEYCODE_SPACE:
2718 // WebView/WebTextView handle the keys in the KeyDown. As
2719 // the Activity's shortcut keys are only handled when WebView
2720 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002721 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002722 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002723 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002724 pageDown();
2725 }
2726 return true;
2727 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002728 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002729 event.startTracking();
2730 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002731 case KeyEvent.KEYCODE_FORWARD:
2732 if (!noModifiers) break;
2733 tab.goForward();
2734 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002735 case KeyEvent.KEYCODE_DPAD_LEFT:
2736 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002737 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002738 return true;
2739 }
2740 break;
2741 case KeyEvent.KEYCODE_DPAD_RIGHT:
2742 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002743 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002744 return true;
2745 }
2746 break;
2747 case KeyEvent.KEYCODE_A:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002748 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002749 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002750 return true;
2751 }
2752 break;
Michael Kolba4183062011-01-16 10:43:21 -08002753// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002754 case KeyEvent.KEYCODE_C:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002755 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002756 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002757 return true;
2758 }
2759 break;
Michael Kolba4183062011-01-16 10:43:21 -08002760// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002761// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002762// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002763// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002764// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002765// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002766// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002767// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002768// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002769// case KeyEvent.KEYCODE_M: // unused
2770// case KeyEvent.KEYCODE_N: // in Chrome: new window
2771// case KeyEvent.KEYCODE_O: // in Chrome: open file
2772// case KeyEvent.KEYCODE_P: // in Chrome: print page
2773// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002774// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002775// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2776 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002777 // we can't use the ctrl/shift flags, they check for
2778 // exclusive use of a modifier
2779 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002780 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002781 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002782 } else {
2783 openTabToHomePage();
2784 }
2785 return true;
2786 }
2787 break;
2788// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2789// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002790// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002791// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2792// case KeyEvent.KEYCODE_Y: // unused
2793// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002794 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002795 // it is a regular key and webview is not null
2796 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002797 }
2798
John Reck9c35b9c2012-05-30 10:08:50 -07002799 @Override
2800 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002801 switch(keyCode) {
2802 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002803 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002804 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002805 return true;
2806 }
2807 break;
2808 }
2809 return false;
2810 }
2811
John Reck9c35b9c2012-05-30 10:08:50 -07002812 @Override
2813 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002814 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002815 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002816 if (KeyEvent.KEYCODE_MENU == keyCode
2817 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002818 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002819 }
2820 }
Cary Clark160bbb92011-01-10 11:17:07 -05002821 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002822 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002823 case KeyEvent.KEYCODE_BACK:
2824 if (event.isTracking() && !event.isCanceled()) {
2825 onBackKey();
2826 return true;
2827 }
2828 break;
2829 }
2830 return false;
2831 }
2832
2833 public boolean isMenuDown() {
2834 return mMenuIsDown;
2835 }
2836
John Reck9c35b9c2012-05-30 10:08:50 -07002837 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002838 public void setupAutoFill(Message message) {
2839 // Open the settings activity at the AutoFill profile fragment so that
2840 // the user can create a new profile. When they return, we will dispatch
2841 // the message so that we can autofill the form using their new profile.
2842 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2843 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2844 AutoFillSettingsFragment.class.getName());
2845 mAutoFillSetupMessage = message;
2846 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2847 }
John Reckb3417f02011-01-14 11:01:05 -08002848
John Reck9c35b9c2012-05-30 10:08:50 -07002849 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002850 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002851 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002852 return true;
2853 }
2854
John Reck1cf4b792011-07-26 10:22:22 -07002855 @Override
2856 public boolean shouldCaptureThumbnails() {
2857 return mUi.shouldCaptureThumbnails();
2858 }
2859
Michael Kolbc3af0672011-08-09 10:24:41 -07002860 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07002861 public boolean supportsVoice() {
2862 PackageManager pm = mActivity.getPackageManager();
2863 List activities = pm.queryIntentActivities(new Intent(
2864 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
2865 return activities.size() != 0;
2866 }
2867
2868 @Override
2869 public void startVoiceRecognizer() {
2870 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
2871 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
2872 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
2873 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
2874 mActivity.startActivityForResult(voice, VOICE_RESULT);
2875 }
2876
2877 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002878 public void setBlockEvents(boolean block) {
2879 mBlockEvents = block;
2880 }
2881
John Reck9c35b9c2012-05-30 10:08:50 -07002882 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002883 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002884 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002885 }
2886
John Reck9c35b9c2012-05-30 10:08:50 -07002887 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002888 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002889 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002890 }
2891
John Reck9c35b9c2012-05-30 10:08:50 -07002892 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002893 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002894 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002895 }
2896
John Reck9c35b9c2012-05-30 10:08:50 -07002897 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002898 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002899 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002900 }
2901
John Reck9c35b9c2012-05-30 10:08:50 -07002902 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002903 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002904 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002905 }
2906
Michael Kolb8233fac2010-10-26 16:08:53 -07002907}