blob: 4ec50424994a9d5323131ee28636486b9f641ba6 [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) {
kaiyiz47097d62013-08-09 09:30:28 +08001206 if (url != null) {
1207 parent = openTab(url, parent,
1208 !mSettings.openInBackground(), true);
1209 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001210 }
1211 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1212 long id = intent.getLongExtra(
1213 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1214 if (id >= 0) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08001215 if (BrowserWebView.isClassic()) {
1216 createNewSnapshotTab(id, true);
1217 } else {
1218 Toast.makeText(mActivity, "Snapshot Tab requires WebViewClassic",
1219 Toast.LENGTH_LONG).show();
1220 }
John Reckd3e4d5b2011-07-13 15:48:43 -07001221 }
1222 }
1223 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001224 case VOICE_RESULT:
1225 if (resultCode == Activity.RESULT_OK && intent != null) {
1226 ArrayList<String> results = intent.getStringArrayListExtra(
1227 RecognizerIntent.EXTRA_RESULTS);
1228 if (results.size() >= 1) {
1229 mVoiceResult = results.get(0);
1230 }
1231 }
1232 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001233 default:
1234 break;
1235 }
1236 getCurrentTopWebView().requestFocus();
1237 }
1238
1239 /**
1240 * Open the Go page.
1241 * @param startWithHistory If true, open starting on the history tab.
1242 * Otherwise, start with the bookmarks tab.
1243 */
1244 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001245 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001246 if (mTabControl.getCurrentWebView() == null) {
1247 return;
1248 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001249 // clear action mode
1250 if (isInCustomActionMode()) {
1251 endActionMode();
1252 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001253 Bundle extras = new Bundle();
1254 // Disable opening in a new window if we have maxed out the windows
1255 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1256 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001257 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001258 }
1259
1260 // combo view callbacks
1261
Michael Kolb8233fac2010-10-26 16:08:53 -07001262 // key handling
1263 protected void onBackKey() {
1264 if (!mUi.onBackKey()) {
1265 WebView subwindow = mTabControl.getCurrentSubWindow();
1266 if (subwindow != null) {
1267 if (subwindow.canGoBack()) {
1268 subwindow.goBack();
1269 } else {
1270 dismissSubWindow(mTabControl.getCurrentTab());
1271 }
1272 } else {
1273 goBackOnePageOrQuit();
1274 }
1275 }
1276 }
1277
Michael Kolb4bd767d2011-05-27 11:33:55 -07001278 protected boolean onMenuKey() {
1279 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001280 }
1281
Michael Kolb8233fac2010-10-26 16:08:53 -07001282 // menu handling and state
1283 // TODO: maybe put into separate handler
1284
John Reck9c35b9c2012-05-30 10:08:50 -07001285 @Override
1286 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001287 if (mMenuState == EMPTY_MENU) {
1288 return false;
1289 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001290 MenuInflater inflater = mActivity.getMenuInflater();
1291 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001292 return true;
1293 }
1294
John Reck9c35b9c2012-05-30 10:08:50 -07001295 @Override
1296 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001297 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001298 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001299 return;
1300 }
1301 if (!(v instanceof WebView)) {
1302 return;
1303 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001304 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001305 WebView.HitTestResult result = webview.getHitTestResult();
1306 if (result == null) {
1307 return;
1308 }
1309
1310 int type = result.getType();
1311 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1312 Log.w(LOGTAG,
1313 "We should not show context menu when nothing is touched");
1314 return;
1315 }
1316 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1317 // let TextView handles context menu
1318 return;
1319 }
1320
1321 // Note, http://b/issue?id=1106666 is requesting that
1322 // an inflated menu can be used again. This is not available
1323 // yet, so inflate each time (yuk!)
1324 MenuInflater inflater = mActivity.getMenuInflater();
1325 inflater.inflate(R.menu.browsercontext, menu);
1326
1327 // Show the correct menu group
1328 final String extra = result.getExtra();
Michael Kolbc159c1a2011-12-15 16:06:38 -08001329 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001330 menu.setGroupVisible(R.id.PHONE_MENU,
1331 type == WebView.HitTestResult.PHONE_TYPE);
1332 menu.setGroupVisible(R.id.EMAIL_MENU,
1333 type == WebView.HitTestResult.EMAIL_TYPE);
1334 menu.setGroupVisible(R.id.GEO_MENU,
1335 type == WebView.HitTestResult.GEO_TYPE);
1336 menu.setGroupVisible(R.id.IMAGE_MENU,
1337 type == WebView.HitTestResult.IMAGE_TYPE
1338 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1339 menu.setGroupVisible(R.id.ANCHOR_MENU,
1340 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1341 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001342 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1343 || type == WebView.HitTestResult.PHONE_TYPE
1344 || type == WebView.HitTestResult.EMAIL_TYPE
1345 || type == WebView.HitTestResult.GEO_TYPE;
1346 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1347 if (hitText) {
1348 menu.findItem(R.id.select_text_menu_id)
1349 .setOnMenuItemClickListener(new SelectText(webview));
1350 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001351 // Setup custom handling depending on the type
1352 switch (type) {
1353 case WebView.HitTestResult.PHONE_TYPE:
1354 menu.setHeaderTitle(Uri.decode(extra));
1355 menu.findItem(R.id.dial_context_menu_id).setIntent(
1356 new Intent(Intent.ACTION_VIEW, Uri
1357 .parse(WebView.SCHEME_TEL + extra)));
1358 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1359 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1360 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1361 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1362 addIntent);
1363 menu.findItem(R.id.copy_phone_context_menu_id)
1364 .setOnMenuItemClickListener(
1365 new Copy(extra));
1366 break;
1367
1368 case WebView.HitTestResult.EMAIL_TYPE:
1369 menu.setHeaderTitle(extra);
1370 menu.findItem(R.id.email_context_menu_id).setIntent(
1371 new Intent(Intent.ACTION_VIEW, Uri
1372 .parse(WebView.SCHEME_MAILTO + extra)));
1373 menu.findItem(R.id.copy_mail_context_menu_id)
1374 .setOnMenuItemClickListener(
1375 new Copy(extra));
1376 break;
1377
1378 case WebView.HitTestResult.GEO_TYPE:
1379 menu.setHeaderTitle(extra);
1380 menu.findItem(R.id.map_context_menu_id).setIntent(
1381 new Intent(Intent.ACTION_VIEW, Uri
1382 .parse(WebView.SCHEME_GEO
1383 + URLEncoder.encode(extra))));
1384 menu.findItem(R.id.copy_geo_context_menu_id)
1385 .setOnMenuItemClickListener(
1386 new Copy(extra));
1387 break;
1388
1389 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1390 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001391 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001392 // decide whether to show the open link in new tab option
1393 boolean showNewTab = mTabControl.canCreateNewTab();
1394 MenuItem newTabItem
1395 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001396 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001397 ? R.string.contextmenu_openlink_newwindow_background
1398 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001399 newTabItem.setVisible(showNewTab);
1400 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001401 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1402 newTabItem.setOnMenuItemClickListener(
1403 new MenuItem.OnMenuItemClickListener() {
1404 @Override
1405 public boolean onMenuItemClick(MenuItem item) {
1406 final HashMap<String, WebView> hrefMap =
1407 new HashMap<String, WebView>();
1408 hrefMap.put("webview", webview);
1409 final Message msg = mHandler.obtainMessage(
1410 FOCUS_NODE_HREF,
1411 R.id.open_newtab_context_menu_id,
1412 0, hrefMap);
1413 webview.requestFocusNodeHref(msg);
1414 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001415 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001416 });
1417 } else {
1418 newTabItem.setOnMenuItemClickListener(
1419 new MenuItem.OnMenuItemClickListener() {
1420 @Override
1421 public boolean onMenuItemClick(MenuItem item) {
1422 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001423 openTab(extra, parent,
1424 !mSettings.openInBackground(),
1425 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001426 return true;
1427 }
1428 });
1429 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001430 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001431 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1432 break;
1433 }
1434 // otherwise fall through to handle image part
1435 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001436 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1437 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001438 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1439 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001440 shareItem.setOnMenuItemClickListener(
1441 new MenuItem.OnMenuItemClickListener() {
1442 @Override
1443 public boolean onMenuItemClick(MenuItem item) {
1444 sharePage(mActivity, null, extra, null,
1445 null);
1446 return true;
1447 }
1448 }
1449 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001450 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001451 menu.findItem(R.id.view_image_context_menu_id)
1452 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1453 @Override
1454 public boolean onMenuItemClick(MenuItem item) {
1455 openTab(extra, mTabControl.getCurrentTab(), true, true);
1456 return false;
1457 }
1458 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001459 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1460 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1461 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001462 menu.findItem(R.id.set_wallpaper_context_menu_id).
1463 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1464 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001465 break;
1466
1467 default:
1468 Log.w(LOGTAG, "We should not get here.");
1469 break;
1470 }
1471 //update the ui
1472 mUi.onContextMenuCreated(menu);
1473 }
1474
1475 /**
1476 * As the menu can be open when loading state changes
1477 * we must manually update the state of the stop/reload menu
1478 * item
1479 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001480 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001481 if (menu == null) {
1482 return;
1483 }
1484 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001485 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001486 menu.findItem(R.id.stop_menu_id):
1487 menu.findItem(R.id.reload_menu_id);
1488 if (src != null) {
1489 dest.setIcon(src.getIcon());
1490 dest.setTitle(src.getTitle());
1491 }
1492 }
1493
John Reck9c35b9c2012-05-30 10:08:50 -07001494 @Override
1495 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001496 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001497 // hold on to the menu reference here; it is used by the page callbacks
1498 // to update the menu based on loading state
1499 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001500 // Note: setVisible will decide whether an item is visible; while
1501 // setEnabled() will decide whether an item is enabled, which also means
1502 // whether the matching shortcut key will function.
1503 switch (mMenuState) {
1504 case EMPTY_MENU:
1505 if (mCurrentMenuState != mMenuState) {
1506 menu.setGroupVisible(R.id.MAIN_MENU, false);
1507 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1508 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1509 }
1510 break;
1511 default:
1512 if (mCurrentMenuState != mMenuState) {
1513 menu.setGroupVisible(R.id.MAIN_MENU, true);
1514 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1515 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1516 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001517 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001518 break;
1519 }
1520 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001521 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001522 }
1523
Michael Kolb4bf79712011-07-14 14:18:12 -07001524 @Override
1525 public void updateMenuState(Tab tab, Menu menu) {
1526 boolean canGoBack = false;
1527 boolean canGoForward = false;
John Reck42229bc2011-08-19 13:26:43 -07001528 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001529 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001530 if (tab != null) {
1531 canGoBack = tab.canGoBack();
1532 canGoForward = tab.canGoForward();
John Reck42229bc2011-08-19 13:26:43 -07001533 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001534 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001535 }
1536 final MenuItem back = menu.findItem(R.id.back_menu_id);
1537 back.setEnabled(canGoBack);
1538
1539 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001540
1541 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1542 forward.setEnabled(canGoForward);
1543
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001544 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1545 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001546 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001547 if (source != null && dest != null) {
1548 dest.setTitle(source.getTitle());
1549 dest.setIcon(source.getIcon());
1550 }
John Recke1a03a32011-09-14 17:04:16 -07001551 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001552
1553 // decide whether to show the share link option
1554 PackageManager pm = mActivity.getPackageManager();
1555 Intent send = new Intent(Intent.ACTION_SEND);
1556 send.setType("text/plain");
1557 ResolveInfo ri = pm.resolveActivity(send,
1558 PackageManager.MATCH_DEFAULT_ONLY);
1559 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1560
1561 boolean isNavDump = mSettings.enableNavDump();
1562 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1563 nav.setVisible(isNavDump);
1564 nav.setEnabled(isNavDump);
1565
1566 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001567 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1568 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001569 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1570 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
kaiyiza8b6dbb2013-07-29 18:11:22 +08001571
1572 // history and snapshots item are the members of COMBO menu group,
1573 // so if show history item, only make snapshots item invisible.
1574 menu.findItem(R.id.snapshots_menu_id).setVisible(false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001575
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001576 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001577 }
1578
John Reck9c35b9c2012-05-30 10:08:50 -07001579 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001580 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 if (null == getCurrentTopWebView()) {
1582 return false;
1583 }
1584 if (mMenuIsDown) {
1585 // The shortcut action consumes the MENU. Even if it is still down,
1586 // it won't trigger the next shortcut action. In the case of the
1587 // shortcut action triggering a new activity, like Bookmarks, we
1588 // won't get onKeyUp for MENU. So it is important to reset it here.
1589 mMenuIsDown = false;
1590 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001591 if (mUi.onOptionsItemSelected(item)) {
1592 // ui callback handled it
1593 return true;
1594 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001595 switch (item.getItemId()) {
1596 // -- Main menu
1597 case R.id.new_tab_menu_id:
1598 openTabToHomePage();
1599 break;
1600
1601 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001602 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001603 break;
1604
Afzal Najamd4e33312012-04-26 01:54:01 -04001605 case R.id.close_other_tabs_id:
1606 closeOtherTabs();
1607 break;
1608
Michael Kolb8233fac2010-10-26 16:08:53 -07001609 case R.id.goto_menu_id:
1610 editUrl();
1611 break;
1612
1613 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001614 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1615 break;
1616
1617 case R.id.history_menu_id:
1618 bookmarksOrHistoryPicker(ComboViews.History);
1619 break;
1620
1621 case R.id.snapshots_menu_id:
1622 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001623 break;
1624
Michael Kolb8233fac2010-10-26 16:08:53 -07001625 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001626 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001627 break;
1628
1629 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001630 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001631 stopLoading();
1632 } else {
1633 getCurrentTopWebView().reload();
1634 }
1635 break;
1636
1637 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001638 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001639 break;
1640
1641 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001642 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001643 break;
1644
1645 case R.id.close_menu_id:
1646 // Close the subwindow if it exists.
1647 if (mTabControl.getCurrentSubWindow() != null) {
1648 dismissSubWindow(mTabControl.getCurrentTab());
1649 break;
1650 }
1651 closeCurrentTab();
1652 break;
1653
kaiyiza8b6dbb2013-07-29 18:11:22 +08001654 case R.id.exit_menu_id:
1655 String ret = SystemProperties.get("persist.debug.browsermonkeytest");
1656 if (ret != null && ret.equals("enable"))
1657 break;
luxiaolb40014b2013-07-19 10:01:43 +08001658 showExitDialog(mActivity);
1659 return true;
kaiyiza8b6dbb2013-07-29 18:11:22 +08001660
Michael Kolb8233fac2010-10-26 16:08:53 -07001661 case R.id.homepage_menu_id:
1662 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001663 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001664 break;
1665
1666 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001667 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001668 break;
1669
1670 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001671 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001672 break;
1673
John Reck2bc80422011-06-30 15:11:49 -07001674 case R.id.save_snapshot_menu_id:
1675 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001676 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001677 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001678 break;
1679
Michael Kolb8233fac2010-10-26 16:08:53 -07001680 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001681 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001682 break;
1683
John Recke1a03a32011-09-14 17:04:16 -07001684 case R.id.snapshot_go_live:
1685 goLive();
1686 return true;
1687
Michael Kolb8233fac2010-10-26 16:08:53 -07001688 case R.id.share_page_menu_id:
1689 Tab currentTab = mTabControl.getCurrentTab();
1690 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001691 return false;
1692 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001693 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001694 break;
1695
1696 case R.id.dump_nav_menu_id:
1697 getCurrentTopWebView().debugDump();
1698 break;
1699
Michael Kolb8233fac2010-10-26 16:08:53 -07001700 case R.id.zoom_in_menu_id:
1701 getCurrentTopWebView().zoomIn();
1702 break;
1703
1704 case R.id.zoom_out_menu_id:
1705 getCurrentTopWebView().zoomOut();
1706 break;
1707
1708 case R.id.view_downloads_menu_id:
1709 viewDownloads();
1710 break;
1711
John Reck42229bc2011-08-19 13:26:43 -07001712 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001713 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07001714 break;
1715
Michael Kolb8233fac2010-10-26 16:08:53 -07001716 case R.id.window_one_menu_id:
1717 case R.id.window_two_menu_id:
1718 case R.id.window_three_menu_id:
1719 case R.id.window_four_menu_id:
1720 case R.id.window_five_menu_id:
1721 case R.id.window_six_menu_id:
1722 case R.id.window_seven_menu_id:
1723 case R.id.window_eight_menu_id:
1724 {
1725 int menuid = item.getItemId();
1726 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1727 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1728 Tab desiredTab = mTabControl.getTab(id);
1729 if (desiredTab != null &&
1730 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001731 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001732 }
1733 break;
1734 }
1735 }
1736 }
1737 break;
1738
1739 default:
1740 return false;
1741 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001742 return true;
1743 }
1744
John Reck68234a92012-04-19 15:27:12 -07001745 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
1746 implements OnCancelListener {
1747
1748 private Tab mTab;
1749 private Dialog mProgressDialog;
1750 private ContentValues mValues;
1751
1752 private SaveSnapshotTask(Tab tab) {
1753 mTab = tab;
1754 }
1755
1756 @Override
1757 protected void onPreExecute() {
1758 CharSequence message = mActivity.getText(R.string.saving_snapshot);
1759 mProgressDialog = ProgressDialog.show(mActivity, null, message,
1760 true, true, this);
1761 mValues = mTab.createSnapshotValues();
1762 }
1763
1764 @Override
1765 protected Long doInBackground(Void... params) {
1766 if (!mTab.saveViewState(mValues)) {
1767 return null;
1768 }
1769 if (isCancelled()) {
1770 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
1771 File file = mActivity.getFileStreamPath(path);
1772 if (!file.delete()) {
1773 file.deleteOnExit();
1774 }
1775 return null;
1776 }
1777 final ContentResolver cr = mActivity.getContentResolver();
1778 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
1779 if (result == null) {
1780 return null;
1781 }
1782 long id = ContentUris.parseId(result);
1783 return id;
1784 }
1785
1786 @Override
1787 protected void onPostExecute(Long id) {
1788 if (isCancelled()) {
1789 return;
1790 }
1791 mProgressDialog.dismiss();
1792 if (id == null) {
1793 Toast.makeText(mActivity, R.string.snapshot_failed,
1794 Toast.LENGTH_SHORT).show();
1795 return;
1796 }
1797 Bundle b = new Bundle();
1798 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1799 mUi.showComboView(ComboViews.Snapshots, b);
1800 }
1801
1802 @Override
1803 public void onCancel(DialogInterface dialog) {
1804 cancel(true);
1805 }
1806 }
1807
Michael Kolb80f75082012-04-10 10:50:06 -07001808 @Override
1809 public void toggleUserAgent() {
1810 WebView web = getCurrentWebView();
1811 mSettings.toggleDesktopUseragent(web);
1812 web.loadUrl(web.getOriginalUrl());
1813 }
1814
1815 @Override
1816 public void findOnPage() {
1817 getCurrentTopWebView().showFindDialog(null, true);
1818 }
1819
1820 @Override
1821 public void openPreferences() {
1822 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1823 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1824 getCurrentTopWebView().getUrl());
1825 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1826 }
1827
1828 @Override
1829 public void bookmarkCurrentPage() {
1830 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
1831 if (bookmarkIntent != null) {
1832 mActivity.startActivity(bookmarkIntent);
1833 }
1834 }
1835
John Recke1a03a32011-09-14 17:04:16 -07001836 private void goLive() {
1837 Tab t = getCurrentTab();
1838 t.loadUrl(t.getUrl(), null);
1839 }
1840
luxiaolb40014b2013-07-19 10:01:43 +08001841 private void showExitDialog(final Activity activity) {
1842 new AlertDialog.Builder(activity)
1843 .setTitle(R.string.exit_browser_title)
1844 .setIcon(android.R.drawable.ic_dialog_alert)
1845 .setMessage(R.string.exit_browser_msg)
1846 .setNegativeButton(R.string.exit_minimize, new DialogInterface.OnClickListener() {
1847 public void onClick(DialogInterface dialog, int which) {
1848 activity.moveTaskToBack(true);
1849 dialog.dismiss();
1850 }
1851 })
1852 .setPositiveButton(R.string.exit_quit, new DialogInterface.OnClickListener() {
1853 public void onClick(DialogInterface dialog, int which) {
1854 activity.finish();
1855 mHandler.postDelayed(new Runnable() {
1856 @Override
1857 public void run() {
1858 // TODO Auto-generated method stub
1859 mCrashRecoveryHandler.clearState(true);
1860 int pid = android.os.Process.myPid();
1861 android.os.Process.killProcess(pid);
1862 }
1863 }, 300);
1864 dialog.dismiss();
1865 }
1866 })
1867 .show();
1868 }
1869
Michael Kolb315d5022011-10-13 12:47:11 -07001870 @Override
1871 public void showPageInfo() {
1872 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
1873 }
1874
John Reck9c35b9c2012-05-30 10:08:50 -07001875 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001876 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001877 // Let the History and Bookmark fragments handle menus they created.
1878 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1879 return false;
1880 }
1881
Michael Kolb8233fac2010-10-26 16:08:53 -07001882 int id = item.getItemId();
1883 boolean result = true;
1884 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001885 // -- Browser context menu
1886 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001887 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001888 case R.id.copy_link_context_menu_id:
1889 final WebView webView = getCurrentTopWebView();
1890 if (null == webView) {
1891 result = false;
1892 break;
1893 }
1894 final HashMap<String, WebView> hrefMap =
1895 new HashMap<String, WebView>();
1896 hrefMap.put("webview", webView);
1897 final Message msg = mHandler.obtainMessage(
1898 FOCUS_NODE_HREF, id, 0, hrefMap);
1899 webView.requestFocusNodeHref(msg);
1900 break;
1901
1902 default:
1903 // For other context menus
1904 result = onOptionsItemSelected(item);
1905 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001906 return result;
1907 }
1908
1909 /**
1910 * support programmatically opening the context menu
1911 */
1912 public void openContextMenu(View view) {
1913 mActivity.openContextMenu(view);
1914 }
1915
1916 /**
1917 * programmatically open the options menu
1918 */
1919 public void openOptionsMenu() {
1920 mActivity.openOptionsMenu();
1921 }
1922
John Reck9c35b9c2012-05-30 10:08:50 -07001923 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001924 public boolean onMenuOpened(int featureId, Menu menu) {
1925 if (mOptionsMenuOpen) {
1926 if (mConfigChanged) {
1927 // We do not need to make any changes to the state of the
1928 // title bar, since the only thing that happened was a
1929 // change in orientation
1930 mConfigChanged = false;
1931 } else {
1932 if (!mExtendedMenuOpen) {
1933 mExtendedMenuOpen = true;
1934 mUi.onExtendedMenuOpened();
1935 } else {
1936 // Switching the menu back to icon view, so show the
1937 // title bar once again.
1938 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001939 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001940 }
1941 }
1942 } else {
1943 // The options menu is closed, so open it, and show the title
1944 mOptionsMenuOpen = true;
1945 mConfigChanged = false;
1946 mExtendedMenuOpen = false;
1947 mUi.onOptionsMenuOpened();
1948 }
1949 return true;
1950 }
1951
John Reck9c35b9c2012-05-30 10:08:50 -07001952 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001953 public void onOptionsMenuClosed(Menu menu) {
1954 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001955 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001956 }
1957
John Reck9c35b9c2012-05-30 10:08:50 -07001958 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001959 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001960 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001961 }
1962
1963 // Helper method for getting the top window.
1964 @Override
1965 public WebView getCurrentTopWebView() {
1966 return mTabControl.getCurrentTopWebView();
1967 }
1968
1969 @Override
1970 public WebView getCurrentWebView() {
1971 return mTabControl.getCurrentWebView();
1972 }
1973
1974 /*
1975 * This method is called as a result of the user selecting the options
1976 * menu to see the download window. It shows the download window on top of
1977 * the current window.
1978 */
1979 void viewDownloads() {
1980 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1981 mActivity.startActivity(intent);
1982 }
1983
John Reck30b065e2011-07-19 10:58:05 -07001984 int getActionModeHeight() {
1985 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1986 new int[] { android.R.attr.actionBarSize });
1987 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1988 actionBarSizeTypedArray.recycle();
1989 return size;
1990 }
1991
Michael Kolb8233fac2010-10-26 16:08:53 -07001992 // action mode
1993
John Reck9c35b9c2012-05-30 10:08:50 -07001994 @Override
1995 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001996 mUi.onActionModeStarted(mode);
1997 mActionMode = mode;
1998 }
1999
2000 /*
2001 * True if a custom ActionMode (i.e. find or select) is in use.
2002 */
2003 @Override
2004 public boolean isInCustomActionMode() {
2005 return mActionMode != null;
2006 }
2007
2008 /*
2009 * End the current ActionMode.
2010 */
2011 @Override
2012 public void endActionMode() {
2013 if (mActionMode != null) {
2014 mActionMode.finish();
2015 }
2016 }
2017
2018 /*
2019 * Called by find and select when they are finished. Replace title bars
2020 * as necessary.
2021 */
John Reck9c35b9c2012-05-30 10:08:50 -07002022 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002023 public void onActionModeFinished(ActionMode mode) {
2024 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08002025 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07002026 mActionMode = null;
2027 }
2028
2029 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08002030 final Tab tab = getCurrentTab();
2031 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07002032 }
2033
2034 // bookmark handling
2035
2036 /**
2037 * add the current page as a bookmark to the given folder id
2038 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07002039 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002040 * bookmarked, and if it is, edit that bookmark. If false, and
2041 * the site is already bookmarked, do not attempt to edit the
2042 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07002043 */
2044 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07002045 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07002046 WebView w = getCurrentTopWebView();
2047 if (w == null) {
2048 return null;
2049 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002050 Intent i = new Intent(mActivity,
2051 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07002052 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
2053 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
2054 String touchIconUrl = w.getTouchIconUrl();
2055 if (touchIconUrl != null) {
2056 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
2057 WebSettings settings = w.getSettings();
2058 if (settings != null) {
2059 i.putExtra(AddBookmarkPage.USER_AGENT,
2060 settings.getUserAgentString());
2061 }
2062 }
2063 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2064 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2065 getDesiredThumbnailHeight(mActivity)));
2066 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002067 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002068 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2069 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002070 // Put the dialog at the upper right of the screen, covering the
2071 // star on the title bar.
2072 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002073 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002074 }
2075
2076 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002077 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002078 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002079 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002080 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002081 }
2082
2083 // thumbnails
2084
2085 /**
2086 * Return the desired width for thumbnail screenshots, which are stored in
2087 * the database, and used on the bookmarks screen.
2088 * @param context Context for finding out the density of the screen.
2089 * @return desired width for thumbnail screenshot.
2090 */
2091 static int getDesiredThumbnailWidth(Context context) {
2092 return context.getResources().getDimensionPixelOffset(
2093 R.dimen.bookmarkThumbnailWidth);
2094 }
2095
2096 /**
2097 * Return the desired height for thumbnail screenshots, which are stored in
2098 * the database, and used on the bookmarks screen.
2099 * @param context Context for finding out the density of the screen.
2100 * @return desired height for thumbnail screenshot.
2101 */
2102 static int getDesiredThumbnailHeight(Context context) {
2103 return context.getResources().getDimensionPixelOffset(
2104 R.dimen.bookmarkThumbnailHeight);
2105 }
2106
John Reck8cc92352011-07-06 17:41:52 -07002107 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002108 if (view == null || view.getContentHeight() == 0
2109 || view.getContentWidth() == 0) {
2110 return null;
2111 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002112 // We render to a bitmap 2x the desired size so that we can then
2113 // re-scale it with filtering since canvas.scale doesn't filter
2114 // This helps reduce aliasing at the cost of being slightly blurry
2115 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002116 int scaledWidth = width * filter_scale;
2117 int scaledHeight = height * filter_scale;
2118 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2119 || sThumbnailBitmap.getHeight() != scaledHeight) {
2120 if (sThumbnailBitmap != null) {
2121 sThumbnailBitmap.recycle();
2122 sThumbnailBitmap = null;
2123 }
2124 sThumbnailBitmap =
2125 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002126 }
John Reckd7dd9b22011-08-30 09:18:29 -07002127 Canvas canvas = new Canvas(sThumbnailBitmap);
2128 int contentWidth = view.getContentWidth();
2129 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2130 if (view instanceof BrowserWebView) {
2131 int dy = -((BrowserWebView)view).getTitleHeight();
2132 canvas.translate(0, dy * overviewScale);
2133 }
2134
2135 canvas.scale(overviewScale, overviewScale);
2136
2137 if (view instanceof BrowserWebView) {
2138 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002139 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002140 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002141 }
John Reckd7dd9b22011-08-30 09:18:29 -07002142 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2143 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002144 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002145 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002146 }
2147
John Reck34ef2672011-02-10 11:30:55 -08002148 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002149 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002150 // FIXME: Would like to make sure there is actually something to
2151 // draw, but the API for that (WebViewCore.pictureReady()) is not
2152 // currently accessible here.
2153
John Reck34ef2672011-02-10 11:30:55 -08002154 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002155 if (view == null) {
2156 // Tab was destroyed
2157 return;
2158 }
John Reck34ef2672011-02-10 11:30:55 -08002159 final String url = tab.getUrl();
2160 final String originalUrl = view.getOriginalUrl();
John Reck34ef2672011-02-10 11:30:55 -08002161 if (TextUtils.isEmpty(url)) {
2162 return;
2163 }
2164
2165 // Only update thumbnails for web urls (http(s)://), not for
2166 // about:, javascript:, data:, etc...
2167 // Unless it is a bookmarked site, then always update
2168 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2169 return;
2170 }
2171
Michael Kolb8233fac2010-10-26 16:08:53 -07002172 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2173 getDesiredThumbnailHeight(mActivity));
2174 if (bm == null) {
2175 return;
2176 }
2177
2178 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002179 new AsyncTask<Void, Void, Void>() {
2180 @Override
2181 protected Void doInBackground(Void... unused) {
2182 Cursor cursor = null;
2183 try {
2184 // TODO: Clean this up
2185 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2186 if (cursor != null && cursor.moveToFirst()) {
2187 final ByteArrayOutputStream os =
2188 new ByteArrayOutputStream();
2189 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002190
John Reck34ef2672011-02-10 11:30:55 -08002191 ContentValues values = new ContentValues();
2192 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002193
John Reck34ef2672011-02-10 11:30:55 -08002194 do {
John Reck617fd832011-02-16 14:35:59 -08002195 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002196 cr.update(Images.CONTENT_URI, values, null, null);
2197 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002198 }
John Reck34ef2672011-02-10 11:30:55 -08002199 } catch (IllegalStateException e) {
2200 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002201 } catch (SQLiteException s) {
2202 // Added for possible error when user tries to remove the same bookmark
2203 // that is being updated with a screen shot
2204 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002205 } finally {
2206 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002207 }
John Reck34ef2672011-02-10 11:30:55 -08002208 return null;
2209 }
2210 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002211 }
2212
2213 private class Copy implements OnMenuItemClickListener {
2214 private CharSequence mText;
2215
John Reck9c35b9c2012-05-30 10:08:50 -07002216 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002217 public boolean onMenuItemClick(MenuItem item) {
2218 copy(mText);
2219 return true;
2220 }
2221
2222 public Copy(CharSequence toCopy) {
2223 mText = toCopy;
2224 }
2225 }
2226
Leon Scroggins63c02662010-11-18 15:16:27 -05002227 private static class Download implements OnMenuItemClickListener {
2228 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002229 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002230 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002231 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002232 private static final String FALLBACK_EXTENSION = "dat";
2233 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002234
John Reck9c35b9c2012-05-30 10:08:50 -07002235 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002236 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002237 if (DataUri.isDataUri(mText)) {
2238 saveDataUri();
2239 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002240 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07002241 null, null, null, mPrivateBrowsing);
George Mount387d45d2011-10-07 15:57:53 -07002242 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002243 return true;
2244 }
2245
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002246 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2247 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002248 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002249 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002250 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002251 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002252 }
George Mount387d45d2011-10-07 15:57:53 -07002253
2254 /**
2255 * Treats mText as a data URI and writes its contents to a file
2256 * based on the current time.
2257 */
2258 private void saveDataUri() {
2259 FileOutputStream outputStream = null;
2260 try {
2261 DataUri uri = new DataUri(mText);
2262 File target = getTarget(uri);
2263 outputStream = new FileOutputStream(target);
2264 outputStream.write(uri.getData());
2265 final DownloadManager manager =
2266 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2267 manager.addCompletedDownload(target.getName(),
2268 mActivity.getTitle().toString(), false,
2269 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002270 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002271 } catch (IOException e) {
2272 Log.e(LOGTAG, "Could not save data URL");
2273 } finally {
2274 if (outputStream != null) {
2275 try {
2276 outputStream.close();
2277 } catch (IOException e) {
2278 // ignore close errors
2279 }
2280 }
2281 }
2282 }
2283
2284 /**
2285 * Creates a File based on the current time stamp and uses
2286 * the mime type of the DataUri to get the extension.
2287 */
2288 private File getTarget(DataUri uri) throws IOException {
2289 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
Johan Redestigaa676182012-10-03 13:33:01 +02002290 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT, Locale.US);
George Mount387d45d2011-10-07 15:57:53 -07002291 String nameBase = format.format(new Date());
2292 String mimeType = uri.getMimeType();
2293 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2294 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2295 if (extension == null) {
2296 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2297 extension = FALLBACK_EXTENSION;
2298 }
2299 extension = "." + extension; // createTempFile needs the '.'
2300 File targetFile = File.createTempFile(nameBase, extension, dir);
2301 return targetFile;
2302 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002303 }
2304
Cary Clark8974d282010-11-22 10:46:05 -05002305 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002306 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002307
John Reck9c35b9c2012-05-30 10:08:50 -07002308 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002309 public boolean onMenuItemClick(MenuItem item) {
2310 if (mWebView != null) {
2311 return mWebView.selectText();
2312 }
2313 return false;
2314 }
2315
2316 public SelectText(WebView webView) {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002317 if (BrowserWebView.isClassic()) {
2318 mWebView = WebViewClassic.fromWebView(webView);
2319 }
Cary Clark8974d282010-11-22 10:46:05 -05002320 }
2321
2322 }
2323
Michael Kolb8233fac2010-10-26 16:08:53 -07002324 /********************** TODO: UI stuff *****************************/
2325
2326 // these methods have been copied, they still need to be cleaned up
2327
2328 /****************** tabs ***************************************************/
2329
2330 // basic tab interactions:
2331
2332 // it is assumed that tabcontrol already knows about the tab
2333 protected void addTab(Tab tab) {
2334 mUi.addTab(tab);
2335 }
2336
2337 protected void removeTab(Tab tab) {
2338 mUi.removeTab(tab);
2339 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002340 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002341 }
2342
Michael Kolbf2055602011-04-09 17:20:03 -07002343 @Override
2344 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002345 // monkey protection against delayed start
2346 if (tab != null) {
2347 mTabControl.setCurrentTab(tab);
2348 // the tab is guaranteed to have a webview after setCurrentTab
2349 mUi.setActiveTab(tab);
2350 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002351 }
2352
John Reck8bcafc12011-08-29 16:43:02 -07002353 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002354 Tab current = mTabControl.getCurrentTab();
2355 if (current != null
2356 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002357 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002358 }
2359 }
2360
John Reck26b18322011-06-21 13:08:58 -07002361 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002362 // Dismiss the subwindow if applicable.
2363 dismissSubWindow(appTab);
2364 // Since we might kill the WebView, remove it from the
2365 // content view first.
2366 mUi.detachTab(appTab);
2367 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002368 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002369 // TODO: analyze why the remove and add are necessary
2370 mUi.attachTab(appTab);
2371 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002372 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002373 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002374 } else {
2375 // If the tab was the current tab, we have to attach
2376 // it to the view system again.
2377 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002378 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002379 }
2380 }
2381
2382 // Remove the sub window if it exists. Also called by TabControl when the
2383 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002384 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002385 public void dismissSubWindow(Tab tab) {
2386 removeSubWindow(tab);
2387 // dismiss the subwindow. This will destroy the WebView.
2388 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002389 WebView wv = getCurrentTopWebView();
2390 if (wv != null) {
2391 wv.requestFocus();
2392 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002393 }
2394
2395 @Override
2396 public void removeSubWindow(Tab t) {
2397 if (t.getSubWebView() != null) {
2398 mUi.removeSubWindow(t.getSubViewContainer());
2399 }
2400 }
2401
2402 @Override
2403 public void attachSubWindow(Tab tab) {
2404 if (tab.getSubWebView() != null) {
2405 mUi.attachSubWindow(tab.getSubViewContainer());
2406 getCurrentTopWebView().requestFocus();
2407 }
2408 }
2409
Mathew Inwood29721c22011-06-29 17:55:24 +01002410 private Tab showPreloadedTab(final UrlData urlData) {
2411 if (!urlData.isPreloaded()) {
2412 return null;
2413 }
2414 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2415 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2416 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002417 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002418 // Could not submit query. Fallback to regular tab creation
2419 tabControl.destroy();
2420 return null;
2421 }
2422 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002423 // check tab count and make room for new tab
2424 if (!mTabControl.canCreateNewTab()) {
2425 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2426 if (leastUsed != null) {
2427 closeTab(leastUsed);
2428 }
2429 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002430 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002431 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002432 mTabControl.addPreloadedTab(t);
2433 addTab(t);
2434 setActiveTab(t);
2435 return t;
2436 }
2437
Michael Kolb7bcafde2011-05-09 13:55:59 -07002438 // open a non inconito tab with the given url data
2439 // and set as active tab
2440 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002441 Tab tab = showPreloadedTab(urlData);
2442 if (tab == null) {
2443 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002444 if ((tab != null) && !urlData.isEmpty()) {
2445 loadUrlDataIn(tab, urlData);
2446 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002447 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002448 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002449 }
2450
Michael Kolb843510f2010-12-09 10:51:49 -08002451 @Override
2452 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002453 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002454 }
2455
Michael Kolb8233fac2010-10-26 16:08:53 -07002456 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002457 public Tab openIncognitoTab() {
2458 return openTab(INCOGNITO_URI, true, true, false);
2459 }
2460
2461 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002462 public Tab openTab(String url, boolean incognito, boolean setActive,
2463 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002464 return openTab(url, incognito, setActive, useCurrent, null);
2465 }
2466
2467 @Override
2468 public Tab openTab(String url, Tab parent, boolean setActive,
2469 boolean useCurrent) {
2470 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2471 setActive, useCurrent, parent);
2472 }
2473
2474 public Tab openTab(String url, boolean incognito, boolean setActive,
2475 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002476 Tab tab = createNewTab(incognito, setActive, useCurrent);
2477 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002478 if (parent != null && parent != tab) {
2479 parent.addChildTab(tab);
2480 }
Michael Kolb519d2282011-05-09 17:03:19 -07002481 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002482 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002483 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002484 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002485 return tab;
2486 }
2487
2488 // this method will attempt to create a new tab
2489 // incognito: private browsing tab
2490 // setActive: ste tab as current tab
2491 // useCurrent: if no new tab can be created, return current tab
2492 private Tab createNewTab(boolean incognito, boolean setActive,
2493 boolean useCurrent) {
2494 Tab tab = null;
2495 if (mTabControl.canCreateNewTab()) {
2496 tab = mTabControl.createNewTab(incognito);
2497 addTab(tab);
2498 if (setActive) {
2499 setActiveTab(tab);
2500 }
2501 } else {
2502 if (useCurrent) {
2503 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002504 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002505 } else {
2506 mUi.showMaxTabsWarning();
2507 }
2508 }
2509 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002510 }
2511
John Reck2bc80422011-06-30 15:11:49 -07002512 @Override
2513 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2514 SnapshotTab tab = null;
2515 if (mTabControl.canCreateNewTab()) {
2516 tab = mTabControl.createSnapshotTab(snapshotId);
2517 addTab(tab);
2518 if (setActive) {
2519 setActiveTab(tab);
2520 }
2521 } else {
2522 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002523 }
2524 return tab;
2525 }
2526
Michael Kolb8233fac2010-10-26 16:08:53 -07002527 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002528 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002529 * @return boolean True if we successfully switched to a different tab. If
2530 * the indexth tab is null, or if that tab is the same as
2531 * the current one, return false.
2532 */
2533 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002534 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002535 Tab currentTab = mTabControl.getCurrentTab();
2536 if (tab == null || tab == currentTab) {
2537 return false;
2538 }
2539 setActiveTab(tab);
2540 return true;
2541 }
2542
2543 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002544 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002545 closeCurrentTab(false);
2546 }
2547
2548 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002549 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002550 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002551 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002552 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002553 return;
2554 }
Michael Kolbc831b632011-05-11 09:30:34 -07002555 final Tab current = mTabControl.getCurrentTab();
2556 final int pos = mTabControl.getCurrentPosition();
2557 Tab newTab = current.getParent();
2558 if (newTab == null) {
2559 newTab = mTabControl.getTab(pos + 1);
2560 if (newTab == null) {
2561 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002562 }
2563 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002564 if (andQuit) {
2565 mTabControl.setCurrentTab(newTab);
2566 closeTab(current);
2567 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002568 // Close window
2569 closeTab(current);
2570 }
2571 }
2572
2573 /**
2574 * Close the tab, remove its associated title bar, and adjust mTabControl's
2575 * current tab to a valid value.
2576 */
2577 @Override
2578 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002579 if (tab == mTabControl.getCurrentTab()) {
2580 closeCurrentTab();
2581 } else {
2582 removeTab(tab);
2583 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002584 }
2585
Afzal Najamd4e33312012-04-26 01:54:01 -04002586 /**
2587 * Close all tabs except the current one
2588 */
2589 @Override
2590 public void closeOtherTabs() {
2591 int inactiveTabs = mTabControl.getTabCount() - 1;
2592 for (int i = inactiveTabs; i >= 0; i--) {
2593 Tab tab = mTabControl.getTab(i);
2594 if (tab != mTabControl.getCurrentTab()) {
2595 removeTab(tab);
2596 }
2597 }
2598 }
2599
Michael Kolb8233fac2010-10-26 16:08:53 -07002600 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002601 protected void loadUrlFromContext(String url) {
2602 Tab tab = getCurrentTab();
2603 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002604 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002605 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002606 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002607 if (!((BrowserWebView) view).getWebViewClient().
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002608 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002609 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002610 }
2611 }
2612 }
2613
2614 /**
2615 * Load the URL into the given WebView and update the title bar
2616 * to reflect the new load. Call this instead of WebView.loadUrl
2617 * directly.
2618 * @param view The WebView used to load url.
2619 * @param url The URL to load.
2620 */
John Reck71e51422011-07-01 16:49:28 -07002621 @Override
2622 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002623 loadUrl(tab, url, null);
2624 }
2625
2626 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2627 if (tab != null) {
2628 dismissSubWindow(tab);
2629 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002630 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002631 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002632 }
2633
2634 /**
2635 * Load UrlData into a Tab and update the title bar to reflect the new
2636 * load. Call this instead of UrlData.loadIn directly.
2637 * @param t The Tab used to load.
2638 * @param data The UrlData being loaded.
2639 */
2640 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002641 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002642 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002643 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002644 } else {
John Reck38b39652012-06-05 09:22:59 -07002645 if (t != null && data.mDisableUrlOverride) {
2646 t.disableUrlOverridingForLoad();
2647 }
John Reck26b18322011-06-21 13:08:58 -07002648 loadUrl(t, data.mUrl, data.mHeaders);
2649 }
2650 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002651 }
2652
John Reck30c714c2010-12-16 17:30:34 -08002653 @Override
2654 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002655 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002656 if (tab.canGoBack()) {
2657 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002658 } else {
John Reckef654f12011-07-12 16:42:08 -07002659 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002660 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002661 }
2662
2663 void goBackOnePageOrQuit() {
2664 Tab current = mTabControl.getCurrentTab();
2665 if (current == null) {
2666 /*
2667 * Instead of finishing the activity, simply push this to the back
2668 * of the stack and let ActivityManager to choose the foreground
2669 * activity. As BrowserActivity is singleTask, it will be always the
2670 * root of the task. So we can use either true or false for
2671 * moveTaskToBack().
2672 */
luxiaolb40014b2013-07-19 10:01:43 +08002673 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07002674 return;
2675 }
John Reckef654f12011-07-12 16:42:08 -07002676 if (current.canGoBack()) {
2677 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002678 } else {
2679 // Check to see if we are closing a window that was created by
2680 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002681 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002682 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002683 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002684 // Now we close the other tab
2685 closeTab(current);
2686 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07002687 /*
2688 * Instead of finishing the activity, simply push this to the back
2689 * of the stack and let ActivityManager to choose the foreground
2690 * activity. As BrowserActivity is singleTask, it will be always the
2691 * root of the task. So we can use either true or false for
2692 * moveTaskToBack().
2693 */
luxiaolb40014b2013-07-19 10:01:43 +08002694 showExitDialog(mActivity);
Michael Kolb8233fac2010-10-26 16:08:53 -07002695 }
2696 }
2697 }
2698
2699 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002700 * helper method for key handler
2701 * returns the current tab if it can't advance
2702 */
Michael Kolbc831b632011-05-11 09:30:34 -07002703 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002704 int pos = mTabControl.getCurrentPosition() + 1;
2705 if (pos >= mTabControl.getTabCount()) {
2706 pos = 0;
2707 }
2708 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002709 }
2710
2711 /**
2712 * helper method for key handler
2713 * returns the current tab if it can't advance
2714 */
Michael Kolbc831b632011-05-11 09:30:34 -07002715 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002716 int pos = mTabControl.getCurrentPosition() - 1;
2717 if ( pos < 0) {
2718 pos = mTabControl.getTabCount() - 1;
2719 }
2720 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002721 }
2722
John Reckbcef87f2012-02-03 14:58:34 -08002723 boolean isMenuOrCtrlKey(int keyCode) {
2724 return (KeyEvent.KEYCODE_MENU == keyCode)
2725 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2726 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2727 }
2728
Michael Kolb0035fad2011-03-14 13:25:28 -07002729 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002730 * handle key events in browser
2731 *
2732 * @param keyCode
2733 * @param event
2734 * @return true if handled, false to pass to super
2735 */
John Reck9c35b9c2012-05-30 10:08:50 -07002736 @Override
2737 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002738 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002739 // Even if MENU is already held down, we need to call to super to open
2740 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002741 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002742 mMenuIsDown = true;
2743 return false;
2744 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002745
Cary Clark8ff8c662010-12-29 15:03:05 -05002746 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002747 Tab tab = getCurrentTab();
2748 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002749
Cary Clark160bbb92011-01-10 11:17:07 -05002750 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2751 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002752
Michael Kolb8233fac2010-10-26 16:08:53 -07002753 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002754 case KeyEvent.KEYCODE_TAB:
2755 if (event.isCtrlPressed()) {
2756 if (event.isShiftPressed()) {
2757 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002758 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002759 } else {
2760 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002761 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002762 }
2763 return true;
2764 }
2765 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002766 case KeyEvent.KEYCODE_SPACE:
2767 // WebView/WebTextView handle the keys in the KeyDown. As
2768 // the Activity's shortcut keys are only handled when WebView
2769 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002770 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002771 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002772 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002773 pageDown();
2774 }
2775 return true;
2776 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002777 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002778 event.startTracking();
2779 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002780 case KeyEvent.KEYCODE_FORWARD:
2781 if (!noModifiers) break;
2782 tab.goForward();
2783 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002784 case KeyEvent.KEYCODE_DPAD_LEFT:
2785 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002786 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002787 return true;
2788 }
2789 break;
2790 case KeyEvent.KEYCODE_DPAD_RIGHT:
2791 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002792 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002793 return true;
2794 }
2795 break;
2796 case KeyEvent.KEYCODE_A:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002797 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002798 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002799 return true;
2800 }
2801 break;
Michael Kolba4183062011-01-16 10:43:21 -08002802// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002803 case KeyEvent.KEYCODE_C:
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08002804 if (ctrl && BrowserWebView.isClassic()) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002805 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002806 return true;
2807 }
2808 break;
Michael Kolba4183062011-01-16 10:43:21 -08002809// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002810// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002811// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002812// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002813// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002814// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002815// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002816// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002817// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002818// case KeyEvent.KEYCODE_M: // unused
2819// case KeyEvent.KEYCODE_N: // in Chrome: new window
2820// case KeyEvent.KEYCODE_O: // in Chrome: open file
2821// case KeyEvent.KEYCODE_P: // in Chrome: print page
2822// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002823// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002824// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2825 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002826 // we can't use the ctrl/shift flags, they check for
2827 // exclusive use of a modifier
2828 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002829 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002830 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002831 } else {
2832 openTabToHomePage();
2833 }
2834 return true;
2835 }
2836 break;
2837// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2838// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002839// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002840// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2841// case KeyEvent.KEYCODE_Y: // unused
2842// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002843 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002844 // it is a regular key and webview is not null
2845 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002846 }
2847
John Reck9c35b9c2012-05-30 10:08:50 -07002848 @Override
2849 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002850 switch(keyCode) {
2851 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002852 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002853 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002854 return true;
2855 }
2856 break;
2857 }
2858 return false;
2859 }
2860
John Reck9c35b9c2012-05-30 10:08:50 -07002861 @Override
2862 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002863 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002864 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002865 if (KeyEvent.KEYCODE_MENU == keyCode
2866 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002867 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002868 }
2869 }
Cary Clark160bbb92011-01-10 11:17:07 -05002870 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002871 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002872 case KeyEvent.KEYCODE_BACK:
2873 if (event.isTracking() && !event.isCanceled()) {
2874 onBackKey();
2875 return true;
2876 }
2877 break;
2878 }
2879 return false;
2880 }
2881
2882 public boolean isMenuDown() {
2883 return mMenuIsDown;
2884 }
2885
John Reck9c35b9c2012-05-30 10:08:50 -07002886 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002887 public void setupAutoFill(Message message) {
2888 // Open the settings activity at the AutoFill profile fragment so that
2889 // the user can create a new profile. When they return, we will dispatch
2890 // the message so that we can autofill the form using their new profile.
2891 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2892 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2893 AutoFillSettingsFragment.class.getName());
2894 mAutoFillSetupMessage = message;
2895 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2896 }
John Reckb3417f02011-01-14 11:01:05 -08002897
John Reck9c35b9c2012-05-30 10:08:50 -07002898 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002899 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002900 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002901 return true;
2902 }
2903
John Reck1cf4b792011-07-26 10:22:22 -07002904 @Override
2905 public boolean shouldCaptureThumbnails() {
2906 return mUi.shouldCaptureThumbnails();
2907 }
2908
Michael Kolbc3af0672011-08-09 10:24:41 -07002909 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07002910 public boolean supportsVoice() {
2911 PackageManager pm = mActivity.getPackageManager();
2912 List activities = pm.queryIntentActivities(new Intent(
2913 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
2914 return activities.size() != 0;
2915 }
2916
2917 @Override
2918 public void startVoiceRecognizer() {
2919 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
2920 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
2921 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
2922 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
2923 mActivity.startActivityForResult(voice, VOICE_RESULT);
2924 }
2925
2926 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002927 public void setBlockEvents(boolean block) {
2928 mBlockEvents = block;
2929 }
2930
John Reck9c35b9c2012-05-30 10:08:50 -07002931 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002932 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002933 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002934 }
2935
John Reck9c35b9c2012-05-30 10:08:50 -07002936 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002937 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002938 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002939 }
2940
John Reck9c35b9c2012-05-30 10:08:50 -07002941 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002942 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002943 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002944 }
2945
John Reck9c35b9c2012-05-30 10:08:50 -07002946 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002947 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002948 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002949 }
2950
John Reck9c35b9c2012-05-30 10:08:50 -07002951 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002952 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002953 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002954 }
2955
Michael Kolb8233fac2010-10-26 16:08:53 -07002956}