blob: e84358fd9272635e3ba1723af45411fb03461399 [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
John Reck68234a92012-04-19 15:27:12 -070020import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070022import android.app.ProgressDialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070024import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070025import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070026import android.content.ContentValues;
27import android.content.Context;
John Reck68234a92012-04-19 15:27:12 -070028import android.content.DialogInterface;
29import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070030import android.content.Intent;
31import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
33import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070034import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050035import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.database.Cursor;
37import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020038import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070039import android.graphics.Bitmap;
40import android.graphics.Canvas;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.net.Uri;
42import android.net.http.SslError;
43import android.os.AsyncTask;
44import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070045import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070046import android.os.Handler;
47import android.os.Message;
48import android.os.PowerManager;
49import android.os.PowerManager.WakeLock;
kaiyiza8b6dbb2013-07-29 18:11:22 +080050import android.os.SystemProperties;
Ben Murdoch8029a772010-11-16 11:58:21 +000051import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070052import android.provider.Browser;
53import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.provider.BrowserContract.Images;
55import android.provider.ContactsContract;
56import android.provider.ContactsContract.Intents.Insert;
Michael Kolb0b129122012-06-04 16:31:58 -070057import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070058import android.text.TextUtils;
59import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080060import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070061import android.view.ActionMode;
62import android.view.ContextMenu;
63import android.view.ContextMenu.ContextMenuInfo;
64import android.view.Gravity;
65import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070066import android.view.Menu;
67import android.view.MenuInflater;
68import android.view.MenuItem;
69import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070070import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070071import android.view.View;
72import android.webkit.CookieManager;
73import android.webkit.CookieSyncManager;
74import android.webkit.HttpAuthHandler;
George Mount387d45d2011-10-07 15:57:53 -070075import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070076import android.webkit.SslErrorHandler;
77import android.webkit.ValueCallback;
78import android.webkit.WebChromeClient;
79import android.webkit.WebIconDatabase;
80import android.webkit.WebSettings;
81import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000082import android.webkit.WebViewClassic;
Leon Scrogginsac993842011-02-02 12:54:07 -050083import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070084
Michael Kolb4bd767d2011-05-27 11:33:55 -070085import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070086import com.android.browser.UI.ComboViews;
John Reck1cf4b792011-07-26 10:22:22 -070087import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070088import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070089
Michael Kolb8233fac2010-10-26 16:08:53 -070090import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -070091import java.io.File;
92import java.io.FileOutputStream;
93import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -070094import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -070095import java.text.DateFormat;
96import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -070097import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -070098import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -070099import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700100import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800101import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200102import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700103import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700104
105/**
106 * Controller for browser
107 */
108public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700109 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700110
111 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800112 private static final String SEND_APP_ID_EXTRA =
113 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700114 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800115
Michael Kolb8233fac2010-10-26 16:08:53 -0700116
117 // public message ids
118 public final static int LOAD_URL = 1001;
119 public final static int STOP_LOAD = 1002;
120
121 // Message Ids
122 private static final int FOCUS_NODE_HREF = 102;
123 private static final int RELEASE_WAKELOCK = 107;
124
125 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
126
127 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800128 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700129
130 private static final int EMPTY_MENU = -1;
131
Michael Kolb8233fac2010-10-26 16:08:53 -0700132 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700133 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700134 final static int PREFERENCES_PAGE = 3;
135 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000136 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700137 final static int VOICE_RESULT = 6;
Ben Murdoch8029a772010-11-16 11:58:21 +0000138
Michael Kolb8233fac2010-10-26 16:08:53 -0700139 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
140
141 // As the ids are dynamically created, we can't guarantee that they will
142 // be in sequence, so this static array maps ids to a window number.
143 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
144 { R.id.window_one_menu_id, R.id.window_two_menu_id,
145 R.id.window_three_menu_id, R.id.window_four_menu_id,
146 R.id.window_five_menu_id, R.id.window_six_menu_id,
147 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
148
149 // "source" parameter for Google search through search key
150 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
151 // "source" parameter for Google search through simplily type
152 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
153
George Mount387d45d2011-10-07 15:57:53 -0700154 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700155 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
156
John Reckd7dd9b22011-08-30 09:18:29 -0700157 // A bitmap that is re-used in createScreenshot as scratch space
158 private static Bitmap sThumbnailBitmap;
159
Michael Kolb8233fac2010-10-26 16:08:53 -0700160 private Activity mActivity;
161 private UI mUi;
162 private TabControl mTabControl;
163 private BrowserSettings mSettings;
164 private WebViewFactory mFactory;
165
166 private WakeLock mWakeLock;
167
168 private UrlHandler mUrlHandler;
169 private UploadHandler mUploadHandler;
170 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700171 private PageDialogsHandler mPageDialogsHandler;
172 private NetworkStateHandler mNetworkHandler;
173
Ben Murdoch8029a772010-11-16 11:58:21 +0000174 private Message mAutoFillSetupMessage;
175
Michael Kolb8233fac2010-10-26 16:08:53 -0700176 private boolean mShouldShowErrorConsole;
177
178 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
179
180 // FIXME, temp address onPrepareMenu performance problem.
181 // When we move everything out of view, we should rewrite this.
182 private int mCurrentMenuState = 0;
183 private int mMenuState = R.id.MAIN_MENU;
184 private int mOldMenuState = EMPTY_MENU;
185 private Menu mCachedMenu;
186
Michael Kolb8233fac2010-10-26 16:08:53 -0700187 private boolean mMenuIsDown;
188
189 // For select and find, we keep track of the ActionMode so that
190 // finish() can be called as desired.
191 private ActionMode mActionMode;
192
193 /**
194 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
195 * of whether the configuration has changed. The first onMenuOpened call
196 * after a configuration change is simply a reopening of the same menu
197 * (i.e. mIconView did not change).
198 */
199 private boolean mConfigChanged;
200
201 /**
202 * Keeps track of whether the options menu is open. This is important in
203 * determining whether to show or hide the title bar overlay
204 */
205 private boolean mOptionsMenuOpen;
206
207 /**
208 * Whether or not the options menu is in its bigger, popup menu form. When
209 * true, we want the title bar overlay to be gone. When false, we do not.
210 * Only meaningful if mOptionsMenuOpen is true.
211 */
212 private boolean mExtendedMenuOpen;
213
Michael Kolb8233fac2010-10-26 16:08:53 -0700214 private boolean mActivityPaused = true;
215 private boolean mLoadStopped;
216
217 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500218 // Checks to see when the bookmarks database has changed, and updates the
219 // Tabs' notion of whether they represent bookmarked sites.
220 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700221 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700222
Michael Kolbc3af0672011-08-09 10:24:41 -0700223 private boolean mBlockEvents;
224
Michael Kolb0b129122012-06-04 16:31:58 -0700225 private String mVoiceResult;
226
George Mount3636d0a2011-11-21 09:08:21 -0800227 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700228 mActivity = browser;
229 mSettings = BrowserSettings.getInstance();
230 mTabControl = new TabControl(this);
231 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700232 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800233 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700234 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700235
236 mUrlHandler = new UrlHandler(this);
237 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700238 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
239
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500241 mBookmarksObserver = new ContentObserver(mHandler) {
242 @Override
243 public void onChange(boolean selfChange) {
244 int size = mTabControl.getTabCount();
245 for (int i = 0; i < size; i++) {
246 mTabControl.getTab(i).updateBookmarkedStatus();
247 }
248 }
249
250 };
251 browser.getContentResolver().registerContentObserver(
252 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700253
254 mNetworkHandler = new NetworkStateHandler(mActivity, this);
255 // Start watching the default geolocation permissions
256 mSystemAllowGeolocationOrigins =
257 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
258 mSystemAllowGeolocationOrigins.start();
259
John Reckaf262e72011-07-25 13:55:44 -0700260 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700261 }
262
John Reck9c35b9c2012-05-30 10:08:50 -0700263 @Override
264 public void start(final Intent intent) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800265 if (BrowserWebView.isClassic()) WebViewClassic.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800266 // mCrashRecoverHandler has any previously saved state.
267 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700268 }
269
George Mount3636d0a2011-11-21 09:08:21 -0800270 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700271 // Unless the last browser usage was within 24 hours, destroy any
272 // remaining incognito tabs.
273
274 Calendar lastActiveDate = icicle != null ?
275 (Calendar) icicle.getSerializable("lastActiveDate") : null;
276 Calendar today = Calendar.getInstance();
277 Calendar yesterday = Calendar.getInstance();
278 yesterday.add(Calendar.DATE, -1);
279
Patrick Scott7d50a932011-02-04 09:27:26 -0500280 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700281 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800282 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700283
Patrick Scott7d50a932011-02-04 09:27:26 -0500284 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700285 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500286 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000287
Michael Kolbc831b632011-05-11 09:30:34 -0700288 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500289 // Not able to restore so we go ahead and clear session cookies. We
290 // must do this before trying to login the user as we don't want to
291 // clear any session cookies set during login.
292 CookieManager.getInstance().removeSessionCookie();
293 }
294
Patrick Scottd43e75a2011-03-14 14:47:23 -0400295 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500296 new Runnable() {
297 @Override public void run() {
George Mount3636d0a2011-11-21 09:08:21 -0800298 onPreloginFinished(icicle, intent, currentTabId,
299 restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500300 }
301 });
302 }
303
Michael Kolbc831b632011-05-11 09:30:34 -0700304 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
George Mount3636d0a2011-11-21 09:08:21 -0800305 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700306 if (currentTabId == -1) {
John Reck1cf4b792011-07-26 10:22:22 -0700307 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800308 if (intent == null) {
309 // This won't happen under common scenarios. The icicle is
310 // not null, but there aren't any tabs to restore.
311 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700312 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800313 final Bundle extra = intent.getExtras();
314 // Create an initial tab.
315 // If the intent is ACTION_VIEW and data is not null, the Browser is
316 // invoked to view the content by another application. In this case,
317 // the tab will be close when exit.
318 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
319 Tab t = null;
320 if (urlData.isEmpty()) {
321 t = openTabToHomePage();
322 } else {
323 t = openTab(urlData);
324 }
325 if (t != null) {
326 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
327 }
328 WebView webView = t.getWebView();
329 if (extra != null) {
330 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
331 if (scale > 0 && scale <= 1000) {
332 webView.setInitialScale(scale);
333 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700334 }
335 }
John Reckd8c74522011-06-14 08:45:00 -0700336 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700337 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700338 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500339 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700340 List<Tab> tabs = mTabControl.getTabs();
341 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
342 for (Tab t : tabs) {
343 restoredTabs.add(t.getId());
344 }
345 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700346 if (tabs.size() == 0) {
347 openTabToHomePage();
348 }
John Reck1cf4b792011-07-26 10:22:22 -0700349 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700350 // TabControl.restoreState() will create a new tab even if
351 // restoring the state fails.
352 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800353 // Intent is non-null when framework thinks the browser should be
354 // launching with a new intent (icicle is null).
355 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700356 mIntentHandler.onNewIntent(intent);
357 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700359 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700360 String jsFlags = getSettings().getJsEngineFlags();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800361 if (jsFlags.trim().length() != 0 && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000362 WebViewClassic.fromWebView(getCurrentWebView()).setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700363 }
George Mount3636d0a2011-11-21 09:08:21 -0800364 if (intent != null
365 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700366 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800367 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700368 }
369
John Reck1cf4b792011-07-26 10:22:22 -0700370 private static class PruneThumbnails implements Runnable {
371 private Context mContext;
372 private List<Long> mIds;
373
374 PruneThumbnails(Context context, List<Long> preserveIds) {
375 mContext = context.getApplicationContext();
376 mIds = preserveIds;
377 }
378
379 @Override
380 public void run() {
381 ContentResolver cr = mContext.getContentResolver();
382 if (mIds == null || mIds.size() == 0) {
383 cr.delete(Thumbnails.CONTENT_URI, null, null);
384 } else {
385 int length = mIds.size();
386 StringBuilder where = new StringBuilder();
387 where.append(Thumbnails._ID);
388 where.append(" not in (");
389 for (int i = 0; i < length; i++) {
390 where.append(mIds.get(i));
391 if (i < (length - 1)) {
392 where.append(",");
393 }
394 }
395 where.append(")");
396 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
397 }
398 }
399
400 }
401
Michael Kolb1514bb72010-11-22 09:11:48 -0800402 @Override
403 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700404 return mFactory;
405 }
406
407 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800408 public void onSetWebView(Tab tab, WebView view) {
409 mUi.onSetWebView(tab, view);
410 }
411
412 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800413 public void createSubWindow(Tab tab) {
414 endActionMode();
415 WebView mainView = tab.getWebView();
416 WebView subView = mFactory.createWebView((mainView == null)
417 ? false
418 : mainView.isPrivateBrowsingEnabled());
419 mUi.createSubWindow(tab, subView);
420 }
421
422 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700423 public Context getContext() {
424 return mActivity;
425 }
426
427 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700428 public Activity getActivity() {
429 return mActivity;
430 }
431
432 void setUi(UI ui) {
433 mUi = ui;
434 }
435
Michael Kolbaf63dba2012-05-16 12:58:05 -0700436 @Override
437 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700438 return mSettings;
439 }
440
441 IntentHandler getIntentHandler() {
442 return mIntentHandler;
443 }
444
445 @Override
446 public UI getUi() {
447 return mUi;
448 }
449
450 int getMaxTabs() {
451 return mActivity.getResources().getInteger(R.integer.max_tabs);
452 }
453
454 @Override
455 public TabControl getTabControl() {
456 return mTabControl;
457 }
458
Michael Kolb1bf23132010-11-19 12:55:12 -0800459 @Override
460 public List<Tab> getTabs() {
461 return mTabControl.getTabs();
462 }
463
John Reckaf262e72011-07-25 13:55:44 -0700464 // Open the icon database.
465 private void openIconDatabase() {
466 // We have to call getInstance on the UI thread
467 final WebIconDatabase instance = WebIconDatabase.getInstance();
468 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000469
John Reckaf262e72011-07-25 13:55:44 -0700470 @Override
471 public void run() {
472 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700473 }
John Reckaf262e72011-07-25 13:55:44 -0700474 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700475 }
476
477 private void startHandler() {
478 mHandler = new Handler() {
479
480 @Override
481 public void handleMessage(Message msg) {
482 switch (msg.what) {
483 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700484 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700485 break;
486 case FOCUS_NODE_HREF:
487 {
488 String url = (String) msg.getData().get("url");
489 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500490 String src = (String) msg.getData().get("src");
491 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700492 if (TextUtils.isEmpty(url)) {
493 break;
494 }
495 HashMap focusNodeMap = (HashMap) msg.obj;
496 WebView view = (WebView) focusNodeMap.get("webview");
497 // Only apply the action if the top window did not change.
498 if (getCurrentTopWebView() != view) {
499 break;
500 }
501 switch (msg.arg1) {
502 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700503 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500505 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700506 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500507 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500508 case R.id.open_newtab_context_menu_id:
509 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700510 openTab(url, parent,
511 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500512 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700513 case R.id.copy_link_context_menu_id:
514 copy(url);
515 break;
516 case R.id.save_link_context_menu_id:
517 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500518 DownloadHandler.onDownloadStartNoStream(
Andreas Sandblad8e4ce662012-06-11 11:02:49 +0200519 mActivity, url, view.getSettings().getUserAgentString(),
Jean-Baptiste Queru2584c312012-09-04 14:39:07 -0700520 null, null, null, view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700521 break;
522 }
523 break;
524 }
525
526 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700527 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700528 break;
529
530 case STOP_LOAD:
531 stopLoading();
532 break;
533
534 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700535 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700536 mWakeLock.release();
537 // if we reach here, Browser should be still in the
538 // background loading after WAKELOCK_TIMEOUT (5-min).
539 // To avoid burning the battery, stop loading.
540 mTabControl.stopAllLoading();
541 }
542 break;
543
544 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800545 Tab tab = (Tab) msg.obj;
546 if (tab != null) {
547 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700548 }
549 break;
kaiyiz591110b2013-08-06 17:11:06 +0800550
551 case OPEN_MENU:
552 if (!mOptionsMenuOpen && mActivity != null ) {
553 mActivity.openOptionsMenu();
554 }
555 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700556 }
557 }
558 };
559
560 }
561
John Reckef654f12011-07-12 16:42:08 -0700562 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200563 public Tab getCurrentTab() {
564 return mTabControl.getCurrentTab();
565 }
566
Michael Kolbba99c5d2010-11-29 14:57:41 -0800567 @Override
568 public void shareCurrentPage() {
569 shareCurrentPage(mTabControl.getCurrentTab());
570 }
571
572 private void shareCurrentPage(Tab tab) {
573 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800574 sharePage(mActivity, tab.getTitle(),
575 tab.getUrl(), tab.getFavicon(),
576 createScreenshot(tab.getWebView(),
577 getDesiredThumbnailWidth(mActivity),
578 getDesiredThumbnailHeight(mActivity)));
579 }
580 }
581
Michael Kolb8233fac2010-10-26 16:08:53 -0700582 /**
583 * Share a page, providing the title, url, favicon, and a screenshot. Uses
584 * an {@link Intent} to launch the Activity chooser.
585 * @param c Context used to launch a new Activity.
586 * @param title Title of the page. Stored in the Intent with
587 * {@link Intent#EXTRA_SUBJECT}
588 * @param url URL of the page. Stored in the Intent with
589 * {@link Intent#EXTRA_TEXT}
590 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
591 * with {@link Browser#EXTRA_SHARE_FAVICON}
592 * @param screenshot Bitmap of a screenshot of the page. Stored in the
593 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
594 */
595 static final void sharePage(Context c, String title, String url,
596 Bitmap favicon, Bitmap screenshot) {
597 Intent send = new Intent(Intent.ACTION_SEND);
598 send.setType("text/plain");
599 send.putExtra(Intent.EXTRA_TEXT, url);
600 send.putExtra(Intent.EXTRA_SUBJECT, title);
601 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
602 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
603 try {
604 c.startActivity(Intent.createChooser(send, c.getString(
605 R.string.choosertitle_sharevia)));
606 } catch(android.content.ActivityNotFoundException ex) {
607 // if no app handles it, do nothing
608 }
609 }
610
611 private void copy(CharSequence text) {
612 ClipboardManager cm = (ClipboardManager) mActivity
613 .getSystemService(Context.CLIPBOARD_SERVICE);
614 cm.setText(text);
615 }
616
617 // lifecycle
618
John Reck9c35b9c2012-05-30 10:08:50 -0700619 @Override
620 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800622 // update the menu in case of a locale change
623 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800624 if (mOptionsMenuOpen) {
625 mActivity.closeOptionsMenu();
626 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
627 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700628 if (mPageDialogsHandler != null) {
629 mPageDialogsHandler.onConfigurationChanged(config);
630 }
631 mUi.onConfigurationChanged(config);
632 }
633
634 @Override
635 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700636 if (!mUi.isWebShowing()) {
637 mUi.showWeb(false);
638 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 mIntentHandler.onNewIntent(intent);
640 }
641
John Reck9c35b9c2012-05-30 10:08:50 -0700642 @Override
643 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800644 if (mUi.isCustomViewShowing()) {
645 hideCustomView();
646 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700647 if (mActivityPaused) {
648 Log.e(LOGTAG, "BrowserActivity is already paused.");
649 return;
650 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700651 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800652 Tab tab = mTabControl.getCurrentTab();
653 if (tab != null) {
654 tab.pause();
655 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700656 if (mWakeLock == null) {
657 PowerManager pm = (PowerManager) mActivity
658 .getSystemService(Context.POWER_SERVICE);
659 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
660 }
Michael Kolb70976932010-11-30 11:34:01 -0800661 mWakeLock.acquire();
662 mHandler.sendMessageDelayed(mHandler
663 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
664 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700665 }
666 mUi.onPause();
667 mNetworkHandler.onPause();
668
669 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100670 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700671 if (sThumbnailBitmap != null) {
672 sThumbnailBitmap.recycle();
673 sThumbnailBitmap = null;
674 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700675 }
676
John Reck9c35b9c2012-05-30 10:08:50 -0700677 @Override
678 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700679 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800680 Bundle saveState = createSaveState();
681
682 // crash recovery manages all save & restore state
683 mCrashRecoveryHandler.writeState(saveState);
684 mSettings.setLastRunPaused(true);
685 }
686
687 /**
688 * Save the current state to outState. Does not write the state to
689 * disk.
690 * @return Bundle containing the current state of all tabs.
691 */
692 /* package */ Bundle createSaveState() {
693 Bundle saveState = new Bundle();
694 mTabControl.saveState(saveState);
695 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700696 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800697 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700698 }
George Mount3636d0a2011-11-21 09:08:21 -0800699 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700700 }
701
John Reck9c35b9c2012-05-30 10:08:50 -0700702 @Override
703 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700704 if (!mActivityPaused) {
705 Log.e(LOGTAG, "BrowserActivity is already resumed.");
706 return;
707 }
George Mount3636d0a2011-11-21 09:08:21 -0800708 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700709 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800710 Tab current = mTabControl.getCurrentTab();
711 if (current != null) {
712 current.resume();
713 resumeWebViewTimers(current);
714 }
John Reckf57c0292011-07-21 18:15:39 -0700715 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200716
Michael Kolb8233fac2010-10-26 16:08:53 -0700717 mUi.onResume();
718 mNetworkHandler.onResume();
719 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100720 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700721 if (mVoiceResult != null) {
722 mUi.onVoiceResult(mVoiceResult);
723 mVoiceResult = null;
724 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700725 }
726
John Reckf57c0292011-07-21 18:15:39 -0700727 private void releaseWakeLock() {
728 if (mWakeLock != null && mWakeLock.isHeld()) {
729 mHandler.removeMessages(RELEASE_WAKELOCK);
730 mWakeLock.release();
731 }
732 }
733
Michael Kolb70976932010-11-30 11:34:01 -0800734 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800735 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800736 * @param tab guaranteed non-null
737 */
738 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700739 boolean inLoad = tab.inPageLoad();
740 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
741 CookieSyncManager.getInstance().startSync();
742 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100743 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700744 }
745 }
746
Michael Kolb70976932010-11-30 11:34:01 -0800747 /**
748 * Pause all WebView timers using the WebView of the given tab
749 * @param tab
750 * @return true if the timers are paused or tab is null
751 */
752 private boolean pauseWebViewTimers(Tab tab) {
753 if (tab == null) {
754 return true;
755 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700756 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100757 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700758 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700759 }
Michael Kolb70976932010-11-30 11:34:01 -0800760 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700761 }
762
John Reck9c35b9c2012-05-30 10:08:50 -0700763 @Override
764 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800765 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700766 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
767 mUploadHandler = null;
768 }
769 if (mTabControl == null) return;
770 mUi.onDestroy();
771 // Remove the current tab and sub window
772 Tab t = mTabControl.getCurrentTab();
773 if (t != null) {
774 dismissSubWindow(t);
775 removeTab(t);
776 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500777 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700778 // Destroy all the tabs
779 mTabControl.destroy();
780 WebIconDatabase.getInstance().close();
781 // Stop watching the default geolocation permissions
782 mSystemAllowGeolocationOrigins.stop();
783 mSystemAllowGeolocationOrigins = null;
kaiyiza8b6dbb2013-07-29 18:11:22 +0800784 mCrashRecoveryHandler.clearState();
Michael Kolb8233fac2010-10-26 16:08:53 -0700785 }
786
787 protected boolean isActivityPaused() {
788 return mActivityPaused;
789 }
790
John Reck9c35b9c2012-05-30 10:08:50 -0700791 @Override
792 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700793 mTabControl.freeMemory();
794 }
795
796 @Override
797 public boolean shouldShowErrorConsole() {
798 return mShouldShowErrorConsole;
799 }
800
801 protected void setShouldShowErrorConsole(boolean show) {
802 if (show == mShouldShowErrorConsole) {
803 // Nothing to do.
804 return;
805 }
806 mShouldShowErrorConsole = show;
807 Tab t = mTabControl.getCurrentTab();
808 if (t == null) {
809 // There is no current tab so we cannot toggle the error console
810 return;
811 }
812 mUi.setShouldShowErrorConsole(t, show);
813 }
814
815 @Override
816 public void stopLoading() {
817 mLoadStopped = true;
818 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700819 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700820 if (w != null) {
821 w.stopLoading();
822 mUi.onPageStopped(tab);
823 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700824 }
825
826 boolean didUserStopLoading() {
827 return mLoadStopped;
828 }
829
830 // WebViewController
831
832 @Override
John Reck324d4402011-01-11 16:56:42 -0800833 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700834
835 // We've started to load a new page. If there was a pending message
836 // to save a screenshot then we will now take the new page and save
837 // an incorrect screenshot. Therefore, remove any pending thumbnail
838 // messages from the queue.
839 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800840 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700841
842 // reset sync timer to avoid sync starts during loading a page
843 CookieSyncManager.getInstance().resetSync();
844
845 if (!mNetworkHandler.isNetworkUp()) {
846 view.setNetworkAvailable(false);
847 }
848
849 // when BrowserActivity just starts, onPageStarted may be called before
850 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
851 // to start the timer. As we won't switch tabs while an activity is in
852 // pause state, we can ensure calling resume and pause in pair.
853 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800854 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700855 }
856 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700857 endActionMode();
858
John Reck30c714c2010-12-16 17:30:34 -0800859 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700860
John Reck324d4402011-01-11 16:56:42 -0800861 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700862 // update the bookmark database for favicon
863 maybeUpdateFavicon(tab, null, url, favicon);
864
865 Performance.tracePageStart(url);
866
867 // Performance probe
868 if (false) {
869 Performance.onPageStarted();
870 }
871
872 }
873
874 @Override
John Reck324d4402011-01-11 16:56:42 -0800875 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700876 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800877 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200878
Michael Kolb8233fac2010-10-26 16:08:53 -0700879 // Performance probe
880 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800881 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700882 }
883
884 Performance.tracePageFinished();
885 }
886
887 @Override
John Reck30c714c2010-12-16 17:30:34 -0800888 public void onProgressChanged(Tab tab) {
889 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700890
891 if (newProgress == 100) {
892 CookieSyncManager.getInstance().sync();
893 // onProgressChanged() may continue to be called after the main
894 // frame has finished loading, as any remaining sub frames continue
895 // to load. We'll only get called once though with newProgress as
896 // 100 when everything is loaded. (onPageFinished is called once
897 // when the main frame completes loading regardless of the state of
898 // any sub frames so calls to onProgressChanges may continue after
899 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800900 if (tab.inPageLoad()) {
901 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200902 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
903 // pause the WebView timer and release the wake lock if it is
904 // finished while BrowserActivity is in pause state.
905 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700906 }
John Reckd9862372012-02-21 15:04:50 -0800907 if (!tab.isPrivateBrowsingEnabled()
908 && !TextUtils.isEmpty(tab.getUrl())
909 && !tab.isSnapshot()) {
910 // Only update the bookmark screenshot if the user did not
911 // cancel the load early and there is not already
912 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700913 if (tab.shouldUpdateThumbnail() &&
914 (tab.inForeground() && !didUserStopLoading()
915 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800916 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
917 mHandler.sendMessageDelayed(mHandler.obtainMessage(
918 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
919 500);
920 }
921 }
922 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700923 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800924 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700925 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800926 // still loading
927 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -0700928 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800929 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700930 }
931 }
John Reck30c714c2010-12-16 17:30:34 -0800932 mUi.onProgressChanged(tab);
933 }
934
935 @Override
Steve Block2466eff2011-10-03 15:33:09 +0100936 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800937 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 }
939
940 @Override
941 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800942 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800943 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800944 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700945 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
946 return;
947 }
948 // Update the title in the history database if not in private browsing mode
949 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -0700950 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700951 }
952 }
953
954 @Override
955 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800956 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700957 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
958 }
959
960 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800961 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
962 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700963 }
964
965 @Override
966 public boolean shouldOverrideKeyEvent(KeyEvent event) {
967 if (mMenuIsDown) {
968 // only check shortcut key when MENU is held
969 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
970 event);
971 } else {
972 return false;
973 }
974 }
975
976 @Override
John Reck997b1b72012-04-19 18:08:25 -0700977 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700978 if (!isActivityPaused()) {
979 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -0700980 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700981 } else {
John Reck997b1b72012-04-19 18:08:25 -0700982 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700983 }
984 }
John Reck997b1b72012-04-19 18:08:25 -0700985 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700986 }
987
988 @Override
John Reck324d4402011-01-11 16:56:42 -0800989 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700990 // Don't save anything in private browsing mode
991 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800992 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700993
John Reck324d4402011-01-11 16:56:42 -0800994 if (TextUtils.isEmpty(url)
995 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700996 return;
997 }
John Reckf57c0292011-07-21 18:15:39 -0700998 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -0700999 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001000 }
1001
1002 @Override
1003 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1004 AsyncTask<Void, Void, String[]> task =
1005 new AsyncTask<Void, Void, String[]>() {
1006 @Override
1007 public String[] doInBackground(Void... unused) {
1008 return Browser.getVisitedHistory(mActivity.getContentResolver());
1009 }
1010 @Override
1011 public void onPostExecute(String[] result) {
1012 callback.onReceiveValue(result);
1013 }
1014 };
1015 task.execute();
1016 }
1017
1018 @Override
1019 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1020 final HttpAuthHandler handler, final String host,
1021 final String realm) {
1022 String username = null;
1023 String password = null;
1024
1025 boolean reuseHttpAuthUsernamePassword
1026 = handler.useHttpAuthUsernamePassword();
1027
1028 if (reuseHttpAuthUsernamePassword && view != null) {
1029 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1030 if (credentials != null && credentials.length == 2) {
1031 username = credentials[0];
1032 password = credentials[1];
1033 }
1034 }
1035
1036 if (username != null && password != null) {
1037 handler.proceed(username, password);
1038 } else {
Ben Murdochfdd37112011-08-05 15:48:14 +01001039 if (tab.inForeground() && !handler.suppressDialog()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001040 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1041 } else {
1042 handler.cancel();
1043 }
1044 }
1045 }
1046
1047 @Override
1048 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001049 String contentDisposition, String mimetype, String referer,
1050 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001051 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001052 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001053 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled());
qqzhoua95a2e22013-04-18 17:28:31 +08001054 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001055 // This Tab was opened for the sole purpose of downloading a
1056 // file. Remove it.
1057 if (tab == mTabControl.getCurrentTab()) {
1058 // In this case, the Tab is still on top.
1059 goBackOnePageOrQuit();
1060 } else {
1061 // In this case, it is not.
1062 closeTab(tab);
1063 }
1064 }
1065 }
1066
1067 @Override
1068 public Bitmap getDefaultVideoPoster() {
1069 return mUi.getDefaultVideoPoster();
1070 }
1071
1072 @Override
1073 public View getVideoLoadingProgressView() {
1074 return mUi.getVideoLoadingProgressView();
1075 }
1076
1077 @Override
1078 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1079 SslError error) {
1080 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1081 }
1082
Patrick Scott92066772011-03-10 08:46:27 -05001083 @Override
1084 public void showAutoLogin(Tab tab) {
1085 assert tab.inForeground();
1086 // Update the title bar to show the auto-login request.
1087 mUi.showAutoLogin(tab);
1088 }
1089
1090 @Override
1091 public void hideAutoLogin(Tab tab) {
1092 assert tab.inForeground();
1093 mUi.hideAutoLogin(tab);
1094 }
1095
Michael Kolb8233fac2010-10-26 16:08:53 -07001096 // helper method
1097
1098 /*
1099 * Update the favorites icon if the private browsing isn't enabled and the
1100 * icon is valid.
1101 */
1102 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1103 final String url, Bitmap favicon) {
1104 if (favicon == null) {
1105 return;
1106 }
1107 if (!tab.isPrivateBrowsingEnabled()) {
1108 Bookmarks.updateFavicon(mActivity
1109 .getContentResolver(), originalUrl, url, favicon);
1110 }
1111 }
1112
Leon Scroggins4cd97792010-12-03 15:31:56 -05001113 @Override
1114 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001115 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001116 mUi.bookmarkedStatusHasChanged(tab);
1117 }
1118
Michael Kolb8233fac2010-10-26 16:08:53 -07001119 // end WebViewController
1120
1121 protected void pageUp() {
1122 getCurrentTopWebView().pageUp(false);
1123 }
1124
1125 protected void pageDown() {
1126 getCurrentTopWebView().pageDown(false);
1127 }
1128
1129 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001130 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001131 public void editUrl() {
1132 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001133 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001134 }
1135
John Reck9c35b9c2012-05-30 10:08:50 -07001136 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001137 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001138 WebChromeClient.CustomViewCallback callback) {
1139 if (tab.inForeground()) {
1140 if (mUi.isCustomViewShowing()) {
1141 callback.onCustomViewHidden();
1142 return;
1143 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001144 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001145 // Save the menu state and set it to empty while the custom
1146 // view is showing.
1147 mOldMenuState = mMenuState;
1148 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001149 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001150 }
1151 }
1152
1153 @Override
1154 public void hideCustomView() {
1155 if (mUi.isCustomViewShowing()) {
1156 mUi.onHideCustomView();
1157 // Reset the old menu state.
1158 mMenuState = mOldMenuState;
1159 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001160 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 }
1162 }
1163
John Reck9c35b9c2012-05-30 10:08:50 -07001164 @Override
1165 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 Intent intent) {
1167 if (getCurrentTopWebView() == null) return;
1168 switch (requestCode) {
1169 case PREFERENCES_PAGE:
1170 if (resultCode == Activity.RESULT_OK && intent != null) {
1171 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001172 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001173 mTabControl.removeParentChildRelationShips();
1174 }
1175 }
1176 break;
1177 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001178 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001179 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001180 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001181 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001182 case AUTOFILL_SETUP:
1183 // Determine whether a profile was actually set up or not
1184 // and if so, send the message back to the WebTextView to
1185 // fill the form with the new profile.
1186 if (getSettings().getAutoFillProfile() != null) {
1187 mAutoFillSetupMessage.sendToTarget();
1188 mAutoFillSetupMessage = null;
1189 }
1190 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001191 case COMBO_VIEW:
1192 if (intent == null || resultCode != Activity.RESULT_OK) {
1193 break;
1194 }
John Reck3ba45532011-08-11 16:26:53 -07001195 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001196 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1197 Tab t = getCurrentTab();
1198 Uri uri = intent.getData();
1199 loadUrl(t, uri.toString());
1200 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1201 String[] urls = intent.getStringArrayExtra(
1202 ComboViewActivity.EXTRA_OPEN_ALL);
1203 Tab parent = getCurrentTab();
1204 for (String url : urls) {
1205 parent = openTab(url, parent,
1206 !mSettings.openInBackground(), true);
1207 }
1208 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1209 long id = intent.getLongExtra(
1210 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1211 if (id >= 0) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08001212 if (BrowserWebView.isClassic()) {
1213 createNewSnapshotTab(id, true);
1214 } else {
1215 Toast.makeText(mActivity, "Snapshot Tab requires WebViewClassic",
1216 Toast.LENGTH_LONG).show();
1217 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001218 }
1219 }
1220 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001221 case VOICE_RESULT:
1222 if (resultCode == Activity.RESULT_OK && intent != null) {
1223 ArrayList<String> results = intent.getStringArrayListExtra(
1224 RecognizerIntent.EXTRA_RESULTS);
1225 if (results.size() >= 1) {
1226 mVoiceResult = results.get(0);
1227 }
1228 }
1229 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001230 default:
1231 break;
1232 }
1233 getCurrentTopWebView().requestFocus();
1234 }
1235
1236 /**
1237 * Open the Go page.
1238 * @param startWithHistory If true, open starting on the history tab.
1239 * Otherwise, start with the bookmarks tab.
1240 */
1241 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001242 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001243 if (mTabControl.getCurrentWebView() == null) {
1244 return;
1245 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001246 // clear action mode
1247 if (isInCustomActionMode()) {
1248 endActionMode();
1249 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001250 Bundle extras = new Bundle();
1251 // Disable opening in a new window if we have maxed out the windows
1252 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1253 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001254 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001255 }
1256
1257 // combo view callbacks
1258
Michael Kolb8233fac2010-10-26 16:08:53 -07001259 // key handling
1260 protected void onBackKey() {
1261 if (!mUi.onBackKey()) {
1262 WebView subwindow = mTabControl.getCurrentSubWindow();
1263 if (subwindow != null) {
1264 if (subwindow.canGoBack()) {
1265 subwindow.goBack();
1266 } else {
1267 dismissSubWindow(mTabControl.getCurrentTab());
1268 }
1269 } else {
1270 goBackOnePageOrQuit();
1271 }
1272 }
1273 }
1274
Michael Kolb4bd767d2011-05-27 11:33:55 -07001275 protected boolean onMenuKey() {
1276 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001277 }
1278
Michael Kolb8233fac2010-10-26 16:08:53 -07001279 // menu handling and state
1280 // TODO: maybe put into separate handler
1281
John Reck9c35b9c2012-05-30 10:08:50 -07001282 @Override
1283 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001284 if (mMenuState == EMPTY_MENU) {
1285 return false;
1286 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001287 MenuInflater inflater = mActivity.getMenuInflater();
1288 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001289 return true;
1290 }
1291
John Reck9c35b9c2012-05-30 10:08:50 -07001292 @Override
1293 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001294 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001295 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001296 return;
1297 }
1298 if (!(v instanceof WebView)) {
1299 return;
1300 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001301 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001302 WebView.HitTestResult result = webview.getHitTestResult();
1303 if (result == null) {
1304 return;
1305 }
1306
1307 int type = result.getType();
1308 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1309 Log.w(LOGTAG,
1310 "We should not show context menu when nothing is touched");
1311 return;
1312 }
1313 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1314 // let TextView handles context menu
1315 return;
1316 }
1317
1318 // Note, http://b/issue?id=1106666 is requesting that
1319 // an inflated menu can be used again. This is not available
1320 // yet, so inflate each time (yuk!)
1321 MenuInflater inflater = mActivity.getMenuInflater();
1322 inflater.inflate(R.menu.browsercontext, menu);
1323
1324 // Show the correct menu group
1325 final String extra = result.getExtra();
Michael Kolbc159c1a2011-12-15 16:06:38 -08001326 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001327 menu.setGroupVisible(R.id.PHONE_MENU,
1328 type == WebView.HitTestResult.PHONE_TYPE);
1329 menu.setGroupVisible(R.id.EMAIL_MENU,
1330 type == WebView.HitTestResult.EMAIL_TYPE);
1331 menu.setGroupVisible(R.id.GEO_MENU,
1332 type == WebView.HitTestResult.GEO_TYPE);
1333 menu.setGroupVisible(R.id.IMAGE_MENU,
1334 type == WebView.HitTestResult.IMAGE_TYPE
1335 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1336 menu.setGroupVisible(R.id.ANCHOR_MENU,
1337 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1338 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001339 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1340 || type == WebView.HitTestResult.PHONE_TYPE
1341 || type == WebView.HitTestResult.EMAIL_TYPE
1342 || type == WebView.HitTestResult.GEO_TYPE;
1343 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1344 if (hitText) {
1345 menu.findItem(R.id.select_text_menu_id)
1346 .setOnMenuItemClickListener(new SelectText(webview));
1347 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001348 // Setup custom handling depending on the type
1349 switch (type) {
1350 case WebView.HitTestResult.PHONE_TYPE:
1351 menu.setHeaderTitle(Uri.decode(extra));
1352 menu.findItem(R.id.dial_context_menu_id).setIntent(
1353 new Intent(Intent.ACTION_VIEW, Uri
1354 .parse(WebView.SCHEME_TEL + extra)));
1355 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1356 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1357 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1358 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1359 addIntent);
1360 menu.findItem(R.id.copy_phone_context_menu_id)
1361 .setOnMenuItemClickListener(
1362 new Copy(extra));
1363 break;
1364
1365 case WebView.HitTestResult.EMAIL_TYPE:
1366 menu.setHeaderTitle(extra);
1367 menu.findItem(R.id.email_context_menu_id).setIntent(
1368 new Intent(Intent.ACTION_VIEW, Uri
1369 .parse(WebView.SCHEME_MAILTO + extra)));
1370 menu.findItem(R.id.copy_mail_context_menu_id)
1371 .setOnMenuItemClickListener(
1372 new Copy(extra));
1373 break;
1374
1375 case WebView.HitTestResult.GEO_TYPE:
1376 menu.setHeaderTitle(extra);
1377 menu.findItem(R.id.map_context_menu_id).setIntent(
1378 new Intent(Intent.ACTION_VIEW, Uri
1379 .parse(WebView.SCHEME_GEO
1380 + URLEncoder.encode(extra))));
1381 menu.findItem(R.id.copy_geo_context_menu_id)
1382 .setOnMenuItemClickListener(
1383 new Copy(extra));
1384 break;
1385
1386 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1387 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001388 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001389 // decide whether to show the open link in new tab option
1390 boolean showNewTab = mTabControl.canCreateNewTab();
1391 MenuItem newTabItem
1392 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001393 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001394 ? R.string.contextmenu_openlink_newwindow_background
1395 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 newTabItem.setVisible(showNewTab);
1397 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001398 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1399 newTabItem.setOnMenuItemClickListener(
1400 new MenuItem.OnMenuItemClickListener() {
1401 @Override
1402 public boolean onMenuItemClick(MenuItem item) {
1403 final HashMap<String, WebView> hrefMap =
1404 new HashMap<String, WebView>();
1405 hrefMap.put("webview", webview);
1406 final Message msg = mHandler.obtainMessage(
1407 FOCUS_NODE_HREF,
1408 R.id.open_newtab_context_menu_id,
1409 0, hrefMap);
1410 webview.requestFocusNodeHref(msg);
1411 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001412 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001413 });
1414 } else {
1415 newTabItem.setOnMenuItemClickListener(
1416 new MenuItem.OnMenuItemClickListener() {
1417 @Override
1418 public boolean onMenuItemClick(MenuItem item) {
1419 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001420 openTab(extra, parent,
1421 !mSettings.openInBackground(),
1422 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001423 return true;
1424 }
1425 });
1426 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001427 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001428 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1429 break;
1430 }
1431 // otherwise fall through to handle image part
1432 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001433 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1434 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001435 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1436 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001437 shareItem.setOnMenuItemClickListener(
1438 new MenuItem.OnMenuItemClickListener() {
1439 @Override
1440 public boolean onMenuItemClick(MenuItem item) {
1441 sharePage(mActivity, null, extra, null,
1442 null);
1443 return true;
1444 }
1445 }
1446 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001447 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001448 menu.findItem(R.id.view_image_context_menu_id)
1449 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1450 @Override
1451 public boolean onMenuItemClick(MenuItem item) {
1452 openTab(extra, mTabControl.getCurrentTab(), true, true);
1453 return false;
1454 }
1455 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001456 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1457 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1458 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001459 menu.findItem(R.id.set_wallpaper_context_menu_id).
1460 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1461 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001462 break;
1463
1464 default:
1465 Log.w(LOGTAG, "We should not get here.");
1466 break;
1467 }
1468 //update the ui
1469 mUi.onContextMenuCreated(menu);
1470 }
1471
1472 /**
1473 * As the menu can be open when loading state changes
1474 * we must manually update the state of the stop/reload menu
1475 * item
1476 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001477 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001478 if (menu == null) {
1479 return;
1480 }
1481 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001482 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001483 menu.findItem(R.id.stop_menu_id):
1484 menu.findItem(R.id.reload_menu_id);
1485 if (src != null) {
1486 dest.setIcon(src.getIcon());
1487 dest.setTitle(src.getTitle());
1488 }
1489 }
1490
John Reck9c35b9c2012-05-30 10:08:50 -07001491 @Override
1492 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001493 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001494 // hold on to the menu reference here; it is used by the page callbacks
1495 // to update the menu based on loading state
1496 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001497 // Note: setVisible will decide whether an item is visible; while
1498 // setEnabled() will decide whether an item is enabled, which also means
1499 // whether the matching shortcut key will function.
1500 switch (mMenuState) {
1501 case EMPTY_MENU:
1502 if (mCurrentMenuState != mMenuState) {
1503 menu.setGroupVisible(R.id.MAIN_MENU, false);
1504 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1505 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1506 }
1507 break;
1508 default:
1509 if (mCurrentMenuState != mMenuState) {
1510 menu.setGroupVisible(R.id.MAIN_MENU, true);
1511 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1512 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1513 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001514 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001515 break;
1516 }
1517 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001518 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001519 }
1520
Michael Kolb4bf79712011-07-14 14:18:12 -07001521 @Override
1522 public void updateMenuState(Tab tab, Menu menu) {
1523 boolean canGoBack = false;
1524 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001525 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001526 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001527 if (tab != null) {
1528 canGoBack = tab.canGoBack();
1529 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001530 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001531 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001532 }
1533 final MenuItem back = menu.findItem(R.id.back_menu_id);
1534 back.setEnabled(canGoBack);
1535
1536 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001537
1538 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1539 forward.setEnabled(canGoForward);
1540
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001541 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1542 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001543 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001544 if (source != null && dest != null) {
1545 dest.setTitle(source.getTitle());
1546 dest.setIcon(source.getIcon());
1547 }
John Recke1a03a32011-09-14 17:04:16 -07001548 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001549
1550 // decide whether to show the share link option
1551 PackageManager pm = mActivity.getPackageManager();
1552 Intent send = new Intent(Intent.ACTION_SEND);
1553 send.setType("text/plain");
1554 ResolveInfo ri = pm.resolveActivity(send,
1555 PackageManager.MATCH_DEFAULT_ONLY);
1556 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1557
1558 boolean isNavDump = mSettings.enableNavDump();
1559 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1560 nav.setVisible(isNavDump);
1561 nav.setEnabled(isNavDump);
1562
1563 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001564 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1565 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001566 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1567 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001568
1569 // history and snapshots item are the members of COMBO menu group,
1570 // so if show history item, only make snapshots item invisible.
1571 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001572
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001573 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001574 }
1575
John Reck9c35b9c2012-05-30 10:08:50 -07001576 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001577 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001578 if (null == getCurrentTopWebView()) {
1579 return false;
1580 }
1581 if (mMenuIsDown) {
1582 // The shortcut action consumes the MENU. Even if it is still down,
1583 // it won't trigger the next shortcut action. In the case of the
1584 // shortcut action triggering a new activity, like Bookmarks, we
1585 // won't get onKeyUp for MENU. So it is important to reset it here.
1586 mMenuIsDown = false;
1587 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001588 if (mUi.onOptionsItemSelected(item)) {
1589 // ui callback handled it
1590 return true;
1591 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001592 switch (item.getItemId()) {
1593 // -- Main menu
1594 case R.id.new_tab_menu_id:
1595 openTabToHomePage();
1596 break;
1597
1598 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001599 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001600 break;
1601
Afzal Najamd4e33312012-04-26 01:54:01 -04001602 case R.id.close_other_tabs_id:
1603 closeOtherTabs();
1604 break;
1605
Michael Kolb8233fac2010-10-26 16:08:53 -07001606 case R.id.goto_menu_id:
1607 editUrl();
1608 break;
1609
1610 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001611 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1612 break;
1613
1614 case R.id.history_menu_id:
1615 bookmarksOrHistoryPicker(ComboViews.History);
1616 break;
1617
1618 case R.id.snapshots_menu_id:
1619 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001620 break;
1621
Michael Kolb8233fac2010-10-26 16:08:53 -07001622 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001623 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001624 break;
1625
1626 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001627 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001628 stopLoading();
1629 } else {
1630 getCurrentTopWebView().reload();
1631 }
1632 break;
1633
1634 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001635 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001636 break;
1637
1638 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001639 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001640 break;
1641
1642 case R.id.close_menu_id:
1643 // Close the subwindow if it exists.
1644 if (mTabControl.getCurrentSubWindow() != null) {
1645 dismissSubWindow(mTabControl.getCurrentTab());
1646 break;
1647 }
1648 closeCurrentTab();
1649 break;
1650
kaiyiza8b6dbb2013-07-29 18:11:22 +08001651 case R.id.exit_menu_id:
1652 String ret = SystemProperties.get("persist.debug.browsermonkeytest");
1653 if (ret != null && ret.equals("enable"))
1654 break;
1655 onPause();
1656 return false;
1657
Michael Kolb8233fac2010-10-26 16:08:53 -07001658 case R.id.homepage_menu_id:
1659 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001660 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001661 break;
1662
1663 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001664 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001665 break;
1666
1667 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001668 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001669 break;
1670
John Reck2bc80422011-06-30 15:11:49 -07001671 case R.id.save_snapshot_menu_id:
1672 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001673 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001674 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001675 break;
1676
Michael Kolb8233fac2010-10-26 16:08:53 -07001677 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001678 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001679 break;
1680
John Recke1a03a32011-09-14 17:04:16 -07001681 case R.id.snapshot_go_live:
1682 goLive();
1683 return true;
1684
Michael Kolb8233fac2010-10-26 16:08:53 -07001685 case R.id.share_page_menu_id:
1686 Tab currentTab = mTabControl.getCurrentTab();
1687 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001688 return false;
1689 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001690 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001691 break;
1692
1693 case R.id.dump_nav_menu_id:
1694 getCurrentTopWebView().debugDump();
1695 break;
1696
Michael Kolb8233fac2010-10-26 16:08:53 -07001697 case R.id.zoom_in_menu_id:
1698 getCurrentTopWebView().zoomIn();
1699 break;
1700
1701 case R.id.zoom_out_menu_id:
1702 getCurrentTopWebView().zoomOut();
1703 break;
1704
1705 case R.id.view_downloads_menu_id:
1706 viewDownloads();
1707 break;
1708
John Reck42229bc2011-08-19 13:26:43 -07001709 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001710 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07001711 break;
1712
Michael Kolb8233fac2010-10-26 16:08:53 -07001713 case R.id.window_one_menu_id:
1714 case R.id.window_two_menu_id:
1715 case R.id.window_three_menu_id:
1716 case R.id.window_four_menu_id:
1717 case R.id.window_five_menu_id:
1718 case R.id.window_six_menu_id:
1719 case R.id.window_seven_menu_id:
1720 case R.id.window_eight_menu_id:
1721 {
1722 int menuid = item.getItemId();
1723 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1724 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1725 Tab desiredTab = mTabControl.getTab(id);
1726 if (desiredTab != null &&
1727 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001728 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001729 }
1730 break;
1731 }
1732 }
1733 }
1734 break;
1735
1736 default:
1737 return false;
1738 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001739 return true;
1740 }
1741
John Reck68234a92012-04-19 15:27:12 -07001742 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
1743 implements OnCancelListener {
1744
1745 private Tab mTab;
1746 private Dialog mProgressDialog;
1747 private ContentValues mValues;
1748
1749 private SaveSnapshotTask(Tab tab) {
1750 mTab = tab;
1751 }
1752
1753 @Override
1754 protected void onPreExecute() {
1755 CharSequence message = mActivity.getText(R.string.saving_snapshot);
1756 mProgressDialog = ProgressDialog.show(mActivity, null, message,
1757 true, true, this);
1758 mValues = mTab.createSnapshotValues();
1759 }
1760
1761 @Override
1762 protected Long doInBackground(Void... params) {
1763 if (!mTab.saveViewState(mValues)) {
1764 return null;
1765 }
1766 if (isCancelled()) {
1767 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
1768 File file = mActivity.getFileStreamPath(path);
1769 if (!file.delete()) {
1770 file.deleteOnExit();
1771 }
1772 return null;
1773 }
1774 final ContentResolver cr = mActivity.getContentResolver();
1775 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
1776 if (result == null) {
1777 return null;
1778 }
1779 long id = ContentUris.parseId(result);
1780 return id;
1781 }
1782
1783 @Override
1784 protected void onPostExecute(Long id) {
1785 if (isCancelled()) {
1786 return;
1787 }
1788 mProgressDialog.dismiss();
1789 if (id == null) {
1790 Toast.makeText(mActivity, R.string.snapshot_failed,
1791 Toast.LENGTH_SHORT).show();
1792 return;
1793 }
1794 Bundle b = new Bundle();
1795 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1796 mUi.showComboView(ComboViews.Snapshots, b);
1797 }
1798
1799 @Override
1800 public void onCancel(DialogInterface dialog) {
1801 cancel(true);
1802 }
1803 }
1804
Michael Kolb80f75082012-04-10 10:50:06 -07001805 @Override
1806 public void toggleUserAgent() {
1807 WebView web = getCurrentWebView();
1808 mSettings.toggleDesktopUseragent(web);
1809 web.loadUrl(web.getOriginalUrl());
1810 }
1811
1812 @Override
1813 public void findOnPage() {
1814 getCurrentTopWebView().showFindDialog(null, true);
1815 }
1816
1817 @Override
1818 public void openPreferences() {
1819 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1820 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1821 getCurrentTopWebView().getUrl());
1822 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1823 }
1824
1825 @Override
1826 public void bookmarkCurrentPage() {
1827 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
1828 if (bookmarkIntent != null) {
1829 mActivity.startActivity(bookmarkIntent);
1830 }
1831 }
1832
John Recke1a03a32011-09-14 17:04:16 -07001833 private void goLive() {
1834 Tab t = getCurrentTab();
1835 t.loadUrl(t.getUrl(), null);
1836 }
1837
Michael Kolb315d5022011-10-13 12:47:11 -07001838 @Override
1839 public void showPageInfo() {
1840 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
1841 }
1842
John Reck9c35b9c2012-05-30 10:08:50 -07001843 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001844 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001845 // Let the History and Bookmark fragments handle menus they created.
1846 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1847 return false;
1848 }
1849
Michael Kolb8233fac2010-10-26 16:08:53 -07001850 int id = item.getItemId();
1851 boolean result = true;
1852 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001853 // -- Browser context menu
1854 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001855 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001856 case R.id.copy_link_context_menu_id:
1857 final WebView webView = getCurrentTopWebView();
1858 if (null == webView) {
1859 result = false;
1860 break;
1861 }
1862 final HashMap<String, WebView> hrefMap =
1863 new HashMap<String, WebView>();
1864 hrefMap.put("webview", webView);
1865 final Message msg = mHandler.obtainMessage(
1866 FOCUS_NODE_HREF, id, 0, hrefMap);
1867 webView.requestFocusNodeHref(msg);
1868 break;
1869
1870 default:
1871 // For other context menus
1872 result = onOptionsItemSelected(item);
1873 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001874 return result;
1875 }
1876
1877 /**
1878 * support programmatically opening the context menu
1879 */
1880 public void openContextMenu(View view) {
1881 mActivity.openContextMenu(view);
1882 }
1883
1884 /**
1885 * programmatically open the options menu
1886 */
1887 public void openOptionsMenu() {
1888 mActivity.openOptionsMenu();
1889 }
1890
John Reck9c35b9c2012-05-30 10:08:50 -07001891 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001892 public boolean onMenuOpened(int featureId, Menu menu) {
1893 if (mOptionsMenuOpen) {
1894 if (mConfigChanged) {
1895 // We do not need to make any changes to the state of the
1896 // title bar, since the only thing that happened was a
1897 // change in orientation
1898 mConfigChanged = false;
1899 } else {
1900 if (!mExtendedMenuOpen) {
1901 mExtendedMenuOpen = true;
1902 mUi.onExtendedMenuOpened();
1903 } else {
1904 // Switching the menu back to icon view, so show the
1905 // title bar once again.
1906 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001907 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001908 }
1909 }
1910 } else {
1911 // The options menu is closed, so open it, and show the title
1912 mOptionsMenuOpen = true;
1913 mConfigChanged = false;
1914 mExtendedMenuOpen = false;
1915 mUi.onOptionsMenuOpened();
1916 }
1917 return true;
1918 }
1919
John Reck9c35b9c2012-05-30 10:08:50 -07001920 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001921 public void onOptionsMenuClosed(Menu menu) {
1922 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001923 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001924 }
1925
John Reck9c35b9c2012-05-30 10:08:50 -07001926 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001927 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001928 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001929 }
1930
1931 // Helper method for getting the top window.
1932 @Override
1933 public WebView getCurrentTopWebView() {
1934 return mTabControl.getCurrentTopWebView();
1935 }
1936
1937 @Override
1938 public WebView getCurrentWebView() {
1939 return mTabControl.getCurrentWebView();
1940 }
1941
1942 /*
1943 * This method is called as a result of the user selecting the options
1944 * menu to see the download window. It shows the download window on top of
1945 * the current window.
1946 */
1947 void viewDownloads() {
1948 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1949 mActivity.startActivity(intent);
1950 }
1951
John Reck30b065e2011-07-19 10:58:05 -07001952 int getActionModeHeight() {
1953 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1954 new int[] { android.R.attr.actionBarSize });
1955 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1956 actionBarSizeTypedArray.recycle();
1957 return size;
1958 }
1959
Michael Kolb8233fac2010-10-26 16:08:53 -07001960 // action mode
1961
John Reck9c35b9c2012-05-30 10:08:50 -07001962 @Override
1963 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001964 mUi.onActionModeStarted(mode);
1965 mActionMode = mode;
1966 }
1967
1968 /*
1969 * True if a custom ActionMode (i.e. find or select) is in use.
1970 */
1971 @Override
1972 public boolean isInCustomActionMode() {
1973 return mActionMode != null;
1974 }
1975
1976 /*
1977 * End the current ActionMode.
1978 */
1979 @Override
1980 public void endActionMode() {
1981 if (mActionMode != null) {
1982 mActionMode.finish();
1983 }
1984 }
1985
1986 /*
1987 * Called by find and select when they are finished. Replace title bars
1988 * as necessary.
1989 */
John Reck9c35b9c2012-05-30 10:08:50 -07001990 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001991 public void onActionModeFinished(ActionMode mode) {
1992 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001993 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001994 mActionMode = null;
1995 }
1996
1997 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08001998 final Tab tab = getCurrentTab();
1999 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002000 }
2001
2002 // bookmark handling
2003
2004 /**
2005 * add the current page as a bookmark to the given folder id
2006 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002007 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002008 * bookmarked, and if it is, edit that bookmark. If false, and
2009 * the site is already bookmarked, do not attempt to edit the
2010 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002011 */
2012 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002013 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002014 WebView w = getCurrentTopWebView();
2015 if (w == null) {
2016 return null;
2017 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002018 Intent i = new Intent(mActivity,
2019 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002020 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2021 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2022 String touchIconUrl = w.getTouchIconUrl();
2023 if (touchIconUrl != null) {
2024 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2025 WebSettings settings = w.getSettings();
2026 if (settings != null) {
2027 i.putExtra(AddBookmarkPage.USER_AGENT,
2028 settings.getUserAgentString());
2029 }
2030 }
2031 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2032 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2033 getDesiredThumbnailHeight(mActivity)));
2034 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002035 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002036 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2037 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002038 // Put the dialog at the upper right of the screen, covering the
2039 // star on the title bar.
2040 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002041 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002042 }
2043
2044 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002045 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002046 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002047 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002048 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002049 }
2050
2051 // thumbnails
2052
2053 /**
2054 * Return the desired width for thumbnail screenshots, which are stored in
2055 * the database, and used on the bookmarks screen.
2056 * @param context Context for finding out the density of the screen.
2057 * @return desired width for thumbnail screenshot.
2058 */
2059 static int getDesiredThumbnailWidth(Context context) {
2060 return context.getResources().getDimensionPixelOffset(
2061 R.dimen.bookmarkThumbnailWidth);
2062 }
2063
2064 /**
2065 * Return the desired height for thumbnail screenshots, which are stored in
2066 * the database, and used on the bookmarks screen.
2067 * @param context Context for finding out the density of the screen.
2068 * @return desired height for thumbnail screenshot.
2069 */
2070 static int getDesiredThumbnailHeight(Context context) {
2071 return context.getResources().getDimensionPixelOffset(
2072 R.dimen.bookmarkThumbnailHeight);
2073 }
2074
John Reck8cc92352011-07-06 17:41:52 -07002075 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002076 if (view == null || view.getContentHeight() == 0
2077 || view.getContentWidth() == 0) {
2078 return null;
2079 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002080 // We render to a bitmap 2x the desired size so that we can then
2081 // re-scale it with filtering since canvas.scale doesn't filter
2082 // This helps reduce aliasing at the cost of being slightly blurry
2083 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002084 int scaledWidth = width * filter_scale;
2085 int scaledHeight = height * filter_scale;
2086 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2087 || sThumbnailBitmap.getHeight() != scaledHeight) {
2088 if (sThumbnailBitmap != null) {
2089 sThumbnailBitmap.recycle();
2090 sThumbnailBitmap = null;
2091 }
2092 sThumbnailBitmap =
2093 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002094 }
John Reckd7dd9b22011-08-30 09:18:29 -07002095 Canvas canvas = new Canvas(sThumbnailBitmap);
2096 int contentWidth = view.getContentWidth();
2097 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2098 if (view instanceof BrowserWebView) {
2099 int dy = -((BrowserWebView)view).getTitleHeight();
2100 canvas.translate(0, dy * overviewScale);
2101 }
2102
2103 canvas.scale(overviewScale, overviewScale);
2104
2105 if (view instanceof BrowserWebView) {
2106 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002107 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002108 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002109 }
John Reckd7dd9b22011-08-30 09:18:29 -07002110 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2111 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002112 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002113 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002114 }
2115
John Reck34ef2672011-02-10 11:30:55 -08002116 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002117 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002118 // FIXME: Would like to make sure there is actually something to
2119 // draw, but the API for that (WebViewCore.pictureReady()) is not
2120 // currently accessible here.
2121
John Reck34ef2672011-02-10 11:30:55 -08002122 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002123 if (view == null) {
2124 // Tab was destroyed
2125 return;
2126 }
John Reck34ef2672011-02-10 11:30:55 -08002127 final String url = tab.getUrl();
2128 final String originalUrl = view.getOriginalUrl();
John Reck34ef2672011-02-10 11:30:55 -08002129 if (TextUtils.isEmpty(url)) {
2130 return;
2131 }
2132
2133 // Only update thumbnails for web urls (http(s)://), not for
2134 // about:, javascript:, data:, etc...
2135 // Unless it is a bookmarked site, then always update
2136 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2137 return;
2138 }
2139
Michael Kolb8233fac2010-10-26 16:08:53 -07002140 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2141 getDesiredThumbnailHeight(mActivity));
2142 if (bm == null) {
2143 return;
2144 }
2145
2146 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002147 new AsyncTask<Void, Void, Void>() {
2148 @Override
2149 protected Void doInBackground(Void... unused) {
2150 Cursor cursor = null;
2151 try {
2152 // TODO: Clean this up
2153 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2154 if (cursor != null && cursor.moveToFirst()) {
2155 final ByteArrayOutputStream os =
2156 new ByteArrayOutputStream();
2157 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002158
John Reck34ef2672011-02-10 11:30:55 -08002159 ContentValues values = new ContentValues();
2160 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002161
John Reck34ef2672011-02-10 11:30:55 -08002162 do {
John Reck617fd832011-02-16 14:35:59 -08002163 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002164 cr.update(Images.CONTENT_URI, values, null, null);
2165 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002166 }
John Reck34ef2672011-02-10 11:30:55 -08002167 } catch (IllegalStateException e) {
2168 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002169 } catch (SQLiteException s) {
2170 // Added for possible error when user tries to remove the same bookmark
2171 // that is being updated with a screen shot
2172 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002173 } finally {
2174 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002175 }
John Reck34ef2672011-02-10 11:30:55 -08002176 return null;
2177 }
2178 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002179 }
2180
2181 private class Copy implements OnMenuItemClickListener {
2182 private CharSequence mText;
2183
John Reck9c35b9c2012-05-30 10:08:50 -07002184 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002185 public boolean onMenuItemClick(MenuItem item) {
2186 copy(mText);
2187 return true;
2188 }
2189
2190 public Copy(CharSequence toCopy) {
2191 mText = toCopy;
2192 }
2193 }
2194
Leon Scroggins63c02662010-11-18 15:16:27 -05002195 private static class Download implements OnMenuItemClickListener {
2196 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002197 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002198 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002199 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002200 private static final String FALLBACK_EXTENSION = "dat";
2201 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002202
John Reck9c35b9c2012-05-30 10:08:50 -07002203 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002204 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002205 if (DataUri.isDataUri(mText)) {
2206 saveDataUri();
2207 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002208 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07002209 null, null, null, mPrivateBrowsing);
George Mount387d45d2011-10-07 15:57:53 -07002210 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002211 return true;
2212 }
2213
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002214 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2215 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002216 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002217 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002218 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002219 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002220 }
George Mount387d45d2011-10-07 15:57:53 -07002221
2222 /**
2223 * Treats mText as a data URI and writes its contents to a file
2224 * based on the current time.
2225 */
2226 private void saveDataUri() {
2227 FileOutputStream outputStream = null;
2228 try {
2229 DataUri uri = new DataUri(mText);
2230 File target = getTarget(uri);
2231 outputStream = new FileOutputStream(target);
2232 outputStream.write(uri.getData());
2233 final DownloadManager manager =
2234 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2235 manager.addCompletedDownload(target.getName(),
2236 mActivity.getTitle().toString(), false,
2237 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002238 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002239 } catch (IOException e) {
2240 Log.e(LOGTAG, "Could not save data URL");
2241 } finally {
2242 if (outputStream != null) {
2243 try {
2244 outputStream.close();
2245 } catch (IOException e) {
2246 // ignore close errors
2247 }
2248 }
2249 }
2250 }
2251
2252 /**
2253 * Creates a File based on the current time stamp and uses
2254 * the mime type of the DataUri to get the extension.
2255 */
2256 private File getTarget(DataUri uri) throws IOException {
2257 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002258 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002259 String nameBase = format.format(new Date());
2260 String mimeType = uri.getMimeType();
2261 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2262 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2263 if (extension == null) {
2264 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2265 extension = FALLBACK_EXTENSION;
2266 }
2267 extension = "." + extension; // createTempFile needs the '.'
2268 File targetFile = File.createTempFile(nameBase, extension, dir);
2269 return targetFile;
2270 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002271 }
2272
Cary Clark8974d282010-11-22 10:46:05 -05002273 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002274 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002275
John Reck9c35b9c2012-05-30 10:08:50 -07002276 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002277 public boolean onMenuItemClick(MenuItem item) {
2278 if (mWebView != null) {
2279 return mWebView.selectText();
2280 }
2281 return false;
2282 }
2283
2284 public SelectText(WebView webView) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002285 if (BrowserWebView.isClassic()) {
2286 mWebView = WebViewClassic.fromWebView(webView);
2287 }
Cary Clark8974d282010-11-22 10:46:05 -05002288 }
2289
2290 }
2291
Michael Kolb8233fac2010-10-26 16:08:53 -07002292 /********************** TODO: UI stuff *****************************/
2293
2294 // these methods have been copied, they still need to be cleaned up
2295
2296 /****************** tabs ***************************************************/
2297
2298 // basic tab interactions:
2299
2300 // it is assumed that tabcontrol already knows about the tab
2301 protected void addTab(Tab tab) {
2302 mUi.addTab(tab);
2303 }
2304
2305 protected void removeTab(Tab tab) {
2306 mUi.removeTab(tab);
2307 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002308 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002309 }
2310
Michael Kolbf2055602011-04-09 17:20:03 -07002311 @Override
2312 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002313 // monkey protection against delayed start
2314 if (tab != null) {
2315 mTabControl.setCurrentTab(tab);
2316 // the tab is guaranteed to have a webview after setCurrentTab
2317 mUi.setActiveTab(tab);
2318 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002319 }
2320
John Reck8bcafc12011-08-29 16:43:02 -07002321 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002322 Tab current = mTabControl.getCurrentTab();
2323 if (current != null
2324 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002325 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002326 }
2327 }
2328
John Reck26b18322011-06-21 13:08:58 -07002329 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002330 // Dismiss the subwindow if applicable.
2331 dismissSubWindow(appTab);
2332 // Since we might kill the WebView, remove it from the
2333 // content view first.
2334 mUi.detachTab(appTab);
2335 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002336 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002337 // TODO: analyze why the remove and add are necessary
2338 mUi.attachTab(appTab);
2339 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002340 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002341 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002342 } else {
2343 // If the tab was the current tab, we have to attach
2344 // it to the view system again.
2345 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002346 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002347 }
2348 }
2349
2350 // Remove the sub window if it exists. Also called by TabControl when the
2351 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002352 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002353 public void dismissSubWindow(Tab tab) {
2354 removeSubWindow(tab);
2355 // dismiss the subwindow. This will destroy the WebView.
2356 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002357 WebView wv = getCurrentTopWebView();
2358 if (wv != null) {
2359 wv.requestFocus();
2360 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002361 }
2362
2363 @Override
2364 public void removeSubWindow(Tab t) {
2365 if (t.getSubWebView() != null) {
2366 mUi.removeSubWindow(t.getSubViewContainer());
2367 }
2368 }
2369
2370 @Override
2371 public void attachSubWindow(Tab tab) {
2372 if (tab.getSubWebView() != null) {
2373 mUi.attachSubWindow(tab.getSubViewContainer());
2374 getCurrentTopWebView().requestFocus();
2375 }
2376 }
2377
Mathew Inwood29721c22011-06-29 17:55:24 +01002378 private Tab showPreloadedTab(final UrlData urlData) {
2379 if (!urlData.isPreloaded()) {
2380 return null;
2381 }
2382 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2383 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2384 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002385 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002386 // Could not submit query. Fallback to regular tab creation
2387 tabControl.destroy();
2388 return null;
2389 }
2390 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002391 // check tab count and make room for new tab
2392 if (!mTabControl.canCreateNewTab()) {
2393 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2394 if (leastUsed != null) {
2395 closeTab(leastUsed);
2396 }
2397 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002398 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002399 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002400 mTabControl.addPreloadedTab(t);
2401 addTab(t);
2402 setActiveTab(t);
2403 return t;
2404 }
2405
Michael Kolb7bcafde2011-05-09 13:55:59 -07002406 // open a non inconito tab with the given url data
2407 // and set as active tab
2408 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002409 Tab tab = showPreloadedTab(urlData);
2410 if (tab == null) {
2411 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002412 if ((tab != null) && !urlData.isEmpty()) {
2413 loadUrlDataIn(tab, urlData);
2414 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002415 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002416 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002417 }
2418
Michael Kolb843510f2010-12-09 10:51:49 -08002419 @Override
2420 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002421 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002422 }
2423
Michael Kolb8233fac2010-10-26 16:08:53 -07002424 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002425 public Tab openIncognitoTab() {
2426 return openTab(INCOGNITO_URI, true, true, false);
2427 }
2428
2429 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002430 public Tab openTab(String url, boolean incognito, boolean setActive,
2431 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002432 return openTab(url, incognito, setActive, useCurrent, null);
2433 }
2434
2435 @Override
2436 public Tab openTab(String url, Tab parent, boolean setActive,
2437 boolean useCurrent) {
2438 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2439 setActive, useCurrent, parent);
2440 }
2441
2442 public Tab openTab(String url, boolean incognito, boolean setActive,
2443 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002444 Tab tab = createNewTab(incognito, setActive, useCurrent);
2445 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002446 if (parent != null && parent != tab) {
2447 parent.addChildTab(tab);
2448 }
Michael Kolb519d2282011-05-09 17:03:19 -07002449 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002450 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002451 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002452 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002453 return tab;
2454 }
2455
2456 // this method will attempt to create a new tab
2457 // incognito: private browsing tab
2458 // setActive: ste tab as current tab
2459 // useCurrent: if no new tab can be created, return current tab
2460 private Tab createNewTab(boolean incognito, boolean setActive,
2461 boolean useCurrent) {
2462 Tab tab = null;
2463 if (mTabControl.canCreateNewTab()) {
2464 tab = mTabControl.createNewTab(incognito);
2465 addTab(tab);
2466 if (setActive) {
2467 setActiveTab(tab);
2468 }
2469 } else {
2470 if (useCurrent) {
2471 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002472 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002473 } else {
2474 mUi.showMaxTabsWarning();
2475 }
2476 }
2477 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002478 }
2479
John Reck2bc80422011-06-30 15:11:49 -07002480 @Override
2481 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2482 SnapshotTab tab = null;
2483 if (mTabControl.canCreateNewTab()) {
2484 tab = mTabControl.createSnapshotTab(snapshotId);
2485 addTab(tab);
2486 if (setActive) {
2487 setActiveTab(tab);
2488 }
2489 } else {
2490 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002491 }
2492 return tab;
2493 }
2494
Michael Kolb8233fac2010-10-26 16:08:53 -07002495 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002496 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002497 * @return boolean True if we successfully switched to a different tab. If
2498 * the indexth tab is null, or if that tab is the same as
2499 * the current one, return false.
2500 */
2501 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002502 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002503 Tab currentTab = mTabControl.getCurrentTab();
2504 if (tab == null || tab == currentTab) {
2505 return false;
2506 }
2507 setActiveTab(tab);
2508 return true;
2509 }
2510
2511 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002512 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002513 closeCurrentTab(false);
2514 }
2515
2516 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002517 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002518 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002519 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002520 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002521 return;
2522 }
Michael Kolbc831b632011-05-11 09:30:34 -07002523 final Tab current = mTabControl.getCurrentTab();
2524 final int pos = mTabControl.getCurrentPosition();
2525 Tab newTab = current.getParent();
2526 if (newTab == null) {
2527 newTab = mTabControl.getTab(pos + 1);
2528 if (newTab == null) {
2529 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002530 }
2531 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002532 if (andQuit) {
2533 mTabControl.setCurrentTab(newTab);
2534 closeTab(current);
2535 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002536 // Close window
2537 closeTab(current);
2538 }
2539 }
2540
2541 /**
2542 * Close the tab, remove its associated title bar, and adjust mTabControl's
2543 * current tab to a valid value.
2544 */
2545 @Override
2546 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002547 if (tab == mTabControl.getCurrentTab()) {
2548 closeCurrentTab();
2549 } else {
2550 removeTab(tab);
2551 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002552 }
2553
Afzal Najamd4e33312012-04-26 01:54:01 -04002554 /**
2555 * Close all tabs except the current one
2556 */
2557 @Override
2558 public void closeOtherTabs() {
2559 int inactiveTabs = mTabControl.getTabCount() - 1;
2560 for (int i = inactiveTabs; i >= 0; i--) {
2561 Tab tab = mTabControl.getTab(i);
2562 if (tab != mTabControl.getCurrentTab()) {
2563 removeTab(tab);
2564 }
2565 }
2566 }
2567
Michael Kolb8233fac2010-10-26 16:08:53 -07002568 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002569 protected void loadUrlFromContext(String url) {
2570 Tab tab = getCurrentTab();
2571 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002572 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002573 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002574 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002575 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002576 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002577 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002578 }
2579 }
2580 }
2581
2582 /**
2583 * Load the URL into the given WebView and update the title bar
2584 * to reflect the new load. Call this instead of WebView.loadUrl
2585 * directly.
2586 * @param view The WebView used to load url.
2587 * @param url The URL to load.
2588 */
John Reck71e51422011-07-01 16:49:28 -07002589 @Override
2590 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002591 loadUrl(tab, url, null);
2592 }
2593
2594 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2595 if (tab != null) {
2596 dismissSubWindow(tab);
2597 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002598 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002599 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002600 }
2601
2602 /**
2603 * Load UrlData into a Tab and update the title bar to reflect the new
2604 * load. Call this instead of UrlData.loadIn directly.
2605 * @param t The Tab used to load.
2606 * @param data The UrlData being loaded.
2607 */
2608 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002609 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002610 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002611 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002612 } else {
John Reck38b39652012-06-05 09:22:59 -07002613 if (t != null && data.mDisableUrlOverride) {
2614 t.disableUrlOverridingForLoad();
2615 }
John Reck26b18322011-06-21 13:08:58 -07002616 loadUrl(t, data.mUrl, data.mHeaders);
2617 }
2618 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002619 }
2620
John Reck30c714c2010-12-16 17:30:34 -08002621 @Override
2622 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002623 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002624 if (tab.canGoBack()) {
2625 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002626 } else {
John Reckef654f12011-07-12 16:42:08 -07002627 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002628 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002629 }
2630
2631 void goBackOnePageOrQuit() {
2632 Tab current = mTabControl.getCurrentTab();
2633 if (current == null) {
2634 /*
2635 * Instead of finishing the activity, simply push this to the back
2636 * of the stack and let ActivityManager to choose the foreground
2637 * activity. As BrowserActivity is singleTask, it will be always the
2638 * root of the task. So we can use either true or false for
2639 * moveTaskToBack().
2640 */
2641 mActivity.moveTaskToBack(true);
2642 return;
2643 }
John Reckef654f12011-07-12 16:42:08 -07002644 if (current.canGoBack()) {
2645 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002646 } else {
2647 // Check to see if we are closing a window that was created by
2648 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002649 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002650 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002651 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002652 // Now we close the other tab
2653 closeTab(current);
2654 } else {
Michael Kolbe28b3472011-08-04 16:54:31 -07002655 if ((current.getAppId() != null) || current.closeOnBack()) {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002656 closeCurrentTab(true);
Michael Kolbe28b3472011-08-04 16:54:31 -07002657 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002658 /*
2659 * Instead of finishing the activity, simply push this to the back
2660 * of the stack and let ActivityManager to choose the foreground
2661 * activity. As BrowserActivity is singleTask, it will be always the
2662 * root of the task. So we can use either true or false for
2663 * moveTaskToBack().
2664 */
2665 mActivity.moveTaskToBack(true);
2666 }
2667 }
2668 }
2669
2670 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002671 * helper method for key handler
2672 * returns the current tab if it can't advance
2673 */
Michael Kolbc831b632011-05-11 09:30:34 -07002674 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002675 int pos = mTabControl.getCurrentPosition() + 1;
2676 if (pos >= mTabControl.getTabCount()) {
2677 pos = 0;
2678 }
2679 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002680 }
2681
2682 /**
2683 * helper method for key handler
2684 * returns the current tab if it can't advance
2685 */
Michael Kolbc831b632011-05-11 09:30:34 -07002686 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002687 int pos = mTabControl.getCurrentPosition() - 1;
2688 if ( pos < 0) {
2689 pos = mTabControl.getTabCount() - 1;
2690 }
2691 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002692 }
2693
John Reckbcef87f2012-02-03 14:58:34 -08002694 boolean isMenuOrCtrlKey(int keyCode) {
2695 return (KeyEvent.KEYCODE_MENU == keyCode)
2696 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2697 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2698 }
2699
Michael Kolb0035fad2011-03-14 13:25:28 -07002700 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002701 * handle key events in browser
2702 *
2703 * @param keyCode
2704 * @param event
2705 * @return true if handled, false to pass to super
2706 */
John Reck9c35b9c2012-05-30 10:08:50 -07002707 @Override
2708 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002709 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002710 // Even if MENU is already held down, we need to call to super to open
2711 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002712 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002713 mMenuIsDown = true;
2714 return false;
2715 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002716
Cary Clark8ff8c662010-12-29 15:03:05 -05002717 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002718 Tab tab = getCurrentTab();
2719 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002720
Cary Clark160bbb92011-01-10 11:17:07 -05002721 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2722 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002723
Michael Kolb8233fac2010-10-26 16:08:53 -07002724 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002725 case KeyEvent.KEYCODE_TAB:
2726 if (event.isCtrlPressed()) {
2727 if (event.isShiftPressed()) {
2728 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002729 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002730 } else {
2731 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002732 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002733 }
2734 return true;
2735 }
2736 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002737 case KeyEvent.KEYCODE_SPACE:
2738 // WebView/WebTextView handle the keys in the KeyDown. As
2739 // the Activity's shortcut keys are only handled when WebView
2740 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002741 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002742 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002743 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002744 pageDown();
2745 }
2746 return true;
2747 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002748 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002749 event.startTracking();
2750 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002751 case KeyEvent.KEYCODE_FORWARD:
2752 if (!noModifiers) break;
2753 tab.goForward();
2754 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002755 case KeyEvent.KEYCODE_DPAD_LEFT:
2756 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002757 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002758 return true;
2759 }
2760 break;
2761 case KeyEvent.KEYCODE_DPAD_RIGHT:
2762 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002763 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002764 return true;
2765 }
2766 break;
2767 case KeyEvent.KEYCODE_A:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002768 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002769 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002770 return true;
2771 }
2772 break;
Michael Kolba4183062011-01-16 10:43:21 -08002773// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002774 case KeyEvent.KEYCODE_C:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002775 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002776 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002777 return true;
2778 }
2779 break;
Michael Kolba4183062011-01-16 10:43:21 -08002780// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002781// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002782// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002783// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002784// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002785// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002786// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002787// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002788// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002789// case KeyEvent.KEYCODE_M: // unused
2790// case KeyEvent.KEYCODE_N: // in Chrome: new window
2791// case KeyEvent.KEYCODE_O: // in Chrome: open file
2792// case KeyEvent.KEYCODE_P: // in Chrome: print page
2793// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002794// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002795// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2796 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002797 // we can't use the ctrl/shift flags, they check for
2798 // exclusive use of a modifier
2799 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002800 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002801 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002802 } else {
2803 openTabToHomePage();
2804 }
2805 return true;
2806 }
2807 break;
2808// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2809// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002810// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002811// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2812// case KeyEvent.KEYCODE_Y: // unused
2813// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002814 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002815 // it is a regular key and webview is not null
2816 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002817 }
2818
John Reck9c35b9c2012-05-30 10:08:50 -07002819 @Override
2820 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002821 switch(keyCode) {
2822 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002823 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002824 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002825 return true;
2826 }
2827 break;
2828 }
2829 return false;
2830 }
2831
John Reck9c35b9c2012-05-30 10:08:50 -07002832 @Override
2833 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002834 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002835 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002836 if (KeyEvent.KEYCODE_MENU == keyCode
2837 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002838 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002839 }
2840 }
Cary Clark160bbb92011-01-10 11:17:07 -05002841 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002842 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002843 case KeyEvent.KEYCODE_BACK:
2844 if (event.isTracking() && !event.isCanceled()) {
2845 onBackKey();
2846 return true;
2847 }
2848 break;
2849 }
2850 return false;
2851 }
2852
2853 public boolean isMenuDown() {
2854 return mMenuIsDown;
2855 }
2856
John Reck9c35b9c2012-05-30 10:08:50 -07002857 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002858 public void setupAutoFill(Message message) {
2859 // Open the settings activity at the AutoFill profile fragment so that
2860 // the user can create a new profile. When they return, we will dispatch
2861 // the message so that we can autofill the form using their new profile.
2862 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2863 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2864 AutoFillSettingsFragment.class.getName());
2865 mAutoFillSetupMessage = message;
2866 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2867 }
John Reckb3417f02011-01-14 11:01:05 -08002868
John Reck9c35b9c2012-05-30 10:08:50 -07002869 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002870 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002871 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002872 return true;
2873 }
2874
John Reck1cf4b792011-07-26 10:22:22 -07002875 @Override
2876 public boolean shouldCaptureThumbnails() {
2877 return mUi.shouldCaptureThumbnails();
2878 }
2879
Michael Kolbc3af0672011-08-09 10:24:41 -07002880 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07002881 public boolean supportsVoice() {
2882 PackageManager pm = mActivity.getPackageManager();
2883 List activities = pm.queryIntentActivities(new Intent(
2884 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
2885 return activities.size() != 0;
2886 }
2887
2888 @Override
2889 public void startVoiceRecognizer() {
2890 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
2891 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
2892 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
2893 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
2894 mActivity.startActivityForResult(voice, VOICE_RESULT);
2895 }
2896
2897 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002898 public void setBlockEvents(boolean block) {
2899 mBlockEvents = block;
2900 }
2901
John Reck9c35b9c2012-05-30 10:08:50 -07002902 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002903 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002904 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002905 }
2906
John Reck9c35b9c2012-05-30 10:08:50 -07002907 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002908 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002909 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002910 }
2911
John Reck9c35b9c2012-05-30 10:08:50 -07002912 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002913 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002914 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002915 }
2916
John Reck9c35b9c2012-05-30 10:08:50 -07002917 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002918 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002919 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002920 }
2921
John Reck9c35b9c2012-05-30 10:08:50 -07002922 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002923 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002924 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002925 }
2926
Michael Kolb8233fac2010-10-26 16:08:53 -07002927}