blob: 56a9e20964cf231791cc1e074e80b8d25e7132e4 [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;
John Reck68234a92012-04-19 15:27:12 -070020import android.app.Dialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.app.DownloadManager;
John Reck68234a92012-04-19 15:27:12 -070022import android.app.ProgressDialog;
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.content.ClipboardManager;
Michael Kolb8233fac2010-10-26 16:08:53 -070024import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070025import android.content.ContentUris;
Michael Kolb8233fac2010-10-26 16:08:53 -070026import android.content.ContentValues;
27import android.content.Context;
John Reck68234a92012-04-19 15:27:12 -070028import android.content.DialogInterface;
29import android.content.DialogInterface.OnCancelListener;
Michael Kolb8233fac2010-10-26 16:08:53 -070030import android.content.Intent;
31import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
33import android.content.res.Configuration;
John Reck30b065e2011-07-19 10:58:05 -070034import android.content.res.TypedArray;
Leon Scroggins1961ed22010-12-07 15:22:21 -050035import android.database.ContentObserver;
Michael Kolb8233fac2010-10-26 16:08:53 -070036import android.database.Cursor;
37import android.database.sqlite.SQLiteDatabase;
Mattias Nilsson561d1952011-10-04 10:18:50 +020038import android.database.sqlite.SQLiteException;
Michael Kolb8233fac2010-10-26 16:08:53 -070039import android.graphics.Bitmap;
40import android.graphics.Canvas;
Michael Kolb8233fac2010-10-26 16:08:53 -070041import android.net.Uri;
42import android.net.http.SslError;
43import android.os.AsyncTask;
44import android.os.Bundle;
George Mount387d45d2011-10-07 15:57:53 -070045import android.os.Environment;
Michael Kolb8233fac2010-10-26 16:08:53 -070046import android.os.Handler;
47import android.os.Message;
48import android.os.PowerManager;
49import android.os.PowerManager.WakeLock;
Ben Murdoch8029a772010-11-16 11:58:21 +000050import android.preference.PreferenceActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -070051import android.provider.Browser;
52import android.provider.BrowserContract;
Michael Kolb8233fac2010-10-26 16:08:53 -070053import android.provider.BrowserContract.Images;
54import android.provider.ContactsContract;
55import android.provider.ContactsContract.Intents.Insert;
Michael Kolb0b129122012-06-04 16:31:58 -070056import android.speech.RecognizerIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070057import android.text.TextUtils;
58import android.util.Log;
John Recka00cbbd2010-12-16 12:38:19 -080059import android.util.Patterns;
Michael Kolb8233fac2010-10-26 16:08:53 -070060import android.view.ActionMode;
61import android.view.ContextMenu;
62import android.view.ContextMenu.ContextMenuInfo;
63import android.view.Gravity;
64import android.view.KeyEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070065import android.view.Menu;
66import android.view.MenuInflater;
67import android.view.MenuItem;
68import android.view.MenuItem.OnMenuItemClickListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070069import android.view.MotionEvent;
Michael Kolb8233fac2010-10-26 16:08:53 -070070import android.view.View;
71import android.webkit.CookieManager;
72import android.webkit.CookieSyncManager;
73import android.webkit.HttpAuthHandler;
George Mount387d45d2011-10-07 15:57:53 -070074import android.webkit.MimeTypeMap;
Michael Kolb8233fac2010-10-26 16:08:53 -070075import android.webkit.SslErrorHandler;
76import android.webkit.ValueCallback;
77import android.webkit.WebChromeClient;
78import android.webkit.WebIconDatabase;
79import android.webkit.WebSettings;
80import android.webkit.WebView;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000081import android.webkit.WebViewClassic;
Leon Scrogginsac993842011-02-02 12:54:07 -050082import android.widget.Toast;
Michael Kolb8233fac2010-10-26 16:08:53 -070083
Michael Kolb4bd767d2011-05-27 11:33:55 -070084import com.android.browser.IntentHandler.UrlData;
John Reck2bc80422011-06-30 15:11:49 -070085import com.android.browser.UI.ComboViews;
John Reck1cf4b792011-07-26 10:22:22 -070086import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070087import com.android.browser.provider.SnapshotProvider.Snapshots;
Michael Kolb4bd767d2011-05-27 11:33:55 -070088
Michael Kolb8233fac2010-10-26 16:08:53 -070089import java.io.ByteArrayOutputStream;
George Mount387d45d2011-10-07 15:57:53 -070090import java.io.File;
91import java.io.FileOutputStream;
92import java.io.IOException;
Michael Kolb8233fac2010-10-26 16:08:53 -070093import java.net.URLEncoder;
George Mount387d45d2011-10-07 15:57:53 -070094import java.text.DateFormat;
95import java.text.SimpleDateFormat;
John Reck1cf4b792011-07-26 10:22:22 -070096import java.util.ArrayList;
Michael Kolb8233fac2010-10-26 16:08:53 -070097import java.util.Calendar;
George Mount387d45d2011-10-07 15:57:53 -070098import java.util.Date;
Michael Kolb8233fac2010-10-26 16:08:53 -070099import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -0800100import java.util.List;
John Reck26b18322011-06-21 13:08:58 -0700101import java.util.Map;
Michael Kolb8233fac2010-10-26 16:08:53 -0700102
103/**
104 * Controller for browser
105 */
106public class Controller
John Reck9c35b9c2012-05-30 10:08:50 -0700107 implements WebViewController, UiController, ActivityController {
Michael Kolb8233fac2010-10-26 16:08:53 -0700108
109 private static final String LOGTAG = "Controller";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800110 private static final String SEND_APP_ID_EXTRA =
111 "android.speech.extras.SEND_APPLICATION_ID_EXTRA";
Michael Kolba4261fd2011-05-05 11:27:37 -0700112 private static final String INCOGNITO_URI = "browser:incognito";
Michael Kolbcfa3af52010-12-14 10:36:11 -0800113
Michael Kolb8233fac2010-10-26 16:08:53 -0700114
115 // public message ids
116 public final static int LOAD_URL = 1001;
117 public final static int STOP_LOAD = 1002;
118
119 // Message Ids
120 private static final int FOCUS_NODE_HREF = 102;
121 private static final int RELEASE_WAKELOCK = 107;
122
123 static final int UPDATE_BOOKMARK_THUMBNAIL = 108;
124
125 private static final int OPEN_BOOKMARKS = 201;
126
127 private static final int EMPTY_MENU = -1;
128
Michael Kolb8233fac2010-10-26 16:08:53 -0700129 // activity requestCode
John Reckd3e4d5b2011-07-13 15:48:43 -0700130 final static int COMBO_VIEW = 1;
Michael Kolb8233fac2010-10-26 16:08:53 -0700131 final static int PREFERENCES_PAGE = 3;
132 final static int FILE_SELECTED = 4;
Ben Murdoch8029a772010-11-16 11:58:21 +0000133 final static int AUTOFILL_SETUP = 5;
Michael Kolb0b129122012-06-04 16:31:58 -0700134 final static int VOICE_RESULT = 6;
Ben Murdoch8029a772010-11-16 11:58:21 +0000135
Michael Kolb8233fac2010-10-26 16:08:53 -0700136 private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
137
138 // As the ids are dynamically created, we can't guarantee that they will
139 // be in sequence, so this static array maps ids to a window number.
140 final static private int[] WINDOW_SHORTCUT_ID_ARRAY =
141 { R.id.window_one_menu_id, R.id.window_two_menu_id,
142 R.id.window_three_menu_id, R.id.window_four_menu_id,
143 R.id.window_five_menu_id, R.id.window_six_menu_id,
144 R.id.window_seven_menu_id, R.id.window_eight_menu_id };
145
146 // "source" parameter for Google search through search key
147 final static String GOOGLE_SEARCH_SOURCE_SEARCHKEY = "browser-key";
148 // "source" parameter for Google search through simplily type
149 final static String GOOGLE_SEARCH_SOURCE_TYPE = "browser-type";
150
George Mount387d45d2011-10-07 15:57:53 -0700151 // "no-crash-recovery" parameter in intent to suppress crash recovery
Guang Zhu9e78f512011-05-04 11:45:11 -0700152 final static String NO_CRASH_RECOVERY = "no-crash-recovery";
153
John Reckd7dd9b22011-08-30 09:18:29 -0700154 // A bitmap that is re-used in createScreenshot as scratch space
155 private static Bitmap sThumbnailBitmap;
156
Michael Kolb8233fac2010-10-26 16:08:53 -0700157 private Activity mActivity;
158 private UI mUi;
159 private TabControl mTabControl;
160 private BrowserSettings mSettings;
161 private WebViewFactory mFactory;
162
163 private WakeLock mWakeLock;
164
165 private UrlHandler mUrlHandler;
166 private UploadHandler mUploadHandler;
167 private IntentHandler mIntentHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700168 private PageDialogsHandler mPageDialogsHandler;
169 private NetworkStateHandler mNetworkHandler;
170
Ben Murdoch8029a772010-11-16 11:58:21 +0000171 private Message mAutoFillSetupMessage;
172
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 private boolean mShouldShowErrorConsole;
174
175 private SystemAllowGeolocationOrigins mSystemAllowGeolocationOrigins;
176
177 // FIXME, temp address onPrepareMenu performance problem.
178 // When we move everything out of view, we should rewrite this.
179 private int mCurrentMenuState = 0;
180 private int mMenuState = R.id.MAIN_MENU;
181 private int mOldMenuState = EMPTY_MENU;
182 private Menu mCachedMenu;
183
Michael Kolb8233fac2010-10-26 16:08:53 -0700184 private boolean mMenuIsDown;
185
186 // For select and find, we keep track of the ActionMode so that
187 // finish() can be called as desired.
188 private ActionMode mActionMode;
189
190 /**
191 * Only meaningful when mOptionsMenuOpen is true. This variable keeps track
192 * of whether the configuration has changed. The first onMenuOpened call
193 * after a configuration change is simply a reopening of the same menu
194 * (i.e. mIconView did not change).
195 */
196 private boolean mConfigChanged;
197
198 /**
199 * Keeps track of whether the options menu is open. This is important in
200 * determining whether to show or hide the title bar overlay
201 */
202 private boolean mOptionsMenuOpen;
203
204 /**
205 * Whether or not the options menu is in its bigger, popup menu form. When
206 * true, we want the title bar overlay to be gone. When false, we do not.
207 * Only meaningful if mOptionsMenuOpen is true.
208 */
209 private boolean mExtendedMenuOpen;
210
Michael Kolb8233fac2010-10-26 16:08:53 -0700211 private boolean mActivityPaused = true;
212 private boolean mLoadStopped;
213
214 private Handler mHandler;
Leon Scroggins1961ed22010-12-07 15:22:21 -0500215 // Checks to see when the bookmarks database has changed, and updates the
216 // Tabs' notion of whether they represent bookmarked sites.
217 private ContentObserver mBookmarksObserver;
John Reck847b5322011-04-14 17:02:18 -0700218 private CrashRecoveryHandler mCrashRecoveryHandler;
Michael Kolb8233fac2010-10-26 16:08:53 -0700219
Michael Kolbc3af0672011-08-09 10:24:41 -0700220 private boolean mBlockEvents;
221
Michael Kolb0b129122012-06-04 16:31:58 -0700222 private String mVoiceResult;
223
George Mount3636d0a2011-11-21 09:08:21 -0800224 public Controller(Activity browser) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700225 mActivity = browser;
226 mSettings = BrowserSettings.getInstance();
227 mTabControl = new TabControl(this);
228 mSettings.setController(this);
John Reck378a4102011-06-09 16:23:01 -0700229 mCrashRecoveryHandler = CrashRecoveryHandler.initialize(this);
George Mount3636d0a2011-11-21 09:08:21 -0800230 mCrashRecoveryHandler.preloadCrashState();
Michael Kolb14612442011-06-24 13:06:29 -0700231 mFactory = new BrowserWebViewFactory(browser);
Michael Kolb8233fac2010-10-26 16:08:53 -0700232
233 mUrlHandler = new UrlHandler(this);
234 mIntentHandler = new IntentHandler(mActivity, this);
Michael Kolb8233fac2010-10-26 16:08:53 -0700235 mPageDialogsHandler = new PageDialogsHandler(mActivity, this);
236
Michael Kolb8233fac2010-10-26 16:08:53 -0700237 startHandler();
Leon Scroggins1961ed22010-12-07 15:22:21 -0500238 mBookmarksObserver = new ContentObserver(mHandler) {
239 @Override
240 public void onChange(boolean selfChange) {
241 int size = mTabControl.getTabCount();
242 for (int i = 0; i < size; i++) {
243 mTabControl.getTab(i).updateBookmarkedStatus();
244 }
245 }
246
247 };
248 browser.getContentResolver().registerContentObserver(
249 BrowserContract.Bookmarks.CONTENT_URI, true, mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700250
251 mNetworkHandler = new NetworkStateHandler(mActivity, this);
252 // Start watching the default geolocation permissions
253 mSystemAllowGeolocationOrigins =
254 new SystemAllowGeolocationOrigins(mActivity.getApplicationContext());
255 mSystemAllowGeolocationOrigins.start();
256
John Reckaf262e72011-07-25 13:55:44 -0700257 openIconDatabase();
Michael Kolb8233fac2010-10-26 16:08:53 -0700258 }
259
John Reck9c35b9c2012-05-30 10:08:50 -0700260 @Override
261 public void start(final Intent intent) {
Ben Murdochf695f6b2012-05-24 12:59:45 +0100262 WebViewClassic.setShouldMonitorWebCoreThread();
George Mount3636d0a2011-11-21 09:08:21 -0800263 // mCrashRecoverHandler has any previously saved state.
264 mCrashRecoveryHandler.startRecovery(intent);
John Reck847b5322011-04-14 17:02:18 -0700265 }
266
George Mount3636d0a2011-11-21 09:08:21 -0800267 void doStart(final Bundle icicle, final Intent intent) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700268 // Unless the last browser usage was within 24 hours, destroy any
269 // remaining incognito tabs.
270
271 Calendar lastActiveDate = icicle != null ?
272 (Calendar) icicle.getSerializable("lastActiveDate") : null;
273 Calendar today = Calendar.getInstance();
274 Calendar yesterday = Calendar.getInstance();
275 yesterday.add(Calendar.DATE, -1);
276
Patrick Scott7d50a932011-02-04 09:27:26 -0500277 final boolean restoreIncognitoTabs = !(lastActiveDate == null
Michael Kolb8233fac2010-10-26 16:08:53 -0700278 || lastActiveDate.before(yesterday)
Michael Kolb1bf23132010-11-19 12:55:12 -0800279 || lastActiveDate.after(today));
Michael Kolb8233fac2010-10-26 16:08:53 -0700280
Patrick Scott7d50a932011-02-04 09:27:26 -0500281 // Find out if we will restore any state and remember the tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700282 final long currentTabId =
Patrick Scott7d50a932011-02-04 09:27:26 -0500283 mTabControl.canRestoreState(icicle, restoreIncognitoTabs);
Kristian Monsen2cd97012010-12-07 11:11:40 +0000284
Michael Kolbc831b632011-05-11 09:30:34 -0700285 if (currentTabId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500286 // Not able to restore so we go ahead and clear session cookies. We
287 // must do this before trying to login the user as we don't want to
288 // clear any session cookies set during login.
289 CookieManager.getInstance().removeSessionCookie();
290 }
291
Patrick Scottd43e75a2011-03-14 14:47:23 -0400292 GoogleAccountLogin.startLoginIfNeeded(mActivity,
Patrick Scott7d50a932011-02-04 09:27:26 -0500293 new Runnable() {
294 @Override public void run() {
George Mount3636d0a2011-11-21 09:08:21 -0800295 onPreloginFinished(icicle, intent, currentTabId,
296 restoreIncognitoTabs);
Patrick Scott7d50a932011-02-04 09:27:26 -0500297 }
298 });
299 }
300
Michael Kolbc831b632011-05-11 09:30:34 -0700301 private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
George Mount3636d0a2011-11-21 09:08:21 -0800302 boolean restoreIncognitoTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700303 if (currentTabId == -1) {
John Reck1cf4b792011-07-26 10:22:22 -0700304 BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
George Mount3636d0a2011-11-21 09:08:21 -0800305 if (intent == null) {
306 // This won't happen under common scenarios. The icicle is
307 // not null, but there aren't any tabs to restore.
308 openTabToHomePage();
Michael Kolb7bcafde2011-05-09 13:55:59 -0700309 } else {
George Mount3636d0a2011-11-21 09:08:21 -0800310 final Bundle extra = intent.getExtras();
311 // Create an initial tab.
312 // If the intent is ACTION_VIEW and data is not null, the Browser is
313 // invoked to view the content by another application. In this case,
314 // the tab will be close when exit.
315 UrlData urlData = IntentHandler.getUrlDataFromIntent(intent);
316 Tab t = null;
317 if (urlData.isEmpty()) {
318 t = openTabToHomePage();
319 } else {
320 t = openTab(urlData);
321 }
322 if (t != null) {
323 t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
324 }
325 WebView webView = t.getWebView();
326 if (extra != null) {
327 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
328 if (scale > 0 && scale <= 1000) {
329 webView.setInitialScale(scale);
330 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700331 }
332 }
John Reckd8c74522011-06-14 08:45:00 -0700333 mUi.updateTabs(mTabControl.getTabs());
Michael Kolb8233fac2010-10-26 16:08:53 -0700334 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700335 mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
Patrick Scott7d50a932011-02-04 09:27:26 -0500336 mUi.needsRestoreAllTabs());
John Reck1cf4b792011-07-26 10:22:22 -0700337 List<Tab> tabs = mTabControl.getTabs();
338 ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
339 for (Tab t : tabs) {
340 restoredTabs.add(t.getId());
341 }
342 BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
John Reck52be4782011-08-26 15:37:29 -0700343 if (tabs.size() == 0) {
344 openTabToHomePage();
345 }
John Reck1cf4b792011-07-26 10:22:22 -0700346 mUi.updateTabs(tabs);
Michael Kolb8233fac2010-10-26 16:08:53 -0700347 // TabControl.restoreState() will create a new tab even if
348 // restoring the state fails.
349 setActiveTab(mTabControl.getCurrentTab());
George Mount3636d0a2011-11-21 09:08:21 -0800350 // Intent is non-null when framework thinks the browser should be
351 // launching with a new intent (icicle is null).
352 if (intent != null) {
John Reck9dd4a412011-10-05 14:55:30 -0700353 mIntentHandler.onNewIntent(intent);
354 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700355 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700356 // Read JavaScript flags if it exists.
John Reck35e9dd62011-04-25 09:01:54 -0700357 String jsFlags = getSettings().getJsEngineFlags();
Michael Kolb8233fac2010-10-26 16:08:53 -0700358 if (jsFlags.trim().length() != 0) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +0000359 WebViewClassic.fromWebView(getCurrentWebView()).setJsFlags(jsFlags);
Michael Kolb8233fac2010-10-26 16:08:53 -0700360 }
George Mount3636d0a2011-11-21 09:08:21 -0800361 if (intent != null
362 && BrowserActivity.ACTION_SHOW_BOOKMARKS.equals(intent.getAction())) {
Michael Kolb315d5022011-10-13 12:47:11 -0700363 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reck439c9a52010-12-14 10:04:39 -0800364 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700365 }
366
John Reck1cf4b792011-07-26 10:22:22 -0700367 private static class PruneThumbnails implements Runnable {
368 private Context mContext;
369 private List<Long> mIds;
370
371 PruneThumbnails(Context context, List<Long> preserveIds) {
372 mContext = context.getApplicationContext();
373 mIds = preserveIds;
374 }
375
376 @Override
377 public void run() {
378 ContentResolver cr = mContext.getContentResolver();
379 if (mIds == null || mIds.size() == 0) {
380 cr.delete(Thumbnails.CONTENT_URI, null, null);
381 } else {
382 int length = mIds.size();
383 StringBuilder where = new StringBuilder();
384 where.append(Thumbnails._ID);
385 where.append(" not in (");
386 for (int i = 0; i < length; i++) {
387 where.append(mIds.get(i));
388 if (i < (length - 1)) {
389 where.append(",");
390 }
391 }
392 where.append(")");
393 cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
394 }
395 }
396
397 }
398
Michael Kolb1514bb72010-11-22 09:11:48 -0800399 @Override
400 public WebViewFactory getWebViewFactory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 return mFactory;
402 }
403
404 @Override
Michael Kolba713ec82010-11-29 17:27:06 -0800405 public void onSetWebView(Tab tab, WebView view) {
406 mUi.onSetWebView(tab, view);
407 }
408
409 @Override
Michael Kolb1514bb72010-11-22 09:11:48 -0800410 public void createSubWindow(Tab tab) {
411 endActionMode();
412 WebView mainView = tab.getWebView();
413 WebView subView = mFactory.createWebView((mainView == null)
414 ? false
415 : mainView.isPrivateBrowsingEnabled());
416 mUi.createSubWindow(tab, subView);
417 }
418
419 @Override
Michael Kolb14612442011-06-24 13:06:29 -0700420 public Context getContext() {
421 return mActivity;
422 }
423
424 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -0700425 public Activity getActivity() {
426 return mActivity;
427 }
428
429 void setUi(UI ui) {
430 mUi = ui;
431 }
432
Michael Kolbaf63dba2012-05-16 12:58:05 -0700433 @Override
434 public BrowserSettings getSettings() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700435 return mSettings;
436 }
437
438 IntentHandler getIntentHandler() {
439 return mIntentHandler;
440 }
441
442 @Override
443 public UI getUi() {
444 return mUi;
445 }
446
447 int getMaxTabs() {
448 return mActivity.getResources().getInteger(R.integer.max_tabs);
449 }
450
451 @Override
452 public TabControl getTabControl() {
453 return mTabControl;
454 }
455
Michael Kolb1bf23132010-11-19 12:55:12 -0800456 @Override
457 public List<Tab> getTabs() {
458 return mTabControl.getTabs();
459 }
460
John Reckaf262e72011-07-25 13:55:44 -0700461 // Open the icon database.
462 private void openIconDatabase() {
463 // We have to call getInstance on the UI thread
464 final WebIconDatabase instance = WebIconDatabase.getInstance();
465 BackgroundHandler.execute(new Runnable() {
Ben Murdoch9446b932010-11-25 16:20:14 +0000466
John Reckaf262e72011-07-25 13:55:44 -0700467 @Override
468 public void run() {
469 instance.open(mActivity.getDir("icons", 0).getPath());
Michael Kolb8233fac2010-10-26 16:08:53 -0700470 }
John Reckaf262e72011-07-25 13:55:44 -0700471 });
Michael Kolb8233fac2010-10-26 16:08:53 -0700472 }
473
474 private void startHandler() {
475 mHandler = new Handler() {
476
477 @Override
478 public void handleMessage(Message msg) {
479 switch (msg.what) {
480 case OPEN_BOOKMARKS:
Michael Kolb315d5022011-10-13 12:47:11 -0700481 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolb8233fac2010-10-26 16:08:53 -0700482 break;
483 case FOCUS_NODE_HREF:
484 {
485 String url = (String) msg.getData().get("url");
486 String title = (String) msg.getData().get("title");
Cary Clark043c2d62010-12-15 11:19:39 -0500487 String src = (String) msg.getData().get("src");
488 if (url == "") url = src; // use image if no anchor
Michael Kolb8233fac2010-10-26 16:08:53 -0700489 if (TextUtils.isEmpty(url)) {
490 break;
491 }
492 HashMap focusNodeMap = (HashMap) msg.obj;
493 WebView view = (WebView) focusNodeMap.get("webview");
494 // Only apply the action if the top window did not change.
495 if (getCurrentTopWebView() != view) {
496 break;
497 }
498 switch (msg.arg1) {
499 case R.id.open_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700500 loadUrlFromContext(url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700501 break;
Cary Clark043c2d62010-12-15 11:19:39 -0500502 case R.id.view_image_context_menu_id:
John Reck26b18322011-06-21 13:08:58 -0700503 loadUrlFromContext(src);
Cary Clark043c2d62010-12-15 11:19:39 -0500504 break;
Leon Scroggins026f2542010-11-22 13:26:12 -0500505 case R.id.open_newtab_context_menu_id:
506 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -0700507 openTab(url, parent,
508 !mSettings.openInBackground(), true);
Leon Scroggins026f2542010-11-22 13:26:12 -0500509 break;
Michael Kolb8233fac2010-10-26 16:08:53 -0700510 case R.id.copy_link_context_menu_id:
511 copy(url);
512 break;
513 case R.id.save_link_context_menu_id:
514 case R.id.download_context_menu_id:
Leon Scroggins63c02662010-11-18 15:16:27 -0500515 DownloadHandler.onDownloadStartNoStream(
Andreas Sandblad8e4ce662012-06-11 11:02:49 +0200516 mActivity, url, view.getSettings().getUserAgentString(),
517 null, null, view.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -0700518 break;
519 }
520 break;
521 }
522
523 case LOAD_URL:
John Reck26b18322011-06-21 13:08:58 -0700524 loadUrlFromContext((String) msg.obj);
Michael Kolb8233fac2010-10-26 16:08:53 -0700525 break;
526
527 case STOP_LOAD:
528 stopLoading();
529 break;
530
531 case RELEASE_WAKELOCK:
John Reckf57c0292011-07-21 18:15:39 -0700532 if (mWakeLock != null && mWakeLock.isHeld()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700533 mWakeLock.release();
534 // if we reach here, Browser should be still in the
535 // background loading after WAKELOCK_TIMEOUT (5-min).
536 // To avoid burning the battery, stop loading.
537 mTabControl.stopAllLoading();
538 }
539 break;
540
541 case UPDATE_BOOKMARK_THUMBNAIL:
John Reck34ef2672011-02-10 11:30:55 -0800542 Tab tab = (Tab) msg.obj;
543 if (tab != null) {
544 updateScreenshot(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700545 }
546 break;
547 }
548 }
549 };
550
551 }
552
John Reckef654f12011-07-12 16:42:08 -0700553 @Override
Martijn Coenenb2f93552011-06-14 10:48:35 +0200554 public Tab getCurrentTab() {
555 return mTabControl.getCurrentTab();
556 }
557
Michael Kolbba99c5d2010-11-29 14:57:41 -0800558 @Override
559 public void shareCurrentPage() {
560 shareCurrentPage(mTabControl.getCurrentTab());
561 }
562
563 private void shareCurrentPage(Tab tab) {
564 if (tab != null) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800565 sharePage(mActivity, tab.getTitle(),
566 tab.getUrl(), tab.getFavicon(),
567 createScreenshot(tab.getWebView(),
568 getDesiredThumbnailWidth(mActivity),
569 getDesiredThumbnailHeight(mActivity)));
570 }
571 }
572
Michael Kolb8233fac2010-10-26 16:08:53 -0700573 /**
574 * Share a page, providing the title, url, favicon, and a screenshot. Uses
575 * an {@link Intent} to launch the Activity chooser.
576 * @param c Context used to launch a new Activity.
577 * @param title Title of the page. Stored in the Intent with
578 * {@link Intent#EXTRA_SUBJECT}
579 * @param url URL of the page. Stored in the Intent with
580 * {@link Intent#EXTRA_TEXT}
581 * @param favicon Bitmap of the favicon for the page. Stored in the Intent
582 * with {@link Browser#EXTRA_SHARE_FAVICON}
583 * @param screenshot Bitmap of a screenshot of the page. Stored in the
584 * Intent with {@link Browser#EXTRA_SHARE_SCREENSHOT}
585 */
586 static final void sharePage(Context c, String title, String url,
587 Bitmap favicon, Bitmap screenshot) {
588 Intent send = new Intent(Intent.ACTION_SEND);
589 send.setType("text/plain");
590 send.putExtra(Intent.EXTRA_TEXT, url);
591 send.putExtra(Intent.EXTRA_SUBJECT, title);
592 send.putExtra(Browser.EXTRA_SHARE_FAVICON, favicon);
593 send.putExtra(Browser.EXTRA_SHARE_SCREENSHOT, screenshot);
594 try {
595 c.startActivity(Intent.createChooser(send, c.getString(
596 R.string.choosertitle_sharevia)));
597 } catch(android.content.ActivityNotFoundException ex) {
598 // if no app handles it, do nothing
599 }
600 }
601
602 private void copy(CharSequence text) {
603 ClipboardManager cm = (ClipboardManager) mActivity
604 .getSystemService(Context.CLIPBOARD_SERVICE);
605 cm.setText(text);
606 }
607
608 // lifecycle
609
John Reck9c35b9c2012-05-30 10:08:50 -0700610 @Override
611 public void onConfgurationChanged(Configuration config) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700612 mConfigChanged = true;
Michael Kolb18f252f2012-03-06 13:36:20 -0800613 // update the menu in case of a locale change
614 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -0700615 if (mPageDialogsHandler != null) {
616 mPageDialogsHandler.onConfigurationChanged(config);
617 }
618 mUi.onConfigurationChanged(config);
619 }
620
621 @Override
622 public void handleNewIntent(Intent intent) {
Michael Kolb59e232c2011-08-18 17:19:53 -0700623 if (!mUi.isWebShowing()) {
624 mUi.showWeb(false);
625 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 mIntentHandler.onNewIntent(intent);
627 }
628
John Reck9c35b9c2012-05-30 10:08:50 -0700629 @Override
630 public void onPause() {
Michael Kolb11fe02d2011-02-02 09:52:16 -0800631 if (mUi.isCustomViewShowing()) {
632 hideCustomView();
633 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700634 if (mActivityPaused) {
635 Log.e(LOGTAG, "BrowserActivity is already paused.");
636 return;
637 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700638 mActivityPaused = true;
Michael Kolb70976932010-11-30 11:34:01 -0800639 Tab tab = mTabControl.getCurrentTab();
640 if (tab != null) {
641 tab.pause();
642 if (!pauseWebViewTimers(tab)) {
John Reckf57c0292011-07-21 18:15:39 -0700643 if (mWakeLock == null) {
644 PowerManager pm = (PowerManager) mActivity
645 .getSystemService(Context.POWER_SERVICE);
646 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Browser");
647 }
Michael Kolb70976932010-11-30 11:34:01 -0800648 mWakeLock.acquire();
649 mHandler.sendMessageDelayed(mHandler
650 .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT);
651 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 }
653 mUi.onPause();
654 mNetworkHandler.onPause();
655
656 WebView.disablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100657 NfcHandler.unregister(mActivity);
John Reckd7dd9b22011-08-30 09:18:29 -0700658 if (sThumbnailBitmap != null) {
659 sThumbnailBitmap.recycle();
660 sThumbnailBitmap = null;
661 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700662 }
663
John Reck9c35b9c2012-05-30 10:08:50 -0700664 @Override
665 public void onSaveInstanceState(Bundle outState) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 // Save all the tabs
George Mount3636d0a2011-11-21 09:08:21 -0800667 Bundle saveState = createSaveState();
668
669 // crash recovery manages all save & restore state
670 mCrashRecoveryHandler.writeState(saveState);
671 mSettings.setLastRunPaused(true);
672 }
673
674 /**
675 * Save the current state to outState. Does not write the state to
676 * disk.
677 * @return Bundle containing the current state of all tabs.
678 */
679 /* package */ Bundle createSaveState() {
680 Bundle saveState = new Bundle();
681 mTabControl.saveState(saveState);
682 if (!saveState.isEmpty()) {
John Reck24f18262011-06-17 14:47:20 -0700683 // Save time so that we know how old incognito tabs (if any) are.
George Mount3636d0a2011-11-21 09:08:21 -0800684 saveState.putSerializable("lastActiveDate", Calendar.getInstance());
John Reck24f18262011-06-17 14:47:20 -0700685 }
George Mount3636d0a2011-11-21 09:08:21 -0800686 return saveState;
Michael Kolb8233fac2010-10-26 16:08:53 -0700687 }
688
John Reck9c35b9c2012-05-30 10:08:50 -0700689 @Override
690 public void onResume() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700691 if (!mActivityPaused) {
692 Log.e(LOGTAG, "BrowserActivity is already resumed.");
693 return;
694 }
George Mount3636d0a2011-11-21 09:08:21 -0800695 mSettings.setLastRunPaused(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700696 mActivityPaused = false;
Michael Kolb70976932010-11-30 11:34:01 -0800697 Tab current = mTabControl.getCurrentTab();
698 if (current != null) {
699 current.resume();
700 resumeWebViewTimers(current);
701 }
John Reckf57c0292011-07-21 18:15:39 -0700702 releaseWakeLock();
Martijn Coenenb2f93552011-06-14 10:48:35 +0200703
Michael Kolb8233fac2010-10-26 16:08:53 -0700704 mUi.onResume();
705 mNetworkHandler.onResume();
706 WebView.enablePlatformNotifications();
Ben Murdoch015e1e32011-09-01 23:45:31 +0100707 NfcHandler.register(mActivity, this);
Michael Kolb0b129122012-06-04 16:31:58 -0700708 if (mVoiceResult != null) {
709 mUi.onVoiceResult(mVoiceResult);
710 mVoiceResult = null;
711 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700712 }
713
John Reckf57c0292011-07-21 18:15:39 -0700714 private void releaseWakeLock() {
715 if (mWakeLock != null && mWakeLock.isHeld()) {
716 mHandler.removeMessages(RELEASE_WAKELOCK);
717 mWakeLock.release();
718 }
719 }
720
Michael Kolb70976932010-11-30 11:34:01 -0800721 /**
Michael Kolbba99c5d2010-11-29 14:57:41 -0800722 * resume all WebView timers using the WebView instance of the given tab
Michael Kolb70976932010-11-30 11:34:01 -0800723 * @param tab guaranteed non-null
724 */
725 private void resumeWebViewTimers(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700726 boolean inLoad = tab.inPageLoad();
727 if ((!mActivityPaused && !inLoad) || (mActivityPaused && inLoad)) {
728 CookieSyncManager.getInstance().startSync();
729 WebView w = tab.getWebView();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100730 WebViewTimersControl.getInstance().onBrowserActivityResume(w);
Michael Kolb8233fac2010-10-26 16:08:53 -0700731 }
732 }
733
Michael Kolb70976932010-11-30 11:34:01 -0800734 /**
735 * Pause all WebView timers using the WebView of the given tab
736 * @param tab
737 * @return true if the timers are paused or tab is null
738 */
739 private boolean pauseWebViewTimers(Tab tab) {
740 if (tab == null) {
741 return true;
742 } else if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700743 CookieSyncManager.getInstance().stopSync();
Mathew Inwoode1dbb952011-07-08 17:27:38 +0100744 WebViewTimersControl.getInstance().onBrowserActivityPause(getCurrentWebView());
Michael Kolb8233fac2010-10-26 16:08:53 -0700745 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700746 }
Michael Kolb70976932010-11-30 11:34:01 -0800747 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700748 }
749
John Reck9c35b9c2012-05-30 10:08:50 -0700750 @Override
751 public void onDestroy() {
John Reck38b4bf52011-02-22 14:39:34 -0800752 if (mUploadHandler != null && !mUploadHandler.handled()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700753 mUploadHandler.onResult(Activity.RESULT_CANCELED, null);
754 mUploadHandler = null;
755 }
756 if (mTabControl == null) return;
757 mUi.onDestroy();
758 // Remove the current tab and sub window
759 Tab t = mTabControl.getCurrentTab();
760 if (t != null) {
761 dismissSubWindow(t);
762 removeTab(t);
763 }
Leon Scroggins1961ed22010-12-07 15:22:21 -0500764 mActivity.getContentResolver().unregisterContentObserver(mBookmarksObserver);
Michael Kolb8233fac2010-10-26 16:08:53 -0700765 // Destroy all the tabs
766 mTabControl.destroy();
767 WebIconDatabase.getInstance().close();
768 // Stop watching the default geolocation permissions
769 mSystemAllowGeolocationOrigins.stop();
770 mSystemAllowGeolocationOrigins = null;
771 }
772
773 protected boolean isActivityPaused() {
774 return mActivityPaused;
775 }
776
John Reck9c35b9c2012-05-30 10:08:50 -0700777 @Override
778 public void onLowMemory() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700779 mTabControl.freeMemory();
780 }
781
782 @Override
783 public boolean shouldShowErrorConsole() {
784 return mShouldShowErrorConsole;
785 }
786
787 protected void setShouldShowErrorConsole(boolean show) {
788 if (show == mShouldShowErrorConsole) {
789 // Nothing to do.
790 return;
791 }
792 mShouldShowErrorConsole = show;
793 Tab t = mTabControl.getCurrentTab();
794 if (t == null) {
795 // There is no current tab so we cannot toggle the error console
796 return;
797 }
798 mUi.setShouldShowErrorConsole(t, show);
799 }
800
801 @Override
802 public void stopLoading() {
803 mLoadStopped = true;
804 Tab tab = mTabControl.getCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -0700805 WebView w = getCurrentTopWebView();
Michael Kolb778a4882012-04-12 15:27:28 -0700806 if (w != null) {
807 w.stopLoading();
808 mUi.onPageStopped(tab);
809 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700810 }
811
812 boolean didUserStopLoading() {
813 return mLoadStopped;
814 }
815
816 // WebViewController
817
818 @Override
John Reck324d4402011-01-11 16:56:42 -0800819 public void onPageStarted(Tab tab, WebView view, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700820
821 // We've started to load a new page. If there was a pending message
822 // to save a screenshot then we will now take the new page and save
823 // an incorrect screenshot. Therefore, remove any pending thumbnail
824 // messages from the queue.
825 mHandler.removeMessages(Controller.UPDATE_BOOKMARK_THUMBNAIL,
John Reck34ef2672011-02-10 11:30:55 -0800826 tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700827
828 // reset sync timer to avoid sync starts during loading a page
829 CookieSyncManager.getInstance().resetSync();
830
831 if (!mNetworkHandler.isNetworkUp()) {
832 view.setNetworkAvailable(false);
833 }
834
835 // when BrowserActivity just starts, onPageStarted may be called before
836 // onResume as it is triggered from onCreate. Call resumeWebViewTimers
837 // to start the timer. As we won't switch tabs while an activity is in
838 // pause state, we can ensure calling resume and pause in pair.
839 if (mActivityPaused) {
Michael Kolb70976932010-11-30 11:34:01 -0800840 resumeWebViewTimers(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700841 }
842 mLoadStopped = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700843 endActionMode();
844
John Reck30c714c2010-12-16 17:30:34 -0800845 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700846
John Reck324d4402011-01-11 16:56:42 -0800847 String url = tab.getUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700848 // update the bookmark database for favicon
849 maybeUpdateFavicon(tab, null, url, favicon);
850
851 Performance.tracePageStart(url);
852
853 // Performance probe
854 if (false) {
855 Performance.onPageStarted();
856 }
857
858 }
859
860 @Override
John Reck324d4402011-01-11 16:56:42 -0800861 public void onPageFinished(Tab tab) {
Michael Kolb72864272012-05-03 15:42:15 -0700862 mCrashRecoveryHandler.backupState();
John Reck30c714c2010-12-16 17:30:34 -0800863 mUi.onTabDataChanged(tab);
Martijn Coenenb2f93552011-06-14 10:48:35 +0200864
Michael Kolb8233fac2010-10-26 16:08:53 -0700865 // Performance probe
866 if (false) {
John Reck324d4402011-01-11 16:56:42 -0800867 Performance.onPageFinished(tab.getUrl());
Michael Kolb8233fac2010-10-26 16:08:53 -0700868 }
869
870 Performance.tracePageFinished();
871 }
872
873 @Override
John Reck30c714c2010-12-16 17:30:34 -0800874 public void onProgressChanged(Tab tab) {
875 int newProgress = tab.getLoadProgress();
Michael Kolb8233fac2010-10-26 16:08:53 -0700876
877 if (newProgress == 100) {
878 CookieSyncManager.getInstance().sync();
879 // onProgressChanged() may continue to be called after the main
880 // frame has finished loading, as any remaining sub frames continue
881 // to load. We'll only get called once though with newProgress as
882 // 100 when everything is loaded. (onPageFinished is called once
883 // when the main frame completes loading regardless of the state of
884 // any sub frames so calls to onProgressChanges may continue after
885 // onPageFinished has executed)
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800886 if (tab.inPageLoad()) {
887 updateInLoadMenuItems(mCachedMenu, tab);
Mattias Falk9cc2d032012-05-25 09:40:31 +0200888 } else if (mActivityPaused && pauseWebViewTimers(tab)) {
889 // pause the WebView timer and release the wake lock if it is
890 // finished while BrowserActivity is in pause state.
891 releaseWakeLock();
Michael Kolb8233fac2010-10-26 16:08:53 -0700892 }
John Reckd9862372012-02-21 15:04:50 -0800893 if (!tab.isPrivateBrowsingEnabled()
894 && !TextUtils.isEmpty(tab.getUrl())
895 && !tab.isSnapshot()) {
896 // Only update the bookmark screenshot if the user did not
897 // cancel the load early and there is not already
898 // a pending update for the tab.
Michael Kolb72864272012-05-03 15:42:15 -0700899 if (tab.shouldUpdateThumbnail() &&
900 (tab.inForeground() && !didUserStopLoading()
901 || !tab.inForeground())) {
John Reckd9862372012-02-21 15:04:50 -0800902 if (!mHandler.hasMessages(UPDATE_BOOKMARK_THUMBNAIL, tab)) {
903 mHandler.sendMessageDelayed(mHandler.obtainMessage(
904 UPDATE_BOOKMARK_THUMBNAIL, 0, 0, tab),
905 500);
906 }
907 }
908 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700909 } else {
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800910 if (!tab.inPageLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700911 // onPageFinished may have already been called but a subframe is
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800912 // still loading
913 // updating the progress and
Michael Kolb8233fac2010-10-26 16:08:53 -0700914 // update the menu items.
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800915 updateInLoadMenuItems(mCachedMenu, tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700916 }
917 }
John Reck30c714c2010-12-16 17:30:34 -0800918 mUi.onProgressChanged(tab);
919 }
920
921 @Override
Steve Block2466eff2011-10-03 15:33:09 +0100922 public void onUpdatedSecurityState(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -0800923 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700924 }
925
926 @Override
927 public void onReceivedTitle(Tab tab, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800928 mUi.onTabDataChanged(tab);
John Reck49a603c2011-03-03 09:33:05 -0800929 final String pageUrl = tab.getOriginalUrl();
John Reck324d4402011-01-11 16:56:42 -0800930 if (TextUtils.isEmpty(pageUrl) || pageUrl.length()
Michael Kolb8233fac2010-10-26 16:08:53 -0700931 >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) {
932 return;
933 }
934 // Update the title in the history database if not in private browsing mode
935 if (!tab.isPrivateBrowsingEnabled()) {
John Reckf57c0292011-07-21 18:15:39 -0700936 DataController.getInstance(mActivity).updateHistoryTitle(pageUrl, title);
Michael Kolb8233fac2010-10-26 16:08:53 -0700937 }
938 }
939
940 @Override
941 public void onFavicon(Tab tab, WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800942 mUi.onTabDataChanged(tab);
Michael Kolb8233fac2010-10-26 16:08:53 -0700943 maybeUpdateFavicon(tab, view.getOriginalUrl(), view.getUrl(), icon);
944 }
945
946 @Override
Michael Kolb18eb3772010-12-10 14:29:51 -0800947 public boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
948 return mUrlHandler.shouldOverrideUrlLoading(tab, view, url);
Michael Kolb8233fac2010-10-26 16:08:53 -0700949 }
950
951 @Override
952 public boolean shouldOverrideKeyEvent(KeyEvent event) {
953 if (mMenuIsDown) {
954 // only check shortcut key when MENU is held
955 return mActivity.getWindow().isShortcutKey(event.getKeyCode(),
956 event);
957 } else {
958 return false;
959 }
960 }
961
962 @Override
John Reck997b1b72012-04-19 18:08:25 -0700963 public boolean onUnhandledKeyEvent(KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 if (!isActivityPaused()) {
965 if (event.getAction() == KeyEvent.ACTION_DOWN) {
John Reck997b1b72012-04-19 18:08:25 -0700966 return mActivity.onKeyDown(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700967 } else {
John Reck997b1b72012-04-19 18:08:25 -0700968 return mActivity.onKeyUp(event.getKeyCode(), event);
Michael Kolb8233fac2010-10-26 16:08:53 -0700969 }
970 }
John Reck997b1b72012-04-19 18:08:25 -0700971 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700972 }
973
974 @Override
John Reck324d4402011-01-11 16:56:42 -0800975 public void doUpdateVisitedHistory(Tab tab, boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700976 // Don't save anything in private browsing mode
977 if (tab.isPrivateBrowsingEnabled()) return;
John Reck49a603c2011-03-03 09:33:05 -0800978 String url = tab.getOriginalUrl();
Michael Kolb8233fac2010-10-26 16:08:53 -0700979
John Reck324d4402011-01-11 16:56:42 -0800980 if (TextUtils.isEmpty(url)
981 || url.regionMatches(true, 0, "about:", 0, 6)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700982 return;
983 }
John Reckf57c0292011-07-21 18:15:39 -0700984 DataController.getInstance(mActivity).updateVisitedHistory(url);
John Reck6c2e2f32011-08-22 13:41:23 -0700985 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -0700986 }
987
988 @Override
989 public void getVisitedHistory(final ValueCallback<String[]> callback) {
990 AsyncTask<Void, Void, String[]> task =
991 new AsyncTask<Void, Void, String[]>() {
992 @Override
993 public String[] doInBackground(Void... unused) {
994 return Browser.getVisitedHistory(mActivity.getContentResolver());
995 }
996 @Override
997 public void onPostExecute(String[] result) {
998 callback.onReceiveValue(result);
999 }
1000 };
1001 task.execute();
1002 }
1003
1004 @Override
1005 public void onReceivedHttpAuthRequest(Tab tab, WebView view,
1006 final HttpAuthHandler handler, final String host,
1007 final String realm) {
1008 String username = null;
1009 String password = null;
1010
1011 boolean reuseHttpAuthUsernamePassword
1012 = handler.useHttpAuthUsernamePassword();
1013
1014 if (reuseHttpAuthUsernamePassword && view != null) {
1015 String[] credentials = view.getHttpAuthUsernamePassword(host, realm);
1016 if (credentials != null && credentials.length == 2) {
1017 username = credentials[0];
1018 password = credentials[1];
1019 }
1020 }
1021
1022 if (username != null && password != null) {
1023 handler.proceed(username, password);
1024 } else {
Ben Murdochfdd37112011-08-05 15:48:14 +01001025 if (tab.inForeground() && !handler.suppressDialog()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001026 mPageDialogsHandler.showHttpAuthentication(tab, handler, host, realm);
1027 } else {
1028 handler.cancel();
1029 }
1030 }
1031 }
1032
1033 @Override
1034 public void onDownloadStart(Tab tab, String url, String userAgent,
1035 String contentDisposition, String mimetype, long contentLength) {
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001036 WebView w = tab.getWebView();
Leon Scroggins63c02662010-11-18 15:16:27 -05001037 DownloadHandler.onDownloadStart(mActivity, url, userAgent,
Kristian Monsenbc5cc752011-03-02 13:14:03 +00001038 contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
1039 if (w.copyBackForwardList().getSize() == 0) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001040 // This Tab was opened for the sole purpose of downloading a
1041 // file. Remove it.
1042 if (tab == mTabControl.getCurrentTab()) {
1043 // In this case, the Tab is still on top.
1044 goBackOnePageOrQuit();
1045 } else {
1046 // In this case, it is not.
1047 closeTab(tab);
1048 }
1049 }
1050 }
1051
1052 @Override
1053 public Bitmap getDefaultVideoPoster() {
1054 return mUi.getDefaultVideoPoster();
1055 }
1056
1057 @Override
1058 public View getVideoLoadingProgressView() {
1059 return mUi.getVideoLoadingProgressView();
1060 }
1061
1062 @Override
1063 public void showSslCertificateOnError(WebView view, SslErrorHandler handler,
1064 SslError error) {
1065 mPageDialogsHandler.showSSLCertificateOnError(view, handler, error);
1066 }
1067
Patrick Scott92066772011-03-10 08:46:27 -05001068 @Override
1069 public void showAutoLogin(Tab tab) {
1070 assert tab.inForeground();
1071 // Update the title bar to show the auto-login request.
1072 mUi.showAutoLogin(tab);
1073 }
1074
1075 @Override
1076 public void hideAutoLogin(Tab tab) {
1077 assert tab.inForeground();
1078 mUi.hideAutoLogin(tab);
1079 }
1080
Michael Kolb8233fac2010-10-26 16:08:53 -07001081 // helper method
1082
1083 /*
1084 * Update the favorites icon if the private browsing isn't enabled and the
1085 * icon is valid.
1086 */
1087 private void maybeUpdateFavicon(Tab tab, final String originalUrl,
1088 final String url, Bitmap favicon) {
1089 if (favicon == null) {
1090 return;
1091 }
1092 if (!tab.isPrivateBrowsingEnabled()) {
1093 Bookmarks.updateFavicon(mActivity
1094 .getContentResolver(), originalUrl, url, favicon);
1095 }
1096 }
1097
Leon Scroggins4cd97792010-12-03 15:31:56 -05001098 @Override
1099 public void bookmarkedStatusHasChanged(Tab tab) {
John Recke969cc52010-12-21 17:24:43 -08001100 // TODO: Switch to using onTabDataChanged after b/3262950 is fixed
Leon Scroggins4cd97792010-12-03 15:31:56 -05001101 mUi.bookmarkedStatusHasChanged(tab);
1102 }
1103
Michael Kolb8233fac2010-10-26 16:08:53 -07001104 // end WebViewController
1105
1106 protected void pageUp() {
1107 getCurrentTopWebView().pageUp(false);
1108 }
1109
1110 protected void pageDown() {
1111 getCurrentTopWebView().pageDown(false);
1112 }
1113
1114 // callback from phone title bar
John Reck9c35b9c2012-05-30 10:08:50 -07001115 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001116 public void editUrl() {
1117 if (mOptionsMenuOpen) mActivity.closeOptionsMenu();
Michael Kolb1f9b3562012-04-24 14:38:34 -07001118 mUi.editUrl(false, true);
Michael Kolb8233fac2010-10-26 16:08:53 -07001119 }
1120
John Reck9c35b9c2012-05-30 10:08:50 -07001121 @Override
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001122 public void showCustomView(Tab tab, View view, int requestedOrientation,
Michael Kolb8233fac2010-10-26 16:08:53 -07001123 WebChromeClient.CustomViewCallback callback) {
1124 if (tab.inForeground()) {
1125 if (mUi.isCustomViewShowing()) {
1126 callback.onCustomViewHidden();
1127 return;
1128 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001129 mUi.showCustomView(view, requestedOrientation, callback);
Michael Kolb8233fac2010-10-26 16:08:53 -07001130 // Save the menu state and set it to empty while the custom
1131 // view is showing.
1132 mOldMenuState = mMenuState;
1133 mMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001134 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001135 }
1136 }
1137
1138 @Override
1139 public void hideCustomView() {
1140 if (mUi.isCustomViewShowing()) {
1141 mUi.onHideCustomView();
1142 // Reset the old menu state.
1143 mMenuState = mOldMenuState;
1144 mOldMenuState = EMPTY_MENU;
John Reckd73c5a22010-12-22 10:22:50 -08001145 mActivity.invalidateOptionsMenu();
Michael Kolb8233fac2010-10-26 16:08:53 -07001146 }
1147 }
1148
John Reck9c35b9c2012-05-30 10:08:50 -07001149 @Override
1150 public void onActivityResult(int requestCode, int resultCode,
Michael Kolb8233fac2010-10-26 16:08:53 -07001151 Intent intent) {
1152 if (getCurrentTopWebView() == null) return;
1153 switch (requestCode) {
1154 case PREFERENCES_PAGE:
1155 if (resultCode == Activity.RESULT_OK && intent != null) {
1156 String action = intent.getStringExtra(Intent.EXTRA_TEXT);
John Reck35e9dd62011-04-25 09:01:54 -07001157 if (PreferenceKeys.PREF_PRIVACY_CLEAR_HISTORY.equals(action)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001158 mTabControl.removeParentChildRelationShips();
1159 }
1160 }
1161 break;
1162 case FILE_SELECTED:
Ben Murdoch51f6a2f2011-02-21 12:27:07 +00001163 // Chose a file from the file picker.
John Reck9dfcdb12011-02-22 16:40:46 -08001164 if (null == mUploadHandler) break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001165 mUploadHandler.onResult(resultCode, intent);
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 break;
Ben Murdoch8029a772010-11-16 11:58:21 +00001167 case AUTOFILL_SETUP:
1168 // Determine whether a profile was actually set up or not
1169 // and if so, send the message back to the WebTextView to
1170 // fill the form with the new profile.
1171 if (getSettings().getAutoFillProfile() != null) {
1172 mAutoFillSetupMessage.sendToTarget();
1173 mAutoFillSetupMessage = null;
1174 }
1175 break;
John Reckd3e4d5b2011-07-13 15:48:43 -07001176 case COMBO_VIEW:
1177 if (intent == null || resultCode != Activity.RESULT_OK) {
1178 break;
1179 }
John Reck3ba45532011-08-11 16:26:53 -07001180 mUi.showWeb(false);
John Reckd3e4d5b2011-07-13 15:48:43 -07001181 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
1182 Tab t = getCurrentTab();
1183 Uri uri = intent.getData();
1184 loadUrl(t, uri.toString());
1185 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_ALL)) {
1186 String[] urls = intent.getStringArrayExtra(
1187 ComboViewActivity.EXTRA_OPEN_ALL);
1188 Tab parent = getCurrentTab();
1189 for (String url : urls) {
1190 parent = openTab(url, parent,
1191 !mSettings.openInBackground(), true);
1192 }
1193 } else if (intent.hasExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT)) {
1194 long id = intent.getLongExtra(
1195 ComboViewActivity.EXTRA_OPEN_SNAPSHOT, -1);
1196 if (id >= 0) {
1197 createNewSnapshotTab(id, true);
1198 }
1199 }
1200 break;
Michael Kolb0b129122012-06-04 16:31:58 -07001201 case VOICE_RESULT:
1202 if (resultCode == Activity.RESULT_OK && intent != null) {
1203 ArrayList<String> results = intent.getStringArrayListExtra(
1204 RecognizerIntent.EXTRA_RESULTS);
1205 if (results.size() >= 1) {
1206 mVoiceResult = results.get(0);
1207 }
1208 }
1209 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07001210 default:
1211 break;
1212 }
1213 getCurrentTopWebView().requestFocus();
1214 }
1215
1216 /**
1217 * Open the Go page.
1218 * @param startWithHistory If true, open starting on the history tab.
1219 * Otherwise, start with the bookmarks tab.
1220 */
1221 @Override
Michael Kolb315d5022011-10-13 12:47:11 -07001222 public void bookmarksOrHistoryPicker(ComboViews startView) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001223 if (mTabControl.getCurrentWebView() == null) {
1224 return;
1225 }
Michael Kolbbd3dd942011-01-12 11:09:38 -08001226 // clear action mode
1227 if (isInCustomActionMode()) {
1228 endActionMode();
1229 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001230 Bundle extras = new Bundle();
1231 // Disable opening in a new window if we have maxed out the windows
1232 extras.putBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW,
1233 !mTabControl.canCreateNewTab());
Michael Kolb315d5022011-10-13 12:47:11 -07001234 mUi.showComboView(startView, extras);
Michael Kolb8233fac2010-10-26 16:08:53 -07001235 }
1236
1237 // combo view callbacks
1238
Michael Kolb8233fac2010-10-26 16:08:53 -07001239 // key handling
1240 protected void onBackKey() {
1241 if (!mUi.onBackKey()) {
1242 WebView subwindow = mTabControl.getCurrentSubWindow();
1243 if (subwindow != null) {
1244 if (subwindow.canGoBack()) {
1245 subwindow.goBack();
1246 } else {
1247 dismissSubWindow(mTabControl.getCurrentTab());
1248 }
1249 } else {
1250 goBackOnePageOrQuit();
1251 }
1252 }
1253 }
1254
Michael Kolb4bd767d2011-05-27 11:33:55 -07001255 protected boolean onMenuKey() {
1256 return mUi.onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07001257 }
1258
Michael Kolb8233fac2010-10-26 16:08:53 -07001259 // menu handling and state
1260 // TODO: maybe put into separate handler
1261
John Reck9c35b9c2012-05-30 10:08:50 -07001262 @Override
1263 public boolean onCreateOptionsMenu(Menu menu) {
John Reckd73c5a22010-12-22 10:22:50 -08001264 if (mMenuState == EMPTY_MENU) {
1265 return false;
1266 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001267 MenuInflater inflater = mActivity.getMenuInflater();
1268 inflater.inflate(R.menu.browser, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001269 return true;
1270 }
1271
John Reck9c35b9c2012-05-30 10:08:50 -07001272 @Override
1273 public void onCreateContextMenu(ContextMenu menu, View v,
Michael Kolb8233fac2010-10-26 16:08:53 -07001274 ContextMenuInfo menuInfo) {
John Reck0f602f32011-07-07 15:38:43 -07001275 if (v instanceof TitleBar) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001276 return;
1277 }
1278 if (!(v instanceof WebView)) {
1279 return;
1280 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001281 final WebView webview = (WebView) v;
Michael Kolb8233fac2010-10-26 16:08:53 -07001282 WebView.HitTestResult result = webview.getHitTestResult();
1283 if (result == null) {
1284 return;
1285 }
1286
1287 int type = result.getType();
1288 if (type == WebView.HitTestResult.UNKNOWN_TYPE) {
1289 Log.w(LOGTAG,
1290 "We should not show context menu when nothing is touched");
1291 return;
1292 }
1293 if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
1294 // let TextView handles context menu
1295 return;
1296 }
1297
1298 // Note, http://b/issue?id=1106666 is requesting that
1299 // an inflated menu can be used again. This is not available
1300 // yet, so inflate each time (yuk!)
1301 MenuInflater inflater = mActivity.getMenuInflater();
1302 inflater.inflate(R.menu.browsercontext, menu);
1303
1304 // Show the correct menu group
1305 final String extra = result.getExtra();
Michael Kolbc159c1a2011-12-15 16:06:38 -08001306 if (extra == null) return;
Michael Kolb8233fac2010-10-26 16:08:53 -07001307 menu.setGroupVisible(R.id.PHONE_MENU,
1308 type == WebView.HitTestResult.PHONE_TYPE);
1309 menu.setGroupVisible(R.id.EMAIL_MENU,
1310 type == WebView.HitTestResult.EMAIL_TYPE);
1311 menu.setGroupVisible(R.id.GEO_MENU,
1312 type == WebView.HitTestResult.GEO_TYPE);
1313 menu.setGroupVisible(R.id.IMAGE_MENU,
1314 type == WebView.HitTestResult.IMAGE_TYPE
1315 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
1316 menu.setGroupVisible(R.id.ANCHOR_MENU,
1317 type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1318 || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);
Cary Clark8974d282010-11-22 10:46:05 -05001319 boolean hitText = type == WebView.HitTestResult.SRC_ANCHOR_TYPE
1320 || type == WebView.HitTestResult.PHONE_TYPE
1321 || type == WebView.HitTestResult.EMAIL_TYPE
1322 || type == WebView.HitTestResult.GEO_TYPE;
1323 menu.setGroupVisible(R.id.SELECT_TEXT_MENU, hitText);
1324 if (hitText) {
1325 menu.findItem(R.id.select_text_menu_id)
1326 .setOnMenuItemClickListener(new SelectText(webview));
1327 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001328 // Setup custom handling depending on the type
1329 switch (type) {
1330 case WebView.HitTestResult.PHONE_TYPE:
1331 menu.setHeaderTitle(Uri.decode(extra));
1332 menu.findItem(R.id.dial_context_menu_id).setIntent(
1333 new Intent(Intent.ACTION_VIEW, Uri
1334 .parse(WebView.SCHEME_TEL + extra)));
1335 Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
1336 addIntent.putExtra(Insert.PHONE, Uri.decode(extra));
1337 addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
1338 menu.findItem(R.id.add_contact_context_menu_id).setIntent(
1339 addIntent);
1340 menu.findItem(R.id.copy_phone_context_menu_id)
1341 .setOnMenuItemClickListener(
1342 new Copy(extra));
1343 break;
1344
1345 case WebView.HitTestResult.EMAIL_TYPE:
1346 menu.setHeaderTitle(extra);
1347 menu.findItem(R.id.email_context_menu_id).setIntent(
1348 new Intent(Intent.ACTION_VIEW, Uri
1349 .parse(WebView.SCHEME_MAILTO + extra)));
1350 menu.findItem(R.id.copy_mail_context_menu_id)
1351 .setOnMenuItemClickListener(
1352 new Copy(extra));
1353 break;
1354
1355 case WebView.HitTestResult.GEO_TYPE:
1356 menu.setHeaderTitle(extra);
1357 menu.findItem(R.id.map_context_menu_id).setIntent(
1358 new Intent(Intent.ACTION_VIEW, Uri
1359 .parse(WebView.SCHEME_GEO
1360 + URLEncoder.encode(extra))));
1361 menu.findItem(R.id.copy_geo_context_menu_id)
1362 .setOnMenuItemClickListener(
1363 new Copy(extra));
1364 break;
1365
1366 case WebView.HitTestResult.SRC_ANCHOR_TYPE:
1367 case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
Michael Kolb4c537ce2011-01-13 15:19:33 -08001368 menu.setHeaderTitle(extra);
Michael Kolb8233fac2010-10-26 16:08:53 -07001369 // decide whether to show the open link in new tab option
1370 boolean showNewTab = mTabControl.canCreateNewTab();
1371 MenuItem newTabItem
1372 = menu.findItem(R.id.open_newtab_context_menu_id);
John Reck35e9dd62011-04-25 09:01:54 -07001373 newTabItem.setTitle(getSettings().openInBackground()
Michael Kolb2dd65c82011-01-14 11:07:38 -08001374 ? R.string.contextmenu_openlink_newwindow_background
1375 : R.string.contextmenu_openlink_newwindow);
Michael Kolb8233fac2010-10-26 16:08:53 -07001376 newTabItem.setVisible(showNewTab);
1377 if (showNewTab) {
Leon Scroggins026f2542010-11-22 13:26:12 -05001378 if (WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE == type) {
1379 newTabItem.setOnMenuItemClickListener(
1380 new MenuItem.OnMenuItemClickListener() {
1381 @Override
1382 public boolean onMenuItemClick(MenuItem item) {
1383 final HashMap<String, WebView> hrefMap =
1384 new HashMap<String, WebView>();
1385 hrefMap.put("webview", webview);
1386 final Message msg = mHandler.obtainMessage(
1387 FOCUS_NODE_HREF,
1388 R.id.open_newtab_context_menu_id,
1389 0, hrefMap);
1390 webview.requestFocusNodeHref(msg);
1391 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -07001392 }
Leon Scroggins026f2542010-11-22 13:26:12 -05001393 });
1394 } else {
1395 newTabItem.setOnMenuItemClickListener(
1396 new MenuItem.OnMenuItemClickListener() {
1397 @Override
1398 public boolean onMenuItemClick(MenuItem item) {
1399 final Tab parent = mTabControl.getCurrentTab();
John Reck5949c662011-05-27 09:52:29 -07001400 openTab(extra, parent,
1401 !mSettings.openInBackground(),
1402 true);
Leon Scroggins026f2542010-11-22 13:26:12 -05001403 return true;
1404 }
1405 });
1406 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001407 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001408 if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
1409 break;
1410 }
1411 // otherwise fall through to handle image part
1412 case WebView.HitTestResult.IMAGE_TYPE:
Victoria Lease5987b802012-03-12 13:05:23 -07001413 MenuItem shareItem = menu.findItem(R.id.share_link_context_menu_id);
1414 shareItem.setVisible(type == WebView.HitTestResult.IMAGE_TYPE);
Michael Kolb8233fac2010-10-26 16:08:53 -07001415 if (type == WebView.HitTestResult.IMAGE_TYPE) {
1416 menu.setHeaderTitle(extra);
Victoria Lease5987b802012-03-12 13:05:23 -07001417 shareItem.setOnMenuItemClickListener(
1418 new MenuItem.OnMenuItemClickListener() {
1419 @Override
1420 public boolean onMenuItemClick(MenuItem item) {
1421 sharePage(mActivity, null, extra, null,
1422 null);
1423 return true;
1424 }
1425 }
1426 );
Michael Kolb8233fac2010-10-26 16:08:53 -07001427 }
Michael Kolb3639c4c2011-09-28 15:36:40 -07001428 menu.findItem(R.id.view_image_context_menu_id)
1429 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
1430 @Override
1431 public boolean onMenuItemClick(MenuItem item) {
1432 openTab(extra, mTabControl.getCurrentTab(), true, true);
1433 return false;
1434 }
1435 });
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02001436 menu.findItem(R.id.download_context_menu_id).setOnMenuItemClickListener(
1437 new Download(mActivity, extra, webview.isPrivateBrowsingEnabled(),
1438 webview.getSettings().getUserAgentString()));
John Reck3527dd12011-02-22 10:35:29 -08001439 menu.findItem(R.id.set_wallpaper_context_menu_id).
1440 setOnMenuItemClickListener(new WallpaperHandler(mActivity,
1441 extra));
Michael Kolb8233fac2010-10-26 16:08:53 -07001442 break;
1443
1444 default:
1445 Log.w(LOGTAG, "We should not get here.");
1446 break;
1447 }
1448 //update the ui
1449 mUi.onContextMenuCreated(menu);
1450 }
1451
1452 /**
1453 * As the menu can be open when loading state changes
1454 * we must manually update the state of the stop/reload menu
1455 * item
1456 */
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001457 private void updateInLoadMenuItems(Menu menu, Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001458 if (menu == null) {
1459 return;
1460 }
1461 MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolbb8337132011-11-28 15:07:39 -08001462 MenuItem src = ((tab != null) && tab.inPageLoad()) ?
Michael Kolb8233fac2010-10-26 16:08:53 -07001463 menu.findItem(R.id.stop_menu_id):
1464 menu.findItem(R.id.reload_menu_id);
1465 if (src != null) {
1466 dest.setIcon(src.getIcon());
1467 dest.setTitle(src.getTitle());
1468 }
1469 }
1470
John Reck9c35b9c2012-05-30 10:08:50 -07001471 @Override
1472 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001473 updateInLoadMenuItems(menu, getCurrentTab());
John Recke1a03a32011-09-14 17:04:16 -07001474 // hold on to the menu reference here; it is used by the page callbacks
1475 // to update the menu based on loading state
1476 mCachedMenu = menu;
Michael Kolb8233fac2010-10-26 16:08:53 -07001477 // Note: setVisible will decide whether an item is visible; while
1478 // setEnabled() will decide whether an item is enabled, which also means
1479 // whether the matching shortcut key will function.
1480 switch (mMenuState) {
1481 case EMPTY_MENU:
1482 if (mCurrentMenuState != mMenuState) {
1483 menu.setGroupVisible(R.id.MAIN_MENU, false);
1484 menu.setGroupEnabled(R.id.MAIN_MENU, false);
1485 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, false);
1486 }
1487 break;
1488 default:
1489 if (mCurrentMenuState != mMenuState) {
1490 menu.setGroupVisible(R.id.MAIN_MENU, true);
1491 menu.setGroupEnabled(R.id.MAIN_MENU, true);
1492 menu.setGroupEnabled(R.id.MAIN_SHORTCUT_MENU, true);
1493 }
Michael Kolb4bf79712011-07-14 14:18:12 -07001494 updateMenuState(getCurrentTab(), menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001495 break;
1496 }
1497 mCurrentMenuState = mMenuState;
Michael Kolb1acef692011-03-08 14:12:06 -08001498 return mUi.onPrepareOptionsMenu(menu);
Michael Kolb8233fac2010-10-26 16:08:53 -07001499 }
1500
Michael Kolb4bf79712011-07-14 14:18:12 -07001501 @Override
1502 public void updateMenuState(Tab tab, Menu menu) {
1503 boolean canGoBack = false;
1504 boolean canGoForward = false;
1505 boolean isHome = false;
John Reck42229bc2011-08-19 13:26:43 -07001506 boolean isDesktopUa = false;
John Recke1a03a32011-09-14 17:04:16 -07001507 boolean isLive = false;
Michael Kolb4bf79712011-07-14 14:18:12 -07001508 if (tab != null) {
1509 canGoBack = tab.canGoBack();
1510 canGoForward = tab.canGoForward();
1511 isHome = mSettings.getHomePage().equals(tab.getUrl());
John Reck42229bc2011-08-19 13:26:43 -07001512 isDesktopUa = mSettings.hasDesktopUseragent(tab.getWebView());
John Recke1a03a32011-09-14 17:04:16 -07001513 isLive = !tab.isSnapshot();
Michael Kolb4bf79712011-07-14 14:18:12 -07001514 }
1515 final MenuItem back = menu.findItem(R.id.back_menu_id);
1516 back.setEnabled(canGoBack);
1517
1518 final MenuItem home = menu.findItem(R.id.homepage_menu_id);
1519 home.setEnabled(!isHome);
1520
1521 final MenuItem forward = menu.findItem(R.id.forward_menu_id);
1522 forward.setEnabled(canGoForward);
1523
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001524 final MenuItem source = menu.findItem(isInLoad() ? R.id.stop_menu_id
1525 : R.id.reload_menu_id);
Michael Kolb4bf79712011-07-14 14:18:12 -07001526 final MenuItem dest = menu.findItem(R.id.stop_reload_menu_id);
Michael Kolb7ab75ee2011-07-14 16:36:38 -07001527 if (source != null && dest != null) {
1528 dest.setTitle(source.getTitle());
1529 dest.setIcon(source.getIcon());
1530 }
John Recke1a03a32011-09-14 17:04:16 -07001531 menu.setGroupVisible(R.id.NAV_MENU, isLive);
Michael Kolb4bf79712011-07-14 14:18:12 -07001532
1533 // decide whether to show the share link option
1534 PackageManager pm = mActivity.getPackageManager();
1535 Intent send = new Intent(Intent.ACTION_SEND);
1536 send.setType("text/plain");
1537 ResolveInfo ri = pm.resolveActivity(send,
1538 PackageManager.MATCH_DEFAULT_ONLY);
1539 menu.findItem(R.id.share_page_menu_id).setVisible(ri != null);
1540
1541 boolean isNavDump = mSettings.enableNavDump();
1542 final MenuItem nav = menu.findItem(R.id.dump_nav_menu_id);
1543 nav.setVisible(isNavDump);
1544 nav.setEnabled(isNavDump);
1545
1546 boolean showDebugSettings = mSettings.isDebugEnabled();
John Reck42229bc2011-08-19 13:26:43 -07001547 final MenuItem uaSwitcher = menu.findItem(R.id.ua_desktop_menu_id);
1548 uaSwitcher.setChecked(isDesktopUa);
John Recke1a03a32011-09-14 17:04:16 -07001549 menu.setGroupVisible(R.id.LIVE_MENU, isLive);
1550 menu.setGroupVisible(R.id.SNAPSHOT_MENU, !isLive);
Michael Kolb315d5022011-10-13 12:47:11 -07001551 menu.setGroupVisible(R.id.COMBO_MENU, false);
Michael Kolb4bf79712011-07-14 14:18:12 -07001552
Michael Kolb7bdee0b2011-08-01 11:55:38 -07001553 mUi.updateMenuState(tab, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -07001554 }
1555
John Reck9c35b9c2012-05-30 10:08:50 -07001556 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001557 public boolean onOptionsItemSelected(MenuItem item) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001558 if (null == getCurrentTopWebView()) {
1559 return false;
1560 }
1561 if (mMenuIsDown) {
1562 // The shortcut action consumes the MENU. Even if it is still down,
1563 // it won't trigger the next shortcut action. In the case of the
1564 // shortcut action triggering a new activity, like Bookmarks, we
1565 // won't get onKeyUp for MENU. So it is important to reset it here.
1566 mMenuIsDown = false;
1567 }
Michael Kolb3ca12752011-07-20 13:52:25 -07001568 if (mUi.onOptionsItemSelected(item)) {
1569 // ui callback handled it
1570 return true;
1571 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001572 switch (item.getItemId()) {
1573 // -- Main menu
1574 case R.id.new_tab_menu_id:
1575 openTabToHomePage();
1576 break;
1577
1578 case R.id.incognito_menu_id:
Michael Kolb519d2282011-05-09 17:03:19 -07001579 openIncognitoTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07001580 break;
1581
Afzal Najamd4e33312012-04-26 01:54:01 -04001582 case R.id.close_other_tabs_id:
1583 closeOtherTabs();
1584 break;
1585
Michael Kolb8233fac2010-10-26 16:08:53 -07001586 case R.id.goto_menu_id:
1587 editUrl();
1588 break;
1589
1590 case R.id.bookmarks_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001591 bookmarksOrHistoryPicker(ComboViews.Bookmarks);
1592 break;
1593
1594 case R.id.history_menu_id:
1595 bookmarksOrHistoryPicker(ComboViews.History);
1596 break;
1597
1598 case R.id.snapshots_menu_id:
1599 bookmarksOrHistoryPicker(ComboViews.Snapshots);
Michael Kolb8233fac2010-10-26 16:08:53 -07001600 break;
1601
Michael Kolb8233fac2010-10-26 16:08:53 -07001602 case R.id.add_bookmark_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001603 bookmarkCurrentPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001604 break;
1605
1606 case R.id.stop_reload_menu_id:
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001607 if (isInLoad()) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001608 stopLoading();
1609 } else {
1610 getCurrentTopWebView().reload();
1611 }
1612 break;
1613
1614 case R.id.back_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001615 getCurrentTab().goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07001616 break;
1617
1618 case R.id.forward_menu_id:
John Reckef654f12011-07-12 16:42:08 -07001619 getCurrentTab().goForward();
Michael Kolb8233fac2010-10-26 16:08:53 -07001620 break;
1621
1622 case R.id.close_menu_id:
1623 // Close the subwindow if it exists.
1624 if (mTabControl.getCurrentSubWindow() != null) {
1625 dismissSubWindow(mTabControl.getCurrentTab());
1626 break;
1627 }
1628 closeCurrentTab();
1629 break;
1630
1631 case R.id.homepage_menu_id:
1632 Tab current = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07001633 loadUrl(current, mSettings.getHomePage());
Michael Kolb8233fac2010-10-26 16:08:53 -07001634 break;
1635
1636 case R.id.preferences_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001637 openPreferences();
Michael Kolb8233fac2010-10-26 16:08:53 -07001638 break;
1639
1640 case R.id.find_menu_id:
Michael Kolbe11b9212012-04-13 13:39:54 -07001641 findOnPage();
Michael Kolb8233fac2010-10-26 16:08:53 -07001642 break;
1643
John Reck2bc80422011-06-30 15:11:49 -07001644 case R.id.save_snapshot_menu_id:
1645 final Tab source = getTabControl().getCurrentTab();
John Reckf33b1632011-06-04 20:00:23 -07001646 if (source == null) break;
John Reck68234a92012-04-19 15:27:12 -07001647 new SaveSnapshotTask(source).execute();
Leon Scrogginsac993842011-02-02 12:54:07 -05001648 break;
1649
Michael Kolb8233fac2010-10-26 16:08:53 -07001650 case R.id.page_info_menu_id:
Michael Kolb315d5022011-10-13 12:47:11 -07001651 showPageInfo();
Michael Kolb8233fac2010-10-26 16:08:53 -07001652 break;
1653
John Recke1a03a32011-09-14 17:04:16 -07001654 case R.id.snapshot_go_live:
1655 goLive();
1656 return true;
1657
Michael Kolb8233fac2010-10-26 16:08:53 -07001658 case R.id.share_page_menu_id:
1659 Tab currentTab = mTabControl.getCurrentTab();
1660 if (null == currentTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001661 return false;
1662 }
Michael Kolbba99c5d2010-11-29 14:57:41 -08001663 shareCurrentPage(currentTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001664 break;
1665
1666 case R.id.dump_nav_menu_id:
1667 getCurrentTopWebView().debugDump();
1668 break;
1669
Michael Kolb8233fac2010-10-26 16:08:53 -07001670 case R.id.zoom_in_menu_id:
1671 getCurrentTopWebView().zoomIn();
1672 break;
1673
1674 case R.id.zoom_out_menu_id:
1675 getCurrentTopWebView().zoomOut();
1676 break;
1677
1678 case R.id.view_downloads_menu_id:
1679 viewDownloads();
1680 break;
1681
John Reck42229bc2011-08-19 13:26:43 -07001682 case R.id.ua_desktop_menu_id:
Michael Kolb80f75082012-04-10 10:50:06 -07001683 toggleUserAgent();
John Reck42229bc2011-08-19 13:26:43 -07001684 break;
1685
Michael Kolb8233fac2010-10-26 16:08:53 -07001686 case R.id.window_one_menu_id:
1687 case R.id.window_two_menu_id:
1688 case R.id.window_three_menu_id:
1689 case R.id.window_four_menu_id:
1690 case R.id.window_five_menu_id:
1691 case R.id.window_six_menu_id:
1692 case R.id.window_seven_menu_id:
1693 case R.id.window_eight_menu_id:
1694 {
1695 int menuid = item.getItemId();
1696 for (int id = 0; id < WINDOW_SHORTCUT_ID_ARRAY.length; id++) {
1697 if (WINDOW_SHORTCUT_ID_ARRAY[id] == menuid) {
1698 Tab desiredTab = mTabControl.getTab(id);
1699 if (desiredTab != null &&
1700 desiredTab != mTabControl.getCurrentTab()) {
Michael Kolbc831b632011-05-11 09:30:34 -07001701 switchToTab(desiredTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07001702 }
1703 break;
1704 }
1705 }
1706 }
1707 break;
1708
1709 default:
1710 return false;
1711 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001712 return true;
1713 }
1714
John Reck68234a92012-04-19 15:27:12 -07001715 private class SaveSnapshotTask extends AsyncTask<Void, Void, Long>
1716 implements OnCancelListener {
1717
1718 private Tab mTab;
1719 private Dialog mProgressDialog;
1720 private ContentValues mValues;
1721
1722 private SaveSnapshotTask(Tab tab) {
1723 mTab = tab;
1724 }
1725
1726 @Override
1727 protected void onPreExecute() {
1728 CharSequence message = mActivity.getText(R.string.saving_snapshot);
1729 mProgressDialog = ProgressDialog.show(mActivity, null, message,
1730 true, true, this);
1731 mValues = mTab.createSnapshotValues();
1732 }
1733
1734 @Override
1735 protected Long doInBackground(Void... params) {
1736 if (!mTab.saveViewState(mValues)) {
1737 return null;
1738 }
1739 if (isCancelled()) {
1740 String path = mValues.getAsString(Snapshots.VIEWSTATE_PATH);
1741 File file = mActivity.getFileStreamPath(path);
1742 if (!file.delete()) {
1743 file.deleteOnExit();
1744 }
1745 return null;
1746 }
1747 final ContentResolver cr = mActivity.getContentResolver();
1748 Uri result = cr.insert(Snapshots.CONTENT_URI, mValues);
1749 if (result == null) {
1750 return null;
1751 }
1752 long id = ContentUris.parseId(result);
1753 return id;
1754 }
1755
1756 @Override
1757 protected void onPostExecute(Long id) {
1758 if (isCancelled()) {
1759 return;
1760 }
1761 mProgressDialog.dismiss();
1762 if (id == null) {
1763 Toast.makeText(mActivity, R.string.snapshot_failed,
1764 Toast.LENGTH_SHORT).show();
1765 return;
1766 }
1767 Bundle b = new Bundle();
1768 b.putLong(BrowserSnapshotPage.EXTRA_ANIMATE_ID, id);
1769 mUi.showComboView(ComboViews.Snapshots, b);
1770 }
1771
1772 @Override
1773 public void onCancel(DialogInterface dialog) {
1774 cancel(true);
1775 }
1776 }
1777
Michael Kolb80f75082012-04-10 10:50:06 -07001778 @Override
1779 public void toggleUserAgent() {
1780 WebView web = getCurrentWebView();
1781 mSettings.toggleDesktopUseragent(web);
1782 web.loadUrl(web.getOriginalUrl());
1783 }
1784
1785 @Override
1786 public void findOnPage() {
1787 getCurrentTopWebView().showFindDialog(null, true);
1788 }
1789
1790 @Override
1791 public void openPreferences() {
1792 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
1793 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE,
1794 getCurrentTopWebView().getUrl());
1795 mActivity.startActivityForResult(intent, PREFERENCES_PAGE);
1796 }
1797
1798 @Override
1799 public void bookmarkCurrentPage() {
1800 Intent bookmarkIntent = createBookmarkCurrentPageIntent(false);
1801 if (bookmarkIntent != null) {
1802 mActivity.startActivity(bookmarkIntent);
1803 }
1804 }
1805
John Recke1a03a32011-09-14 17:04:16 -07001806 private void goLive() {
1807 Tab t = getCurrentTab();
1808 t.loadUrl(t.getUrl(), null);
1809 }
1810
Michael Kolb315d5022011-10-13 12:47:11 -07001811 @Override
1812 public void showPageInfo() {
1813 mPageDialogsHandler.showPageInfo(mTabControl.getCurrentTab(), false, null);
1814 }
1815
John Reck9c35b9c2012-05-30 10:08:50 -07001816 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001817 public boolean onContextItemSelected(MenuItem item) {
John Reckdbf57df2010-11-09 16:34:03 -08001818 // Let the History and Bookmark fragments handle menus they created.
1819 if (item.getGroupId() == R.id.CONTEXT_MENU) {
1820 return false;
1821 }
1822
Michael Kolb8233fac2010-10-26 16:08:53 -07001823 int id = item.getItemId();
1824 boolean result = true;
1825 switch (id) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001826 // -- Browser context menu
1827 case R.id.open_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001828 case R.id.save_link_context_menu_id:
Michael Kolb8233fac2010-10-26 16:08:53 -07001829 case R.id.copy_link_context_menu_id:
1830 final WebView webView = getCurrentTopWebView();
1831 if (null == webView) {
1832 result = false;
1833 break;
1834 }
1835 final HashMap<String, WebView> hrefMap =
1836 new HashMap<String, WebView>();
1837 hrefMap.put("webview", webView);
1838 final Message msg = mHandler.obtainMessage(
1839 FOCUS_NODE_HREF, id, 0, hrefMap);
1840 webView.requestFocusNodeHref(msg);
1841 break;
1842
1843 default:
1844 // For other context menus
1845 result = onOptionsItemSelected(item);
1846 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001847 return result;
1848 }
1849
1850 /**
1851 * support programmatically opening the context menu
1852 */
1853 public void openContextMenu(View view) {
1854 mActivity.openContextMenu(view);
1855 }
1856
1857 /**
1858 * programmatically open the options menu
1859 */
1860 public void openOptionsMenu() {
1861 mActivity.openOptionsMenu();
1862 }
1863
John Reck9c35b9c2012-05-30 10:08:50 -07001864 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001865 public boolean onMenuOpened(int featureId, Menu menu) {
1866 if (mOptionsMenuOpen) {
1867 if (mConfigChanged) {
1868 // We do not need to make any changes to the state of the
1869 // title bar, since the only thing that happened was a
1870 // change in orientation
1871 mConfigChanged = false;
1872 } else {
1873 if (!mExtendedMenuOpen) {
1874 mExtendedMenuOpen = true;
1875 mUi.onExtendedMenuOpened();
1876 } else {
1877 // Switching the menu back to icon view, so show the
1878 // title bar once again.
1879 mExtendedMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001880 mUi.onExtendedMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001881 }
1882 }
1883 } else {
1884 // The options menu is closed, so open it, and show the title
1885 mOptionsMenuOpen = true;
1886 mConfigChanged = false;
1887 mExtendedMenuOpen = false;
1888 mUi.onOptionsMenuOpened();
1889 }
1890 return true;
1891 }
1892
John Reck9c35b9c2012-05-30 10:08:50 -07001893 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001894 public void onOptionsMenuClosed(Menu menu) {
1895 mOptionsMenuOpen = false;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001896 mUi.onOptionsMenuClosed(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001897 }
1898
John Reck9c35b9c2012-05-30 10:08:50 -07001899 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001900 public void onContextMenuClosed(Menu menu) {
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001901 mUi.onContextMenuClosed(menu, isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001902 }
1903
1904 // Helper method for getting the top window.
1905 @Override
1906 public WebView getCurrentTopWebView() {
1907 return mTabControl.getCurrentTopWebView();
1908 }
1909
1910 @Override
1911 public WebView getCurrentWebView() {
1912 return mTabControl.getCurrentWebView();
1913 }
1914
1915 /*
1916 * This method is called as a result of the user selecting the options
1917 * menu to see the download window. It shows the download window on top of
1918 * the current window.
1919 */
1920 void viewDownloads() {
1921 Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
1922 mActivity.startActivity(intent);
1923 }
1924
John Reck30b065e2011-07-19 10:58:05 -07001925 int getActionModeHeight() {
1926 TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
1927 new int[] { android.R.attr.actionBarSize });
1928 int size = (int) actionBarSizeTypedArray.getDimension(0, 0f);
1929 actionBarSizeTypedArray.recycle();
1930 return size;
1931 }
1932
Michael Kolb8233fac2010-10-26 16:08:53 -07001933 // action mode
1934
John Reck9c35b9c2012-05-30 10:08:50 -07001935 @Override
1936 public void onActionModeStarted(ActionMode mode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001937 mUi.onActionModeStarted(mode);
1938 mActionMode = mode;
1939 }
1940
1941 /*
1942 * True if a custom ActionMode (i.e. find or select) is in use.
1943 */
1944 @Override
1945 public boolean isInCustomActionMode() {
1946 return mActionMode != null;
1947 }
1948
1949 /*
1950 * End the current ActionMode.
1951 */
1952 @Override
1953 public void endActionMode() {
1954 if (mActionMode != null) {
1955 mActionMode.finish();
1956 }
1957 }
1958
1959 /*
1960 * Called by find and select when they are finished. Replace title bars
1961 * as necessary.
1962 */
John Reck9c35b9c2012-05-30 10:08:50 -07001963 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07001964 public void onActionModeFinished(ActionMode mode) {
1965 if (!isInCustomActionMode()) return;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001966 mUi.onActionModeFinished(isInLoad());
Michael Kolb8233fac2010-10-26 16:08:53 -07001967 mActionMode = null;
1968 }
1969
1970 boolean isInLoad() {
Michael Kolbc5b0b2d2011-11-29 16:13:35 -08001971 final Tab tab = getCurrentTab();
1972 return (tab != null) && tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -07001973 }
1974
1975 // bookmark handling
1976
1977 /**
1978 * add the current page as a bookmark to the given folder id
1979 * @param folderId use -1 for the default folder
John Reckd3e4d5b2011-07-13 15:48:43 -07001980 * @param editExisting If true, check to see whether the site is already
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05001981 * bookmarked, and if it is, edit that bookmark. If false, and
1982 * the site is already bookmarked, do not attempt to edit the
1983 * existing bookmark.
Michael Kolb8233fac2010-10-26 16:08:53 -07001984 */
1985 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -07001986 public Intent createBookmarkCurrentPageIntent(boolean editExisting) {
John Recka60fffa2011-09-06 16:30:29 -07001987 WebView w = getCurrentTopWebView();
1988 if (w == null) {
1989 return null;
1990 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001991 Intent i = new Intent(mActivity,
1992 AddBookmarkPage.class);
Michael Kolb8233fac2010-10-26 16:08:53 -07001993 i.putExtra(BrowserContract.Bookmarks.URL, w.getUrl());
1994 i.putExtra(BrowserContract.Bookmarks.TITLE, w.getTitle());
1995 String touchIconUrl = w.getTouchIconUrl();
1996 if (touchIconUrl != null) {
1997 i.putExtra(AddBookmarkPage.TOUCH_ICON_URL, touchIconUrl);
1998 WebSettings settings = w.getSettings();
1999 if (settings != null) {
2000 i.putExtra(AddBookmarkPage.USER_AGENT,
2001 settings.getUserAgentString());
2002 }
2003 }
2004 i.putExtra(BrowserContract.Bookmarks.THUMBNAIL,
2005 createScreenshot(w, getDesiredThumbnailWidth(mActivity),
2006 getDesiredThumbnailHeight(mActivity)));
2007 i.putExtra(BrowserContract.Bookmarks.FAVICON, w.getFavicon());
John Reckd3e4d5b2011-07-13 15:48:43 -07002008 if (editExisting) {
Leon Scrogginsbdff8a72011-02-11 15:49:04 -05002009 i.putExtra(AddBookmarkPage.CHECK_FOR_DUPE, true);
2010 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002011 // Put the dialog at the upper right of the screen, covering the
2012 // star on the title bar.
2013 i.putExtra("gravity", Gravity.RIGHT | Gravity.TOP);
John Reckd3e4d5b2011-07-13 15:48:43 -07002014 return i;
Michael Kolb8233fac2010-10-26 16:08:53 -07002015 }
2016
2017 // file chooser
John Reck9c35b9c2012-05-30 10:08:50 -07002018 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00002019 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002020 mUploadHandler = new UploadHandler(this);
Ben Murdoch8cad4132012-01-11 10:56:43 +00002021 mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
Michael Kolb8233fac2010-10-26 16:08:53 -07002022 }
2023
2024 // thumbnails
2025
2026 /**
2027 * Return the desired width for thumbnail screenshots, which are stored in
2028 * the database, and used on the bookmarks screen.
2029 * @param context Context for finding out the density of the screen.
2030 * @return desired width for thumbnail screenshot.
2031 */
2032 static int getDesiredThumbnailWidth(Context context) {
2033 return context.getResources().getDimensionPixelOffset(
2034 R.dimen.bookmarkThumbnailWidth);
2035 }
2036
2037 /**
2038 * Return the desired height for thumbnail screenshots, which are stored in
2039 * the database, and used on the bookmarks screen.
2040 * @param context Context for finding out the density of the screen.
2041 * @return desired height for thumbnail screenshot.
2042 */
2043 static int getDesiredThumbnailHeight(Context context) {
2044 return context.getResources().getDimensionPixelOffset(
2045 R.dimen.bookmarkThumbnailHeight);
2046 }
2047
John Reck8cc92352011-07-06 17:41:52 -07002048 static Bitmap createScreenshot(WebView view, int width, int height) {
John Reckd7dd9b22011-08-30 09:18:29 -07002049 if (view == null || view.getContentHeight() == 0
2050 || view.getContentWidth() == 0) {
2051 return null;
2052 }
John Reck5c6ac2f2011-01-05 10:18:03 -08002053 // We render to a bitmap 2x the desired size so that we can then
2054 // re-scale it with filtering since canvas.scale doesn't filter
2055 // This helps reduce aliasing at the cost of being slightly blurry
2056 final int filter_scale = 2;
John Reckd7dd9b22011-08-30 09:18:29 -07002057 int scaledWidth = width * filter_scale;
2058 int scaledHeight = height * filter_scale;
2059 if (sThumbnailBitmap == null || sThumbnailBitmap.getWidth() != scaledWidth
2060 || sThumbnailBitmap.getHeight() != scaledHeight) {
2061 if (sThumbnailBitmap != null) {
2062 sThumbnailBitmap.recycle();
2063 sThumbnailBitmap = null;
2064 }
2065 sThumbnailBitmap =
2066 Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.RGB_565);
Michael Kolb8233fac2010-10-26 16:08:53 -07002067 }
John Reckd7dd9b22011-08-30 09:18:29 -07002068 Canvas canvas = new Canvas(sThumbnailBitmap);
2069 int contentWidth = view.getContentWidth();
2070 float overviewScale = scaledWidth / (view.getScale() * contentWidth);
2071 if (view instanceof BrowserWebView) {
2072 int dy = -((BrowserWebView)view).getTitleHeight();
2073 canvas.translate(0, dy * overviewScale);
2074 }
2075
2076 canvas.scale(overviewScale, overviewScale);
2077
2078 if (view instanceof BrowserWebView) {
2079 ((BrowserWebView)view).drawContent(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002080 } else {
John Reckd7dd9b22011-08-30 09:18:29 -07002081 view.draw(canvas);
Michael Kolb8233fac2010-10-26 16:08:53 -07002082 }
John Reckd7dd9b22011-08-30 09:18:29 -07002083 Bitmap ret = Bitmap.createScaledBitmap(sThumbnailBitmap,
2084 width, height, true);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002085 canvas.setBitmap(null);
John Reck5c6ac2f2011-01-05 10:18:03 -08002086 return ret;
Michael Kolb8233fac2010-10-26 16:08:53 -07002087 }
2088
John Reck34ef2672011-02-10 11:30:55 -08002089 private void updateScreenshot(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002090 // If this is a bookmarked site, add a screenshot to the database.
Michael Kolb8233fac2010-10-26 16:08:53 -07002091 // FIXME: Would like to make sure there is actually something to
2092 // draw, but the API for that (WebViewCore.pictureReady()) is not
2093 // currently accessible here.
2094
John Reck34ef2672011-02-10 11:30:55 -08002095 WebView view = tab.getWebView();
John Reck7a591202011-02-16 15:44:01 -08002096 if (view == null) {
2097 // Tab was destroyed
2098 return;
2099 }
John Reck34ef2672011-02-10 11:30:55 -08002100 final String url = tab.getUrl();
2101 final String originalUrl = view.getOriginalUrl();
John Reck34ef2672011-02-10 11:30:55 -08002102 if (TextUtils.isEmpty(url)) {
2103 return;
2104 }
2105
2106 // Only update thumbnails for web urls (http(s)://), not for
2107 // about:, javascript:, data:, etc...
2108 // Unless it is a bookmarked site, then always update
2109 if (!Patterns.WEB_URL.matcher(url).matches() && !tab.isBookmarkedSite()) {
2110 return;
2111 }
2112
Michael Kolb8233fac2010-10-26 16:08:53 -07002113 final Bitmap bm = createScreenshot(view, getDesiredThumbnailWidth(mActivity),
2114 getDesiredThumbnailHeight(mActivity));
2115 if (bm == null) {
2116 return;
2117 }
2118
2119 final ContentResolver cr = mActivity.getContentResolver();
John Reck34ef2672011-02-10 11:30:55 -08002120 new AsyncTask<Void, Void, Void>() {
2121 @Override
2122 protected Void doInBackground(Void... unused) {
2123 Cursor cursor = null;
2124 try {
2125 // TODO: Clean this up
2126 cursor = Bookmarks.queryCombinedForUrl(cr, originalUrl, url);
2127 if (cursor != null && cursor.moveToFirst()) {
2128 final ByteArrayOutputStream os =
2129 new ByteArrayOutputStream();
2130 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
Michael Kolb8233fac2010-10-26 16:08:53 -07002131
John Reck34ef2672011-02-10 11:30:55 -08002132 ContentValues values = new ContentValues();
2133 values.put(Images.THUMBNAIL, os.toByteArray());
Michael Kolb8233fac2010-10-26 16:08:53 -07002134
John Reck34ef2672011-02-10 11:30:55 -08002135 do {
John Reck617fd832011-02-16 14:35:59 -08002136 values.put(Images.URL, cursor.getString(0));
John Reck34ef2672011-02-10 11:30:55 -08002137 cr.update(Images.CONTENT_URI, values, null, null);
2138 } while (cursor.moveToNext());
Michael Kolb8233fac2010-10-26 16:08:53 -07002139 }
John Reck34ef2672011-02-10 11:30:55 -08002140 } catch (IllegalStateException e) {
2141 // Ignore
Mattias Nilsson561d1952011-10-04 10:18:50 +02002142 } catch (SQLiteException s) {
2143 // Added for possible error when user tries to remove the same bookmark
2144 // that is being updated with a screen shot
2145 Log.w(LOGTAG, "Error when running updateScreenshot ", s);
John Reck34ef2672011-02-10 11:30:55 -08002146 } finally {
2147 if (cursor != null) cursor.close();
Michael Kolb8233fac2010-10-26 16:08:53 -07002148 }
John Reck34ef2672011-02-10 11:30:55 -08002149 return null;
2150 }
2151 }.execute();
Michael Kolb8233fac2010-10-26 16:08:53 -07002152 }
2153
2154 private class Copy implements OnMenuItemClickListener {
2155 private CharSequence mText;
2156
John Reck9c35b9c2012-05-30 10:08:50 -07002157 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002158 public boolean onMenuItemClick(MenuItem item) {
2159 copy(mText);
2160 return true;
2161 }
2162
2163 public Copy(CharSequence toCopy) {
2164 mText = toCopy;
2165 }
2166 }
2167
Leon Scroggins63c02662010-11-18 15:16:27 -05002168 private static class Download implements OnMenuItemClickListener {
2169 private Activity mActivity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002170 private String mText;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002171 private boolean mPrivateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002172 private String mUserAgent;
George Mount387d45d2011-10-07 15:57:53 -07002173 private static final String FALLBACK_EXTENSION = "dat";
2174 private static final String IMAGE_BASE_FORMAT = "yyyy-MM-dd-HH-mm-ss-";
Michael Kolb8233fac2010-10-26 16:08:53 -07002175
John Reck9c35b9c2012-05-30 10:08:50 -07002176 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002177 public boolean onMenuItemClick(MenuItem item) {
George Mount387d45d2011-10-07 15:57:53 -07002178 if (DataUri.isDataUri(mText)) {
2179 saveDataUri();
2180 } else {
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002181 DownloadHandler.onDownloadStartNoStream(mActivity, mText, mUserAgent,
George Mount387d45d2011-10-07 15:57:53 -07002182 null, null, mPrivateBrowsing);
2183 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002184 return true;
2185 }
2186
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002187 public Download(Activity activity, String toDownload, boolean privateBrowsing,
2188 String userAgent) {
Leon Scroggins63c02662010-11-18 15:16:27 -05002189 mActivity = activity;
Michael Kolb8233fac2010-10-26 16:08:53 -07002190 mText = toDownload;
Kristian Monsenbc5cc752011-03-02 13:14:03 +00002191 mPrivateBrowsing = privateBrowsing;
Andreas Sandblad8e4ce662012-06-11 11:02:49 +02002192 mUserAgent = userAgent;
Michael Kolb8233fac2010-10-26 16:08:53 -07002193 }
George Mount387d45d2011-10-07 15:57:53 -07002194
2195 /**
2196 * Treats mText as a data URI and writes its contents to a file
2197 * based on the current time.
2198 */
2199 private void saveDataUri() {
2200 FileOutputStream outputStream = null;
2201 try {
2202 DataUri uri = new DataUri(mText);
2203 File target = getTarget(uri);
2204 outputStream = new FileOutputStream(target);
2205 outputStream.write(uri.getData());
2206 final DownloadManager manager =
2207 (DownloadManager) mActivity.getSystemService(Context.DOWNLOAD_SERVICE);
2208 manager.addCompletedDownload(target.getName(),
2209 mActivity.getTitle().toString(), false,
2210 uri.getMimeType(), target.getAbsolutePath(),
John Reck9c35b9c2012-05-30 10:08:50 -07002211 uri.getData().length, true);
George Mount387d45d2011-10-07 15:57:53 -07002212 } catch (IOException e) {
2213 Log.e(LOGTAG, "Could not save data URL");
2214 } finally {
2215 if (outputStream != null) {
2216 try {
2217 outputStream.close();
2218 } catch (IOException e) {
2219 // ignore close errors
2220 }
2221 }
2222 }
2223 }
2224
2225 /**
2226 * Creates a File based on the current time stamp and uses
2227 * the mime type of the DataUri to get the extension.
2228 */
2229 private File getTarget(DataUri uri) throws IOException {
2230 File dir = mActivity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
2231 DateFormat format = new SimpleDateFormat(IMAGE_BASE_FORMAT);
2232 String nameBase = format.format(new Date());
2233 String mimeType = uri.getMimeType();
2234 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
2235 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
2236 if (extension == null) {
2237 Log.w(LOGTAG, "Unknown mime type in data URI" + mimeType);
2238 extension = FALLBACK_EXTENSION;
2239 }
2240 extension = "." + extension; // createTempFile needs the '.'
2241 File targetFile = File.createTempFile(nameBase, extension, dir);
2242 return targetFile;
2243 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002244 }
2245
Cary Clark8974d282010-11-22 10:46:05 -05002246 private static class SelectText implements OnMenuItemClickListener {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002247 private WebViewClassic mWebView;
Cary Clark8974d282010-11-22 10:46:05 -05002248
John Reck9c35b9c2012-05-30 10:08:50 -07002249 @Override
Cary Clark8974d282010-11-22 10:46:05 -05002250 public boolean onMenuItemClick(MenuItem item) {
2251 if (mWebView != null) {
2252 return mWebView.selectText();
2253 }
2254 return false;
2255 }
2256
2257 public SelectText(WebView webView) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002258 mWebView = WebViewClassic.fromWebView(webView);
Cary Clark8974d282010-11-22 10:46:05 -05002259 }
2260
2261 }
2262
Michael Kolb8233fac2010-10-26 16:08:53 -07002263 /********************** TODO: UI stuff *****************************/
2264
2265 // these methods have been copied, they still need to be cleaned up
2266
2267 /****************** tabs ***************************************************/
2268
2269 // basic tab interactions:
2270
2271 // it is assumed that tabcontrol already knows about the tab
2272 protected void addTab(Tab tab) {
2273 mUi.addTab(tab);
2274 }
2275
2276 protected void removeTab(Tab tab) {
2277 mUi.removeTab(tab);
2278 mTabControl.removeTab(tab);
John Reck378a4102011-06-09 16:23:01 -07002279 mCrashRecoveryHandler.backupState();
Michael Kolb8233fac2010-10-26 16:08:53 -07002280 }
2281
Michael Kolbf2055602011-04-09 17:20:03 -07002282 @Override
2283 public void setActiveTab(Tab tab) {
Michael Kolbcd424e92011-02-24 10:26:26 -08002284 // monkey protection against delayed start
2285 if (tab != null) {
2286 mTabControl.setCurrentTab(tab);
2287 // the tab is guaranteed to have a webview after setCurrentTab
2288 mUi.setActiveTab(tab);
2289 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002290 }
2291
John Reck8bcafc12011-08-29 16:43:02 -07002292 protected void closeEmptyTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -07002293 Tab current = mTabControl.getCurrentTab();
2294 if (current != null
2295 && current.getWebView().copyBackForwardList().getSize() == 0) {
John Reck8bcafc12011-08-29 16:43:02 -07002296 closeCurrentTab();
Michael Kolb8233fac2010-10-26 16:08:53 -07002297 }
2298 }
2299
John Reck26b18322011-06-21 13:08:58 -07002300 protected void reuseTab(Tab appTab, UrlData urlData) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002301 // Dismiss the subwindow if applicable.
2302 dismissSubWindow(appTab);
2303 // Since we might kill the WebView, remove it from the
2304 // content view first.
2305 mUi.detachTab(appTab);
2306 // Recreate the main WebView after destroying the old one.
John Reck30c714c2010-12-16 17:30:34 -08002307 mTabControl.recreateWebView(appTab);
Michael Kolb8233fac2010-10-26 16:08:53 -07002308 // TODO: analyze why the remove and add are necessary
2309 mUi.attachTab(appTab);
2310 if (mTabControl.getCurrentTab() != appTab) {
Michael Kolbc831b632011-05-11 09:30:34 -07002311 switchToTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002312 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002313 } else {
2314 // If the tab was the current tab, we have to attach
2315 // it to the view system again.
2316 setActiveTab(appTab);
John Reck30c714c2010-12-16 17:30:34 -08002317 loadUrlDataIn(appTab, urlData);
Michael Kolb8233fac2010-10-26 16:08:53 -07002318 }
2319 }
2320
2321 // Remove the sub window if it exists. Also called by TabControl when the
2322 // user clicks the 'X' to dismiss a sub window.
John Reck9c35b9c2012-05-30 10:08:50 -07002323 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002324 public void dismissSubWindow(Tab tab) {
2325 removeSubWindow(tab);
2326 // dismiss the subwindow. This will destroy the WebView.
2327 tab.dismissSubWindow();
Michael Kolbe8c97572011-11-21 14:03:56 -08002328 WebView wv = getCurrentTopWebView();
2329 if (wv != null) {
2330 wv.requestFocus();
2331 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002332 }
2333
2334 @Override
2335 public void removeSubWindow(Tab t) {
2336 if (t.getSubWebView() != null) {
2337 mUi.removeSubWindow(t.getSubViewContainer());
2338 }
2339 }
2340
2341 @Override
2342 public void attachSubWindow(Tab tab) {
2343 if (tab.getSubWebView() != null) {
2344 mUi.attachSubWindow(tab.getSubViewContainer());
2345 getCurrentTopWebView().requestFocus();
2346 }
2347 }
2348
Mathew Inwood29721c22011-06-29 17:55:24 +01002349 private Tab showPreloadedTab(final UrlData urlData) {
2350 if (!urlData.isPreloaded()) {
2351 return null;
2352 }
2353 final PreloadedTabControl tabControl = urlData.getPreloadedTab();
2354 final String sbQuery = urlData.getSearchBoxQueryToSubmit();
2355 if (sbQuery != null) {
Mathew Inwood9ad1eac2011-09-15 11:29:50 +01002356 if (!tabControl.searchBoxSubmit(sbQuery, urlData.mUrl, urlData.mHeaders)) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002357 // Could not submit query. Fallback to regular tab creation
2358 tabControl.destroy();
2359 return null;
2360 }
2361 }
Michael Kolbff6a7482011-07-26 16:37:15 -07002362 // check tab count and make room for new tab
2363 if (!mTabControl.canCreateNewTab()) {
2364 Tab leastUsed = mTabControl.getLeastUsedTab(getCurrentTab());
2365 if (leastUsed != null) {
2366 closeTab(leastUsed);
2367 }
2368 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002369 Tab t = tabControl.getTab();
Mathew Inwoode09305e2011-09-02 12:03:26 +01002370 t.refreshIdAfterPreload();
Mathew Inwood29721c22011-06-29 17:55:24 +01002371 mTabControl.addPreloadedTab(t);
2372 addTab(t);
2373 setActiveTab(t);
2374 return t;
2375 }
2376
Michael Kolb7bcafde2011-05-09 13:55:59 -07002377 // open a non inconito tab with the given url data
2378 // and set as active tab
2379 public Tab openTab(UrlData urlData) {
Mathew Inwood29721c22011-06-29 17:55:24 +01002380 Tab tab = showPreloadedTab(urlData);
2381 if (tab == null) {
2382 tab = createNewTab(false, true, true);
Michael Kolb14612442011-06-24 13:06:29 -07002383 if ((tab != null) && !urlData.isEmpty()) {
2384 loadUrlDataIn(tab, urlData);
2385 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002386 }
Mathew Inwood29721c22011-06-29 17:55:24 +01002387 return tab;
Michael Kolb7bcafde2011-05-09 13:55:59 -07002388 }
2389
Michael Kolb843510f2010-12-09 10:51:49 -08002390 @Override
2391 public Tab openTabToHomePage() {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002392 return openTab(mSettings.getHomePage(), false, true, false);
Michael Kolb8233fac2010-10-26 16:08:53 -07002393 }
2394
Michael Kolb8233fac2010-10-26 16:08:53 -07002395 @Override
Michael Kolb519d2282011-05-09 17:03:19 -07002396 public Tab openIncognitoTab() {
2397 return openTab(INCOGNITO_URI, true, true, false);
2398 }
2399
2400 @Override
Michael Kolb7bcafde2011-05-09 13:55:59 -07002401 public Tab openTab(String url, boolean incognito, boolean setActive,
2402 boolean useCurrent) {
John Reck5949c662011-05-27 09:52:29 -07002403 return openTab(url, incognito, setActive, useCurrent, null);
2404 }
2405
2406 @Override
2407 public Tab openTab(String url, Tab parent, boolean setActive,
2408 boolean useCurrent) {
2409 return openTab(url, (parent != null) && parent.isPrivateBrowsingEnabled(),
2410 setActive, useCurrent, parent);
2411 }
2412
2413 public Tab openTab(String url, boolean incognito, boolean setActive,
2414 boolean useCurrent, Tab parent) {
Michael Kolb7bcafde2011-05-09 13:55:59 -07002415 Tab tab = createNewTab(incognito, setActive, useCurrent);
2416 if (tab != null) {
John Reck5949c662011-05-27 09:52:29 -07002417 if (parent != null && parent != tab) {
2418 parent.addChildTab(tab);
2419 }
Michael Kolb519d2282011-05-09 17:03:19 -07002420 if (url != null) {
John Reck26b18322011-06-21 13:08:58 -07002421 loadUrl(tab, url);
Michael Kolb519d2282011-05-09 17:03:19 -07002422 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002423 }
Michael Kolb7bcafde2011-05-09 13:55:59 -07002424 return tab;
2425 }
2426
2427 // this method will attempt to create a new tab
2428 // incognito: private browsing tab
2429 // setActive: ste tab as current tab
2430 // useCurrent: if no new tab can be created, return current tab
2431 private Tab createNewTab(boolean incognito, boolean setActive,
2432 boolean useCurrent) {
2433 Tab tab = null;
2434 if (mTabControl.canCreateNewTab()) {
2435 tab = mTabControl.createNewTab(incognito);
2436 addTab(tab);
2437 if (setActive) {
2438 setActiveTab(tab);
2439 }
2440 } else {
2441 if (useCurrent) {
2442 tab = mTabControl.getCurrentTab();
John Reck26b18322011-06-21 13:08:58 -07002443 reuseTab(tab, null);
Michael Kolb7bcafde2011-05-09 13:55:59 -07002444 } else {
2445 mUi.showMaxTabsWarning();
2446 }
2447 }
2448 return tab;
Michael Kolb8233fac2010-10-26 16:08:53 -07002449 }
2450
John Reck2bc80422011-06-30 15:11:49 -07002451 @Override
2452 public SnapshotTab createNewSnapshotTab(long snapshotId, boolean setActive) {
2453 SnapshotTab tab = null;
2454 if (mTabControl.canCreateNewTab()) {
2455 tab = mTabControl.createSnapshotTab(snapshotId);
2456 addTab(tab);
2457 if (setActive) {
2458 setActiveTab(tab);
2459 }
2460 } else {
2461 mUi.showMaxTabsWarning();
John Reckd8c74522011-06-14 08:45:00 -07002462 }
2463 return tab;
2464 }
2465
Michael Kolb8233fac2010-10-26 16:08:53 -07002466 /**
Michael Kolbc831b632011-05-11 09:30:34 -07002467 * @param tab the tab to switch to
Michael Kolb8233fac2010-10-26 16:08:53 -07002468 * @return boolean True if we successfully switched to a different tab. If
2469 * the indexth tab is null, or if that tab is the same as
2470 * the current one, return false.
2471 */
2472 @Override
Michael Kolbc831b632011-05-11 09:30:34 -07002473 public boolean switchToTab(Tab tab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002474 Tab currentTab = mTabControl.getCurrentTab();
2475 if (tab == null || tab == currentTab) {
2476 return false;
2477 }
2478 setActiveTab(tab);
2479 return true;
2480 }
2481
2482 @Override
Michael Kolb8233fac2010-10-26 16:08:53 -07002483 public void closeCurrentTab() {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002484 closeCurrentTab(false);
2485 }
2486
2487 protected void closeCurrentTab(boolean andQuit) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002488 if (mTabControl.getTabCount() == 1) {
John Reckbc490d22011-07-22 15:04:59 -07002489 mCrashRecoveryHandler.clearState();
Michael Kolbc1eeb122011-08-01 09:56:26 -07002490 mTabControl.removeTab(getCurrentTab());
John Reck958b2422010-12-03 17:56:17 -08002491 mActivity.finish();
Michael Kolb8233fac2010-10-26 16:08:53 -07002492 return;
2493 }
Michael Kolbc831b632011-05-11 09:30:34 -07002494 final Tab current = mTabControl.getCurrentTab();
2495 final int pos = mTabControl.getCurrentPosition();
2496 Tab newTab = current.getParent();
2497 if (newTab == null) {
2498 newTab = mTabControl.getTab(pos + 1);
2499 if (newTab == null) {
2500 newTab = mTabControl.getTab(pos - 1);
Michael Kolb8233fac2010-10-26 16:08:53 -07002501 }
2502 }
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002503 if (andQuit) {
2504 mTabControl.setCurrentTab(newTab);
2505 closeTab(current);
2506 } else if (switchToTab(newTab)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002507 // Close window
2508 closeTab(current);
2509 }
2510 }
2511
2512 /**
2513 * Close the tab, remove its associated title bar, and adjust mTabControl's
2514 * current tab to a valid value.
2515 */
2516 @Override
2517 public void closeTab(Tab tab) {
John Reck28263772011-10-11 17:13:42 -07002518 if (tab == mTabControl.getCurrentTab()) {
2519 closeCurrentTab();
2520 } else {
2521 removeTab(tab);
2522 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002523 }
2524
Afzal Najamd4e33312012-04-26 01:54:01 -04002525 /**
2526 * Close all tabs except the current one
2527 */
2528 @Override
2529 public void closeOtherTabs() {
2530 int inactiveTabs = mTabControl.getTabCount() - 1;
2531 for (int i = inactiveTabs; i >= 0; i--) {
2532 Tab tab = mTabControl.getTab(i);
2533 if (tab != mTabControl.getCurrentTab()) {
2534 removeTab(tab);
2535 }
2536 }
2537 }
2538
Michael Kolb8233fac2010-10-26 16:08:53 -07002539 // Called when loading from context menu or LOAD_URL message
John Reck26b18322011-06-21 13:08:58 -07002540 protected void loadUrlFromContext(String url) {
2541 Tab tab = getCurrentTab();
2542 WebView view = tab != null ? tab.getWebView() : null;
Michael Kolb8233fac2010-10-26 16:08:53 -07002543 // In case the user enters nothing.
John Reck26b18322011-06-21 13:08:58 -07002544 if (url != null && url.length() != 0 && tab != null && view != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002545 url = UrlUtils.smartUrlFilter(url);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002546 if (!WebViewClassic.fromWebView(view).getWebViewClient().
2547 shouldOverrideUrlLoading(view, url)) {
John Reck26b18322011-06-21 13:08:58 -07002548 loadUrl(tab, url);
Michael Kolb8233fac2010-10-26 16:08:53 -07002549 }
2550 }
2551 }
2552
2553 /**
2554 * Load the URL into the given WebView and update the title bar
2555 * to reflect the new load. Call this instead of WebView.loadUrl
2556 * directly.
2557 * @param view The WebView used to load url.
2558 * @param url The URL to load.
2559 */
John Reck71e51422011-07-01 16:49:28 -07002560 @Override
2561 public void loadUrl(Tab tab, String url) {
John Reck26b18322011-06-21 13:08:58 -07002562 loadUrl(tab, url, null);
2563 }
2564
2565 protected void loadUrl(Tab tab, String url, Map<String, String> headers) {
2566 if (tab != null) {
2567 dismissSubWindow(tab);
2568 tab.loadUrl(url, headers);
Michael Kolba53c9892011-10-05 13:31:40 -07002569 mUi.onProgressChanged(tab);
John Reck26b18322011-06-21 13:08:58 -07002570 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002571 }
2572
2573 /**
2574 * Load UrlData into a Tab and update the title bar to reflect the new
2575 * load. Call this instead of UrlData.loadIn directly.
2576 * @param t The Tab used to load.
2577 * @param data The UrlData being loaded.
2578 */
2579 protected void loadUrlDataIn(Tab t, UrlData data) {
John Reck26b18322011-06-21 13:08:58 -07002580 if (data != null) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -07002581 if (data.isPreloaded()) {
Michael Kolb14612442011-06-24 13:06:29 -07002582 // this isn't called for preloaded tabs
John Reck26b18322011-06-21 13:08:58 -07002583 } else {
2584 loadUrl(t, data.mUrl, data.mHeaders);
2585 }
2586 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002587 }
2588
John Reck30c714c2010-12-16 17:30:34 -08002589 @Override
2590 public void onUserCanceledSsl(Tab tab) {
John Reck30c714c2010-12-16 17:30:34 -08002591 // TODO: Figure out the "right" behavior
John Reckef654f12011-07-12 16:42:08 -07002592 if (tab.canGoBack()) {
2593 tab.goBack();
John Reck30c714c2010-12-16 17:30:34 -08002594 } else {
John Reckef654f12011-07-12 16:42:08 -07002595 tab.loadUrl(mSettings.getHomePage(), null);
John Reck30c714c2010-12-16 17:30:34 -08002596 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002597 }
2598
2599 void goBackOnePageOrQuit() {
2600 Tab current = mTabControl.getCurrentTab();
2601 if (current == null) {
2602 /*
2603 * Instead of finishing the activity, simply push this to the back
2604 * of the stack and let ActivityManager to choose the foreground
2605 * activity. As BrowserActivity is singleTask, it will be always the
2606 * root of the task. So we can use either true or false for
2607 * moveTaskToBack().
2608 */
2609 mActivity.moveTaskToBack(true);
2610 return;
2611 }
John Reckef654f12011-07-12 16:42:08 -07002612 if (current.canGoBack()) {
2613 current.goBack();
Michael Kolb8233fac2010-10-26 16:08:53 -07002614 } else {
2615 // Check to see if we are closing a window that was created by
2616 // another window. If so, we switch back to that window.
Michael Kolbc831b632011-05-11 09:30:34 -07002617 Tab parent = current.getParent();
Michael Kolb8233fac2010-10-26 16:08:53 -07002618 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -07002619 switchToTab(parent);
Michael Kolb8233fac2010-10-26 16:08:53 -07002620 // Now we close the other tab
2621 closeTab(current);
2622 } else {
Michael Kolbe28b3472011-08-04 16:54:31 -07002623 if ((current.getAppId() != null) || current.closeOnBack()) {
Michael Kolb2ae6ef72011-08-22 14:49:34 -07002624 closeCurrentTab(true);
Michael Kolbe28b3472011-08-04 16:54:31 -07002625 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002626 /*
2627 * Instead of finishing the activity, simply push this to the back
2628 * of the stack and let ActivityManager to choose the foreground
2629 * activity. As BrowserActivity is singleTask, it will be always the
2630 * root of the task. So we can use either true or false for
2631 * moveTaskToBack().
2632 */
2633 mActivity.moveTaskToBack(true);
2634 }
2635 }
2636 }
2637
2638 /**
Michael Kolb0035fad2011-03-14 13:25:28 -07002639 * helper method for key handler
2640 * returns the current tab if it can't advance
2641 */
Michael Kolbc831b632011-05-11 09:30:34 -07002642 private Tab getNextTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002643 int pos = mTabControl.getCurrentPosition() + 1;
2644 if (pos >= mTabControl.getTabCount()) {
2645 pos = 0;
2646 }
2647 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002648 }
2649
2650 /**
2651 * helper method for key handler
2652 * returns the current tab if it can't advance
2653 */
Michael Kolbc831b632011-05-11 09:30:34 -07002654 private Tab getPrevTab() {
Michael Kolbf5261da2011-12-15 15:07:35 -08002655 int pos = mTabControl.getCurrentPosition() - 1;
2656 if ( pos < 0) {
2657 pos = mTabControl.getTabCount() - 1;
2658 }
2659 return mTabControl.getTab(pos);
Michael Kolb0035fad2011-03-14 13:25:28 -07002660 }
2661
John Reckbcef87f2012-02-03 14:58:34 -08002662 boolean isMenuOrCtrlKey(int keyCode) {
2663 return (KeyEvent.KEYCODE_MENU == keyCode)
2664 || (KeyEvent.KEYCODE_CTRL_LEFT == keyCode)
2665 || (KeyEvent.KEYCODE_CTRL_RIGHT == keyCode);
2666 }
2667
Michael Kolb0035fad2011-03-14 13:25:28 -07002668 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07002669 * handle key events in browser
2670 *
2671 * @param keyCode
2672 * @param event
2673 * @return true if handled, false to pass to super
2674 */
John Reck9c35b9c2012-05-30 10:08:50 -07002675 @Override
2676 public boolean onKeyDown(int keyCode, KeyEvent event) {
Cary Clark160bbb92011-01-10 11:17:07 -05002677 boolean noModifiers = event.hasNoModifiers();
Michael Kolb8233fac2010-10-26 16:08:53 -07002678 // Even if MENU is already held down, we need to call to super to open
2679 // the IME on long press.
John Reckbcef87f2012-02-03 14:58:34 -08002680 if (!noModifiers && isMenuOrCtrlKey(keyCode)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002681 mMenuIsDown = true;
2682 return false;
2683 }
Michael Kolb8233fac2010-10-26 16:08:53 -07002684
Cary Clark8ff8c662010-12-29 15:03:05 -05002685 WebView webView = getCurrentTopWebView();
John Reckef654f12011-07-12 16:42:08 -07002686 Tab tab = getCurrentTab();
2687 if (webView == null || tab == null) return false;
Cary Clark8ff8c662010-12-29 15:03:05 -05002688
Cary Clark160bbb92011-01-10 11:17:07 -05002689 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
2690 boolean shift = event.hasModifiers(KeyEvent.META_SHIFT_ON);
Cary Clark8ff8c662010-12-29 15:03:05 -05002691
Michael Kolb8233fac2010-10-26 16:08:53 -07002692 switch(keyCode) {
Michael Kolb0035fad2011-03-14 13:25:28 -07002693 case KeyEvent.KEYCODE_TAB:
2694 if (event.isCtrlPressed()) {
2695 if (event.isShiftPressed()) {
2696 // prev tab
Michael Kolbc831b632011-05-11 09:30:34 -07002697 switchToTab(getPrevTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002698 } else {
2699 // next tab
Michael Kolbc831b632011-05-11 09:30:34 -07002700 switchToTab(getNextTab());
Michael Kolb0035fad2011-03-14 13:25:28 -07002701 }
2702 return true;
2703 }
2704 break;
Michael Kolb8233fac2010-10-26 16:08:53 -07002705 case KeyEvent.KEYCODE_SPACE:
2706 // WebView/WebTextView handle the keys in the KeyDown. As
2707 // the Activity's shortcut keys are only handled when WebView
2708 // doesn't, have to do it in onKeyDown instead of onKeyUp.
Cary Clark160bbb92011-01-10 11:17:07 -05002709 if (shift) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002710 pageUp();
Cary Clark160bbb92011-01-10 11:17:07 -05002711 } else if (noModifiers) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002712 pageDown();
2713 }
2714 return true;
2715 case KeyEvent.KEYCODE_BACK:
Cary Clark160bbb92011-01-10 11:17:07 -05002716 if (!noModifiers) break;
John Recke6bf4ab2011-02-24 15:48:05 -08002717 event.startTracking();
2718 return true;
Michael Kolbe9e1d4a2011-07-14 15:02:17 -07002719 case KeyEvent.KEYCODE_FORWARD:
2720 if (!noModifiers) break;
2721 tab.goForward();
2722 return true;
Cary Clark8ff8c662010-12-29 15:03:05 -05002723 case KeyEvent.KEYCODE_DPAD_LEFT:
2724 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002725 tab.goBack();
Cary Clark8ff8c662010-12-29 15:03:05 -05002726 return true;
2727 }
2728 break;
2729 case KeyEvent.KEYCODE_DPAD_RIGHT:
2730 if (ctrl) {
John Reckef654f12011-07-12 16:42:08 -07002731 tab.goForward();
Cary Clark8ff8c662010-12-29 15:03:05 -05002732 return true;
2733 }
2734 break;
2735 case KeyEvent.KEYCODE_A:
2736 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002737 WebViewClassic.fromWebView(webView).selectAll();
Cary Clark8ff8c662010-12-29 15:03:05 -05002738 return true;
2739 }
2740 break;
Michael Kolba4183062011-01-16 10:43:21 -08002741// case KeyEvent.KEYCODE_B: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002742 case KeyEvent.KEYCODE_C:
2743 if (ctrl) {
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002744 WebViewClassic.fromWebView(webView).copySelection();
Cary Clark8ff8c662010-12-29 15:03:05 -05002745 return true;
2746 }
2747 break;
Michael Kolba4183062011-01-16 10:43:21 -08002748// case KeyEvent.KEYCODE_D: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002749// case KeyEvent.KEYCODE_E: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002750// case KeyEvent.KEYCODE_F: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002751// case KeyEvent.KEYCODE_G: // in Chrome: finds next match
Michael Kolba4183062011-01-16 10:43:21 -08002752// case KeyEvent.KEYCODE_H: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002753// case KeyEvent.KEYCODE_I: // unused
Michael Kolba4183062011-01-16 10:43:21 -08002754// case KeyEvent.KEYCODE_J: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002755// case KeyEvent.KEYCODE_K: // in Chrome: puts '?' in URL bar
Michael Kolba4183062011-01-16 10:43:21 -08002756// case KeyEvent.KEYCODE_L: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002757// case KeyEvent.KEYCODE_M: // unused
2758// case KeyEvent.KEYCODE_N: // in Chrome: new window
2759// case KeyEvent.KEYCODE_O: // in Chrome: open file
2760// case KeyEvent.KEYCODE_P: // in Chrome: print page
2761// case KeyEvent.KEYCODE_Q: // unused
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002762// case KeyEvent.KEYCODE_R:
Cary Clark8ff8c662010-12-29 15:03:05 -05002763// case KeyEvent.KEYCODE_S: // in Chrome: saves page
2764 case KeyEvent.KEYCODE_T:
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002765 // we can't use the ctrl/shift flags, they check for
2766 // exclusive use of a modifier
2767 if (event.isCtrlPressed()) {
Cary Clark8ff8c662010-12-29 15:03:05 -05002768 if (event.isShiftPressed()) {
Michael Kolb519d2282011-05-09 17:03:19 -07002769 openIncognitoTab();
Cary Clark8ff8c662010-12-29 15:03:05 -05002770 } else {
2771 openTabToHomePage();
2772 }
2773 return true;
2774 }
2775 break;
2776// case KeyEvent.KEYCODE_U: // in Chrome: opens source of page
2777// case KeyEvent.KEYCODE_V: // text view intercepts to paste
Michael Kolb5ae15bd2011-08-16 17:09:27 -07002778// case KeyEvent.KEYCODE_W: // menu
Cary Clark8ff8c662010-12-29 15:03:05 -05002779// case KeyEvent.KEYCODE_X: // text view intercepts to cut
2780// case KeyEvent.KEYCODE_Y: // unused
2781// case KeyEvent.KEYCODE_Z: // unused
Michael Kolb8233fac2010-10-26 16:08:53 -07002782 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -08002783 // it is a regular key and webview is not null
2784 return mUi.dispatchKey(keyCode, event);
Michael Kolb8233fac2010-10-26 16:08:53 -07002785 }
2786
John Reck9c35b9c2012-05-30 10:08:50 -07002787 @Override
2788 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
John Recke6bf4ab2011-02-24 15:48:05 -08002789 switch(keyCode) {
2790 case KeyEvent.KEYCODE_BACK:
John Reck3ba45532011-08-11 16:26:53 -07002791 if (mUi.isWebShowing()) {
Michael Kolb315d5022011-10-13 12:47:11 -07002792 bookmarksOrHistoryPicker(ComboViews.History);
John Recke6bf4ab2011-02-24 15:48:05 -08002793 return true;
2794 }
2795 break;
2796 }
2797 return false;
2798 }
2799
John Reck9c35b9c2012-05-30 10:08:50 -07002800 @Override
2801 public boolean onKeyUp(int keyCode, KeyEvent event) {
John Reckbcef87f2012-02-03 14:58:34 -08002802 if (isMenuOrCtrlKey(keyCode)) {
Michael Kolb2814a362011-05-19 15:49:41 -07002803 mMenuIsDown = false;
John Reckbcef87f2012-02-03 14:58:34 -08002804 if (KeyEvent.KEYCODE_MENU == keyCode
2805 && event.isTracking() && !event.isCanceled()) {
Michael Kolb4bd767d2011-05-27 11:33:55 -07002806 return onMenuKey();
Michael Kolb2814a362011-05-19 15:49:41 -07002807 }
2808 }
Cary Clark160bbb92011-01-10 11:17:07 -05002809 if (!event.hasNoModifiers()) return false;
Michael Kolb8233fac2010-10-26 16:08:53 -07002810 switch(keyCode) {
Michael Kolb8233fac2010-10-26 16:08:53 -07002811 case KeyEvent.KEYCODE_BACK:
2812 if (event.isTracking() && !event.isCanceled()) {
2813 onBackKey();
2814 return true;
2815 }
2816 break;
2817 }
2818 return false;
2819 }
2820
2821 public boolean isMenuDown() {
2822 return mMenuIsDown;
2823 }
2824
John Reck9c35b9c2012-05-30 10:08:50 -07002825 @Override
Ben Murdoch8029a772010-11-16 11:58:21 +00002826 public void setupAutoFill(Message message) {
2827 // Open the settings activity at the AutoFill profile fragment so that
2828 // the user can create a new profile. When they return, we will dispatch
2829 // the message so that we can autofill the form using their new profile.
2830 Intent intent = new Intent(mActivity, BrowserPreferencesPage.class);
2831 intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT,
2832 AutoFillSettingsFragment.class.getName());
2833 mAutoFillSetupMessage = message;
2834 mActivity.startActivityForResult(intent, AUTOFILL_SETUP);
2835 }
John Reckb3417f02011-01-14 11:01:05 -08002836
John Reck9c35b9c2012-05-30 10:08:50 -07002837 @Override
Michael Kolbfbc579a2011-07-07 15:59:33 -07002838 public boolean onSearchRequested() {
Michael Kolb1f9b3562012-04-24 14:38:34 -07002839 mUi.editUrl(false, true);
Michael Kolbfbc579a2011-07-07 15:59:33 -07002840 return true;
2841 }
2842
John Reck1cf4b792011-07-26 10:22:22 -07002843 @Override
2844 public boolean shouldCaptureThumbnails() {
2845 return mUi.shouldCaptureThumbnails();
2846 }
2847
Michael Kolbc3af0672011-08-09 10:24:41 -07002848 @Override
Michael Kolb0b129122012-06-04 16:31:58 -07002849 public boolean supportsVoice() {
2850 PackageManager pm = mActivity.getPackageManager();
2851 List activities = pm.queryIntentActivities(new Intent(
2852 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
2853 return activities.size() != 0;
2854 }
2855
2856 @Override
2857 public void startVoiceRecognizer() {
2858 Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
2859 voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
2860 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
2861 voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
2862 mActivity.startActivityForResult(voice, VOICE_RESULT);
2863 }
2864
2865 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002866 public void setBlockEvents(boolean block) {
2867 mBlockEvents = block;
2868 }
2869
John Reck9c35b9c2012-05-30 10:08:50 -07002870 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002871 public boolean dispatchKeyEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002872 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002873 }
2874
John Reck9c35b9c2012-05-30 10:08:50 -07002875 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002876 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
Michael Kolb87357642011-08-24 14:19:18 -07002877 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002878 }
2879
John Reck9c35b9c2012-05-30 10:08:50 -07002880 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002881 public boolean dispatchTouchEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002882 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002883 }
2884
John Reck9c35b9c2012-05-30 10:08:50 -07002885 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002886 public boolean dispatchTrackballEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002887 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002888 }
2889
John Reck9c35b9c2012-05-30 10:08:50 -07002890 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -07002891 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
Michael Kolb87357642011-08-24 14:19:18 -07002892 return mBlockEvents;
Michael Kolbc3af0672011-08-09 10:24:41 -07002893 }
2894
Michael Kolb8233fac2010-10-26 16:08:53 -07002895}