blob: fddb0fe248338e527995ac7d7e14e29a3e5d046e [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;
kaiyizb7bf1f22013-10-02 11:42:23 +080020import android.app.ActivityManagerNative;
luxiaolb40014b2013-07-19 10:01:43 +080021import android.app.AlertDialog;
John Reck68234a92012-04-19 15:27:12 -070022import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070024import android.app.ProgressDialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070025import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070026import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070027import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070028import android.content.ContentValues;
29import android.content.Context;
John Reck68234a92012-04-19 15:27:12 -070030import android.content.DialogInterface;
31import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070032import android.content.Intent;
33import android.content.pm.PackageManager;
34import android.content.pm.ResolveInfo;
35import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070036import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050037import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070038import android.database.Cursor;
39import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020040import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.graphics.Bitmap;
kaiyiz6e5b3e02013-08-19 20:02:01 +080042import android.graphics.BitmapFactory;
Michael Kolb8233fac2010-10-26 16:08:53 -070043import android.graphics.Canvas;
kaiyiza016da12013-08-26 17:50:22 +080044import android.net.ConnectivityManager;
45import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070046import android.net.Uri;
47import android.net.http.SslError;
kaiyiz6e5b3e02013-08-19 20:02:01 +080048import android.net.WebAddress;
kaiyiza016da12013-08-26 17:50:22 +080049import android.net.wifi.WifiManager;
kaiyizb7bf1f22013-10-02 11:42:23 +080050import android.net.wifi.ScanResult;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.os.AsyncTask;
52import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070053import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070054import android.os.Handler;
55import android.os.Message;
56import android.os.PowerManager;
57import android.os.PowerManager.WakeLock;
kaiyiza8b6dbb2013-07-29 18:11:22 +080058import android.os.SystemProperties;
Ben Murdoch8029a772010-11-16 11:58:21 +000059import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070060import android.provider.Browser;
61import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070062import android.provider.BrowserContract.Images;
63import android.provider.ContactsContract;
64import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080065import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070066import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070067import android.text.TextUtils;
68import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080069import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070070import android.view.ActionMode;
71import android.view.ContextMenu;
72import android.view.ContextMenu.ContextMenuInfo;
73import android.view.Gravity;
74import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070075import android.view.Menu;
76import android.view.MenuInflater;
77import android.view.MenuItem;
78import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070079import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070080import android.view.View;
81import android.webkit.CookieManager;
82import android.webkit.CookieSyncManager;
83import android.webkit.HttpAuthHandler;
George Mount387d45d2011-10-07 15:57:53 -070084import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070085import android.webkit.SslErrorHandler;
86import android.webkit.ValueCallback;
87import android.webkit.WebChromeClient;
88import android.webkit.WebIconDatabase;
89import android.webkit.WebSettings;
90import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000091import android.webkit.WebViewClassic;
Leon Scrogginsac993842011-02-02 12:54:07 -050092import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070093
Michael Kolb4bd767d2011-05-27 11:33:55 -070094import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070095import com.android.browser.UI.ComboViews;
John Reck1cf4b792011-07-26 10:22:22 -070096import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070097import com.android.browser.provider.SnapshotProvider.Snapshots;
kaiyiz6e5b3e02013-08-19 20:02:01 +080098import com.android.browser.mynavigation.AddMyNavigationPage;
99import com.android.browser.mynavigation.MyNavigationUtil;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700100
Michael Kolb8233fac2010-10-26 16:08:53 -0700101import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -0700102import java.io.File;
103import java.io.FileOutputStream;
104import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -0700105import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700106import java.text.DateFormat;
107import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700108import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -0700109import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -0700110import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700111import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800112import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200113import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700114import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115
116/**
117 * Controller for browser
118 */
119public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700120 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700121
122 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800123 private static final String SEND_APP_ID_EXTRA =
124 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700125 private static final String INCOGNITO_URI = "browser:incognito";
kaiyiza016da12013-08-26 17:50:22 +0800126 private static final String PROP_NETSWITCH = "persist.env.browser.netswitch";
127 private static final String INTENT_WIFI_SELECTION_DATA_CONNECTION =
128 "android.net.wifi.cmcc.WIFI_SELECTION_DATA_CONNECTION";
kaiyiz6e5b3e02013-08-19 20:02:01 +0800129 private static final String OFFLINE_PAGE =
130 "content://com.android.browser.mynavigation/websites";
kaiyizb7bf1f22013-10-02 11:42:23 +0800131 private static final String INTENT_PICK_NETWORK =
132 "android.net.wifi.cmcc.PICK_WIFI_NETWORK_AND_GPRS";
Michael Kolb8233fac2010-10-26 16:08:53 -0700133 // public message ids
134 public final static int LOAD_URL = 1001;
135 public final static int STOP_LOAD = 1002;
136
137 // Message Ids
138 private static final int FOCUS_NODE_HREF = 102;
139 private static final int RELEASE_WAKELOCK = 107;
140
141 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
142
143 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800144 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700145
146 private static final int EMPTY_MENU = -1;
147
Michael Kolb8233fac2010-10-26 16:08:53 -0700148 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700149 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700150 final static int PREFERENCES_PAGE = 3;
151 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000152 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700153 final static int VOICE_RESULT = 6;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800154 final static int MY_NAVIGATION = 7;
Ben Murdoch8029a772010-11-16 11:58:21 +0000155
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
157
158 // As the ids are dynamically created, we can't guarantee that they will
159 // be in sequence, so this static array maps ids to a window number.
160 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
161 { R.id.window_one_menu_id, R.id.window_two_menu_id,
162 R.id.window_three_menu_id, R.id.window_four_menu_id,
163 R.id.window_five_menu_id, R.id.window_six_menu_id,
164 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
165
166 // "source" parameter for Google search through search key
167 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
168 // "source" parameter for Google search through simplily type
169 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
170
George Mount387d45d2011-10-07 15:57:53 -0700171 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700172 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
173
John Reckd7dd9b22011-08-30 09:18:29 -0700174 // A bitmap that is re-used in createScreenshot as scratch space
175 private static Bitmap sThumbnailBitmap;
176
Michael Kolb8233fac2010-10-26 16:08:53 -0700177 private Activity mActivity;
178 private UI mUi;
179 private TabControl mTabControl;
180 private BrowserSettings mSettings;
181 private WebViewFactory mFactory;
182
183 private WakeLock mWakeLock;
184
185 private UrlHandler mUrlHandler;
186 private UploadHandler mUploadHandler;
187 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700188 private PageDialogsHandler mPageDialogsHandler;
189 private NetworkStateHandler mNetworkHandler;
190
Ben Murdoch8029a772010-11-16 11:58:21 +0000191 private Message mAutoFillSetupMessage;
192
Michael Kolb8233fac2010-10-26 16:08:53 -0700193 private boolean mShouldShowErrorConsole;
kaiyiza016da12013-08-26 17:50:22 +0800194 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700195
196 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
197
198 // FIXME, temp address onPrepareMenu performance problem.
199 // When we move everything out of view, we should rewrite this.
200 private int mCurrentMenuState = 0;
201 private int mMenuState = R.id.MAIN_MENU;
202 private int mOldMenuState = EMPTY_MENU;
203 private Menu mCachedMenu;
204
Michael Kolb8233fac2010-10-26 16:08:53 -0700205 private boolean mMenuIsDown;
206
207 // For select and find, we keep track of the ActionMode so that
208 // finish() can be called as desired.
209 private ActionMode mActionMode;
210
211 /**
212 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
213 * of whether the configuration has changed. The first onMenuOpened call
214 * after a configuration change is simply a reopening of the same menu
215 * (i.e. mIconView did not change).
216 */
217 private boolean mConfigChanged;
218
219 /**
220 * Keeps track of whether the options menu is open. This is important in
221 * determining whether to show or hide the title bar overlay
222 */
223 private boolean mOptionsMenuOpen;
224
225 /**
226 * Whether or not the options menu is in its bigger, popup menu form. When
227 * true, we want the title bar overlay to be gone. When false, we do not.
228 * Only meaningful if mOptionsMenuOpen is true.
229 */
230 private boolean mExtendedMenuOpen;
231
Michael Kolb8233fac2010-10-26 16:08:53 -0700232 private boolean mActivityPaused = true;
233 private boolean mLoadStopped;
234
235 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500236 // Checks to see when the bookmarks database has changed, and updates the
237 // Tabs' notion of whether they represent bookmarked sites.
238 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700239 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700240
Michael Kolbc3af0672011-08-09 10:24:41 -0700241 private boolean mBlockEvents;
242
Michael Kolb0b129122012-06-04 16:31:58 -0700243 private String mVoiceResult;
kaiyiz6e5b3e02013-08-19 20:02:01 +0800244 private boolean mUpdateMyNavThumbnail;
245 private String mUpdateMyNavThumbnailUrl;
Michael Kolb0b129122012-06-04 16:31:58 -0700246
George Mount3636d0a2011-11-21 09:08:21 -0800247 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700248 mActivity = browser;
249 mSettings = BrowserSettings.getInstance();
250 mTabControl = new TabControl(this);
251 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700252 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800253 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700254 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700255
256 mUrlHandler = new UrlHandler(this);
257 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700258 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
259
Michael Kolb8233fac2010-10-26 16:08:53 -0700260 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500261 mBookmarksObserver = new ContentObserver(mHandler) {
262 @Override
263 public void onChange(boolean selfChange) {
264 int size = mTabControl.getTabCount();
265 for (int i = 0; i < size; i++) {
266 mTabControl.getTab(i).updateBookmarkedStatus();
267 }
268 }
269
270 };
271 browser.getContentResolver().registerContentObserver(
272 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700273
274 mNetworkHandler = new NetworkStateHandler(mActivity, this);
275 // Start watching the default geolocation permissions
276 mSystemAllowGeolocationOrigins =
277 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
278 mSystemAllowGeolocationOrigins.start();
279
John Reckaf262e72011-07-25 13:55:44 -0700280 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700281 }
282
John Reck9c35b9c2012-05-30 10:08:50 -0700283 @Override
284 public void start(final Intent intent) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800285 if (BrowserWebView.isClassic()) WebViewClassic.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800286 // mCrashRecoverHandler has any previously saved state.
287 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700288 }
289
George Mount3636d0a2011-11-21 09:08:21 -0800290 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700291 // Unless the last browser usage was within 24 hours, destroy any
292 // remaining incognito tabs.
293
294 Calendar lastActiveDate = icicle != null ?
295 (Calendar) icicle.getSerializable("lastActiveDate") : null;
296 Calendar today = Calendar.getInstance();
297 Calendar yesterday = Calendar.getInstance();
298 yesterday.add(Calendar.DATE, -1);
299
Patrick Scott7d50a932011-02-04 09:27:26 -0500300 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700301 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800302 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700303
Patrick Scott7d50a932011-02-04 09:27:26 -0500304 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700305 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500306 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000307
Michael Kolbc831b632011-05-11 09:30:34 -0700308 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500309 // Not able to restore so we go ahead and clear session cookies. We
310 // must do this before trying to login the user as we don't want to
311 // clear any session cookies set during login.
312 CookieManager.getInstance().removeSessionCookie();
313 }
314
Patrick Scottd43e75a2011-03-14 14:47:23 -0400315 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500316 new Runnable() {
317 @Override public void run() {
George Mount3636d0a2011-11-21 09:08:21 -0800318 onPreloginFinished(icicle, intent, currentTabId,
319 restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500320 }
321 });
322 }
323
Michael Kolbc831b632011-05-11 09:30:34 -0700324 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
George Mount3636d0a2011-11-21 09:08:21 -0800325 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700326 if (currentTabId == -1) {
John Reck1cf4b792011-07-26 10:22:22 -0700327 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800328 if (intent == null) {
329 // This won't happen under common scenarios. The icicle is
330 // not null, but there aren't any tabs to restore.
331 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700332 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800333 final Bundle extra = intent.getExtras();
334 // Create an initial tab.
335 // If the intent is ACTION_VIEW and data is not null, the Browser is
336 // invoked to view the content by another application. In this case,
337 // the tab will be close when exit.
kaiyiz6e5b3e02013-08-19 20:02:01 +0800338 UrlData urlData = null;
339 if (intent.getData() != null
340 && Intent.ACTION_VIEW.equals(intent.getAction())
341 && intent.getData().toString().startsWith("content://")) {
342 urlData = new UrlData(intent.getData().toString());
343 } else {
344 urlData = IntentHandler.getUrlDataFromIntent(intent);
345 }
346
George Mount3636d0a2011-11-21 09:08:21 -0800347 Tab t = null;
348 if (urlData.isEmpty()) {
kaiyiz6e5b3e02013-08-19 20:02:01 +0800349 if (SystemProperties.get("persist.env.c.browser.resource", "default").equals(
350 "cmcc")) {
351 t = openTab(OFFLINE_PAGE, false, true, true);
352 } else {
353 t = openTabToHomePage();
354 }
George Mount3636d0a2011-11-21 09:08:21 -0800355 } else {
356 t = openTab(urlData);
357 }
358 if (t != null) {
359 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
360 }
361 WebView webView = t.getWebView();
362 if (extra != null) {
363 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
364 if (scale > 0 && scale <= 1000) {
365 webView.setInitialScale(scale);
366 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700367 }
368 }
John Reckd8c74522011-06-14 08:45:00 -0700369 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700370 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700371 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500372 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700373 List<Tab> tabs = mTabControl.getTabs();
374 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
375 for (Tab t : tabs) {
376 restoredTabs.add(t.getId());
377 }
378 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700379 if (tabs.size() == 0) {
380 openTabToHomePage();
381 }
John Reck1cf4b792011-07-26 10:22:22 -0700382 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700383 // TabControl.restoreState() will create a new tab even if
384 // restoring the state fails.
385 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800386 // Intent is non-null when framework thinks the browser should be
387 // launching with a new intent (icicle is null).
388 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700389 mIntentHandler.onNewIntent(intent);
390 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700391 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700392 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700393 String jsFlags = getSettings().getJsEngineFlags();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800394 if (jsFlags.trim().length() != 0 && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000395 WebViewClassic.fromWebView(getCurrentWebView()).setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700396 }
George Mount3636d0a2011-11-21 09:08:21 -0800397 if (intent != null
398 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700399 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800400 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 }
402
John Reck1cf4b792011-07-26 10:22:22 -0700403 private static class PruneThumbnails implements Runnable {
404 private Context mContext;
405 private List<Long> mIds;
406
407 PruneThumbnails(Context context, List<Long> preserveIds) {
408 mContext = context.getApplicationContext();
409 mIds = preserveIds;
410 }
411
412 @Override
413 public void run() {
414 ContentResolver cr = mContext.getContentResolver();
415 if (mIds == null || mIds.size() == 0) {
416 cr.delete(Thumbnails.CONTENT_URI, null, null);
417 } else {
418 int length = mIds.size();
419 StringBuilder where = new StringBuilder();
420 where.append(Thumbnails._ID);
421 where.append(" not in (");
422 for (int i = 0; i < length; i++) {
423 where.append(mIds.get(i));
424 if (i < (length - 1)) {
425 where.append(",");
426 }
427 }
428 where.append(")");
429 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
430 }
431 }
432
433 }
434
Michael Kolb1514bb72010-11-22 09:11:48 -0800435 @Override
436 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700437 return mFactory;
438 }
439
440 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800441 public void onSetWebView(Tab tab, WebView view) {
442 mUi.onSetWebView(tab, view);
443 }
444
445 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800446 public void createSubWindow(Tab tab) {
447 endActionMode();
448 WebView mainView = tab.getWebView();
449 WebView subView = mFactory.createWebView((mainView == null)
450 ? false
451 : mainView.isPrivateBrowsingEnabled());
452 mUi.createSubWindow(tab, subView);
453 }
454
455 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700456 public Context getContext() {
457 return mActivity;
458 }
459
460 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700461 public Activity getActivity() {
462 return mActivity;
463 }
464
465 void setUi(UI ui) {
466 mUi = ui;
467 }
468
Michael Kolbaf63dba2012-05-16 12:58:05 -0700469 @Override
470 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700471 return mSettings;
472 }
473
474 IntentHandler getIntentHandler() {
475 return mIntentHandler;
476 }
477
478 @Override
479 public UI getUi() {
480 return mUi;
481 }
482
483 int getMaxTabs() {
484 return mActivity.getResources().getInteger(R.integer.max_tabs);
485 }
486
487 @Override
488 public TabControl getTabControl() {
489 return mTabControl;
490 }
491
Michael Kolb1bf23132010-11-19 12:55:12 -0800492 @Override
493 public List<Tab> getTabs() {
494 return mTabControl.getTabs();
495 }
496
John Reckaf262e72011-07-25 13:55:44 -0700497 // Open the icon database.
498 private void openIconDatabase() {
499 // We have to call getInstance on the UI thread
500 final WebIconDatabase instance = WebIconDatabase.getInstance();
501 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000502
John Reckaf262e72011-07-25 13:55:44 -0700503 @Override
504 public void run() {
505 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700506 }
John Reckaf262e72011-07-25 13:55:44 -0700507 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700508 }
509
510 private void startHandler() {
511 mHandler = new Handler() {
512
513 @Override
514 public void handleMessage(Message msg) {
515 switch (msg.what) {
516 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700517 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700518 break;
519 case FOCUS_NODE_HREF:
520 {
521 String url = (String) msg.getData().get("url");
522 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500523 String src = (String) msg.getData().get("src");
524 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700525 if (TextUtils.isEmpty(url)) {
526 break;
527 }
528 HashMap focusNodeMap = (HashMap) msg.obj;
529 WebView view = (WebView) focusNodeMap.get("webview");
530 // Only apply the action if the top window did not change.
531 if (getCurrentTopWebView() != view) {
532 break;
533 }
534 switch (msg.arg1) {
535 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700536 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700537 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500538 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700539 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500540 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500541 case R.id.open_newtab_context_menu_id:
542 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700543 openTab(url, parent,
544 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500545 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 case R.id.copy_link_context_menu_id:
547 copy(url);
548 break;
549 case R.id.save_link_context_menu_id:
550 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500551 DownloadHandler.onDownloadStartNoStream(
Andreas Sandblad8e4ce662012-06-11 11:02:49 +0200552 mActivity, url, view.getSettings().getUserAgentString(),
luxiaol62677b02013-07-22 07:54:49 +0800553 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700554 break;
555 }
556 break;
557 }
558
559 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700560 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700561 break;
562
563 case STOP_LOAD:
564 stopLoading();
565 break;
566
567 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700568 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700569 mWakeLock.release();
570 // if we reach here, Browser should be still in the
571 // background loading after WAKELOCK_TIMEOUT (5-min).
572 // To avoid burning the battery, stop loading.
573 mTabControl.stopAllLoading();
574 }
575 break;
576
577 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800578 Tab tab = (Tab) msg.obj;
579 if (tab != null) {
580 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700581 }
582 break;
kaiyiz591110b2013-08-06 17:11:06 +0800583
584 case OPEN_MENU:
585 if (!mOptionsMenuOpen && mActivity != null ) {
586 mActivity.openOptionsMenu();
587 }
588 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700589 }
590 }
591 };
592
593 }
594
John Reckef654f12011-07-12 16:42:08 -0700595 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200596 public Tab getCurrentTab() {
597 return mTabControl.getCurrentTab();
598 }
599
Michael Kolbba99c5d2010-11-29 14:57:41 -0800600 @Override
601 public void shareCurrentPage() {
602 shareCurrentPage(mTabControl.getCurrentTab());
603 }
604
605 private void shareCurrentPage(Tab tab) {
606 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800607 sharePage(mActivity, tab.getTitle(),
608 tab.getUrl(), tab.getFavicon(),
609 createScreenshot(tab.getWebView(),
610 getDesiredThumbnailWidth(mActivity),
611 getDesiredThumbnailHeight(mActivity)));
612 }
613 }
614
Michael Kolb8233fac2010-10-26 16:08:53 -0700615 /**
616 * Share a page, providing the title, url, favicon, and a screenshot. Uses
617 * an {@link Intent} to launch the Activity chooser.
618 * @param c Context used to launch a new Activity.
619 * @param title Title of the page. Stored in the Intent with
620 * {@link Intent#EXTRA_SUBJECT}
621 * @param url URL of the page. Stored in the Intent with
622 * {@link Intent#EXTRA_TEXT}
623 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
624 * with {@link Browser#EXTRA_SHARE_FAVICON}
625 * @param screenshot Bitmap of a screenshot of the page. Stored in the
626 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
627 */
628 static final void sharePage(Context c, String title, String url,
629 Bitmap favicon, Bitmap screenshot) {
630 Intent send = new Intent(Intent.ACTION_SEND);
631 send.setType("text/plain");
632 send.putExtra(Intent.EXTRA_TEXT, url);
633 send.putExtra(Intent.EXTRA_SUBJECT, title);
634 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
635 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
636 try {
637 c.startActivity(Intent.createChooser(send, c.getString(
638 R.string.choosertitle_sharevia)));
639 } catch(android.content.ActivityNotFoundException ex) {
640 // if no app handles it, do nothing
641 }
642 }
643
644 private void copy(CharSequence text) {
645 ClipboardManager cm = (ClipboardManager) mActivity
646 .getSystemService(Context.CLIPBOARD_SERVICE);
647 cm.setText(text);
648 }
649
650 // lifecycle
651
John Reck9c35b9c2012-05-30 10:08:50 -0700652 @Override
653 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700654 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800655 // update the menu in case of a locale change
656 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800657 if (mOptionsMenuOpen) {
658 mActivity.closeOptionsMenu();
659 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
660 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 if (mPageDialogsHandler != null) {
662 mPageDialogsHandler.onConfigurationChanged(config);
663 }
664 mUi.onConfigurationChanged(config);
665 }
666
667 @Override
668 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700669 if (!mUi.isWebShowing()) {
670 mUi.showWeb(false);
671 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700672 mIntentHandler.onNewIntent(intent);
673 }
674
John Reck9c35b9c2012-05-30 10:08:50 -0700675 @Override
676 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800677 if (mUi.isCustomViewShowing()) {
678 hideCustomView();
679 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700680 if (mActivityPaused) {
681 Log.e(LOGTAG, "BrowserActivity is already paused.");
682 return;
683 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700684 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800685 Tab tab = mTabControl.getCurrentTab();
686 if (tab != null) {
687 tab.pause();
688 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700689 if (mWakeLock == null) {
690 PowerManager pm = (PowerManager) mActivity
691 .getSystemService(Context.POWER_SERVICE);
692 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
693 }
Michael Kolb70976932010-11-30 11:34:01 -0800694 mWakeLock.acquire();
695 mHandler.sendMessageDelayed(mHandler
696 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
697 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700698 }
699 mUi.onPause();
700 mNetworkHandler.onPause();
701
702 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100703 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700704 if (sThumbnailBitmap != null) {
705 sThumbnailBitmap.recycle();
706 sThumbnailBitmap = null;
707 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700708 }
709
John Reck9c35b9c2012-05-30 10:08:50 -0700710 @Override
711 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700712 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800713 Bundle saveState = createSaveState();
714
715 // crash recovery manages all save & restore state
716 mCrashRecoveryHandler.writeState(saveState);
717 mSettings.setLastRunPaused(true);
718 }
719
720 /**
721 * Save the current state to outState. Does not write the state to
722 * disk.
723 * @return Bundle containing the current state of all tabs.
724 */
725 /* package */ Bundle createSaveState() {
726 Bundle saveState = new Bundle();
727 mTabControl.saveState(saveState);
728 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700729 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800730 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700731 }
George Mount3636d0a2011-11-21 09:08:21 -0800732 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700733 }
734
John Reck9c35b9c2012-05-30 10:08:50 -0700735 @Override
736 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700737 if (!mActivityPaused) {
738 Log.e(LOGTAG, "BrowserActivity is already resumed.");
739 return;
740 }
George Mount3636d0a2011-11-21 09:08:21 -0800741 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700742 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800743 Tab current = mTabControl.getCurrentTab();
744 if (current != null) {
745 current.resume();
746 resumeWebViewTimers(current);
747 }
John Reckf57c0292011-07-21 18:15:39 -0700748 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200749
Michael Kolb8233fac2010-10-26 16:08:53 -0700750 mUi.onResume();
751 mNetworkHandler.onResume();
752 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100753 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700754 if (mVoiceResult != null) {
755 mUi.onVoiceResult(mVoiceResult);
756 mVoiceResult = null;
757 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700758 }
759
John Reckf57c0292011-07-21 18:15:39 -0700760 private void releaseWakeLock() {
761 if (mWakeLock != null && mWakeLock.isHeld()) {
762 mHandler.removeMessages(RELEASE_WAKELOCK);
763 mWakeLock.release();
764 }
765 }
766
Michael Kolb70976932010-11-30 11:34:01 -0800767 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800768 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800769 * @param tab guaranteed non-null
770 */
771 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700772 boolean inLoad = tab.inPageLoad();
773 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
774 CookieSyncManager.getInstance().startSync();
775 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100776 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700777 }
778 }
779
Michael Kolb70976932010-11-30 11:34:01 -0800780 /**
781 * Pause all WebView timers using the WebView of the given tab
782 * @param tab
783 * @return true if the timers are paused or tab is null
784 */
785 private boolean pauseWebViewTimers(Tab tab) {
786 if (tab == null) {
787 return true;
788 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700789 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100790 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700791 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700792 }
Michael Kolb70976932010-11-30 11:34:01 -0800793 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700794 }
795
John Reck9c35b9c2012-05-30 10:08:50 -0700796 @Override
797 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800798 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700799 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
800 mUploadHandler = null;
801 }
802 if (mTabControl == null) return;
803 mUi.onDestroy();
804 // Remove the current tab and sub window
805 Tab t = mTabControl.getCurrentTab();
806 if (t != null) {
807 dismissSubWindow(t);
808 removeTab(t);
809 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500810 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700811 // Destroy all the tabs
812 mTabControl.destroy();
813 WebIconDatabase.getInstance().close();
814 // Stop watching the default geolocation permissions
815 mSystemAllowGeolocationOrigins.stop();
816 mSystemAllowGeolocationOrigins = null;
817 }
818
819 protected boolean isActivityPaused() {
820 return mActivityPaused;
821 }
822
John Reck9c35b9c2012-05-30 10:08:50 -0700823 @Override
824 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 mTabControl.freeMemory();
826 }
827
828 @Override
829 public boolean shouldShowErrorConsole() {
830 return mShouldShowErrorConsole;
831 }
832
833 protected void setShouldShowErrorConsole(boolean show) {
834 if (show == mShouldShowErrorConsole) {
835 // Nothing to do.
836 return;
837 }
838 mShouldShowErrorConsole = show;
839 Tab t = mTabControl.getCurrentTab();
840 if (t == null) {
841 // There is no current tab so we cannot toggle the error console
842 return;
843 }
844 mUi.setShouldShowErrorConsole(t, show);
845 }
846
847 @Override
848 public void stopLoading() {
849 mLoadStopped = true;
850 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700851 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700852 if (w != null) {
853 w.stopLoading();
854 mUi.onPageStopped(tab);
855 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700856 }
857
858 boolean didUserStopLoading() {
859 return mLoadStopped;
860 }
861
kaiyiza016da12013-08-26 17:50:22 +0800862 private void handleNetworkNotify(WebView view) {
863 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
864 Context.CONNECTIVITY_SERVICE);
865 WifiManager wifiMgr = (WifiManager) this.getContext()
866 .getSystemService(Context.WIFI_SERVICE);
867 int networkSwitchTypeOK = this.getContext().getResources()
868 .getInteger(R.integer.netswitch_type_remind);
869
870 if (wifiMgr.isWifiEnabled()) {
871 NetworkInfo mNetworkInfo = conMgr.getActiveNetworkInfo();
872 if (mNetworkInfo == null
873 || (mNetworkInfo != null && (mNetworkInfo.getType() !=
874 ConnectivityManager.TYPE_WIFI))) {
kaiyizb7bf1f22013-10-02 11:42:23 +0800875 List<ScanResult> list = wifiMgr.getScanResults();
876 if (list != null && list.size() == 0) {
877 int isReminder = Settings.System.getInt(
878 mActivity.getContentResolver(),
879 this.getContext().getResources()
880 .getString(R.string.network_switch_remind_type),
881 networkSwitchTypeOK);
882 if (isReminder == networkSwitchTypeOK) {
883 Intent intent = new Intent(
884 INTENT_WIFI_SELECTION_DATA_CONNECTION);
885 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
886 this.getContext().startActivity(intent);
887 }
888 } else {
889 if (ActivityManagerNative.isSystemReady()) {
890 try {
891 Intent intent = new Intent(INTENT_PICK_NETWORK);
892 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
893 this.getContext().startActivity(intent);
894 } catch (Exception e) {
895 String err_msg = this.getContext().getString(
896 R.string.acivity_not_found, INTENT_PICK_NETWORK);
897 Toast.makeText(this.getContext(), err_msg, Toast.LENGTH_LONG).show();
898 }
899 } else {
900 Log.e(LOGTAG, "System is not ready!");
901 }
kaiyiza016da12013-08-26 17:50:22 +0800902 }
903 mNetworkShouldNotify = false;
904 }
905 } else {
906 if (!mNetworkHandler.isNetworkUp()) {
907 view.setNetworkAvailable(false);
908 Log.v(LOGTAG, "handleNetworkNotify() Wlan is not enabled.");
909 }
910 }
911 }
912
Michael Kolb8233fac2010-10-26 16:08:53 -0700913 // WebViewController
914
915 @Override
John Reck324d4402011-01-11 16:56:42 -0800916 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700917
918 // We've started to load a new page. If there was a pending message
919 // to save a screenshot then we will now take the new page and save
920 // an incorrect screenshot. Therefore, remove any pending thumbnail
921 // messages from the queue.
922 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800923 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700924
925 // reset sync timer to avoid sync starts during loading a page
926 CookieSyncManager.getInstance().resetSync();
927
kaiyiza016da12013-08-26 17:50:22 +0800928 if (SystemProperties.getBoolean(PROP_NETSWITCH, false)) {
929 if (!mNetworkHandler.isNetworkUp()) {
930 Log.d(LOGTAG, "onPageStarted() network unavailable");
931 if (mNetworkShouldNotify) {
932 handleNetworkNotify(view);
933 } else {
934 view.setNetworkAvailable(false);
935 }
936 mNetworkShouldNotify = false;
937 } else {
938 Log.d(LOGTAG, "onPageStarted() network available");
939 if (mNetworkShouldNotify) {
940 handleNetworkNotify(view);
941 }
942 mNetworkShouldNotify = false;
943 }
944 } else {
945 if (!mNetworkHandler.isNetworkUp()) {
946 view.setNetworkAvailable(false);
947 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700948 }
949
950 // when BrowserActivity just starts, onPageStarted may be called before
951 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
952 // to start the timer. As we won't switch tabs while an activity is in
953 // pause state, we can ensure calling resume and pause in pair.
954 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800955 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700956 }
957 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700958 endActionMode();
959
John Reck30c714c2010-12-16 17:30:34 -0800960 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700961
John Reck324d4402011-01-11 16:56:42 -0800962 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700963 // update the bookmark database for favicon
964 maybeUpdateFavicon(tab, null, url, favicon);
965
966 Performance.tracePageStart(url);
967
968 // Performance probe
969 if (false) {
970 Performance.onPageStarted();
971 }
972
973 }
974
975 @Override
John Reck324d4402011-01-11 16:56:42 -0800976 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700977 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800978 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200979
Michael Kolb8233fac2010-10-26 16:08:53 -0700980 // Performance probe
981 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800982 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700983 }
984
985 Performance.tracePageFinished();
986 }
987
988 @Override
John Reck30c714c2010-12-16 17:30:34 -0800989 public void onProgressChanged(Tab tab) {
990 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700991
992 if (newProgress == 100) {
993 CookieSyncManager.getInstance().sync();
994 // onProgressChanged() may continue to be called after the main
995 // frame has finished loading, as any remaining sub frames continue
996 // to load. We'll only get called once though with newProgress as
997 // 100 when everything is loaded. (onPageFinished is called once
998 // when the main frame completes loading regardless of the state of
999 // any sub frames so calls to onProgressChanges may continue after
1000 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001001 if (tab.inPageLoad()) {
1002 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +02001003 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
1004 // pause the WebView timer and release the wake lock if it is
1005 // finished while BrowserActivity is in pause state.
1006 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -07001007 }
John Reckd9862372012-02-21 15:04:50 -08001008 if (!tab.isPrivateBrowsingEnabled()
1009 && !TextUtils.isEmpty(tab.getUrl())
1010 && !tab.isSnapshot()) {
1011 // Only update the bookmark screenshot if the user did not
1012 // cancel the load early and there is not already
1013 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -07001014 if (tab.shouldUpdateThumbnail() &&
1015 (tab.inForeground() && !didUserStopLoading()
1016 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -08001017 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
1018 mHandler.sendMessageDelayed(mHandler.obtainMessage(
1019 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
1020 500);
1021 }
1022 }
1023 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001024 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001025 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001026 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001027 // still loading
1028 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -07001029 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001030 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001031 }
1032 }
John Reck30c714c2010-12-16 17:30:34 -08001033 mUi.onProgressChanged(tab);
1034 }
1035
1036 @Override
Steve Block2466eff2011-10-03 15:33:09 +01001037 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08001038 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001039 }
1040
1041 @Override
1042 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001043 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001044 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001045 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001046 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1047 return;
1048 }
1049 // Update the title in the history database if not in private browsing mode
1050 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001051 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001052 }
1053 }
1054
1055 @Override
1056 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001057 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001058 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1059 }
1060
1061 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001062 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1063 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001064 }
1065
1066 @Override
1067 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1068 if (mMenuIsDown) {
1069 // only check shortcut key when MENU is held
1070 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1071 event);
1072 } else {
1073 return false;
1074 }
1075 }
1076
1077 @Override
John Reck997b1b72012-04-19 18:08:25 -07001078 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001079 if (!isActivityPaused()) {
1080 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001081 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001082 } else {
John Reck997b1b72012-04-19 18:08:25 -07001083 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001084 }
1085 }
John Reck997b1b72012-04-19 18:08:25 -07001086 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001087 }
1088
1089 @Override
John Reck324d4402011-01-11 16:56:42 -08001090 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001091 // Don't save anything in private browsing mode
1092 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -08001093 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001094
John Reck324d4402011-01-11 16:56:42 -08001095 if (TextUtils.isEmpty(url)
1096 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001097 return;
1098 }
John Reckf57c0292011-07-21 18:15:39 -07001099 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001100 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001101 }
1102
1103 @Override
1104 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1105 AsyncTask<Void, Void, String[]> task =
1106 new AsyncTask<Void, Void, String[]>() {
1107 @Override
1108 public String[] doInBackground(Void... unused) {
1109 return Browser.getVisitedHistory(mActivity.getContentResolver());
1110 }
1111 @Override
1112 public void onPostExecute(String[] result) {
1113 callback.onReceiveValue(result);
1114 }
1115 };
1116 task.execute();
1117 }
1118
1119 @Override
1120 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1121 final HttpAuthHandler handler, final String host,
1122 final String realm) {
1123 String username = null;
1124 String password = null;
1125
1126 boolean reuseHttpAuthUsernamePassword
1127 = handler.useHttpAuthUsernamePassword();
1128
1129 if (reuseHttpAuthUsernamePassword && view != null) {
1130 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1131 if (credentials != null && credentials.length == 2) {
1132 username = credentials[0];
1133 password = credentials[1];
1134 }
1135 }
1136
1137 if (username != null && password != null) {
1138 handler.proceed(username, password);
1139 } else {
Ben Murdochfdd37112011-08-05 15:48:14 +01001140 if (tab.inForeground() && !handler.suppressDialog()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001141 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1142 } else {
1143 handler.cancel();
1144 }
1145 }
1146 }
1147
1148 @Override
1149 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001150 String contentDisposition, String mimetype, String referer,
1151 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001152 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001153 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001154 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001155 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001156 // This Tab was opened for the sole purpose of downloading a
1157 // file. Remove it.
1158 if (tab == mTabControl.getCurrentTab()) {
1159 // In this case, the Tab is still on top.
1160 goBackOnePageOrQuit();
1161 } else {
1162 // In this case, it is not.
1163 closeTab(tab);
1164 }
1165 }
1166 }
1167
1168 @Override
1169 public Bitmap getDefaultVideoPoster() {
1170 return mUi.getDefaultVideoPoster();
1171 }
1172
1173 @Override
1174 public View getVideoLoadingProgressView() {
1175 return mUi.getVideoLoadingProgressView();
1176 }
1177
1178 @Override
1179 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1180 SslError error) {
1181 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1182 }
1183
Patrick Scott92066772011-03-10 08:46:27 -05001184 @Override
1185 public void showAutoLogin(Tab tab) {
1186 assert tab.inForeground();
1187 // Update the title bar to show the auto-login request.
1188 mUi.showAutoLogin(tab);
1189 }
1190
1191 @Override
1192 public void hideAutoLogin(Tab tab) {
1193 assert tab.inForeground();
1194 mUi.hideAutoLogin(tab);
1195 }
1196
Michael Kolb8233fac2010-10-26 16:08:53 -07001197 // helper method
1198
1199 /*
1200 * Update the favorites icon if the private browsing isn't enabled and the
1201 * icon is valid.
1202 */
1203 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1204 final String url, Bitmap favicon) {
1205 if (favicon == null) {
1206 return;
1207 }
1208 if (!tab.isPrivateBrowsingEnabled()) {
1209 Bookmarks.updateFavicon(mActivity
1210 .getContentResolver(), originalUrl, url, favicon);
1211 }
1212 }
1213
Leon Scroggins4cd97792010-12-03 15:31:56 -05001214 @Override
1215 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001216 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001217 mUi.bookmarkedStatusHasChanged(tab);
1218 }
1219
Michael Kolb8233fac2010-10-26 16:08:53 -07001220 // end WebViewController
1221
1222 protected void pageUp() {
1223 getCurrentTopWebView().pageUp(false);
1224 }
1225
1226 protected void pageDown() {
1227 getCurrentTopWebView().pageDown(false);
1228 }
1229
1230 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001231 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 public void editUrl() {
1233 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001234 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001235 }
1236
John Reck9c35b9c2012-05-30 10:08:50 -07001237 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001238 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001239 WebChromeClient.CustomViewCallback callback) {
1240 if (tab.inForeground()) {
1241 if (mUi.isCustomViewShowing()) {
1242 callback.onCustomViewHidden();
1243 return;
1244 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001245 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001246 // Save the menu state and set it to empty while the custom
1247 // view is showing.
1248 mOldMenuState = mMenuState;
1249 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001250 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001251 }
1252 }
1253
1254 @Override
1255 public void hideCustomView() {
1256 if (mUi.isCustomViewShowing()) {
1257 mUi.onHideCustomView();
1258 // Reset the old menu state.
1259 mMenuState = mOldMenuState;
1260 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001261 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001262 }
1263 }
1264
John Reck9c35b9c2012-05-30 10:08:50 -07001265 @Override
1266 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001267 Intent intent) {
1268 if (getCurrentTopWebView() == null) return;
1269 switch (requestCode) {
1270 case PREFERENCES_PAGE:
1271 if (resultCode == Activity.RESULT_OK && intent != null) {
1272 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001273 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001274 mTabControl.removeParentChildRelationShips();
1275 }
1276 }
1277 break;
1278 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001279 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001280 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001281 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001282 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001283 case AUTOFILL_SETUP:
1284 // Determine whether a profile was actually set up or not
1285 // and if so, send the message back to the WebTextView to
1286 // fill the form with the new profile.
1287 if (getSettings().getAutoFillProfile() != null) {
1288 mAutoFillSetupMessage.sendToTarget();
1289 mAutoFillSetupMessage = null;
1290 }
1291 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001292 case COMBO_VIEW:
1293 if (intent == null || resultCode != Activity.RESULT_OK) {
1294 break;
1295 }
John Reck3ba45532011-08-11 16:26:53 -07001296 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001297 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1298 Tab t = getCurrentTab();
1299 Uri uri = intent.getData();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001300 mUpdateMyNavThumbnail = true;
1301 mUpdateMyNavThumbnailUrl = uri.toString();
John Reckd3e4d5b2011-07-13 15:48:43 -07001302 loadUrl(t, uri.toString());
1303 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1304 String[] urls = intent.getStringArrayExtra(
1305 ComboViewActivity.EXTRA_OPEN_ALL);
1306 Tab parent = getCurrentTab();
1307 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001308 if (url != null) {
1309 parent = openTab(url, parent,
1310 !mSettings.openInBackground(), true);
1311 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001312 }
1313 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1314 long id = intent.getLongExtra(
1315 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1316 if (id >= 0) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08001317 if (BrowserWebView.isClassic()) {
1318 createNewSnapshotTab(id, true);
1319 } else {
1320 Toast.makeText(mActivity, "Snapshot Tab requires WebViewClassic",
1321 Toast.LENGTH_LONG).show();
1322 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001323 }
1324 }
1325 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001326 case VOICE_RESULT:
1327 if (resultCode == Activity.RESULT_OK && intent != null) {
1328 ArrayList<String> results = intent.getStringArrayListExtra(
1329 RecognizerIntent.EXTRA_RESULTS);
1330 if (results.size() >= 1) {
1331 mVoiceResult = results.get(0);
1332 }
1333 }
1334 break;
kaiyiz6e5b3e02013-08-19 20:02:01 +08001335
1336 case MY_NAVIGATION:
1337 if (intent == null || resultCode != Activity.RESULT_OK) {
1338 break;
1339 }
1340
1341 if (intent.getBooleanExtra("need_refresh", false) &&
1342 getCurrentTopWebView() != null) {
1343 getCurrentTopWebView().reload();
1344 }
1345 break;
1346
Michael Kolb8233fac2010-10-26 16:08:53 -07001347 default:
1348 break;
1349 }
1350 getCurrentTopWebView().requestFocus();
1351 }
1352
1353 /**
1354 * Open the Go page.
1355 * @param startWithHistory If true, open starting on the history tab.
1356 * Otherwise, start with the bookmarks tab.
1357 */
1358 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001359 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001360 if (mTabControl.getCurrentWebView() == null) {
1361 return;
1362 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001363 // clear action mode
1364 if (isInCustomActionMode()) {
1365 endActionMode();
1366 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001367 Bundle extras = new Bundle();
1368 // Disable opening in a new window if we have maxed out the windows
1369 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1370 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001371 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001372 }
1373
1374 // combo view callbacks
1375
Michael Kolb8233fac2010-10-26 16:08:53 -07001376 // key handling
1377 protected void onBackKey() {
1378 if (!mUi.onBackKey()) {
1379 WebView subwindow = mTabControl.getCurrentSubWindow();
1380 if (subwindow != null) {
1381 if (subwindow.canGoBack()) {
1382 subwindow.goBack();
1383 } else {
1384 dismissSubWindow(mTabControl.getCurrentTab());
1385 }
1386 } else {
1387 goBackOnePageOrQuit();
1388 }
1389 }
1390 }
1391
Michael Kolb4bd767d2011-05-27 11:33:55 -07001392 protected boolean onMenuKey() {
1393 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001394 }
1395
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 // menu handling and state
1397 // TODO: maybe put into separate handler
1398
John Reck9c35b9c2012-05-30 10:08:50 -07001399 @Override
1400 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001401 if (mMenuState == EMPTY_MENU) {
1402 return false;
1403 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001404 MenuInflater inflater = mActivity.getMenuInflater();
1405 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001406 return true;
1407 }
1408
John Reck9c35b9c2012-05-30 10:08:50 -07001409 @Override
1410 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001411 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001412 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001413 return;
1414 }
1415 if (!(v instanceof WebView)) {
1416 return;
1417 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001418 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001419 WebView.HitTestResult result = webview.getHitTestResult();
1420 if (result == null) {
1421 return;
1422 }
1423
1424 int type = result.getType();
1425 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1426 Log.w(LOGTAG,
1427 "We should not show context menu when nothing is touched");
1428 return;
1429 }
1430 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1431 // let TextView handles context menu
1432 return;
1433 }
1434
1435 // Note, http://b/issue?id=1106666 is requesting that
1436 // an inflated menu can be used again. This is not available
1437 // yet, so inflate each time (yuk!)
1438 MenuInflater inflater = mActivity.getMenuInflater();
1439 inflater.inflate(R.menu.browsercontext, menu);
1440
1441 // Show the correct menu group
1442 final String extra = result.getExtra();
kaiyiz6e5b3e02013-08-19 20:02:01 +08001443 final String navigationUrl = MyNavigationUtil.getMyNavigationUrl(extra);
Michael Kolbc159c1a2011-12-15 16:06:38 -08001444 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001445 menu.setGroupVisible(R.id.PHONE_MENU,
1446 type == WebView.HitTestResult.PHONE_TYPE);
1447 menu.setGroupVisible(R.id.EMAIL_MENU,
1448 type == WebView.HitTestResult.EMAIL_TYPE);
1449 menu.setGroupVisible(R.id.GEO_MENU,
1450 type == WebView.HitTestResult.GEO_TYPE);
kaiyiz6e5b3e02013-08-19 20:02:01 +08001451
1452 String itemUrl = null;
1453 String url = webview.getOriginalUrl();
1454 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1455 itemUrl = Uri.decode(navigationUrl);
1456 if (itemUrl != null && !MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1457 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU,
1458 type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1459 } else {
1460 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1461 }
1462 menu.setGroupVisible(R.id.IMAGE_MENU, false);
1463 menu.setGroupVisible(R.id.ANCHOR_MENU, false);
1464 } else {
1465 menu.setGroupVisible(R.id.MY_NAVIGATION_MENU, false);
1466
1467 menu.setGroupVisible(R.id.IMAGE_MENU,
1468 type == WebView.HitTestResult.IMAGE_TYPE
1469 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1470 menu.setGroupVisible(R.id.ANCHOR_MENU,
1471 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1472 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1473 }
1474
Cary Clark8974d282010-11-22 10:46:05 -05001475 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1476 || type == WebView.HitTestResult.PHONE_TYPE
1477 || type == WebView.HitTestResult.EMAIL_TYPE
1478 || type == WebView.HitTestResult.GEO_TYPE;
1479 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1480 if (hitText) {
1481 menu.findItem(R.id.select_text_menu_id)
1482 .setOnMenuItemClickListener(new SelectText(webview));
1483 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001484 // Setup custom handling depending on the type
1485 switch (type) {
1486 case WebView.HitTestResult.PHONE_TYPE:
1487 menu.setHeaderTitle(Uri.decode(extra));
1488 menu.findItem(R.id.dial_context_menu_id).setIntent(
1489 new Intent(Intent.ACTION_VIEW, Uri
1490 .parse(WebView.SCHEME_TEL + extra)));
1491 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1492 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1493 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1494 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1495 addIntent);
1496 menu.findItem(R.id.copy_phone_context_menu_id)
1497 .setOnMenuItemClickListener(
1498 new Copy(extra));
1499 break;
1500
1501 case WebView.HitTestResult.EMAIL_TYPE:
1502 menu.setHeaderTitle(extra);
1503 menu.findItem(R.id.email_context_menu_id).setIntent(
1504 new Intent(Intent.ACTION_VIEW, Uri
1505 .parse(WebView.SCHEME_MAILTO + extra)));
1506 menu.findItem(R.id.copy_mail_context_menu_id)
1507 .setOnMenuItemClickListener(
1508 new Copy(extra));
1509 break;
1510
1511 case WebView.HitTestResult.GEO_TYPE:
1512 menu.setHeaderTitle(extra);
1513 menu.findItem(R.id.map_context_menu_id).setIntent(
1514 new Intent(Intent.ACTION_VIEW, Uri
1515 .parse(WebView.SCHEME_GEO
1516 + URLEncoder.encode(extra))));
1517 menu.findItem(R.id.copy_geo_context_menu_id)
1518 .setOnMenuItemClickListener(
1519 new Copy(extra));
1520 break;
1521
1522 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1523 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001524 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001525 // decide whether to show the open link in new tab option
1526 boolean showNewTab = mTabControl.canCreateNewTab();
1527 MenuItem newTabItem
1528 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001529 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001530 ? R.string.contextmenu_openlink_newwindow_background
1531 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001532 newTabItem.setVisible(showNewTab);
1533 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001534 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1535 newTabItem.setOnMenuItemClickListener(
1536 new MenuItem.OnMenuItemClickListener() {
1537 @Override
1538 public boolean onMenuItemClick(MenuItem item) {
1539 final HashMap<String, WebView> hrefMap =
1540 new HashMap<String, WebView>();
1541 hrefMap.put("webview", webview);
1542 final Message msg = mHandler.obtainMessage(
1543 FOCUS_NODE_HREF,
1544 R.id.open_newtab_context_menu_id,
1545 0, hrefMap);
1546 webview.requestFocusNodeHref(msg);
1547 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001548 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001549 });
1550 } else {
1551 newTabItem.setOnMenuItemClickListener(
1552 new MenuItem.OnMenuItemClickListener() {
1553 @Override
1554 public boolean onMenuItemClick(MenuItem item) {
1555 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001556 openTab(extra, parent,
1557 !mSettings.openInBackground(),
1558 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001559 return true;
1560 }
1561 });
1562 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001563 }
kaiyiz6e5b3e02013-08-19 20:02:01 +08001564 if (url != null && url.equalsIgnoreCase(MyNavigationUtil.MY_NAVIGATION)) {
1565 menu.setHeaderTitle(navigationUrl);
1566 menu.findItem(R.id.open_newtab_context_menu_id).setVisible(false);
1567
1568 if (itemUrl != null) {
1569 if (!MyNavigationUtil.isDefaultMyNavigation(itemUrl)) {
1570 menu.findItem(R.id.edit_my_navigation_context_menu_id)
1571 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1572 @Override
1573 public boolean onMenuItemClick(MenuItem item) {
1574 final Intent intent = new Intent(Controller.this
1575 .getContext(),
1576 AddMyNavigationPage.class);
1577 Bundle bundle = new Bundle();
1578 String url = Uri.decode(navigationUrl);
1579 bundle.putBoolean("isAdding", false);
1580 bundle.putString("url", url);
1581 bundle.putString("name", getNameFromUrl(url));
1582 intent.putExtra("websites", bundle);
1583 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1584 return false;
1585 }
1586 });
1587 menu.findItem(R.id.delete_my_navigation_context_menu_id)
1588 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1589 @Override
1590 public boolean onMenuItemClick(MenuItem item) {
1591 showMyNavigationDeleteDialog(Uri.decode(navigationUrl));
1592 return false;
1593 }
1594 });
1595 }
1596 } else {
1597 Log.e(LOGTAG, "mynavigation onCreateContextMenu itemUrl is null!");
1598 }
1599 }
1600
Michael Kolb8233fac2010-10-26 16:08:53 -07001601 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1602 break;
1603 }
1604 // otherwise fall through to handle image part
1605 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001606 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1607 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001608 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1609 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001610 shareItem.setOnMenuItemClickListener(
1611 new MenuItem.OnMenuItemClickListener() {
1612 @Override
1613 public boolean onMenuItemClick(MenuItem item) {
1614 sharePage(mActivity, null, extra, null,
1615 null);
1616 return true;
1617 }
1618 }
1619 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001620 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001621 menu.findItem(R.id.view_image_context_menu_id)
1622 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1623 @Override
1624 public boolean onMenuItemClick(MenuItem item) {
1625 openTab(extra, mTabControl.getCurrentTab(), true, true);
1626 return false;
1627 }
1628 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001629 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1630 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1631 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001632 menu.findItem(R.id.set_wallpaper_context_menu_id).
1633 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1634 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001635 break;
1636
1637 default:
1638 Log.w(LOGTAG, "We should not get here.");
1639 break;
1640 }
1641 //update the ui
1642 mUi.onContextMenuCreated(menu);
1643 }
1644
kaiyiz6e5b3e02013-08-19 20:02:01 +08001645 public void startAddMyNavigation(String url) {
1646 final Intent intent = new Intent(Controller.this.getContext(), AddMyNavigationPage.class);
1647 Bundle bundle = new Bundle();
1648 bundle.putBoolean("isAdding", true);
1649 bundle.putString("url", url);
1650 bundle.putString("name", getNameFromUrl(url));
1651 intent.putExtra("websites", bundle);
1652 mActivity.startActivityForResult(intent, MY_NAVIGATION);
1653 }
1654
1655 private void showMyNavigationDeleteDialog(final String itemUrl) {
1656 new AlertDialog.Builder(this.getContext())
1657 .setTitle(R.string.my_navigation_delete_label)
1658 .setIcon(android.R.drawable.ic_dialog_alert)
1659 .setMessage(R.string.my_navigation_delete_msg)
1660 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1661 @Override
1662 public void onClick(DialogInterface dialog, int whichButton) {
1663 deleteMyNavigationItem(itemUrl);
1664 }
1665 })
1666 .setNegativeButton(R.string.cancel, null)
1667 .show();
1668 }
1669
1670 private void deleteMyNavigationItem(final String itemUrl) {
1671 ContentResolver cr = this.getContext().getContentResolver();
1672 Cursor cursor = null;
1673
1674 try {
1675 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1676 new String[] {
1677 MyNavigationUtil.ID
1678 }, "url = ?", new String[] {
1679 itemUrl
1680 }, null);
1681 if (null != cursor && cursor.moveToFirst()) {
1682 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1683 cursor.getLong(0));
1684
1685 ContentValues values = new ContentValues();
1686 values.put(MyNavigationUtil.TITLE, "");
1687 values.put(MyNavigationUtil.URL, "ae://" + cursor.getLong(0) + "add-fav");
1688 values.put(MyNavigationUtil.WEBSITE, 0 + "");
1689 ByteArrayOutputStream os = new ByteArrayOutputStream();
1690 Bitmap bm = BitmapFactory.decodeResource(this.getContext().getResources(),
1691 R.raw.my_navigation_add);
1692 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1693 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1694 Log.d(LOGTAG, "deleteMyNavigationItem uri is : " + uri);
1695 cr.update(uri, values, null, null);
1696 } else {
1697 Log.e(LOGTAG, "deleteMyNavigationItem the item does not exist!");
1698 }
1699 } catch (IllegalStateException e) {
1700 Log.e(LOGTAG, "deleteMyNavigationItem", e);
1701 } finally {
1702 if (null != cursor) {
1703 cursor.close();
1704 }
1705 }
1706
1707 if (getCurrentTopWebView() != null) {
1708 getCurrentTopWebView().reload();
1709 }
1710 }
1711
1712 private String getNameFromUrl(String itemUrl) {
1713 ContentResolver cr = this.getContext().getContentResolver();
1714 Cursor cursor = null;
1715 String name = null;
1716
1717 try {
1718 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1719 new String[] {
1720 MyNavigationUtil.TITLE
1721 }, "url = ?", new String[] {
1722 itemUrl
1723 }, null);
1724 if (null != cursor && cursor.moveToFirst()) {
1725 name = cursor.getString(0);
1726 } else {
1727 Log.e(LOGTAG, "this item does not exist!");
1728 }
1729 } catch (IllegalStateException e) {
1730 Log.e(LOGTAG, "getNameFromUrl", e);
1731 } finally {
1732 if (null != cursor) {
1733 cursor.close();
1734 }
1735 }
1736 return name;
1737 }
1738
1739 private void updateMyNavigationThumbnail(final String itemUrl, WebView webView) {
1740 int width = mActivity.getResources().getDimensionPixelOffset(
1741 R.dimen.myNavigationThumbnailWidth);
1742 int height = mActivity.getResources().getDimensionPixelOffset(
1743 R.dimen.myNavigationThumbnailHeight);
1744
1745 final Bitmap bm = createScreenshot(webView, width, height);
1746
1747 if (bm == null) {
1748 Log.e(LOGTAG, "updateMyNavigationThumbnail bm is null!");
1749 return;
1750 }
1751
1752 final ContentResolver cr = mActivity.getContentResolver();
1753 new AsyncTask<Void, Void, Void>() {
1754 @Override
1755 protected Void doInBackground(Void... unused) {
1756 ContentResolver cr = mActivity.getContentResolver();
1757 Cursor cursor = null;
1758 try {
1759 cursor = cr.query(MyNavigationUtil.MY_NAVIGATION_URI,
1760 new String[] {
1761 MyNavigationUtil.ID
1762 }, "url = ?", new String[] {
1763 itemUrl
1764 }, null);
1765 if (null != cursor && cursor.moveToFirst()) {
1766 final ByteArrayOutputStream os = new ByteArrayOutputStream();
1767 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
1768
1769 ContentValues values = new ContentValues();
1770 values.put(MyNavigationUtil.THUMBNAIL, os.toByteArray());
1771 Uri uri = ContentUris.withAppendedId(MyNavigationUtil.MY_NAVIGATION_URI,
1772 cursor.getLong(0));
1773 Log.d(LOGTAG, "updateMyNavigationThumbnail uri is " + uri);
1774 cr.update(uri, values, null, null);
1775 os.close();
1776 }
1777 } catch (IllegalStateException e) {
1778 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1779 } catch (IOException e) {
1780 Log.e(LOGTAG, "updateMyNavigationThumbnail", e);
1781 } finally {
1782 if (null != cursor) {
1783 cursor.close();
1784 }
1785 }
1786 return null;
1787 }
1788 }.execute();
1789 }
1790
Michael Kolb8233fac2010-10-26 16:08:53 -07001791 /**
1792 * As the menu can be open when loading state changes
1793 * we must manually update the state of the stop/reload menu
1794 * item
1795 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001796 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001797 if (menu == null) {
1798 return;
1799 }
1800 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001801 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001802 menu.findItem(R.id.stop_menu_id):
1803 menu.findItem(R.id.reload_menu_id);
1804 if (src != null) {
1805 dest.setIcon(src.getIcon());
1806 dest.setTitle(src.getTitle());
1807 }
1808 }
1809
John Reck9c35b9c2012-05-30 10:08:50 -07001810 @Override
1811 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001812 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001813 // hold on to the menu reference here; it is used by the page callbacks
1814 // to update the menu based on loading state
1815 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001816 // Note: setVisible will decide whether an item is visible; while
1817 // setEnabled() will decide whether an item is enabled, which also means
1818 // whether the matching shortcut key will function.
1819 switch (mMenuState) {
1820 case EMPTY_MENU:
1821 if (mCurrentMenuState != mMenuState) {
1822 menu.setGroupVisible(R.id.MAIN_MENU, false);
1823 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1824 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1825 }
1826 break;
1827 default:
1828 if (mCurrentMenuState != mMenuState) {
1829 menu.setGroupVisible(R.id.MAIN_MENU, true);
1830 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1831 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1832 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001833 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001834 break;
1835 }
1836 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001837 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001838 }
1839
Michael Kolb4bf79712011-07-14 14:18:12 -07001840 @Override
1841 public void updateMenuState(Tab tab, Menu menu) {
1842 boolean canGoBack = false;
1843 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001844 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001845 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001846 if (tab != null) {
1847 canGoBack = tab.canGoBack();
1848 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001849 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001850 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001851 }
1852 final MenuItem back = menu.findItem(R.id.back_menu_id);
1853 back.setEnabled(canGoBack);
1854
1855 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001856
1857 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1858 forward.setEnabled(canGoForward);
1859
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001860 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1861 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001862 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001863 if (source != null && dest != null) {
1864 dest.setTitle(source.getTitle());
1865 dest.setIcon(source.getIcon());
1866 }
John Recke1a03a32011-09-14 17:04:16 -07001867 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001868
1869 // decide whether to show the share link option
1870 PackageManager pm = mActivity.getPackageManager();
1871 Intent send = new Intent(Intent.ACTION_SEND);
1872 send.setType("text/plain");
1873 ResolveInfo ri = pm.resolveActivity(send,
1874 PackageManager.MATCH_DEFAULT_ONLY);
1875 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1876
1877 boolean isNavDump = mSettings.enableNavDump();
1878 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1879 nav.setVisible(isNavDump);
1880 nav.setEnabled(isNavDump);
1881
1882 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001883 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1884 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001885 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1886 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001887
1888 // history and snapshots item are the members of COMBO menu group,
1889 // so if show history item, only make snapshots item invisible.
1890 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001891
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001892 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001893 }
1894
John Reck9c35b9c2012-05-30 10:08:50 -07001895 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001896 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001897 if (null == getCurrentTopWebView()) {
1898 return false;
1899 }
1900 if (mMenuIsDown) {
1901 // The shortcut action consumes the MENU. Even if it is still down,
1902 // it won't trigger the next shortcut action. In the case of the
1903 // shortcut action triggering a new activity, like Bookmarks, we
1904 // won't get onKeyUp for MENU. So it is important to reset it here.
1905 mMenuIsDown = false;
1906 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001907 if (mUi.onOptionsItemSelected(item)) {
1908 // ui callback handled it
1909 return true;
1910 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001911 switch (item.getItemId()) {
1912 // -- Main menu
1913 case R.id.new_tab_menu_id:
1914 openTabToHomePage();
1915 break;
1916
1917 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001918 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001919 break;
1920
Afzal Najamd4e33312012-04-26 01:54:01 -04001921 case R.id.close_other_tabs_id:
1922 closeOtherTabs();
1923 break;
1924
Michael Kolb8233fac2010-10-26 16:08:53 -07001925 case R.id.goto_menu_id:
1926 editUrl();
1927 break;
1928
1929 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001930 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1931 break;
1932
1933 case R.id.history_menu_id:
1934 bookmarksOrHistoryPicker(ComboViews.History);
1935 break;
1936
1937 case R.id.snapshots_menu_id:
1938 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001939 break;
1940
Michael Kolb8233fac2010-10-26 16:08:53 -07001941 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001942 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001943 break;
1944
1945 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001946 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001947 stopLoading();
1948 } else {
1949 getCurrentTopWebView().reload();
1950 }
1951 break;
1952
1953 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001954 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001955 break;
1956
1957 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001958 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001959 break;
1960
1961 case R.id.close_menu_id:
1962 // Close the subwindow if it exists.
1963 if (mTabControl.getCurrentSubWindow() != null) {
1964 dismissSubWindow(mTabControl.getCurrentTab());
1965 break;
1966 }
1967 closeCurrentTab();
1968 break;
1969
kaiyiza8b6dbb2013-07-29 18:11:22 +08001970 case R.id.exit_menu_id:
1971 String ret = SystemProperties.get("persist.debug.browsermonkeytest");
1972 if (ret != null && ret.equals("enable"))
1973 break;
luxiaolb40014b2013-07-19 10:01:43 +08001974 showExitDialog(mActivity);
1975 return true;
kaiyiza8b6dbb2013-07-29 18:11:22 +08001976
Michael Kolb8233fac2010-10-26 16:08:53 -07001977 case R.id.homepage_menu_id:
1978 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001979 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001980 break;
1981
1982 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001983 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001984 break;
1985
1986 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001987 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001988 break;
1989
John Reck2bc80422011-06-30 15:11:49 -07001990 case R.id.save_snapshot_menu_id:
1991 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001992 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001993 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001994 break;
1995
Michael Kolb8233fac2010-10-26 16:08:53 -07001996 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001997 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001998 break;
1999
John Recke1a03a32011-09-14 17:04:16 -07002000 case R.id.snapshot_go_live:
2001 goLive();
2002 return true;
2003
Michael Kolb8233fac2010-10-26 16:08:53 -07002004 case R.id.share_page_menu_id:
2005 Tab currentTab = mTabControl.getCurrentTab();
2006 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002007 return false;
2008 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08002009 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002010 break;
2011
2012 case R.id.dump_nav_menu_id:
2013 getCurrentTopWebView().debugDump();
2014 break;
2015
Michael Kolb8233fac2010-10-26 16:08:53 -07002016 case R.id.zoom_in_menu_id:
2017 getCurrentTopWebView().zoomIn();
2018 break;
2019
2020 case R.id.zoom_out_menu_id:
2021 getCurrentTopWebView().zoomOut();
2022 break;
2023
2024 case R.id.view_downloads_menu_id:
2025 viewDownloads();
2026 break;
2027
John Reck42229bc2011-08-19 13:26:43 -07002028 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07002029 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07002030 break;
2031
Michael Kolb8233fac2010-10-26 16:08:53 -07002032 case R.id.window_one_menu_id:
2033 case R.id.window_two_menu_id:
2034 case R.id.window_three_menu_id:
2035 case R.id.window_four_menu_id:
2036 case R.id.window_five_menu_id:
2037 case R.id.window_six_menu_id:
2038 case R.id.window_seven_menu_id:
2039 case R.id.window_eight_menu_id:
2040 {
2041 int menuid = item.getItemId();
2042 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
2043 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
2044 Tab desiredTab = mTabControl.getTab(id);
2045 if (desiredTab != null &&
2046 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07002047 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002048 }
2049 break;
2050 }
2051 }
2052 }
2053 break;
2054
2055 default:
2056 return false;
2057 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002058 return true;
2059 }
2060
John Reck68234a92012-04-19 15:27:12 -07002061 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
2062 implements OnCancelListener {
2063
2064 private Tab mTab;
2065 private Dialog mProgressDialog;
2066 private ContentValues mValues;
2067
2068 private SaveSnapshotTask(Tab tab) {
2069 mTab = tab;
2070 }
2071
2072 @Override
2073 protected void onPreExecute() {
2074 CharSequence message = mActivity.getText(R.string.saving_snapshot);
2075 mProgressDialog = ProgressDialog.show(mActivity, null, message,
2076 true, true, this);
2077 mValues = mTab.createSnapshotValues();
2078 }
2079
2080 @Override
2081 protected Long doInBackground(Void... params) {
2082 if (!mTab.saveViewState(mValues)) {
2083 return null;
2084 }
2085 if (isCancelled()) {
2086 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
2087 File file = mActivity.getFileStreamPath(path);
2088 if (!file.delete()) {
2089 file.deleteOnExit();
2090 }
2091 return null;
2092 }
2093 final ContentResolver cr = mActivity.getContentResolver();
2094 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
2095 if (result == null) {
2096 return null;
2097 }
2098 long id = ContentUris.parseId(result);
2099 return id;
2100 }
2101
2102 @Override
2103 protected void onPostExecute(Long id) {
2104 if (isCancelled()) {
2105 return;
2106 }
2107 mProgressDialog.dismiss();
2108 if (id == null) {
2109 Toast.makeText(mActivity, R.string.snapshot_failed,
2110 Toast.LENGTH_SHORT).show();
2111 return;
2112 }
2113 Bundle b = new Bundle();
2114 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
2115 mUi.showComboView(ComboViews.Snapshots, b);
2116 }
2117
2118 @Override
2119 public void onCancel(DialogInterface dialog) {
2120 cancel(true);
2121 }
2122 }
2123
Michael Kolb80f75082012-04-10 10:50:06 -07002124 @Override
2125 public void toggleUserAgent() {
2126 WebView web = getCurrentWebView();
2127 mSettings.toggleDesktopUseragent(web);
2128 web.loadUrl(web.getOriginalUrl());
2129 }
2130
2131 @Override
2132 public void findOnPage() {
2133 getCurrentTopWebView().showFindDialog(null, true);
2134 }
2135
2136 @Override
2137 public void openPreferences() {
2138 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2139 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
2140 getCurrentTopWebView().getUrl());
2141 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
2142 }
2143
2144 @Override
2145 public void bookmarkCurrentPage() {
2146 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
2147 if (bookmarkIntent != null) {
2148 mActivity.startActivity(bookmarkIntent);
2149 }
2150 }
2151
John Recke1a03a32011-09-14 17:04:16 -07002152 private void goLive() {
2153 Tab t = getCurrentTab();
2154 t.loadUrl(t.getUrl(), null);
2155 }
2156
luxiaolb40014b2013-07-19 10:01:43 +08002157 private void showExitDialog(final Activity activity) {
2158 new AlertDialog.Builder(activity)
2159 .setTitle(R.string.exit_browser_title)
2160 .setIcon(android.R.drawable.ic_dialog_alert)
2161 .setMessage(R.string.exit_browser_msg)
2162 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
2163 public void onClick(DialogInterface dialog, int which) {
2164 activity.moveTaskToBack(true);
2165 dialog.dismiss();
2166 }
2167 })
2168 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
2169 public void onClick(DialogInterface dialog, int which) {
2170 activity.finish();
2171 mHandler.postDelayed(new Runnable() {
2172 @Override
2173 public void run() {
2174 // TODO Auto-generated method stub
2175 mCrashRecoveryHandler.clearState(true);
2176 int pid = android.os.Process.myPid();
2177 android.os.Process.killProcess(pid);
2178 }
2179 }, 300);
2180 dialog.dismiss();
2181 }
2182 })
2183 .show();
2184 }
2185
Michael Kolb315d5022011-10-13 12:47:11 -07002186 @Override
2187 public void showPageInfo() {
2188 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
2189 }
2190
John Reck9c35b9c2012-05-30 10:08:50 -07002191 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002192 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08002193 // Let the History and Bookmark fragments handle menus they created.
2194 if (item.getGroupId() == R.id.CONTEXT_MENU) {
2195 return false;
2196 }
2197
Michael Kolb8233fac2010-10-26 16:08:53 -07002198 int id = item.getItemId();
2199 boolean result = true;
2200 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002201 // -- Browser context menu
2202 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002203 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07002204 case R.id.copy_link_context_menu_id:
2205 final WebView webView = getCurrentTopWebView();
2206 if (null == webView) {
2207 result = false;
2208 break;
2209 }
2210 final HashMap<String, WebView> hrefMap =
2211 new HashMap<String, WebView>();
2212 hrefMap.put("webview", webView);
2213 final Message msg = mHandler.obtainMessage(
2214 FOCUS_NODE_HREF, id, 0, hrefMap);
2215 webView.requestFocusNodeHref(msg);
2216 break;
2217
2218 default:
2219 // For other context menus
2220 result = onOptionsItemSelected(item);
2221 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002222 return result;
2223 }
2224
2225 /**
2226 * support programmatically opening the context menu
2227 */
2228 public void openContextMenu(View view) {
2229 mActivity.openContextMenu(view);
2230 }
2231
2232 /**
2233 * programmatically open the options menu
2234 */
2235 public void openOptionsMenu() {
2236 mActivity.openOptionsMenu();
2237 }
2238
John Reck9c35b9c2012-05-30 10:08:50 -07002239 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002240 public boolean onMenuOpened(int featureId, Menu menu) {
2241 if (mOptionsMenuOpen) {
2242 if (mConfigChanged) {
2243 // We do not need to make any changes to the state of the
2244 // title bar, since the only thing that happened was a
2245 // change in orientation
2246 mConfigChanged = false;
2247 } else {
2248 if (!mExtendedMenuOpen) {
2249 mExtendedMenuOpen = true;
2250 mUi.onExtendedMenuOpened();
2251 } else {
2252 // Switching the menu back to icon view, so show the
2253 // title bar once again.
2254 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002255 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002256 }
2257 }
2258 } else {
2259 // The options menu is closed, so open it, and show the title
2260 mOptionsMenuOpen = true;
2261 mConfigChanged = false;
2262 mExtendedMenuOpen = false;
2263 mUi.onOptionsMenuOpened();
2264 }
2265 return true;
2266 }
2267
John Reck9c35b9c2012-05-30 10:08:50 -07002268 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002269 public void onOptionsMenuClosed(Menu menu) {
2270 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002271 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002272 }
2273
John Reck9c35b9c2012-05-30 10:08:50 -07002274 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002275 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002276 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002277 }
2278
2279 // Helper method for getting the top window.
2280 @Override
2281 public WebView getCurrentTopWebView() {
2282 return mTabControl.getCurrentTopWebView();
2283 }
2284
2285 @Override
2286 public WebView getCurrentWebView() {
2287 return mTabControl.getCurrentWebView();
2288 }
2289
2290 /*
2291 * This method is called as a result of the user selecting the options
2292 * menu to see the download window. It shows the download window on top of
2293 * the current window.
2294 */
2295 void viewDownloads() {
2296 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2297 mActivity.startActivity(intent);
2298 }
2299
John Reck30b065e2011-07-19 10:58:05 -07002300 int getActionModeHeight() {
2301 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2302 new int[] { android.R.attr.actionBarSize });
2303 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2304 actionBarSizeTypedArray.recycle();
2305 return size;
2306 }
2307
Michael Kolb8233fac2010-10-26 16:08:53 -07002308 // action mode
2309
John Reck9c35b9c2012-05-30 10:08:50 -07002310 @Override
2311 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002312 mUi.onActionModeStarted(mode);
2313 mActionMode = mode;
2314 }
2315
2316 /*
2317 * True if a custom ActionMode (i.e. find or select) is in use.
2318 */
2319 @Override
2320 public boolean isInCustomActionMode() {
2321 return mActionMode != null;
2322 }
2323
2324 /*
2325 * End the current ActionMode.
2326 */
2327 @Override
2328 public void endActionMode() {
2329 if (mActionMode != null) {
2330 mActionMode.finish();
2331 }
2332 }
2333
2334 /*
2335 * Called by find and select when they are finished. Replace title bars
2336 * as necessary.
2337 */
John Reck9c35b9c2012-05-30 10:08:50 -07002338 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002339 public void onActionModeFinished(ActionMode mode) {
2340 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002341 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002342 mActionMode = null;
2343 }
2344
2345 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002346 final Tab tab = getCurrentTab();
2347 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002348 }
2349
2350 // bookmark handling
2351
2352 /**
2353 * add the current page as a bookmark to the given folder id
2354 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002355 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002356 * bookmarked, and if it is, edit that bookmark. If false, and
2357 * the site is already bookmarked, do not attempt to edit the
2358 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002359 */
2360 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002361 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002362 WebView w = getCurrentTopWebView();
2363 if (w == null) {
2364 return null;
2365 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002366 Intent i = new Intent(mActivity,
2367 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002368 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2369 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2370 String touchIconUrl = w.getTouchIconUrl();
2371 if (touchIconUrl != null) {
2372 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2373 WebSettings settings = w.getSettings();
2374 if (settings != null) {
2375 i.putExtra(AddBookmarkPage.USER_AGENT,
2376 settings.getUserAgentString());
2377 }
2378 }
2379 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2380 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2381 getDesiredThumbnailHeight(mActivity)));
2382 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002383 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002384 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2385 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002386 // Put the dialog at the upper right of the screen, covering the
2387 // star on the title bar.
2388 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002389 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002390 }
2391
2392 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002393 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002394 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002395 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002396 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002397 }
2398
2399 // thumbnails
2400
2401 /**
2402 * Return the desired width for thumbnail screenshots, which are stored in
2403 * the database, and used on the bookmarks screen.
2404 * @param context Context for finding out the density of the screen.
2405 * @return desired width for thumbnail screenshot.
2406 */
2407 static int getDesiredThumbnailWidth(Context context) {
2408 return context.getResources().getDimensionPixelOffset(
2409 R.dimen.bookmarkThumbnailWidth);
2410 }
2411
2412 /**
2413 * Return the desired height for thumbnail screenshots, which are stored in
2414 * the database, and used on the bookmarks screen.
2415 * @param context Context for finding out the density of the screen.
2416 * @return desired height for thumbnail screenshot.
2417 */
2418 static int getDesiredThumbnailHeight(Context context) {
2419 return context.getResources().getDimensionPixelOffset(
2420 R.dimen.bookmarkThumbnailHeight);
2421 }
2422
John Reck8cc92352011-07-06 17:41:52 -07002423 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002424 if (view == null || view.getContentHeight() == 0
2425 || view.getContentWidth() == 0) {
2426 return null;
2427 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002428 // We render to a bitmap 2x the desired size so that we can then
2429 // re-scale it with filtering since canvas.scale doesn't filter
2430 // This helps reduce aliasing at the cost of being slightly blurry
2431 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002432 int scaledWidth = width * filter_scale;
2433 int scaledHeight = height * filter_scale;
2434 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2435 || sThumbnailBitmap.getHeight() != scaledHeight) {
2436 if (sThumbnailBitmap != null) {
2437 sThumbnailBitmap.recycle();
2438 sThumbnailBitmap = null;
2439 }
2440 sThumbnailBitmap =
2441 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002442 }
John Reckd7dd9b22011-08-30 09:18:29 -07002443 Canvas canvas = new Canvas(sThumbnailBitmap);
2444 int contentWidth = view.getContentWidth();
2445 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2446 if (view instanceof BrowserWebView) {
2447 int dy = -((BrowserWebView)view).getTitleHeight();
2448 canvas.translate(0, dy * overviewScale);
2449 }
2450
2451 canvas.scale(overviewScale, overviewScale);
2452
2453 if (view instanceof BrowserWebView) {
2454 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002455 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002456 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002457 }
John Reckd7dd9b22011-08-30 09:18:29 -07002458 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2459 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002460 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002461 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002462 }
2463
John Reck34ef2672011-02-10 11:30:55 -08002464 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002465 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002466 // FIXME: Would like to make sure there is actually something to
2467 // draw, but the API for that (WebViewCore.pictureReady()) is not
2468 // currently accessible here.
2469
John Reck34ef2672011-02-10 11:30:55 -08002470 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002471 if (view == null) {
2472 // Tab was destroyed
2473 return;
2474 }
John Reck34ef2672011-02-10 11:30:55 -08002475 final String url = tab.getUrl();
2476 final String originalUrl = view.getOriginalUrl();
kaiyiz6e5b3e02013-08-19 20:02:01 +08002477 final String thumbnailUrl = mUpdateMyNavThumbnailUrl;
John Reck34ef2672011-02-10 11:30:55 -08002478 if (TextUtils.isEmpty(url)) {
2479 return;
2480 }
2481
kaiyiz6e5b3e02013-08-19 20:02:01 +08002482 //update My Navigation Thumbnails
2483 boolean isMyNavigationUrl = MyNavigationUtil.isMyNavigationUrl(mActivity, url);
2484 if (isMyNavigationUrl) {
2485 updateMyNavigationThumbnail(url, view);
2486 }
John Reck34ef2672011-02-10 11:30:55 -08002487 // Only update thumbnails for web urls (http(s)://), not for
2488 // about:, javascript:, data:, etc...
2489 // Unless it is a bookmarked site, then always update
2490 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2491 return;
2492 }
2493
kaiyiz6e5b3e02013-08-19 20:02:01 +08002494 if (url != null && mUpdateMyNavThumbnailUrl != null
2495 && Patterns.WEB_URL.matcher(url).matches()
2496 && Patterns.WEB_URL.matcher(mUpdateMyNavThumbnailUrl).matches()) {
2497 String urlHost = (new WebAddress(url)).getHost();
2498 String bookmarkHost = (new WebAddress(mUpdateMyNavThumbnailUrl)).getHost();
2499 if (urlHost == null || urlHost.length() == 0 || bookmarkHost == null
2500 || bookmarkHost.length() == 0) {
2501 return;
2502 }
2503 String urlDomain = urlHost.substring(urlHost.indexOf('.'), urlHost.length());
2504 String bookmarkDomain = bookmarkHost.substring(bookmarkHost.indexOf('.'),
2505 bookmarkHost.length());
2506 Log.d(LOGTAG, "addressUrl domain is " + urlDomain);
2507 Log.d(LOGTAG, "bookmarkUrl domain is " + bookmarkDomain);
2508 if (!bookmarkDomain.equals(urlDomain)) {
2509 return;
2510 }
2511 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002512 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2513 getDesiredThumbnailHeight(mActivity));
2514 if (bm == null) {
2515 return;
2516 }
2517
2518 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002519 new AsyncTask<Void, Void, Void>() {
2520 @Override
2521 protected Void doInBackground(Void... unused) {
2522 Cursor cursor = null;
2523 try {
2524 // TODO: Clean this up
kaiyiz6e5b3e02013-08-19 20:02:01 +08002525 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl,
2526 mUpdateMyNavThumbnail ? ((thumbnailUrl != null) ? thumbnailUrl : url)
2527 : url);
2528 if (mUpdateMyNavThumbnail) {
2529 mUpdateMyNavThumbnail = false;
2530 mUpdateMyNavThumbnailUrl = null;
2531 }
John Reck34ef2672011-02-10 11:30:55 -08002532 if (cursor != null && cursor.moveToFirst()) {
2533 final ByteArrayOutputStream os =
2534 new ByteArrayOutputStream();
2535 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002536
John Reck34ef2672011-02-10 11:30:55 -08002537 ContentValues values = new ContentValues();
2538 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002539
John Reck34ef2672011-02-10 11:30:55 -08002540 do {
John Reck617fd832011-02-16 14:35:59 -08002541 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002542 cr.update(Images.CONTENT_URI, values, null, null);
2543 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002544 }
John Reck34ef2672011-02-10 11:30:55 -08002545 } catch (IllegalStateException e) {
2546 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002547 } catch (SQLiteException s) {
2548 // Added for possible error when user tries to remove the same bookmark
2549 // that is being updated with a screen shot
2550 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002551 } finally {
2552 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002553 }
John Reck34ef2672011-02-10 11:30:55 -08002554 return null;
2555 }
2556 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002557 }
2558
2559 private class Copy implements OnMenuItemClickListener {
2560 private CharSequence mText;
2561
John Reck9c35b9c2012-05-30 10:08:50 -07002562 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002563 public boolean onMenuItemClick(MenuItem item) {
2564 copy(mText);
2565 return true;
2566 }
2567
2568 public Copy(CharSequence toCopy) {
2569 mText = toCopy;
2570 }
2571 }
2572
Leon Scroggins63c02662010-11-18 15:16:27 -05002573 private static class Download implements OnMenuItemClickListener {
2574 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002575 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002576 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002577 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002578 private static final String FALLBACK_EXTENSION = "dat";
2579 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002580
John Reck9c35b9c2012-05-30 10:08:50 -07002581 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002582 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002583 if (DataUri.isDataUri(mText)) {
2584 saveDataUri();
2585 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002586 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002587 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002588 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002589 return true;
2590 }
2591
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002592 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2593 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002594 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002595 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002596 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002597 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002598 }
George Mount387d45d2011-10-07 15:57:53 -07002599
2600 /**
2601 * Treats mText as a data URI and writes its contents to a file
2602 * based on the current time.
2603 */
2604 private void saveDataUri() {
2605 FileOutputStream outputStream = null;
2606 try {
2607 DataUri uri = new DataUri(mText);
2608 File target = getTarget(uri);
2609 outputStream = new FileOutputStream(target);
2610 outputStream.write(uri.getData());
2611 final DownloadManager manager =
2612 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2613 manager.addCompletedDownload(target.getName(),
2614 mActivity.getTitle().toString(), false,
2615 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002616 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002617 } catch (IOException e) {
2618 Log.e(LOGTAG, "Could not save data URL");
2619 } finally {
2620 if (outputStream != null) {
2621 try {
2622 outputStream.close();
2623 } catch (IOException e) {
2624 // ignore close errors
2625 }
2626 }
2627 }
2628 }
2629
2630 /**
2631 * Creates a File based on the current time stamp and uses
2632 * the mime type of the DataUri to get the extension.
2633 */
2634 private File getTarget(DataUri uri) throws IOException {
2635 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002636 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002637 String nameBase = format.format(new Date());
2638 String mimeType = uri.getMimeType();
2639 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2640 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2641 if (extension == null) {
2642 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2643 extension = FALLBACK_EXTENSION;
2644 }
2645 extension = "." + extension; // createTempFile needs the '.'
2646 File targetFile = File.createTempFile(nameBase, extension, dir);
2647 return targetFile;
2648 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002649 }
2650
Cary Clark8974d282010-11-22 10:46:05 -05002651 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002652 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002653
John Reck9c35b9c2012-05-30 10:08:50 -07002654 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002655 public boolean onMenuItemClick(MenuItem item) {
2656 if (mWebView != null) {
2657 return mWebView.selectText();
2658 }
2659 return false;
2660 }
2661
2662 public SelectText(WebView webView) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002663 if (BrowserWebView.isClassic()) {
2664 mWebView = WebViewClassic.fromWebView(webView);
2665 }
Cary Clark8974d282010-11-22 10:46:05 -05002666 }
2667
2668 }
2669
Michael Kolb8233fac2010-10-26 16:08:53 -07002670 /********************** TODO: UI stuff *****************************/
2671
2672 // these methods have been copied, they still need to be cleaned up
2673
2674 /****************** tabs ***************************************************/
2675
2676 // basic tab interactions:
2677
2678 // it is assumed that tabcontrol already knows about the tab
2679 protected void addTab(Tab tab) {
2680 mUi.addTab(tab);
2681 }
2682
2683 protected void removeTab(Tab tab) {
2684 mUi.removeTab(tab);
2685 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002686 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002687 }
2688
Michael Kolbf2055602011-04-09 17:20:03 -07002689 @Override
2690 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002691 // monkey protection against delayed start
2692 if (tab != null) {
2693 mTabControl.setCurrentTab(tab);
2694 // the tab is guaranteed to have a webview after setCurrentTab
2695 mUi.setActiveTab(tab);
2696 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002697 }
2698
John Reck8bcafc12011-08-29 16:43:02 -07002699 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002700 Tab current = mTabControl.getCurrentTab();
2701 if (current != null
2702 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002703 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002704 }
2705 }
2706
John Reck26b18322011-06-21 13:08:58 -07002707 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002708 // Dismiss the subwindow if applicable.
2709 dismissSubWindow(appTab);
2710 // Since we might kill the WebView, remove it from the
2711 // content view first.
2712 mUi.detachTab(appTab);
2713 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002714 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002715 // TODO: analyze why the remove and add are necessary
2716 mUi.attachTab(appTab);
2717 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002718 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002719 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002720 } else {
2721 // If the tab was the current tab, we have to attach
2722 // it to the view system again.
2723 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002724 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002725 }
2726 }
2727
2728 // Remove the sub window if it exists. Also called by TabControl when the
2729 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002730 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002731 public void dismissSubWindow(Tab tab) {
2732 removeSubWindow(tab);
2733 // dismiss the subwindow. This will destroy the WebView.
2734 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002735 WebView wv = getCurrentTopWebView();
2736 if (wv != null) {
2737 wv.requestFocus();
2738 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002739 }
2740
2741 @Override
2742 public void removeSubWindow(Tab t) {
2743 if (t.getSubWebView() != null) {
2744 mUi.removeSubWindow(t.getSubViewContainer());
2745 }
2746 }
2747
2748 @Override
2749 public void attachSubWindow(Tab tab) {
2750 if (tab.getSubWebView() != null) {
2751 mUi.attachSubWindow(tab.getSubViewContainer());
2752 getCurrentTopWebView().requestFocus();
2753 }
2754 }
2755
Mathew Inwood29721c22011-06-29 17:55:24 +01002756 private Tab showPreloadedTab(final UrlData urlData) {
2757 if (!urlData.isPreloaded()) {
2758 return null;
2759 }
2760 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2761 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2762 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002763 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002764 // Could not submit query. Fallback to regular tab creation
2765 tabControl.destroy();
2766 return null;
2767 }
2768 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002769 // check tab count and make room for new tab
2770 if (!mTabControl.canCreateNewTab()) {
2771 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2772 if (leastUsed != null) {
2773 closeTab(leastUsed);
2774 }
2775 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002776 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002777 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002778 mTabControl.addPreloadedTab(t);
2779 addTab(t);
2780 setActiveTab(t);
2781 return t;
2782 }
2783
Michael Kolb7bcafde2011-05-09 13:55:59 -07002784 // open a non inconito tab with the given url data
2785 // and set as active tab
2786 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002787 Tab tab = showPreloadedTab(urlData);
2788 if (tab == null) {
2789 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002790 if ((tab != null) && !urlData.isEmpty()) {
2791 loadUrlDataIn(tab, urlData);
2792 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002793 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002794 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002795 }
2796
Michael Kolb843510f2010-12-09 10:51:49 -08002797 @Override
2798 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002799 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002800 }
2801
Michael Kolb8233fac2010-10-26 16:08:53 -07002802 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002803 public Tab openIncognitoTab() {
2804 return openTab(INCOGNITO_URI, true, true, false);
2805 }
2806
2807 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002808 public Tab openTab(String url, boolean incognito, boolean setActive,
2809 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002810 return openTab(url, incognito, setActive, useCurrent, null);
2811 }
2812
2813 @Override
2814 public Tab openTab(String url, Tab parent, boolean setActive,
2815 boolean useCurrent) {
2816 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2817 setActive, useCurrent, parent);
2818 }
2819
2820 public Tab openTab(String url, boolean incognito, boolean setActive,
2821 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002822 Tab tab = createNewTab(incognito, setActive, useCurrent);
2823 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002824 if (parent != null && parent != tab) {
2825 parent.addChildTab(tab);
2826 }
Michael Kolb519d2282011-05-09 17:03:19 -07002827 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002828 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002829 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002830 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002831 return tab;
2832 }
2833
2834 // this method will attempt to create a new tab
2835 // incognito: private browsing tab
2836 // setActive: ste tab as current tab
2837 // useCurrent: if no new tab can be created, return current tab
2838 private Tab createNewTab(boolean incognito, boolean setActive,
2839 boolean useCurrent) {
2840 Tab tab = null;
2841 if (mTabControl.canCreateNewTab()) {
2842 tab = mTabControl.createNewTab(incognito);
2843 addTab(tab);
2844 if (setActive) {
2845 setActiveTab(tab);
2846 }
2847 } else {
2848 if (useCurrent) {
2849 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002850 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002851 } else {
2852 mUi.showMaxTabsWarning();
2853 }
2854 }
2855 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002856 }
2857
John Reck2bc80422011-06-30 15:11:49 -07002858 @Override
2859 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2860 SnapshotTab tab = null;
2861 if (mTabControl.canCreateNewTab()) {
2862 tab = mTabControl.createSnapshotTab(snapshotId);
2863 addTab(tab);
2864 if (setActive) {
2865 setActiveTab(tab);
2866 }
2867 } else {
2868 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002869 }
2870 return tab;
2871 }
2872
Michael Kolb8233fac2010-10-26 16:08:53 -07002873 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002874 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002875 * @return boolean True if we successfully switched to a different tab. If
2876 * the indexth tab is null, or if that tab is the same as
2877 * the current one, return false.
2878 */
2879 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002880 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002881 Tab currentTab = mTabControl.getCurrentTab();
2882 if (tab == null || tab == currentTab) {
2883 return false;
2884 }
2885 setActiveTab(tab);
2886 return true;
2887 }
2888
2889 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002890 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002891 closeCurrentTab(false);
2892 }
2893
2894 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002895 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002896 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002897 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002898 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002899 return;
2900 }
Michael Kolbc831b632011-05-11 09:30:34 -07002901 final Tab current = mTabControl.getCurrentTab();
2902 final int pos = mTabControl.getCurrentPosition();
2903 Tab newTab = current.getParent();
2904 if (newTab == null) {
2905 newTab = mTabControl.getTab(pos + 1);
2906 if (newTab == null) {
2907 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002908 }
2909 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002910 if (andQuit) {
2911 mTabControl.setCurrentTab(newTab);
2912 closeTab(current);
2913 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002914 // Close window
2915 closeTab(current);
2916 }
2917 }
2918
2919 /**
2920 * Close the tab, remove its associated title bar, and adjust mTabControl's
2921 * current tab to a valid value.
2922 */
2923 @Override
2924 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002925 if (tab == mTabControl.getCurrentTab()) {
2926 closeCurrentTab();
2927 } else {
2928 removeTab(tab);
2929 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002930 }
2931
Afzal Najamd4e33312012-04-26 01:54:01 -04002932 /**
2933 * Close all tabs except the current one
2934 */
2935 @Override
2936 public void closeOtherTabs() {
2937 int inactiveTabs = mTabControl.getTabCount() - 1;
2938 for (int i = inactiveTabs; i >= 0; i--) {
2939 Tab tab = mTabControl.getTab(i);
2940 if (tab != mTabControl.getCurrentTab()) {
2941 removeTab(tab);
2942 }
2943 }
2944 }
2945
Michael Kolb8233fac2010-10-26 16:08:53 -07002946 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002947 protected void loadUrlFromContext(String url) {
2948 Tab tab = getCurrentTab();
2949 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002950 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002951 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002952 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002953 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002954 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002955 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002956 }
2957 }
2958 }
2959
2960 /**
2961 * Load the URL into the given WebView and update the title bar
2962 * to reflect the new load. Call this instead of WebView.loadUrl
2963 * directly.
2964 * @param view The WebView used to load url.
2965 * @param url The URL to load.
2966 */
John Reck71e51422011-07-01 16:49:28 -07002967 @Override
2968 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002969 loadUrl(tab, url, null);
2970 }
2971
2972 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2973 if (tab != null) {
2974 dismissSubWindow(tab);
2975 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002976 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002977 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002978 }
2979
2980 /**
2981 * Load UrlData into a Tab and update the title bar to reflect the new
2982 * load. Call this instead of UrlData.loadIn directly.
2983 * @param t The Tab used to load.
2984 * @param data The UrlData being loaded.
2985 */
2986 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002987 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002988 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002989 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002990 } else {
John Reck38b39652012-06-05 09:22:59 -07002991 if (t != null && data.mDisableUrlOverride) {
2992 t.disableUrlOverridingForLoad();
2993 }
John Reck26b18322011-06-21 13:08:58 -07002994 loadUrl(t, data.mUrl, data.mHeaders);
2995 }
2996 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002997 }
2998
John Reck30c714c2010-12-16 17:30:34 -08002999 @Override
3000 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08003001 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07003002 if (tab.canGoBack()) {
3003 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08003004 } else {
John Reckef654f12011-07-12 16:42:08 -07003005 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08003006 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003007 }
3008
3009 void goBackOnePageOrQuit() {
3010 Tab current = mTabControl.getCurrentTab();
3011 if (current == null) {
3012 /*
3013 * Instead of finishing the activity, simply push this to the back
3014 * of the stack and let ActivityManager to choose the foreground
3015 * activity. As BrowserActivity is singleTask, it will be always the
3016 * root of the task. So we can use either true or false for
3017 * moveTaskToBack().
3018 */
luxiaolb40014b2013-07-19 10:01:43 +08003019 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003020 return;
3021 }
John Reckef654f12011-07-12 16:42:08 -07003022 if (current.canGoBack()) {
3023 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07003024 } else {
3025 // Check to see if we are closing a window that was created by
3026 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07003027 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07003028 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07003029 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07003030 // Now we close the other tab
3031 closeTab(current);
3032 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07003033 /*
3034 * Instead of finishing the activity, simply push this to the back
3035 * of the stack and let ActivityManager to choose the foreground
3036 * activity. As BrowserActivity is singleTask, it will be always the
3037 * root of the task. So we can use either true or false for
3038 * moveTaskToBack().
3039 */
luxiaolb40014b2013-07-19 10:01:43 +08003040 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07003041 }
3042 }
3043 }
3044
3045 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07003046 * helper method for key handler
3047 * returns the current tab if it can't advance
3048 */
Michael Kolbc831b632011-05-11 09:30:34 -07003049 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003050 int pos = mTabControl.getCurrentPosition() + 1;
3051 if (pos >= mTabControl.getTabCount()) {
3052 pos = 0;
3053 }
3054 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003055 }
3056
3057 /**
3058 * helper method for key handler
3059 * returns the current tab if it can't advance
3060 */
Michael Kolbc831b632011-05-11 09:30:34 -07003061 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08003062 int pos = mTabControl.getCurrentPosition() - 1;
3063 if ( pos < 0) {
3064 pos = mTabControl.getTabCount() - 1;
3065 }
3066 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07003067 }
3068
John Reckbcef87f2012-02-03 14:58:34 -08003069 boolean isMenuOrCtrlKey(int keyCode) {
3070 return (KeyEvent.KEYCODE_MENU == keyCode)
3071 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
3072 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
3073 }
3074
Michael Kolb0035fad2011-03-14 13:25:28 -07003075 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07003076 * handle key events in browser
3077 *
3078 * @param keyCode
3079 * @param event
3080 * @return true if handled, false to pass to super
3081 */
John Reck9c35b9c2012-05-30 10:08:50 -07003082 @Override
3083 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05003084 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07003085 // Even if MENU is already held down, we need to call to super to open
3086 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08003087 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003088 mMenuIsDown = true;
3089 return false;
3090 }
Michael Kolb8233fac2010-10-26 16:08:53 -07003091
Cary Clark8ff8c662010-12-29 15:03:05 -05003092 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07003093 Tab tab = getCurrentTab();
3094 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05003095
Cary Clark160bbb92011-01-10 11:17:07 -05003096 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
3097 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05003098
Michael Kolb8233fac2010-10-26 16:08:53 -07003099 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07003100 case KeyEvent.KEYCODE_TAB:
3101 if (event.isCtrlPressed()) {
3102 if (event.isShiftPressed()) {
3103 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07003104 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003105 } else {
3106 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07003107 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07003108 }
3109 return true;
3110 }
3111 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07003112 case KeyEvent.KEYCODE_SPACE:
3113 // WebView/WebTextView handle the keys in the KeyDown. As
3114 // the Activity's shortcut keys are only handled when WebView
3115 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05003116 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003117 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05003118 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003119 pageDown();
3120 }
3121 return true;
3122 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05003123 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08003124 event.startTracking();
3125 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07003126 case KeyEvent.KEYCODE_FORWARD:
3127 if (!noModifiers) break;
3128 tab.goForward();
3129 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05003130 case KeyEvent.KEYCODE_DPAD_LEFT:
3131 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003132 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05003133 return true;
3134 }
3135 break;
3136 case KeyEvent.KEYCODE_DPAD_RIGHT:
3137 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07003138 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05003139 return true;
3140 }
3141 break;
3142 case KeyEvent.KEYCODE_A:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003143 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003144 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05003145 return true;
3146 }
3147 break;
Michael Kolba4183062011-01-16 10:43:21 -08003148// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003149 case KeyEvent.KEYCODE_C:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08003150 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00003151 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05003152 return true;
3153 }
3154 break;
Michael Kolba4183062011-01-16 10:43:21 -08003155// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003156// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003157// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003158// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08003159// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003160// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08003161// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003162// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08003163// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003164// case KeyEvent.KEYCODE_M: // unused
3165// case KeyEvent.KEYCODE_N: // in Chrome: new window
3166// case KeyEvent.KEYCODE_O: // in Chrome: open file
3167// case KeyEvent.KEYCODE_P: // in Chrome: print page
3168// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003169// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05003170// case KeyEvent.KEYCODE_S: // in Chrome: saves page
3171 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003172 // we can't use the ctrl/shift flags, they check for
3173 // exclusive use of a modifier
3174 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05003175 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07003176 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05003177 } else {
3178 openTabToHomePage();
3179 }
3180 return true;
3181 }
3182 break;
3183// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
3184// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07003185// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05003186// case KeyEvent.KEYCODE_X: // text view intercepts to cut
3187// case KeyEvent.KEYCODE_Y: // unused
3188// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07003189 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08003190 // it is a regular key and webview is not null
3191 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07003192 }
3193
John Reck9c35b9c2012-05-30 10:08:50 -07003194 @Override
3195 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08003196 switch(keyCode) {
3197 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07003198 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07003199 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08003200 return true;
3201 }
3202 break;
3203 }
3204 return false;
3205 }
3206
John Reck9c35b9c2012-05-30 10:08:50 -07003207 @Override
3208 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08003209 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07003210 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08003211 if (KeyEvent.KEYCODE_MENU == keyCode
3212 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07003213 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07003214 }
3215 }
Cary Clark160bbb92011-01-10 11:17:07 -05003216 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07003217 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07003218 case KeyEvent.KEYCODE_BACK:
3219 if (event.isTracking() && !event.isCanceled()) {
3220 onBackKey();
3221 return true;
3222 }
3223 break;
3224 }
3225 return false;
3226 }
3227
3228 public boolean isMenuDown() {
3229 return mMenuIsDown;
3230 }
3231
John Reck9c35b9c2012-05-30 10:08:50 -07003232 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00003233 public void setupAutoFill(Message message) {
3234 // Open the settings activity at the AutoFill profile fragment so that
3235 // the user can create a new profile. When they return, we will dispatch
3236 // the message so that we can autofill the form using their new profile.
3237 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
3238 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
3239 AutoFillSettingsFragment.class.getName());
3240 mAutoFillSetupMessage = message;
3241 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
3242 }
John Reckb3417f02011-01-14 11:01:05 -08003243
John Reck9c35b9c2012-05-30 10:08:50 -07003244 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07003245 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07003246 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07003247 return true;
3248 }
3249
John Reck1cf4b792011-07-26 10:22:22 -07003250 @Override
3251 public boolean shouldCaptureThumbnails() {
3252 return mUi.shouldCaptureThumbnails();
3253 }
3254
Michael Kolbc3af0672011-08-09 10:24:41 -07003255 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07003256 public boolean supportsVoice() {
3257 PackageManager pm = mActivity.getPackageManager();
3258 List activities = pm.queryIntentActivities(new Intent(
3259 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
3260 return activities.size() != 0;
3261 }
3262
3263 @Override
3264 public void startVoiceRecognizer() {
3265 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
3266 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
3267 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
3268 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
3269 mActivity.startActivityForResult(voice, VOICE_RESULT);
3270 }
3271
3272 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003273 public void setBlockEvents(boolean block) {
3274 mBlockEvents = block;
3275 }
3276
John Reck9c35b9c2012-05-30 10:08:50 -07003277 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003278 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003279 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003280 }
3281
John Reck9c35b9c2012-05-30 10:08:50 -07003282 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003283 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07003284 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003285 }
3286
John Reck9c35b9c2012-05-30 10:08:50 -07003287 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003288 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003289 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003290 }
3291
John Reck9c35b9c2012-05-30 10:08:50 -07003292 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003293 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003294 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003295 }
3296
John Reck9c35b9c2012-05-30 10:08:50 -07003297 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003298 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003299 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003300 }
3301
Michael Kolb8233fac2010-10-26 16:08:53 -07003302}