blob: d7331601e865960b38225f3767d036f407df87a3 [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
Afzal Najamd4e33312012-04-26 01:54:01 -04001565 case R.id.close_other_tabs_id:
1566 closeOtherTabs();
1567 break;
1568
Michael Kolb8233fac2010-10-26 16:08:53 -07001569 case R.id.goto_menu_id:
1570 editUrl();
1571 break;
1572
1573 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001574 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1575 break;
1576
1577 case R.id.history_menu_id:
1578 bookmarksOrHistoryPicker(ComboViews.History);
1579 break;
1580
1581 case R.id.snapshots_menu_id:
1582 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001583 break;
1584
Michael Kolb8233fac2010-10-26 16:08:53 -07001585 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001586 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001587 break;
1588
1589 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001590 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001591 stopLoading();
1592 } else {
1593 getCurrentTopWebView().reload();
1594 }
1595 break;
1596
1597 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001598 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001599 break;
1600
1601 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001602 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001603 break;
1604
1605 case R.id.close_menu_id:
1606 // Close the subwindow if it exists.
1607 if (mTabControl.getCurrentSubWindow() != null) {
1608 dismissSubWindow(mTabControl.getCurrentTab());
1609 break;
1610 }
1611 closeCurrentTab();
1612 break;
1613
1614 case R.id.homepage_menu_id:
1615 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001616 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001617 break;
1618
1619 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001620 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001621 break;
1622
1623 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001624 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001625 break;
1626
John Reck2bc80422011-06-30 15:11:49 -07001627 case R.id.save_snapshot_menu_id:
1628 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001629 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001630 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001631 break;
1632
Michael Kolb8233fac2010-10-26 16:08:53 -07001633 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001634 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001635 break;
1636
John Recke1a03a32011-09-14 17:04:16 -07001637 case R.id.snapshot_go_live:
1638 goLive();
1639 return true;
1640
Michael Kolb8233fac2010-10-26 16:08:53 -07001641 case R.id.share_page_menu_id:
1642 Tab currentTab = mTabControl.getCurrentTab();
1643 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001644 return false;
1645 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001646 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001647 break;
1648
1649 case R.id.dump_nav_menu_id:
1650 getCurrentTopWebView().debugDump();
1651 break;
1652
Michael Kolb8233fac2010-10-26 16:08:53 -07001653 case R.id.zoom_in_menu_id:
1654 getCurrentTopWebView().zoomIn();
1655 break;
1656
1657 case R.id.zoom_out_menu_id:
1658 getCurrentTopWebView().zoomOut();
1659 break;
1660
1661 case R.id.view_downloads_menu_id:
1662 viewDownloads();
1663 break;
1664
John Reck42229bc2011-08-19 13:26:43 -07001665 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001666 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07001667 break;
1668
Michael Kolb8233fac2010-10-26 16:08:53 -07001669 case R.id.window_one_menu_id:
1670 case R.id.window_two_menu_id:
1671 case R.id.window_three_menu_id:
1672 case R.id.window_four_menu_id:
1673 case R.id.window_five_menu_id:
1674 case R.id.window_six_menu_id:
1675 case R.id.window_seven_menu_id:
1676 case R.id.window_eight_menu_id:
1677 {
1678 int menuid = item.getItemId();
1679 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1680 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1681 Tab desiredTab = mTabControl.getTab(id);
1682 if (desiredTab != null &&
1683 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001684 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001685 }
1686 break;
1687 }
1688 }
1689 }
1690 break;
1691
1692 default:
1693 return false;
1694 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001695 return true;
1696 }
1697
John Reck68234a92012-04-19 15:27:12 -07001698 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
1699 implements OnCancelListener {
1700
1701 private Tab mTab;
1702 private Dialog mProgressDialog;
1703 private ContentValues mValues;
1704
1705 private SaveSnapshotTask(Tab tab) {
1706 mTab = tab;
1707 }
1708
1709 @Override
1710 protected void onPreExecute() {
1711 CharSequence message = mActivity.getText(R.string.saving_snapshot);
1712 mProgressDialog = ProgressDialog.show(mActivity, null, message,
1713 true, true, this);
1714 mValues = mTab.createSnapshotValues();
1715 }
1716
1717 @Override
1718 protected Long doInBackground(Void... params) {
1719 if (!mTab.saveViewState(mValues)) {
1720 return null;
1721 }
1722 if (isCancelled()) {
1723 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
1724 File file = mActivity.getFileStreamPath(path);
1725 if (!file.delete()) {
1726 file.deleteOnExit();
1727 }
1728 return null;
1729 }
1730 final ContentResolver cr = mActivity.getContentResolver();
1731 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
1732 if (result == null) {
1733 return null;
1734 }
1735 long id = ContentUris.parseId(result);
1736 return id;
1737 }
1738
1739 @Override
1740 protected void onPostExecute(Long id) {
1741 if (isCancelled()) {
1742 return;
1743 }
1744 mProgressDialog.dismiss();
1745 if (id == null) {
1746 Toast.makeText(mActivity, R.string.snapshot_failed,
1747 Toast.LENGTH_SHORT).show();
1748 return;
1749 }
1750 Bundle b = new Bundle();
1751 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1752 mUi.showComboView(ComboViews.Snapshots, b);
1753 }
1754
1755 @Override
1756 public void onCancel(DialogInterface dialog) {
1757 cancel(true);
1758 }
1759 }
1760
Michael Kolb80f75082012-04-10 10:50:06 -07001761 @Override
1762 public void toggleUserAgent() {
1763 WebView web = getCurrentWebView();
1764 mSettings.toggleDesktopUseragent(web);
1765 web.loadUrl(web.getOriginalUrl());
1766 }
1767
1768 @Override
1769 public void findOnPage() {
1770 getCurrentTopWebView().showFindDialog(null, true);
1771 }
1772
1773 @Override
1774 public void openPreferences() {
1775 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1776 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1777 getCurrentTopWebView().getUrl());
1778 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1779 }
1780
1781 @Override
1782 public void bookmarkCurrentPage() {
1783 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
1784 if (bookmarkIntent != null) {
1785 mActivity.startActivity(bookmarkIntent);
1786 }
1787 }
1788
John Recke1a03a32011-09-14 17:04:16 -07001789 private void goLive() {
1790 Tab t = getCurrentTab();
1791 t.loadUrl(t.getUrl(), null);
1792 }
1793
Michael Kolb315d5022011-10-13 12:47:11 -07001794 @Override
1795 public void showPageInfo() {
1796 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
1797 }
1798
John Reck9c35b9c2012-05-30 10:08:50 -07001799 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001800 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001801 // Let the History and Bookmark fragments handle menus they created.
1802 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1803 return false;
1804 }
1805
Michael Kolb8233fac2010-10-26 16:08:53 -07001806 int id = item.getItemId();
1807 boolean result = true;
1808 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001809 // -- Browser context menu
1810 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001811 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001812 case R.id.copy_link_context_menu_id:
1813 final WebView webView = getCurrentTopWebView();
1814 if (null == webView) {
1815 result = false;
1816 break;
1817 }
1818 final HashMap<String, WebView> hrefMap =
1819 new HashMap<String, WebView>();
1820 hrefMap.put("webview", webView);
1821 final Message msg = mHandler.obtainMessage(
1822 FOCUS_NODE_HREF, id, 0, hrefMap);
1823 webView.requestFocusNodeHref(msg);
1824 break;
1825
1826 default:
1827 // For other context menus
1828 result = onOptionsItemSelected(item);
1829 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001830 return result;
1831 }
1832
1833 /**
1834 * support programmatically opening the context menu
1835 */
1836 public void openContextMenu(View view) {
1837 mActivity.openContextMenu(view);
1838 }
1839
1840 /**
1841 * programmatically open the options menu
1842 */
1843 public void openOptionsMenu() {
1844 mActivity.openOptionsMenu();
1845 }
1846
John Reck9c35b9c2012-05-30 10:08:50 -07001847 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001848 public boolean onMenuOpened(int featureId, Menu menu) {
1849 if (mOptionsMenuOpen) {
1850 if (mConfigChanged) {
1851 // We do not need to make any changes to the state of the
1852 // title bar, since the only thing that happened was a
1853 // change in orientation
1854 mConfigChanged = false;
1855 } else {
1856 if (!mExtendedMenuOpen) {
1857 mExtendedMenuOpen = true;
1858 mUi.onExtendedMenuOpened();
1859 } else {
1860 // Switching the menu back to icon view, so show the
1861 // title bar once again.
1862 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001863 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001864 }
1865 }
1866 } else {
1867 // The options menu is closed, so open it, and show the title
1868 mOptionsMenuOpen = true;
1869 mConfigChanged = false;
1870 mExtendedMenuOpen = false;
1871 mUi.onOptionsMenuOpened();
1872 }
1873 return true;
1874 }
1875
John Reck9c35b9c2012-05-30 10:08:50 -07001876 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001877 public void onOptionsMenuClosed(Menu menu) {
1878 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001879 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001880 }
1881
John Reck9c35b9c2012-05-30 10:08:50 -07001882 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001883 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001884 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001885 }
1886
1887 // Helper method for getting the top window.
1888 @Override
1889 public WebView getCurrentTopWebView() {
1890 return mTabControl.getCurrentTopWebView();
1891 }
1892
1893 @Override
1894 public WebView getCurrentWebView() {
1895 return mTabControl.getCurrentWebView();
1896 }
1897
1898 /*
1899 * This method is called as a result of the user selecting the options
1900 * menu to see the download window. It shows the download window on top of
1901 * the current window.
1902 */
1903 void viewDownloads() {
1904 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1905 mActivity.startActivity(intent);
1906 }
1907
John Reck30b065e2011-07-19 10:58:05 -07001908 int getActionModeHeight() {
1909 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1910 new int[] { android.R.attr.actionBarSize });
1911 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1912 actionBarSizeTypedArray.recycle();
1913 return size;
1914 }
1915
Michael Kolb8233fac2010-10-26 16:08:53 -07001916 // action mode
1917
John Reck9c35b9c2012-05-30 10:08:50 -07001918 @Override
1919 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001920 mUi.onActionModeStarted(mode);
1921 mActionMode = mode;
1922 }
1923
1924 /*
1925 * True if a custom ActionMode (i.e. find or select) is in use.
1926 */
1927 @Override
1928 public boolean isInCustomActionMode() {
1929 return mActionMode != null;
1930 }
1931
1932 /*
1933 * End the current ActionMode.
1934 */
1935 @Override
1936 public void endActionMode() {
1937 if (mActionMode != null) {
1938 mActionMode.finish();
1939 }
1940 }
1941
1942 /*
1943 * Called by find and select when they are finished. Replace title bars
1944 * as necessary.
1945 */
John Reck9c35b9c2012-05-30 10:08:50 -07001946 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001947 public void onActionModeFinished(ActionMode mode) {
1948 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001949 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001950 mActionMode = null;
1951 }
1952
1953 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08001954 final Tab tab = getCurrentTab();
1955 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07001956 }
1957
1958 // bookmark handling
1959
1960 /**
1961 * add the current page as a bookmark to the given folder id
1962 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07001963 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001964 * bookmarked, and if it is, edit that bookmark. If false, and
1965 * the site is already bookmarked, do not attempt to edit the
1966 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001967 */
1968 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07001969 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07001970 WebView w = getCurrentTopWebView();
1971 if (w == null) {
1972 return null;
1973 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001974 Intent i = new Intent(mActivity,
1975 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07001976 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1977 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1978 String touchIconUrl = w.getTouchIconUrl();
1979 if (touchIconUrl != null) {
1980 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1981 WebSettings settings = w.getSettings();
1982 if (settings != null) {
1983 i.putExtra(AddBookmarkPage.USER_AGENT,
1984 settings.getUserAgentString());
1985 }
1986 }
1987 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
1988 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
1989 getDesiredThumbnailHeight(mActivity)));
1990 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07001991 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001992 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
1993 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001994 // Put the dialog at the upper right of the screen, covering the
1995 // star on the title bar.
1996 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07001997 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07001998 }
1999
2000 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002001 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002002 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002003 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002004 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002005 }
2006
2007 // thumbnails
2008
2009 /**
2010 * Return the desired width for thumbnail screenshots, which are stored in
2011 * the database, and used on the bookmarks screen.
2012 * @param context Context for finding out the density of the screen.
2013 * @return desired width for thumbnail screenshot.
2014 */
2015 static int getDesiredThumbnailWidth(Context context) {
2016 return context.getResources().getDimensionPixelOffset(
2017 R.dimen.bookmarkThumbnailWidth);
2018 }
2019
2020 /**
2021 * Return the desired height for thumbnail screenshots, which are stored in
2022 * the database, and used on the bookmarks screen.
2023 * @param context Context for finding out the density of the screen.
2024 * @return desired height for thumbnail screenshot.
2025 */
2026 static int getDesiredThumbnailHeight(Context context) {
2027 return context.getResources().getDimensionPixelOffset(
2028 R.dimen.bookmarkThumbnailHeight);
2029 }
2030
John Reck8cc92352011-07-06 17:41:52 -07002031 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002032 if (view == null || view.getContentHeight() == 0
2033 || view.getContentWidth() == 0) {
2034 return null;
2035 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002036 // We render to a bitmap 2x the desired size so that we can then
2037 // re-scale it with filtering since canvas.scale doesn't filter
2038 // This helps reduce aliasing at the cost of being slightly blurry
2039 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002040 int scaledWidth = width * filter_scale;
2041 int scaledHeight = height * filter_scale;
2042 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2043 || sThumbnailBitmap.getHeight() != scaledHeight) {
2044 if (sThumbnailBitmap != null) {
2045 sThumbnailBitmap.recycle();
2046 sThumbnailBitmap = null;
2047 }
2048 sThumbnailBitmap =
2049 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002050 }
John Reckd7dd9b22011-08-30 09:18:29 -07002051 Canvas canvas = new Canvas(sThumbnailBitmap);
2052 int contentWidth = view.getContentWidth();
2053 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2054 if (view instanceof BrowserWebView) {
2055 int dy = -((BrowserWebView)view).getTitleHeight();
2056 canvas.translate(0, dy * overviewScale);
2057 }
2058
2059 canvas.scale(overviewScale, overviewScale);
2060
2061 if (view instanceof BrowserWebView) {
2062 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002063 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002064 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002065 }
John Reckd7dd9b22011-08-30 09:18:29 -07002066 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2067 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002068 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002069 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002070 }
2071
John Reck34ef2672011-02-10 11:30:55 -08002072 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002073 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002074 // FIXME: Would like to make sure there is actually something to
2075 // draw, but the API for that (WebViewCore.pictureReady()) is not
2076 // currently accessible here.
2077
John Reck34ef2672011-02-10 11:30:55 -08002078 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002079 if (view == null) {
2080 // Tab was destroyed
2081 return;
2082 }
John Reck34ef2672011-02-10 11:30:55 -08002083 final String url = tab.getUrl();
2084 final String originalUrl = view.getOriginalUrl();
John Reck34ef2672011-02-10 11:30:55 -08002085 if (TextUtils.isEmpty(url)) {
2086 return;
2087 }
2088
2089 // Only update thumbnails for web urls (http(s)://), not for
2090 // about:, javascript:, data:, etc...
2091 // Unless it is a bookmarked site, then always update
2092 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2093 return;
2094 }
2095
Michael Kolb8233fac2010-10-26 16:08:53 -07002096 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2097 getDesiredThumbnailHeight(mActivity));
2098 if (bm == null) {
2099 return;
2100 }
2101
2102 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002103 new AsyncTask<Void, Void, Void>() {
2104 @Override
2105 protected Void doInBackground(Void... unused) {
2106 Cursor cursor = null;
2107 try {
2108 // TODO: Clean this up
2109 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2110 if (cursor != null && cursor.moveToFirst()) {
2111 final ByteArrayOutputStream os =
2112 new ByteArrayOutputStream();
2113 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002114
John Reck34ef2672011-02-10 11:30:55 -08002115 ContentValues values = new ContentValues();
2116 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002117
John Reck34ef2672011-02-10 11:30:55 -08002118 do {
John Reck617fd832011-02-16 14:35:59 -08002119 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002120 cr.update(Images.CONTENT_URI, values, null, null);
2121 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002122 }
John Reck34ef2672011-02-10 11:30:55 -08002123 } catch (IllegalStateException e) {
2124 // Ignore
2125 } finally {
2126 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002127 }
John Reck34ef2672011-02-10 11:30:55 -08002128 return null;
2129 }
2130 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002131 }
2132
2133 private class Copy implements OnMenuItemClickListener {
2134 private CharSequence mText;
2135
John Reck9c35b9c2012-05-30 10:08:50 -07002136 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002137 public boolean onMenuItemClick(MenuItem item) {
2138 copy(mText);
2139 return true;
2140 }
2141
2142 public Copy(CharSequence toCopy) {
2143 mText = toCopy;
2144 }
2145 }
2146
Leon Scroggins63c02662010-11-18 15:16:27 -05002147 private static class Download implements OnMenuItemClickListener {
2148 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002149 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002150 private boolean mPrivateBrowsing;
George Mount387d45d2011-10-07 15:57:53 -07002151 private static final String FALLBACK_EXTENSION = "dat";
2152 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002153
John Reck9c35b9c2012-05-30 10:08:50 -07002154 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002155 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002156 if (DataUri.isDataUri(mText)) {
2157 saveDataUri();
2158 } else {
2159 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
2160 null, null, mPrivateBrowsing);
2161 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002162 return true;
2163 }
2164
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002165 public Download(Activity activity, String toDownload, boolean privateBrowsing) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002166 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002167 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002168 mPrivateBrowsing = privateBrowsing;
Michael Kolb8233fac2010-10-26 16:08:53 -07002169 }
George Mount387d45d2011-10-07 15:57:53 -07002170
2171 /**
2172 * Treats mText as a data URI and writes its contents to a file
2173 * based on the current time.
2174 */
2175 private void saveDataUri() {
2176 FileOutputStream outputStream = null;
2177 try {
2178 DataUri uri = new DataUri(mText);
2179 File target = getTarget(uri);
2180 outputStream = new FileOutputStream(target);
2181 outputStream.write(uri.getData());
2182 final DownloadManager manager =
2183 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2184 manager.addCompletedDownload(target.getName(),
2185 mActivity.getTitle().toString(), false,
2186 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002187 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002188 } catch (IOException e) {
2189 Log.e(LOGTAG, "Could not save data URL");
2190 } finally {
2191 if (outputStream != null) {
2192 try {
2193 outputStream.close();
2194 } catch (IOException e) {
2195 // ignore close errors
2196 }
2197 }
2198 }
2199 }
2200
2201 /**
2202 * Creates a File based on the current time stamp and uses
2203 * the mime type of the DataUri to get the extension.
2204 */
2205 private File getTarget(DataUri uri) throws IOException {
2206 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
2207 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT);
2208 String nameBase = format.format(new Date());
2209 String mimeType = uri.getMimeType();
2210 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2211 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2212 if (extension == null) {
2213 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2214 extension = FALLBACK_EXTENSION;
2215 }
2216 extension = "." + extension; // createTempFile needs the '.'
2217 File targetFile = File.createTempFile(nameBase, extension, dir);
2218 return targetFile;
2219 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002220 }
2221
Cary Clark8974d282010-11-22 10:46:05 -05002222 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002223 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002224
John Reck9c35b9c2012-05-30 10:08:50 -07002225 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002226 public boolean onMenuItemClick(MenuItem item) {
2227 if (mWebView != null) {
2228 return mWebView.selectText();
2229 }
2230 return false;
2231 }
2232
2233 public SelectText(WebView webView) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002234 mWebView = WebViewClassic.fromWebView(webView);
Cary Clark8974d282010-11-22 10:46:05 -05002235 }
2236
2237 }
2238
Michael Kolb8233fac2010-10-26 16:08:53 -07002239 /********************** TODO: UI stuff *****************************/
2240
2241 // these methods have been copied, they still need to be cleaned up
2242
2243 /****************** tabs ***************************************************/
2244
2245 // basic tab interactions:
2246
2247 // it is assumed that tabcontrol already knows about the tab
2248 protected void addTab(Tab tab) {
2249 mUi.addTab(tab);
2250 }
2251
2252 protected void removeTab(Tab tab) {
2253 mUi.removeTab(tab);
2254 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002255 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002256 }
2257
Michael Kolbf2055602011-04-09 17:20:03 -07002258 @Override
2259 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002260 // monkey protection against delayed start
2261 if (tab != null) {
2262 mTabControl.setCurrentTab(tab);
2263 // the tab is guaranteed to have a webview after setCurrentTab
2264 mUi.setActiveTab(tab);
2265 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002266 }
2267
John Reck8bcafc12011-08-29 16:43:02 -07002268 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002269 Tab current = mTabControl.getCurrentTab();
2270 if (current != null
2271 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002272 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002273 }
2274 }
2275
John Reck26b18322011-06-21 13:08:58 -07002276 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002277 // Dismiss the subwindow if applicable.
2278 dismissSubWindow(appTab);
2279 // Since we might kill the WebView, remove it from the
2280 // content view first.
2281 mUi.detachTab(appTab);
2282 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002283 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002284 // TODO: analyze why the remove and add are necessary
2285 mUi.attachTab(appTab);
2286 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002287 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002288 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002289 } else {
2290 // If the tab was the current tab, we have to attach
2291 // it to the view system again.
2292 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002293 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002294 }
2295 }
2296
2297 // Remove the sub window if it exists. Also called by TabControl when the
2298 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002299 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002300 public void dismissSubWindow(Tab tab) {
2301 removeSubWindow(tab);
2302 // dismiss the subwindow. This will destroy the WebView.
2303 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002304 WebView wv = getCurrentTopWebView();
2305 if (wv != null) {
2306 wv.requestFocus();
2307 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002308 }
2309
2310 @Override
2311 public void removeSubWindow(Tab t) {
2312 if (t.getSubWebView() != null) {
2313 mUi.removeSubWindow(t.getSubViewContainer());
2314 }
2315 }
2316
2317 @Override
2318 public void attachSubWindow(Tab tab) {
2319 if (tab.getSubWebView() != null) {
2320 mUi.attachSubWindow(tab.getSubViewContainer());
2321 getCurrentTopWebView().requestFocus();
2322 }
2323 }
2324
Mathew Inwood29721c22011-06-29 17:55:24 +01002325 private Tab showPreloadedTab(final UrlData urlData) {
2326 if (!urlData.isPreloaded()) {
2327 return null;
2328 }
2329 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2330 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2331 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002332 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002333 // Could not submit query. Fallback to regular tab creation
2334 tabControl.destroy();
2335 return null;
2336 }
2337 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002338 // check tab count and make room for new tab
2339 if (!mTabControl.canCreateNewTab()) {
2340 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2341 if (leastUsed != null) {
2342 closeTab(leastUsed);
2343 }
2344 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002345 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002346 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002347 mTabControl.addPreloadedTab(t);
2348 addTab(t);
2349 setActiveTab(t);
2350 return t;
2351 }
2352
Michael Kolb7bcafde2011-05-09 13:55:59 -07002353 // open a non inconito tab with the given url data
2354 // and set as active tab
2355 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002356 Tab tab = showPreloadedTab(urlData);
2357 if (tab == null) {
2358 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002359 if ((tab != null) && !urlData.isEmpty()) {
2360 loadUrlDataIn(tab, urlData);
2361 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002362 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002363 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002364 }
2365
Michael Kolb843510f2010-12-09 10:51:49 -08002366 @Override
2367 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002368 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002369 }
2370
Michael Kolb8233fac2010-10-26 16:08:53 -07002371 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002372 public Tab openIncognitoTab() {
2373 return openTab(INCOGNITO_URI, true, true, false);
2374 }
2375
2376 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002377 public Tab openTab(String url, boolean incognito, boolean setActive,
2378 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002379 return openTab(url, incognito, setActive, useCurrent, null);
2380 }
2381
2382 @Override
2383 public Tab openTab(String url, Tab parent, boolean setActive,
2384 boolean useCurrent) {
2385 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2386 setActive, useCurrent, parent);
2387 }
2388
2389 public Tab openTab(String url, boolean incognito, boolean setActive,
2390 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002391 Tab tab = createNewTab(incognito, setActive, useCurrent);
2392 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002393 if (parent != null && parent != tab) {
2394 parent.addChildTab(tab);
2395 }
Michael Kolb519d2282011-05-09 17:03:19 -07002396 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002397 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002398 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002399 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002400 return tab;
2401 }
2402
2403 // this method will attempt to create a new tab
2404 // incognito: private browsing tab
2405 // setActive: ste tab as current tab
2406 // useCurrent: if no new tab can be created, return current tab
2407 private Tab createNewTab(boolean incognito, boolean setActive,
2408 boolean useCurrent) {
2409 Tab tab = null;
2410 if (mTabControl.canCreateNewTab()) {
2411 tab = mTabControl.createNewTab(incognito);
2412 addTab(tab);
2413 if (setActive) {
2414 setActiveTab(tab);
2415 }
2416 } else {
2417 if (useCurrent) {
2418 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002419 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002420 } else {
2421 mUi.showMaxTabsWarning();
2422 }
2423 }
2424 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002425 }
2426
John Reck2bc80422011-06-30 15:11:49 -07002427 @Override
2428 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2429 SnapshotTab tab = null;
2430 if (mTabControl.canCreateNewTab()) {
2431 tab = mTabControl.createSnapshotTab(snapshotId);
2432 addTab(tab);
2433 if (setActive) {
2434 setActiveTab(tab);
2435 }
2436 } else {
2437 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002438 }
2439 return tab;
2440 }
2441
Michael Kolb8233fac2010-10-26 16:08:53 -07002442 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002443 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002444 * @return boolean True if we successfully switched to a different tab. If
2445 * the indexth tab is null, or if that tab is the same as
2446 * the current one, return false.
2447 */
2448 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002449 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002450 Tab currentTab = mTabControl.getCurrentTab();
2451 if (tab == null || tab == currentTab) {
2452 return false;
2453 }
2454 setActiveTab(tab);
2455 return true;
2456 }
2457
2458 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002459 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002460 closeCurrentTab(false);
2461 }
2462
2463 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002464 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002465 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002466 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002467 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002468 return;
2469 }
Michael Kolbc831b632011-05-11 09:30:34 -07002470 final Tab current = mTabControl.getCurrentTab();
2471 final int pos = mTabControl.getCurrentPosition();
2472 Tab newTab = current.getParent();
2473 if (newTab == null) {
2474 newTab = mTabControl.getTab(pos + 1);
2475 if (newTab == null) {
2476 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002477 }
2478 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002479 if (andQuit) {
2480 mTabControl.setCurrentTab(newTab);
2481 closeTab(current);
2482 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002483 // Close window
2484 closeTab(current);
2485 }
2486 }
2487
2488 /**
2489 * Close the tab, remove its associated title bar, and adjust mTabControl's
2490 * current tab to a valid value.
2491 */
2492 @Override
2493 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002494 if (tab == mTabControl.getCurrentTab()) {
2495 closeCurrentTab();
2496 } else {
2497 removeTab(tab);
2498 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002499 }
2500
Afzal Najamd4e33312012-04-26 01:54:01 -04002501 /**
2502 * Close all tabs except the current one
2503 */
2504 @Override
2505 public void closeOtherTabs() {
2506 int inactiveTabs = mTabControl.getTabCount() - 1;
2507 for (int i = inactiveTabs; i >= 0; i--) {
2508 Tab tab = mTabControl.getTab(i);
2509 if (tab != mTabControl.getCurrentTab()) {
2510 removeTab(tab);
2511 }
2512 }
2513 }
2514
Michael Kolb8233fac2010-10-26 16:08:53 -07002515 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002516 protected void loadUrlFromContext(String url) {
2517 Tab tab = getCurrentTab();
2518 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002519 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002520 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002521 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002522 if (!WebViewClassic.fromWebView(view).getWebViewClient().
2523 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002524 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002525 }
2526 }
2527 }
2528
2529 /**
2530 * Load the URL into the given WebView and update the title bar
2531 * to reflect the new load. Call this instead of WebView.loadUrl
2532 * directly.
2533 * @param view The WebView used to load url.
2534 * @param url The URL to load.
2535 */
John Reck71e51422011-07-01 16:49:28 -07002536 @Override
2537 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002538 loadUrl(tab, url, null);
2539 }
2540
2541 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2542 if (tab != null) {
2543 dismissSubWindow(tab);
2544 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002545 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002546 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002547 }
2548
2549 /**
2550 * Load UrlData into a Tab and update the title bar to reflect the new
2551 * load. Call this instead of UrlData.loadIn directly.
2552 * @param t The Tab used to load.
2553 * @param data The UrlData being loaded.
2554 */
2555 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002556 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002557 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002558 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002559 } else {
2560 loadUrl(t, data.mUrl, data.mHeaders);
2561 }
2562 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002563 }
2564
John Reck30c714c2010-12-16 17:30:34 -08002565 @Override
2566 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002567 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002568 if (tab.canGoBack()) {
2569 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002570 } else {
John Reckef654f12011-07-12 16:42:08 -07002571 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002572 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002573 }
2574
2575 void goBackOnePageOrQuit() {
2576 Tab current = mTabControl.getCurrentTab();
2577 if (current == null) {
2578 /*
2579 * Instead of finishing the activity, simply push this to the back
2580 * of the stack and let ActivityManager to choose the foreground
2581 * activity. As BrowserActivity is singleTask, it will be always the
2582 * root of the task. So we can use either true or false for
2583 * moveTaskToBack().
2584 */
2585 mActivity.moveTaskToBack(true);
2586 return;
2587 }
John Reckef654f12011-07-12 16:42:08 -07002588 if (current.canGoBack()) {
2589 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002590 } else {
2591 // Check to see if we are closing a window that was created by
2592 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002593 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002594 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002595 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002596 // Now we close the other tab
2597 closeTab(current);
2598 } else {
Michael Kolbe28b3472011-08-04 16:54:31 -07002599 if ((current.getAppId() != null) || current.closeOnBack()) {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002600 closeCurrentTab(true);
Michael Kolbe28b3472011-08-04 16:54:31 -07002601 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002602 /*
2603 * Instead of finishing the activity, simply push this to the back
2604 * of the stack and let ActivityManager to choose the foreground
2605 * activity. As BrowserActivity is singleTask, it will be always the
2606 * root of the task. So we can use either true or false for
2607 * moveTaskToBack().
2608 */
2609 mActivity.moveTaskToBack(true);
2610 }
2611 }
2612 }
2613
2614 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002615 * helper method for key handler
2616 * returns the current tab if it can't advance
2617 */
Michael Kolbc831b632011-05-11 09:30:34 -07002618 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002619 int pos = mTabControl.getCurrentPosition() + 1;
2620 if (pos >= mTabControl.getTabCount()) {
2621 pos = 0;
2622 }
2623 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002624 }
2625
2626 /**
2627 * helper method for key handler
2628 * returns the current tab if it can't advance
2629 */
Michael Kolbc831b632011-05-11 09:30:34 -07002630 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002631 int pos = mTabControl.getCurrentPosition() - 1;
2632 if ( pos < 0) {
2633 pos = mTabControl.getTabCount() - 1;
2634 }
2635 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002636 }
2637
John Reckbcef87f2012-02-03 14:58:34 -08002638 boolean isMenuOrCtrlKey(int keyCode) {
2639 return (KeyEvent.KEYCODE_MENU == keyCode)
2640 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2641 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2642 }
2643
Michael Kolb0035fad2011-03-14 13:25:28 -07002644 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002645 * handle key events in browser
2646 *
2647 * @param keyCode
2648 * @param event
2649 * @return true if handled, false to pass to super
2650 */
John Reck9c35b9c2012-05-30 10:08:50 -07002651 @Override
2652 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002653 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002654 // Even if MENU is already held down, we need to call to super to open
2655 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002656 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002657 mMenuIsDown = true;
2658 return false;
2659 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002660
Cary Clark8ff8c662010-12-29 15:03:05 -05002661 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002662 Tab tab = getCurrentTab();
2663 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002664
Cary Clark160bbb92011-01-10 11:17:07 -05002665 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2666 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002667
Michael Kolb8233fac2010-10-26 16:08:53 -07002668 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002669 case KeyEvent.KEYCODE_TAB:
2670 if (event.isCtrlPressed()) {
2671 if (event.isShiftPressed()) {
2672 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002673 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002674 } else {
2675 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002676 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002677 }
2678 return true;
2679 }
2680 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002681 case KeyEvent.KEYCODE_SPACE:
2682 // WebView/WebTextView handle the keys in the KeyDown. As
2683 // the Activity's shortcut keys are only handled when WebView
2684 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002685 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002686 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002687 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002688 pageDown();
2689 }
2690 return true;
2691 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002692 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002693 event.startTracking();
2694 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002695 case KeyEvent.KEYCODE_FORWARD:
2696 if (!noModifiers) break;
2697 tab.goForward();
2698 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002699 case KeyEvent.KEYCODE_DPAD_LEFT:
2700 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002701 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002702 return true;
2703 }
2704 break;
2705 case KeyEvent.KEYCODE_DPAD_RIGHT:
2706 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002707 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002708 return true;
2709 }
2710 break;
2711 case KeyEvent.KEYCODE_A:
2712 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002713 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002714 return true;
2715 }
2716 break;
Michael Kolba4183062011-01-16 10:43:21 -08002717// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002718 case KeyEvent.KEYCODE_C:
2719 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002720 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002721 return true;
2722 }
2723 break;
Michael Kolba4183062011-01-16 10:43:21 -08002724// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002725// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002726// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002727// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002728// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002729// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002730// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002731// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002732// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002733// case KeyEvent.KEYCODE_M: // unused
2734// case KeyEvent.KEYCODE_N: // in Chrome: new window
2735// case KeyEvent.KEYCODE_O: // in Chrome: open file
2736// case KeyEvent.KEYCODE_P: // in Chrome: print page
2737// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002738// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002739// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2740 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002741 // we can't use the ctrl/shift flags, they check for
2742 // exclusive use of a modifier
2743 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002744 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002745 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002746 } else {
2747 openTabToHomePage();
2748 }
2749 return true;
2750 }
2751 break;
2752// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2753// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002754// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002755// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2756// case KeyEvent.KEYCODE_Y: // unused
2757// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002758 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002759 // it is a regular key and webview is not null
2760 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002761 }
2762
John Reck9c35b9c2012-05-30 10:08:50 -07002763 @Override
2764 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002765 switch(keyCode) {
2766 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002767 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002768 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002769 return true;
2770 }
2771 break;
2772 }
2773 return false;
2774 }
2775
John Reck9c35b9c2012-05-30 10:08:50 -07002776 @Override
2777 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002778 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002779 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002780 if (KeyEvent.KEYCODE_MENU == keyCode
2781 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002782 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002783 }
2784 }
Cary Clark160bbb92011-01-10 11:17:07 -05002785 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002786 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002787 case KeyEvent.KEYCODE_BACK:
2788 if (event.isTracking() && !event.isCanceled()) {
2789 onBackKey();
2790 return true;
2791 }
2792 break;
2793 }
2794 return false;
2795 }
2796
2797 public boolean isMenuDown() {
2798 return mMenuIsDown;
2799 }
2800
John Reck9c35b9c2012-05-30 10:08:50 -07002801 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002802 public void setupAutoFill(Message message) {
2803 // Open the settings activity at the AutoFill profile fragment so that
2804 // the user can create a new profile. When they return, we will dispatch
2805 // the message so that we can autofill the form using their new profile.
2806 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2807 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2808 AutoFillSettingsFragment.class.getName());
2809 mAutoFillSetupMessage = message;
2810 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2811 }
John Reckb3417f02011-01-14 11:01:05 -08002812
John Reck9c35b9c2012-05-30 10:08:50 -07002813 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002814 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002815 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002816 return true;
2817 }
2818
John Reck1cf4b792011-07-26 10:22:22 -07002819 @Override
2820 public boolean shouldCaptureThumbnails() {
2821 return mUi.shouldCaptureThumbnails();
2822 }
2823
Michael Kolbc3af0672011-08-09 10:24:41 -07002824 @Override
2825 public void setBlockEvents(boolean block) {
2826 mBlockEvents = block;
2827 }
2828
John Reck9c35b9c2012-05-30 10:08:50 -07002829 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002830 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002831 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002832 }
2833
John Reck9c35b9c2012-05-30 10:08:50 -07002834 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002835 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002836 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002837 }
2838
John Reck9c35b9c2012-05-30 10:08:50 -07002839 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002840 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002841 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002842 }
2843
John Reck9c35b9c2012-05-30 10:08:50 -07002844 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002845 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002846 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002847 }
2848
John Reck9c35b9c2012-05-30 10:08:50 -07002849 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002850 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002851 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002852 }
2853
Michael Kolb8233fac2010-10-26 16:08:53 -07002854}