blob: 8a00ba33632b8bbbd4f10bfdbc0917e9f05dd00c [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;
Michael Kolb8233fac2010-10-26 16:08:53 -070038import android.graphics.Bitmap;
39import android.graphics.Canvas;
Michael Kolb8233fac2010-10-26 16:08:53 -070040import android.net.Uri;
41import android.net.http.SslError;
42import android.os.AsyncTask;
43import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070044import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070045import android.os.Handler;
46import android.os.Message;
47import android.os.PowerManager;
48import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000049import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.provider.Browser;
51import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070052import android.provider.BrowserContract.Images;
53import android.provider.ContactsContract;
54import android.provider.ContactsContract.Intents.Insert;
Michael Kolb8233fac2010-10-26 16:08:53 -070055import android.text.TextUtils;
56import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080057import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070058import android.view.ActionMode;
59import android.view.ContextMenu;
60import android.view.ContextMenu.ContextMenuInfo;
61import android.view.Gravity;
62import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070063import android.view.Menu;
64import android.view.MenuInflater;
65import android.view.MenuItem;
66import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070067import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070068import android.view.View;
69import android.webkit.CookieManager;
70import android.webkit.CookieSyncManager;
71import android.webkit.HttpAuthHandler;
George Mount387d45d2011-10-07 15:57:53 -070072import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070073import android.webkit.SslErrorHandler;
74import android.webkit.ValueCallback;
75import android.webkit.WebChromeClient;
76import android.webkit.WebIconDatabase;
77import android.webkit.WebSettings;
78import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000079import android.webkit.WebViewClassic;
Leon Scrogginsac993842011-02-02 12:54:07 -050080import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070081
Michael Kolb4bd767d2011-05-27 11:33:55 -070082import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070083import com.android.browser.UI.ComboViews;
John Reck1cf4b792011-07-26 10:22:22 -070084import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070085import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070086
Michael Kolb8233fac2010-10-26 16:08:53 -070087import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -070088import java.io.File;
89import java.io.FileOutputStream;
90import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -070091import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -070092import java.text.DateFormat;
93import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -070094import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -070095import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -070096import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -070097import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080098import java.util.List;
John Reck26b18322011-06-21 13:08:58 -070099import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700100
101/**
102 * Controller for browser
103 */
104public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700105 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700106
107 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800108 private static final String SEND_APP_ID_EXTRA =
109 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700110 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800111
Michael Kolb8233fac2010-10-26 16:08:53 -0700112
113 // public message ids
114 public final static int LOAD_URL = 1001;
115 public final static int STOP_LOAD = 1002;
116
117 // Message Ids
118 private static final int FOCUS_NODE_HREF = 102;
119 private static final int RELEASE_WAKELOCK = 107;
120
121 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
122
123 private static final int OPEN_BOOKMARKS = 201;
124
125 private static final int EMPTY_MENU = -1;
126
Michael Kolb8233fac2010-10-26 16:08:53 -0700127 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700128 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700129 final static int PREFERENCES_PAGE = 3;
130 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000131 final static int AUTOFILL_SETUP = 5;
132
Michael Kolb8233fac2010-10-26 16:08:53 -0700133 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
134
135 // As the ids are dynamically created, we can't guarantee that they will
136 // be in sequence, so this static array maps ids to a window number.
137 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
138 { R.id.window_one_menu_id, R.id.window_two_menu_id,
139 R.id.window_three_menu_id, R.id.window_four_menu_id,
140 R.id.window_five_menu_id, R.id.window_six_menu_id,
141 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
142
143 // "source" parameter for Google search through search key
144 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
145 // "source" parameter for Google search through simplily type
146 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
147
George Mount387d45d2011-10-07 15:57:53 -0700148 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700149 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
150
John Reckd7dd9b22011-08-30 09:18:29 -0700151 // A bitmap that is re-used in createScreenshot as scratch space
152 private static Bitmap sThumbnailBitmap;
153
Michael Kolb8233fac2010-10-26 16:08:53 -0700154 private Activity mActivity;
155 private UI mUi;
156 private TabControl mTabControl;
157 private BrowserSettings mSettings;
158 private WebViewFactory mFactory;
159
160 private WakeLock mWakeLock;
161
162 private UrlHandler mUrlHandler;
163 private UploadHandler mUploadHandler;
164 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700165 private PageDialogsHandler mPageDialogsHandler;
166 private NetworkStateHandler mNetworkHandler;
167
Ben Murdoch8029a772010-11-16 11:58:21 +0000168 private Message mAutoFillSetupMessage;
169
Michael Kolb8233fac2010-10-26 16:08:53 -0700170 private boolean mShouldShowErrorConsole;
171
172 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
173
174 // FIXME, temp address onPrepareMenu performance problem.
175 // When we move everything out of view, we should rewrite this.
176 private int mCurrentMenuState = 0;
177 private int mMenuState = R.id.MAIN_MENU;
178 private int mOldMenuState = EMPTY_MENU;
179 private Menu mCachedMenu;
180
Michael Kolb8233fac2010-10-26 16:08:53 -0700181 private boolean mMenuIsDown;
182
183 // For select and find, we keep track of the ActionMode so that
184 // finish() can be called as desired.
185 private ActionMode mActionMode;
186
187 /**
188 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
189 * of whether the configuration has changed. The first onMenuOpened call
190 * after a configuration change is simply a reopening of the same menu
191 * (i.e. mIconView did not change).
192 */
193 private boolean mConfigChanged;
194
195 /**
196 * Keeps track of whether the options menu is open. This is important in
197 * determining whether to show or hide the title bar overlay
198 */
199 private boolean mOptionsMenuOpen;
200
201 /**
202 * Whether or not the options menu is in its bigger, popup menu form. When
203 * true, we want the title bar overlay to be gone. When false, we do not.
204 * Only meaningful if mOptionsMenuOpen is true.
205 */
206 private boolean mExtendedMenuOpen;
207
Michael Kolb8233fac2010-10-26 16:08:53 -0700208 private boolean mActivityPaused = true;
209 private boolean mLoadStopped;
210
211 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500212 // Checks to see when the bookmarks database has changed, and updates the
213 // Tabs' notion of whether they represent bookmarked sites.
214 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700215 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700216
Michael Kolbc3af0672011-08-09 10:24:41 -0700217 private boolean mBlockEvents;
218
George Mount3636d0a2011-11-21 09:08:21 -0800219 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700220 mActivity = browser;
221 mSettings = BrowserSettings.getInstance();
222 mTabControl = new TabControl(this);
223 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700224 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800225 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700226 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700227
228 mUrlHandler = new UrlHandler(this);
229 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700230 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
231
Michael Kolb8233fac2010-10-26 16:08:53 -0700232 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500233 mBookmarksObserver = new ContentObserver(mHandler) {
234 @Override
235 public void onChange(boolean selfChange) {
236 int size = mTabControl.getTabCount();
237 for (int i = 0; i < size; i++) {
238 mTabControl.getTab(i).updateBookmarkedStatus();
239 }
240 }
241
242 };
243 browser.getContentResolver().registerContentObserver(
244 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700245
246 mNetworkHandler = new NetworkStateHandler(mActivity, this);
247 // Start watching the default geolocation permissions
248 mSystemAllowGeolocationOrigins =
249 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
250 mSystemAllowGeolocationOrigins.start();
251
John Reckaf262e72011-07-25 13:55:44 -0700252 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700253 }
254
John Reck9c35b9c2012-05-30 10:08:50 -0700255 @Override
256 public void start(final Intent intent) {
Ben Murdochf695f6b2012-05-24 12:59:45 +0100257 WebViewClassic.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800258 // mCrashRecoverHandler has any previously saved state.
259 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700260 }
261
George Mount3636d0a2011-11-21 09:08:21 -0800262 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700263 // Unless the last browser usage was within 24 hours, destroy any
264 // remaining incognito tabs.
265
266 Calendar lastActiveDate = icicle != null ?
267 (Calendar) icicle.getSerializable("lastActiveDate") : null;
268 Calendar today = Calendar.getInstance();
269 Calendar yesterday = Calendar.getInstance();
270 yesterday.add(Calendar.DATE, -1);
271
Patrick Scott7d50a932011-02-04 09:27:26 -0500272 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700273 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800274 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700275
Patrick Scott7d50a932011-02-04 09:27:26 -0500276 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700277 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500278 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000279
Michael Kolbc831b632011-05-11 09:30:34 -0700280 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500281 // Not able to restore so we go ahead and clear session cookies. We
282 // must do this before trying to login the user as we don't want to
283 // clear any session cookies set during login.
284 CookieManager.getInstance().removeSessionCookie();
285 }
286
Patrick Scottd43e75a2011-03-14 14:47:23 -0400287 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500288 new Runnable() {
289 @Override public void run() {
George Mount3636d0a2011-11-21 09:08:21 -0800290 onPreloginFinished(icicle, intent, currentTabId,
291 restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500292 }
293 });
294 }
295
Michael Kolbc831b632011-05-11 09:30:34 -0700296 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
George Mount3636d0a2011-11-21 09:08:21 -0800297 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700298 if (currentTabId == -1) {
John Reck1cf4b792011-07-26 10:22:22 -0700299 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800300 if (intent == null) {
301 // This won't happen under common scenarios. The icicle is
302 // not null, but there aren't any tabs to restore.
303 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700304 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800305 final Bundle extra = intent.getExtras();
306 // Create an initial tab.
307 // If the intent is ACTION_VIEW and data is not null, the Browser is
308 // invoked to view the content by another application. In this case,
309 // the tab will be close when exit.
310 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
311 Tab t = null;
312 if (urlData.isEmpty()) {
313 t = openTabToHomePage();
314 } else {
315 t = openTab(urlData);
316 }
317 if (t != null) {
318 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
319 }
320 WebView webView = t.getWebView();
321 if (extra != null) {
322 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
323 if (scale > 0 && scale <= 1000) {
324 webView.setInitialScale(scale);
325 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700326 }
327 }
John Reckd8c74522011-06-14 08:45:00 -0700328 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700329 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700330 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500331 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700332 List<Tab> tabs = mTabControl.getTabs();
333 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
334 for (Tab t : tabs) {
335 restoredTabs.add(t.getId());
336 }
337 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700338 if (tabs.size() == 0) {
339 openTabToHomePage();
340 }
John Reck1cf4b792011-07-26 10:22:22 -0700341 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700342 // TabControl.restoreState() will create a new tab even if
343 // restoring the state fails.
344 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800345 // Intent is non-null when framework thinks the browser should be
346 // launching with a new intent (icicle is null).
347 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700348 mIntentHandler.onNewIntent(intent);
349 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700350 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700351 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700352 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700353 if (jsFlags.trim().length() != 0) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000354 WebViewClassic.fromWebView(getCurrentWebView()).setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700355 }
George Mount3636d0a2011-11-21 09:08:21 -0800356 if (intent != null
357 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700358 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800359 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700360 }
361
John Reck1cf4b792011-07-26 10:22:22 -0700362 private static class PruneThumbnails implements Runnable {
363 private Context mContext;
364 private List<Long> mIds;
365
366 PruneThumbnails(Context context, List<Long> preserveIds) {
367 mContext = context.getApplicationContext();
368 mIds = preserveIds;
369 }
370
371 @Override
372 public void run() {
373 ContentResolver cr = mContext.getContentResolver();
374 if (mIds == null || mIds.size() == 0) {
375 cr.delete(Thumbnails.CONTENT_URI, null, null);
376 } else {
377 int length = mIds.size();
378 StringBuilder where = new StringBuilder();
379 where.append(Thumbnails._ID);
380 where.append(" not in (");
381 for (int i = 0; i < length; i++) {
382 where.append(mIds.get(i));
383 if (i < (length - 1)) {
384 where.append(",");
385 }
386 }
387 where.append(")");
388 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
389 }
390 }
391
392 }
393
Michael Kolb1514bb72010-11-22 09:11:48 -0800394 @Override
395 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700396 return mFactory;
397 }
398
399 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800400 public void onSetWebView(Tab tab, WebView view) {
401 mUi.onSetWebView(tab, view);
402 }
403
404 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800405 public void createSubWindow(Tab tab) {
406 endActionMode();
407 WebView mainView = tab.getWebView();
408 WebView subView = mFactory.createWebView((mainView == null)
409 ? false
410 : mainView.isPrivateBrowsingEnabled());
411 mUi.createSubWindow(tab, subView);
412 }
413
414 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700415 public Context getContext() {
416 return mActivity;
417 }
418
419 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700420 public Activity getActivity() {
421 return mActivity;
422 }
423
424 void setUi(UI ui) {
425 mUi = ui;
426 }
427
Michael Kolbaf63dba2012-05-16 12:58:05 -0700428 @Override
429 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700430 return mSettings;
431 }
432
433 IntentHandler getIntentHandler() {
434 return mIntentHandler;
435 }
436
437 @Override
438 public UI getUi() {
439 return mUi;
440 }
441
442 int getMaxTabs() {
443 return mActivity.getResources().getInteger(R.integer.max_tabs);
444 }
445
446 @Override
447 public TabControl getTabControl() {
448 return mTabControl;
449 }
450
Michael Kolb1bf23132010-11-19 12:55:12 -0800451 @Override
452 public List<Tab> getTabs() {
453 return mTabControl.getTabs();
454 }
455
John Reckaf262e72011-07-25 13:55:44 -0700456 // Open the icon database.
457 private void openIconDatabase() {
458 // We have to call getInstance on the UI thread
459 final WebIconDatabase instance = WebIconDatabase.getInstance();
460 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000461
John Reckaf262e72011-07-25 13:55:44 -0700462 @Override
463 public void run() {
464 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700465 }
John Reckaf262e72011-07-25 13:55:44 -0700466 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700467 }
468
469 private void startHandler() {
470 mHandler = new Handler() {
471
472 @Override
473 public void handleMessage(Message msg) {
474 switch (msg.what) {
475 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700476 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700477 break;
478 case FOCUS_NODE_HREF:
479 {
480 String url = (String) msg.getData().get("url");
481 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500482 String src = (String) msg.getData().get("src");
483 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700484 if (TextUtils.isEmpty(url)) {
485 break;
486 }
487 HashMap focusNodeMap = (HashMap) msg.obj;
488 WebView view = (WebView) focusNodeMap.get("webview");
489 // Only apply the action if the top window did not change.
490 if (getCurrentTopWebView() != view) {
491 break;
492 }
493 switch (msg.arg1) {
494 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700495 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700496 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500497 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700498 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500499 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500500 case R.id.open_newtab_context_menu_id:
501 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700502 openTab(url, parent,
503 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500504 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700505 case R.id.copy_link_context_menu_id:
506 copy(url);
507 break;
508 case R.id.save_link_context_menu_id:
509 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500510 DownloadHandler.onDownloadStartNoStream(
Kristian Monsenbc5cc752011-03-02 13:14:03 +0000511 mActivity, url, null, null, null,
512 view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700513 break;
514 }
515 break;
516 }
517
518 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700519 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700520 break;
521
522 case STOP_LOAD:
523 stopLoading();
524 break;
525
526 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700527 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700528 mWakeLock.release();
529 // if we reach here, Browser should be still in the
530 // background loading after WAKELOCK_TIMEOUT (5-min).
531 // To avoid burning the battery, stop loading.
532 mTabControl.stopAllLoading();
533 }
534 break;
535
536 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800537 Tab tab = (Tab) msg.obj;
538 if (tab != null) {
539 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700540 }
541 break;
542 }
543 }
544 };
545
546 }
547
John Reckef654f12011-07-12 16:42:08 -0700548 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200549 public Tab getCurrentTab() {
550 return mTabControl.getCurrentTab();
551 }
552
Michael Kolbba99c5d2010-11-29 14:57:41 -0800553 @Override
554 public void shareCurrentPage() {
555 shareCurrentPage(mTabControl.getCurrentTab());
556 }
557
558 private void shareCurrentPage(Tab tab) {
559 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800560 sharePage(mActivity, tab.getTitle(),
561 tab.getUrl(), tab.getFavicon(),
562 createScreenshot(tab.getWebView(),
563 getDesiredThumbnailWidth(mActivity),
564 getDesiredThumbnailHeight(mActivity)));
565 }
566 }
567
Michael Kolb8233fac2010-10-26 16:08:53 -0700568 /**
569 * Share a page, providing the title, url, favicon, and a screenshot. Uses
570 * an {@link Intent} to launch the Activity chooser.
571 * @param c Context used to launch a new Activity.
572 * @param title Title of the page. Stored in the Intent with
573 * {@link Intent#EXTRA_SUBJECT}
574 * @param url URL of the page. Stored in the Intent with
575 * {@link Intent#EXTRA_TEXT}
576 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
577 * with {@link Browser#EXTRA_SHARE_FAVICON}
578 * @param screenshot Bitmap of a screenshot of the page. Stored in the
579 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
580 */
581 static final void sharePage(Context c, String title, String url,
582 Bitmap favicon, Bitmap screenshot) {
583 Intent send = new Intent(Intent.ACTION_SEND);
584 send.setType("text/plain");
585 send.putExtra(Intent.EXTRA_TEXT, url);
586 send.putExtra(Intent.EXTRA_SUBJECT, title);
587 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
588 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
589 try {
590 c.startActivity(Intent.createChooser(send, c.getString(
591 R.string.choosertitle_sharevia)));
592 } catch(android.content.ActivityNotFoundException ex) {
593 // if no app handles it, do nothing
594 }
595 }
596
597 private void copy(CharSequence text) {
598 ClipboardManager cm = (ClipboardManager) mActivity
599 .getSystemService(Context.CLIPBOARD_SERVICE);
600 cm.setText(text);
601 }
602
603 // lifecycle
604
John Reck9c35b9c2012-05-30 10:08:50 -0700605 @Override
606 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700607 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800608 // update the menu in case of a locale change
609 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700610 if (mPageDialogsHandler != null) {
611 mPageDialogsHandler.onConfigurationChanged(config);
612 }
613 mUi.onConfigurationChanged(config);
614 }
615
616 @Override
617 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700618 if (!mUi.isWebShowing()) {
619 mUi.showWeb(false);
620 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 mIntentHandler.onNewIntent(intent);
622 }
623
John Reck9c35b9c2012-05-30 10:08:50 -0700624 @Override
625 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800626 if (mUi.isCustomViewShowing()) {
627 hideCustomView();
628 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700629 if (mActivityPaused) {
630 Log.e(LOGTAG, "BrowserActivity is already paused.");
631 return;
632 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700633 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800634 Tab tab = mTabControl.getCurrentTab();
635 if (tab != null) {
636 tab.pause();
637 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700638 if (mWakeLock == null) {
639 PowerManager pm = (PowerManager) mActivity
640 .getSystemService(Context.POWER_SERVICE);
641 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
642 }
Michael Kolb70976932010-11-30 11:34:01 -0800643 mWakeLock.acquire();
644 mHandler.sendMessageDelayed(mHandler
645 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
646 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700647 }
648 mUi.onPause();
649 mNetworkHandler.onPause();
650
651 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100652 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700653 if (sThumbnailBitmap != null) {
654 sThumbnailBitmap.recycle();
655 sThumbnailBitmap = null;
656 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700657 }
658
John Reck9c35b9c2012-05-30 10:08:50 -0700659 @Override
660 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800662 Bundle saveState = createSaveState();
663
664 // crash recovery manages all save & restore state
665 mCrashRecoveryHandler.writeState(saveState);
666 mSettings.setLastRunPaused(true);
667 }
668
669 /**
670 * Save the current state to outState. Does not write the state to
671 * disk.
672 * @return Bundle containing the current state of all tabs.
673 */
674 /* package */ Bundle createSaveState() {
675 Bundle saveState = new Bundle();
676 mTabControl.saveState(saveState);
677 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700678 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800679 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700680 }
George Mount3636d0a2011-11-21 09:08:21 -0800681 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700682 }
683
John Reck9c35b9c2012-05-30 10:08:50 -0700684 @Override
685 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700686 if (!mActivityPaused) {
687 Log.e(LOGTAG, "BrowserActivity is already resumed.");
688 return;
689 }
George Mount3636d0a2011-11-21 09:08:21 -0800690 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700691 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800692 Tab current = mTabControl.getCurrentTab();
693 if (current != null) {
694 current.resume();
695 resumeWebViewTimers(current);
696 }
John Reckf57c0292011-07-21 18:15:39 -0700697 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200698
Michael Kolb8233fac2010-10-26 16:08:53 -0700699 mUi.onResume();
700 mNetworkHandler.onResume();
701 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100702 NfcHandler.register(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 }
704
John Reckf57c0292011-07-21 18:15:39 -0700705 private void releaseWakeLock() {
706 if (mWakeLock != null && mWakeLock.isHeld()) {
707 mHandler.removeMessages(RELEASE_WAKELOCK);
708 mWakeLock.release();
709 }
710 }
711
Michael Kolb70976932010-11-30 11:34:01 -0800712 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800713 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800714 * @param tab guaranteed non-null
715 */
716 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700717 boolean inLoad = tab.inPageLoad();
718 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
719 CookieSyncManager.getInstance().startSync();
720 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100721 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700722 }
723 }
724
Michael Kolb70976932010-11-30 11:34:01 -0800725 /**
726 * Pause all WebView timers using the WebView of the given tab
727 * @param tab
728 * @return true if the timers are paused or tab is null
729 */
730 private boolean pauseWebViewTimers(Tab tab) {
731 if (tab == null) {
732 return true;
733 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700734 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100735 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700736 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700737 }
Michael Kolb70976932010-11-30 11:34:01 -0800738 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700739 }
740
John Reck9c35b9c2012-05-30 10:08:50 -0700741 @Override
742 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800743 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700744 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
745 mUploadHandler = null;
746 }
747 if (mTabControl == null) return;
748 mUi.onDestroy();
749 // Remove the current tab and sub window
750 Tab t = mTabControl.getCurrentTab();
751 if (t != null) {
752 dismissSubWindow(t);
753 removeTab(t);
754 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500755 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700756 // Destroy all the tabs
757 mTabControl.destroy();
758 WebIconDatabase.getInstance().close();
759 // Stop watching the default geolocation permissions
760 mSystemAllowGeolocationOrigins.stop();
761 mSystemAllowGeolocationOrigins = null;
762 }
763
764 protected boolean isActivityPaused() {
765 return mActivityPaused;
766 }
767
John Reck9c35b9c2012-05-30 10:08:50 -0700768 @Override
769 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700770 mTabControl.freeMemory();
771 }
772
773 @Override
774 public boolean shouldShowErrorConsole() {
775 return mShouldShowErrorConsole;
776 }
777
778 protected void setShouldShowErrorConsole(boolean show) {
779 if (show == mShouldShowErrorConsole) {
780 // Nothing to do.
781 return;
782 }
783 mShouldShowErrorConsole = show;
784 Tab t = mTabControl.getCurrentTab();
785 if (t == null) {
786 // There is no current tab so we cannot toggle the error console
787 return;
788 }
789 mUi.setShouldShowErrorConsole(t, show);
790 }
791
792 @Override
793 public void stopLoading() {
794 mLoadStopped = true;
795 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700796 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700797 if (w != null) {
798 w.stopLoading();
799 mUi.onPageStopped(tab);
800 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700801 }
802
803 boolean didUserStopLoading() {
804 return mLoadStopped;
805 }
806
807 // WebViewController
808
809 @Override
John Reck324d4402011-01-11 16:56:42 -0800810 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700811
812 // We've started to load a new page. If there was a pending message
813 // to save a screenshot then we will now take the new page and save
814 // an incorrect screenshot. Therefore, remove any pending thumbnail
815 // messages from the queue.
816 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800817 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700818
819 // reset sync timer to avoid sync starts during loading a page
820 CookieSyncManager.getInstance().resetSync();
821
822 if (!mNetworkHandler.isNetworkUp()) {
823 view.setNetworkAvailable(false);
824 }
825
826 // when BrowserActivity just starts, onPageStarted may be called before
827 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
828 // to start the timer. As we won't switch tabs while an activity is in
829 // pause state, we can ensure calling resume and pause in pair.
830 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800831 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700832 }
833 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700834 endActionMode();
835
John Reck30c714c2010-12-16 17:30:34 -0800836 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700837
John Reck324d4402011-01-11 16:56:42 -0800838 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 // update the bookmark database for favicon
840 maybeUpdateFavicon(tab, null, url, favicon);
841
842 Performance.tracePageStart(url);
843
844 // Performance probe
845 if (false) {
846 Performance.onPageStarted();
847 }
848
849 }
850
851 @Override
John Reck324d4402011-01-11 16:56:42 -0800852 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700853 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800854 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700855 // pause the WebView timer and release the wake lock if it is finished
856 // while BrowserActivity is in pause state.
Michael Kolb70976932010-11-30 11:34:01 -0800857 if (mActivityPaused && pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700858 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700859 }
Martijn Coenenb2f93552011-06-14 10:48:35 +0200860
Michael Kolb8233fac2010-10-26 16:08:53 -0700861 // Performance probe
862 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800863 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 }
865
866 Performance.tracePageFinished();
867 }
868
869 @Override
John Reck30c714c2010-12-16 17:30:34 -0800870 public void onProgressChanged(Tab tab) {
871 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700872
873 if (newProgress == 100) {
874 CookieSyncManager.getInstance().sync();
875 // onProgressChanged() may continue to be called after the main
876 // frame has finished loading, as any remaining sub frames continue
877 // to load. We'll only get called once though with newProgress as
878 // 100 when everything is loaded. (onPageFinished is called once
879 // when the main frame completes loading regardless of the state of
880 // any sub frames so calls to onProgressChanges may continue after
881 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800882 if (tab.inPageLoad()) {
883 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700884 }
John Reckd9862372012-02-21 15:04:50 -0800885 if (!tab.isPrivateBrowsingEnabled()
886 && !TextUtils.isEmpty(tab.getUrl())
887 && !tab.isSnapshot()) {
888 // Only update the bookmark screenshot if the user did not
889 // cancel the load early and there is not already
890 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700891 if (tab.shouldUpdateThumbnail() &&
892 (tab.inForeground() && !didUserStopLoading()
893 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800894 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
895 mHandler.sendMessageDelayed(mHandler.obtainMessage(
896 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
897 500);
898 }
899 }
900 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700901 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800902 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700903 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800904 // still loading
905 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -0700906 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800907 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700908 }
909 }
John Reck30c714c2010-12-16 17:30:34 -0800910 mUi.onProgressChanged(tab);
911 }
912
913 @Override
Steve Block2466eff2011-10-03 15:33:09 +0100914 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800915 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700916 }
917
918 @Override
919 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800920 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800921 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800922 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700923 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
924 return;
925 }
926 // Update the title in the history database if not in private browsing mode
927 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -0700928 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700929 }
930 }
931
932 @Override
933 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800934 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700935 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
936 }
937
938 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800939 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
940 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700941 }
942
943 @Override
944 public boolean shouldOverrideKeyEvent(KeyEvent event) {
945 if (mMenuIsDown) {
946 // only check shortcut key when MENU is held
947 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
948 event);
949 } else {
950 return false;
951 }
952 }
953
954 @Override
John Reck997b1b72012-04-19 18:08:25 -0700955 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700956 if (!isActivityPaused()) {
957 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -0700958 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700959 } else {
John Reck997b1b72012-04-19 18:08:25 -0700960 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700961 }
962 }
John Reck997b1b72012-04-19 18:08:25 -0700963 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 }
965
966 @Override
John Reck324d4402011-01-11 16:56:42 -0800967 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700968 // Don't save anything in private browsing mode
969 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800970 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700971
John Reck324d4402011-01-11 16:56:42 -0800972 if (TextUtils.isEmpty(url)
973 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700974 return;
975 }
John Reckf57c0292011-07-21 18:15:39 -0700976 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -0700977 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -0700978 }
979
980 @Override
981 public void getVisitedHistory(final ValueCallback<String[]> callback) {
982 AsyncTask<Void, Void, String[]> task =
983 new AsyncTask<Void, Void, String[]>() {
984 @Override
985 public String[] doInBackground(Void... unused) {
986 return Browser.getVisitedHistory(mActivity.getContentResolver());
987 }
988 @Override
989 public void onPostExecute(String[] result) {
990 callback.onReceiveValue(result);
991 }
992 };
993 task.execute();
994 }
995
996 @Override
997 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
998 final HttpAuthHandler handler, final String host,
999 final String realm) {
1000 String username = null;
1001 String password = null;
1002
1003 boolean reuseHttpAuthUsernamePassword
1004 = handler.useHttpAuthUsernamePassword();
1005
1006 if (reuseHttpAuthUsernamePassword && view != null) {
1007 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1008 if (credentials != null && credentials.length == 2) {
1009 username = credentials[0];
1010 password = credentials[1];
1011 }
1012 }
1013
1014 if (username != null && password != null) {
1015 handler.proceed(username, password);
1016 } else {
Ben Murdochfdd37112011-08-05 15:48:14 +01001017 if (tab.inForeground() && !handler.suppressDialog()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001018 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1019 } else {
1020 handler.cancel();
1021 }
1022 }
1023 }
1024
1025 @Override
1026 public void onDownloadStart(Tab tab, String url, String userAgent,
1027 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001028 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -05001029 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001030 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
1031 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001032 // This Tab was opened for the sole purpose of downloading a
1033 // file. Remove it.
1034 if (tab == mTabControl.getCurrentTab()) {
1035 // In this case, the Tab is still on top.
1036 goBackOnePageOrQuit();
1037 } else {
1038 // In this case, it is not.
1039 closeTab(tab);
1040 }
1041 }
1042 }
1043
1044 @Override
1045 public Bitmap getDefaultVideoPoster() {
1046 return mUi.getDefaultVideoPoster();
1047 }
1048
1049 @Override
1050 public View getVideoLoadingProgressView() {
1051 return mUi.getVideoLoadingProgressView();
1052 }
1053
1054 @Override
1055 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1056 SslError error) {
1057 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1058 }
1059
Patrick Scott92066772011-03-10 08:46:27 -05001060 @Override
1061 public void showAutoLogin(Tab tab) {
1062 assert tab.inForeground();
1063 // Update the title bar to show the auto-login request.
1064 mUi.showAutoLogin(tab);
1065 }
1066
1067 @Override
1068 public void hideAutoLogin(Tab tab) {
1069 assert tab.inForeground();
1070 mUi.hideAutoLogin(tab);
1071 }
1072
Michael Kolb8233fac2010-10-26 16:08:53 -07001073 // helper method
1074
1075 /*
1076 * Update the favorites icon if the private browsing isn't enabled and the
1077 * icon is valid.
1078 */
1079 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1080 final String url, Bitmap favicon) {
1081 if (favicon == null) {
1082 return;
1083 }
1084 if (!tab.isPrivateBrowsingEnabled()) {
1085 Bookmarks.updateFavicon(mActivity
1086 .getContentResolver(), originalUrl, url, favicon);
1087 }
1088 }
1089
Leon Scroggins4cd97792010-12-03 15:31:56 -05001090 @Override
1091 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001092 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001093 mUi.bookmarkedStatusHasChanged(tab);
1094 }
1095
Michael Kolb8233fac2010-10-26 16:08:53 -07001096 // end WebViewController
1097
1098 protected void pageUp() {
1099 getCurrentTopWebView().pageUp(false);
1100 }
1101
1102 protected void pageDown() {
1103 getCurrentTopWebView().pageDown(false);
1104 }
1105
1106 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001107 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001108 public void editUrl() {
1109 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001110 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001111 }
1112
John Reck9c35b9c2012-05-30 10:08:50 -07001113 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001114 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001115 WebChromeClient.CustomViewCallback callback) {
1116 if (tab.inForeground()) {
1117 if (mUi.isCustomViewShowing()) {
1118 callback.onCustomViewHidden();
1119 return;
1120 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001121 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001122 // Save the menu state and set it to empty while the custom
1123 // view is showing.
1124 mOldMenuState = mMenuState;
1125 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001126 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001127 }
1128 }
1129
1130 @Override
1131 public void hideCustomView() {
1132 if (mUi.isCustomViewShowing()) {
1133 mUi.onHideCustomView();
1134 // Reset the old menu state.
1135 mMenuState = mOldMenuState;
1136 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001137 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001138 }
1139 }
1140
John Reck9c35b9c2012-05-30 10:08:50 -07001141 @Override
1142 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001143 Intent intent) {
1144 if (getCurrentTopWebView() == null) return;
1145 switch (requestCode) {
1146 case PREFERENCES_PAGE:
1147 if (resultCode == Activity.RESULT_OK && intent != null) {
1148 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001149 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001150 mTabControl.removeParentChildRelationShips();
1151 }
1152 }
1153 break;
1154 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001155 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001156 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001157 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001158 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001159 case AUTOFILL_SETUP:
1160 // Determine whether a profile was actually set up or not
1161 // and if so, send the message back to the WebTextView to
1162 // fill the form with the new profile.
1163 if (getSettings().getAutoFillProfile() != null) {
1164 mAutoFillSetupMessage.sendToTarget();
1165 mAutoFillSetupMessage = null;
1166 }
1167 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001168 case COMBO_VIEW:
1169 if (intent == null || resultCode != Activity.RESULT_OK) {
1170 break;
1171 }
John Reck3ba45532011-08-11 16:26:53 -07001172 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001173 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1174 Tab t = getCurrentTab();
1175 Uri uri = intent.getData();
1176 loadUrl(t, uri.toString());
1177 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1178 String[] urls = intent.getStringArrayExtra(
1179 ComboViewActivity.EXTRA_OPEN_ALL);
1180 Tab parent = getCurrentTab();
1181 for (String url : urls) {
1182 parent = openTab(url, parent,
1183 !mSettings.openInBackground(), true);
1184 }
1185 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1186 long id = intent.getLongExtra(
1187 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1188 if (id >= 0) {
1189 createNewSnapshotTab(id, true);
1190 }
1191 }
1192 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001193 default:
1194 break;
1195 }
1196 getCurrentTopWebView().requestFocus();
1197 }
1198
1199 /**
1200 * Open the Go page.
1201 * @param startWithHistory If true, open starting on the history tab.
1202 * Otherwise, start with the bookmarks tab.
1203 */
1204 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001205 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001206 if (mTabControl.getCurrentWebView() == null) {
1207 return;
1208 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001209 // clear action mode
1210 if (isInCustomActionMode()) {
1211 endActionMode();
1212 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001213 Bundle extras = new Bundle();
1214 // Disable opening in a new window if we have maxed out the windows
1215 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1216 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001217 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001218 }
1219
1220 // combo view callbacks
1221
Michael Kolb8233fac2010-10-26 16:08:53 -07001222 // key handling
1223 protected void onBackKey() {
1224 if (!mUi.onBackKey()) {
1225 WebView subwindow = mTabControl.getCurrentSubWindow();
1226 if (subwindow != null) {
1227 if (subwindow.canGoBack()) {
1228 subwindow.goBack();
1229 } else {
1230 dismissSubWindow(mTabControl.getCurrentTab());
1231 }
1232 } else {
1233 goBackOnePageOrQuit();
1234 }
1235 }
1236 }
1237
Michael Kolb4bd767d2011-05-27 11:33:55 -07001238 protected boolean onMenuKey() {
1239 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001240 }
1241
Michael Kolb8233fac2010-10-26 16:08:53 -07001242 // menu handling and state
1243 // TODO: maybe put into separate handler
1244
John Reck9c35b9c2012-05-30 10:08:50 -07001245 @Override
1246 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001247 if (mMenuState == EMPTY_MENU) {
1248 return false;
1249 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001250 MenuInflater inflater = mActivity.getMenuInflater();
1251 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001252 return true;
1253 }
1254
John Reck9c35b9c2012-05-30 10:08:50 -07001255 @Override
1256 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001257 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001258 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001259 return;
1260 }
1261 if (!(v instanceof WebView)) {
1262 return;
1263 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001264 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001265 WebView.HitTestResult result = webview.getHitTestResult();
1266 if (result == null) {
1267 return;
1268 }
1269
1270 int type = result.getType();
1271 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1272 Log.w(LOGTAG,
1273 "We should not show context menu when nothing is touched");
1274 return;
1275 }
1276 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1277 // let TextView handles context menu
1278 return;
1279 }
1280
1281 // Note, http://b/issue?id=1106666 is requesting that
1282 // an inflated menu can be used again. This is not available
1283 // yet, so inflate each time (yuk!)
1284 MenuInflater inflater = mActivity.getMenuInflater();
1285 inflater.inflate(R.menu.browsercontext, menu);
1286
1287 // Show the correct menu group
1288 final String extra = result.getExtra();
Michael Kolbc159c1a2011-12-15 16:06:38 -08001289 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001290 menu.setGroupVisible(R.id.PHONE_MENU,
1291 type == WebView.HitTestResult.PHONE_TYPE);
1292 menu.setGroupVisible(R.id.EMAIL_MENU,
1293 type == WebView.HitTestResult.EMAIL_TYPE);
1294 menu.setGroupVisible(R.id.GEO_MENU,
1295 type == WebView.HitTestResult.GEO_TYPE);
1296 menu.setGroupVisible(R.id.IMAGE_MENU,
1297 type == WebView.HitTestResult.IMAGE_TYPE
1298 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1299 menu.setGroupVisible(R.id.ANCHOR_MENU,
1300 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1301 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001302 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1303 || type == WebView.HitTestResult.PHONE_TYPE
1304 || type == WebView.HitTestResult.EMAIL_TYPE
1305 || type == WebView.HitTestResult.GEO_TYPE;
1306 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1307 if (hitText) {
1308 menu.findItem(R.id.select_text_menu_id)
1309 .setOnMenuItemClickListener(new SelectText(webview));
1310 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001311 // Setup custom handling depending on the type
1312 switch (type) {
1313 case WebView.HitTestResult.PHONE_TYPE:
1314 menu.setHeaderTitle(Uri.decode(extra));
1315 menu.findItem(R.id.dial_context_menu_id).setIntent(
1316 new Intent(Intent.ACTION_VIEW, Uri
1317 .parse(WebView.SCHEME_TEL + extra)));
1318 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1319 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1320 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1321 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1322 addIntent);
1323 menu.findItem(R.id.copy_phone_context_menu_id)
1324 .setOnMenuItemClickListener(
1325 new Copy(extra));
1326 break;
1327
1328 case WebView.HitTestResult.EMAIL_TYPE:
1329 menu.setHeaderTitle(extra);
1330 menu.findItem(R.id.email_context_menu_id).setIntent(
1331 new Intent(Intent.ACTION_VIEW, Uri
1332 .parse(WebView.SCHEME_MAILTO + extra)));
1333 menu.findItem(R.id.copy_mail_context_menu_id)
1334 .setOnMenuItemClickListener(
1335 new Copy(extra));
1336 break;
1337
1338 case WebView.HitTestResult.GEO_TYPE:
1339 menu.setHeaderTitle(extra);
1340 menu.findItem(R.id.map_context_menu_id).setIntent(
1341 new Intent(Intent.ACTION_VIEW, Uri
1342 .parse(WebView.SCHEME_GEO
1343 + URLEncoder.encode(extra))));
1344 menu.findItem(R.id.copy_geo_context_menu_id)
1345 .setOnMenuItemClickListener(
1346 new Copy(extra));
1347 break;
1348
1349 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1350 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001351 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001352 // decide whether to show the open link in new tab option
1353 boolean showNewTab = mTabControl.canCreateNewTab();
1354 MenuItem newTabItem
1355 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001356 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001357 ? R.string.contextmenu_openlink_newwindow_background
1358 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001359 newTabItem.setVisible(showNewTab);
1360 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001361 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1362 newTabItem.setOnMenuItemClickListener(
1363 new MenuItem.OnMenuItemClickListener() {
1364 @Override
1365 public boolean onMenuItemClick(MenuItem item) {
1366 final HashMap<String, WebView> hrefMap =
1367 new HashMap<String, WebView>();
1368 hrefMap.put("webview", webview);
1369 final Message msg = mHandler.obtainMessage(
1370 FOCUS_NODE_HREF,
1371 R.id.open_newtab_context_menu_id,
1372 0, hrefMap);
1373 webview.requestFocusNodeHref(msg);
1374 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001375 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001376 });
1377 } else {
1378 newTabItem.setOnMenuItemClickListener(
1379 new MenuItem.OnMenuItemClickListener() {
1380 @Override
1381 public boolean onMenuItemClick(MenuItem item) {
1382 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001383 openTab(extra, parent,
1384 !mSettings.openInBackground(),
1385 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001386 return true;
1387 }
1388 });
1389 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001390 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001391 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1392 break;
1393 }
1394 // otherwise fall through to handle image part
1395 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001396 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1397 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001398 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1399 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001400 shareItem.setOnMenuItemClickListener(
1401 new MenuItem.OnMenuItemClickListener() {
1402 @Override
1403 public boolean onMenuItemClick(MenuItem item) {
1404 sharePage(mActivity, null, extra, null,
1405 null);
1406 return true;
1407 }
1408 }
1409 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001411 menu.findItem(R.id.view_image_context_menu_id)
1412 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1413 @Override
1414 public boolean onMenuItemClick(MenuItem item) {
1415 openTab(extra, mTabControl.getCurrentTab(), true, true);
1416 return false;
1417 }
1418 });
Michael Kolb8233fac2010-10-26 16:08:53 -07001419 menu.findItem(R.id.download_context_menu_id).
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001420 setOnMenuItemClickListener(
1421 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled()));
John Reck3527dd12011-02-22 10:35:29 -08001422 menu.findItem(R.id.set_wallpaper_context_menu_id).
1423 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1424 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001425 break;
1426
1427 default:
1428 Log.w(LOGTAG, "We should not get here.");
1429 break;
1430 }
1431 //update the ui
1432 mUi.onContextMenuCreated(menu);
1433 }
1434
1435 /**
1436 * As the menu can be open when loading state changes
1437 * we must manually update the state of the stop/reload menu
1438 * item
1439 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001440 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001441 if (menu == null) {
1442 return;
1443 }
1444 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001445 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001446 menu.findItem(R.id.stop_menu_id):
1447 menu.findItem(R.id.reload_menu_id);
1448 if (src != null) {
1449 dest.setIcon(src.getIcon());
1450 dest.setTitle(src.getTitle());
1451 }
1452 }
1453
John Reck9c35b9c2012-05-30 10:08:50 -07001454 @Override
1455 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001456 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001457 // hold on to the menu reference here; it is used by the page callbacks
1458 // to update the menu based on loading state
1459 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001460 // Note: setVisible will decide whether an item is visible; while
1461 // setEnabled() will decide whether an item is enabled, which also means
1462 // whether the matching shortcut key will function.
1463 switch (mMenuState) {
1464 case EMPTY_MENU:
1465 if (mCurrentMenuState != mMenuState) {
1466 menu.setGroupVisible(R.id.MAIN_MENU, false);
1467 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1468 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1469 }
1470 break;
1471 default:
1472 if (mCurrentMenuState != mMenuState) {
1473 menu.setGroupVisible(R.id.MAIN_MENU, true);
1474 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1475 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1476 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001477 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001478 break;
1479 }
1480 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001481 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001482 }
1483
Michael Kolb4bf79712011-07-14 14:18:12 -07001484 @Override
1485 public void updateMenuState(Tab tab, Menu menu) {
1486 boolean canGoBack = false;
1487 boolean canGoForward = false;
1488 boolean isHome = false;
John Reck42229bc2011-08-19 13:26:43 -07001489 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001490 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001491 if (tab != null) {
1492 canGoBack = tab.canGoBack();
1493 canGoForward = tab.canGoForward();
1494 isHome = mSettings.getHomePage().equals(tab.getUrl());
John Reck42229bc2011-08-19 13:26:43 -07001495 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001496 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001497 }
1498 final MenuItem back = menu.findItem(R.id.back_menu_id);
1499 back.setEnabled(canGoBack);
1500
1501 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1502 home.setEnabled(!isHome);
1503
1504 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1505 forward.setEnabled(canGoForward);
1506
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001507 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1508 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001509 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001510 if (source != null && dest != null) {
1511 dest.setTitle(source.getTitle());
1512 dest.setIcon(source.getIcon());
1513 }
John Recke1a03a32011-09-14 17:04:16 -07001514 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001515
1516 // decide whether to show the share link option
1517 PackageManager pm = mActivity.getPackageManager();
1518 Intent send = new Intent(Intent.ACTION_SEND);
1519 send.setType("text/plain");
1520 ResolveInfo ri = pm.resolveActivity(send,
1521 PackageManager.MATCH_DEFAULT_ONLY);
1522 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1523
1524 boolean isNavDump = mSettings.enableNavDump();
1525 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1526 nav.setVisible(isNavDump);
1527 nav.setEnabled(isNavDump);
1528
1529 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001530 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1531 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001532 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1533 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Michael Kolb315d5022011-10-13 12:47:11 -07001534 menu.setGroupVisible(R.id.COMBO_MENU, false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001535
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001536 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001537 }
1538
John Reck9c35b9c2012-05-30 10:08:50 -07001539 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001540 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001541 if (null == getCurrentTopWebView()) {
1542 return false;
1543 }
1544 if (mMenuIsDown) {
1545 // The shortcut action consumes the MENU. Even if it is still down,
1546 // it won't trigger the next shortcut action. In the case of the
1547 // shortcut action triggering a new activity, like Bookmarks, we
1548 // won't get onKeyUp for MENU. So it is important to reset it here.
1549 mMenuIsDown = false;
1550 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001551 if (mUi.onOptionsItemSelected(item)) {
1552 // ui callback handled it
1553 return true;
1554 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001555 switch (item.getItemId()) {
1556 // -- Main menu
1557 case R.id.new_tab_menu_id:
1558 openTabToHomePage();
1559 break;
1560
1561 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001562 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001563 break;
1564
1565 case R.id.goto_menu_id:
1566 editUrl();
1567 break;
1568
1569 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001570 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1571 break;
1572
1573 case R.id.history_menu_id:
1574 bookmarksOrHistoryPicker(ComboViews.History);
1575 break;
1576
1577 case R.id.snapshots_menu_id:
1578 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001579 break;
1580
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001582 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001583 break;
1584
1585 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001586 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001587 stopLoading();
1588 } else {
1589 getCurrentTopWebView().reload();
1590 }
1591 break;
1592
1593 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001594 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001595 break;
1596
1597 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001598 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001599 break;
1600
1601 case R.id.close_menu_id:
1602 // Close the subwindow if it exists.
1603 if (mTabControl.getCurrentSubWindow() != null) {
1604 dismissSubWindow(mTabControl.getCurrentTab());
1605 break;
1606 }
1607 closeCurrentTab();
1608 break;
1609
1610 case R.id.homepage_menu_id:
1611 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001612 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001613 break;
1614
1615 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001616 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001617 break;
1618
1619 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001620 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001621 break;
1622
John Reck2bc80422011-06-30 15:11:49 -07001623 case R.id.save_snapshot_menu_id:
1624 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001625 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001626 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001627 break;
1628
Michael Kolb8233fac2010-10-26 16:08:53 -07001629 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001630 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001631 break;
1632
John Recke1a03a32011-09-14 17:04:16 -07001633 case R.id.snapshot_go_live:
1634 goLive();
1635 return true;
1636
Michael Kolb8233fac2010-10-26 16:08:53 -07001637 case R.id.share_page_menu_id:
1638 Tab currentTab = mTabControl.getCurrentTab();
1639 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001640 return false;
1641 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001642 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001643 break;
1644
1645 case R.id.dump_nav_menu_id:
1646 getCurrentTopWebView().debugDump();
1647 break;
1648
Michael Kolb8233fac2010-10-26 16:08:53 -07001649 case R.id.zoom_in_menu_id:
1650 getCurrentTopWebView().zoomIn();
1651 break;
1652
1653 case R.id.zoom_out_menu_id:
1654 getCurrentTopWebView().zoomOut();
1655 break;
1656
1657 case R.id.view_downloads_menu_id:
1658 viewDownloads();
1659 break;
1660
John Reck42229bc2011-08-19 13:26:43 -07001661 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001662 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07001663 break;
1664
Michael Kolb8233fac2010-10-26 16:08:53 -07001665 case R.id.window_one_menu_id:
1666 case R.id.window_two_menu_id:
1667 case R.id.window_three_menu_id:
1668 case R.id.window_four_menu_id:
1669 case R.id.window_five_menu_id:
1670 case R.id.window_six_menu_id:
1671 case R.id.window_seven_menu_id:
1672 case R.id.window_eight_menu_id:
1673 {
1674 int menuid = item.getItemId();
1675 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1676 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1677 Tab desiredTab = mTabControl.getTab(id);
1678 if (desiredTab != null &&
1679 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001680 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001681 }
1682 break;
1683 }
1684 }
1685 }
1686 break;
1687
1688 default:
1689 return false;
1690 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001691 return true;
1692 }
1693
John Reck68234a92012-04-19 15:27:12 -07001694 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
1695 implements OnCancelListener {
1696
1697 private Tab mTab;
1698 private Dialog mProgressDialog;
1699 private ContentValues mValues;
1700
1701 private SaveSnapshotTask(Tab tab) {
1702 mTab = tab;
1703 }
1704
1705 @Override
1706 protected void onPreExecute() {
1707 CharSequence message = mActivity.getText(R.string.saving_snapshot);
1708 mProgressDialog = ProgressDialog.show(mActivity, null, message,
1709 true, true, this);
1710 mValues = mTab.createSnapshotValues();
1711 }
1712
1713 @Override
1714 protected Long doInBackground(Void... params) {
1715 if (!mTab.saveViewState(mValues)) {
1716 return null;
1717 }
1718 if (isCancelled()) {
1719 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
1720 File file = mActivity.getFileStreamPath(path);
1721 if (!file.delete()) {
1722 file.deleteOnExit();
1723 }
1724 return null;
1725 }
1726 final ContentResolver cr = mActivity.getContentResolver();
1727 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
1728 if (result == null) {
1729 return null;
1730 }
1731 long id = ContentUris.parseId(result);
1732 return id;
1733 }
1734
1735 @Override
1736 protected void onPostExecute(Long id) {
1737 if (isCancelled()) {
1738 return;
1739 }
1740 mProgressDialog.dismiss();
1741 if (id == null) {
1742 Toast.makeText(mActivity, R.string.snapshot_failed,
1743 Toast.LENGTH_SHORT).show();
1744 return;
1745 }
1746 Bundle b = new Bundle();
1747 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1748 mUi.showComboView(ComboViews.Snapshots, b);
1749 }
1750
1751 @Override
1752 public void onCancel(DialogInterface dialog) {
1753 cancel(true);
1754 }
1755 }
1756
Michael Kolb80f75082012-04-10 10:50:06 -07001757 @Override
1758 public void toggleUserAgent() {
1759 WebView web = getCurrentWebView();
1760 mSettings.toggleDesktopUseragent(web);
1761 web.loadUrl(web.getOriginalUrl());
1762 }
1763
1764 @Override
1765 public void findOnPage() {
1766 getCurrentTopWebView().showFindDialog(null, true);
1767 }
1768
1769 @Override
1770 public void openPreferences() {
1771 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1772 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1773 getCurrentTopWebView().getUrl());
1774 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1775 }
1776
1777 @Override
1778 public void bookmarkCurrentPage() {
1779 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
1780 if (bookmarkIntent != null) {
1781 mActivity.startActivity(bookmarkIntent);
1782 }
1783 }
1784
John Recke1a03a32011-09-14 17:04:16 -07001785 private void goLive() {
1786 Tab t = getCurrentTab();
1787 t.loadUrl(t.getUrl(), null);
1788 }
1789
Michael Kolb315d5022011-10-13 12:47:11 -07001790 @Override
1791 public void showPageInfo() {
1792 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
1793 }
1794
John Reck9c35b9c2012-05-30 10:08:50 -07001795 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001796 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001797 // Let the History and Bookmark fragments handle menus they created.
1798 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1799 return false;
1800 }
1801
Michael Kolb8233fac2010-10-26 16:08:53 -07001802 int id = item.getItemId();
1803 boolean result = true;
1804 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001805 // -- Browser context menu
1806 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001807 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001808 case R.id.copy_link_context_menu_id:
1809 final WebView webView = getCurrentTopWebView();
1810 if (null == webView) {
1811 result = false;
1812 break;
1813 }
1814 final HashMap<String, WebView> hrefMap =
1815 new HashMap<String, WebView>();
1816 hrefMap.put("webview", webView);
1817 final Message msg = mHandler.obtainMessage(
1818 FOCUS_NODE_HREF, id, 0, hrefMap);
1819 webView.requestFocusNodeHref(msg);
1820 break;
1821
1822 default:
1823 // For other context menus
1824 result = onOptionsItemSelected(item);
1825 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001826 return result;
1827 }
1828
1829 /**
1830 * support programmatically opening the context menu
1831 */
1832 public void openContextMenu(View view) {
1833 mActivity.openContextMenu(view);
1834 }
1835
1836 /**
1837 * programmatically open the options menu
1838 */
1839 public void openOptionsMenu() {
1840 mActivity.openOptionsMenu();
1841 }
1842
John Reck9c35b9c2012-05-30 10:08:50 -07001843 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001844 public boolean onMenuOpened(int featureId, Menu menu) {
1845 if (mOptionsMenuOpen) {
1846 if (mConfigChanged) {
1847 // We do not need to make any changes to the state of the
1848 // title bar, since the only thing that happened was a
1849 // change in orientation
1850 mConfigChanged = false;
1851 } else {
1852 if (!mExtendedMenuOpen) {
1853 mExtendedMenuOpen = true;
1854 mUi.onExtendedMenuOpened();
1855 } else {
1856 // Switching the menu back to icon view, so show the
1857 // title bar once again.
1858 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001859 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001860 }
1861 }
1862 } else {
1863 // The options menu is closed, so open it, and show the title
1864 mOptionsMenuOpen = true;
1865 mConfigChanged = false;
1866 mExtendedMenuOpen = false;
1867 mUi.onOptionsMenuOpened();
1868 }
1869 return true;
1870 }
1871
John Reck9c35b9c2012-05-30 10:08:50 -07001872 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001873 public void onOptionsMenuClosed(Menu menu) {
1874 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001875 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001876 }
1877
John Reck9c35b9c2012-05-30 10:08:50 -07001878 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001879 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001880 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001881 }
1882
1883 // Helper method for getting the top window.
1884 @Override
1885 public WebView getCurrentTopWebView() {
1886 return mTabControl.getCurrentTopWebView();
1887 }
1888
1889 @Override
1890 public WebView getCurrentWebView() {
1891 return mTabControl.getCurrentWebView();
1892 }
1893
1894 /*
1895 * This method is called as a result of the user selecting the options
1896 * menu to see the download window. It shows the download window on top of
1897 * the current window.
1898 */
1899 void viewDownloads() {
1900 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1901 mActivity.startActivity(intent);
1902 }
1903
John Reck30b065e2011-07-19 10:58:05 -07001904 int getActionModeHeight() {
1905 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1906 new int[] { android.R.attr.actionBarSize });
1907 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1908 actionBarSizeTypedArray.recycle();
1909 return size;
1910 }
1911
Michael Kolb8233fac2010-10-26 16:08:53 -07001912 // action mode
1913
John Reck9c35b9c2012-05-30 10:08:50 -07001914 @Override
1915 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001916 mUi.onActionModeStarted(mode);
1917 mActionMode = mode;
1918 }
1919
1920 /*
1921 * True if a custom ActionMode (i.e. find or select) is in use.
1922 */
1923 @Override
1924 public boolean isInCustomActionMode() {
1925 return mActionMode != null;
1926 }
1927
1928 /*
1929 * End the current ActionMode.
1930 */
1931 @Override
1932 public void endActionMode() {
1933 if (mActionMode != null) {
1934 mActionMode.finish();
1935 }
1936 }
1937
1938 /*
1939 * Called by find and select when they are finished. Replace title bars
1940 * as necessary.
1941 */
John Reck9c35b9c2012-05-30 10:08:50 -07001942 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001943 public void onActionModeFinished(ActionMode mode) {
1944 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001945 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001946 mActionMode = null;
1947 }
1948
1949 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08001950 final Tab tab = getCurrentTab();
1951 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07001952 }
1953
1954 // bookmark handling
1955
1956 /**
1957 * add the current page as a bookmark to the given folder id
1958 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07001959 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001960 * bookmarked, and if it is, edit that bookmark. If false, and
1961 * the site is already bookmarked, do not attempt to edit the
1962 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001963 */
1964 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07001965 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07001966 WebView w = getCurrentTopWebView();
1967 if (w == null) {
1968 return null;
1969 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001970 Intent i = new Intent(mActivity,
1971 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07001972 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1973 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1974 String touchIconUrl = w.getTouchIconUrl();
1975 if (touchIconUrl != null) {
1976 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1977 WebSettings settings = w.getSettings();
1978 if (settings != null) {
1979 i.putExtra(AddBookmarkPage.USER_AGENT,
1980 settings.getUserAgentString());
1981 }
1982 }
1983 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1984 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1985 getDesiredThumbnailHeight(mActivity)));
1986 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07001987 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001988 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1989 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001990 // Put the dialog at the upper right of the screen, covering the
1991 // star on the title bar.
1992 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07001993 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07001994 }
1995
1996 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07001997 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001998 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001999 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002000 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002001 }
2002
2003 // thumbnails
2004
2005 /**
2006 * Return the desired width for thumbnail screenshots, which are stored in
2007 * the database, and used on the bookmarks screen.
2008 * @param context Context for finding out the density of the screen.
2009 * @return desired width for thumbnail screenshot.
2010 */
2011 static int getDesiredThumbnailWidth(Context context) {
2012 return context.getResources().getDimensionPixelOffset(
2013 R.dimen.bookmarkThumbnailWidth);
2014 }
2015
2016 /**
2017 * Return the desired height for thumbnail screenshots, which are stored in
2018 * the database, and used on the bookmarks screen.
2019 * @param context Context for finding out the density of the screen.
2020 * @return desired height for thumbnail screenshot.
2021 */
2022 static int getDesiredThumbnailHeight(Context context) {
2023 return context.getResources().getDimensionPixelOffset(
2024 R.dimen.bookmarkThumbnailHeight);
2025 }
2026
John Reck8cc92352011-07-06 17:41:52 -07002027 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002028 if (view == null || view.getContentHeight() == 0
2029 || view.getContentWidth() == 0) {
2030 return null;
2031 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002032 // We render to a bitmap 2x the desired size so that we can then
2033 // re-scale it with filtering since canvas.scale doesn't filter
2034 // This helps reduce aliasing at the cost of being slightly blurry
2035 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002036 int scaledWidth = width * filter_scale;
2037 int scaledHeight = height * filter_scale;
2038 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2039 || sThumbnailBitmap.getHeight() != scaledHeight) {
2040 if (sThumbnailBitmap != null) {
2041 sThumbnailBitmap.recycle();
2042 sThumbnailBitmap = null;
2043 }
2044 sThumbnailBitmap =
2045 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002046 }
John Reckd7dd9b22011-08-30 09:18:29 -07002047 Canvas canvas = new Canvas(sThumbnailBitmap);
2048 int contentWidth = view.getContentWidth();
2049 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2050 if (view instanceof BrowserWebView) {
2051 int dy = -((BrowserWebView)view).getTitleHeight();
2052 canvas.translate(0, dy * overviewScale);
2053 }
2054
2055 canvas.scale(overviewScale, overviewScale);
2056
2057 if (view instanceof BrowserWebView) {
2058 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002059 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002060 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002061 }
John Reckd7dd9b22011-08-30 09:18:29 -07002062 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2063 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002064 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002065 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002066 }
2067
John Reck34ef2672011-02-10 11:30:55 -08002068 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002069 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002070 // FIXME: Would like to make sure there is actually something to
2071 // draw, but the API for that (WebViewCore.pictureReady()) is not
2072 // currently accessible here.
2073
John Reck34ef2672011-02-10 11:30:55 -08002074 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002075 if (view == null) {
2076 // Tab was destroyed
2077 return;
2078 }
John Reck34ef2672011-02-10 11:30:55 -08002079 final String url = tab.getUrl();
2080 final String originalUrl = view.getOriginalUrl();
John Reck34ef2672011-02-10 11:30:55 -08002081 if (TextUtils.isEmpty(url)) {
2082 return;
2083 }
2084
2085 // Only update thumbnails for web urls (http(s)://), not for
2086 // about:, javascript:, data:, etc...
2087 // Unless it is a bookmarked site, then always update
2088 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2089 return;
2090 }
2091
Michael Kolb8233fac2010-10-26 16:08:53 -07002092 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2093 getDesiredThumbnailHeight(mActivity));
2094 if (bm == null) {
2095 return;
2096 }
2097
2098 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002099 new AsyncTask<Void, Void, Void>() {
2100 @Override
2101 protected Void doInBackground(Void... unused) {
2102 Cursor cursor = null;
2103 try {
2104 // TODO: Clean this up
2105 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2106 if (cursor != null && cursor.moveToFirst()) {
2107 final ByteArrayOutputStream os =
2108 new ByteArrayOutputStream();
2109 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002110
John Reck34ef2672011-02-10 11:30:55 -08002111 ContentValues values = new ContentValues();
2112 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002113
John Reck34ef2672011-02-10 11:30:55 -08002114 do {
John Reck617fd832011-02-16 14:35:59 -08002115 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002116 cr.update(Images.CONTENT_URI, values, null, null);
2117 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002118 }
John Reck34ef2672011-02-10 11:30:55 -08002119 } catch (IllegalStateException e) {
2120 // Ignore
2121 } finally {
2122 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002123 }
John Reck34ef2672011-02-10 11:30:55 -08002124 return null;
2125 }
2126 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002127 }
2128
2129 private class Copy implements OnMenuItemClickListener {
2130 private CharSequence mText;
2131
John Reck9c35b9c2012-05-30 10:08:50 -07002132 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002133 public boolean onMenuItemClick(MenuItem item) {
2134 copy(mText);
2135 return true;
2136 }
2137
2138 public Copy(CharSequence toCopy) {
2139 mText = toCopy;
2140 }
2141 }
2142
Leon Scroggins63c02662010-11-18 15:16:27 -05002143 private static class Download implements OnMenuItemClickListener {
2144 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002145 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002146 private boolean mPrivateBrowsing;
George Mount387d45d2011-10-07 15:57:53 -07002147 private static final String FALLBACK_EXTENSION = "dat";
2148 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002149
John Reck9c35b9c2012-05-30 10:08:50 -07002150 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002151 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002152 if (DataUri.isDataUri(mText)) {
2153 saveDataUri();
2154 } else {
2155 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
2156 null, null, mPrivateBrowsing);
2157 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002158 return true;
2159 }
2160
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002161 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002162 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002163 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002164 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002165 }
George Mount387d45d2011-10-07 15:57:53 -07002166
2167 /**
2168 * Treats mText as a data URI and writes its contents to a file
2169 * based on the current time.
2170 */
2171 private void saveDataUri() {
2172 FileOutputStream outputStream = null;
2173 try {
2174 DataUri uri = new DataUri(mText);
2175 File target = getTarget(uri);
2176 outputStream = new FileOutputStream(target);
2177 outputStream.write(uri.getData());
2178 final DownloadManager manager =
2179 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2180 manager.addCompletedDownload(target.getName(),
2181 mActivity.getTitle().toString(), false,
2182 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002183 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002184 } catch (IOException e) {
2185 Log.e(LOGTAG, "Could not save data URL");
2186 } finally {
2187 if (outputStream != null) {
2188 try {
2189 outputStream.close();
2190 } catch (IOException e) {
2191 // ignore close errors
2192 }
2193 }
2194 }
2195 }
2196
2197 /**
2198 * Creates a File based on the current time stamp and uses
2199 * the mime type of the DataUri to get the extension.
2200 */
2201 private File getTarget(DataUri uri) throws IOException {
2202 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
2203 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT);
2204 String nameBase = format.format(new Date());
2205 String mimeType = uri.getMimeType();
2206 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2207 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2208 if (extension == null) {
2209 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2210 extension = FALLBACK_EXTENSION;
2211 }
2212 extension = "." + extension; // createTempFile needs the '.'
2213 File targetFile = File.createTempFile(nameBase, extension, dir);
2214 return targetFile;
2215 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002216 }
2217
Cary Clark8974d282010-11-22 10:46:05 -05002218 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002219 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002220
John Reck9c35b9c2012-05-30 10:08:50 -07002221 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002222 public boolean onMenuItemClick(MenuItem item) {
2223 if (mWebView != null) {
2224 return mWebView.selectText();
2225 }
2226 return false;
2227 }
2228
2229 public SelectText(WebView webView) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002230 mWebView = WebViewClassic.fromWebView(webView);
Cary Clark8974d282010-11-22 10:46:05 -05002231 }
2232
2233 }
2234
Michael Kolb8233fac2010-10-26 16:08:53 -07002235 /********************** TODO: UI stuff *****************************/
2236
2237 // these methods have been copied, they still need to be cleaned up
2238
2239 /****************** tabs ***************************************************/
2240
2241 // basic tab interactions:
2242
2243 // it is assumed that tabcontrol already knows about the tab
2244 protected void addTab(Tab tab) {
2245 mUi.addTab(tab);
2246 }
2247
2248 protected void removeTab(Tab tab) {
2249 mUi.removeTab(tab);
2250 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002251 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002252 }
2253
Michael Kolbf2055602011-04-09 17:20:03 -07002254 @Override
2255 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002256 // monkey protection against delayed start
2257 if (tab != null) {
2258 mTabControl.setCurrentTab(tab);
2259 // the tab is guaranteed to have a webview after setCurrentTab
2260 mUi.setActiveTab(tab);
2261 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002262 }
2263
John Reck8bcafc12011-08-29 16:43:02 -07002264 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002265 Tab current = mTabControl.getCurrentTab();
2266 if (current != null
2267 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002268 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002269 }
2270 }
2271
John Reck26b18322011-06-21 13:08:58 -07002272 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002273 // Dismiss the subwindow if applicable.
2274 dismissSubWindow(appTab);
2275 // Since we might kill the WebView, remove it from the
2276 // content view first.
2277 mUi.detachTab(appTab);
2278 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002279 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002280 // TODO: analyze why the remove and add are necessary
2281 mUi.attachTab(appTab);
2282 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002283 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002284 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002285 } else {
2286 // If the tab was the current tab, we have to attach
2287 // it to the view system again.
2288 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002289 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002290 }
2291 }
2292
2293 // Remove the sub window if it exists. Also called by TabControl when the
2294 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002295 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002296 public void dismissSubWindow(Tab tab) {
2297 removeSubWindow(tab);
2298 // dismiss the subwindow. This will destroy the WebView.
2299 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002300 WebView wv = getCurrentTopWebView();
2301 if (wv != null) {
2302 wv.requestFocus();
2303 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002304 }
2305
2306 @Override
2307 public void removeSubWindow(Tab t) {
2308 if (t.getSubWebView() != null) {
2309 mUi.removeSubWindow(t.getSubViewContainer());
2310 }
2311 }
2312
2313 @Override
2314 public void attachSubWindow(Tab tab) {
2315 if (tab.getSubWebView() != null) {
2316 mUi.attachSubWindow(tab.getSubViewContainer());
2317 getCurrentTopWebView().requestFocus();
2318 }
2319 }
2320
Mathew Inwood29721c22011-06-29 17:55:24 +01002321 private Tab showPreloadedTab(final UrlData urlData) {
2322 if (!urlData.isPreloaded()) {
2323 return null;
2324 }
2325 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2326 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2327 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002328 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002329 // Could not submit query. Fallback to regular tab creation
2330 tabControl.destroy();
2331 return null;
2332 }
2333 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002334 // check tab count and make room for new tab
2335 if (!mTabControl.canCreateNewTab()) {
2336 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2337 if (leastUsed != null) {
2338 closeTab(leastUsed);
2339 }
2340 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002341 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002342 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002343 mTabControl.addPreloadedTab(t);
2344 addTab(t);
2345 setActiveTab(t);
2346 return t;
2347 }
2348
Michael Kolb7bcafde2011-05-09 13:55:59 -07002349 // open a non inconito tab with the given url data
2350 // and set as active tab
2351 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002352 Tab tab = showPreloadedTab(urlData);
2353 if (tab == null) {
2354 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002355 if ((tab != null) && !urlData.isEmpty()) {
2356 loadUrlDataIn(tab, urlData);
2357 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002358 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002359 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002360 }
2361
Michael Kolb843510f2010-12-09 10:51:49 -08002362 @Override
2363 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002364 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002365 }
2366
Michael Kolb8233fac2010-10-26 16:08:53 -07002367 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002368 public Tab openIncognitoTab() {
2369 return openTab(INCOGNITO_URI, true, true, false);
2370 }
2371
2372 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002373 public Tab openTab(String url, boolean incognito, boolean setActive,
2374 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002375 return openTab(url, incognito, setActive, useCurrent, null);
2376 }
2377
2378 @Override
2379 public Tab openTab(String url, Tab parent, boolean setActive,
2380 boolean useCurrent) {
2381 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2382 setActive, useCurrent, parent);
2383 }
2384
2385 public Tab openTab(String url, boolean incognito, boolean setActive,
2386 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002387 Tab tab = createNewTab(incognito, setActive, useCurrent);
2388 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002389 if (parent != null && parent != tab) {
2390 parent.addChildTab(tab);
2391 }
Michael Kolb519d2282011-05-09 17:03:19 -07002392 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002393 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002394 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002395 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002396 return tab;
2397 }
2398
2399 // this method will attempt to create a new tab
2400 // incognito: private browsing tab
2401 // setActive: ste tab as current tab
2402 // useCurrent: if no new tab can be created, return current tab
2403 private Tab createNewTab(boolean incognito, boolean setActive,
2404 boolean useCurrent) {
2405 Tab tab = null;
2406 if (mTabControl.canCreateNewTab()) {
2407 tab = mTabControl.createNewTab(incognito);
2408 addTab(tab);
2409 if (setActive) {
2410 setActiveTab(tab);
2411 }
2412 } else {
2413 if (useCurrent) {
2414 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002415 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002416 } else {
2417 mUi.showMaxTabsWarning();
2418 }
2419 }
2420 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002421 }
2422
John Reck2bc80422011-06-30 15:11:49 -07002423 @Override
2424 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2425 SnapshotTab tab = null;
2426 if (mTabControl.canCreateNewTab()) {
2427 tab = mTabControl.createSnapshotTab(snapshotId);
2428 addTab(tab);
2429 if (setActive) {
2430 setActiveTab(tab);
2431 }
2432 } else {
2433 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002434 }
2435 return tab;
2436 }
2437
Michael Kolb8233fac2010-10-26 16:08:53 -07002438 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002439 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002440 * @return boolean True if we successfully switched to a different tab. If
2441 * the indexth tab is null, or if that tab is the same as
2442 * the current one, return false.
2443 */
2444 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002445 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002446 Tab currentTab = mTabControl.getCurrentTab();
2447 if (tab == null || tab == currentTab) {
2448 return false;
2449 }
2450 setActiveTab(tab);
2451 return true;
2452 }
2453
2454 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002455 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002456 closeCurrentTab(false);
2457 }
2458
2459 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002460 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002461 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002462 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002463 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002464 return;
2465 }
Michael Kolbc831b632011-05-11 09:30:34 -07002466 final Tab current = mTabControl.getCurrentTab();
2467 final int pos = mTabControl.getCurrentPosition();
2468 Tab newTab = current.getParent();
2469 if (newTab == null) {
2470 newTab = mTabControl.getTab(pos + 1);
2471 if (newTab == null) {
2472 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002473 }
2474 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002475 if (andQuit) {
2476 mTabControl.setCurrentTab(newTab);
2477 closeTab(current);
2478 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002479 // Close window
2480 closeTab(current);
2481 }
2482 }
2483
2484 /**
2485 * Close the tab, remove its associated title bar, and adjust mTabControl's
2486 * current tab to a valid value.
2487 */
2488 @Override
2489 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002490 if (tab == mTabControl.getCurrentTab()) {
2491 closeCurrentTab();
2492 } else {
2493 removeTab(tab);
2494 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002495 }
2496
Michael Kolb8233fac2010-10-26 16:08:53 -07002497 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002498 protected void loadUrlFromContext(String url) {
2499 Tab tab = getCurrentTab();
2500 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002501 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002502 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002503 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002504 if (!WebViewClassic.fromWebView(view).getWebViewClient().
2505 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002506 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002507 }
2508 }
2509 }
2510
2511 /**
2512 * Load the URL into the given WebView and update the title bar
2513 * to reflect the new load. Call this instead of WebView.loadUrl
2514 * directly.
2515 * @param view The WebView used to load url.
2516 * @param url The URL to load.
2517 */
John Reck71e51422011-07-01 16:49:28 -07002518 @Override
2519 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002520 loadUrl(tab, url, null);
2521 }
2522
2523 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2524 if (tab != null) {
2525 dismissSubWindow(tab);
2526 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002527 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002528 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002529 }
2530
2531 /**
2532 * Load UrlData into a Tab and update the title bar to reflect the new
2533 * load. Call this instead of UrlData.loadIn directly.
2534 * @param t The Tab used to load.
2535 * @param data The UrlData being loaded.
2536 */
2537 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002538 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002539 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002540 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002541 } else {
2542 loadUrl(t, data.mUrl, data.mHeaders);
2543 }
2544 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002545 }
2546
John Reck30c714c2010-12-16 17:30:34 -08002547 @Override
2548 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002549 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002550 if (tab.canGoBack()) {
2551 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002552 } else {
John Reckef654f12011-07-12 16:42:08 -07002553 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002554 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002555 }
2556
2557 void goBackOnePageOrQuit() {
2558 Tab current = mTabControl.getCurrentTab();
2559 if (current == null) {
2560 /*
2561 * Instead of finishing the activity, simply push this to the back
2562 * of the stack and let ActivityManager to choose the foreground
2563 * activity. As BrowserActivity is singleTask, it will be always the
2564 * root of the task. So we can use either true or false for
2565 * moveTaskToBack().
2566 */
2567 mActivity.moveTaskToBack(true);
2568 return;
2569 }
John Reckef654f12011-07-12 16:42:08 -07002570 if (current.canGoBack()) {
2571 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002572 } else {
2573 // Check to see if we are closing a window that was created by
2574 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002575 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002576 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002577 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002578 // Now we close the other tab
2579 closeTab(current);
2580 } else {
Michael Kolbe28b3472011-08-04 16:54:31 -07002581 if ((current.getAppId() != null) || current.closeOnBack()) {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002582 closeCurrentTab(true);
Michael Kolbe28b3472011-08-04 16:54:31 -07002583 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002584 /*
2585 * Instead of finishing the activity, simply push this to the back
2586 * of the stack and let ActivityManager to choose the foreground
2587 * activity. As BrowserActivity is singleTask, it will be always the
2588 * root of the task. So we can use either true or false for
2589 * moveTaskToBack().
2590 */
2591 mActivity.moveTaskToBack(true);
2592 }
2593 }
2594 }
2595
2596 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002597 * helper method for key handler
2598 * returns the current tab if it can't advance
2599 */
Michael Kolbc831b632011-05-11 09:30:34 -07002600 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002601 int pos = mTabControl.getCurrentPosition() + 1;
2602 if (pos >= mTabControl.getTabCount()) {
2603 pos = 0;
2604 }
2605 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002606 }
2607
2608 /**
2609 * helper method for key handler
2610 * returns the current tab if it can't advance
2611 */
Michael Kolbc831b632011-05-11 09:30:34 -07002612 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002613 int pos = mTabControl.getCurrentPosition() - 1;
2614 if ( pos < 0) {
2615 pos = mTabControl.getTabCount() - 1;
2616 }
2617 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002618 }
2619
John Reckbcef87f2012-02-03 14:58:34 -08002620 boolean isMenuOrCtrlKey(int keyCode) {
2621 return (KeyEvent.KEYCODE_MENU == keyCode)
2622 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2623 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2624 }
2625
Michael Kolb0035fad2011-03-14 13:25:28 -07002626 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002627 * handle key events in browser
2628 *
2629 * @param keyCode
2630 * @param event
2631 * @return true if handled, false to pass to super
2632 */
John Reck9c35b9c2012-05-30 10:08:50 -07002633 @Override
2634 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002635 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002636 // Even if MENU is already held down, we need to call to super to open
2637 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002638 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002639 mMenuIsDown = true;
2640 return false;
2641 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002642
Cary Clark8ff8c662010-12-29 15:03:05 -05002643 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002644 Tab tab = getCurrentTab();
2645 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002646
Cary Clark160bbb92011-01-10 11:17:07 -05002647 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2648 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002649
Michael Kolb8233fac2010-10-26 16:08:53 -07002650 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002651 case KeyEvent.KEYCODE_TAB:
2652 if (event.isCtrlPressed()) {
2653 if (event.isShiftPressed()) {
2654 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002655 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002656 } else {
2657 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002658 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002659 }
2660 return true;
2661 }
2662 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002663 case KeyEvent.KEYCODE_SPACE:
2664 // WebView/WebTextView handle the keys in the KeyDown. As
2665 // the Activity's shortcut keys are only handled when WebView
2666 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002667 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002668 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002669 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002670 pageDown();
2671 }
2672 return true;
2673 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002674 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002675 event.startTracking();
2676 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002677 case KeyEvent.KEYCODE_FORWARD:
2678 if (!noModifiers) break;
2679 tab.goForward();
2680 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002681 case KeyEvent.KEYCODE_DPAD_LEFT:
2682 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002683 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002684 return true;
2685 }
2686 break;
2687 case KeyEvent.KEYCODE_DPAD_RIGHT:
2688 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002689 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002690 return true;
2691 }
2692 break;
2693 case KeyEvent.KEYCODE_A:
2694 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002695 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002696 return true;
2697 }
2698 break;
Michael Kolba4183062011-01-16 10:43:21 -08002699// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002700 case KeyEvent.KEYCODE_C:
2701 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002702 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002703 return true;
2704 }
2705 break;
Michael Kolba4183062011-01-16 10:43:21 -08002706// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002707// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002708// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002709// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002710// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002711// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002712// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002713// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002714// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002715// case KeyEvent.KEYCODE_M: // unused
2716// case KeyEvent.KEYCODE_N: // in Chrome: new window
2717// case KeyEvent.KEYCODE_O: // in Chrome: open file
2718// case KeyEvent.KEYCODE_P: // in Chrome: print page
2719// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002720// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002721// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2722 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002723 // we can't use the ctrl/shift flags, they check for
2724 // exclusive use of a modifier
2725 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002726 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002727 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002728 } else {
2729 openTabToHomePage();
2730 }
2731 return true;
2732 }
2733 break;
2734// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2735// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002736// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002737// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2738// case KeyEvent.KEYCODE_Y: // unused
2739// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002740 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002741 // it is a regular key and webview is not null
2742 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002743 }
2744
John Reck9c35b9c2012-05-30 10:08:50 -07002745 @Override
2746 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002747 switch(keyCode) {
2748 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002749 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002750 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002751 return true;
2752 }
2753 break;
2754 }
2755 return false;
2756 }
2757
John Reck9c35b9c2012-05-30 10:08:50 -07002758 @Override
2759 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002760 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002761 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002762 if (KeyEvent.KEYCODE_MENU == keyCode
2763 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002764 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002765 }
2766 }
Cary Clark160bbb92011-01-10 11:17:07 -05002767 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002768 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002769 case KeyEvent.KEYCODE_BACK:
2770 if (event.isTracking() && !event.isCanceled()) {
2771 onBackKey();
2772 return true;
2773 }
2774 break;
2775 }
2776 return false;
2777 }
2778
2779 public boolean isMenuDown() {
2780 return mMenuIsDown;
2781 }
2782
John Reck9c35b9c2012-05-30 10:08:50 -07002783 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002784 public void setupAutoFill(Message message) {
2785 // Open the settings activity at the AutoFill profile fragment so that
2786 // the user can create a new profile. When they return, we will dispatch
2787 // the message so that we can autofill the form using their new profile.
2788 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2789 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2790 AutoFillSettingsFragment.class.getName());
2791 mAutoFillSetupMessage = message;
2792 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2793 }
John Reckb3417f02011-01-14 11:01:05 -08002794
John Reck9c35b9c2012-05-30 10:08:50 -07002795 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002796 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002797 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002798 return true;
2799 }
2800
John Reck1cf4b792011-07-26 10:22:22 -07002801 @Override
2802 public boolean shouldCaptureThumbnails() {
2803 return mUi.shouldCaptureThumbnails();
2804 }
2805
Michael Kolbc3af0672011-08-09 10:24:41 -07002806 @Override
2807 public void setBlockEvents(boolean block) {
2808 mBlockEvents = block;
2809 }
2810
John Reck9c35b9c2012-05-30 10:08:50 -07002811 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002812 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002813 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002814 }
2815
John Reck9c35b9c2012-05-30 10:08:50 -07002816 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002817 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002818 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002819 }
2820
John Reck9c35b9c2012-05-30 10:08:50 -07002821 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002822 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002823 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002824 }
2825
John Reck9c35b9c2012-05-30 10:08:50 -07002826 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002827 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002828 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002829 }
2830
John Reck9c35b9c2012-05-30 10:08:50 -07002831 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002832 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002833 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002834 }
2835
Michael Kolb8233fac2010-10-26 16:08:53 -07002836}