blob: 729e79ea7f9d59b91f431c55e4cfca51fed92e40 [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;
luxiaolb40014b2013-07-19 10:01:43 +080020import android.app.AlertDialog;
John Reck68234a92012-04-19 15:27:12 -070021import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070022import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070023import android.app.ProgressDialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070024import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070025import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070026import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070027import android.content.ContentValues;
28import android.content.Context;
John Reck68234a92012-04-19 15:27:12 -070029import android.content.DialogInterface;
30import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070031import android.content.Intent;
32import android.content.pm.PackageManager;
33import android.content.pm.ResolveInfo;
34import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070035import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050036import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070037import android.database.Cursor;
38import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020039import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070040import android.graphics.Bitmap;
41import android.graphics.Canvas;
kaiyiza016da12013-08-26 17:50:22 +080042import android.net.ConnectivityManager;
43import android.net.NetworkInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070044import android.net.Uri;
45import android.net.http.SslError;
kaiyiza016da12013-08-26 17:50:22 +080046import android.net.wifi.WifiManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070047import android.os.AsyncTask;
48import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070049import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070050import android.os.Handler;
51import android.os.Message;
52import android.os.PowerManager;
53import android.os.PowerManager.WakeLock;
kaiyiza8b6dbb2013-07-29 18:11:22 +080054import android.os.SystemProperties;
Ben Murdoch8029a772010-11-16 11:58:21 +000055import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070056import android.provider.Browser;
57import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070058import android.provider.BrowserContract.Images;
59import android.provider.ContactsContract;
60import android.provider.ContactsContract.Intents.Insert;
kaiyiza016da12013-08-26 17:50:22 +080061import android.provider.Settings;
Michael Kolb0b129122012-06-04 16:31:58 -070062import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070063import android.text.TextUtils;
64import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080065import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070066import android.view.ActionMode;
67import android.view.ContextMenu;
68import android.view.ContextMenu.ContextMenuInfo;
69import android.view.Gravity;
70import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070071import android.view.Menu;
72import android.view.MenuInflater;
73import android.view.MenuItem;
74import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070075import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070076import android.view.View;
77import android.webkit.CookieManager;
78import android.webkit.CookieSyncManager;
79import android.webkit.HttpAuthHandler;
George Mount387d45d2011-10-07 15:57:53 -070080import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070081import android.webkit.SslErrorHandler;
82import android.webkit.ValueCallback;
83import android.webkit.WebChromeClient;
84import android.webkit.WebIconDatabase;
85import android.webkit.WebSettings;
86import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000087import android.webkit.WebViewClassic;
Leon Scrogginsac993842011-02-02 12:54:07 -050088import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070089
Michael Kolb4bd767d2011-05-27 11:33:55 -070090import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070091import com.android.browser.UI.ComboViews;
John Reck1cf4b792011-07-26 10:22:22 -070092import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070093import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070094
Michael Kolb8233fac2010-10-26 16:08:53 -070095import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -070096import java.io.File;
97import java.io.FileOutputStream;
98import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -070099import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -0700100import java.text.DateFormat;
101import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -0700102import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -0700103import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -0700104import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700105import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800106import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200107import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700108import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700109
110/**
111 * Controller for browser
112 */
113public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700114 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700115
116 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800117 private static final String SEND_APP_ID_EXTRA =
118 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700119 private static final String INCOGNITO_URI = "browser:incognito";
kaiyiza016da12013-08-26 17:50:22 +0800120 private static final String PROP_NETSWITCH = "persist.env.browser.netswitch";
121 private static final String INTENT_WIFI_SELECTION_DATA_CONNECTION =
122 "android.net.wifi.cmcc.WIFI_SELECTION_DATA_CONNECTION";
Michael Kolb8233fac2010-10-26 16:08:53 -0700123
124 // public message ids
125 public final static int LOAD_URL = 1001;
126 public final static int STOP_LOAD = 1002;
127
128 // Message Ids
129 private static final int FOCUS_NODE_HREF = 102;
130 private static final int RELEASE_WAKELOCK = 107;
131
132 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
133
134 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800135 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700136
137 private static final int EMPTY_MENU = -1;
138
Michael Kolb8233fac2010-10-26 16:08:53 -0700139 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700140 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700141 final static int PREFERENCES_PAGE = 3;
142 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000143 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700144 final static int VOICE_RESULT = 6;
Ben Murdoch8029a772010-11-16 11:58:21 +0000145
Michael Kolb8233fac2010-10-26 16:08:53 -0700146 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
147
148 // As the ids are dynamically created, we can't guarantee that they will
149 // be in sequence, so this static array maps ids to a window number.
150 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
151 { R.id.window_one_menu_id, R.id.window_two_menu_id,
152 R.id.window_three_menu_id, R.id.window_four_menu_id,
153 R.id.window_five_menu_id, R.id.window_six_menu_id,
154 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
155
156 // "source" parameter for Google search through search key
157 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
158 // "source" parameter for Google search through simplily type
159 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
160
George Mount387d45d2011-10-07 15:57:53 -0700161 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700162 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
163
John Reckd7dd9b22011-08-30 09:18:29 -0700164 // A bitmap that is re-used in createScreenshot as scratch space
165 private static Bitmap sThumbnailBitmap;
166
Michael Kolb8233fac2010-10-26 16:08:53 -0700167 private Activity mActivity;
168 private UI mUi;
169 private TabControl mTabControl;
170 private BrowserSettings mSettings;
171 private WebViewFactory mFactory;
172
173 private WakeLock mWakeLock;
174
175 private UrlHandler mUrlHandler;
176 private UploadHandler mUploadHandler;
177 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700178 private PageDialogsHandler mPageDialogsHandler;
179 private NetworkStateHandler mNetworkHandler;
180
Ben Murdoch8029a772010-11-16 11:58:21 +0000181 private Message mAutoFillSetupMessage;
182
Michael Kolb8233fac2010-10-26 16:08:53 -0700183 private boolean mShouldShowErrorConsole;
kaiyiza016da12013-08-26 17:50:22 +0800184 private boolean mNetworkShouldNotify = true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700185
186 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
187
188 // FIXME, temp address onPrepareMenu performance problem.
189 // When we move everything out of view, we should rewrite this.
190 private int mCurrentMenuState = 0;
191 private int mMenuState = R.id.MAIN_MENU;
192 private int mOldMenuState = EMPTY_MENU;
193 private Menu mCachedMenu;
194
Michael Kolb8233fac2010-10-26 16:08:53 -0700195 private boolean mMenuIsDown;
196
197 // For select and find, we keep track of the ActionMode so that
198 // finish() can be called as desired.
199 private ActionMode mActionMode;
200
201 /**
202 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
203 * of whether the configuration has changed. The first onMenuOpened call
204 * after a configuration change is simply a reopening of the same menu
205 * (i.e. mIconView did not change).
206 */
207 private boolean mConfigChanged;
208
209 /**
210 * Keeps track of whether the options menu is open. This is important in
211 * determining whether to show or hide the title bar overlay
212 */
213 private boolean mOptionsMenuOpen;
214
215 /**
216 * Whether or not the options menu is in its bigger, popup menu form. When
217 * true, we want the title bar overlay to be gone. When false, we do not.
218 * Only meaningful if mOptionsMenuOpen is true.
219 */
220 private boolean mExtendedMenuOpen;
221
Michael Kolb8233fac2010-10-26 16:08:53 -0700222 private boolean mActivityPaused = true;
223 private boolean mLoadStopped;
224
225 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500226 // Checks to see when the bookmarks database has changed, and updates the
227 // Tabs' notion of whether they represent bookmarked sites.
228 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700229 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700230
Michael Kolbc3af0672011-08-09 10:24:41 -0700231 private boolean mBlockEvents;
232
Michael Kolb0b129122012-06-04 16:31:58 -0700233 private String mVoiceResult;
234
George Mount3636d0a2011-11-21 09:08:21 -0800235 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 mActivity = browser;
237 mSettings = BrowserSettings.getInstance();
238 mTabControl = new TabControl(this);
239 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700240 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800241 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700242 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700243
244 mUrlHandler = new UrlHandler(this);
245 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700246 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
247
Michael Kolb8233fac2010-10-26 16:08:53 -0700248 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500249 mBookmarksObserver = new ContentObserver(mHandler) {
250 @Override
251 public void onChange(boolean selfChange) {
252 int size = mTabControl.getTabCount();
253 for (int i = 0; i < size; i++) {
254 mTabControl.getTab(i).updateBookmarkedStatus();
255 }
256 }
257
258 };
259 browser.getContentResolver().registerContentObserver(
260 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700261
262 mNetworkHandler = new NetworkStateHandler(mActivity, this);
263 // Start watching the default geolocation permissions
264 mSystemAllowGeolocationOrigins =
265 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
266 mSystemAllowGeolocationOrigins.start();
267
John Reckaf262e72011-07-25 13:55:44 -0700268 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700269 }
270
John Reck9c35b9c2012-05-30 10:08:50 -0700271 @Override
272 public void start(final Intent intent) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800273 if (BrowserWebView.isClassic()) WebViewClassic.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800274 // mCrashRecoverHandler has any previously saved state.
275 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700276 }
277
George Mount3636d0a2011-11-21 09:08:21 -0800278 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700279 // Unless the last browser usage was within 24 hours, destroy any
280 // remaining incognito tabs.
281
282 Calendar lastActiveDate = icicle != null ?
283 (Calendar) icicle.getSerializable("lastActiveDate") : null;
284 Calendar today = Calendar.getInstance();
285 Calendar yesterday = Calendar.getInstance();
286 yesterday.add(Calendar.DATE, -1);
287
Patrick Scott7d50a932011-02-04 09:27:26 -0500288 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700289 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800290 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700291
Patrick Scott7d50a932011-02-04 09:27:26 -0500292 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700293 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500294 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000295
Michael Kolbc831b632011-05-11 09:30:34 -0700296 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500297 // Not able to restore so we go ahead and clear session cookies. We
298 // must do this before trying to login the user as we don't want to
299 // clear any session cookies set during login.
300 CookieManager.getInstance().removeSessionCookie();
301 }
302
Patrick Scottd43e75a2011-03-14 14:47:23 -0400303 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500304 new Runnable() {
305 @Override public void run() {
George Mount3636d0a2011-11-21 09:08:21 -0800306 onPreloginFinished(icicle, intent, currentTabId,
307 restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500308 }
309 });
310 }
311
Michael Kolbc831b632011-05-11 09:30:34 -0700312 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
George Mount3636d0a2011-11-21 09:08:21 -0800313 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700314 if (currentTabId == -1) {
John Reck1cf4b792011-07-26 10:22:22 -0700315 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800316 if (intent == null) {
317 // This won't happen under common scenarios. The icicle is
318 // not null, but there aren't any tabs to restore.
319 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700320 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800321 final Bundle extra = intent.getExtras();
322 // Create an initial tab.
323 // If the intent is ACTION_VIEW and data is not null, the Browser is
324 // invoked to view the content by another application. In this case,
325 // the tab will be close when exit.
326 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
327 Tab t = null;
328 if (urlData.isEmpty()) {
329 t = openTabToHomePage();
330 } else {
331 t = openTab(urlData);
332 }
333 if (t != null) {
334 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
335 }
336 WebView webView = t.getWebView();
337 if (extra != null) {
338 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
339 if (scale > 0 && scale <= 1000) {
340 webView.setInitialScale(scale);
341 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700342 }
343 }
John Reckd8c74522011-06-14 08:45:00 -0700344 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700345 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700346 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500347 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700348 List<Tab> tabs = mTabControl.getTabs();
349 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
350 for (Tab t : tabs) {
351 restoredTabs.add(t.getId());
352 }
353 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700354 if (tabs.size() == 0) {
355 openTabToHomePage();
356 }
John Reck1cf4b792011-07-26 10:22:22 -0700357 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 // TabControl.restoreState() will create a new tab even if
359 // restoring the state fails.
360 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800361 // Intent is non-null when framework thinks the browser should be
362 // launching with a new intent (icicle is null).
363 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700364 mIntentHandler.onNewIntent(intent);
365 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700366 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700367 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700368 String jsFlags = getSettings().getJsEngineFlags();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800369 if (jsFlags.trim().length() != 0 && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000370 WebViewClassic.fromWebView(getCurrentWebView()).setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700371 }
George Mount3636d0a2011-11-21 09:08:21 -0800372 if (intent != null
373 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700374 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800375 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700376 }
377
John Reck1cf4b792011-07-26 10:22:22 -0700378 private static class PruneThumbnails implements Runnable {
379 private Context mContext;
380 private List<Long> mIds;
381
382 PruneThumbnails(Context context, List<Long> preserveIds) {
383 mContext = context.getApplicationContext();
384 mIds = preserveIds;
385 }
386
387 @Override
388 public void run() {
389 ContentResolver cr = mContext.getContentResolver();
390 if (mIds == null || mIds.size() == 0) {
391 cr.delete(Thumbnails.CONTENT_URI, null, null);
392 } else {
393 int length = mIds.size();
394 StringBuilder where = new StringBuilder();
395 where.append(Thumbnails._ID);
396 where.append(" not in (");
397 for (int i = 0; i < length; i++) {
398 where.append(mIds.get(i));
399 if (i < (length - 1)) {
400 where.append(",");
401 }
402 }
403 where.append(")");
404 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
405 }
406 }
407
408 }
409
Michael Kolb1514bb72010-11-22 09:11:48 -0800410 @Override
411 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700412 return mFactory;
413 }
414
415 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800416 public void onSetWebView(Tab tab, WebView view) {
417 mUi.onSetWebView(tab, view);
418 }
419
420 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800421 public void createSubWindow(Tab tab) {
422 endActionMode();
423 WebView mainView = tab.getWebView();
424 WebView subView = mFactory.createWebView((mainView == null)
425 ? false
426 : mainView.isPrivateBrowsingEnabled());
427 mUi.createSubWindow(tab, subView);
428 }
429
430 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700431 public Context getContext() {
432 return mActivity;
433 }
434
435 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700436 public Activity getActivity() {
437 return mActivity;
438 }
439
440 void setUi(UI ui) {
441 mUi = ui;
442 }
443
Michael Kolbaf63dba2012-05-16 12:58:05 -0700444 @Override
445 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700446 return mSettings;
447 }
448
449 IntentHandler getIntentHandler() {
450 return mIntentHandler;
451 }
452
453 @Override
454 public UI getUi() {
455 return mUi;
456 }
457
458 int getMaxTabs() {
459 return mActivity.getResources().getInteger(R.integer.max_tabs);
460 }
461
462 @Override
463 public TabControl getTabControl() {
464 return mTabControl;
465 }
466
Michael Kolb1bf23132010-11-19 12:55:12 -0800467 @Override
468 public List<Tab> getTabs() {
469 return mTabControl.getTabs();
470 }
471
John Reckaf262e72011-07-25 13:55:44 -0700472 // Open the icon database.
473 private void openIconDatabase() {
474 // We have to call getInstance on the UI thread
475 final WebIconDatabase instance = WebIconDatabase.getInstance();
476 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000477
John Reckaf262e72011-07-25 13:55:44 -0700478 @Override
479 public void run() {
480 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700481 }
John Reckaf262e72011-07-25 13:55:44 -0700482 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700483 }
484
485 private void startHandler() {
486 mHandler = new Handler() {
487
488 @Override
489 public void handleMessage(Message msg) {
490 switch (msg.what) {
491 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700492 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700493 break;
494 case FOCUS_NODE_HREF:
495 {
496 String url = (String) msg.getData().get("url");
497 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500498 String src = (String) msg.getData().get("src");
499 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700500 if (TextUtils.isEmpty(url)) {
501 break;
502 }
503 HashMap focusNodeMap = (HashMap) msg.obj;
504 WebView view = (WebView) focusNodeMap.get("webview");
505 // Only apply the action if the top window did not change.
506 if (getCurrentTopWebView() != view) {
507 break;
508 }
509 switch (msg.arg1) {
510 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700511 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700512 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500513 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700514 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500515 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500516 case R.id.open_newtab_context_menu_id:
517 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700518 openTab(url, parent,
519 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500520 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700521 case R.id.copy_link_context_menu_id:
522 copy(url);
523 break;
524 case R.id.save_link_context_menu_id:
525 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500526 DownloadHandler.onDownloadStartNoStream(
Andreas Sandblad8e4ce662012-06-11 11:02:49 +0200527 mActivity, url, view.getSettings().getUserAgentString(),
luxiaol62677b02013-07-22 07:54:49 +0800528 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700529 break;
530 }
531 break;
532 }
533
534 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700535 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700536 break;
537
538 case STOP_LOAD:
539 stopLoading();
540 break;
541
542 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700543 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700544 mWakeLock.release();
545 // if we reach here, Browser should be still in the
546 // background loading after WAKELOCK_TIMEOUT (5-min).
547 // To avoid burning the battery, stop loading.
548 mTabControl.stopAllLoading();
549 }
550 break;
551
552 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800553 Tab tab = (Tab) msg.obj;
554 if (tab != null) {
555 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700556 }
557 break;
kaiyiz591110b2013-08-06 17:11:06 +0800558
559 case OPEN_MENU:
560 if (!mOptionsMenuOpen && mActivity != null ) {
561 mActivity.openOptionsMenu();
562 }
563 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700564 }
565 }
566 };
567
568 }
569
John Reckef654f12011-07-12 16:42:08 -0700570 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200571 public Tab getCurrentTab() {
572 return mTabControl.getCurrentTab();
573 }
574
Michael Kolbba99c5d2010-11-29 14:57:41 -0800575 @Override
576 public void shareCurrentPage() {
577 shareCurrentPage(mTabControl.getCurrentTab());
578 }
579
580 private void shareCurrentPage(Tab tab) {
581 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800582 sharePage(mActivity, tab.getTitle(),
583 tab.getUrl(), tab.getFavicon(),
584 createScreenshot(tab.getWebView(),
585 getDesiredThumbnailWidth(mActivity),
586 getDesiredThumbnailHeight(mActivity)));
587 }
588 }
589
Michael Kolb8233fac2010-10-26 16:08:53 -0700590 /**
591 * Share a page, providing the title, url, favicon, and a screenshot. Uses
592 * an {@link Intent} to launch the Activity chooser.
593 * @param c Context used to launch a new Activity.
594 * @param title Title of the page. Stored in the Intent with
595 * {@link Intent#EXTRA_SUBJECT}
596 * @param url URL of the page. Stored in the Intent with
597 * {@link Intent#EXTRA_TEXT}
598 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
599 * with {@link Browser#EXTRA_SHARE_FAVICON}
600 * @param screenshot Bitmap of a screenshot of the page. Stored in the
601 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
602 */
603 static final void sharePage(Context c, String title, String url,
604 Bitmap favicon, Bitmap screenshot) {
605 Intent send = new Intent(Intent.ACTION_SEND);
606 send.setType("text/plain");
607 send.putExtra(Intent.EXTRA_TEXT, url);
608 send.putExtra(Intent.EXTRA_SUBJECT, title);
609 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
610 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
611 try {
612 c.startActivity(Intent.createChooser(send, c.getString(
613 R.string.choosertitle_sharevia)));
614 } catch(android.content.ActivityNotFoundException ex) {
615 // if no app handles it, do nothing
616 }
617 }
618
619 private void copy(CharSequence text) {
620 ClipboardManager cm = (ClipboardManager) mActivity
621 .getSystemService(Context.CLIPBOARD_SERVICE);
622 cm.setText(text);
623 }
624
625 // lifecycle
626
John Reck9c35b9c2012-05-30 10:08:50 -0700627 @Override
628 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700629 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800630 // update the menu in case of a locale change
631 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800632 if (mOptionsMenuOpen) {
633 mActivity.closeOptionsMenu();
634 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
635 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700636 if (mPageDialogsHandler != null) {
637 mPageDialogsHandler.onConfigurationChanged(config);
638 }
639 mUi.onConfigurationChanged(config);
640 }
641
642 @Override
643 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700644 if (!mUi.isWebShowing()) {
645 mUi.showWeb(false);
646 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700647 mIntentHandler.onNewIntent(intent);
648 }
649
John Reck9c35b9c2012-05-30 10:08:50 -0700650 @Override
651 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800652 if (mUi.isCustomViewShowing()) {
653 hideCustomView();
654 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700655 if (mActivityPaused) {
656 Log.e(LOGTAG, "BrowserActivity is already paused.");
657 return;
658 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700659 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800660 Tab tab = mTabControl.getCurrentTab();
661 if (tab != null) {
662 tab.pause();
663 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700664 if (mWakeLock == null) {
665 PowerManager pm = (PowerManager) mActivity
666 .getSystemService(Context.POWER_SERVICE);
667 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
668 }
Michael Kolb70976932010-11-30 11:34:01 -0800669 mWakeLock.acquire();
670 mHandler.sendMessageDelayed(mHandler
671 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
672 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700673 }
674 mUi.onPause();
675 mNetworkHandler.onPause();
676
677 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100678 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700679 if (sThumbnailBitmap != null) {
680 sThumbnailBitmap.recycle();
681 sThumbnailBitmap = null;
682 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700683 }
684
John Reck9c35b9c2012-05-30 10:08:50 -0700685 @Override
686 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700687 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800688 Bundle saveState = createSaveState();
689
690 // crash recovery manages all save & restore state
691 mCrashRecoveryHandler.writeState(saveState);
692 mSettings.setLastRunPaused(true);
693 }
694
695 /**
696 * Save the current state to outState. Does not write the state to
697 * disk.
698 * @return Bundle containing the current state of all tabs.
699 */
700 /* package */ Bundle createSaveState() {
701 Bundle saveState = new Bundle();
702 mTabControl.saveState(saveState);
703 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700704 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800705 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700706 }
George Mount3636d0a2011-11-21 09:08:21 -0800707 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700708 }
709
John Reck9c35b9c2012-05-30 10:08:50 -0700710 @Override
711 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700712 if (!mActivityPaused) {
713 Log.e(LOGTAG, "BrowserActivity is already resumed.");
714 return;
715 }
George Mount3636d0a2011-11-21 09:08:21 -0800716 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700717 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800718 Tab current = mTabControl.getCurrentTab();
719 if (current != null) {
720 current.resume();
721 resumeWebViewTimers(current);
722 }
John Reckf57c0292011-07-21 18:15:39 -0700723 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200724
Michael Kolb8233fac2010-10-26 16:08:53 -0700725 mUi.onResume();
726 mNetworkHandler.onResume();
727 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100728 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700729 if (mVoiceResult != null) {
730 mUi.onVoiceResult(mVoiceResult);
731 mVoiceResult = null;
732 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700733 }
734
John Reckf57c0292011-07-21 18:15:39 -0700735 private void releaseWakeLock() {
736 if (mWakeLock != null && mWakeLock.isHeld()) {
737 mHandler.removeMessages(RELEASE_WAKELOCK);
738 mWakeLock.release();
739 }
740 }
741
Michael Kolb70976932010-11-30 11:34:01 -0800742 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800743 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800744 * @param tab guaranteed non-null
745 */
746 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700747 boolean inLoad = tab.inPageLoad();
748 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
749 CookieSyncManager.getInstance().startSync();
750 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100751 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700752 }
753 }
754
Michael Kolb70976932010-11-30 11:34:01 -0800755 /**
756 * Pause all WebView timers using the WebView of the given tab
757 * @param tab
758 * @return true if the timers are paused or tab is null
759 */
760 private boolean pauseWebViewTimers(Tab tab) {
761 if (tab == null) {
762 return true;
763 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700764 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100765 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700766 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700767 }
Michael Kolb70976932010-11-30 11:34:01 -0800768 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700769 }
770
John Reck9c35b9c2012-05-30 10:08:50 -0700771 @Override
772 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800773 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700774 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
775 mUploadHandler = null;
776 }
777 if (mTabControl == null) return;
778 mUi.onDestroy();
779 // Remove the current tab and sub window
780 Tab t = mTabControl.getCurrentTab();
781 if (t != null) {
782 dismissSubWindow(t);
783 removeTab(t);
784 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500785 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700786 // Destroy all the tabs
787 mTabControl.destroy();
788 WebIconDatabase.getInstance().close();
789 // Stop watching the default geolocation permissions
790 mSystemAllowGeolocationOrigins.stop();
791 mSystemAllowGeolocationOrigins = null;
792 }
793
794 protected boolean isActivityPaused() {
795 return mActivityPaused;
796 }
797
John Reck9c35b9c2012-05-30 10:08:50 -0700798 @Override
799 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700800 mTabControl.freeMemory();
801 }
802
803 @Override
804 public boolean shouldShowErrorConsole() {
805 return mShouldShowErrorConsole;
806 }
807
808 protected void setShouldShowErrorConsole(boolean show) {
809 if (show == mShouldShowErrorConsole) {
810 // Nothing to do.
811 return;
812 }
813 mShouldShowErrorConsole = show;
814 Tab t = mTabControl.getCurrentTab();
815 if (t == null) {
816 // There is no current tab so we cannot toggle the error console
817 return;
818 }
819 mUi.setShouldShowErrorConsole(t, show);
820 }
821
822 @Override
823 public void stopLoading() {
824 mLoadStopped = true;
825 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700826 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700827 if (w != null) {
828 w.stopLoading();
829 mUi.onPageStopped(tab);
830 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700831 }
832
833 boolean didUserStopLoading() {
834 return mLoadStopped;
835 }
836
kaiyiza016da12013-08-26 17:50:22 +0800837 private void handleNetworkNotify(WebView view) {
838 ConnectivityManager conMgr = (ConnectivityManager) this.getContext().getSystemService(
839 Context.CONNECTIVITY_SERVICE);
840 WifiManager wifiMgr = (WifiManager) this.getContext()
841 .getSystemService(Context.WIFI_SERVICE);
842 int networkSwitchTypeOK = this.getContext().getResources()
843 .getInteger(R.integer.netswitch_type_remind);
844
845 if (wifiMgr.isWifiEnabled()) {
846 NetworkInfo mNetworkInfo = conMgr.getActiveNetworkInfo();
847 if (mNetworkInfo == null
848 || (mNetworkInfo != null && (mNetworkInfo.getType() !=
849 ConnectivityManager.TYPE_WIFI))) {
850 int isReminder = Settings.System.getInt(
851 mActivity.getContentResolver(),
852 this.getContext().getResources()
853 .getString(R.string.network_switch_remind_type),
854 networkSwitchTypeOK);
855
856 if (isReminder == networkSwitchTypeOK) {
857 Intent intent = new Intent(
858 INTENT_WIFI_SELECTION_DATA_CONNECTION);
859 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
860 this.getContext().startActivity(intent);
861 }
862 mNetworkShouldNotify = false;
863 }
864 } else {
865 if (!mNetworkHandler.isNetworkUp()) {
866 view.setNetworkAvailable(false);
867 Log.v(LOGTAG, "handleNetworkNotify() Wlan is not enabled.");
868 }
869 }
870 }
871
Michael Kolb8233fac2010-10-26 16:08:53 -0700872 // WebViewController
873
874 @Override
John Reck324d4402011-01-11 16:56:42 -0800875 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700876
877 // We've started to load a new page. If there was a pending message
878 // to save a screenshot then we will now take the new page and save
879 // an incorrect screenshot. Therefore, remove any pending thumbnail
880 // messages from the queue.
881 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800882 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700883
884 // reset sync timer to avoid sync starts during loading a page
885 CookieSyncManager.getInstance().resetSync();
886
kaiyiza016da12013-08-26 17:50:22 +0800887 if (SystemProperties.getBoolean(PROP_NETSWITCH, false)) {
888 if (!mNetworkHandler.isNetworkUp()) {
889 Log.d(LOGTAG, "onPageStarted() network unavailable");
890 if (mNetworkShouldNotify) {
891 handleNetworkNotify(view);
892 } else {
893 view.setNetworkAvailable(false);
894 }
895 mNetworkShouldNotify = false;
896 } else {
897 Log.d(LOGTAG, "onPageStarted() network available");
898 if (mNetworkShouldNotify) {
899 handleNetworkNotify(view);
900 }
901 mNetworkShouldNotify = false;
902 }
903 } else {
904 if (!mNetworkHandler.isNetworkUp()) {
905 view.setNetworkAvailable(false);
906 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700907 }
908
909 // when BrowserActivity just starts, onPageStarted may be called before
910 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
911 // to start the timer. As we won't switch tabs while an activity is in
912 // pause state, we can ensure calling resume and pause in pair.
913 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800914 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700915 }
916 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700917 endActionMode();
918
John Reck30c714c2010-12-16 17:30:34 -0800919 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700920
John Reck324d4402011-01-11 16:56:42 -0800921 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700922 // update the bookmark database for favicon
923 maybeUpdateFavicon(tab, null, url, favicon);
924
925 Performance.tracePageStart(url);
926
927 // Performance probe
928 if (false) {
929 Performance.onPageStarted();
930 }
931
932 }
933
934 @Override
John Reck324d4402011-01-11 16:56:42 -0800935 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700936 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800937 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200938
Michael Kolb8233fac2010-10-26 16:08:53 -0700939 // Performance probe
940 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800941 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700942 }
943
944 Performance.tracePageFinished();
945 }
946
947 @Override
John Reck30c714c2010-12-16 17:30:34 -0800948 public void onProgressChanged(Tab tab) {
949 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700950
951 if (newProgress == 100) {
952 CookieSyncManager.getInstance().sync();
953 // onProgressChanged() may continue to be called after the main
954 // frame has finished loading, as any remaining sub frames continue
955 // to load. We'll only get called once though with newProgress as
956 // 100 when everything is loaded. (onPageFinished is called once
957 // when the main frame completes loading regardless of the state of
958 // any sub frames so calls to onProgressChanges may continue after
959 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800960 if (tab.inPageLoad()) {
961 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200962 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
963 // pause the WebView timer and release the wake lock if it is
964 // finished while BrowserActivity is in pause state.
965 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700966 }
John Reckd9862372012-02-21 15:04:50 -0800967 if (!tab.isPrivateBrowsingEnabled()
968 && !TextUtils.isEmpty(tab.getUrl())
969 && !tab.isSnapshot()) {
970 // Only update the bookmark screenshot if the user did not
971 // cancel the load early and there is not already
972 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700973 if (tab.shouldUpdateThumbnail() &&
974 (tab.inForeground() && !didUserStopLoading()
975 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800976 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
977 mHandler.sendMessageDelayed(mHandler.obtainMessage(
978 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
979 500);
980 }
981 }
982 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700983 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800984 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700985 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800986 // still loading
987 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -0700988 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800989 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700990 }
991 }
John Reck30c714c2010-12-16 17:30:34 -0800992 mUi.onProgressChanged(tab);
993 }
994
995 @Override
Steve Block2466eff2011-10-03 15:33:09 +0100996 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800997 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700998 }
999
1000 @Override
1001 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001002 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -08001003 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -08001004 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -07001005 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
1006 return;
1007 }
1008 // Update the title in the history database if not in private browsing mode
1009 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -07001010 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -07001011 }
1012 }
1013
1014 @Override
1015 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001016 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001017 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
1018 }
1019
1020 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -08001021 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
1022 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07001023 }
1024
1025 @Override
1026 public boolean shouldOverrideKeyEvent(KeyEvent event) {
1027 if (mMenuIsDown) {
1028 // only check shortcut key when MENU is held
1029 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
1030 event);
1031 } else {
1032 return false;
1033 }
1034 }
1035
1036 @Override
John Reck997b1b72012-04-19 18:08:25 -07001037 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 if (!isActivityPaused()) {
1039 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -07001040 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001041 } else {
John Reck997b1b72012-04-19 18:08:25 -07001042 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -07001043 }
1044 }
John Reck997b1b72012-04-19 18:08:25 -07001045 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07001046 }
1047
1048 @Override
John Reck324d4402011-01-11 16:56:42 -08001049 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001050 // Don't save anything in private browsing mode
1051 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -08001052 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -07001053
John Reck324d4402011-01-11 16:56:42 -08001054 if (TextUtils.isEmpty(url)
1055 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001056 return;
1057 }
John Reckf57c0292011-07-21 18:15:39 -07001058 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001059 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001060 }
1061
1062 @Override
1063 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1064 AsyncTask<Void, Void, String[]> task =
1065 new AsyncTask<Void, Void, String[]>() {
1066 @Override
1067 public String[] doInBackground(Void... unused) {
1068 return Browser.getVisitedHistory(mActivity.getContentResolver());
1069 }
1070 @Override
1071 public void onPostExecute(String[] result) {
1072 callback.onReceiveValue(result);
1073 }
1074 };
1075 task.execute();
1076 }
1077
1078 @Override
1079 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1080 final HttpAuthHandler handler, final String host,
1081 final String realm) {
1082 String username = null;
1083 String password = null;
1084
1085 boolean reuseHttpAuthUsernamePassword
1086 = handler.useHttpAuthUsernamePassword();
1087
1088 if (reuseHttpAuthUsernamePassword && view != null) {
1089 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1090 if (credentials != null && credentials.length == 2) {
1091 username = credentials[0];
1092 password = credentials[1];
1093 }
1094 }
1095
1096 if (username != null && password != null) {
1097 handler.proceed(username, password);
1098 } else {
Ben Murdochfdd37112011-08-05 15:48:14 +01001099 if (tab.inForeground() && !handler.suppressDialog()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001100 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1101 } else {
1102 handler.cancel();
1103 }
1104 }
1105 }
1106
1107 @Override
1108 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001109 String contentDisposition, String mimetype, String referer,
1110 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001111 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001112 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001113 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001114 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001115 // This Tab was opened for the sole purpose of downloading a
1116 // file. Remove it.
1117 if (tab == mTabControl.getCurrentTab()) {
1118 // In this case, the Tab is still on top.
1119 goBackOnePageOrQuit();
1120 } else {
1121 // In this case, it is not.
1122 closeTab(tab);
1123 }
1124 }
1125 }
1126
1127 @Override
1128 public Bitmap getDefaultVideoPoster() {
1129 return mUi.getDefaultVideoPoster();
1130 }
1131
1132 @Override
1133 public View getVideoLoadingProgressView() {
1134 return mUi.getVideoLoadingProgressView();
1135 }
1136
1137 @Override
1138 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1139 SslError error) {
1140 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1141 }
1142
Patrick Scott92066772011-03-10 08:46:27 -05001143 @Override
1144 public void showAutoLogin(Tab tab) {
1145 assert tab.inForeground();
1146 // Update the title bar to show the auto-login request.
1147 mUi.showAutoLogin(tab);
1148 }
1149
1150 @Override
1151 public void hideAutoLogin(Tab tab) {
1152 assert tab.inForeground();
1153 mUi.hideAutoLogin(tab);
1154 }
1155
Michael Kolb8233fac2010-10-26 16:08:53 -07001156 // helper method
1157
1158 /*
1159 * Update the favorites icon if the private browsing isn't enabled and the
1160 * icon is valid.
1161 */
1162 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1163 final String url, Bitmap favicon) {
1164 if (favicon == null) {
1165 return;
1166 }
1167 if (!tab.isPrivateBrowsingEnabled()) {
1168 Bookmarks.updateFavicon(mActivity
1169 .getContentResolver(), originalUrl, url, favicon);
1170 }
1171 }
1172
Leon Scroggins4cd97792010-12-03 15:31:56 -05001173 @Override
1174 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001175 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001176 mUi.bookmarkedStatusHasChanged(tab);
1177 }
1178
Michael Kolb8233fac2010-10-26 16:08:53 -07001179 // end WebViewController
1180
1181 protected void pageUp() {
1182 getCurrentTopWebView().pageUp(false);
1183 }
1184
1185 protected void pageDown() {
1186 getCurrentTopWebView().pageDown(false);
1187 }
1188
1189 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001190 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001191 public void editUrl() {
1192 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001193 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001194 }
1195
John Reck9c35b9c2012-05-30 10:08:50 -07001196 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001197 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001198 WebChromeClient.CustomViewCallback callback) {
1199 if (tab.inForeground()) {
1200 if (mUi.isCustomViewShowing()) {
1201 callback.onCustomViewHidden();
1202 return;
1203 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001204 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001205 // Save the menu state and set it to empty while the custom
1206 // view is showing.
1207 mOldMenuState = mMenuState;
1208 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001209 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001210 }
1211 }
1212
1213 @Override
1214 public void hideCustomView() {
1215 if (mUi.isCustomViewShowing()) {
1216 mUi.onHideCustomView();
1217 // Reset the old menu state.
1218 mMenuState = mOldMenuState;
1219 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001220 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001221 }
1222 }
1223
John Reck9c35b9c2012-05-30 10:08:50 -07001224 @Override
1225 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001226 Intent intent) {
1227 if (getCurrentTopWebView() == null) return;
1228 switch (requestCode) {
1229 case PREFERENCES_PAGE:
1230 if (resultCode == Activity.RESULT_OK && intent != null) {
1231 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001232 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001233 mTabControl.removeParentChildRelationShips();
1234 }
1235 }
1236 break;
1237 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001238 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001239 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001240 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001241 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001242 case AUTOFILL_SETUP:
1243 // Determine whether a profile was actually set up or not
1244 // and if so, send the message back to the WebTextView to
1245 // fill the form with the new profile.
1246 if (getSettings().getAutoFillProfile() != null) {
1247 mAutoFillSetupMessage.sendToTarget();
1248 mAutoFillSetupMessage = null;
1249 }
1250 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001251 case COMBO_VIEW:
1252 if (intent == null || resultCode != Activity.RESULT_OK) {
1253 break;
1254 }
John Reck3ba45532011-08-11 16:26:53 -07001255 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001256 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1257 Tab t = getCurrentTab();
1258 Uri uri = intent.getData();
1259 loadUrl(t, uri.toString());
1260 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1261 String[] urls = intent.getStringArrayExtra(
1262 ComboViewActivity.EXTRA_OPEN_ALL);
1263 Tab parent = getCurrentTab();
1264 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001265 if (url != null) {
1266 parent = openTab(url, parent,
1267 !mSettings.openInBackground(), true);
1268 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001269 }
1270 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1271 long id = intent.getLongExtra(
1272 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1273 if (id >= 0) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08001274 if (BrowserWebView.isClassic()) {
1275 createNewSnapshotTab(id, true);
1276 } else {
1277 Toast.makeText(mActivity, "Snapshot Tab requires WebViewClassic",
1278 Toast.LENGTH_LONG).show();
1279 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001280 }
1281 }
1282 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001283 case VOICE_RESULT:
1284 if (resultCode == Activity.RESULT_OK && intent != null) {
1285 ArrayList<String> results = intent.getStringArrayListExtra(
1286 RecognizerIntent.EXTRA_RESULTS);
1287 if (results.size() >= 1) {
1288 mVoiceResult = results.get(0);
1289 }
1290 }
1291 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001292 default:
1293 break;
1294 }
1295 getCurrentTopWebView().requestFocus();
1296 }
1297
1298 /**
1299 * Open the Go page.
1300 * @param startWithHistory If true, open starting on the history tab.
1301 * Otherwise, start with the bookmarks tab.
1302 */
1303 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001304 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001305 if (mTabControl.getCurrentWebView() == null) {
1306 return;
1307 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001308 // clear action mode
1309 if (isInCustomActionMode()) {
1310 endActionMode();
1311 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001312 Bundle extras = new Bundle();
1313 // Disable opening in a new window if we have maxed out the windows
1314 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1315 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001316 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001317 }
1318
1319 // combo view callbacks
1320
Michael Kolb8233fac2010-10-26 16:08:53 -07001321 // key handling
1322 protected void onBackKey() {
1323 if (!mUi.onBackKey()) {
1324 WebView subwindow = mTabControl.getCurrentSubWindow();
1325 if (subwindow != null) {
1326 if (subwindow.canGoBack()) {
1327 subwindow.goBack();
1328 } else {
1329 dismissSubWindow(mTabControl.getCurrentTab());
1330 }
1331 } else {
1332 goBackOnePageOrQuit();
1333 }
1334 }
1335 }
1336
Michael Kolb4bd767d2011-05-27 11:33:55 -07001337 protected boolean onMenuKey() {
1338 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001339 }
1340
Michael Kolb8233fac2010-10-26 16:08:53 -07001341 // menu handling and state
1342 // TODO: maybe put into separate handler
1343
John Reck9c35b9c2012-05-30 10:08:50 -07001344 @Override
1345 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001346 if (mMenuState == EMPTY_MENU) {
1347 return false;
1348 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001349 MenuInflater inflater = mActivity.getMenuInflater();
1350 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001351 return true;
1352 }
1353
John Reck9c35b9c2012-05-30 10:08:50 -07001354 @Override
1355 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001356 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001357 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001358 return;
1359 }
1360 if (!(v instanceof WebView)) {
1361 return;
1362 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001363 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001364 WebView.HitTestResult result = webview.getHitTestResult();
1365 if (result == null) {
1366 return;
1367 }
1368
1369 int type = result.getType();
1370 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1371 Log.w(LOGTAG,
1372 "We should not show context menu when nothing is touched");
1373 return;
1374 }
1375 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1376 // let TextView handles context menu
1377 return;
1378 }
1379
1380 // Note, http://b/issue?id=1106666 is requesting that
1381 // an inflated menu can be used again. This is not available
1382 // yet, so inflate each time (yuk!)
1383 MenuInflater inflater = mActivity.getMenuInflater();
1384 inflater.inflate(R.menu.browsercontext, menu);
1385
1386 // Show the correct menu group
1387 final String extra = result.getExtra();
Michael Kolbc159c1a2011-12-15 16:06:38 -08001388 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001389 menu.setGroupVisible(R.id.PHONE_MENU,
1390 type == WebView.HitTestResult.PHONE_TYPE);
1391 menu.setGroupVisible(R.id.EMAIL_MENU,
1392 type == WebView.HitTestResult.EMAIL_TYPE);
1393 menu.setGroupVisible(R.id.GEO_MENU,
1394 type == WebView.HitTestResult.GEO_TYPE);
1395 menu.setGroupVisible(R.id.IMAGE_MENU,
1396 type == WebView.HitTestResult.IMAGE_TYPE
1397 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1398 menu.setGroupVisible(R.id.ANCHOR_MENU,
1399 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1400 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001401 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1402 || type == WebView.HitTestResult.PHONE_TYPE
1403 || type == WebView.HitTestResult.EMAIL_TYPE
1404 || type == WebView.HitTestResult.GEO_TYPE;
1405 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1406 if (hitText) {
1407 menu.findItem(R.id.select_text_menu_id)
1408 .setOnMenuItemClickListener(new SelectText(webview));
1409 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 // Setup custom handling depending on the type
1411 switch (type) {
1412 case WebView.HitTestResult.PHONE_TYPE:
1413 menu.setHeaderTitle(Uri.decode(extra));
1414 menu.findItem(R.id.dial_context_menu_id).setIntent(
1415 new Intent(Intent.ACTION_VIEW, Uri
1416 .parse(WebView.SCHEME_TEL + extra)));
1417 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1418 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1419 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1420 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1421 addIntent);
1422 menu.findItem(R.id.copy_phone_context_menu_id)
1423 .setOnMenuItemClickListener(
1424 new Copy(extra));
1425 break;
1426
1427 case WebView.HitTestResult.EMAIL_TYPE:
1428 menu.setHeaderTitle(extra);
1429 menu.findItem(R.id.email_context_menu_id).setIntent(
1430 new Intent(Intent.ACTION_VIEW, Uri
1431 .parse(WebView.SCHEME_MAILTO + extra)));
1432 menu.findItem(R.id.copy_mail_context_menu_id)
1433 .setOnMenuItemClickListener(
1434 new Copy(extra));
1435 break;
1436
1437 case WebView.HitTestResult.GEO_TYPE:
1438 menu.setHeaderTitle(extra);
1439 menu.findItem(R.id.map_context_menu_id).setIntent(
1440 new Intent(Intent.ACTION_VIEW, Uri
1441 .parse(WebView.SCHEME_GEO
1442 + URLEncoder.encode(extra))));
1443 menu.findItem(R.id.copy_geo_context_menu_id)
1444 .setOnMenuItemClickListener(
1445 new Copy(extra));
1446 break;
1447
1448 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1449 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001450 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001451 // decide whether to show the open link in new tab option
1452 boolean showNewTab = mTabControl.canCreateNewTab();
1453 MenuItem newTabItem
1454 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001455 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001456 ? R.string.contextmenu_openlink_newwindow_background
1457 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001458 newTabItem.setVisible(showNewTab);
1459 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001460 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1461 newTabItem.setOnMenuItemClickListener(
1462 new MenuItem.OnMenuItemClickListener() {
1463 @Override
1464 public boolean onMenuItemClick(MenuItem item) {
1465 final HashMap<String, WebView> hrefMap =
1466 new HashMap<String, WebView>();
1467 hrefMap.put("webview", webview);
1468 final Message msg = mHandler.obtainMessage(
1469 FOCUS_NODE_HREF,
1470 R.id.open_newtab_context_menu_id,
1471 0, hrefMap);
1472 webview.requestFocusNodeHref(msg);
1473 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001474 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001475 });
1476 } else {
1477 newTabItem.setOnMenuItemClickListener(
1478 new MenuItem.OnMenuItemClickListener() {
1479 @Override
1480 public boolean onMenuItemClick(MenuItem item) {
1481 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001482 openTab(extra, parent,
1483 !mSettings.openInBackground(),
1484 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001485 return true;
1486 }
1487 });
1488 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001489 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001490 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1491 break;
1492 }
1493 // otherwise fall through to handle image part
1494 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001495 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1496 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001497 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1498 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001499 shareItem.setOnMenuItemClickListener(
1500 new MenuItem.OnMenuItemClickListener() {
1501 @Override
1502 public boolean onMenuItemClick(MenuItem item) {
1503 sharePage(mActivity, null, extra, null,
1504 null);
1505 return true;
1506 }
1507 }
1508 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001509 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001510 menu.findItem(R.id.view_image_context_menu_id)
1511 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1512 @Override
1513 public boolean onMenuItemClick(MenuItem item) {
1514 openTab(extra, mTabControl.getCurrentTab(), true, true);
1515 return false;
1516 }
1517 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001518 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1519 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1520 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001521 menu.findItem(R.id.set_wallpaper_context_menu_id).
1522 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1523 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001524 break;
1525
1526 default:
1527 Log.w(LOGTAG, "We should not get here.");
1528 break;
1529 }
1530 //update the ui
1531 mUi.onContextMenuCreated(menu);
1532 }
1533
1534 /**
1535 * As the menu can be open when loading state changes
1536 * we must manually update the state of the stop/reload menu
1537 * item
1538 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001539 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001540 if (menu == null) {
1541 return;
1542 }
1543 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001544 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001545 menu.findItem(R.id.stop_menu_id):
1546 menu.findItem(R.id.reload_menu_id);
1547 if (src != null) {
1548 dest.setIcon(src.getIcon());
1549 dest.setTitle(src.getTitle());
1550 }
1551 }
1552
John Reck9c35b9c2012-05-30 10:08:50 -07001553 @Override
1554 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001555 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001556 // hold on to the menu reference here; it is used by the page callbacks
1557 // to update the menu based on loading state
1558 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001559 // Note: setVisible will decide whether an item is visible; while
1560 // setEnabled() will decide whether an item is enabled, which also means
1561 // whether the matching shortcut key will function.
1562 switch (mMenuState) {
1563 case EMPTY_MENU:
1564 if (mCurrentMenuState != mMenuState) {
1565 menu.setGroupVisible(R.id.MAIN_MENU, false);
1566 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1567 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1568 }
1569 break;
1570 default:
1571 if (mCurrentMenuState != mMenuState) {
1572 menu.setGroupVisible(R.id.MAIN_MENU, true);
1573 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1574 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1575 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001576 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001577 break;
1578 }
1579 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001580 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 }
1582
Michael Kolb4bf79712011-07-14 14:18:12 -07001583 @Override
1584 public void updateMenuState(Tab tab, Menu menu) {
1585 boolean canGoBack = false;
1586 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001587 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001588 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001589 if (tab != null) {
1590 canGoBack = tab.canGoBack();
1591 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001592 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001593 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001594 }
1595 final MenuItem back = menu.findItem(R.id.back_menu_id);
1596 back.setEnabled(canGoBack);
1597
1598 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001599
1600 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1601 forward.setEnabled(canGoForward);
1602
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001603 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1604 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001605 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001606 if (source != null && dest != null) {
1607 dest.setTitle(source.getTitle());
1608 dest.setIcon(source.getIcon());
1609 }
John Recke1a03a32011-09-14 17:04:16 -07001610 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001611
1612 // decide whether to show the share link option
1613 PackageManager pm = mActivity.getPackageManager();
1614 Intent send = new Intent(Intent.ACTION_SEND);
1615 send.setType("text/plain");
1616 ResolveInfo ri = pm.resolveActivity(send,
1617 PackageManager.MATCH_DEFAULT_ONLY);
1618 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1619
1620 boolean isNavDump = mSettings.enableNavDump();
1621 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1622 nav.setVisible(isNavDump);
1623 nav.setEnabled(isNavDump);
1624
1625 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001626 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1627 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001628 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1629 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001630
1631 // history and snapshots item are the members of COMBO menu group,
1632 // so if show history item, only make snapshots item invisible.
1633 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001634
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001635 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001636 }
1637
John Reck9c35b9c2012-05-30 10:08:50 -07001638 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001639 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001640 if (null == getCurrentTopWebView()) {
1641 return false;
1642 }
1643 if (mMenuIsDown) {
1644 // The shortcut action consumes the MENU. Even if it is still down,
1645 // it won't trigger the next shortcut action. In the case of the
1646 // shortcut action triggering a new activity, like Bookmarks, we
1647 // won't get onKeyUp for MENU. So it is important to reset it here.
1648 mMenuIsDown = false;
1649 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001650 if (mUi.onOptionsItemSelected(item)) {
1651 // ui callback handled it
1652 return true;
1653 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001654 switch (item.getItemId()) {
1655 // -- Main menu
1656 case R.id.new_tab_menu_id:
1657 openTabToHomePage();
1658 break;
1659
1660 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001661 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001662 break;
1663
Afzal Najamd4e33312012-04-26 01:54:01 -04001664 case R.id.close_other_tabs_id:
1665 closeOtherTabs();
1666 break;
1667
Michael Kolb8233fac2010-10-26 16:08:53 -07001668 case R.id.goto_menu_id:
1669 editUrl();
1670 break;
1671
1672 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001673 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1674 break;
1675
1676 case R.id.history_menu_id:
1677 bookmarksOrHistoryPicker(ComboViews.History);
1678 break;
1679
1680 case R.id.snapshots_menu_id:
1681 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001682 break;
1683
Michael Kolb8233fac2010-10-26 16:08:53 -07001684 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001685 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001686 break;
1687
1688 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001689 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001690 stopLoading();
1691 } else {
1692 getCurrentTopWebView().reload();
1693 }
1694 break;
1695
1696 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001697 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001698 break;
1699
1700 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001701 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001702 break;
1703
1704 case R.id.close_menu_id:
1705 // Close the subwindow if it exists.
1706 if (mTabControl.getCurrentSubWindow() != null) {
1707 dismissSubWindow(mTabControl.getCurrentTab());
1708 break;
1709 }
1710 closeCurrentTab();
1711 break;
1712
kaiyiza8b6dbb2013-07-29 18:11:22 +08001713 case R.id.exit_menu_id:
1714 String ret = SystemProperties.get("persist.debug.browsermonkeytest");
1715 if (ret != null && ret.equals("enable"))
1716 break;
luxiaolb40014b2013-07-19 10:01:43 +08001717 showExitDialog(mActivity);
1718 return true;
kaiyiza8b6dbb2013-07-29 18:11:22 +08001719
Michael Kolb8233fac2010-10-26 16:08:53 -07001720 case R.id.homepage_menu_id:
1721 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001722 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001723 break;
1724
1725 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001726 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001727 break;
1728
1729 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001730 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001731 break;
1732
John Reck2bc80422011-06-30 15:11:49 -07001733 case R.id.save_snapshot_menu_id:
1734 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001735 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001736 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001737 break;
1738
Michael Kolb8233fac2010-10-26 16:08:53 -07001739 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001740 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001741 break;
1742
John Recke1a03a32011-09-14 17:04:16 -07001743 case R.id.snapshot_go_live:
1744 goLive();
1745 return true;
1746
Michael Kolb8233fac2010-10-26 16:08:53 -07001747 case R.id.share_page_menu_id:
1748 Tab currentTab = mTabControl.getCurrentTab();
1749 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001750 return false;
1751 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001752 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001753 break;
1754
1755 case R.id.dump_nav_menu_id:
1756 getCurrentTopWebView().debugDump();
1757 break;
1758
Michael Kolb8233fac2010-10-26 16:08:53 -07001759 case R.id.zoom_in_menu_id:
1760 getCurrentTopWebView().zoomIn();
1761 break;
1762
1763 case R.id.zoom_out_menu_id:
1764 getCurrentTopWebView().zoomOut();
1765 break;
1766
1767 case R.id.view_downloads_menu_id:
1768 viewDownloads();
1769 break;
1770
John Reck42229bc2011-08-19 13:26:43 -07001771 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001772 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07001773 break;
1774
Michael Kolb8233fac2010-10-26 16:08:53 -07001775 case R.id.window_one_menu_id:
1776 case R.id.window_two_menu_id:
1777 case R.id.window_three_menu_id:
1778 case R.id.window_four_menu_id:
1779 case R.id.window_five_menu_id:
1780 case R.id.window_six_menu_id:
1781 case R.id.window_seven_menu_id:
1782 case R.id.window_eight_menu_id:
1783 {
1784 int menuid = item.getItemId();
1785 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1786 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1787 Tab desiredTab = mTabControl.getTab(id);
1788 if (desiredTab != null &&
1789 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001790 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001791 }
1792 break;
1793 }
1794 }
1795 }
1796 break;
1797
1798 default:
1799 return false;
1800 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001801 return true;
1802 }
1803
John Reck68234a92012-04-19 15:27:12 -07001804 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
1805 implements OnCancelListener {
1806
1807 private Tab mTab;
1808 private Dialog mProgressDialog;
1809 private ContentValues mValues;
1810
1811 private SaveSnapshotTask(Tab tab) {
1812 mTab = tab;
1813 }
1814
1815 @Override
1816 protected void onPreExecute() {
1817 CharSequence message = mActivity.getText(R.string.saving_snapshot);
1818 mProgressDialog = ProgressDialog.show(mActivity, null, message,
1819 true, true, this);
1820 mValues = mTab.createSnapshotValues();
1821 }
1822
1823 @Override
1824 protected Long doInBackground(Void... params) {
1825 if (!mTab.saveViewState(mValues)) {
1826 return null;
1827 }
1828 if (isCancelled()) {
1829 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
1830 File file = mActivity.getFileStreamPath(path);
1831 if (!file.delete()) {
1832 file.deleteOnExit();
1833 }
1834 return null;
1835 }
1836 final ContentResolver cr = mActivity.getContentResolver();
1837 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
1838 if (result == null) {
1839 return null;
1840 }
1841 long id = ContentUris.parseId(result);
1842 return id;
1843 }
1844
1845 @Override
1846 protected void onPostExecute(Long id) {
1847 if (isCancelled()) {
1848 return;
1849 }
1850 mProgressDialog.dismiss();
1851 if (id == null) {
1852 Toast.makeText(mActivity, R.string.snapshot_failed,
1853 Toast.LENGTH_SHORT).show();
1854 return;
1855 }
1856 Bundle b = new Bundle();
1857 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1858 mUi.showComboView(ComboViews.Snapshots, b);
1859 }
1860
1861 @Override
1862 public void onCancel(DialogInterface dialog) {
1863 cancel(true);
1864 }
1865 }
1866
Michael Kolb80f75082012-04-10 10:50:06 -07001867 @Override
1868 public void toggleUserAgent() {
1869 WebView web = getCurrentWebView();
1870 mSettings.toggleDesktopUseragent(web);
1871 web.loadUrl(web.getOriginalUrl());
1872 }
1873
1874 @Override
1875 public void findOnPage() {
1876 getCurrentTopWebView().showFindDialog(null, true);
1877 }
1878
1879 @Override
1880 public void openPreferences() {
1881 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1882 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1883 getCurrentTopWebView().getUrl());
1884 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1885 }
1886
1887 @Override
1888 public void bookmarkCurrentPage() {
1889 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
1890 if (bookmarkIntent != null) {
1891 mActivity.startActivity(bookmarkIntent);
1892 }
1893 }
1894
John Recke1a03a32011-09-14 17:04:16 -07001895 private void goLive() {
1896 Tab t = getCurrentTab();
1897 t.loadUrl(t.getUrl(), null);
1898 }
1899
luxiaolb40014b2013-07-19 10:01:43 +08001900 private void showExitDialog(final Activity activity) {
1901 new AlertDialog.Builder(activity)
1902 .setTitle(R.string.exit_browser_title)
1903 .setIcon(android.R.drawable.ic_dialog_alert)
1904 .setMessage(R.string.exit_browser_msg)
1905 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
1906 public void onClick(DialogInterface dialog, int which) {
1907 activity.moveTaskToBack(true);
1908 dialog.dismiss();
1909 }
1910 })
1911 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
1912 public void onClick(DialogInterface dialog, int which) {
1913 activity.finish();
1914 mHandler.postDelayed(new Runnable() {
1915 @Override
1916 public void run() {
1917 // TODO Auto-generated method stub
1918 mCrashRecoveryHandler.clearState(true);
1919 int pid = android.os.Process.myPid();
1920 android.os.Process.killProcess(pid);
1921 }
1922 }, 300);
1923 dialog.dismiss();
1924 }
1925 })
1926 .show();
1927 }
1928
Michael Kolb315d5022011-10-13 12:47:11 -07001929 @Override
1930 public void showPageInfo() {
1931 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
1932 }
1933
John Reck9c35b9c2012-05-30 10:08:50 -07001934 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001935 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001936 // Let the History and Bookmark fragments handle menus they created.
1937 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1938 return false;
1939 }
1940
Michael Kolb8233fac2010-10-26 16:08:53 -07001941 int id = item.getItemId();
1942 boolean result = true;
1943 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001944 // -- Browser context menu
1945 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001946 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001947 case R.id.copy_link_context_menu_id:
1948 final WebView webView = getCurrentTopWebView();
1949 if (null == webView) {
1950 result = false;
1951 break;
1952 }
1953 final HashMap<String, WebView> hrefMap =
1954 new HashMap<String, WebView>();
1955 hrefMap.put("webview", webView);
1956 final Message msg = mHandler.obtainMessage(
1957 FOCUS_NODE_HREF, id, 0, hrefMap);
1958 webView.requestFocusNodeHref(msg);
1959 break;
1960
1961 default:
1962 // For other context menus
1963 result = onOptionsItemSelected(item);
1964 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001965 return result;
1966 }
1967
1968 /**
1969 * support programmatically opening the context menu
1970 */
1971 public void openContextMenu(View view) {
1972 mActivity.openContextMenu(view);
1973 }
1974
1975 /**
1976 * programmatically open the options menu
1977 */
1978 public void openOptionsMenu() {
1979 mActivity.openOptionsMenu();
1980 }
1981
John Reck9c35b9c2012-05-30 10:08:50 -07001982 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001983 public boolean onMenuOpened(int featureId, Menu menu) {
1984 if (mOptionsMenuOpen) {
1985 if (mConfigChanged) {
1986 // We do not need to make any changes to the state of the
1987 // title bar, since the only thing that happened was a
1988 // change in orientation
1989 mConfigChanged = false;
1990 } else {
1991 if (!mExtendedMenuOpen) {
1992 mExtendedMenuOpen = true;
1993 mUi.onExtendedMenuOpened();
1994 } else {
1995 // Switching the menu back to icon view, so show the
1996 // title bar once again.
1997 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001998 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001999 }
2000 }
2001 } else {
2002 // The options menu is closed, so open it, and show the title
2003 mOptionsMenuOpen = true;
2004 mConfigChanged = false;
2005 mExtendedMenuOpen = false;
2006 mUi.onOptionsMenuOpened();
2007 }
2008 return true;
2009 }
2010
John Reck9c35b9c2012-05-30 10:08:50 -07002011 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002012 public void onOptionsMenuClosed(Menu menu) {
2013 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002014 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002015 }
2016
John Reck9c35b9c2012-05-30 10:08:50 -07002017 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002018 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002019 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002020 }
2021
2022 // Helper method for getting the top window.
2023 @Override
2024 public WebView getCurrentTopWebView() {
2025 return mTabControl.getCurrentTopWebView();
2026 }
2027
2028 @Override
2029 public WebView getCurrentWebView() {
2030 return mTabControl.getCurrentWebView();
2031 }
2032
2033 /*
2034 * This method is called as a result of the user selecting the options
2035 * menu to see the download window. It shows the download window on top of
2036 * the current window.
2037 */
2038 void viewDownloads() {
2039 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
2040 mActivity.startActivity(intent);
2041 }
2042
John Reck30b065e2011-07-19 10:58:05 -07002043 int getActionModeHeight() {
2044 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
2045 new int[] { android.R.attr.actionBarSize });
2046 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
2047 actionBarSizeTypedArray.recycle();
2048 return size;
2049 }
2050
Michael Kolb8233fac2010-10-26 16:08:53 -07002051 // action mode
2052
John Reck9c35b9c2012-05-30 10:08:50 -07002053 @Override
2054 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002055 mUi.onActionModeStarted(mode);
2056 mActionMode = mode;
2057 }
2058
2059 /*
2060 * True if a custom ActionMode (i.e. find or select) is in use.
2061 */
2062 @Override
2063 public boolean isInCustomActionMode() {
2064 return mActionMode != null;
2065 }
2066
2067 /*
2068 * End the current ActionMode.
2069 */
2070 @Override
2071 public void endActionMode() {
2072 if (mActionMode != null) {
2073 mActionMode.finish();
2074 }
2075 }
2076
2077 /*
2078 * Called by find and select when they are finished. Replace title bars
2079 * as necessary.
2080 */
John Reck9c35b9c2012-05-30 10:08:50 -07002081 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002082 public void onActionModeFinished(ActionMode mode) {
2083 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002084 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002085 mActionMode = null;
2086 }
2087
2088 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002089 final Tab tab = getCurrentTab();
2090 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002091 }
2092
2093 // bookmark handling
2094
2095 /**
2096 * add the current page as a bookmark to the given folder id
2097 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002098 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002099 * bookmarked, and if it is, edit that bookmark. If false, and
2100 * the site is already bookmarked, do not attempt to edit the
2101 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002102 */
2103 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002104 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002105 WebView w = getCurrentTopWebView();
2106 if (w == null) {
2107 return null;
2108 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002109 Intent i = new Intent(mActivity,
2110 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002111 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2112 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2113 String touchIconUrl = w.getTouchIconUrl();
2114 if (touchIconUrl != null) {
2115 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2116 WebSettings settings = w.getSettings();
2117 if (settings != null) {
2118 i.putExtra(AddBookmarkPage.USER_AGENT,
2119 settings.getUserAgentString());
2120 }
2121 }
2122 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2123 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2124 getDesiredThumbnailHeight(mActivity)));
2125 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002126 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002127 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2128 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002129 // Put the dialog at the upper right of the screen, covering the
2130 // star on the title bar.
2131 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002132 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002133 }
2134
2135 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002136 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002137 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002138 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002139 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002140 }
2141
2142 // thumbnails
2143
2144 /**
2145 * Return the desired width for thumbnail screenshots, which are stored in
2146 * the database, and used on the bookmarks screen.
2147 * @param context Context for finding out the density of the screen.
2148 * @return desired width for thumbnail screenshot.
2149 */
2150 static int getDesiredThumbnailWidth(Context context) {
2151 return context.getResources().getDimensionPixelOffset(
2152 R.dimen.bookmarkThumbnailWidth);
2153 }
2154
2155 /**
2156 * Return the desired height for thumbnail screenshots, which are stored in
2157 * the database, and used on the bookmarks screen.
2158 * @param context Context for finding out the density of the screen.
2159 * @return desired height for thumbnail screenshot.
2160 */
2161 static int getDesiredThumbnailHeight(Context context) {
2162 return context.getResources().getDimensionPixelOffset(
2163 R.dimen.bookmarkThumbnailHeight);
2164 }
2165
John Reck8cc92352011-07-06 17:41:52 -07002166 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002167 if (view == null || view.getContentHeight() == 0
2168 || view.getContentWidth() == 0) {
2169 return null;
2170 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002171 // We render to a bitmap 2x the desired size so that we can then
2172 // re-scale it with filtering since canvas.scale doesn't filter
2173 // This helps reduce aliasing at the cost of being slightly blurry
2174 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002175 int scaledWidth = width * filter_scale;
2176 int scaledHeight = height * filter_scale;
2177 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2178 || sThumbnailBitmap.getHeight() != scaledHeight) {
2179 if (sThumbnailBitmap != null) {
2180 sThumbnailBitmap.recycle();
2181 sThumbnailBitmap = null;
2182 }
2183 sThumbnailBitmap =
2184 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002185 }
John Reckd7dd9b22011-08-30 09:18:29 -07002186 Canvas canvas = new Canvas(sThumbnailBitmap);
2187 int contentWidth = view.getContentWidth();
2188 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2189 if (view instanceof BrowserWebView) {
2190 int dy = -((BrowserWebView)view).getTitleHeight();
2191 canvas.translate(0, dy * overviewScale);
2192 }
2193
2194 canvas.scale(overviewScale, overviewScale);
2195
2196 if (view instanceof BrowserWebView) {
2197 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002198 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002199 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002200 }
John Reckd7dd9b22011-08-30 09:18:29 -07002201 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2202 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002203 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002204 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002205 }
2206
John Reck34ef2672011-02-10 11:30:55 -08002207 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002208 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002209 // FIXME: Would like to make sure there is actually something to
2210 // draw, but the API for that (WebViewCore.pictureReady()) is not
2211 // currently accessible here.
2212
John Reck34ef2672011-02-10 11:30:55 -08002213 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002214 if (view == null) {
2215 // Tab was destroyed
2216 return;
2217 }
John Reck34ef2672011-02-10 11:30:55 -08002218 final String url = tab.getUrl();
2219 final String originalUrl = view.getOriginalUrl();
John Reck34ef2672011-02-10 11:30:55 -08002220 if (TextUtils.isEmpty(url)) {
2221 return;
2222 }
2223
2224 // Only update thumbnails for web urls (http(s)://), not for
2225 // about:, javascript:, data:, etc...
2226 // Unless it is a bookmarked site, then always update
2227 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2228 return;
2229 }
2230
Michael Kolb8233fac2010-10-26 16:08:53 -07002231 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2232 getDesiredThumbnailHeight(mActivity));
2233 if (bm == null) {
2234 return;
2235 }
2236
2237 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002238 new AsyncTask<Void, Void, Void>() {
2239 @Override
2240 protected Void doInBackground(Void... unused) {
2241 Cursor cursor = null;
2242 try {
2243 // TODO: Clean this up
2244 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2245 if (cursor != null && cursor.moveToFirst()) {
2246 final ByteArrayOutputStream os =
2247 new ByteArrayOutputStream();
2248 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002249
John Reck34ef2672011-02-10 11:30:55 -08002250 ContentValues values = new ContentValues();
2251 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002252
John Reck34ef2672011-02-10 11:30:55 -08002253 do {
John Reck617fd832011-02-16 14:35:59 -08002254 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002255 cr.update(Images.CONTENT_URI, values, null, null);
2256 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002257 }
John Reck34ef2672011-02-10 11:30:55 -08002258 } catch (IllegalStateException e) {
2259 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002260 } catch (SQLiteException s) {
2261 // Added for possible error when user tries to remove the same bookmark
2262 // that is being updated with a screen shot
2263 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002264 } finally {
2265 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002266 }
John Reck34ef2672011-02-10 11:30:55 -08002267 return null;
2268 }
2269 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002270 }
2271
2272 private class Copy implements OnMenuItemClickListener {
2273 private CharSequence mText;
2274
John Reck9c35b9c2012-05-30 10:08:50 -07002275 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002276 public boolean onMenuItemClick(MenuItem item) {
2277 copy(mText);
2278 return true;
2279 }
2280
2281 public Copy(CharSequence toCopy) {
2282 mText = toCopy;
2283 }
2284 }
2285
Leon Scroggins63c02662010-11-18 15:16:27 -05002286 private static class Download implements OnMenuItemClickListener {
2287 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002288 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002289 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002290 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002291 private static final String FALLBACK_EXTENSION = "dat";
2292 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002293
John Reck9c35b9c2012-05-30 10:08:50 -07002294 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002295 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002296 if (DataUri.isDataUri(mText)) {
2297 saveDataUri();
2298 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002299 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002300 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002301 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002302 return true;
2303 }
2304
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002305 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2306 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002307 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002308 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002309 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002310 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002311 }
George Mount387d45d2011-10-07 15:57:53 -07002312
2313 /**
2314 * Treats mText as a data URI and writes its contents to a file
2315 * based on the current time.
2316 */
2317 private void saveDataUri() {
2318 FileOutputStream outputStream = null;
2319 try {
2320 DataUri uri = new DataUri(mText);
2321 File target = getTarget(uri);
2322 outputStream = new FileOutputStream(target);
2323 outputStream.write(uri.getData());
2324 final DownloadManager manager =
2325 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2326 manager.addCompletedDownload(target.getName(),
2327 mActivity.getTitle().toString(), false,
2328 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002329 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002330 } catch (IOException e) {
2331 Log.e(LOGTAG, "Could not save data URL");
2332 } finally {
2333 if (outputStream != null) {
2334 try {
2335 outputStream.close();
2336 } catch (IOException e) {
2337 // ignore close errors
2338 }
2339 }
2340 }
2341 }
2342
2343 /**
2344 * Creates a File based on the current time stamp and uses
2345 * the mime type of the DataUri to get the extension.
2346 */
2347 private File getTarget(DataUri uri) throws IOException {
2348 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002349 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002350 String nameBase = format.format(new Date());
2351 String mimeType = uri.getMimeType();
2352 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2353 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2354 if (extension == null) {
2355 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2356 extension = FALLBACK_EXTENSION;
2357 }
2358 extension = "." + extension; // createTempFile needs the '.'
2359 File targetFile = File.createTempFile(nameBase, extension, dir);
2360 return targetFile;
2361 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002362 }
2363
Cary Clark8974d282010-11-22 10:46:05 -05002364 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002365 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002366
John Reck9c35b9c2012-05-30 10:08:50 -07002367 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002368 public boolean onMenuItemClick(MenuItem item) {
2369 if (mWebView != null) {
2370 return mWebView.selectText();
2371 }
2372 return false;
2373 }
2374
2375 public SelectText(WebView webView) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002376 if (BrowserWebView.isClassic()) {
2377 mWebView = WebViewClassic.fromWebView(webView);
2378 }
Cary Clark8974d282010-11-22 10:46:05 -05002379 }
2380
2381 }
2382
Michael Kolb8233fac2010-10-26 16:08:53 -07002383 /********************** TODO: UI stuff *****************************/
2384
2385 // these methods have been copied, they still need to be cleaned up
2386
2387 /****************** tabs ***************************************************/
2388
2389 // basic tab interactions:
2390
2391 // it is assumed that tabcontrol already knows about the tab
2392 protected void addTab(Tab tab) {
2393 mUi.addTab(tab);
2394 }
2395
2396 protected void removeTab(Tab tab) {
2397 mUi.removeTab(tab);
2398 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002399 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002400 }
2401
Michael Kolbf2055602011-04-09 17:20:03 -07002402 @Override
2403 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002404 // monkey protection against delayed start
2405 if (tab != null) {
2406 mTabControl.setCurrentTab(tab);
2407 // the tab is guaranteed to have a webview after setCurrentTab
2408 mUi.setActiveTab(tab);
2409 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002410 }
2411
John Reck8bcafc12011-08-29 16:43:02 -07002412 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002413 Tab current = mTabControl.getCurrentTab();
2414 if (current != null
2415 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002416 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002417 }
2418 }
2419
John Reck26b18322011-06-21 13:08:58 -07002420 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002421 // Dismiss the subwindow if applicable.
2422 dismissSubWindow(appTab);
2423 // Since we might kill the WebView, remove it from the
2424 // content view first.
2425 mUi.detachTab(appTab);
2426 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002427 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002428 // TODO: analyze why the remove and add are necessary
2429 mUi.attachTab(appTab);
2430 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002431 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002432 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002433 } else {
2434 // If the tab was the current tab, we have to attach
2435 // it to the view system again.
2436 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002437 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002438 }
2439 }
2440
2441 // Remove the sub window if it exists. Also called by TabControl when the
2442 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002443 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002444 public void dismissSubWindow(Tab tab) {
2445 removeSubWindow(tab);
2446 // dismiss the subwindow. This will destroy the WebView.
2447 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002448 WebView wv = getCurrentTopWebView();
2449 if (wv != null) {
2450 wv.requestFocus();
2451 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002452 }
2453
2454 @Override
2455 public void removeSubWindow(Tab t) {
2456 if (t.getSubWebView() != null) {
2457 mUi.removeSubWindow(t.getSubViewContainer());
2458 }
2459 }
2460
2461 @Override
2462 public void attachSubWindow(Tab tab) {
2463 if (tab.getSubWebView() != null) {
2464 mUi.attachSubWindow(tab.getSubViewContainer());
2465 getCurrentTopWebView().requestFocus();
2466 }
2467 }
2468
Mathew Inwood29721c22011-06-29 17:55:24 +01002469 private Tab showPreloadedTab(final UrlData urlData) {
2470 if (!urlData.isPreloaded()) {
2471 return null;
2472 }
2473 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2474 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2475 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002476 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002477 // Could not submit query. Fallback to regular tab creation
2478 tabControl.destroy();
2479 return null;
2480 }
2481 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002482 // check tab count and make room for new tab
2483 if (!mTabControl.canCreateNewTab()) {
2484 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2485 if (leastUsed != null) {
2486 closeTab(leastUsed);
2487 }
2488 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002489 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002490 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002491 mTabControl.addPreloadedTab(t);
2492 addTab(t);
2493 setActiveTab(t);
2494 return t;
2495 }
2496
Michael Kolb7bcafde2011-05-09 13:55:59 -07002497 // open a non inconito tab with the given url data
2498 // and set as active tab
2499 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002500 Tab tab = showPreloadedTab(urlData);
2501 if (tab == null) {
2502 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002503 if ((tab != null) && !urlData.isEmpty()) {
2504 loadUrlDataIn(tab, urlData);
2505 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002506 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002507 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002508 }
2509
Michael Kolb843510f2010-12-09 10:51:49 -08002510 @Override
2511 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002512 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002513 }
2514
Michael Kolb8233fac2010-10-26 16:08:53 -07002515 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002516 public Tab openIncognitoTab() {
2517 return openTab(INCOGNITO_URI, true, true, false);
2518 }
2519
2520 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002521 public Tab openTab(String url, boolean incognito, boolean setActive,
2522 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002523 return openTab(url, incognito, setActive, useCurrent, null);
2524 }
2525
2526 @Override
2527 public Tab openTab(String url, Tab parent, boolean setActive,
2528 boolean useCurrent) {
2529 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2530 setActive, useCurrent, parent);
2531 }
2532
2533 public Tab openTab(String url, boolean incognito, boolean setActive,
2534 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002535 Tab tab = createNewTab(incognito, setActive, useCurrent);
2536 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002537 if (parent != null && parent != tab) {
2538 parent.addChildTab(tab);
2539 }
Michael Kolb519d2282011-05-09 17:03:19 -07002540 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002541 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002542 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002543 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002544 return tab;
2545 }
2546
2547 // this method will attempt to create a new tab
2548 // incognito: private browsing tab
2549 // setActive: ste tab as current tab
2550 // useCurrent: if no new tab can be created, return current tab
2551 private Tab createNewTab(boolean incognito, boolean setActive,
2552 boolean useCurrent) {
2553 Tab tab = null;
2554 if (mTabControl.canCreateNewTab()) {
2555 tab = mTabControl.createNewTab(incognito);
2556 addTab(tab);
2557 if (setActive) {
2558 setActiveTab(tab);
2559 }
2560 } else {
2561 if (useCurrent) {
2562 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002563 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002564 } else {
2565 mUi.showMaxTabsWarning();
2566 }
2567 }
2568 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002569 }
2570
John Reck2bc80422011-06-30 15:11:49 -07002571 @Override
2572 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2573 SnapshotTab tab = null;
2574 if (mTabControl.canCreateNewTab()) {
2575 tab = mTabControl.createSnapshotTab(snapshotId);
2576 addTab(tab);
2577 if (setActive) {
2578 setActiveTab(tab);
2579 }
2580 } else {
2581 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002582 }
2583 return tab;
2584 }
2585
Michael Kolb8233fac2010-10-26 16:08:53 -07002586 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002587 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002588 * @return boolean True if we successfully switched to a different tab. If
2589 * the indexth tab is null, or if that tab is the same as
2590 * the current one, return false.
2591 */
2592 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002593 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002594 Tab currentTab = mTabControl.getCurrentTab();
2595 if (tab == null || tab == currentTab) {
2596 return false;
2597 }
2598 setActiveTab(tab);
2599 return true;
2600 }
2601
2602 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002603 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002604 closeCurrentTab(false);
2605 }
2606
2607 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002609 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002610 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002611 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002612 return;
2613 }
Michael Kolbc831b632011-05-11 09:30:34 -07002614 final Tab current = mTabControl.getCurrentTab();
2615 final int pos = mTabControl.getCurrentPosition();
2616 Tab newTab = current.getParent();
2617 if (newTab == null) {
2618 newTab = mTabControl.getTab(pos + 1);
2619 if (newTab == null) {
2620 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002621 }
2622 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002623 if (andQuit) {
2624 mTabControl.setCurrentTab(newTab);
2625 closeTab(current);
2626 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002627 // Close window
2628 closeTab(current);
2629 }
2630 }
2631
2632 /**
2633 * Close the tab, remove its associated title bar, and adjust mTabControl's
2634 * current tab to a valid value.
2635 */
2636 @Override
2637 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002638 if (tab == mTabControl.getCurrentTab()) {
2639 closeCurrentTab();
2640 } else {
2641 removeTab(tab);
2642 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002643 }
2644
Afzal Najamd4e33312012-04-26 01:54:01 -04002645 /**
2646 * Close all tabs except the current one
2647 */
2648 @Override
2649 public void closeOtherTabs() {
2650 int inactiveTabs = mTabControl.getTabCount() - 1;
2651 for (int i = inactiveTabs; i >= 0; i--) {
2652 Tab tab = mTabControl.getTab(i);
2653 if (tab != mTabControl.getCurrentTab()) {
2654 removeTab(tab);
2655 }
2656 }
2657 }
2658
Michael Kolb8233fac2010-10-26 16:08:53 -07002659 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002660 protected void loadUrlFromContext(String url) {
2661 Tab tab = getCurrentTab();
2662 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002663 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002664 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002665 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002666 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002667 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002668 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002669 }
2670 }
2671 }
2672
2673 /**
2674 * Load the URL into the given WebView and update the title bar
2675 * to reflect the new load. Call this instead of WebView.loadUrl
2676 * directly.
2677 * @param view The WebView used to load url.
2678 * @param url The URL to load.
2679 */
John Reck71e51422011-07-01 16:49:28 -07002680 @Override
2681 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002682 loadUrl(tab, url, null);
2683 }
2684
2685 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2686 if (tab != null) {
2687 dismissSubWindow(tab);
2688 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002689 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002690 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002691 }
2692
2693 /**
2694 * Load UrlData into a Tab and update the title bar to reflect the new
2695 * load. Call this instead of UrlData.loadIn directly.
2696 * @param t The Tab used to load.
2697 * @param data The UrlData being loaded.
2698 */
2699 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002700 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002701 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002702 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002703 } else {
John Reck38b39652012-06-05 09:22:59 -07002704 if (t != null && data.mDisableUrlOverride) {
2705 t.disableUrlOverridingForLoad();
2706 }
John Reck26b18322011-06-21 13:08:58 -07002707 loadUrl(t, data.mUrl, data.mHeaders);
2708 }
2709 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002710 }
2711
John Reck30c714c2010-12-16 17:30:34 -08002712 @Override
2713 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002714 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002715 if (tab.canGoBack()) {
2716 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002717 } else {
John Reckef654f12011-07-12 16:42:08 -07002718 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002719 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002720 }
2721
2722 void goBackOnePageOrQuit() {
2723 Tab current = mTabControl.getCurrentTab();
2724 if (current == null) {
2725 /*
2726 * Instead of finishing the activity, simply push this to the back
2727 * of the stack and let ActivityManager to choose the foreground
2728 * activity. As BrowserActivity is singleTask, it will be always the
2729 * root of the task. So we can use either true or false for
2730 * moveTaskToBack().
2731 */
luxiaolb40014b2013-07-19 10:01:43 +08002732 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07002733 return;
2734 }
John Reckef654f12011-07-12 16:42:08 -07002735 if (current.canGoBack()) {
2736 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002737 } else {
2738 // Check to see if we are closing a window that was created by
2739 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002740 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002741 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002742 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002743 // Now we close the other tab
2744 closeTab(current);
2745 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002746 /*
2747 * Instead of finishing the activity, simply push this to the back
2748 * of the stack and let ActivityManager to choose the foreground
2749 * activity. As BrowserActivity is singleTask, it will be always the
2750 * root of the task. So we can use either true or false for
2751 * moveTaskToBack().
2752 */
luxiaolb40014b2013-07-19 10:01:43 +08002753 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07002754 }
2755 }
2756 }
2757
2758 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002759 * helper method for key handler
2760 * returns the current tab if it can't advance
2761 */
Michael Kolbc831b632011-05-11 09:30:34 -07002762 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002763 int pos = mTabControl.getCurrentPosition() + 1;
2764 if (pos >= mTabControl.getTabCount()) {
2765 pos = 0;
2766 }
2767 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002768 }
2769
2770 /**
2771 * helper method for key handler
2772 * returns the current tab if it can't advance
2773 */
Michael Kolbc831b632011-05-11 09:30:34 -07002774 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002775 int pos = mTabControl.getCurrentPosition() - 1;
2776 if ( pos < 0) {
2777 pos = mTabControl.getTabCount() - 1;
2778 }
2779 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002780 }
2781
John Reckbcef87f2012-02-03 14:58:34 -08002782 boolean isMenuOrCtrlKey(int keyCode) {
2783 return (KeyEvent.KEYCODE_MENU == keyCode)
2784 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2785 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2786 }
2787
Michael Kolb0035fad2011-03-14 13:25:28 -07002788 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002789 * handle key events in browser
2790 *
2791 * @param keyCode
2792 * @param event
2793 * @return true if handled, false to pass to super
2794 */
John Reck9c35b9c2012-05-30 10:08:50 -07002795 @Override
2796 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002797 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002798 // Even if MENU is already held down, we need to call to super to open
2799 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002800 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002801 mMenuIsDown = true;
2802 return false;
2803 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002804
Cary Clark8ff8c662010-12-29 15:03:05 -05002805 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002806 Tab tab = getCurrentTab();
2807 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002808
Cary Clark160bbb92011-01-10 11:17:07 -05002809 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2810 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002811
Michael Kolb8233fac2010-10-26 16:08:53 -07002812 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002813 case KeyEvent.KEYCODE_TAB:
2814 if (event.isCtrlPressed()) {
2815 if (event.isShiftPressed()) {
2816 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002817 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002818 } else {
2819 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002820 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002821 }
2822 return true;
2823 }
2824 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002825 case KeyEvent.KEYCODE_SPACE:
2826 // WebView/WebTextView handle the keys in the KeyDown. As
2827 // the Activity's shortcut keys are only handled when WebView
2828 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002829 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002830 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002831 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002832 pageDown();
2833 }
2834 return true;
2835 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002836 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002837 event.startTracking();
2838 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002839 case KeyEvent.KEYCODE_FORWARD:
2840 if (!noModifiers) break;
2841 tab.goForward();
2842 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002843 case KeyEvent.KEYCODE_DPAD_LEFT:
2844 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002845 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002846 return true;
2847 }
2848 break;
2849 case KeyEvent.KEYCODE_DPAD_RIGHT:
2850 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002851 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002852 return true;
2853 }
2854 break;
2855 case KeyEvent.KEYCODE_A:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002856 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002857 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002858 return true;
2859 }
2860 break;
Michael Kolba4183062011-01-16 10:43:21 -08002861// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002862 case KeyEvent.KEYCODE_C:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002863 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002864 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002865 return true;
2866 }
2867 break;
Michael Kolba4183062011-01-16 10:43:21 -08002868// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002869// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002870// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002871// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002872// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002873// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002874// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002875// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002876// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002877// case KeyEvent.KEYCODE_M: // unused
2878// case KeyEvent.KEYCODE_N: // in Chrome: new window
2879// case KeyEvent.KEYCODE_O: // in Chrome: open file
2880// case KeyEvent.KEYCODE_P: // in Chrome: print page
2881// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002882// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002883// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2884 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002885 // we can't use the ctrl/shift flags, they check for
2886 // exclusive use of a modifier
2887 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002888 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002889 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002890 } else {
2891 openTabToHomePage();
2892 }
2893 return true;
2894 }
2895 break;
2896// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2897// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002898// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002899// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2900// case KeyEvent.KEYCODE_Y: // unused
2901// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002902 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002903 // it is a regular key and webview is not null
2904 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002905 }
2906
John Reck9c35b9c2012-05-30 10:08:50 -07002907 @Override
2908 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002909 switch(keyCode) {
2910 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002911 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002912 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002913 return true;
2914 }
2915 break;
2916 }
2917 return false;
2918 }
2919
John Reck9c35b9c2012-05-30 10:08:50 -07002920 @Override
2921 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002922 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002923 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002924 if (KeyEvent.KEYCODE_MENU == keyCode
2925 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002926 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002927 }
2928 }
Cary Clark160bbb92011-01-10 11:17:07 -05002929 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002930 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002931 case KeyEvent.KEYCODE_BACK:
2932 if (event.isTracking() && !event.isCanceled()) {
2933 onBackKey();
2934 return true;
2935 }
2936 break;
2937 }
2938 return false;
2939 }
2940
2941 public boolean isMenuDown() {
2942 return mMenuIsDown;
2943 }
2944
John Reck9c35b9c2012-05-30 10:08:50 -07002945 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002946 public void setupAutoFill(Message message) {
2947 // Open the settings activity at the AutoFill profile fragment so that
2948 // the user can create a new profile. When they return, we will dispatch
2949 // the message so that we can autofill the form using their new profile.
2950 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2951 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2952 AutoFillSettingsFragment.class.getName());
2953 mAutoFillSetupMessage = message;
2954 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2955 }
John Reckb3417f02011-01-14 11:01:05 -08002956
John Reck9c35b9c2012-05-30 10:08:50 -07002957 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002958 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002959 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002960 return true;
2961 }
2962
John Reck1cf4b792011-07-26 10:22:22 -07002963 @Override
2964 public boolean shouldCaptureThumbnails() {
2965 return mUi.shouldCaptureThumbnails();
2966 }
2967
Michael Kolbc3af0672011-08-09 10:24:41 -07002968 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07002969 public boolean supportsVoice() {
2970 PackageManager pm = mActivity.getPackageManager();
2971 List activities = pm.queryIntentActivities(new Intent(
2972 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
2973 return activities.size() != 0;
2974 }
2975
2976 @Override
2977 public void startVoiceRecognizer() {
2978 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
2979 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
2980 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
2981 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
2982 mActivity.startActivityForResult(voice, VOICE_RESULT);
2983 }
2984
2985 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002986 public void setBlockEvents(boolean block) {
2987 mBlockEvents = block;
2988 }
2989
John Reck9c35b9c2012-05-30 10:08:50 -07002990 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002991 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002992 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002993 }
2994
John Reck9c35b9c2012-05-30 10:08:50 -07002995 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002996 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002997 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002998 }
2999
John Reck9c35b9c2012-05-30 10:08:50 -07003000 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003001 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003002 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003003 }
3004
John Reck9c35b9c2012-05-30 10:08:50 -07003005 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003006 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003007 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003008 }
3009
John Reck9c35b9c2012-05-30 10:08:50 -07003010 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07003011 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07003012 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07003013 }
3014
Michael Kolb8233fac2010-10-26 16:08:53 -07003015}