blob: 3094595b092b024b07d66a1645cb994754e613e3 [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;
Michael Kolb8233fac2010-10-26 16:08:53 -070042import android.net.Uri;
43import android.net.http.SslError;
44import android.os.AsyncTask;
45import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070046import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070047import android.os.Handler;
48import android.os.Message;
49import android.os.PowerManager;
50import android.os.PowerManager.WakeLock;
kaiyiza8b6dbb2013-07-29 18:11:22 +080051import android.os.SystemProperties;
Ben Murdoch8029a772010-11-16 11:58:21 +000052import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070053import android.provider.Browser;
54import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070055import android.provider.BrowserContract.Images;
56import android.provider.ContactsContract;
57import android.provider.ContactsContract.Intents.Insert;
Michael Kolb0b129122012-06-04 16:31:58 -070058import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070059import android.text.TextUtils;
60import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080061import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070062import android.view.ActionMode;
63import android.view.ContextMenu;
64import android.view.ContextMenu.ContextMenuInfo;
65import android.view.Gravity;
66import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070067import android.view.Menu;
68import android.view.MenuInflater;
69import android.view.MenuItem;
70import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070071import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070072import android.view.View;
73import android.webkit.CookieManager;
74import android.webkit.CookieSyncManager;
75import android.webkit.HttpAuthHandler;
George Mount387d45d2011-10-07 15:57:53 -070076import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070077import android.webkit.SslErrorHandler;
78import android.webkit.ValueCallback;
79import android.webkit.WebChromeClient;
80import android.webkit.WebIconDatabase;
81import android.webkit.WebSettings;
82import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000083import android.webkit.WebViewClassic;
Leon Scrogginsac993842011-02-02 12:54:07 -050084import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070085
Michael Kolb4bd767d2011-05-27 11:33:55 -070086import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070087import com.android.browser.UI.ComboViews;
John Reck1cf4b792011-07-26 10:22:22 -070088import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070089import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070090
Michael Kolb8233fac2010-10-26 16:08:53 -070091import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -070092import java.io.File;
93import java.io.FileOutputStream;
94import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -070095import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -070096import java.text.DateFormat;
97import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -070098import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -070099import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -0700100import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -0700101import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800102import java.util.List;
Johan Redestigaa676182012-10-03 13:33:01 +0200103import java.util.Locale;
John Reck26b18322011-06-21 13:08:58 -0700104import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700105
106/**
107 * Controller for browser
108 */
109public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700110 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700111
112 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800113 private static final String SEND_APP_ID_EXTRA =
114 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700115 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800116
Michael Kolb8233fac2010-10-26 16:08:53 -0700117
118 // public message ids
119 public final static int LOAD_URL = 1001;
120 public final static int STOP_LOAD = 1002;
121
122 // Message Ids
123 private static final int FOCUS_NODE_HREF = 102;
124 private static final int RELEASE_WAKELOCK = 107;
125
126 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
127
128 private static final int OPEN_BOOKMARKS = 201;
kaiyiz591110b2013-08-06 17:11:06 +0800129 private static final int OPEN_MENU = 202;
Michael Kolb8233fac2010-10-26 16:08:53 -0700130
131 private static final int EMPTY_MENU = -1;
132
Michael Kolb8233fac2010-10-26 16:08:53 -0700133 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700134 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700135 final static int PREFERENCES_PAGE = 3;
136 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000137 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700138 final static int VOICE_RESULT = 6;
Ben Murdoch8029a772010-11-16 11:58:21 +0000139
Michael Kolb8233fac2010-10-26 16:08:53 -0700140 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
141
142 // As the ids are dynamically created, we can't guarantee that they will
143 // be in sequence, so this static array maps ids to a window number.
144 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
145 { R.id.window_one_menu_id, R.id.window_two_menu_id,
146 R.id.window_three_menu_id, R.id.window_four_menu_id,
147 R.id.window_five_menu_id, R.id.window_six_menu_id,
148 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
149
150 // "source" parameter for Google search through search key
151 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
152 // "source" parameter for Google search through simplily type
153 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
154
George Mount387d45d2011-10-07 15:57:53 -0700155 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700156 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
157
John Reckd7dd9b22011-08-30 09:18:29 -0700158 // A bitmap that is re-used in createScreenshot as scratch space
159 private static Bitmap sThumbnailBitmap;
160
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 private Activity mActivity;
162 private UI mUi;
163 private TabControl mTabControl;
164 private BrowserSettings mSettings;
165 private WebViewFactory mFactory;
166
167 private WakeLock mWakeLock;
168
169 private UrlHandler mUrlHandler;
170 private UploadHandler mUploadHandler;
171 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700172 private PageDialogsHandler mPageDialogsHandler;
173 private NetworkStateHandler mNetworkHandler;
174
Ben Murdoch8029a772010-11-16 11:58:21 +0000175 private Message mAutoFillSetupMessage;
176
Michael Kolb8233fac2010-10-26 16:08:53 -0700177 private boolean mShouldShowErrorConsole;
178
179 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
180
181 // FIXME, temp address onPrepareMenu performance problem.
182 // When we move everything out of view, we should rewrite this.
183 private int mCurrentMenuState = 0;
184 private int mMenuState = R.id.MAIN_MENU;
185 private int mOldMenuState = EMPTY_MENU;
186 private Menu mCachedMenu;
187
Michael Kolb8233fac2010-10-26 16:08:53 -0700188 private boolean mMenuIsDown;
189
190 // For select and find, we keep track of the ActionMode so that
191 // finish() can be called as desired.
192 private ActionMode mActionMode;
193
194 /**
195 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
196 * of whether the configuration has changed. The first onMenuOpened call
197 * after a configuration change is simply a reopening of the same menu
198 * (i.e. mIconView did not change).
199 */
200 private boolean mConfigChanged;
201
202 /**
203 * Keeps track of whether the options menu is open. This is important in
204 * determining whether to show or hide the title bar overlay
205 */
206 private boolean mOptionsMenuOpen;
207
208 /**
209 * Whether or not the options menu is in its bigger, popup menu form. When
210 * true, we want the title bar overlay to be gone. When false, we do not.
211 * Only meaningful if mOptionsMenuOpen is true.
212 */
213 private boolean mExtendedMenuOpen;
214
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 private boolean mActivityPaused = true;
216 private boolean mLoadStopped;
217
218 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500219 // Checks to see when the bookmarks database has changed, and updates the
220 // Tabs' notion of whether they represent bookmarked sites.
221 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700222 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700223
Michael Kolbc3af0672011-08-09 10:24:41 -0700224 private boolean mBlockEvents;
225
Michael Kolb0b129122012-06-04 16:31:58 -0700226 private String mVoiceResult;
227
George Mount3636d0a2011-11-21 09:08:21 -0800228 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700229 mActivity = browser;
230 mSettings = BrowserSettings.getInstance();
231 mTabControl = new TabControl(this);
232 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700233 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800234 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700235 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700236
237 mUrlHandler = new UrlHandler(this);
238 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700239 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
240
Michael Kolb8233fac2010-10-26 16:08:53 -0700241 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500242 mBookmarksObserver = new ContentObserver(mHandler) {
243 @Override
244 public void onChange(boolean selfChange) {
245 int size = mTabControl.getTabCount();
246 for (int i = 0; i < size; i++) {
247 mTabControl.getTab(i).updateBookmarkedStatus();
248 }
249 }
250
251 };
252 browser.getContentResolver().registerContentObserver(
253 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700254
255 mNetworkHandler = new NetworkStateHandler(mActivity, this);
256 // Start watching the default geolocation permissions
257 mSystemAllowGeolocationOrigins =
258 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
259 mSystemAllowGeolocationOrigins.start();
260
John Reckaf262e72011-07-25 13:55:44 -0700261 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 }
263
John Reck9c35b9c2012-05-30 10:08:50 -0700264 @Override
265 public void start(final Intent intent) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800266 if (BrowserWebView.isClassic()) WebViewClassic.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800267 // mCrashRecoverHandler has any previously saved state.
268 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700269 }
270
George Mount3636d0a2011-11-21 09:08:21 -0800271 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700272 // Unless the last browser usage was within 24 hours, destroy any
273 // remaining incognito tabs.
274
275 Calendar lastActiveDate = icicle != null ?
276 (Calendar) icicle.getSerializable("lastActiveDate") : null;
277 Calendar today = Calendar.getInstance();
278 Calendar yesterday = Calendar.getInstance();
279 yesterday.add(Calendar.DATE, -1);
280
Patrick Scott7d50a932011-02-04 09:27:26 -0500281 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700282 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800283 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700284
Patrick Scott7d50a932011-02-04 09:27:26 -0500285 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700286 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500287 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000288
Michael Kolbc831b632011-05-11 09:30:34 -0700289 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500290 // Not able to restore so we go ahead and clear session cookies. We
291 // must do this before trying to login the user as we don't want to
292 // clear any session cookies set during login.
293 CookieManager.getInstance().removeSessionCookie();
294 }
295
Patrick Scottd43e75a2011-03-14 14:47:23 -0400296 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500297 new Runnable() {
298 @Override public void run() {
George Mount3636d0a2011-11-21 09:08:21 -0800299 onPreloginFinished(icicle, intent, currentTabId,
300 restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500301 }
302 });
303 }
304
Michael Kolbc831b632011-05-11 09:30:34 -0700305 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
George Mount3636d0a2011-11-21 09:08:21 -0800306 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700307 if (currentTabId == -1) {
John Reck1cf4b792011-07-26 10:22:22 -0700308 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800309 if (intent == null) {
310 // This won't happen under common scenarios. The icicle is
311 // not null, but there aren't any tabs to restore.
312 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700313 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800314 final Bundle extra = intent.getExtras();
315 // Create an initial tab.
316 // If the intent is ACTION_VIEW and data is not null, the Browser is
317 // invoked to view the content by another application. In this case,
318 // the tab will be close when exit.
319 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
320 Tab t = null;
321 if (urlData.isEmpty()) {
322 t = openTabToHomePage();
323 } else {
324 t = openTab(urlData);
325 }
326 if (t != null) {
327 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
328 }
329 WebView webView = t.getWebView();
330 if (extra != null) {
331 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
332 if (scale > 0 && scale <= 1000) {
333 webView.setInitialScale(scale);
334 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700335 }
336 }
John Reckd8c74522011-06-14 08:45:00 -0700337 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700338 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700339 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500340 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700341 List<Tab> tabs = mTabControl.getTabs();
342 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
343 for (Tab t : tabs) {
344 restoredTabs.add(t.getId());
345 }
346 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700347 if (tabs.size() == 0) {
348 openTabToHomePage();
349 }
John Reck1cf4b792011-07-26 10:22:22 -0700350 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700351 // TabControl.restoreState() will create a new tab even if
352 // restoring the state fails.
353 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800354 // Intent is non-null when framework thinks the browser should be
355 // launching with a new intent (icicle is null).
356 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700357 mIntentHandler.onNewIntent(intent);
358 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700359 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700360 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700361 String jsFlags = getSettings().getJsEngineFlags();
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -0800362 if (jsFlags.trim().length() != 0 && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000363 WebViewClassic.fromWebView(getCurrentWebView()).setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700364 }
George Mount3636d0a2011-11-21 09:08:21 -0800365 if (intent != null
366 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700367 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800368 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700369 }
370
John Reck1cf4b792011-07-26 10:22:22 -0700371 private static class PruneThumbnails implements Runnable {
372 private Context mContext;
373 private List<Long> mIds;
374
375 PruneThumbnails(Context context, List<Long> preserveIds) {
376 mContext = context.getApplicationContext();
377 mIds = preserveIds;
378 }
379
380 @Override
381 public void run() {
382 ContentResolver cr = mContext.getContentResolver();
383 if (mIds == null || mIds.size() == 0) {
384 cr.delete(Thumbnails.CONTENT_URI, null, null);
385 } else {
386 int length = mIds.size();
387 StringBuilder where = new StringBuilder();
388 where.append(Thumbnails._ID);
389 where.append(" not in (");
390 for (int i = 0; i < length; i++) {
391 where.append(mIds.get(i));
392 if (i < (length - 1)) {
393 where.append(",");
394 }
395 }
396 where.append(")");
397 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
398 }
399 }
400
401 }
402
Michael Kolb1514bb72010-11-22 09:11:48 -0800403 @Override
404 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700405 return mFactory;
406 }
407
408 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800409 public void onSetWebView(Tab tab, WebView view) {
410 mUi.onSetWebView(tab, view);
411 }
412
413 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800414 public void createSubWindow(Tab tab) {
415 endActionMode();
416 WebView mainView = tab.getWebView();
417 WebView subView = mFactory.createWebView((mainView == null)
418 ? false
419 : mainView.isPrivateBrowsingEnabled());
420 mUi.createSubWindow(tab, subView);
421 }
422
423 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700424 public Context getContext() {
425 return mActivity;
426 }
427
428 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700429 public Activity getActivity() {
430 return mActivity;
431 }
432
433 void setUi(UI ui) {
434 mUi = ui;
435 }
436
Michael Kolbaf63dba2012-05-16 12:58:05 -0700437 @Override
438 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700439 return mSettings;
440 }
441
442 IntentHandler getIntentHandler() {
443 return mIntentHandler;
444 }
445
446 @Override
447 public UI getUi() {
448 return mUi;
449 }
450
451 int getMaxTabs() {
452 return mActivity.getResources().getInteger(R.integer.max_tabs);
453 }
454
455 @Override
456 public TabControl getTabControl() {
457 return mTabControl;
458 }
459
Michael Kolb1bf23132010-11-19 12:55:12 -0800460 @Override
461 public List<Tab> getTabs() {
462 return mTabControl.getTabs();
463 }
464
John Reckaf262e72011-07-25 13:55:44 -0700465 // Open the icon database.
466 private void openIconDatabase() {
467 // We have to call getInstance on the UI thread
468 final WebIconDatabase instance = WebIconDatabase.getInstance();
469 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000470
John Reckaf262e72011-07-25 13:55:44 -0700471 @Override
472 public void run() {
473 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700474 }
John Reckaf262e72011-07-25 13:55:44 -0700475 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700476 }
477
478 private void startHandler() {
479 mHandler = new Handler() {
480
481 @Override
482 public void handleMessage(Message msg) {
483 switch (msg.what) {
484 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700485 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700486 break;
487 case FOCUS_NODE_HREF:
488 {
489 String url = (String) msg.getData().get("url");
490 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500491 String src = (String) msg.getData().get("src");
492 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700493 if (TextUtils.isEmpty(url)) {
494 break;
495 }
496 HashMap focusNodeMap = (HashMap) msg.obj;
497 WebView view = (WebView) focusNodeMap.get("webview");
498 // Only apply the action if the top window did not change.
499 if (getCurrentTopWebView() != view) {
500 break;
501 }
502 switch (msg.arg1) {
503 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700504 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700505 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500506 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700507 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500508 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500509 case R.id.open_newtab_context_menu_id:
510 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700511 openTab(url, parent,
512 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500513 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700514 case R.id.copy_link_context_menu_id:
515 copy(url);
516 break;
517 case R.id.save_link_context_menu_id:
518 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500519 DownloadHandler.onDownloadStartNoStream(
Andreas Sandblad8e4ce662012-06-11 11:02:49 +0200520 mActivity, url, view.getSettings().getUserAgentString(),
luxiaol62677b02013-07-22 07:54:49 +0800521 null, null, null, view.isPrivateBrowsingEnabled(), 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700522 break;
523 }
524 break;
525 }
526
527 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700528 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700529 break;
530
531 case STOP_LOAD:
532 stopLoading();
533 break;
534
535 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700536 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700537 mWakeLock.release();
538 // if we reach here, Browser should be still in the
539 // background loading after WAKELOCK_TIMEOUT (5-min).
540 // To avoid burning the battery, stop loading.
541 mTabControl.stopAllLoading();
542 }
543 break;
544
545 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800546 Tab tab = (Tab) msg.obj;
547 if (tab != null) {
548 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700549 }
550 break;
kaiyiz591110b2013-08-06 17:11:06 +0800551
552 case OPEN_MENU:
553 if (!mOptionsMenuOpen && mActivity != null ) {
554 mActivity.openOptionsMenu();
555 }
556 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700557 }
558 }
559 };
560
561 }
562
John Reckef654f12011-07-12 16:42:08 -0700563 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200564 public Tab getCurrentTab() {
565 return mTabControl.getCurrentTab();
566 }
567
Michael Kolbba99c5d2010-11-29 14:57:41 -0800568 @Override
569 public void shareCurrentPage() {
570 shareCurrentPage(mTabControl.getCurrentTab());
571 }
572
573 private void shareCurrentPage(Tab tab) {
574 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800575 sharePage(mActivity, tab.getTitle(),
576 tab.getUrl(), tab.getFavicon(),
577 createScreenshot(tab.getWebView(),
578 getDesiredThumbnailWidth(mActivity),
579 getDesiredThumbnailHeight(mActivity)));
580 }
581 }
582
Michael Kolb8233fac2010-10-26 16:08:53 -0700583 /**
584 * Share a page, providing the title, url, favicon, and a screenshot. Uses
585 * an {@link Intent} to launch the Activity chooser.
586 * @param c Context used to launch a new Activity.
587 * @param title Title of the page. Stored in the Intent with
588 * {@link Intent#EXTRA_SUBJECT}
589 * @param url URL of the page. Stored in the Intent with
590 * {@link Intent#EXTRA_TEXT}
591 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
592 * with {@link Browser#EXTRA_SHARE_FAVICON}
593 * @param screenshot Bitmap of a screenshot of the page. Stored in the
594 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
595 */
596 static final void sharePage(Context c, String title, String url,
597 Bitmap favicon, Bitmap screenshot) {
598 Intent send = new Intent(Intent.ACTION_SEND);
599 send.setType("text/plain");
600 send.putExtra(Intent.EXTRA_TEXT, url);
601 send.putExtra(Intent.EXTRA_SUBJECT, title);
602 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
603 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
604 try {
605 c.startActivity(Intent.createChooser(send, c.getString(
606 R.string.choosertitle_sharevia)));
607 } catch(android.content.ActivityNotFoundException ex) {
608 // if no app handles it, do nothing
609 }
610 }
611
612 private void copy(CharSequence text) {
613 ClipboardManager cm = (ClipboardManager) mActivity
614 .getSystemService(Context.CLIPBOARD_SERVICE);
615 cm.setText(text);
616 }
617
618 // lifecycle
619
John Reck9c35b9c2012-05-30 10:08:50 -0700620 @Override
621 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700622 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800623 // update the menu in case of a locale change
624 mActivity.invalidateOptionsMenu();
kaiyiz591110b2013-08-06 17:11:06 +0800625 if (mOptionsMenuOpen) {
626 mActivity.closeOptionsMenu();
627 mHandler.sendMessageDelayed(mHandler.obtainMessage(OPEN_MENU), 100);
628 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700629 if (mPageDialogsHandler != null) {
630 mPageDialogsHandler.onConfigurationChanged(config);
631 }
632 mUi.onConfigurationChanged(config);
633 }
634
635 @Override
636 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700637 if (!mUi.isWebShowing()) {
638 mUi.showWeb(false);
639 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700640 mIntentHandler.onNewIntent(intent);
641 }
642
John Reck9c35b9c2012-05-30 10:08:50 -0700643 @Override
644 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800645 if (mUi.isCustomViewShowing()) {
646 hideCustomView();
647 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700648 if (mActivityPaused) {
649 Log.e(LOGTAG, "BrowserActivity is already paused.");
650 return;
651 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800653 Tab tab = mTabControl.getCurrentTab();
654 if (tab != null) {
655 tab.pause();
656 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700657 if (mWakeLock == null) {
658 PowerManager pm = (PowerManager) mActivity
659 .getSystemService(Context.POWER_SERVICE);
660 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
661 }
Michael Kolb70976932010-11-30 11:34:01 -0800662 mWakeLock.acquire();
663 mHandler.sendMessageDelayed(mHandler
664 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
665 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 }
667 mUi.onPause();
668 mNetworkHandler.onPause();
669
670 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100671 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700672 if (sThumbnailBitmap != null) {
673 sThumbnailBitmap.recycle();
674 sThumbnailBitmap = null;
675 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700676 }
677
John Reck9c35b9c2012-05-30 10:08:50 -0700678 @Override
679 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700680 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800681 Bundle saveState = createSaveState();
682
683 // crash recovery manages all save & restore state
684 mCrashRecoveryHandler.writeState(saveState);
685 mSettings.setLastRunPaused(true);
686 }
687
688 /**
689 * Save the current state to outState. Does not write the state to
690 * disk.
691 * @return Bundle containing the current state of all tabs.
692 */
693 /* package */ Bundle createSaveState() {
694 Bundle saveState = new Bundle();
695 mTabControl.saveState(saveState);
696 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700697 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800698 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700699 }
George Mount3636d0a2011-11-21 09:08:21 -0800700 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700701 }
702
John Reck9c35b9c2012-05-30 10:08:50 -0700703 @Override
704 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 if (!mActivityPaused) {
706 Log.e(LOGTAG, "BrowserActivity is already resumed.");
707 return;
708 }
George Mount3636d0a2011-11-21 09:08:21 -0800709 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700710 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800711 Tab current = mTabControl.getCurrentTab();
712 if (current != null) {
713 current.resume();
714 resumeWebViewTimers(current);
715 }
John Reckf57c0292011-07-21 18:15:39 -0700716 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200717
Michael Kolb8233fac2010-10-26 16:08:53 -0700718 mUi.onResume();
719 mNetworkHandler.onResume();
720 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100721 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700722 if (mVoiceResult != null) {
723 mUi.onVoiceResult(mVoiceResult);
724 mVoiceResult = null;
725 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700726 }
727
John Reckf57c0292011-07-21 18:15:39 -0700728 private void releaseWakeLock() {
729 if (mWakeLock != null && mWakeLock.isHeld()) {
730 mHandler.removeMessages(RELEASE_WAKELOCK);
731 mWakeLock.release();
732 }
733 }
734
Michael Kolb70976932010-11-30 11:34:01 -0800735 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800736 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800737 * @param tab guaranteed non-null
738 */
739 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700740 boolean inLoad = tab.inPageLoad();
741 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
742 CookieSyncManager.getInstance().startSync();
743 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100744 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700745 }
746 }
747
Michael Kolb70976932010-11-30 11:34:01 -0800748 /**
749 * Pause all WebView timers using the WebView of the given tab
750 * @param tab
751 * @return true if the timers are paused or tab is null
752 */
753 private boolean pauseWebViewTimers(Tab tab) {
754 if (tab == null) {
755 return true;
756 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700757 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100758 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700759 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700760 }
Michael Kolb70976932010-11-30 11:34:01 -0800761 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700762 }
763
John Reck9c35b9c2012-05-30 10:08:50 -0700764 @Override
765 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800766 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700767 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
768 mUploadHandler = null;
769 }
770 if (mTabControl == null) return;
771 mUi.onDestroy();
772 // Remove the current tab and sub window
773 Tab t = mTabControl.getCurrentTab();
774 if (t != null) {
775 dismissSubWindow(t);
776 removeTab(t);
777 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500778 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700779 // Destroy all the tabs
780 mTabControl.destroy();
781 WebIconDatabase.getInstance().close();
782 // Stop watching the default geolocation permissions
783 mSystemAllowGeolocationOrigins.stop();
784 mSystemAllowGeolocationOrigins = null;
785 }
786
787 protected boolean isActivityPaused() {
788 return mActivityPaused;
789 }
790
John Reck9c35b9c2012-05-30 10:08:50 -0700791 @Override
792 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700793 mTabControl.freeMemory();
794 }
795
796 @Override
797 public boolean shouldShowErrorConsole() {
798 return mShouldShowErrorConsole;
799 }
800
801 protected void setShouldShowErrorConsole(boolean show) {
802 if (show == mShouldShowErrorConsole) {
803 // Nothing to do.
804 return;
805 }
806 mShouldShowErrorConsole = show;
807 Tab t = mTabControl.getCurrentTab();
808 if (t == null) {
809 // There is no current tab so we cannot toggle the error console
810 return;
811 }
812 mUi.setShouldShowErrorConsole(t, show);
813 }
814
815 @Override
816 public void stopLoading() {
817 mLoadStopped = true;
818 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700819 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700820 if (w != null) {
821 w.stopLoading();
822 mUi.onPageStopped(tab);
823 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700824 }
825
826 boolean didUserStopLoading() {
827 return mLoadStopped;
828 }
829
830 // WebViewController
831
832 @Override
John Reck324d4402011-01-11 16:56:42 -0800833 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700834
835 // We've started to load a new page. If there was a pending message
836 // to save a screenshot then we will now take the new page and save
837 // an incorrect screenshot. Therefore, remove any pending thumbnail
838 // messages from the queue.
839 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800840 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700841
842 // reset sync timer to avoid sync starts during loading a page
843 CookieSyncManager.getInstance().resetSync();
844
845 if (!mNetworkHandler.isNetworkUp()) {
846 view.setNetworkAvailable(false);
847 }
848
849 // when BrowserActivity just starts, onPageStarted may be called before
850 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
851 // to start the timer. As we won't switch tabs while an activity is in
852 // pause state, we can ensure calling resume and pause in pair.
853 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800854 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700855 }
856 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700857 endActionMode();
858
John Reck30c714c2010-12-16 17:30:34 -0800859 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700860
John Reck324d4402011-01-11 16:56:42 -0800861 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700862 // update the bookmark database for favicon
863 maybeUpdateFavicon(tab, null, url, favicon);
864
865 Performance.tracePageStart(url);
866
867 // Performance probe
868 if (false) {
869 Performance.onPageStarted();
870 }
871
872 }
873
874 @Override
John Reck324d4402011-01-11 16:56:42 -0800875 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700876 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800877 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200878
Michael Kolb8233fac2010-10-26 16:08:53 -0700879 // Performance probe
880 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800881 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700882 }
883
884 Performance.tracePageFinished();
885 }
886
887 @Override
John Reck30c714c2010-12-16 17:30:34 -0800888 public void onProgressChanged(Tab tab) {
889 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700890
891 if (newProgress == 100) {
892 CookieSyncManager.getInstance().sync();
893 // onProgressChanged() may continue to be called after the main
894 // frame has finished loading, as any remaining sub frames continue
895 // to load. We'll only get called once though with newProgress as
896 // 100 when everything is loaded. (onPageFinished is called once
897 // when the main frame completes loading regardless of the state of
898 // any sub frames so calls to onProgressChanges may continue after
899 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800900 if (tab.inPageLoad()) {
901 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200902 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
903 // pause the WebView timer and release the wake lock if it is
904 // finished while BrowserActivity is in pause state.
905 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700906 }
John Reckd9862372012-02-21 15:04:50 -0800907 if (!tab.isPrivateBrowsingEnabled()
908 && !TextUtils.isEmpty(tab.getUrl())
909 && !tab.isSnapshot()) {
910 // Only update the bookmark screenshot if the user did not
911 // cancel the load early and there is not already
912 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700913 if (tab.shouldUpdateThumbnail() &&
914 (tab.inForeground() && !didUserStopLoading()
915 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800916 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
917 mHandler.sendMessageDelayed(mHandler.obtainMessage(
918 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
919 500);
920 }
921 }
922 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700923 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800924 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700925 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800926 // still loading
927 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -0700928 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800929 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700930 }
931 }
John Reck30c714c2010-12-16 17:30:34 -0800932 mUi.onProgressChanged(tab);
933 }
934
935 @Override
Steve Block2466eff2011-10-03 15:33:09 +0100936 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800937 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 }
939
940 @Override
941 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800942 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800943 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800944 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700945 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
946 return;
947 }
948 // Update the title in the history database if not in private browsing mode
949 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -0700950 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700951 }
952 }
953
954 @Override
955 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800956 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700957 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
958 }
959
960 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800961 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
962 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700963 }
964
965 @Override
966 public boolean shouldOverrideKeyEvent(KeyEvent event) {
967 if (mMenuIsDown) {
968 // only check shortcut key when MENU is held
969 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
970 event);
971 } else {
972 return false;
973 }
974 }
975
976 @Override
John Reck997b1b72012-04-19 18:08:25 -0700977 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700978 if (!isActivityPaused()) {
979 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -0700980 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700981 } else {
John Reck997b1b72012-04-19 18:08:25 -0700982 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700983 }
984 }
John Reck997b1b72012-04-19 18:08:25 -0700985 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700986 }
987
988 @Override
John Reck324d4402011-01-11 16:56:42 -0800989 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700990 // Don't save anything in private browsing mode
991 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800992 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700993
John Reck324d4402011-01-11 16:56:42 -0800994 if (TextUtils.isEmpty(url)
995 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700996 return;
997 }
John Reckf57c0292011-07-21 18:15:39 -0700998 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -0700999 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001000 }
1001
1002 @Override
1003 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1004 AsyncTask<Void, Void, String[]> task =
1005 new AsyncTask<Void, Void, String[]>() {
1006 @Override
1007 public String[] doInBackground(Void... unused) {
1008 return Browser.getVisitedHistory(mActivity.getContentResolver());
1009 }
1010 @Override
1011 public void onPostExecute(String[] result) {
1012 callback.onReceiveValue(result);
1013 }
1014 };
1015 task.execute();
1016 }
1017
1018 @Override
1019 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1020 final HttpAuthHandler handler, final String host,
1021 final String realm) {
1022 String username = null;
1023 String password = null;
1024
1025 boolean reuseHttpAuthUsernamePassword
1026 = handler.useHttpAuthUsernamePassword();
1027
1028 if (reuseHttpAuthUsernamePassword && view != null) {
1029 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1030 if (credentials != null && credentials.length == 2) {
1031 username = credentials[0];
1032 password = credentials[1];
1033 }
1034 }
1035
1036 if (username != null && password != null) {
1037 handler.proceed(username, password);
1038 } else {
Ben Murdochfdd37112011-08-05 15:48:14 +01001039 if (tab.inForeground() && !handler.suppressDialog()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001040 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1041 } else {
1042 handler.cancel();
1043 }
1044 }
1045 }
1046
1047 @Override
1048 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001049 String contentDisposition, String mimetype, String referer,
1050 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001051 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001052 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
luxiaol62677b02013-07-22 07:54:49 +08001053 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled(), contentLength);
qqzhoua95a2e22013-04-18 17:28:31 +08001054 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001055 // This Tab was opened for the sole purpose of downloading a
1056 // file. Remove it.
1057 if (tab == mTabControl.getCurrentTab()) {
1058 // In this case, the Tab is still on top.
1059 goBackOnePageOrQuit();
1060 } else {
1061 // In this case, it is not.
1062 closeTab(tab);
1063 }
1064 }
1065 }
1066
1067 @Override
1068 public Bitmap getDefaultVideoPoster() {
1069 return mUi.getDefaultVideoPoster();
1070 }
1071
1072 @Override
1073 public View getVideoLoadingProgressView() {
1074 return mUi.getVideoLoadingProgressView();
1075 }
1076
1077 @Override
1078 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1079 SslError error) {
1080 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1081 }
1082
Patrick Scott92066772011-03-10 08:46:27 -05001083 @Override
1084 public void showAutoLogin(Tab tab) {
1085 assert tab.inForeground();
1086 // Update the title bar to show the auto-login request.
1087 mUi.showAutoLogin(tab);
1088 }
1089
1090 @Override
1091 public void hideAutoLogin(Tab tab) {
1092 assert tab.inForeground();
1093 mUi.hideAutoLogin(tab);
1094 }
1095
Michael Kolb8233fac2010-10-26 16:08:53 -07001096 // helper method
1097
1098 /*
1099 * Update the favorites icon if the private browsing isn't enabled and the
1100 * icon is valid.
1101 */
1102 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1103 final String url, Bitmap favicon) {
1104 if (favicon == null) {
1105 return;
1106 }
1107 if (!tab.isPrivateBrowsingEnabled()) {
1108 Bookmarks.updateFavicon(mActivity
1109 .getContentResolver(), originalUrl, url, favicon);
1110 }
1111 }
1112
Leon Scroggins4cd97792010-12-03 15:31:56 -05001113 @Override
1114 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001115 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001116 mUi.bookmarkedStatusHasChanged(tab);
1117 }
1118
Michael Kolb8233fac2010-10-26 16:08:53 -07001119 // end WebViewController
1120
1121 protected void pageUp() {
1122 getCurrentTopWebView().pageUp(false);
1123 }
1124
1125 protected void pageDown() {
1126 getCurrentTopWebView().pageDown(false);
1127 }
1128
1129 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001130 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001131 public void editUrl() {
1132 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001133 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001134 }
1135
John Reck9c35b9c2012-05-30 10:08:50 -07001136 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001137 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001138 WebChromeClient.CustomViewCallback callback) {
1139 if (tab.inForeground()) {
1140 if (mUi.isCustomViewShowing()) {
1141 callback.onCustomViewHidden();
1142 return;
1143 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001144 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001145 // Save the menu state and set it to empty while the custom
1146 // view is showing.
1147 mOldMenuState = mMenuState;
1148 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001149 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001150 }
1151 }
1152
1153 @Override
1154 public void hideCustomView() {
1155 if (mUi.isCustomViewShowing()) {
1156 mUi.onHideCustomView();
1157 // Reset the old menu state.
1158 mMenuState = mOldMenuState;
1159 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001160 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 }
1162 }
1163
John Reck9c35b9c2012-05-30 10:08:50 -07001164 @Override
1165 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 Intent intent) {
1167 if (getCurrentTopWebView() == null) return;
1168 switch (requestCode) {
1169 case PREFERENCES_PAGE:
1170 if (resultCode == Activity.RESULT_OK && intent != null) {
1171 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001172 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001173 mTabControl.removeParentChildRelationShips();
1174 }
1175 }
1176 break;
1177 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001178 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001179 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001180 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001181 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001182 case AUTOFILL_SETUP:
1183 // Determine whether a profile was actually set up or not
1184 // and if so, send the message back to the WebTextView to
1185 // fill the form with the new profile.
1186 if (getSettings().getAutoFillProfile() != null) {
1187 mAutoFillSetupMessage.sendToTarget();
1188 mAutoFillSetupMessage = null;
1189 }
1190 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001191 case COMBO_VIEW:
1192 if (intent == null || resultCode != Activity.RESULT_OK) {
1193 break;
1194 }
John Reck3ba45532011-08-11 16:26:53 -07001195 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001196 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1197 Tab t = getCurrentTab();
1198 Uri uri = intent.getData();
1199 loadUrl(t, uri.toString());
1200 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1201 String[] urls = intent.getStringArrayExtra(
1202 ComboViewActivity.EXTRA_OPEN_ALL);
1203 Tab parent = getCurrentTab();
1204 for (String url : urls) {
kaiyiz47097d62013-08-09 09:30:28 +08001205 if (url != null) {
1206 parent = openTab(url, parent,
1207 !mSettings.openInBackground(), true);
1208 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001209 }
1210 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1211 long id = intent.getLongExtra(
1212 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1213 if (id >= 0) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08001214 if (BrowserWebView.isClassic()) {
1215 createNewSnapshotTab(id, true);
1216 } else {
1217 Toast.makeText(mActivity, "Snapshot Tab requires WebViewClassic",
1218 Toast.LENGTH_LONG).show();
1219 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001220 }
1221 }
1222 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001223 case VOICE_RESULT:
1224 if (resultCode == Activity.RESULT_OK && intent != null) {
1225 ArrayList<String> results = intent.getStringArrayListExtra(
1226 RecognizerIntent.EXTRA_RESULTS);
1227 if (results.size() >= 1) {
1228 mVoiceResult = results.get(0);
1229 }
1230 }
1231 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 default:
1233 break;
1234 }
1235 getCurrentTopWebView().requestFocus();
1236 }
1237
1238 /**
1239 * Open the Go page.
1240 * @param startWithHistory If true, open starting on the history tab.
1241 * Otherwise, start with the bookmarks tab.
1242 */
1243 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001244 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001245 if (mTabControl.getCurrentWebView() == null) {
1246 return;
1247 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001248 // clear action mode
1249 if (isInCustomActionMode()) {
1250 endActionMode();
1251 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001252 Bundle extras = new Bundle();
1253 // Disable opening in a new window if we have maxed out the windows
1254 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1255 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001256 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001257 }
1258
1259 // combo view callbacks
1260
Michael Kolb8233fac2010-10-26 16:08:53 -07001261 // key handling
1262 protected void onBackKey() {
1263 if (!mUi.onBackKey()) {
1264 WebView subwindow = mTabControl.getCurrentSubWindow();
1265 if (subwindow != null) {
1266 if (subwindow.canGoBack()) {
1267 subwindow.goBack();
1268 } else {
1269 dismissSubWindow(mTabControl.getCurrentTab());
1270 }
1271 } else {
1272 goBackOnePageOrQuit();
1273 }
1274 }
1275 }
1276
Michael Kolb4bd767d2011-05-27 11:33:55 -07001277 protected boolean onMenuKey() {
1278 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001279 }
1280
Michael Kolb8233fac2010-10-26 16:08:53 -07001281 // menu handling and state
1282 // TODO: maybe put into separate handler
1283
John Reck9c35b9c2012-05-30 10:08:50 -07001284 @Override
1285 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001286 if (mMenuState == EMPTY_MENU) {
1287 return false;
1288 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001289 MenuInflater inflater = mActivity.getMenuInflater();
1290 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001291 return true;
1292 }
1293
John Reck9c35b9c2012-05-30 10:08:50 -07001294 @Override
1295 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001296 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001297 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001298 return;
1299 }
1300 if (!(v instanceof WebView)) {
1301 return;
1302 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001303 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001304 WebView.HitTestResult result = webview.getHitTestResult();
1305 if (result == null) {
1306 return;
1307 }
1308
1309 int type = result.getType();
1310 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1311 Log.w(LOGTAG,
1312 "We should not show context menu when nothing is touched");
1313 return;
1314 }
1315 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1316 // let TextView handles context menu
1317 return;
1318 }
1319
1320 // Note, http://b/issue?id=1106666 is requesting that
1321 // an inflated menu can be used again. This is not available
1322 // yet, so inflate each time (yuk!)
1323 MenuInflater inflater = mActivity.getMenuInflater();
1324 inflater.inflate(R.menu.browsercontext, menu);
1325
1326 // Show the correct menu group
1327 final String extra = result.getExtra();
Michael Kolbc159c1a2011-12-15 16:06:38 -08001328 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001329 menu.setGroupVisible(R.id.PHONE_MENU,
1330 type == WebView.HitTestResult.PHONE_TYPE);
1331 menu.setGroupVisible(R.id.EMAIL_MENU,
1332 type == WebView.HitTestResult.EMAIL_TYPE);
1333 menu.setGroupVisible(R.id.GEO_MENU,
1334 type == WebView.HitTestResult.GEO_TYPE);
1335 menu.setGroupVisible(R.id.IMAGE_MENU,
1336 type == WebView.HitTestResult.IMAGE_TYPE
1337 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1338 menu.setGroupVisible(R.id.ANCHOR_MENU,
1339 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1340 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001341 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1342 || type == WebView.HitTestResult.PHONE_TYPE
1343 || type == WebView.HitTestResult.EMAIL_TYPE
1344 || type == WebView.HitTestResult.GEO_TYPE;
1345 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1346 if (hitText) {
1347 menu.findItem(R.id.select_text_menu_id)
1348 .setOnMenuItemClickListener(new SelectText(webview));
1349 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001350 // Setup custom handling depending on the type
1351 switch (type) {
1352 case WebView.HitTestResult.PHONE_TYPE:
1353 menu.setHeaderTitle(Uri.decode(extra));
1354 menu.findItem(R.id.dial_context_menu_id).setIntent(
1355 new Intent(Intent.ACTION_VIEW, Uri
1356 .parse(WebView.SCHEME_TEL + extra)));
1357 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1358 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1359 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1360 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1361 addIntent);
1362 menu.findItem(R.id.copy_phone_context_menu_id)
1363 .setOnMenuItemClickListener(
1364 new Copy(extra));
1365 break;
1366
1367 case WebView.HitTestResult.EMAIL_TYPE:
1368 menu.setHeaderTitle(extra);
1369 menu.findItem(R.id.email_context_menu_id).setIntent(
1370 new Intent(Intent.ACTION_VIEW, Uri
1371 .parse(WebView.SCHEME_MAILTO + extra)));
1372 menu.findItem(R.id.copy_mail_context_menu_id)
1373 .setOnMenuItemClickListener(
1374 new Copy(extra));
1375 break;
1376
1377 case WebView.HitTestResult.GEO_TYPE:
1378 menu.setHeaderTitle(extra);
1379 menu.findItem(R.id.map_context_menu_id).setIntent(
1380 new Intent(Intent.ACTION_VIEW, Uri
1381 .parse(WebView.SCHEME_GEO
1382 + URLEncoder.encode(extra))));
1383 menu.findItem(R.id.copy_geo_context_menu_id)
1384 .setOnMenuItemClickListener(
1385 new Copy(extra));
1386 break;
1387
1388 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1389 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001390 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001391 // decide whether to show the open link in new tab option
1392 boolean showNewTab = mTabControl.canCreateNewTab();
1393 MenuItem newTabItem
1394 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001395 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001396 ? R.string.contextmenu_openlink_newwindow_background
1397 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001398 newTabItem.setVisible(showNewTab);
1399 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001400 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1401 newTabItem.setOnMenuItemClickListener(
1402 new MenuItem.OnMenuItemClickListener() {
1403 @Override
1404 public boolean onMenuItemClick(MenuItem item) {
1405 final HashMap<String, WebView> hrefMap =
1406 new HashMap<String, WebView>();
1407 hrefMap.put("webview", webview);
1408 final Message msg = mHandler.obtainMessage(
1409 FOCUS_NODE_HREF,
1410 R.id.open_newtab_context_menu_id,
1411 0, hrefMap);
1412 webview.requestFocusNodeHref(msg);
1413 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001414 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001415 });
1416 } else {
1417 newTabItem.setOnMenuItemClickListener(
1418 new MenuItem.OnMenuItemClickListener() {
1419 @Override
1420 public boolean onMenuItemClick(MenuItem item) {
1421 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001422 openTab(extra, parent,
1423 !mSettings.openInBackground(),
1424 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001425 return true;
1426 }
1427 });
1428 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001429 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001430 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1431 break;
1432 }
1433 // otherwise fall through to handle image part
1434 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001435 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1436 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001437 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1438 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001439 shareItem.setOnMenuItemClickListener(
1440 new MenuItem.OnMenuItemClickListener() {
1441 @Override
1442 public boolean onMenuItemClick(MenuItem item) {
1443 sharePage(mActivity, null, extra, null,
1444 null);
1445 return true;
1446 }
1447 }
1448 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001449 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001450 menu.findItem(R.id.view_image_context_menu_id)
1451 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1452 @Override
1453 public boolean onMenuItemClick(MenuItem item) {
1454 openTab(extra, mTabControl.getCurrentTab(), true, true);
1455 return false;
1456 }
1457 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001458 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1459 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1460 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001461 menu.findItem(R.id.set_wallpaper_context_menu_id).
1462 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1463 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001464 break;
1465
1466 default:
1467 Log.w(LOGTAG, "We should not get here.");
1468 break;
1469 }
1470 //update the ui
1471 mUi.onContextMenuCreated(menu);
1472 }
1473
1474 /**
1475 * As the menu can be open when loading state changes
1476 * we must manually update the state of the stop/reload menu
1477 * item
1478 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001479 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001480 if (menu == null) {
1481 return;
1482 }
1483 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001484 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001485 menu.findItem(R.id.stop_menu_id):
1486 menu.findItem(R.id.reload_menu_id);
1487 if (src != null) {
1488 dest.setIcon(src.getIcon());
1489 dest.setTitle(src.getTitle());
1490 }
1491 }
1492
John Reck9c35b9c2012-05-30 10:08:50 -07001493 @Override
1494 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001495 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001496 // hold on to the menu reference here; it is used by the page callbacks
1497 // to update the menu based on loading state
1498 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001499 // Note: setVisible will decide whether an item is visible; while
1500 // setEnabled() will decide whether an item is enabled, which also means
1501 // whether the matching shortcut key will function.
1502 switch (mMenuState) {
1503 case EMPTY_MENU:
1504 if (mCurrentMenuState != mMenuState) {
1505 menu.setGroupVisible(R.id.MAIN_MENU, false);
1506 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1507 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1508 }
1509 break;
1510 default:
1511 if (mCurrentMenuState != mMenuState) {
1512 menu.setGroupVisible(R.id.MAIN_MENU, true);
1513 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1514 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1515 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001516 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001517 break;
1518 }
1519 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001520 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001521 }
1522
Michael Kolb4bf79712011-07-14 14:18:12 -07001523 @Override
1524 public void updateMenuState(Tab tab, Menu menu) {
1525 boolean canGoBack = false;
1526 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001527 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001528 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001529 if (tab != null) {
1530 canGoBack = tab.canGoBack();
1531 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001532 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001533 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001534 }
1535 final MenuItem back = menu.findItem(R.id.back_menu_id);
1536 back.setEnabled(canGoBack);
1537
1538 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001539
1540 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1541 forward.setEnabled(canGoForward);
1542
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001543 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1544 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001545 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001546 if (source != null && dest != null) {
1547 dest.setTitle(source.getTitle());
1548 dest.setIcon(source.getIcon());
1549 }
John Recke1a03a32011-09-14 17:04:16 -07001550 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001551
1552 // decide whether to show the share link option
1553 PackageManager pm = mActivity.getPackageManager();
1554 Intent send = new Intent(Intent.ACTION_SEND);
1555 send.setType("text/plain");
1556 ResolveInfo ri = pm.resolveActivity(send,
1557 PackageManager.MATCH_DEFAULT_ONLY);
1558 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1559
1560 boolean isNavDump = mSettings.enableNavDump();
1561 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1562 nav.setVisible(isNavDump);
1563 nav.setEnabled(isNavDump);
1564
1565 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001566 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1567 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001568 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1569 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001570
1571 // history and snapshots item are the members of COMBO menu group,
1572 // so if show history item, only make snapshots item invisible.
1573 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001574
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001575 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001576 }
1577
John Reck9c35b9c2012-05-30 10:08:50 -07001578 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001579 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001580 if (null == getCurrentTopWebView()) {
1581 return false;
1582 }
1583 if (mMenuIsDown) {
1584 // The shortcut action consumes the MENU. Even if it is still down,
1585 // it won't trigger the next shortcut action. In the case of the
1586 // shortcut action triggering a new activity, like Bookmarks, we
1587 // won't get onKeyUp for MENU. So it is important to reset it here.
1588 mMenuIsDown = false;
1589 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001590 if (mUi.onOptionsItemSelected(item)) {
1591 // ui callback handled it
1592 return true;
1593 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001594 switch (item.getItemId()) {
1595 // -- Main menu
1596 case R.id.new_tab_menu_id:
1597 openTabToHomePage();
1598 break;
1599
1600 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001601 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001602 break;
1603
Afzal Najamd4e33312012-04-26 01:54:01 -04001604 case R.id.close_other_tabs_id:
1605 closeOtherTabs();
1606 break;
1607
Michael Kolb8233fac2010-10-26 16:08:53 -07001608 case R.id.goto_menu_id:
1609 editUrl();
1610 break;
1611
1612 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001613 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1614 break;
1615
1616 case R.id.history_menu_id:
1617 bookmarksOrHistoryPicker(ComboViews.History);
1618 break;
1619
1620 case R.id.snapshots_menu_id:
1621 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001622 break;
1623
Michael Kolb8233fac2010-10-26 16:08:53 -07001624 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001625 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001626 break;
1627
1628 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001629 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001630 stopLoading();
1631 } else {
1632 getCurrentTopWebView().reload();
1633 }
1634 break;
1635
1636 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001637 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001638 break;
1639
1640 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001641 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001642 break;
1643
1644 case R.id.close_menu_id:
1645 // Close the subwindow if it exists.
1646 if (mTabControl.getCurrentSubWindow() != null) {
1647 dismissSubWindow(mTabControl.getCurrentTab());
1648 break;
1649 }
1650 closeCurrentTab();
1651 break;
1652
kaiyiza8b6dbb2013-07-29 18:11:22 +08001653 case R.id.exit_menu_id:
1654 String ret = SystemProperties.get("persist.debug.browsermonkeytest");
1655 if (ret != null && ret.equals("enable"))
1656 break;
luxiaolb40014b2013-07-19 10:01:43 +08001657 showExitDialog(mActivity);
1658 return true;
kaiyiza8b6dbb2013-07-29 18:11:22 +08001659
Michael Kolb8233fac2010-10-26 16:08:53 -07001660 case R.id.homepage_menu_id:
1661 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001662 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001663 break;
1664
1665 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001666 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001667 break;
1668
1669 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001670 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001671 break;
1672
John Reck2bc80422011-06-30 15:11:49 -07001673 case R.id.save_snapshot_menu_id:
1674 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001675 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001676 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001677 break;
1678
Michael Kolb8233fac2010-10-26 16:08:53 -07001679 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001680 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001681 break;
1682
John Recke1a03a32011-09-14 17:04:16 -07001683 case R.id.snapshot_go_live:
1684 goLive();
1685 return true;
1686
Michael Kolb8233fac2010-10-26 16:08:53 -07001687 case R.id.share_page_menu_id:
1688 Tab currentTab = mTabControl.getCurrentTab();
1689 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001690 return false;
1691 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001692 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001693 break;
1694
1695 case R.id.dump_nav_menu_id:
1696 getCurrentTopWebView().debugDump();
1697 break;
1698
Michael Kolb8233fac2010-10-26 16:08:53 -07001699 case R.id.zoom_in_menu_id:
1700 getCurrentTopWebView().zoomIn();
1701 break;
1702
1703 case R.id.zoom_out_menu_id:
1704 getCurrentTopWebView().zoomOut();
1705 break;
1706
1707 case R.id.view_downloads_menu_id:
1708 viewDownloads();
1709 break;
1710
John Reck42229bc2011-08-19 13:26:43 -07001711 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001712 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07001713 break;
1714
Michael Kolb8233fac2010-10-26 16:08:53 -07001715 case R.id.window_one_menu_id:
1716 case R.id.window_two_menu_id:
1717 case R.id.window_three_menu_id:
1718 case R.id.window_four_menu_id:
1719 case R.id.window_five_menu_id:
1720 case R.id.window_six_menu_id:
1721 case R.id.window_seven_menu_id:
1722 case R.id.window_eight_menu_id:
1723 {
1724 int menuid = item.getItemId();
1725 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1726 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1727 Tab desiredTab = mTabControl.getTab(id);
1728 if (desiredTab != null &&
1729 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001730 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001731 }
1732 break;
1733 }
1734 }
1735 }
1736 break;
1737
1738 default:
1739 return false;
1740 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001741 return true;
1742 }
1743
John Reck68234a92012-04-19 15:27:12 -07001744 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
1745 implements OnCancelListener {
1746
1747 private Tab mTab;
1748 private Dialog mProgressDialog;
1749 private ContentValues mValues;
1750
1751 private SaveSnapshotTask(Tab tab) {
1752 mTab = tab;
1753 }
1754
1755 @Override
1756 protected void onPreExecute() {
1757 CharSequence message = mActivity.getText(R.string.saving_snapshot);
1758 mProgressDialog = ProgressDialog.show(mActivity, null, message,
1759 true, true, this);
1760 mValues = mTab.createSnapshotValues();
1761 }
1762
1763 @Override
1764 protected Long doInBackground(Void... params) {
1765 if (!mTab.saveViewState(mValues)) {
1766 return null;
1767 }
1768 if (isCancelled()) {
1769 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
1770 File file = mActivity.getFileStreamPath(path);
1771 if (!file.delete()) {
1772 file.deleteOnExit();
1773 }
1774 return null;
1775 }
1776 final ContentResolver cr = mActivity.getContentResolver();
1777 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
1778 if (result == null) {
1779 return null;
1780 }
1781 long id = ContentUris.parseId(result);
1782 return id;
1783 }
1784
1785 @Override
1786 protected void onPostExecute(Long id) {
1787 if (isCancelled()) {
1788 return;
1789 }
1790 mProgressDialog.dismiss();
1791 if (id == null) {
1792 Toast.makeText(mActivity, R.string.snapshot_failed,
1793 Toast.LENGTH_SHORT).show();
1794 return;
1795 }
1796 Bundle b = new Bundle();
1797 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1798 mUi.showComboView(ComboViews.Snapshots, b);
1799 }
1800
1801 @Override
1802 public void onCancel(DialogInterface dialog) {
1803 cancel(true);
1804 }
1805 }
1806
Michael Kolb80f75082012-04-10 10:50:06 -07001807 @Override
1808 public void toggleUserAgent() {
1809 WebView web = getCurrentWebView();
1810 mSettings.toggleDesktopUseragent(web);
1811 web.loadUrl(web.getOriginalUrl());
1812 }
1813
1814 @Override
1815 public void findOnPage() {
1816 getCurrentTopWebView().showFindDialog(null, true);
1817 }
1818
1819 @Override
1820 public void openPreferences() {
1821 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1822 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1823 getCurrentTopWebView().getUrl());
1824 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1825 }
1826
1827 @Override
1828 public void bookmarkCurrentPage() {
1829 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
1830 if (bookmarkIntent != null) {
1831 mActivity.startActivity(bookmarkIntent);
1832 }
1833 }
1834
John Recke1a03a32011-09-14 17:04:16 -07001835 private void goLive() {
1836 Tab t = getCurrentTab();
1837 t.loadUrl(t.getUrl(), null);
1838 }
1839
luxiaolb40014b2013-07-19 10:01:43 +08001840 private void showExitDialog(final Activity activity) {
1841 new AlertDialog.Builder(activity)
1842 .setTitle(R.string.exit_browser_title)
1843 .setIcon(android.R.drawable.ic_dialog_alert)
1844 .setMessage(R.string.exit_browser_msg)
1845 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
1846 public void onClick(DialogInterface dialog, int which) {
1847 activity.moveTaskToBack(true);
1848 dialog.dismiss();
1849 }
1850 })
1851 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
1852 public void onClick(DialogInterface dialog, int which) {
1853 activity.finish();
1854 mHandler.postDelayed(new Runnable() {
1855 @Override
1856 public void run() {
1857 // TODO Auto-generated method stub
1858 mCrashRecoveryHandler.clearState(true);
1859 int pid = android.os.Process.myPid();
1860 android.os.Process.killProcess(pid);
1861 }
1862 }, 300);
1863 dialog.dismiss();
1864 }
1865 })
1866 .show();
1867 }
1868
Michael Kolb315d5022011-10-13 12:47:11 -07001869 @Override
1870 public void showPageInfo() {
1871 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
1872 }
1873
John Reck9c35b9c2012-05-30 10:08:50 -07001874 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001875 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001876 // Let the History and Bookmark fragments handle menus they created.
1877 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1878 return false;
1879 }
1880
Michael Kolb8233fac2010-10-26 16:08:53 -07001881 int id = item.getItemId();
1882 boolean result = true;
1883 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001884 // -- Browser context menu
1885 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001886 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001887 case R.id.copy_link_context_menu_id:
1888 final WebView webView = getCurrentTopWebView();
1889 if (null == webView) {
1890 result = false;
1891 break;
1892 }
1893 final HashMap<String, WebView> hrefMap =
1894 new HashMap<String, WebView>();
1895 hrefMap.put("webview", webView);
1896 final Message msg = mHandler.obtainMessage(
1897 FOCUS_NODE_HREF, id, 0, hrefMap);
1898 webView.requestFocusNodeHref(msg);
1899 break;
1900
1901 default:
1902 // For other context menus
1903 result = onOptionsItemSelected(item);
1904 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001905 return result;
1906 }
1907
1908 /**
1909 * support programmatically opening the context menu
1910 */
1911 public void openContextMenu(View view) {
1912 mActivity.openContextMenu(view);
1913 }
1914
1915 /**
1916 * programmatically open the options menu
1917 */
1918 public void openOptionsMenu() {
1919 mActivity.openOptionsMenu();
1920 }
1921
John Reck9c35b9c2012-05-30 10:08:50 -07001922 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001923 public boolean onMenuOpened(int featureId, Menu menu) {
1924 if (mOptionsMenuOpen) {
1925 if (mConfigChanged) {
1926 // We do not need to make any changes to the state of the
1927 // title bar, since the only thing that happened was a
1928 // change in orientation
1929 mConfigChanged = false;
1930 } else {
1931 if (!mExtendedMenuOpen) {
1932 mExtendedMenuOpen = true;
1933 mUi.onExtendedMenuOpened();
1934 } else {
1935 // Switching the menu back to icon view, so show the
1936 // title bar once again.
1937 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001938 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001939 }
1940 }
1941 } else {
1942 // The options menu is closed, so open it, and show the title
1943 mOptionsMenuOpen = true;
1944 mConfigChanged = false;
1945 mExtendedMenuOpen = false;
1946 mUi.onOptionsMenuOpened();
1947 }
1948 return true;
1949 }
1950
John Reck9c35b9c2012-05-30 10:08:50 -07001951 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001952 public void onOptionsMenuClosed(Menu menu) {
1953 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001954 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001955 }
1956
John Reck9c35b9c2012-05-30 10:08:50 -07001957 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001958 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001959 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001960 }
1961
1962 // Helper method for getting the top window.
1963 @Override
1964 public WebView getCurrentTopWebView() {
1965 return mTabControl.getCurrentTopWebView();
1966 }
1967
1968 @Override
1969 public WebView getCurrentWebView() {
1970 return mTabControl.getCurrentWebView();
1971 }
1972
1973 /*
1974 * This method is called as a result of the user selecting the options
1975 * menu to see the download window. It shows the download window on top of
1976 * the current window.
1977 */
1978 void viewDownloads() {
1979 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1980 mActivity.startActivity(intent);
1981 }
1982
John Reck30b065e2011-07-19 10:58:05 -07001983 int getActionModeHeight() {
1984 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1985 new int[] { android.R.attr.actionBarSize });
1986 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1987 actionBarSizeTypedArray.recycle();
1988 return size;
1989 }
1990
Michael Kolb8233fac2010-10-26 16:08:53 -07001991 // action mode
1992
John Reck9c35b9c2012-05-30 10:08:50 -07001993 @Override
1994 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001995 mUi.onActionModeStarted(mode);
1996 mActionMode = mode;
1997 }
1998
1999 /*
2000 * True if a custom ActionMode (i.e. find or select) is in use.
2001 */
2002 @Override
2003 public boolean isInCustomActionMode() {
2004 return mActionMode != null;
2005 }
2006
2007 /*
2008 * End the current ActionMode.
2009 */
2010 @Override
2011 public void endActionMode() {
2012 if (mActionMode != null) {
2013 mActionMode.finish();
2014 }
2015 }
2016
2017 /*
2018 * Called by find and select when they are finished. Replace title bars
2019 * as necessary.
2020 */
John Reck9c35b9c2012-05-30 10:08:50 -07002021 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002022 public void onActionModeFinished(ActionMode mode) {
2023 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002024 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002025 mActionMode = null;
2026 }
2027
2028 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002029 final Tab tab = getCurrentTab();
2030 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002031 }
2032
2033 // bookmark handling
2034
2035 /**
2036 * add the current page as a bookmark to the given folder id
2037 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002038 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002039 * bookmarked, and if it is, edit that bookmark. If false, and
2040 * the site is already bookmarked, do not attempt to edit the
2041 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002042 */
2043 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002044 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002045 WebView w = getCurrentTopWebView();
2046 if (w == null) {
2047 return null;
2048 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002049 Intent i = new Intent(mActivity,
2050 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002051 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2052 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2053 String touchIconUrl = w.getTouchIconUrl();
2054 if (touchIconUrl != null) {
2055 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2056 WebSettings settings = w.getSettings();
2057 if (settings != null) {
2058 i.putExtra(AddBookmarkPage.USER_AGENT,
2059 settings.getUserAgentString());
2060 }
2061 }
2062 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2063 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2064 getDesiredThumbnailHeight(mActivity)));
2065 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002066 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002067 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2068 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002069 // Put the dialog at the upper right of the screen, covering the
2070 // star on the title bar.
2071 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002072 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002073 }
2074
2075 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002076 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002077 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002078 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002079 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002080 }
2081
2082 // thumbnails
2083
2084 /**
2085 * Return the desired width for thumbnail screenshots, which are stored in
2086 * the database, and used on the bookmarks screen.
2087 * @param context Context for finding out the density of the screen.
2088 * @return desired width for thumbnail screenshot.
2089 */
2090 static int getDesiredThumbnailWidth(Context context) {
2091 return context.getResources().getDimensionPixelOffset(
2092 R.dimen.bookmarkThumbnailWidth);
2093 }
2094
2095 /**
2096 * Return the desired height for thumbnail screenshots, which are stored in
2097 * the database, and used on the bookmarks screen.
2098 * @param context Context for finding out the density of the screen.
2099 * @return desired height for thumbnail screenshot.
2100 */
2101 static int getDesiredThumbnailHeight(Context context) {
2102 return context.getResources().getDimensionPixelOffset(
2103 R.dimen.bookmarkThumbnailHeight);
2104 }
2105
John Reck8cc92352011-07-06 17:41:52 -07002106 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002107 if (view == null || view.getContentHeight() == 0
2108 || view.getContentWidth() == 0) {
2109 return null;
2110 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002111 // We render to a bitmap 2x the desired size so that we can then
2112 // re-scale it with filtering since canvas.scale doesn't filter
2113 // This helps reduce aliasing at the cost of being slightly blurry
2114 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002115 int scaledWidth = width * filter_scale;
2116 int scaledHeight = height * filter_scale;
2117 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2118 || sThumbnailBitmap.getHeight() != scaledHeight) {
2119 if (sThumbnailBitmap != null) {
2120 sThumbnailBitmap.recycle();
2121 sThumbnailBitmap = null;
2122 }
2123 sThumbnailBitmap =
2124 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002125 }
John Reckd7dd9b22011-08-30 09:18:29 -07002126 Canvas canvas = new Canvas(sThumbnailBitmap);
2127 int contentWidth = view.getContentWidth();
2128 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2129 if (view instanceof BrowserWebView) {
2130 int dy = -((BrowserWebView)view).getTitleHeight();
2131 canvas.translate(0, dy * overviewScale);
2132 }
2133
2134 canvas.scale(overviewScale, overviewScale);
2135
2136 if (view instanceof BrowserWebView) {
2137 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002138 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002139 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002140 }
John Reckd7dd9b22011-08-30 09:18:29 -07002141 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2142 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002143 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002144 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002145 }
2146
John Reck34ef2672011-02-10 11:30:55 -08002147 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002148 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002149 // FIXME: Would like to make sure there is actually something to
2150 // draw, but the API for that (WebViewCore.pictureReady()) is not
2151 // currently accessible here.
2152
John Reck34ef2672011-02-10 11:30:55 -08002153 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002154 if (view == null) {
2155 // Tab was destroyed
2156 return;
2157 }
John Reck34ef2672011-02-10 11:30:55 -08002158 final String url = tab.getUrl();
2159 final String originalUrl = view.getOriginalUrl();
John Reck34ef2672011-02-10 11:30:55 -08002160 if (TextUtils.isEmpty(url)) {
2161 return;
2162 }
2163
2164 // Only update thumbnails for web urls (http(s)://), not for
2165 // about:, javascript:, data:, etc...
2166 // Unless it is a bookmarked site, then always update
2167 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2168 return;
2169 }
2170
Michael Kolb8233fac2010-10-26 16:08:53 -07002171 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2172 getDesiredThumbnailHeight(mActivity));
2173 if (bm == null) {
2174 return;
2175 }
2176
2177 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002178 new AsyncTask<Void, Void, Void>() {
2179 @Override
2180 protected Void doInBackground(Void... unused) {
2181 Cursor cursor = null;
2182 try {
2183 // TODO: Clean this up
2184 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2185 if (cursor != null && cursor.moveToFirst()) {
2186 final ByteArrayOutputStream os =
2187 new ByteArrayOutputStream();
2188 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002189
John Reck34ef2672011-02-10 11:30:55 -08002190 ContentValues values = new ContentValues();
2191 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002192
John Reck34ef2672011-02-10 11:30:55 -08002193 do {
John Reck617fd832011-02-16 14:35:59 -08002194 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002195 cr.update(Images.CONTENT_URI, values, null, null);
2196 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002197 }
John Reck34ef2672011-02-10 11:30:55 -08002198 } catch (IllegalStateException e) {
2199 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002200 } catch (SQLiteException s) {
2201 // Added for possible error when user tries to remove the same bookmark
2202 // that is being updated with a screen shot
2203 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002204 } finally {
2205 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002206 }
John Reck34ef2672011-02-10 11:30:55 -08002207 return null;
2208 }
2209 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002210 }
2211
2212 private class Copy implements OnMenuItemClickListener {
2213 private CharSequence mText;
2214
John Reck9c35b9c2012-05-30 10:08:50 -07002215 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002216 public boolean onMenuItemClick(MenuItem item) {
2217 copy(mText);
2218 return true;
2219 }
2220
2221 public Copy(CharSequence toCopy) {
2222 mText = toCopy;
2223 }
2224 }
2225
Leon Scroggins63c02662010-11-18 15:16:27 -05002226 private static class Download implements OnMenuItemClickListener {
2227 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002228 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002229 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002230 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002231 private static final String FALLBACK_EXTENSION = "dat";
2232 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002233
John Reck9c35b9c2012-05-30 10:08:50 -07002234 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002235 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002236 if (DataUri.isDataUri(mText)) {
2237 saveDataUri();
2238 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002239 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
luxiaol62677b02013-07-22 07:54:49 +08002240 null, null, null, mPrivateBrowsing, 0);
George Mount387d45d2011-10-07 15:57:53 -07002241 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002242 return true;
2243 }
2244
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002245 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2246 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002247 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002248 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002249 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002250 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002251 }
George Mount387d45d2011-10-07 15:57:53 -07002252
2253 /**
2254 * Treats mText as a data URI and writes its contents to a file
2255 * based on the current time.
2256 */
2257 private void saveDataUri() {
2258 FileOutputStream outputStream = null;
2259 try {
2260 DataUri uri = new DataUri(mText);
2261 File target = getTarget(uri);
2262 outputStream = new FileOutputStream(target);
2263 outputStream.write(uri.getData());
2264 final DownloadManager manager =
2265 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2266 manager.addCompletedDownload(target.getName(),
2267 mActivity.getTitle().toString(), false,
2268 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002269 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002270 } catch (IOException e) {
2271 Log.e(LOGTAG, "Could not save data URL");
2272 } finally {
2273 if (outputStream != null) {
2274 try {
2275 outputStream.close();
2276 } catch (IOException e) {
2277 // ignore close errors
2278 }
2279 }
2280 }
2281 }
2282
2283 /**
2284 * Creates a File based on the current time stamp and uses
2285 * the mime type of the DataUri to get the extension.
2286 */
2287 private File getTarget(DataUri uri) throws IOException {
2288 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002289 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002290 String nameBase = format.format(new Date());
2291 String mimeType = uri.getMimeType();
2292 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2293 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2294 if (extension == null) {
2295 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2296 extension = FALLBACK_EXTENSION;
2297 }
2298 extension = "." + extension; // createTempFile needs the '.'
2299 File targetFile = File.createTempFile(nameBase, extension, dir);
2300 return targetFile;
2301 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002302 }
2303
Cary Clark8974d282010-11-22 10:46:05 -05002304 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002305 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002306
John Reck9c35b9c2012-05-30 10:08:50 -07002307 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002308 public boolean onMenuItemClick(MenuItem item) {
2309 if (mWebView != null) {
2310 return mWebView.selectText();
2311 }
2312 return false;
2313 }
2314
2315 public SelectText(WebView webView) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002316 if (BrowserWebView.isClassic()) {
2317 mWebView = WebViewClassic.fromWebView(webView);
2318 }
Cary Clark8974d282010-11-22 10:46:05 -05002319 }
2320
2321 }
2322
Michael Kolb8233fac2010-10-26 16:08:53 -07002323 /********************** TODO: UI stuff *****************************/
2324
2325 // these methods have been copied, they still need to be cleaned up
2326
2327 /****************** tabs ***************************************************/
2328
2329 // basic tab interactions:
2330
2331 // it is assumed that tabcontrol already knows about the tab
2332 protected void addTab(Tab tab) {
2333 mUi.addTab(tab);
2334 }
2335
2336 protected void removeTab(Tab tab) {
2337 mUi.removeTab(tab);
2338 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002339 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002340 }
2341
Michael Kolbf2055602011-04-09 17:20:03 -07002342 @Override
2343 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002344 // monkey protection against delayed start
2345 if (tab != null) {
2346 mTabControl.setCurrentTab(tab);
2347 // the tab is guaranteed to have a webview after setCurrentTab
2348 mUi.setActiveTab(tab);
2349 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002350 }
2351
John Reck8bcafc12011-08-29 16:43:02 -07002352 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002353 Tab current = mTabControl.getCurrentTab();
2354 if (current != null
2355 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002356 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002357 }
2358 }
2359
John Reck26b18322011-06-21 13:08:58 -07002360 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002361 // Dismiss the subwindow if applicable.
2362 dismissSubWindow(appTab);
2363 // Since we might kill the WebView, remove it from the
2364 // content view first.
2365 mUi.detachTab(appTab);
2366 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002367 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002368 // TODO: analyze why the remove and add are necessary
2369 mUi.attachTab(appTab);
2370 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002371 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002372 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002373 } else {
2374 // If the tab was the current tab, we have to attach
2375 // it to the view system again.
2376 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002377 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002378 }
2379 }
2380
2381 // Remove the sub window if it exists. Also called by TabControl when the
2382 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002383 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002384 public void dismissSubWindow(Tab tab) {
2385 removeSubWindow(tab);
2386 // dismiss the subwindow. This will destroy the WebView.
2387 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002388 WebView wv = getCurrentTopWebView();
2389 if (wv != null) {
2390 wv.requestFocus();
2391 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002392 }
2393
2394 @Override
2395 public void removeSubWindow(Tab t) {
2396 if (t.getSubWebView() != null) {
2397 mUi.removeSubWindow(t.getSubViewContainer());
2398 }
2399 }
2400
2401 @Override
2402 public void attachSubWindow(Tab tab) {
2403 if (tab.getSubWebView() != null) {
2404 mUi.attachSubWindow(tab.getSubViewContainer());
2405 getCurrentTopWebView().requestFocus();
2406 }
2407 }
2408
Mathew Inwood29721c22011-06-29 17:55:24 +01002409 private Tab showPreloadedTab(final UrlData urlData) {
2410 if (!urlData.isPreloaded()) {
2411 return null;
2412 }
2413 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2414 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2415 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002416 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002417 // Could not submit query. Fallback to regular tab creation
2418 tabControl.destroy();
2419 return null;
2420 }
2421 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002422 // check tab count and make room for new tab
2423 if (!mTabControl.canCreateNewTab()) {
2424 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2425 if (leastUsed != null) {
2426 closeTab(leastUsed);
2427 }
2428 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002429 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002430 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002431 mTabControl.addPreloadedTab(t);
2432 addTab(t);
2433 setActiveTab(t);
2434 return t;
2435 }
2436
Michael Kolb7bcafde2011-05-09 13:55:59 -07002437 // open a non inconito tab with the given url data
2438 // and set as active tab
2439 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002440 Tab tab = showPreloadedTab(urlData);
2441 if (tab == null) {
2442 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002443 if ((tab != null) && !urlData.isEmpty()) {
2444 loadUrlDataIn(tab, urlData);
2445 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002446 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002447 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002448 }
2449
Michael Kolb843510f2010-12-09 10:51:49 -08002450 @Override
2451 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002452 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002453 }
2454
Michael Kolb8233fac2010-10-26 16:08:53 -07002455 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002456 public Tab openIncognitoTab() {
2457 return openTab(INCOGNITO_URI, true, true, false);
2458 }
2459
2460 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002461 public Tab openTab(String url, boolean incognito, boolean setActive,
2462 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002463 return openTab(url, incognito, setActive, useCurrent, null);
2464 }
2465
2466 @Override
2467 public Tab openTab(String url, Tab parent, boolean setActive,
2468 boolean useCurrent) {
2469 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2470 setActive, useCurrent, parent);
2471 }
2472
2473 public Tab openTab(String url, boolean incognito, boolean setActive,
2474 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002475 Tab tab = createNewTab(incognito, setActive, useCurrent);
2476 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002477 if (parent != null && parent != tab) {
2478 parent.addChildTab(tab);
2479 }
Michael Kolb519d2282011-05-09 17:03:19 -07002480 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002481 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002482 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002483 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002484 return tab;
2485 }
2486
2487 // this method will attempt to create a new tab
2488 // incognito: private browsing tab
2489 // setActive: ste tab as current tab
2490 // useCurrent: if no new tab can be created, return current tab
2491 private Tab createNewTab(boolean incognito, boolean setActive,
2492 boolean useCurrent) {
2493 Tab tab = null;
2494 if (mTabControl.canCreateNewTab()) {
2495 tab = mTabControl.createNewTab(incognito);
2496 addTab(tab);
2497 if (setActive) {
2498 setActiveTab(tab);
2499 }
2500 } else {
2501 if (useCurrent) {
2502 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002503 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002504 } else {
2505 mUi.showMaxTabsWarning();
2506 }
2507 }
2508 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002509 }
2510
John Reck2bc80422011-06-30 15:11:49 -07002511 @Override
2512 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2513 SnapshotTab tab = null;
2514 if (mTabControl.canCreateNewTab()) {
2515 tab = mTabControl.createSnapshotTab(snapshotId);
2516 addTab(tab);
2517 if (setActive) {
2518 setActiveTab(tab);
2519 }
2520 } else {
2521 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002522 }
2523 return tab;
2524 }
2525
Michael Kolb8233fac2010-10-26 16:08:53 -07002526 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002527 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002528 * @return boolean True if we successfully switched to a different tab. If
2529 * the indexth tab is null, or if that tab is the same as
2530 * the current one, return false.
2531 */
2532 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002533 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002534 Tab currentTab = mTabControl.getCurrentTab();
2535 if (tab == null || tab == currentTab) {
2536 return false;
2537 }
2538 setActiveTab(tab);
2539 return true;
2540 }
2541
2542 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002543 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002544 closeCurrentTab(false);
2545 }
2546
2547 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002548 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002549 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002550 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002551 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002552 return;
2553 }
Michael Kolbc831b632011-05-11 09:30:34 -07002554 final Tab current = mTabControl.getCurrentTab();
2555 final int pos = mTabControl.getCurrentPosition();
2556 Tab newTab = current.getParent();
2557 if (newTab == null) {
2558 newTab = mTabControl.getTab(pos + 1);
2559 if (newTab == null) {
2560 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002561 }
2562 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002563 if (andQuit) {
2564 mTabControl.setCurrentTab(newTab);
2565 closeTab(current);
2566 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002567 // Close window
2568 closeTab(current);
2569 }
2570 }
2571
2572 /**
2573 * Close the tab, remove its associated title bar, and adjust mTabControl's
2574 * current tab to a valid value.
2575 */
2576 @Override
2577 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002578 if (tab == mTabControl.getCurrentTab()) {
2579 closeCurrentTab();
2580 } else {
2581 removeTab(tab);
2582 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002583 }
2584
Afzal Najamd4e33312012-04-26 01:54:01 -04002585 /**
2586 * Close all tabs except the current one
2587 */
2588 @Override
2589 public void closeOtherTabs() {
2590 int inactiveTabs = mTabControl.getTabCount() - 1;
2591 for (int i = inactiveTabs; i >= 0; i--) {
2592 Tab tab = mTabControl.getTab(i);
2593 if (tab != mTabControl.getCurrentTab()) {
2594 removeTab(tab);
2595 }
2596 }
2597 }
2598
Michael Kolb8233fac2010-10-26 16:08:53 -07002599 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002600 protected void loadUrlFromContext(String url) {
2601 Tab tab = getCurrentTab();
2602 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002603 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002604 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002605 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002606 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002607 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002608 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002609 }
2610 }
2611 }
2612
2613 /**
2614 * Load the URL into the given WebView and update the title bar
2615 * to reflect the new load. Call this instead of WebView.loadUrl
2616 * directly.
2617 * @param view The WebView used to load url.
2618 * @param url The URL to load.
2619 */
John Reck71e51422011-07-01 16:49:28 -07002620 @Override
2621 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002622 loadUrl(tab, url, null);
2623 }
2624
2625 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2626 if (tab != null) {
2627 dismissSubWindow(tab);
2628 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002629 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002630 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002631 }
2632
2633 /**
2634 * Load UrlData into a Tab and update the title bar to reflect the new
2635 * load. Call this instead of UrlData.loadIn directly.
2636 * @param t The Tab used to load.
2637 * @param data The UrlData being loaded.
2638 */
2639 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002640 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002641 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002642 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002643 } else {
John Reck38b39652012-06-05 09:22:59 -07002644 if (t != null && data.mDisableUrlOverride) {
2645 t.disableUrlOverridingForLoad();
2646 }
John Reck26b18322011-06-21 13:08:58 -07002647 loadUrl(t, data.mUrl, data.mHeaders);
2648 }
2649 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002650 }
2651
John Reck30c714c2010-12-16 17:30:34 -08002652 @Override
2653 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002654 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002655 if (tab.canGoBack()) {
2656 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002657 } else {
John Reckef654f12011-07-12 16:42:08 -07002658 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002659 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002660 }
2661
2662 void goBackOnePageOrQuit() {
2663 Tab current = mTabControl.getCurrentTab();
2664 if (current == null) {
2665 /*
2666 * Instead of finishing the activity, simply push this to the back
2667 * of the stack and let ActivityManager to choose the foreground
2668 * activity. As BrowserActivity is singleTask, it will be always the
2669 * root of the task. So we can use either true or false for
2670 * moveTaskToBack().
2671 */
luxiaolb40014b2013-07-19 10:01:43 +08002672 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07002673 return;
2674 }
John Reckef654f12011-07-12 16:42:08 -07002675 if (current.canGoBack()) {
2676 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002677 } else {
2678 // Check to see if we are closing a window that was created by
2679 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002680 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002681 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002682 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002683 // Now we close the other tab
2684 closeTab(current);
2685 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002686 /*
2687 * Instead of finishing the activity, simply push this to the back
2688 * of the stack and let ActivityManager to choose the foreground
2689 * activity. As BrowserActivity is singleTask, it will be always the
2690 * root of the task. So we can use either true or false for
2691 * moveTaskToBack().
2692 */
luxiaolb40014b2013-07-19 10:01:43 +08002693 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07002694 }
2695 }
2696 }
2697
2698 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002699 * helper method for key handler
2700 * returns the current tab if it can't advance
2701 */
Michael Kolbc831b632011-05-11 09:30:34 -07002702 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002703 int pos = mTabControl.getCurrentPosition() + 1;
2704 if (pos >= mTabControl.getTabCount()) {
2705 pos = 0;
2706 }
2707 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002708 }
2709
2710 /**
2711 * helper method for key handler
2712 * returns the current tab if it can't advance
2713 */
Michael Kolbc831b632011-05-11 09:30:34 -07002714 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002715 int pos = mTabControl.getCurrentPosition() - 1;
2716 if ( pos < 0) {
2717 pos = mTabControl.getTabCount() - 1;
2718 }
2719 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002720 }
2721
John Reckbcef87f2012-02-03 14:58:34 -08002722 boolean isMenuOrCtrlKey(int keyCode) {
2723 return (KeyEvent.KEYCODE_MENU == keyCode)
2724 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2725 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2726 }
2727
Michael Kolb0035fad2011-03-14 13:25:28 -07002728 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002729 * handle key events in browser
2730 *
2731 * @param keyCode
2732 * @param event
2733 * @return true if handled, false to pass to super
2734 */
John Reck9c35b9c2012-05-30 10:08:50 -07002735 @Override
2736 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002737 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002738 // Even if MENU is already held down, we need to call to super to open
2739 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002740 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002741 mMenuIsDown = true;
2742 return false;
2743 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002744
Cary Clark8ff8c662010-12-29 15:03:05 -05002745 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002746 Tab tab = getCurrentTab();
2747 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002748
Cary Clark160bbb92011-01-10 11:17:07 -05002749 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2750 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002751
Michael Kolb8233fac2010-10-26 16:08:53 -07002752 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002753 case KeyEvent.KEYCODE_TAB:
2754 if (event.isCtrlPressed()) {
2755 if (event.isShiftPressed()) {
2756 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002757 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002758 } else {
2759 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002760 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002761 }
2762 return true;
2763 }
2764 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002765 case KeyEvent.KEYCODE_SPACE:
2766 // WebView/WebTextView handle the keys in the KeyDown. As
2767 // the Activity's shortcut keys are only handled when WebView
2768 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002769 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002770 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002771 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002772 pageDown();
2773 }
2774 return true;
2775 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002776 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002777 event.startTracking();
2778 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002779 case KeyEvent.KEYCODE_FORWARD:
2780 if (!noModifiers) break;
2781 tab.goForward();
2782 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002783 case KeyEvent.KEYCODE_DPAD_LEFT:
2784 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002785 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002786 return true;
2787 }
2788 break;
2789 case KeyEvent.KEYCODE_DPAD_RIGHT:
2790 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002791 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002792 return true;
2793 }
2794 break;
2795 case KeyEvent.KEYCODE_A:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002796 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002797 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002798 return true;
2799 }
2800 break;
Michael Kolba4183062011-01-16 10:43:21 -08002801// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002802 case KeyEvent.KEYCODE_C:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002803 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002804 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002805 return true;
2806 }
2807 break;
Michael Kolba4183062011-01-16 10:43:21 -08002808// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002809// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002810// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002811// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002812// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002813// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002814// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002815// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002816// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002817// case KeyEvent.KEYCODE_M: // unused
2818// case KeyEvent.KEYCODE_N: // in Chrome: new window
2819// case KeyEvent.KEYCODE_O: // in Chrome: open file
2820// case KeyEvent.KEYCODE_P: // in Chrome: print page
2821// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002822// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002823// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2824 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002825 // we can't use the ctrl/shift flags, they check for
2826 // exclusive use of a modifier
2827 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002828 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002829 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002830 } else {
2831 openTabToHomePage();
2832 }
2833 return true;
2834 }
2835 break;
2836// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2837// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002838// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002839// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2840// case KeyEvent.KEYCODE_Y: // unused
2841// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002842 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002843 // it is a regular key and webview is not null
2844 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002845 }
2846
John Reck9c35b9c2012-05-30 10:08:50 -07002847 @Override
2848 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002849 switch(keyCode) {
2850 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002851 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002852 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002853 return true;
2854 }
2855 break;
2856 }
2857 return false;
2858 }
2859
John Reck9c35b9c2012-05-30 10:08:50 -07002860 @Override
2861 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002862 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002863 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002864 if (KeyEvent.KEYCODE_MENU == keyCode
2865 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002866 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002867 }
2868 }
Cary Clark160bbb92011-01-10 11:17:07 -05002869 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002870 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002871 case KeyEvent.KEYCODE_BACK:
2872 if (event.isTracking() && !event.isCanceled()) {
2873 onBackKey();
2874 return true;
2875 }
2876 break;
2877 }
2878 return false;
2879 }
2880
2881 public boolean isMenuDown() {
2882 return mMenuIsDown;
2883 }
2884
John Reck9c35b9c2012-05-30 10:08:50 -07002885 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002886 public void setupAutoFill(Message message) {
2887 // Open the settings activity at the AutoFill profile fragment so that
2888 // the user can create a new profile. When they return, we will dispatch
2889 // the message so that we can autofill the form using their new profile.
2890 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2891 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2892 AutoFillSettingsFragment.class.getName());
2893 mAutoFillSetupMessage = message;
2894 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2895 }
John Reckb3417f02011-01-14 11:01:05 -08002896
John Reck9c35b9c2012-05-30 10:08:50 -07002897 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002898 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002899 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002900 return true;
2901 }
2902
John Reck1cf4b792011-07-26 10:22:22 -07002903 @Override
2904 public boolean shouldCaptureThumbnails() {
2905 return mUi.shouldCaptureThumbnails();
2906 }
2907
Michael Kolbc3af0672011-08-09 10:24:41 -07002908 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07002909 public boolean supportsVoice() {
2910 PackageManager pm = mActivity.getPackageManager();
2911 List activities = pm.queryIntentActivities(new Intent(
2912 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
2913 return activities.size() != 0;
2914 }
2915
2916 @Override
2917 public void startVoiceRecognizer() {
2918 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
2919 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
2920 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
2921 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
2922 mActivity.startActivityForResult(voice, VOICE_RESULT);
2923 }
2924
2925 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002926 public void setBlockEvents(boolean block) {
2927 mBlockEvents = block;
2928 }
2929
John Reck9c35b9c2012-05-30 10:08:50 -07002930 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002931 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002932 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002933 }
2934
John Reck9c35b9c2012-05-30 10:08:50 -07002935 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002936 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002937 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002938 }
2939
John Reck9c35b9c2012-05-30 10:08:50 -07002940 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002941 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002942 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002943 }
2944
John Reck9c35b9c2012-05-30 10:08:50 -07002945 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002946 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002947 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002948 }
2949
John Reck9c35b9c2012-05-30 10:08:50 -07002950 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002951 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002952 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002953 }
2954
Michael Kolb8233fac2010-10-26 16:08:53 -07002955}