blob: 8e118a1d02c4270f9a8dda36a88319176efb53b7 [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(),
Jean-Baptiste Queru2584c312012-09-04 14:39:07 -0700521 null, null, null, view.isPrivateBrowsingEnabled());
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;
kaiyiza8b6dbb2013-07-29 18:11:22 +0800785 mCrashRecoveryHandler.clearState();
Michael Kolb8233fac2010-10-26 16:08:53 -0700786 }
787
788 protected boolean isActivityPaused() {
789 return mActivityPaused;
790 }
791
John Reck9c35b9c2012-05-30 10:08:50 -0700792 @Override
793 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700794 mTabControl.freeMemory();
795 }
796
797 @Override
798 public boolean shouldShowErrorConsole() {
799 return mShouldShowErrorConsole;
800 }
801
802 protected void setShouldShowErrorConsole(boolean show) {
803 if (show == mShouldShowErrorConsole) {
804 // Nothing to do.
805 return;
806 }
807 mShouldShowErrorConsole = show;
808 Tab t = mTabControl.getCurrentTab();
809 if (t == null) {
810 // There is no current tab so we cannot toggle the error console
811 return;
812 }
813 mUi.setShouldShowErrorConsole(t, show);
814 }
815
816 @Override
817 public void stopLoading() {
818 mLoadStopped = true;
819 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700820 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700821 if (w != null) {
822 w.stopLoading();
823 mUi.onPageStopped(tab);
824 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 }
826
827 boolean didUserStopLoading() {
828 return mLoadStopped;
829 }
830
831 // WebViewController
832
833 @Override
John Reck324d4402011-01-11 16:56:42 -0800834 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700835
836 // We've started to load a new page. If there was a pending message
837 // to save a screenshot then we will now take the new page and save
838 // an incorrect screenshot. Therefore, remove any pending thumbnail
839 // messages from the queue.
840 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800841 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700842
843 // reset sync timer to avoid sync starts during loading a page
844 CookieSyncManager.getInstance().resetSync();
845
846 if (!mNetworkHandler.isNetworkUp()) {
847 view.setNetworkAvailable(false);
848 }
849
850 // when BrowserActivity just starts, onPageStarted may be called before
851 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
852 // to start the timer. As we won't switch tabs while an activity is in
853 // pause state, we can ensure calling resume and pause in pair.
854 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800855 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700856 }
857 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700858 endActionMode();
859
John Reck30c714c2010-12-16 17:30:34 -0800860 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700861
John Reck324d4402011-01-11 16:56:42 -0800862 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 // update the bookmark database for favicon
864 maybeUpdateFavicon(tab, null, url, favicon);
865
866 Performance.tracePageStart(url);
867
868 // Performance probe
869 if (false) {
870 Performance.onPageStarted();
871 }
872
873 }
874
875 @Override
John Reck324d4402011-01-11 16:56:42 -0800876 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700877 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800878 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200879
Michael Kolb8233fac2010-10-26 16:08:53 -0700880 // Performance probe
881 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800882 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 }
884
885 Performance.tracePageFinished();
886 }
887
888 @Override
John Reck30c714c2010-12-16 17:30:34 -0800889 public void onProgressChanged(Tab tab) {
890 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700891
892 if (newProgress == 100) {
893 CookieSyncManager.getInstance().sync();
894 // onProgressChanged() may continue to be called after the main
895 // frame has finished loading, as any remaining sub frames continue
896 // to load. We'll only get called once though with newProgress as
897 // 100 when everything is loaded. (onPageFinished is called once
898 // when the main frame completes loading regardless of the state of
899 // any sub frames so calls to onProgressChanges may continue after
900 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800901 if (tab.inPageLoad()) {
902 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200903 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
904 // pause the WebView timer and release the wake lock if it is
905 // finished while BrowserActivity is in pause state.
906 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700907 }
John Reckd9862372012-02-21 15:04:50 -0800908 if (!tab.isPrivateBrowsingEnabled()
909 && !TextUtils.isEmpty(tab.getUrl())
910 && !tab.isSnapshot()) {
911 // Only update the bookmark screenshot if the user did not
912 // cancel the load early and there is not already
913 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700914 if (tab.shouldUpdateThumbnail() &&
915 (tab.inForeground() && !didUserStopLoading()
916 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800917 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
918 mHandler.sendMessageDelayed(mHandler.obtainMessage(
919 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
920 500);
921 }
922 }
923 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700924 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800925 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700926 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800927 // still loading
928 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -0700929 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800930 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700931 }
932 }
John Reck30c714c2010-12-16 17:30:34 -0800933 mUi.onProgressChanged(tab);
934 }
935
936 @Override
Steve Block2466eff2011-10-03 15:33:09 +0100937 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800938 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700939 }
940
941 @Override
942 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800943 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800944 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800945 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700946 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
947 return;
948 }
949 // Update the title in the history database if not in private browsing mode
950 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -0700951 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700952 }
953 }
954
955 @Override
956 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800957 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700958 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
959 }
960
961 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800962 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
963 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 }
965
966 @Override
967 public boolean shouldOverrideKeyEvent(KeyEvent event) {
968 if (mMenuIsDown) {
969 // only check shortcut key when MENU is held
970 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
971 event);
972 } else {
973 return false;
974 }
975 }
976
977 @Override
John Reck997b1b72012-04-19 18:08:25 -0700978 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700979 if (!isActivityPaused()) {
980 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -0700981 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700982 } else {
John Reck997b1b72012-04-19 18:08:25 -0700983 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700984 }
985 }
John Reck997b1b72012-04-19 18:08:25 -0700986 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700987 }
988
989 @Override
John Reck324d4402011-01-11 16:56:42 -0800990 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700991 // Don't save anything in private browsing mode
992 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800993 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700994
John Reck324d4402011-01-11 16:56:42 -0800995 if (TextUtils.isEmpty(url)
996 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700997 return;
998 }
John Reckf57c0292011-07-21 18:15:39 -0700999 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -07001000 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07001001 }
1002
1003 @Override
1004 public void getVisitedHistory(final ValueCallback<String[]> callback) {
1005 AsyncTask<Void, Void, String[]> task =
1006 new AsyncTask<Void, Void, String[]>() {
1007 @Override
1008 public String[] doInBackground(Void... unused) {
1009 return Browser.getVisitedHistory(mActivity.getContentResolver());
1010 }
1011 @Override
1012 public void onPostExecute(String[] result) {
1013 callback.onReceiveValue(result);
1014 }
1015 };
1016 task.execute();
1017 }
1018
1019 @Override
1020 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1021 final HttpAuthHandler handler, final String host,
1022 final String realm) {
1023 String username = null;
1024 String password = null;
1025
1026 boolean reuseHttpAuthUsernamePassword
1027 = handler.useHttpAuthUsernamePassword();
1028
1029 if (reuseHttpAuthUsernamePassword && view != null) {
1030 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1031 if (credentials != null && credentials.length == 2) {
1032 username = credentials[0];
1033 password = credentials[1];
1034 }
1035 }
1036
1037 if (username != null && password != null) {
1038 handler.proceed(username, password);
1039 } else {
Ben Murdochfdd37112011-08-05 15:48:14 +01001040 if (tab.inForeground() && !handler.suppressDialog()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001041 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1042 } else {
1043 handler.cancel();
1044 }
1045 }
1046 }
1047
1048 @Override
1049 public void onDownloadStart(Tab tab, String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001050 String contentDisposition, String mimetype, String referer,
1051 long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001052 WebView w = tab.getWebView();
qqzhoua95a2e22013-04-18 17:28:31 +08001053 boolean ret = DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001054 contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled());
qqzhoua95a2e22013-04-18 17:28:31 +08001055 if (ret == false && w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001056 // This Tab was opened for the sole purpose of downloading a
1057 // file. Remove it.
1058 if (tab == mTabControl.getCurrentTab()) {
1059 // In this case, the Tab is still on top.
1060 goBackOnePageOrQuit();
1061 } else {
1062 // In this case, it is not.
1063 closeTab(tab);
1064 }
1065 }
1066 }
1067
1068 @Override
1069 public Bitmap getDefaultVideoPoster() {
1070 return mUi.getDefaultVideoPoster();
1071 }
1072
1073 @Override
1074 public View getVideoLoadingProgressView() {
1075 return mUi.getVideoLoadingProgressView();
1076 }
1077
1078 @Override
1079 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1080 SslError error) {
1081 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1082 }
1083
Patrick Scott92066772011-03-10 08:46:27 -05001084 @Override
1085 public void showAutoLogin(Tab tab) {
1086 assert tab.inForeground();
1087 // Update the title bar to show the auto-login request.
1088 mUi.showAutoLogin(tab);
1089 }
1090
1091 @Override
1092 public void hideAutoLogin(Tab tab) {
1093 assert tab.inForeground();
1094 mUi.hideAutoLogin(tab);
1095 }
1096
Michael Kolb8233fac2010-10-26 16:08:53 -07001097 // helper method
1098
1099 /*
1100 * Update the favorites icon if the private browsing isn't enabled and the
1101 * icon is valid.
1102 */
1103 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1104 final String url, Bitmap favicon) {
1105 if (favicon == null) {
1106 return;
1107 }
1108 if (!tab.isPrivateBrowsingEnabled()) {
1109 Bookmarks.updateFavicon(mActivity
1110 .getContentResolver(), originalUrl, url, favicon);
1111 }
1112 }
1113
Leon Scroggins4cd97792010-12-03 15:31:56 -05001114 @Override
1115 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001116 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001117 mUi.bookmarkedStatusHasChanged(tab);
1118 }
1119
Michael Kolb8233fac2010-10-26 16:08:53 -07001120 // end WebViewController
1121
1122 protected void pageUp() {
1123 getCurrentTopWebView().pageUp(false);
1124 }
1125
1126 protected void pageDown() {
1127 getCurrentTopWebView().pageDown(false);
1128 }
1129
1130 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001131 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001132 public void editUrl() {
1133 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001134 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001135 }
1136
John Reck9c35b9c2012-05-30 10:08:50 -07001137 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001138 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001139 WebChromeClient.CustomViewCallback callback) {
1140 if (tab.inForeground()) {
1141 if (mUi.isCustomViewShowing()) {
1142 callback.onCustomViewHidden();
1143 return;
1144 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001145 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001146 // Save the menu state and set it to empty while the custom
1147 // view is showing.
1148 mOldMenuState = mMenuState;
1149 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001150 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001151 }
1152 }
1153
1154 @Override
1155 public void hideCustomView() {
1156 if (mUi.isCustomViewShowing()) {
1157 mUi.onHideCustomView();
1158 // Reset the old menu state.
1159 mMenuState = mOldMenuState;
1160 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001161 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001162 }
1163 }
1164
John Reck9c35b9c2012-05-30 10:08:50 -07001165 @Override
1166 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001167 Intent intent) {
1168 if (getCurrentTopWebView() == null) return;
1169 switch (requestCode) {
1170 case PREFERENCES_PAGE:
1171 if (resultCode == Activity.RESULT_OK && intent != null) {
1172 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001173 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001174 mTabControl.removeParentChildRelationShips();
1175 }
1176 }
1177 break;
1178 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001179 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001180 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001181 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001182 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001183 case AUTOFILL_SETUP:
1184 // Determine whether a profile was actually set up or not
1185 // and if so, send the message back to the WebTextView to
1186 // fill the form with the new profile.
1187 if (getSettings().getAutoFillProfile() != null) {
1188 mAutoFillSetupMessage.sendToTarget();
1189 mAutoFillSetupMessage = null;
1190 }
1191 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001192 case COMBO_VIEW:
1193 if (intent == null || resultCode != Activity.RESULT_OK) {
1194 break;
1195 }
John Reck3ba45532011-08-11 16:26:53 -07001196 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001197 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1198 Tab t = getCurrentTab();
1199 Uri uri = intent.getData();
1200 loadUrl(t, uri.toString());
1201 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1202 String[] urls = intent.getStringArrayExtra(
1203 ComboViewActivity.EXTRA_OPEN_ALL);
1204 Tab parent = getCurrentTab();
1205 for (String url : urls) {
1206 parent = openTab(url, parent,
1207 !mSettings.openInBackground(), true);
1208 }
1209 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1210 long id = intent.getLongExtra(
1211 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1212 if (id >= 0) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08001213 if (BrowserWebView.isClassic()) {
1214 createNewSnapshotTab(id, true);
1215 } else {
1216 Toast.makeText(mActivity, "Snapshot Tab requires WebViewClassic",
1217 Toast.LENGTH_LONG).show();
1218 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001219 }
1220 }
1221 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001222 case VOICE_RESULT:
1223 if (resultCode == Activity.RESULT_OK && intent != null) {
1224 ArrayList<String> results = intent.getStringArrayListExtra(
1225 RecognizerIntent.EXTRA_RESULTS);
1226 if (results.size() >= 1) {
1227 mVoiceResult = results.get(0);
1228 }
1229 }
1230 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001231 default:
1232 break;
1233 }
1234 getCurrentTopWebView().requestFocus();
1235 }
1236
1237 /**
1238 * Open the Go page.
1239 * @param startWithHistory If true, open starting on the history tab.
1240 * Otherwise, start with the bookmarks tab.
1241 */
1242 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001243 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001244 if (mTabControl.getCurrentWebView() == null) {
1245 return;
1246 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001247 // clear action mode
1248 if (isInCustomActionMode()) {
1249 endActionMode();
1250 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001251 Bundle extras = new Bundle();
1252 // Disable opening in a new window if we have maxed out the windows
1253 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1254 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001255 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001256 }
1257
1258 // combo view callbacks
1259
Michael Kolb8233fac2010-10-26 16:08:53 -07001260 // key handling
1261 protected void onBackKey() {
1262 if (!mUi.onBackKey()) {
1263 WebView subwindow = mTabControl.getCurrentSubWindow();
1264 if (subwindow != null) {
1265 if (subwindow.canGoBack()) {
1266 subwindow.goBack();
1267 } else {
1268 dismissSubWindow(mTabControl.getCurrentTab());
1269 }
1270 } else {
1271 goBackOnePageOrQuit();
1272 }
1273 }
1274 }
1275
Michael Kolb4bd767d2011-05-27 11:33:55 -07001276 protected boolean onMenuKey() {
1277 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001278 }
1279
Michael Kolb8233fac2010-10-26 16:08:53 -07001280 // menu handling and state
1281 // TODO: maybe put into separate handler
1282
John Reck9c35b9c2012-05-30 10:08:50 -07001283 @Override
1284 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001285 if (mMenuState == EMPTY_MENU) {
1286 return false;
1287 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001288 MenuInflater inflater = mActivity.getMenuInflater();
1289 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001290 return true;
1291 }
1292
John Reck9c35b9c2012-05-30 10:08:50 -07001293 @Override
1294 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001295 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001296 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001297 return;
1298 }
1299 if (!(v instanceof WebView)) {
1300 return;
1301 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001302 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001303 WebView.HitTestResult result = webview.getHitTestResult();
1304 if (result == null) {
1305 return;
1306 }
1307
1308 int type = result.getType();
1309 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1310 Log.w(LOGTAG,
1311 "We should not show context menu when nothing is touched");
1312 return;
1313 }
1314 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1315 // let TextView handles context menu
1316 return;
1317 }
1318
1319 // Note, http://b/issue?id=1106666 is requesting that
1320 // an inflated menu can be used again. This is not available
1321 // yet, so inflate each time (yuk!)
1322 MenuInflater inflater = mActivity.getMenuInflater();
1323 inflater.inflate(R.menu.browsercontext, menu);
1324
1325 // Show the correct menu group
1326 final String extra = result.getExtra();
Michael Kolbc159c1a2011-12-15 16:06:38 -08001327 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001328 menu.setGroupVisible(R.id.PHONE_MENU,
1329 type == WebView.HitTestResult.PHONE_TYPE);
1330 menu.setGroupVisible(R.id.EMAIL_MENU,
1331 type == WebView.HitTestResult.EMAIL_TYPE);
1332 menu.setGroupVisible(R.id.GEO_MENU,
1333 type == WebView.HitTestResult.GEO_TYPE);
1334 menu.setGroupVisible(R.id.IMAGE_MENU,
1335 type == WebView.HitTestResult.IMAGE_TYPE
1336 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1337 menu.setGroupVisible(R.id.ANCHOR_MENU,
1338 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1339 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001340 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1341 || type == WebView.HitTestResult.PHONE_TYPE
1342 || type == WebView.HitTestResult.EMAIL_TYPE
1343 || type == WebView.HitTestResult.GEO_TYPE;
1344 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1345 if (hitText) {
1346 menu.findItem(R.id.select_text_menu_id)
1347 .setOnMenuItemClickListener(new SelectText(webview));
1348 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001349 // Setup custom handling depending on the type
1350 switch (type) {
1351 case WebView.HitTestResult.PHONE_TYPE:
1352 menu.setHeaderTitle(Uri.decode(extra));
1353 menu.findItem(R.id.dial_context_menu_id).setIntent(
1354 new Intent(Intent.ACTION_VIEW, Uri
1355 .parse(WebView.SCHEME_TEL + extra)));
1356 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1357 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1358 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1359 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1360 addIntent);
1361 menu.findItem(R.id.copy_phone_context_menu_id)
1362 .setOnMenuItemClickListener(
1363 new Copy(extra));
1364 break;
1365
1366 case WebView.HitTestResult.EMAIL_TYPE:
1367 menu.setHeaderTitle(extra);
1368 menu.findItem(R.id.email_context_menu_id).setIntent(
1369 new Intent(Intent.ACTION_VIEW, Uri
1370 .parse(WebView.SCHEME_MAILTO + extra)));
1371 menu.findItem(R.id.copy_mail_context_menu_id)
1372 .setOnMenuItemClickListener(
1373 new Copy(extra));
1374 break;
1375
1376 case WebView.HitTestResult.GEO_TYPE:
1377 menu.setHeaderTitle(extra);
1378 menu.findItem(R.id.map_context_menu_id).setIntent(
1379 new Intent(Intent.ACTION_VIEW, Uri
1380 .parse(WebView.SCHEME_GEO
1381 + URLEncoder.encode(extra))));
1382 menu.findItem(R.id.copy_geo_context_menu_id)
1383 .setOnMenuItemClickListener(
1384 new Copy(extra));
1385 break;
1386
1387 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1388 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001389 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001390 // decide whether to show the open link in new tab option
1391 boolean showNewTab = mTabControl.canCreateNewTab();
1392 MenuItem newTabItem
1393 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001394 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001395 ? R.string.contextmenu_openlink_newwindow_background
1396 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001397 newTabItem.setVisible(showNewTab);
1398 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001399 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1400 newTabItem.setOnMenuItemClickListener(
1401 new MenuItem.OnMenuItemClickListener() {
1402 @Override
1403 public boolean onMenuItemClick(MenuItem item) {
1404 final HashMap<String, WebView> hrefMap =
1405 new HashMap<String, WebView>();
1406 hrefMap.put("webview", webview);
1407 final Message msg = mHandler.obtainMessage(
1408 FOCUS_NODE_HREF,
1409 R.id.open_newtab_context_menu_id,
1410 0, hrefMap);
1411 webview.requestFocusNodeHref(msg);
1412 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001413 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001414 });
1415 } else {
1416 newTabItem.setOnMenuItemClickListener(
1417 new MenuItem.OnMenuItemClickListener() {
1418 @Override
1419 public boolean onMenuItemClick(MenuItem item) {
1420 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001421 openTab(extra, parent,
1422 !mSettings.openInBackground(),
1423 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001424 return true;
1425 }
1426 });
1427 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001428 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001429 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1430 break;
1431 }
1432 // otherwise fall through to handle image part
1433 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001434 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1435 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001436 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1437 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001438 shareItem.setOnMenuItemClickListener(
1439 new MenuItem.OnMenuItemClickListener() {
1440 @Override
1441 public boolean onMenuItemClick(MenuItem item) {
1442 sharePage(mActivity, null, extra, null,
1443 null);
1444 return true;
1445 }
1446 }
1447 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001448 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001449 menu.findItem(R.id.view_image_context_menu_id)
1450 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1451 @Override
1452 public boolean onMenuItemClick(MenuItem item) {
1453 openTab(extra, mTabControl.getCurrentTab(), true, true);
1454 return false;
1455 }
1456 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001457 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1458 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1459 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001460 menu.findItem(R.id.set_wallpaper_context_menu_id).
1461 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1462 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001463 break;
1464
1465 default:
1466 Log.w(LOGTAG, "We should not get here.");
1467 break;
1468 }
1469 //update the ui
1470 mUi.onContextMenuCreated(menu);
1471 }
1472
1473 /**
1474 * As the menu can be open when loading state changes
1475 * we must manually update the state of the stop/reload menu
1476 * item
1477 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001478 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001479 if (menu == null) {
1480 return;
1481 }
1482 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001483 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001484 menu.findItem(R.id.stop_menu_id):
1485 menu.findItem(R.id.reload_menu_id);
1486 if (src != null) {
1487 dest.setIcon(src.getIcon());
1488 dest.setTitle(src.getTitle());
1489 }
1490 }
1491
John Reck9c35b9c2012-05-30 10:08:50 -07001492 @Override
1493 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001494 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001495 // hold on to the menu reference here; it is used by the page callbacks
1496 // to update the menu based on loading state
1497 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001498 // Note: setVisible will decide whether an item is visible; while
1499 // setEnabled() will decide whether an item is enabled, which also means
1500 // whether the matching shortcut key will function.
1501 switch (mMenuState) {
1502 case EMPTY_MENU:
1503 if (mCurrentMenuState != mMenuState) {
1504 menu.setGroupVisible(R.id.MAIN_MENU, false);
1505 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1506 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1507 }
1508 break;
1509 default:
1510 if (mCurrentMenuState != mMenuState) {
1511 menu.setGroupVisible(R.id.MAIN_MENU, true);
1512 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1513 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1514 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001515 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001516 break;
1517 }
1518 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001519 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001520 }
1521
Michael Kolb4bf79712011-07-14 14:18:12 -07001522 @Override
1523 public void updateMenuState(Tab tab, Menu menu) {
1524 boolean canGoBack = false;
1525 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001526 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001527 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001528 if (tab != null) {
1529 canGoBack = tab.canGoBack();
1530 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001531 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001532 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001533 }
1534 final MenuItem back = menu.findItem(R.id.back_menu_id);
1535 back.setEnabled(canGoBack);
1536
1537 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001538
1539 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1540 forward.setEnabled(canGoForward);
1541
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001542 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1543 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001544 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001545 if (source != null && dest != null) {
1546 dest.setTitle(source.getTitle());
1547 dest.setIcon(source.getIcon());
1548 }
John Recke1a03a32011-09-14 17:04:16 -07001549 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001550
1551 // decide whether to show the share link option
1552 PackageManager pm = mActivity.getPackageManager();
1553 Intent send = new Intent(Intent.ACTION_SEND);
1554 send.setType("text/plain");
1555 ResolveInfo ri = pm.resolveActivity(send,
1556 PackageManager.MATCH_DEFAULT_ONLY);
1557 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1558
1559 boolean isNavDump = mSettings.enableNavDump();
1560 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1561 nav.setVisible(isNavDump);
1562 nav.setEnabled(isNavDump);
1563
1564 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001565 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1566 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001567 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1568 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001569
1570 // history and snapshots item are the members of COMBO menu group,
1571 // so if show history item, only make snapshots item invisible.
1572 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001573
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001574 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001575 }
1576
John Reck9c35b9c2012-05-30 10:08:50 -07001577 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001578 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001579 if (null == getCurrentTopWebView()) {
1580 return false;
1581 }
1582 if (mMenuIsDown) {
1583 // The shortcut action consumes the MENU. Even if it is still down,
1584 // it won't trigger the next shortcut action. In the case of the
1585 // shortcut action triggering a new activity, like Bookmarks, we
1586 // won't get onKeyUp for MENU. So it is important to reset it here.
1587 mMenuIsDown = false;
1588 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001589 if (mUi.onOptionsItemSelected(item)) {
1590 // ui callback handled it
1591 return true;
1592 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001593 switch (item.getItemId()) {
1594 // -- Main menu
1595 case R.id.new_tab_menu_id:
1596 openTabToHomePage();
1597 break;
1598
1599 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001600 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001601 break;
1602
Afzal Najamd4e33312012-04-26 01:54:01 -04001603 case R.id.close_other_tabs_id:
1604 closeOtherTabs();
1605 break;
1606
Michael Kolb8233fac2010-10-26 16:08:53 -07001607 case R.id.goto_menu_id:
1608 editUrl();
1609 break;
1610
1611 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001612 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1613 break;
1614
1615 case R.id.history_menu_id:
1616 bookmarksOrHistoryPicker(ComboViews.History);
1617 break;
1618
1619 case R.id.snapshots_menu_id:
1620 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001621 break;
1622
Michael Kolb8233fac2010-10-26 16:08:53 -07001623 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001624 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001625 break;
1626
1627 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001628 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001629 stopLoading();
1630 } else {
1631 getCurrentTopWebView().reload();
1632 }
1633 break;
1634
1635 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001636 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001637 break;
1638
1639 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001640 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001641 break;
1642
1643 case R.id.close_menu_id:
1644 // Close the subwindow if it exists.
1645 if (mTabControl.getCurrentSubWindow() != null) {
1646 dismissSubWindow(mTabControl.getCurrentTab());
1647 break;
1648 }
1649 closeCurrentTab();
1650 break;
1651
kaiyiza8b6dbb2013-07-29 18:11:22 +08001652 case R.id.exit_menu_id:
1653 String ret = SystemProperties.get("persist.debug.browsermonkeytest");
1654 if (ret != null && ret.equals("enable"))
1655 break;
luxiaolb40014b2013-07-19 10:01:43 +08001656 showExitDialog(mActivity);
1657 return true;
kaiyiza8b6dbb2013-07-29 18:11:22 +08001658
Michael Kolb8233fac2010-10-26 16:08:53 -07001659 case R.id.homepage_menu_id:
1660 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001661 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001662 break;
1663
1664 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001665 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001666 break;
1667
1668 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001669 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001670 break;
1671
John Reck2bc80422011-06-30 15:11:49 -07001672 case R.id.save_snapshot_menu_id:
1673 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001674 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001675 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001676 break;
1677
Michael Kolb8233fac2010-10-26 16:08:53 -07001678 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001679 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001680 break;
1681
John Recke1a03a32011-09-14 17:04:16 -07001682 case R.id.snapshot_go_live:
1683 goLive();
1684 return true;
1685
Michael Kolb8233fac2010-10-26 16:08:53 -07001686 case R.id.share_page_menu_id:
1687 Tab currentTab = mTabControl.getCurrentTab();
1688 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001689 return false;
1690 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001691 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001692 break;
1693
1694 case R.id.dump_nav_menu_id:
1695 getCurrentTopWebView().debugDump();
1696 break;
1697
Michael Kolb8233fac2010-10-26 16:08:53 -07001698 case R.id.zoom_in_menu_id:
1699 getCurrentTopWebView().zoomIn();
1700 break;
1701
1702 case R.id.zoom_out_menu_id:
1703 getCurrentTopWebView().zoomOut();
1704 break;
1705
1706 case R.id.view_downloads_menu_id:
1707 viewDownloads();
1708 break;
1709
John Reck42229bc2011-08-19 13:26:43 -07001710 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001711 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07001712 break;
1713
Michael Kolb8233fac2010-10-26 16:08:53 -07001714 case R.id.window_one_menu_id:
1715 case R.id.window_two_menu_id:
1716 case R.id.window_three_menu_id:
1717 case R.id.window_four_menu_id:
1718 case R.id.window_five_menu_id:
1719 case R.id.window_six_menu_id:
1720 case R.id.window_seven_menu_id:
1721 case R.id.window_eight_menu_id:
1722 {
1723 int menuid = item.getItemId();
1724 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1725 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1726 Tab desiredTab = mTabControl.getTab(id);
1727 if (desiredTab != null &&
1728 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001729 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001730 }
1731 break;
1732 }
1733 }
1734 }
1735 break;
1736
1737 default:
1738 return false;
1739 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001740 return true;
1741 }
1742
John Reck68234a92012-04-19 15:27:12 -07001743 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
1744 implements OnCancelListener {
1745
1746 private Tab mTab;
1747 private Dialog mProgressDialog;
1748 private ContentValues mValues;
1749
1750 private SaveSnapshotTask(Tab tab) {
1751 mTab = tab;
1752 }
1753
1754 @Override
1755 protected void onPreExecute() {
1756 CharSequence message = mActivity.getText(R.string.saving_snapshot);
1757 mProgressDialog = ProgressDialog.show(mActivity, null, message,
1758 true, true, this);
1759 mValues = mTab.createSnapshotValues();
1760 }
1761
1762 @Override
1763 protected Long doInBackground(Void... params) {
1764 if (!mTab.saveViewState(mValues)) {
1765 return null;
1766 }
1767 if (isCancelled()) {
1768 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
1769 File file = mActivity.getFileStreamPath(path);
1770 if (!file.delete()) {
1771 file.deleteOnExit();
1772 }
1773 return null;
1774 }
1775 final ContentResolver cr = mActivity.getContentResolver();
1776 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
1777 if (result == null) {
1778 return null;
1779 }
1780 long id = ContentUris.parseId(result);
1781 return id;
1782 }
1783
1784 @Override
1785 protected void onPostExecute(Long id) {
1786 if (isCancelled()) {
1787 return;
1788 }
1789 mProgressDialog.dismiss();
1790 if (id == null) {
1791 Toast.makeText(mActivity, R.string.snapshot_failed,
1792 Toast.LENGTH_SHORT).show();
1793 return;
1794 }
1795 Bundle b = new Bundle();
1796 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1797 mUi.showComboView(ComboViews.Snapshots, b);
1798 }
1799
1800 @Override
1801 public void onCancel(DialogInterface dialog) {
1802 cancel(true);
1803 }
1804 }
1805
Michael Kolb80f75082012-04-10 10:50:06 -07001806 @Override
1807 public void toggleUserAgent() {
1808 WebView web = getCurrentWebView();
1809 mSettings.toggleDesktopUseragent(web);
1810 web.loadUrl(web.getOriginalUrl());
1811 }
1812
1813 @Override
1814 public void findOnPage() {
1815 getCurrentTopWebView().showFindDialog(null, true);
1816 }
1817
1818 @Override
1819 public void openPreferences() {
1820 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1821 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1822 getCurrentTopWebView().getUrl());
1823 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1824 }
1825
1826 @Override
1827 public void bookmarkCurrentPage() {
1828 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
1829 if (bookmarkIntent != null) {
1830 mActivity.startActivity(bookmarkIntent);
1831 }
1832 }
1833
John Recke1a03a32011-09-14 17:04:16 -07001834 private void goLive() {
1835 Tab t = getCurrentTab();
1836 t.loadUrl(t.getUrl(), null);
1837 }
1838
luxiaolb40014b2013-07-19 10:01:43 +08001839 private void showExitDialog(final Activity activity) {
1840 new AlertDialog.Builder(activity)
1841 .setTitle(R.string.exit_browser_title)
1842 .setIcon(android.R.drawable.ic_dialog_alert)
1843 .setMessage(R.string.exit_browser_msg)
1844 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
1845 public void onClick(DialogInterface dialog, int which) {
1846 activity.moveTaskToBack(true);
1847 dialog.dismiss();
1848 }
1849 })
1850 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
1851 public void onClick(DialogInterface dialog, int which) {
1852 activity.finish();
1853 mHandler.postDelayed(new Runnable() {
1854 @Override
1855 public void run() {
1856 // TODO Auto-generated method stub
1857 mCrashRecoveryHandler.clearState(true);
1858 int pid = android.os.Process.myPid();
1859 android.os.Process.killProcess(pid);
1860 }
1861 }, 300);
1862 dialog.dismiss();
1863 }
1864 })
1865 .show();
1866 }
1867
Michael Kolb315d5022011-10-13 12:47:11 -07001868 @Override
1869 public void showPageInfo() {
1870 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
1871 }
1872
John Reck9c35b9c2012-05-30 10:08:50 -07001873 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001874 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001875 // Let the History and Bookmark fragments handle menus they created.
1876 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1877 return false;
1878 }
1879
Michael Kolb8233fac2010-10-26 16:08:53 -07001880 int id = item.getItemId();
1881 boolean result = true;
1882 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001883 // -- Browser context menu
1884 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001885 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001886 case R.id.copy_link_context_menu_id:
1887 final WebView webView = getCurrentTopWebView();
1888 if (null == webView) {
1889 result = false;
1890 break;
1891 }
1892 final HashMap<String, WebView> hrefMap =
1893 new HashMap<String, WebView>();
1894 hrefMap.put("webview", webView);
1895 final Message msg = mHandler.obtainMessage(
1896 FOCUS_NODE_HREF, id, 0, hrefMap);
1897 webView.requestFocusNodeHref(msg);
1898 break;
1899
1900 default:
1901 // For other context menus
1902 result = onOptionsItemSelected(item);
1903 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001904 return result;
1905 }
1906
1907 /**
1908 * support programmatically opening the context menu
1909 */
1910 public void openContextMenu(View view) {
1911 mActivity.openContextMenu(view);
1912 }
1913
1914 /**
1915 * programmatically open the options menu
1916 */
1917 public void openOptionsMenu() {
1918 mActivity.openOptionsMenu();
1919 }
1920
John Reck9c35b9c2012-05-30 10:08:50 -07001921 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001922 public boolean onMenuOpened(int featureId, Menu menu) {
1923 if (mOptionsMenuOpen) {
1924 if (mConfigChanged) {
1925 // We do not need to make any changes to the state of the
1926 // title bar, since the only thing that happened was a
1927 // change in orientation
1928 mConfigChanged = false;
1929 } else {
1930 if (!mExtendedMenuOpen) {
1931 mExtendedMenuOpen = true;
1932 mUi.onExtendedMenuOpened();
1933 } else {
1934 // Switching the menu back to icon view, so show the
1935 // title bar once again.
1936 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001937 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001938 }
1939 }
1940 } else {
1941 // The options menu is closed, so open it, and show the title
1942 mOptionsMenuOpen = true;
1943 mConfigChanged = false;
1944 mExtendedMenuOpen = false;
1945 mUi.onOptionsMenuOpened();
1946 }
1947 return true;
1948 }
1949
John Reck9c35b9c2012-05-30 10:08:50 -07001950 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001951 public void onOptionsMenuClosed(Menu menu) {
1952 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001953 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001954 }
1955
John Reck9c35b9c2012-05-30 10:08:50 -07001956 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001957 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001958 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001959 }
1960
1961 // Helper method for getting the top window.
1962 @Override
1963 public WebView getCurrentTopWebView() {
1964 return mTabControl.getCurrentTopWebView();
1965 }
1966
1967 @Override
1968 public WebView getCurrentWebView() {
1969 return mTabControl.getCurrentWebView();
1970 }
1971
1972 /*
1973 * This method is called as a result of the user selecting the options
1974 * menu to see the download window. It shows the download window on top of
1975 * the current window.
1976 */
1977 void viewDownloads() {
1978 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1979 mActivity.startActivity(intent);
1980 }
1981
John Reck30b065e2011-07-19 10:58:05 -07001982 int getActionModeHeight() {
1983 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1984 new int[] { android.R.attr.actionBarSize });
1985 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1986 actionBarSizeTypedArray.recycle();
1987 return size;
1988 }
1989
Michael Kolb8233fac2010-10-26 16:08:53 -07001990 // action mode
1991
John Reck9c35b9c2012-05-30 10:08:50 -07001992 @Override
1993 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001994 mUi.onActionModeStarted(mode);
1995 mActionMode = mode;
1996 }
1997
1998 /*
1999 * True if a custom ActionMode (i.e. find or select) is in use.
2000 */
2001 @Override
2002 public boolean isInCustomActionMode() {
2003 return mActionMode != null;
2004 }
2005
2006 /*
2007 * End the current ActionMode.
2008 */
2009 @Override
2010 public void endActionMode() {
2011 if (mActionMode != null) {
2012 mActionMode.finish();
2013 }
2014 }
2015
2016 /*
2017 * Called by find and select when they are finished. Replace title bars
2018 * as necessary.
2019 */
John Reck9c35b9c2012-05-30 10:08:50 -07002020 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002021 public void onActionModeFinished(ActionMode mode) {
2022 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002023 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002024 mActionMode = null;
2025 }
2026
2027 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002028 final Tab tab = getCurrentTab();
2029 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002030 }
2031
2032 // bookmark handling
2033
2034 /**
2035 * add the current page as a bookmark to the given folder id
2036 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002037 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002038 * bookmarked, and if it is, edit that bookmark. If false, and
2039 * the site is already bookmarked, do not attempt to edit the
2040 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002041 */
2042 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002043 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002044 WebView w = getCurrentTopWebView();
2045 if (w == null) {
2046 return null;
2047 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002048 Intent i = new Intent(mActivity,
2049 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002050 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2051 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2052 String touchIconUrl = w.getTouchIconUrl();
2053 if (touchIconUrl != null) {
2054 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2055 WebSettings settings = w.getSettings();
2056 if (settings != null) {
2057 i.putExtra(AddBookmarkPage.USER_AGENT,
2058 settings.getUserAgentString());
2059 }
2060 }
2061 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2062 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2063 getDesiredThumbnailHeight(mActivity)));
2064 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002065 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002066 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2067 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002068 // Put the dialog at the upper right of the screen, covering the
2069 // star on the title bar.
2070 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002071 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002072 }
2073
2074 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002075 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002076 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002077 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002078 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002079 }
2080
2081 // thumbnails
2082
2083 /**
2084 * Return the desired width for thumbnail screenshots, which are stored in
2085 * the database, and used on the bookmarks screen.
2086 * @param context Context for finding out the density of the screen.
2087 * @return desired width for thumbnail screenshot.
2088 */
2089 static int getDesiredThumbnailWidth(Context context) {
2090 return context.getResources().getDimensionPixelOffset(
2091 R.dimen.bookmarkThumbnailWidth);
2092 }
2093
2094 /**
2095 * Return the desired height for thumbnail screenshots, which are stored in
2096 * the database, and used on the bookmarks screen.
2097 * @param context Context for finding out the density of the screen.
2098 * @return desired height for thumbnail screenshot.
2099 */
2100 static int getDesiredThumbnailHeight(Context context) {
2101 return context.getResources().getDimensionPixelOffset(
2102 R.dimen.bookmarkThumbnailHeight);
2103 }
2104
John Reck8cc92352011-07-06 17:41:52 -07002105 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002106 if (view == null || view.getContentHeight() == 0
2107 || view.getContentWidth() == 0) {
2108 return null;
2109 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002110 // We render to a bitmap 2x the desired size so that we can then
2111 // re-scale it with filtering since canvas.scale doesn't filter
2112 // This helps reduce aliasing at the cost of being slightly blurry
2113 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002114 int scaledWidth = width * filter_scale;
2115 int scaledHeight = height * filter_scale;
2116 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2117 || sThumbnailBitmap.getHeight() != scaledHeight) {
2118 if (sThumbnailBitmap != null) {
2119 sThumbnailBitmap.recycle();
2120 sThumbnailBitmap = null;
2121 }
2122 sThumbnailBitmap =
2123 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002124 }
John Reckd7dd9b22011-08-30 09:18:29 -07002125 Canvas canvas = new Canvas(sThumbnailBitmap);
2126 int contentWidth = view.getContentWidth();
2127 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2128 if (view instanceof BrowserWebView) {
2129 int dy = -((BrowserWebView)view).getTitleHeight();
2130 canvas.translate(0, dy * overviewScale);
2131 }
2132
2133 canvas.scale(overviewScale, overviewScale);
2134
2135 if (view instanceof BrowserWebView) {
2136 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002137 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002138 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002139 }
John Reckd7dd9b22011-08-30 09:18:29 -07002140 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2141 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002142 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002143 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002144 }
2145
John Reck34ef2672011-02-10 11:30:55 -08002146 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002147 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002148 // FIXME: Would like to make sure there is actually something to
2149 // draw, but the API for that (WebViewCore.pictureReady()) is not
2150 // currently accessible here.
2151
John Reck34ef2672011-02-10 11:30:55 -08002152 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002153 if (view == null) {
2154 // Tab was destroyed
2155 return;
2156 }
John Reck34ef2672011-02-10 11:30:55 -08002157 final String url = tab.getUrl();
2158 final String originalUrl = view.getOriginalUrl();
John Reck34ef2672011-02-10 11:30:55 -08002159 if (TextUtils.isEmpty(url)) {
2160 return;
2161 }
2162
2163 // Only update thumbnails for web urls (http(s)://), not for
2164 // about:, javascript:, data:, etc...
2165 // Unless it is a bookmarked site, then always update
2166 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2167 return;
2168 }
2169
Michael Kolb8233fac2010-10-26 16:08:53 -07002170 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2171 getDesiredThumbnailHeight(mActivity));
2172 if (bm == null) {
2173 return;
2174 }
2175
2176 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002177 new AsyncTask<Void, Void, Void>() {
2178 @Override
2179 protected Void doInBackground(Void... unused) {
2180 Cursor cursor = null;
2181 try {
2182 // TODO: Clean this up
2183 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2184 if (cursor != null && cursor.moveToFirst()) {
2185 final ByteArrayOutputStream os =
2186 new ByteArrayOutputStream();
2187 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002188
John Reck34ef2672011-02-10 11:30:55 -08002189 ContentValues values = new ContentValues();
2190 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002191
John Reck34ef2672011-02-10 11:30:55 -08002192 do {
John Reck617fd832011-02-16 14:35:59 -08002193 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002194 cr.update(Images.CONTENT_URI, values, null, null);
2195 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002196 }
John Reck34ef2672011-02-10 11:30:55 -08002197 } catch (IllegalStateException e) {
2198 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002199 } catch (SQLiteException s) {
2200 // Added for possible error when user tries to remove the same bookmark
2201 // that is being updated with a screen shot
2202 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002203 } finally {
2204 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002205 }
John Reck34ef2672011-02-10 11:30:55 -08002206 return null;
2207 }
2208 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002209 }
2210
2211 private class Copy implements OnMenuItemClickListener {
2212 private CharSequence mText;
2213
John Reck9c35b9c2012-05-30 10:08:50 -07002214 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002215 public boolean onMenuItemClick(MenuItem item) {
2216 copy(mText);
2217 return true;
2218 }
2219
2220 public Copy(CharSequence toCopy) {
2221 mText = toCopy;
2222 }
2223 }
2224
Leon Scroggins63c02662010-11-18 15:16:27 -05002225 private static class Download implements OnMenuItemClickListener {
2226 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002227 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002228 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002229 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002230 private static final String FALLBACK_EXTENSION = "dat";
2231 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002232
John Reck9c35b9c2012-05-30 10:08:50 -07002233 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002234 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002235 if (DataUri.isDataUri(mText)) {
2236 saveDataUri();
2237 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002238 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07002239 null, null, null, mPrivateBrowsing);
George Mount387d45d2011-10-07 15:57:53 -07002240 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002241 return true;
2242 }
2243
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002244 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2245 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002246 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002247 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002248 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002249 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002250 }
George Mount387d45d2011-10-07 15:57:53 -07002251
2252 /**
2253 * Treats mText as a data URI and writes its contents to a file
2254 * based on the current time.
2255 */
2256 private void saveDataUri() {
2257 FileOutputStream outputStream = null;
2258 try {
2259 DataUri uri = new DataUri(mText);
2260 File target = getTarget(uri);
2261 outputStream = new FileOutputStream(target);
2262 outputStream.write(uri.getData());
2263 final DownloadManager manager =
2264 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2265 manager.addCompletedDownload(target.getName(),
2266 mActivity.getTitle().toString(), false,
2267 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002268 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002269 } catch (IOException e) {
2270 Log.e(LOGTAG, "Could not save data URL");
2271 } finally {
2272 if (outputStream != null) {
2273 try {
2274 outputStream.close();
2275 } catch (IOException e) {
2276 // ignore close errors
2277 }
2278 }
2279 }
2280 }
2281
2282 /**
2283 * Creates a File based on the current time stamp and uses
2284 * the mime type of the DataUri to get the extension.
2285 */
2286 private File getTarget(DataUri uri) throws IOException {
2287 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002288 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002289 String nameBase = format.format(new Date());
2290 String mimeType = uri.getMimeType();
2291 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2292 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2293 if (extension == null) {
2294 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2295 extension = FALLBACK_EXTENSION;
2296 }
2297 extension = "." + extension; // createTempFile needs the '.'
2298 File targetFile = File.createTempFile(nameBase, extension, dir);
2299 return targetFile;
2300 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002301 }
2302
Cary Clark8974d282010-11-22 10:46:05 -05002303 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002304 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002305
John Reck9c35b9c2012-05-30 10:08:50 -07002306 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002307 public boolean onMenuItemClick(MenuItem item) {
2308 if (mWebView != null) {
2309 return mWebView.selectText();
2310 }
2311 return false;
2312 }
2313
2314 public SelectText(WebView webView) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002315 if (BrowserWebView.isClassic()) {
2316 mWebView = WebViewClassic.fromWebView(webView);
2317 }
Cary Clark8974d282010-11-22 10:46:05 -05002318 }
2319
2320 }
2321
Michael Kolb8233fac2010-10-26 16:08:53 -07002322 /********************** TODO: UI stuff *****************************/
2323
2324 // these methods have been copied, they still need to be cleaned up
2325
2326 /****************** tabs ***************************************************/
2327
2328 // basic tab interactions:
2329
2330 // it is assumed that tabcontrol already knows about the tab
2331 protected void addTab(Tab tab) {
2332 mUi.addTab(tab);
2333 }
2334
2335 protected void removeTab(Tab tab) {
2336 mUi.removeTab(tab);
2337 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002338 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002339 }
2340
Michael Kolbf2055602011-04-09 17:20:03 -07002341 @Override
2342 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002343 // monkey protection against delayed start
2344 if (tab != null) {
2345 mTabControl.setCurrentTab(tab);
2346 // the tab is guaranteed to have a webview after setCurrentTab
2347 mUi.setActiveTab(tab);
2348 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002349 }
2350
John Reck8bcafc12011-08-29 16:43:02 -07002351 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002352 Tab current = mTabControl.getCurrentTab();
2353 if (current != null
2354 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002355 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002356 }
2357 }
2358
John Reck26b18322011-06-21 13:08:58 -07002359 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002360 // Dismiss the subwindow if applicable.
2361 dismissSubWindow(appTab);
2362 // Since we might kill the WebView, remove it from the
2363 // content view first.
2364 mUi.detachTab(appTab);
2365 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002366 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002367 // TODO: analyze why the remove and add are necessary
2368 mUi.attachTab(appTab);
2369 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002370 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002371 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002372 } else {
2373 // If the tab was the current tab, we have to attach
2374 // it to the view system again.
2375 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002376 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002377 }
2378 }
2379
2380 // Remove the sub window if it exists. Also called by TabControl when the
2381 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002382 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002383 public void dismissSubWindow(Tab tab) {
2384 removeSubWindow(tab);
2385 // dismiss the subwindow. This will destroy the WebView.
2386 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002387 WebView wv = getCurrentTopWebView();
2388 if (wv != null) {
2389 wv.requestFocus();
2390 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002391 }
2392
2393 @Override
2394 public void removeSubWindow(Tab t) {
2395 if (t.getSubWebView() != null) {
2396 mUi.removeSubWindow(t.getSubViewContainer());
2397 }
2398 }
2399
2400 @Override
2401 public void attachSubWindow(Tab tab) {
2402 if (tab.getSubWebView() != null) {
2403 mUi.attachSubWindow(tab.getSubViewContainer());
2404 getCurrentTopWebView().requestFocus();
2405 }
2406 }
2407
Mathew Inwood29721c22011-06-29 17:55:24 +01002408 private Tab showPreloadedTab(final UrlData urlData) {
2409 if (!urlData.isPreloaded()) {
2410 return null;
2411 }
2412 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2413 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2414 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002415 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002416 // Could not submit query. Fallback to regular tab creation
2417 tabControl.destroy();
2418 return null;
2419 }
2420 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002421 // check tab count and make room for new tab
2422 if (!mTabControl.canCreateNewTab()) {
2423 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2424 if (leastUsed != null) {
2425 closeTab(leastUsed);
2426 }
2427 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002428 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002429 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002430 mTabControl.addPreloadedTab(t);
2431 addTab(t);
2432 setActiveTab(t);
2433 return t;
2434 }
2435
Michael Kolb7bcafde2011-05-09 13:55:59 -07002436 // open a non inconito tab with the given url data
2437 // and set as active tab
2438 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002439 Tab tab = showPreloadedTab(urlData);
2440 if (tab == null) {
2441 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002442 if ((tab != null) && !urlData.isEmpty()) {
2443 loadUrlDataIn(tab, urlData);
2444 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002445 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002446 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002447 }
2448
Michael Kolb843510f2010-12-09 10:51:49 -08002449 @Override
2450 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002451 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002452 }
2453
Michael Kolb8233fac2010-10-26 16:08:53 -07002454 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002455 public Tab openIncognitoTab() {
2456 return openTab(INCOGNITO_URI, true, true, false);
2457 }
2458
2459 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002460 public Tab openTab(String url, boolean incognito, boolean setActive,
2461 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002462 return openTab(url, incognito, setActive, useCurrent, null);
2463 }
2464
2465 @Override
2466 public Tab openTab(String url, Tab parent, boolean setActive,
2467 boolean useCurrent) {
2468 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2469 setActive, useCurrent, parent);
2470 }
2471
2472 public Tab openTab(String url, boolean incognito, boolean setActive,
2473 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002474 Tab tab = createNewTab(incognito, setActive, useCurrent);
2475 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002476 if (parent != null && parent != tab) {
2477 parent.addChildTab(tab);
2478 }
Michael Kolb519d2282011-05-09 17:03:19 -07002479 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002480 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002481 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002482 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002483 return tab;
2484 }
2485
2486 // this method will attempt to create a new tab
2487 // incognito: private browsing tab
2488 // setActive: ste tab as current tab
2489 // useCurrent: if no new tab can be created, return current tab
2490 private Tab createNewTab(boolean incognito, boolean setActive,
2491 boolean useCurrent) {
2492 Tab tab = null;
2493 if (mTabControl.canCreateNewTab()) {
2494 tab = mTabControl.createNewTab(incognito);
2495 addTab(tab);
2496 if (setActive) {
2497 setActiveTab(tab);
2498 }
2499 } else {
2500 if (useCurrent) {
2501 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002502 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002503 } else {
2504 mUi.showMaxTabsWarning();
2505 }
2506 }
2507 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002508 }
2509
John Reck2bc80422011-06-30 15:11:49 -07002510 @Override
2511 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2512 SnapshotTab tab = null;
2513 if (mTabControl.canCreateNewTab()) {
2514 tab = mTabControl.createSnapshotTab(snapshotId);
2515 addTab(tab);
2516 if (setActive) {
2517 setActiveTab(tab);
2518 }
2519 } else {
2520 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002521 }
2522 return tab;
2523 }
2524
Michael Kolb8233fac2010-10-26 16:08:53 -07002525 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002526 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002527 * @return boolean True if we successfully switched to a different tab. If
2528 * the indexth tab is null, or if that tab is the same as
2529 * the current one, return false.
2530 */
2531 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002532 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002533 Tab currentTab = mTabControl.getCurrentTab();
2534 if (tab == null || tab == currentTab) {
2535 return false;
2536 }
2537 setActiveTab(tab);
2538 return true;
2539 }
2540
2541 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002542 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002543 closeCurrentTab(false);
2544 }
2545
2546 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002547 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002548 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002549 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002550 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002551 return;
2552 }
Michael Kolbc831b632011-05-11 09:30:34 -07002553 final Tab current = mTabControl.getCurrentTab();
2554 final int pos = mTabControl.getCurrentPosition();
2555 Tab newTab = current.getParent();
2556 if (newTab == null) {
2557 newTab = mTabControl.getTab(pos + 1);
2558 if (newTab == null) {
2559 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002560 }
2561 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002562 if (andQuit) {
2563 mTabControl.setCurrentTab(newTab);
2564 closeTab(current);
2565 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002566 // Close window
2567 closeTab(current);
2568 }
2569 }
2570
2571 /**
2572 * Close the tab, remove its associated title bar, and adjust mTabControl's
2573 * current tab to a valid value.
2574 */
2575 @Override
2576 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002577 if (tab == mTabControl.getCurrentTab()) {
2578 closeCurrentTab();
2579 } else {
2580 removeTab(tab);
2581 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002582 }
2583
Afzal Najamd4e33312012-04-26 01:54:01 -04002584 /**
2585 * Close all tabs except the current one
2586 */
2587 @Override
2588 public void closeOtherTabs() {
2589 int inactiveTabs = mTabControl.getTabCount() - 1;
2590 for (int i = inactiveTabs; i >= 0; i--) {
2591 Tab tab = mTabControl.getTab(i);
2592 if (tab != mTabControl.getCurrentTab()) {
2593 removeTab(tab);
2594 }
2595 }
2596 }
2597
Michael Kolb8233fac2010-10-26 16:08:53 -07002598 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002599 protected void loadUrlFromContext(String url) {
2600 Tab tab = getCurrentTab();
2601 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002602 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002603 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002604 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002605 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002606 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002607 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002608 }
2609 }
2610 }
2611
2612 /**
2613 * Load the URL into the given WebView and update the title bar
2614 * to reflect the new load. Call this instead of WebView.loadUrl
2615 * directly.
2616 * @param view The WebView used to load url.
2617 * @param url The URL to load.
2618 */
John Reck71e51422011-07-01 16:49:28 -07002619 @Override
2620 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002621 loadUrl(tab, url, null);
2622 }
2623
2624 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2625 if (tab != null) {
2626 dismissSubWindow(tab);
2627 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002628 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002629 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002630 }
2631
2632 /**
2633 * Load UrlData into a Tab and update the title bar to reflect the new
2634 * load. Call this instead of UrlData.loadIn directly.
2635 * @param t The Tab used to load.
2636 * @param data The UrlData being loaded.
2637 */
2638 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002639 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002640 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002641 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002642 } else {
John Reck38b39652012-06-05 09:22:59 -07002643 if (t != null && data.mDisableUrlOverride) {
2644 t.disableUrlOverridingForLoad();
2645 }
John Reck26b18322011-06-21 13:08:58 -07002646 loadUrl(t, data.mUrl, data.mHeaders);
2647 }
2648 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002649 }
2650
John Reck30c714c2010-12-16 17:30:34 -08002651 @Override
2652 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002653 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002654 if (tab.canGoBack()) {
2655 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002656 } else {
John Reckef654f12011-07-12 16:42:08 -07002657 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002658 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002659 }
2660
2661 void goBackOnePageOrQuit() {
2662 Tab current = mTabControl.getCurrentTab();
2663 if (current == null) {
2664 /*
2665 * Instead of finishing the activity, simply push this to the back
2666 * of the stack and let ActivityManager to choose the foreground
2667 * activity. As BrowserActivity is singleTask, it will be always the
2668 * root of the task. So we can use either true or false for
2669 * moveTaskToBack().
2670 */
luxiaolb40014b2013-07-19 10:01:43 +08002671 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07002672 return;
2673 }
John Reckef654f12011-07-12 16:42:08 -07002674 if (current.canGoBack()) {
2675 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002676 } else {
2677 // Check to see if we are closing a window that was created by
2678 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002679 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002680 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002681 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002682 // Now we close the other tab
2683 closeTab(current);
2684 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002685 /*
2686 * Instead of finishing the activity, simply push this to the back
2687 * of the stack and let ActivityManager to choose the foreground
2688 * activity. As BrowserActivity is singleTask, it will be always the
2689 * root of the task. So we can use either true or false for
2690 * moveTaskToBack().
2691 */
luxiaolb40014b2013-07-19 10:01:43 +08002692 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07002693 }
2694 }
2695 }
2696
2697 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002698 * helper method for key handler
2699 * returns the current tab if it can't advance
2700 */
Michael Kolbc831b632011-05-11 09:30:34 -07002701 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002702 int pos = mTabControl.getCurrentPosition() + 1;
2703 if (pos >= mTabControl.getTabCount()) {
2704 pos = 0;
2705 }
2706 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002707 }
2708
2709 /**
2710 * helper method for key handler
2711 * returns the current tab if it can't advance
2712 */
Michael Kolbc831b632011-05-11 09:30:34 -07002713 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002714 int pos = mTabControl.getCurrentPosition() - 1;
2715 if ( pos < 0) {
2716 pos = mTabControl.getTabCount() - 1;
2717 }
2718 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002719 }
2720
John Reckbcef87f2012-02-03 14:58:34 -08002721 boolean isMenuOrCtrlKey(int keyCode) {
2722 return (KeyEvent.KEYCODE_MENU == keyCode)
2723 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2724 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2725 }
2726
Michael Kolb0035fad2011-03-14 13:25:28 -07002727 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002728 * handle key events in browser
2729 *
2730 * @param keyCode
2731 * @param event
2732 * @return true if handled, false to pass to super
2733 */
John Reck9c35b9c2012-05-30 10:08:50 -07002734 @Override
2735 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002736 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002737 // Even if MENU is already held down, we need to call to super to open
2738 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002739 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002740 mMenuIsDown = true;
2741 return false;
2742 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002743
Cary Clark8ff8c662010-12-29 15:03:05 -05002744 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002745 Tab tab = getCurrentTab();
2746 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002747
Cary Clark160bbb92011-01-10 11:17:07 -05002748 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2749 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002750
Michael Kolb8233fac2010-10-26 16:08:53 -07002751 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002752 case KeyEvent.KEYCODE_TAB:
2753 if (event.isCtrlPressed()) {
2754 if (event.isShiftPressed()) {
2755 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002756 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002757 } else {
2758 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002759 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002760 }
2761 return true;
2762 }
2763 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002764 case KeyEvent.KEYCODE_SPACE:
2765 // WebView/WebTextView handle the keys in the KeyDown. As
2766 // the Activity's shortcut keys are only handled when WebView
2767 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002768 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002769 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002770 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002771 pageDown();
2772 }
2773 return true;
2774 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002775 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002776 event.startTracking();
2777 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002778 case KeyEvent.KEYCODE_FORWARD:
2779 if (!noModifiers) break;
2780 tab.goForward();
2781 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002782 case KeyEvent.KEYCODE_DPAD_LEFT:
2783 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002784 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002785 return true;
2786 }
2787 break;
2788 case KeyEvent.KEYCODE_DPAD_RIGHT:
2789 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002790 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002791 return true;
2792 }
2793 break;
2794 case KeyEvent.KEYCODE_A:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002795 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002796 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002797 return true;
2798 }
2799 break;
Michael Kolba4183062011-01-16 10:43:21 -08002800// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002801 case KeyEvent.KEYCODE_C:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002802 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002803 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002804 return true;
2805 }
2806 break;
Michael Kolba4183062011-01-16 10:43:21 -08002807// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002808// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002809// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002810// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002811// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002812// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002813// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002814// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002815// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002816// case KeyEvent.KEYCODE_M: // unused
2817// case KeyEvent.KEYCODE_N: // in Chrome: new window
2818// case KeyEvent.KEYCODE_O: // in Chrome: open file
2819// case KeyEvent.KEYCODE_P: // in Chrome: print page
2820// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002821// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002822// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2823 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002824 // we can't use the ctrl/shift flags, they check for
2825 // exclusive use of a modifier
2826 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002827 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002828 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002829 } else {
2830 openTabToHomePage();
2831 }
2832 return true;
2833 }
2834 break;
2835// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2836// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002837// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002838// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2839// case KeyEvent.KEYCODE_Y: // unused
2840// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002841 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002842 // it is a regular key and webview is not null
2843 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002844 }
2845
John Reck9c35b9c2012-05-30 10:08:50 -07002846 @Override
2847 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002848 switch(keyCode) {
2849 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002850 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002851 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002852 return true;
2853 }
2854 break;
2855 }
2856 return false;
2857 }
2858
John Reck9c35b9c2012-05-30 10:08:50 -07002859 @Override
2860 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002861 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002862 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002863 if (KeyEvent.KEYCODE_MENU == keyCode
2864 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002865 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002866 }
2867 }
Cary Clark160bbb92011-01-10 11:17:07 -05002868 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002869 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002870 case KeyEvent.KEYCODE_BACK:
2871 if (event.isTracking() && !event.isCanceled()) {
2872 onBackKey();
2873 return true;
2874 }
2875 break;
2876 }
2877 return false;
2878 }
2879
2880 public boolean isMenuDown() {
2881 return mMenuIsDown;
2882 }
2883
John Reck9c35b9c2012-05-30 10:08:50 -07002884 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002885 public void setupAutoFill(Message message) {
2886 // Open the settings activity at the AutoFill profile fragment so that
2887 // the user can create a new profile. When they return, we will dispatch
2888 // the message so that we can autofill the form using their new profile.
2889 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2890 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2891 AutoFillSettingsFragment.class.getName());
2892 mAutoFillSetupMessage = message;
2893 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2894 }
John Reckb3417f02011-01-14 11:01:05 -08002895
John Reck9c35b9c2012-05-30 10:08:50 -07002896 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002897 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002898 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002899 return true;
2900 }
2901
John Reck1cf4b792011-07-26 10:22:22 -07002902 @Override
2903 public boolean shouldCaptureThumbnails() {
2904 return mUi.shouldCaptureThumbnails();
2905 }
2906
Michael Kolbc3af0672011-08-09 10:24:41 -07002907 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07002908 public boolean supportsVoice() {
2909 PackageManager pm = mActivity.getPackageManager();
2910 List activities = pm.queryIntentActivities(new Intent(
2911 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
2912 return activities.size() != 0;
2913 }
2914
2915 @Override
2916 public void startVoiceRecognizer() {
2917 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
2918 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
2919 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
2920 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
2921 mActivity.startActivityForResult(voice, VOICE_RESULT);
2922 }
2923
2924 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002925 public void setBlockEvents(boolean block) {
2926 mBlockEvents = block;
2927 }
2928
John Reck9c35b9c2012-05-30 10:08:50 -07002929 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002930 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002931 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002932 }
2933
John Reck9c35b9c2012-05-30 10:08:50 -07002934 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002935 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002936 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002937 }
2938
John Reck9c35b9c2012-05-30 10:08:50 -07002939 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002940 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002941 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002942 }
2943
John Reck9c35b9c2012-05-30 10:08:50 -07002944 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002945 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002946 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002947 }
2948
John Reck9c35b9c2012-05-30 10:08:50 -07002949 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002950 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002951 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002952 }
2953
Michael Kolb8233fac2010-10-26 16:08:53 -07002954}